{"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":"","metadata":{}},{"cell_type":"code","source":"import pydicom\nimport os \nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport cv2\nimport math","metadata":{"execution":{"iopub.status.busy":"2021-07-26T15:26:47.920760Z","iopub.execute_input":"2021-07-26T15:26:47.921092Z","iopub.status.idle":"2021-07-26T15:26:47.926054Z","shell.execute_reply.started":"2021-07-26T15:26:47.921064Z","shell.execute_reply":"2021-07-26T15:26:47.924911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# READ DATA","metadata":{}},{"cell_type":"code","source":"data_dir = \"../input/rsna-miccai-brain-tumor-radiogenomic-classification/train\"\npatients = os.listdir(data_dir)\nlabels_df = pd.read_csv(\"../input/rsna-miccai-brain-tumor-radiogenomic-classification/train_labels.csv\")\nlabels_df[\"patient\"] = [(5 - len(str(l))) * \"0\" + str(l) for l in labels_df[\"BraTS21ID\"]]       ","metadata":{"execution":{"iopub.status.busy":"2021-07-26T13:28:41.042139Z","iopub.execute_input":"2021-07-26T13:28:41.042509Z","iopub.status.idle":"2021-07-26T13:28:41.061430Z","shell.execute_reply.started":"2021-07-26T13:28:41.042474Z","shell.execute_reply":"2021-07-26T13:28:41.060439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\" The number of different patients is {len(patients)}\")\nprint(f\" The number of patients with tumor is {sum(labels_df['MGMT_value'])}\") ","metadata":{"execution":{"iopub.status.busy":"2021-07-26T13:30:45.824737Z","iopub.execute_input":"2021-07-26T13:30:45.825064Z","iopub.status.idle":"2021-07-26T13:30:45.833182Z","shell.execute_reply.started":"2021-07-26T13:30:45.825034Z","shell.execute_reply":"2021-07-26T13:30:45.831854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"c = 0\ndims_t2w = {}\ndims_t1w = {}\ndims_t1wce = {}\ndims_flair = {}\nfor patient in patients[1:10]:\n#     print(patient)\n    c += 1\n    label = labels_df[labels_df[\"patient\"] == patient]['MGMT_value']\n    if c % 45 == 0:\n        print(f\"{c}/585\")\n    \n    # T2w\n    path_t2w = data_dir + '/' + patient + '/T2w/'\n    slices_t2w = [pydicom.dcmread(path_t2w + s) for s in os.listdir(path_t2w)]\n    slices_t2w.sort(key = lambda x: int(x.ImagePositionPatient[2]))\n    dims_t2w[patient] = (len(slices_t2w), slices_t2w[0].pixel_array.shape)\n    # T1w\n    path_t1w = data_dir + '/' + patient + '/T1w/'\n    slices_t1w = [pydicom.dcmread(path_t1w + s) for s in os.listdir(path_t1w)]\n    slices_t1w.sort(key = lambda x: int(x.ImagePositionPatient[2]))\n    dims_t1w[patient] = (len(slices_t1w), slices_t1w[0].pixel_array.shape)\n    # T1wCE\n    path_t1wce = data_dir + '/' + patient + '/T1wCE/'\n    slices_t1wce = [pydicom.dcmread(path_t1wce + s) for s in os.listdir(path_t1wce)]\n    slices_t1wce.sort(key = lambda x: int(x.ImagePositionPatient[2]))\n    dims_t1wce[patient] = (len(slices_t1wce), slices_t1wce[0].pixel_array.shape)\n    # FLAIR\n    path_flair = data_dir + '/' + patient + '/FLAIR/'\n    slices_flair = [pydicom.dcmread(path_flair + s) for s in os.listdir(path_flair)]\n    slices_flair.sort(key = lambda x: int(x.ImagePositionPatient[2]))\n    dims_flair[patient] = (len(slices_flair), slices_flair[0].pixel_array.shape)","metadata":{"execution":{"iopub.status.busy":"2021-07-26T14:39:37.861858Z","iopub.execute_input":"2021-07-26T14:39:37.862205Z","iopub.status.idle":"2021-07-26T14:40:09.570893Z","shell.execute_reply.started":"2021-07-26T14:39:37.862173Z","shell.execute_reply":"2021-07-26T14:40:09.569489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# dims_t2w # 27 (256,208)\n# dims_t1w # 29 (256,192)\n# dims_t1wce # 75 (256, 192)\n# dims_flair # 27 (256, 192)","metadata":{"execution":{"iopub.status.busy":"2021-07-26T14:59:19.587651Z","iopub.execute_input":"2021-07-26T14:59:19.588179Z","iopub.status.idle":"2021-07-26T14:59:19.592142Z","shell.execute_reply.started":"2021-07-26T14:59:19.588132Z","shell.execute_reply":"2021-07-26T14:59:19.591129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# VISUALIZE DATA","metadata":{}},{"cell_type":"code","source":"for patient in patients[:1]:\n    # T2w\n    path_t2w = data_dir + '/' + patient + '/T2w/'\n    slices_t2w = [pydicom.dcmread(path_t2w + s) for s in os.listdir(path_t2w)]\n    slices_t2w.sort(key = lambda x: int(x.ImagePositionPatient[2]))\n    \n    plt.imshow(slices_t2w[0].pixel_array)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-26T14:58:44.943437Z","iopub.execute_input":"2021-07-26T14:58:44.943757Z","iopub.status.idle":"2021-07-26T14:58:45.968607Z","shell.execute_reply.started":"2021-07-26T14:58:44.943729Z","shell.execute_reply":"2021-07-26T14:58:45.967455Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imp_px_size = 192\n\nfor patient in patients[:1]:\n    # T1w\n    path_t1w = data_dir + '/' + patient + '/T1w/'\n    slices_t1w = [pydicom.dcmread(path_t1w + s) for s in os.listdir(path_t1w)]\n    slices_t1w.sort(key = lambda x: int(x.ImagePositionPatient[2]))\n    \n    fig = plt.figure()\n    for num, each_slice in enumerate(slices_t1w[72:108]):\n        y = fig.add_subplot(6,6, num + 1)\n        new_image = cv2.resize(np.array(each_slice.pixel_array), (imp_px_size, imp_px_size))\n        y.imshow(new_image, cmap = 'gray')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-26T15:19:13.296762Z","iopub.execute_input":"2021-07-26T15:19:13.298912Z","iopub.status.idle":"2021-07-26T15:19:16.637987Z","shell.execute_reply.started":"2021-07-26T15:19:13.298868Z","shell.execute_reply":"2021-07-26T15:19:16.636985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imp_px_size = 192\n\nfor patient in patients[:1]:\n    # T1w\n    path_t1wce = data_dir + '/' + patient + '/T1wCE/'\n    slices_t1wce = [pydicom.dcmread(path_t1wce + s) for s in os.listdir(path_t1wce)]\n    slices_t1wce.sort(key = lambda x: int(x.ImagePositionPatient[2]))\n    \n    fig = plt.figure()\n    for num, each_slice in enumerate(slices_t1wce[72:108]):\n        y = fig.add_subplot(6,6, num + 1)\n        new_image = cv2.resize(np.array(each_slice.pixel_array), (imp_px_size, imp_px_size))\n        y.imshow(new_image, cmap = 'gray')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-26T15:18:50.339824Z","iopub.execute_input":"2021-07-26T15:18:50.340175Z","iopub.status.idle":"2021-07-26T15:18:53.593274Z","shell.execute_reply.started":"2021-07-26T15:18:50.340144Z","shell.execute_reply":"2021-07-26T15:18:53.592397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imp_px_size = 208\n\nfor patient in patients[:1]:\n    # T2w\n    path_t2w = data_dir + '/' + patient + '/T2w/'\n    slices_t2w = [pydicom.dcmread(path_t2w + s) for s in os.listdir(path_t2w)]\n    slices_t2w.sort(key = lambda x: int(x.ImagePositionPatient[2]))\n    \n    fig = plt.figure()\n    for num, each_slice in enumerate(slices_t2w[:36]):\n        y = fig.add_subplot(6,6, num + 1)\n        new_image = cv2.resize(np.array(each_slice.pixel_array), (imp_px_size, imp_px_size))\n        y.imshow(new_image, cmap = 'gray')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-26T15:19:26.947931Z","iopub.execute_input":"2021-07-26T15:19:26.948296Z","iopub.status.idle":"2021-07-26T15:19:30.258774Z","shell.execute_reply.started":"2021-07-26T15:19:26.948264Z","shell.execute_reply":"2021-07-26T15:19:30.257832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imp_px_size = 208\n\nfor patient in patients[:1]:\n    # flair\n    path_flair = data_dir + '/' + patient + '/FLAIR/'\n    slices_flair = [pydicom.dcmread(path_flair + s) for s in os.listdir(path_flair)]\n    slices_flair.sort(key = lambda x: int(x.ImagePositionPatient[2]))\n    \n    fig = plt.figure()\n    for num, each_slice in enumerate(slices_flair[36:72]):\n        y = fig.add_subplot(6,6, num + 1)\n        new_image = cv2.resize(np.array(each_slice.pixel_array), (imp_px_size, imp_px_size))\n        y.imshow(new_image, cmap = 'gray')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-26T15:20:15.655043Z","iopub.execute_input":"2021-07-26T15:20:15.655401Z","iopub.status.idle":"2021-07-26T15:20:19.297844Z","shell.execute_reply.started":"2021-07-26T15:20:15.655371Z","shell.execute_reply":"2021-07-26T15:20:19.296464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# RESIZE DATA\n\n### NEED TO RESIZE BOTH DEPTH AND PIXEL-GRID ?\n\n**work only with t2w for now**","metadata":{}},{"cell_type":"code","source":"image_pixel_size = 196\nhm_slices = 20\n\ndef chunks(l, n):\n    for i in range(0, len(l), n):\n        yield l[i:i + n]\n        \ndef mean(l):\n    return sum(l)/len(l)\n\nfor patient in patients[:10]:\n    # T2w\n    path_t2w = data_dir + '/' + patient + '/T2w/'\n    slices_t2w = [pydicom.dcmread(path_t2w + s) for s in os.listdir(path_t2w)]\n    slices_t2w.sort(key = lambda x: int(x.ImagePositionPatient[2]))\n    \n    new_slices = []\n    \n    # resize pixel size\n    slices_t2w = [cv2.resize(np.array(each_slice.pixel_array), (imp_px_size, imp_px_size)) for each_slice in slices_t2w]\n    \n    # resize depth\n    c_size = math.ceil(len(slices_t2w) / hm_slices)\n    \n    for s_c in chunks(slices_t2w, c_size):\n        s_c = list(map(mean, zip(*s_c)))\n        new_slices.append(s_c)\n        \n    while len(new_slices) < hm_slices:\n        new_slices.append(new_slices[:-1])\n    \n    \n    print(len(new_slices))","metadata":{"execution":{"iopub.status.busy":"2021-07-26T15:37:29.027568Z","iopub.execute_input":"2021-07-26T15:37:29.027893Z","iopub.status.idle":"2021-07-26T15:37:35.763201Z","shell.execute_reply.started":"2021-07-26T15:37:29.027864Z","shell.execute_reply":"2021-07-26T15:37:35.761363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}