{"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":"* ****RSNA-MICCAI Brain Tumor Radiogenomic Classification resized Dataset in .npy formate****","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19"}},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport os\nimport matplotlib.pyplot as plt\nimport matplotlib\nimport pydicom as dicom\nimport cv2\nimport ast\nfrom glob import glob\nimport warnings\nfrom collections import Counter\nimport seaborn as sns\nfrom scipy import ndimage, misc\nimport pydicom\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2021-07-16T14:54:44.924543Z","iopub.execute_input":"2021-07-16T14:54:44.925132Z","iopub.status.idle":"2021-07-16T14:54:46.343138Z","shell.execute_reply.started":"2021-07-16T14:54:44.925024Z","shell.execute_reply":"2021-07-16T14:54:46.342239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = '../input/rsna-miccai-brain-tumor-radiogenomic-classification/'\ntrain_path = os.path.join(path, 'train')\ntest_path = os.path.join(path, 'test')\n\n\ndef getFullId(id):\n    return str(id).zfill(5)\n\nos.listdir(path)","metadata":{"execution":{"iopub.status.busy":"2021-07-16T14:54:46.344531Z","iopub.execute_input":"2021-07-16T14:54:46.344991Z","iopub.status.idle":"2021-07-16T14:54:46.353901Z","shell.execute_reply.started":"2021-07-16T14:54:46.344953Z","shell.execute_reply":"2021-07-16T14:54:46.352539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(path + 'train_labels.csv')\ntrain_df.describe()","metadata":{"execution":{"iopub.status.busy":"2021-07-16T14:54:46.356123Z","iopub.execute_input":"2021-07-16T14:54:46.356414Z","iopub.status.idle":"2021-07-16T14:54:46.400923Z","shell.execute_reply.started":"2021-07-16T14:54:46.356386Z","shell.execute_reply":"2021-07-16T14:54:46.399899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['MGMT_value'].plot.hist()","metadata":{"execution":{"iopub.status.busy":"2021-07-16T14:54:46.402515Z","iopub.execute_input":"2021-07-16T14:54:46.402825Z","iopub.status.idle":"2021-07-16T14:54:46.615371Z","shell.execute_reply.started":"2021-07-16T14:54:46.402796Z","shell.execute_reply":"2021-07-16T14:54:46.614483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-16T14:54:46.616504Z","iopub.execute_input":"2021-07-16T14:54:46.616795Z","iopub.status.idle":"2021-07-16T14:54:46.626734Z","shell.execute_reply.started":"2021-07-16T14:54:46.616767Z","shell.execute_reply":"2021-07-16T14:54:46.625366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def resize_volume(img):\n    \n    desired_depth = 32\n    desired_width = 128\n    desired_height = 128\n\n    current_depth = img.shape[0]\n    current_width = img.shape[1]\n    current_height = img.shape[2]\n \n    depth = current_depth / desired_depth\n    width = current_width / desired_width\n    height = current_height / desired_height\n    \n    depth_factor = 1 / depth\n    width_factor = 1 / width\n    height_factor = 1 / height\n    \n    img = ndimage.zoom(img, (depth_factor, width_factor, height_factor), order=1)\n    return img\n","metadata":{"execution":{"iopub.status.busy":"2021-07-16T14:54:46.628625Z","iopub.execute_input":"2021-07-16T14:54:46.629086Z","iopub.status.idle":"2021-07-16T14:54:46.637876Z","shell.execute_reply.started":"2021-07-16T14:54:46.629043Z","shell.execute_reply":"2021-07-16T14:54:46.636685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir Train_Image_array Test_Image_array ","metadata":{"execution":{"iopub.status.busy":"2021-07-16T14:54:46.639165Z","iopub.execute_input":"2021-07-16T14:54:46.639459Z","iopub.status.idle":"2021-07-16T14:54:47.426931Z","shell.execute_reply.started":"2021-07-16T14:54:46.639411Z","shell.execute_reply":"2021-07-16T14:54:47.425656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\n\ndef load_dicom(path):\n    dicom = pydicom.read_file(path)\n    data = dicom.pixel_array\n#     print(data.shape)\n    if np.min(data)==np.max(data):\n        data = np.zeros((128,128))\n        return data\n    data = data - np.min(data)\n    if np.max(data) != 0:\n        data = data / np.max(data)\n    \n    data = (data * 255).astype(np.uint8)\n    data = cv2.resize(data, (128, 128))\n    \n    return data\n\ndef load_dicom_line(path):\n    t_paths = sorted(\n        glob(os.path.join(path, \"*\")), \n        key=lambda x: int(x[:-4].split(\"-\")[-1]),\n    )\n    images = []\n    for filename in t_paths:\n        data = load_dicom(filename)\n#         print(data.shape)\n        if data.max() == 0:\n            data = np.zeros((128,128))\n        images.append(data)\n#         print(len(images))\n    images = np.array(images)\n#     print(images.shape)\n        \n    return images","metadata":{"execution":{"iopub.status.busy":"2021-07-16T14:54:47.429894Z","iopub.execute_input":"2021-07-16T14:54:47.430327Z","iopub.status.idle":"2021-07-16T14:54:47.439349Z","shell.execute_reply.started":"2021-07-16T14:54:47.430278Z","shell.execute_reply":"2021-07-16T14:54:47.438109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nid =0\ndef get_image(id):\n    \n    img = []\n    _id = getFullId(id)\n    for t in ( \"T1w\", \"T1wCE\", \"FLAIR\", \"T2w\"):\n        path = os.path.join(train_path, _id, t)\n        frames = np.array(load_dicom_line(path))\n#         print(frames.shape)\n        img.append(resize_volume(np.array(frames)))\n        np.save('./Train_Image_array/'+str(_id)+'.npy', np.array(img).astype(np.uint8))\n    return np.array(img)\n\n# img = get_image(0)","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2021-07-16T14:55:54.77542Z","iopub.execute_input":"2021-07-16T14:55:54.775829Z","iopub.status.idle":"2021-07-16T14:55:54.783744Z","shell.execute_reply.started":"2021-07-16T14:55:54.775793Z","shell.execute_reply":"2021-07-16T14:55:54.782287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# get_image(train_df['BraTS21ID'][81]).shape","metadata":{"execution":{"iopub.status.busy":"2021-07-16T14:55:56.011338Z","iopub.execute_input":"2021-07-16T14:55:56.011915Z","iopub.status.idle":"2021-07-16T14:55:56.889291Z","shell.execute_reply.started":"2021-07-16T14:55:56.011853Z","shell.execute_reply":"2021-07-16T14:55:56.888056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nfrom joblib import Parallel, delayed\nfrom tqdm import tqdm\n    \nresults = Parallel(n_jobs = 8, prefer=\"threads\")(delayed(get_image)(id) for id in tqdm(train_df['BraTS21ID']))","metadata":{"execution":{"iopub.status.busy":"2021-07-16T13:01:55.46535Z","iopub.execute_input":"2021-07-16T13:01:55.46576Z","iopub.status.idle":"2021-07-16T13:05:43.547316Z","shell.execute_reply.started":"2021-07-16T13:01:55.465725Z","shell.execute_reply":"2021-07-16T13:05:43.537606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def get_image(id):\n    \n#     img = []\n#     _id = getFullId(id)\n#     for t in ( \"T1w\", \"T1wCE\", \"FLAIR\", \"T2w\"):\n#         path = os.path.join(test_path, _id, t)\n#         frames = np.array(load_dicom_line(path))\n# #         print(np.array(frames).shape)\n#         img.append(resize_volume(np.array(frames)))\n#         np.save('Test_Image_array/'+str(_id)+'.npy', np.array(img))\n#     return np.array(img)\n","metadata":{"execution":{"iopub.status.busy":"2021-07-16T10:34:59.403627Z","iopub.execute_input":"2021-07-16T10:34:59.404167Z","iopub.status.idle":"2021-07-16T10:34:59.411466Z","shell.execute_reply.started":"2021-07-16T10:34:59.404118Z","shell.execute_reply":"2021-07-16T10:34:59.410396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# %%time\n# from joblib import Parallel, delayed\n# from tqdm import tqdm\n    \n# results = Parallel(n_jobs = 8, prefer=\"threads\")(delayed(get_image)(id) for id in tqdm(os.listdir(test_path)))","metadata":{"execution":{"iopub.status.busy":"2021-07-16T10:35:51.465513Z","iopub.execute_input":"2021-07-16T10:35:51.465865Z","iopub.status.idle":"2021-07-16T10:36:27.275162Z","shell.execute_reply.started":"2021-07-16T10:35:51.465825Z","shell.execute_reply":"2021-07-16T10:36:27.268302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from matplotlib import animation, rc\n# rc('animation', html='jshtml')\n\n\n# def create_animation(ims):\n#     fig = plt.figure(figsize=(6, 6))\n#     plt.axis('off')\n#     im = plt.imshow(ims[0])\n\n#     def animate_func(i):\n#         im.set_array(ims[i])\n#         return [im]\n\n#     return animation.FuncAnimation(fig, animate_func, frames = len(ims), interval = 1000//24)\n\n# create_animation(img[0])","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"scrolled":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from sys import getsizeof\n# getsizeof(frames)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# %%time\n# id =0\n# def get_image(id):\n    \n#     img = []\n#     _id = getFullId(id)\n#     for t in ( \"T1w\", \"T1wCE\", \"FLAIR\", \"T2w\"):\n#         path = os.path.join(train_path, _id, t)\n#         frames =[]\n#         file_names = sorted(os.listdir(path),\n#         key=lambda x: int(x[:-4].split(\"-\")[-1]),)\n#         l = len(file_names)\n#         if l < 32:\n#                 r = range(x)\n#         else:\n#             d = l // 32\n#             r = range(d, 32*d , d)\n#         for i in r:\n#             img_path = os.path.join(path, file_names[i])\n# #             print(img_path)\n#             img_2d_scaled = get3ScaledImage(img_path)\n# #             print(img_2d_scaled.shape)\n#             frames.append(img_2d_scaled)\n# #         print(np.array(frames).shape)\n# #         img.append(resize_volume(np.array(frames)))\n#         img.append(np.array(frames))\n# #         np.save('Image_3d_array/'+str(_id)+'.npy', np.array(img))\n#     return np.array(img)\n\n# img = get_image(0)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def plot_slices(num_rows, num_columns, size, data):\n\n#     fig_width = 13\n#     fig_height = (fig_width / num_columns) / (size[1] / size[0]) * num_rows\n    \n#     fig, ax = plt.subplots(nrows=num_rows, ncols=num_columns, figsize=(fig_width, fig_height))\n#     for i in range(num_rows):\n#         for j in range(num_columns):\n#             ax[i, j].imshow(data[i*num_columns+j])\n#             ax[i, j].axis(\"off\")\n#     plt.subplots_adjust(wspace=0, hspace=0, left=0, right=1, bottom=0, top=1)\n#     plt.show()\n\n# id = 0\n\n# nb_flair = countFlairFiles(id)\n# path_flair = getFlairPath(id)\n# frames_flair = []\n\n# for i in range(nb_flair):\n#     file_name = 'Image-' + str(i+1) + '.dcm'\n#     img_path = os.path.join(path_flair, file_name)\n#     img_2d_scaled, size = get3ScaledImage(img_path)\n#     frames_flair.append(img_2d_scaled)\n    \n# plot_slices(3, 4, size, frames[90:103])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def plot_slices(num_rows, num_columns, size, data):\n\n#     fig_width = 13\n#     fig_height = (fig_width / num_columns) / (size[1] / size[0]) * num_rows\n    \n#     fig, ax = plt.subplots(nrows=num_rows, ncols=num_columns, figsize=(fig_width, fig_height))\n#     for i in range(num_rows):\n#         for j in range(num_columns):\n#             ax[i, j].imshow(data[i*num_columns+j])\n#             ax[i, j].axis(\"off\")\n#     plt.subplots_adjust(wspace=0, hspace=0, left=0, right=1, bottom=0, top=1)\n#     plt.show()\n\n    \n# plot_slices(4, 4, size, resized[24:])","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}