{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Reading and Viewing MRI slices without pydicom","metadata":{}},{"cell_type":"markdown","source":"##  YES it is possible ","metadata":{}},{"cell_type":"markdown","source":"Credits :\n\nDecode DICOM files for medical imaging : https://www.tensorflow.org/io/tutorials/dicom\n\nEfficientnet3D with one MRI type : https://www.kaggle.com/rluethy/efficientnet3d-with-one-mri-type","metadata":{}},{"cell_type":"markdown","source":"Here I share my code for MRI reading and viz within TF\n\nit could be a good alternative solution other than the pydicom package \n\nfeel free to comment and to make changes..","metadata":{}},{"cell_type":"markdown","source":"First install tenserflow_io","metadata":{}},{"cell_type":"code","source":"pip install -q tensorflow-io","metadata":{"execution":{"iopub.status.busy":"2021-08-04T18:27:29.728589Z","iopub.execute_input":"2021-08-04T18:27:29.729011Z","iopub.status.idle":"2021-08-04T18:27:37.300136Z","shell.execute_reply.started":"2021-08-04T18:27:29.728979Z","shell.execute_reply":"2021-08-04T18:27:37.298583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport numpy as np\nimport tensorflow as tf\nimport tensorflow_io as tfio","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-08-04T18:27:37.302398Z","iopub.execute_input":"2021-08-04T18:27:37.30273Z","iopub.status.idle":"2021-08-04T18:27:37.308104Z","shell.execute_reply.started":"2021-08-04T18:27:37.302695Z","shell.execute_reply":"2021-08-04T18:27:37.306818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import glob","metadata":{"execution":{"iopub.status.busy":"2021-08-04T18:27:37.310265Z","iopub.execute_input":"2021-08-04T18:27:37.310753Z","iopub.status.idle":"2021-08-04T18:27:37.32225Z","shell.execute_reply.started":"2021-08-04T18:27:37.310701Z","shell.execute_reply":"2021-08-04T18:27:37.321167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"we test the viz with the patient with the id 0000","metadata":{}},{"cell_type":"code","source":"data_directory = '../input/rsna-miccai-brain-tumor-radiogenomic-classification'\nscan_id = \"00000\"\nsplit = \"train\"\nmri_type = 'T2w'","metadata":{"execution":{"iopub.status.busy":"2021-08-04T18:27:37.324086Z","iopub.execute_input":"2021-08-04T18:27:37.324544Z","iopub.status.idle":"2021-08-04T18:27:37.337869Z","shell.execute_reply.started":"2021-08-04T18:27:37.324491Z","shell.execute_reply":"2021-08-04T18:27:37.336514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"we store the list of slices ","metadata":{}},{"cell_type":"code","source":"slices = sorted(glob.glob(f\"{data_directory}/{split}/{scan_id}/{mri_type}/*.dcm\"))","metadata":{"execution":{"iopub.status.busy":"2021-08-04T18:27:37.339538Z","iopub.execute_input":"2021-08-04T18:27:37.339983Z","iopub.status.idle":"2021-08-04T18:27:37.365049Z","shell.execute_reply.started":"2021-08-04T18:27:37.339938Z","shell.execute_reply":"2021-08-04T18:27:37.363747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lossy_image = []\nfor i in slices : \n    image_bytes_i = tf.io.read_file(i)\n    image_bytes_i.shape\n    skipped_i = tfio.image.decode_dicom_image(image_bytes_i, on_error='skip', dtype=tf.uint8)\n    lossy_image_i = tfio.image.decode_dicom_image(image_bytes_i, scale='auto', on_error='lossy', dtype=tf.uint8)\n    lossy_image.append(lossy_image_i)\nprint(lossy_image_i.shape)","metadata":{"execution":{"iopub.status.busy":"2021-08-04T18:27:37.366918Z","iopub.execute_input":"2021-08-04T18:27:37.367529Z","iopub.status.idle":"2021-08-04T18:27:44.844301Z","shell.execute_reply.started":"2021-08-04T18:27:37.367488Z","shell.execute_reply":"2021-08-04T18:27:44.84294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(lossy_image)","metadata":{"execution":{"iopub.status.busy":"2021-08-04T18:27:44.845775Z","iopub.execute_input":"2021-08-04T18:27:44.846107Z","iopub.status.idle":"2021-08-04T18:27:44.85358Z","shell.execute_reply.started":"2021-08-04T18:27:44.846068Z","shell.execute_reply":"2021-08-04T18:27:44.852314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"we have here 408 slice in the MRI folder","metadata":{}},{"cell_type":"markdown","source":"## viz of the slice 150","metadata":{}},{"cell_type":"code","source":"plt.imshow(np.squeeze(lossy_image[150].numpy()), cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2021-08-04T18:27:44.85624Z","iopub.execute_input":"2021-08-04T18:27:44.856683Z","iopub.status.idle":"2021-08-04T18:27:45.088866Z","shell.execute_reply.started":"2021-08-04T18:27:44.856609Z","shell.execute_reply":"2021-08-04T18:27:45.088013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## viz of the slice 230","metadata":{}},{"cell_type":"code","source":"plt.imshow(np.squeeze(lossy_image[230].numpy()), cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2021-08-04T18:27:45.090452Z","iopub.execute_input":"2021-08-04T18:27:45.091004Z","iopub.status.idle":"2021-08-04T18:27:45.296513Z","shell.execute_reply.started":"2021-08-04T18:27:45.090968Z","shell.execute_reply":"2021-08-04T18:27:45.295607Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"data_directory = '../input/rsna-miccai-brain-tumor-radiogenomic-classification'\nscan_id = \"00044\"\nsplit = \"train\"\nmri_type = 'FLAIR'\nslices = sorted(glob.glob(f\"{data_directory}/{split}/{scan_id}/{mri_type}/*.dcm\"))\nlossy_image = []\nfor i in slices : \n    image_bytes_i = tf.io.read_file(i)\n    image_bytes_i.shape\n    skipped_i = tfio.image.decode_dicom_image(image_bytes_i, on_error='skip', dtype=tf.uint8)\n    lossy_image_i = tfio.image.decode_dicom_image(image_bytes_i, scale='auto', on_error='lossy', dtype=tf.uint8)\n    lossy_image.append(lossy_image_i)","metadata":{"execution":{"iopub.status.busy":"2021-08-04T18:27:45.297795Z","iopub.execute_input":"2021-08-04T18:27:45.29829Z","iopub.status.idle":"2021-08-04T18:27:53.091525Z","shell.execute_reply.started":"2021-08-04T18:27:45.298257Z","shell.execute_reply":"2021-08-04T18:27:53.09027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(lossy_image)","metadata":{"execution":{"iopub.status.busy":"2021-08-04T18:27:53.092851Z","iopub.execute_input":"2021-08-04T18:27:53.093174Z","iopub.status.idle":"2021-08-04T18:27:53.099586Z","shell.execute_reply.started":"2021-08-04T18:27:53.093145Z","shell.execute_reply":"2021-08-04T18:27:53.098329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(np.squeeze(lossy_image[150].numpy()), cmap='gray')","metadata":{"execution":{"iopub.status.busy":"2021-08-04T18:27:53.101176Z","iopub.execute_input":"2021-08-04T18:27:53.101717Z","iopub.status.idle":"2021-08-04T18:27:53.317579Z","shell.execute_reply.started":"2021-08-04T18:27:53.101669Z","shell.execute_reply":"2021-08-04T18:27:53.316582Z"},"trusted":true},"execution_count":null,"outputs":[]}]}