{"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":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\n#for dirname, _, filenames in os.walk('/kaggle/input'):\n#    for filename in filenames:\n#        print(os.path.join(dirname, filename))\n\nimport cv2\nimport os\nimport pydicom\nimport matplotlib.pyplot as plt\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-07-16T13:52:45.861415Z","iopub.execute_input":"2021-07-16T13:52:45.861733Z","iopub.status.idle":"2021-07-16T13:52:45.997005Z","shell.execute_reply.started":"2021-07-16T13:52:45.861708Z","shell.execute_reply":"2021-07-16T13:52:45.996334Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imlist = []\n\nfor dirname, _, filenames in os.walk('../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00000'):\n    for filename in filenames:\n        imlist.append(os.path.join(dirname, filename))","metadata":{"execution":{"iopub.status.busy":"2021-07-15T16:15:30.754178Z","iopub.execute_input":"2021-07-15T16:15:30.75449Z","iopub.status.idle":"2021-07-15T16:15:30.966311Z","shell.execute_reply.started":"2021-07-15T16:15:30.754461Z","shell.execute_reply":"2021-07-15T16:15:30.965222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def convert_from_dicom_to_png(img,filename):\n    \n    if np.max(img) == 0:\n        \n        new = img\n    \n    else:\n        new = img/np.max(img)*255\n    \n\n    cv2.imwrite(filename,new) ","metadata":{"execution":{"iopub.status.busy":"2021-07-15T16:15:30.967707Z","iopub.execute_input":"2021-07-15T16:15:30.967975Z","iopub.status.idle":"2021-07-15T16:15:30.973392Z","shell.execute_reply.started":"2021-07-15T16:15:30.967948Z","shell.execute_reply":"2021-07-15T16:15:30.972303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mpMRI = ['FLAIR','T1w', 'T2w','T1wCE']\n\ninputdir = '../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00000/'\noutdir = './'\n#os.mkdir(outdir)\n\n#test_list = [ f for f in  os.listdir(inputdir)]\n\n#test_list[0]\n\ndirs = [inputdir + mp + '/' for mp in mpMRI ]","metadata":{"execution":{"iopub.status.busy":"2021-07-16T13:52:14.414688Z","iopub.execute_input":"2021-07-16T13:52:14.415024Z","iopub.status.idle":"2021-07-16T13:52:14.419252Z","shell.execute_reply.started":"2021-07-16T13:52:14.414998Z","shell.execute_reply":"2021-07-16T13:52:14.418572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#plt.subplots(nrows = 10, ncols = 2, figsize=(50,50))\n\nfor directory in dirs:\n    \n    test_list = [ f for f in  os.listdir(directory)]\n    \n    #test_list\n\n    for f in test_list[:3]:   # remove \"[:10]\" to convert all images \n        ds = pydicom.read_file(directory + f) # read dicom image\n        img = ds.pixel_array # get image array\n        print(ds)\n        print('---------------------')\n        print('-----DIRECTORY------')\n        print(directory)\n        print('---------------------')\n        print('-----FILE NAME-------')\n        print(inputdir + f)\n        #cv2.imwrite(outdir + f.replace('.dcm','.png'),img) # write png image\n        convert_from_dicom_to_png(img,outdir + f.replace('.dcm','.png'))\n        plt.imshow(img,cmap=plt.cm.gray)\n        plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-14T11:55:28.241736Z","iopub.execute_input":"2021-07-14T11:55:28.242114Z","iopub.status.idle":"2021-07-14T11:55:30.404792Z","shell.execute_reply.started":"2021-07-14T11:55:28.242081Z","shell.execute_reply":"2021-07-14T11:55:30.403456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Playing around with pydicom","metadata":{}},{"cell_type":"markdown","source":"Playing round with the data I hope to find some solution to normalize the differnt Planes.\n\nSee here:\n\nhttps://www.kaggle.com/davidbroberts/determining-mr-image-planes","metadata":{}},{"cell_type":"code","source":"import pydicom\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport sys\nimport glob\n\n# load the DICOM files\n# Code was adapted from here https://pydicom.github.io/pydicom/stable/auto_examples/image_processing/reslice.html\n\ninputdir = '../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00009/T1w'\n\nfiles = [ pydicom.dcmread(inputdir + '/' + f) for f in  os.listdir(inputdir)]\n\n#test_list\n\nprint(\"file count: {}\".format(len(test_list)))\n\n# skip files with no SliceLocation (eg scout views)\nslices = []\nskipcount = 0\nfor f in files:\n    if hasattr(f, 'SliceLocation'):\n        slices.append(f)\n    else:\n        skipcount = skipcount + 1\n\nprint(\"skipped, no SliceLocation: {}\".format(skipcount))\n\n# ensure they are in the correct order\nslices = sorted(slices, key=lambda s: s.SliceLocation)\n\n# pixel aspects, assuming all slices are the same\nps = slices[0].PixelSpacing\nss = slices[0].SliceThickness\nax_aspect = ps[1]/ps[0]\nsag_aspect = ps[1]/ss\ncor_aspect = ss/ps[0]\n\n# create 3D array\nimg_shape = list(slices[0].pixel_array.shape)\nimg_shape.append(len(slices))\nimg3d = np.zeros(img_shape)\n\n# fill 3D array with the images from the files\nfor i, s in enumerate(slices):\n    img2d = s.pixel_array\n    img3d[:, :, i] = img2d\n\n# plot 3 orthogonal slices\na1 = plt.subplot(2, 2, 1)\nplt.imshow(img3d[:, :, img_shape[2]//2])\na1.set_aspect(ax_aspect)\n\na2 = plt.subplot(2, 2, 2)\nplt.imshow(img3d[:, img_shape[1]//2, :])\na2.set_aspect(sag_aspect)\n\na3 = plt.subplot(2, 2, 3)\nplt.imshow(img3d[img_shape[0]//2, :, :].T)\na3.set_aspect(cor_aspect)\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-16T14:19:43.838814Z","iopub.execute_input":"2021-07-16T14:19:43.839336Z","iopub.status.idle":"2021-07-16T14:19:44.607654Z","shell.execute_reply.started":"2021-07-16T14:19:43.839288Z","shell.execute_reply":"2021-07-16T14:19:44.606634Z"},"trusted":true},"execution_count":null,"outputs":[]}]}