{"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":"import numpy as np\nimport pandas as pd\n\nimport os\nimport seaborn as sns\nimport json\nimport ast\nimport glob\nfrom tqdm import tqdm\nimport gc\n\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\n\nimport pydicom\nimport cv2\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\n\nimport random","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-10-11T01:49:03.396986Z","iopub.execute_input":"2021-10-11T01:49:03.397940Z","iopub.status.idle":"2021-10-11T01:49:03.405428Z","shell.execute_reply.started":"2021-10-11T01:49:03.397882Z","shell.execute_reply":"2021-10-11T01:49:03.404563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_csv = pd.read_csv(\"../input/rsna-miccai-brain-tumor-radiogenomic-classification/train_labels.csv\")\ndisplay(train_csv.head())","metadata":{"execution":{"iopub.status.busy":"2021-10-11T01:49:03.412695Z","iopub.execute_input":"2021-10-11T01:49:03.413278Z","iopub.status.idle":"2021-10-11T01:49:03.432816Z","shell.execute_reply.started":"2021-10-11T01:49:03.413239Z","shell.execute_reply":"2021-10-11T01:49:03.431702Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# draw .dcm image function","metadata":{}},{"cell_type":"markdown","source":"## 1.glob.glob -> get image path \n## 2.sorted (data,sorting creteria)\n## 3.pydicom -> read image\n## 4.plt.imshow -> draw image","metadata":{}},{"cell_type":"code","source":"KAGGLE_DIR = '../input/rsna-miccai-brain-tumor-radiogenomic-classification/'\nIMG_PATH_TRAIN = KAGGLE_DIR + 'train/'\n\ndef read_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.0).astype(np.uint8)\n    return data\n\ndef visualize_sample(brats21id, slice_i,mgmt_value,types=(\"FLAIR\", \"T1w\", \"T1wCE\", \"T2w\")):\n    plt.figure(figsize=(16, 5))\n    patient_path = os.path.join(IMG_PATH_TRAIN,str(brats21id).zfill(5),)\n    print(\"patient_path\",patient_path)\n    for i, t in enumerate(types, 1):\n#         print(\"glob data = \", glob.glob(os.path.join(patient_path, t, \"*\")))\n        t_paths = sorted(\n            glob.glob(os.path.join(patient_path, t, \"*\")), \n            key=lambda x: int(x[:-4].split(\"-\")[-1]),\n        )\n#         print(\"image full path=\",os.path.join(patient_path, t, \"*\"))\n#         print(\"t_paths=\",t_paths)\n        data = read_dicom(t_paths[int(len(t_paths) * slice_i)])\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.savefig('plot.png')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-11T01:49:03.434840Z","iopub.execute_input":"2021-10-11T01:49:03.435201Z","iopub.status.idle":"2021-10-11T01:49:03.447353Z","shell.execute_reply.started":"2021-10-11T01:49:03.435169Z","shell.execute_reply":"2021-10-11T01:49:03.446586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# sort by filename  ","metadata":{}},{"cell_type":"markdown","source":"## glob.glob -> not sorted ","metadata":{}},{"cell_type":"code","source":"brats21id = train_csv.iloc[0][\"BraTS21ID\"] \nmgmt_value = train_csv.iloc[0][\"MGMT_value\"] # and tumor class\npatient_path = os.path.join(IMG_PATH_TRAIN,str(brats21id).zfill(5),)\nx = glob.glob(os.path.join(patient_path, \"FLAIR\", \"*\"))\nx[:5]","metadata":{"execution":{"iopub.status.busy":"2021-10-11T01:49:03.448621Z","iopub.execute_input":"2021-10-11T01:49:03.449016Z","iopub.status.idle":"2021-10-11T01:49:03.473080Z","shell.execute_reply.started":"2021-10-11T01:49:03.448986Z","shell.execute_reply":"2021-10-11T01:49:03.472037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## sorted (path,sort by last filename) ","metadata":{}},{"cell_type":"code","source":"t_paths = sorted(x,key=lambda x: int(x[:-4].split(\"-\")[-1]))\nt_paths[:5]","metadata":{"execution":{"iopub.status.busy":"2021-10-11T01:49:03.474440Z","iopub.execute_input":"2021-10-11T01:49:03.474671Z","iopub.status.idle":"2021-10-11T01:49:03.484895Z","shell.execute_reply.started":"2021-10-11T01:49:03.474645Z","shell.execute_reply":"2021-10-11T01:49:03.483936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## split filepath -> distinct filename  ","metadata":{}},{"cell_type":"code","source":"x[0][:-4].split(\"-\")[-1]","metadata":{"execution":{"iopub.status.busy":"2021-10-11T01:49:03.487084Z","iopub.execute_input":"2021-10-11T01:49:03.487332Z","iopub.status.idle":"2021-10-11T01:49:03.500426Z","shell.execute_reply.started":"2021-10-11T01:49:03.487305Z","shell.execute_reply":"2021-10-11T01:49:03.499223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## draw sample","metadata":{}},{"cell_type":"code","source":"_brats21id = train_csv.iloc[0][\"BraTS21ID\"] \n_mgmt_value = train_csv.iloc[0][\"MGMT_value\"] # and tumor class\nprint(\"BraTS21ID=\",_brats21id)\nprint(\"MGMT_value=\",_mgmt_value)\nvisualize_sample(brats21id=_brats21id, mgmt_value=_mgmt_value, slice_i=0.5) # visualize samples","metadata":{"execution":{"iopub.status.busy":"2021-10-11T01:49:03.502361Z","iopub.execute_input":"2021-10-11T01:49:03.502614Z","iopub.status.idle":"2021-10-11T01:49:04.108300Z","shell.execute_reply.started":"2021-10-11T01:49:03.502586Z","shell.execute_reply":"2021-10-11T01:49:04.107173Z"},"trusted":true},"execution_count":null,"outputs":[]}]}