{"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\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\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","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#import libraries\nimport os\nimport json\nimport glob\nimport random\nimport collections\nimport tqdm\n\nimport numpy as np\nimport pandas as pd\nimport pydicom\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\nimport cv2\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2022-04-25T09:44:15.032929Z","iopub.execute_input":"2022-04-25T09:44:15.033742Z","iopub.status.idle":"2022-04-25T09:44:16.615332Z","shell.execute_reply.started":"2022-04-25T09:44:15.033612Z","shell.execute_reply":"2022-04-25T09:44:16.614466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_sub = pd.read_csv(\"../input/rsna-miccai-brain-tumor-radiogenomic-classification/sample_submission.csv\")\nlen(sample_sub)\n##test data folder also had of 87 records ","metadata":{"execution":{"iopub.status.busy":"2022-04-25T04:37:57.479611Z","iopub.execute_input":"2022-04-25T04:37:57.47995Z","iopub.status.idle":"2022-04-25T04:37:57.500851Z","shell.execute_reply.started":"2022-04-25T04:37:57.479916Z","shell.execute_reply":"2022-04-25T04:37:57.499912Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(\"../input/rsna-miccai-brain-tumor-radiogenomic-classification/train_labels.csv\")\ntrain_df","metadata":{"execution":{"iopub.status.busy":"2022-04-25T04:38:04.942039Z","iopub.execute_input":"2022-04-25T04:38:04.943345Z","iopub.status.idle":"2022-04-25T04:38:04.966685Z","shell.execute_reply.started":"2022-04-25T04:38:04.943286Z","shell.execute_reply":"2022-04-25T04:38:04.96566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.MGMT_value.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-04-25T04:38:12.406317Z","iopub.execute_input":"2022-04-25T04:38:12.406757Z","iopub.status.idle":"2022-04-25T04:38:12.421703Z","shell.execute_reply.started":"2022-04-25T04:38:12.406715Z","shell.execute_reply":"2022-04-25T04:38:12.420935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(5, 5))\nsns.countplot(data=train_df, x=\"MGMT_value\");\n# plt.figure(figsize=(5, 5))\n# sns.countplot(data=sample_sub, x=\"MGMT_value\");","metadata":{"execution":{"iopub.status.busy":"2022-04-25T04:38:20.851064Z","iopub.execute_input":"2022-04-25T04:38:20.851624Z","iopub.status.idle":"2022-04-25T04:38:21.027552Z","shell.execute_reply.started":"2022-04-25T04:38:20.851591Z","shell.execute_reply":"2022-04-25T04:38:21.026948Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_dicom(path):\n    dicom = pydicom.read_file(path)\n    data = dicom.pixel_array\n    data = data - np.min(data)\n    if np.max(data) != 0:\n        data = data / np.max(data)\n    data = (data * 255).astype(np.uint8)\n    return data\n\n\ndef visualize_sample(\n    brats21id,\n    #the slice represents the way we slice the brain the first images represents a small slice \n    #the bigger slice_i the bigger the slice(front of the brain)\n    slice_i,\n    mgmt_value,\n    types=(\"FLAIR\", \"T1w\", \"T1wCE\", \"T2w\")\n):\n    #create a 16x16 figure with 5 inch space\n    plt.figure(figsize=(16,5))\n    patient_path = os.path.join(\n        \"../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/\", \n        str(brats21id).zfill(5), \n        \n    #the id in our csv file is 0 but 00000 in our directory that's why we use zfill\n    )\n    print(patient_path)\n    for i, t in enumerate(types, 1):\n        #a sorted list of each subject's images\n        t_paths = sorted(\n            #glob used to return all file paths that match a specific pattern.\n            glob.glob(os.path.join(patient_path, t, \"*\")), \n            key=lambda x: int(x[:-4].split(\"-\")[-1])\n            \n        )\n        data = load_dicom(t_paths[int(len(t_paths) * slice_i)])\n        #print(data.max())\n        plt.subplot(1, 4, i)\n        plt.imshow(data, cmap=\"gray\")\n        plt.title(f\"{t}\", fontsize=16)\n        plt.axis(\"off\")\n\n    plt.suptitle(f\"MGMT_value: {mgmt_value}\", fontsize=16)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-04-25T04:38:37.919348Z","iopub.execute_input":"2022-04-25T04:38:37.919634Z","iopub.status.idle":"2022-04-25T04:38:37.932948Z","shell.execute_reply.started":"2022-04-25T04:38:37.919595Z","shell.execute_reply":"2022-04-25T04:38:37.931997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range((train_df.shape[0])):\n    print(\"***********************************************************\")\n    _brats21id = train_df.iloc[i][\"BraTS21ID\"]\n    _mgmt_value = train_df.iloc[i][\"MGMT_value\"]\n    visualize_sample(brats21id=_brats21id, mgmt_value=_mgmt_value, slice_i=0.5)\n    visualize_sample(brats21id=_brats21id, mgmt_value=_mgmt_value, slice_i=0.2)\n    visualize_sample(brats21id=_brats21id, mgmt_value=_mgmt_value, slice_i=0.3)","metadata":{"execution":{"iopub.status.busy":"2022-04-22T14:03:17.180303Z","iopub.execute_input":"2022-04-22T14:03:17.180891Z","iopub.status.idle":"2022-04-22T14:03:37.137758Z","shell.execute_reply.started":"2022-04-22T14:03:17.180847Z","shell.execute_reply":"2022-04-22T14:03:37.136413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Converting DCM files to niff","metadata":{}},{"cell_type":"code","source":"pip install dicom2nifti","metadata":{"execution":{"iopub.status.busy":"2022-04-25T09:45:20.518262Z","iopub.execute_input":"2022-04-25T09:45:20.51854Z","iopub.status.idle":"2022-04-25T09:45:34.735029Z","shell.execute_reply.started":"2022-04-25T09:45:20.51851Z","shell.execute_reply":"2022-04-25T09:45:34.733982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import dicom2nifti","metadata":{"execution":{"iopub.status.busy":"2022-04-25T07:08:27.009889Z","iopub.execute_input":"2022-04-25T07:08:27.010256Z","iopub.status.idle":"2022-04-25T07:08:27.015976Z","shell.execute_reply.started":"2022-04-25T07:08:27.010221Z","shell.execute_reply":"2022-04-25T07:08:27.014881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"patient_path = os.path.join(\n        \"../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/\")","metadata":{"execution":{"iopub.status.busy":"2022-04-25T10:29:20.049026Z","iopub.execute_input":"2022-04-25T10:29:20.050491Z","iopub.status.idle":"2022-04-25T10:29:20.058719Z","shell.execute_reply.started":"2022-04-25T10:29:20.050358Z","shell.execute_reply":"2022-04-25T10:29:20.057512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.makedirs('./output/train/00003/T2w')","metadata":{"execution":{"iopub.status.busy":"2022-04-25T06:39:19.912284Z","iopub.execute_input":"2022-04-25T06:39:19.912975Z","iopub.status.idle":"2022-04-25T06:39:19.938009Z","shell.execute_reply.started":"2022-04-25T06:39:19.912921Z","shell.execute_reply":"2022-04-25T06:39:19.936527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dicom2nifti.convert_directory(\"../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00011/T2w\", \"output/train/00003/T2w\")","metadata":{"execution":{"iopub.status.busy":"2022-04-25T07:09:03.262459Z","iopub.execute_input":"2022-04-25T07:09:03.263206Z","iopub.status.idle":"2022-04-25T07:09:09.004288Z","shell.execute_reply.started":"2022-04-25T07:09:03.263159Z","shell.execute_reply":"2022-04-25T07:09:09.003403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_filename = \"./output/train/00003/T2w/5_t2w.nii.gz\"","metadata":{"execution":{"iopub.status.busy":"2022-04-25T07:11:08.26998Z","iopub.execute_input":"2022-04-25T07:11:08.270293Z","iopub.status.idle":"2022-04-25T07:11:08.274975Z","shell.execute_reply.started":"2022-04-25T07:11:08.270263Z","shell.execute_reply":"2022-04-25T07:11:08.273914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_img = nib.load(sample_filename )\nsample_img = np.asanyarray(sample_img.dataobj)\n#print(\"img shape ->\", sample_img.shape)\nheight, width, depth= sample_img.shape\nprint(f\"The image object has the following dimensions: height: {height}, width:{width}, depth:{depth}\")","metadata":{"execution":{"iopub.status.busy":"2022-04-25T07:11:10.412958Z","iopub.execute_input":"2022-04-25T07:11:10.41389Z","iopub.status.idle":"2022-04-25T07:11:11.421032Z","shell.execute_reply.started":"2022-04-25T07:11:10.413829Z","shell.execute_reply":"2022-04-25T07:11:11.420329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"maxval = 409\ni = np.random.randint(0, maxval)\nprint(f\"Plotting Layer {i}\")\nplt.imshow(sample_img[:, i, :], cmap='gray')\nplt.axis('off');","metadata":{"execution":{"iopub.status.busy":"2022-04-25T07:12:13.42068Z","iopub.execute_input":"2022-04-25T07:12:13.420966Z","iopub.status.idle":"2022-04-25T07:12:13.578584Z","shell.execute_reply.started":"2022-04-25T07:12:13.420937Z","shell.execute_reply":"2022-04-25T07:12:13.577171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.makedirs('./test/T2w')","metadata":{"execution":{"iopub.status.busy":"2022-04-25T09:44:32.683351Z","iopub.execute_input":"2022-04-25T09:44:32.683815Z","iopub.status.idle":"2022-04-25T09:44:32.689308Z","shell.execute_reply.started":"2022-04-25T09:44:32.683773Z","shell.execute_reply":"2022-04-25T09:44:32.688446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import dicom2nifti\nimport os\ndicom_directory = '../input/rsna-miccai-brain-tumor-radiogenomic-classification/test'\nout_folder = './test/T2w'\nos.listdir(out_folder)\n#for i in range(5):\n#    dicom2nifti.convert_directory(dicom_directory, output_folder, compression=True, reorient=True)\n  \ndirectory = '../input/rsna-miccai-brain-tumor-radiogenomic-classification/test/'\nfor filename in os.listdir(directory):\n    file_path = os.path.join(directory, filename,'T2w')\n    dicom2nifti.dicom_series_to_nifti(file_path, out_folder+'/'+filename+'_T2w.nii.gz')","metadata":{"execution":{"iopub.status.busy":"2022-04-25T09:45:59.086538Z","iopub.execute_input":"2022-04-25T09:45:59.086863Z","iopub.status.idle":"2022-04-25T09:54:23.368032Z","shell.execute_reply.started":"2022-04-25T09:45:59.086827Z","shell.execute_reply":"2022-04-25T09:54:23.366759Z"},"trusted":true},"execution_count":null,"outputs":[]}]}