{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":29653,"databundleVersionId":2420395,"sourceType":"competition"}],"dockerImageVersionId":30558,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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","execution":{"iopub.status.busy":"2024-04-07T09:50:46.598501Z","iopub.execute_input":"2024-04-07T09:50:46.598903Z","iopub.status.idle":"2024-04-07T09:50:53.174007Z","shell.execute_reply.started":"2024-04-07T09:50:46.59887Z","shell.execute_reply":"2024-04-07T09:50:53.171801Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!git clone https://github.com/openai/improved-diffusion.git","metadata":{"execution":{"iopub.status.busy":"2023-09-23T20:42:47.525517Z","iopub.execute_input":"2023-09-23T20:42:47.525939Z","iopub.status.idle":"2023-09-23T20:42:48.635961Z","shell.execute_reply.started":"2023-09-23T20:42:47.52589Z","shell.execute_reply":"2023-09-23T20:42:48.634586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pip install -e .","metadata":{"execution":{"iopub.status.busy":"2023-09-23T20:43:09.435848Z","iopub.execute_input":"2023-09-23T20:43:09.436243Z","iopub.status.idle":"2023-09-23T20:43:31.01385Z","shell.execute_reply.started":"2023-09-23T20:43:09.436211Z","shell.execute_reply":"2023-09-23T20:43:31.012479Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mkdir dataset\n","metadata":{"execution":{"iopub.status.busy":"2023-09-23T20:44:01.486954Z","iopub.execute_input":"2023-09-23T20:44:01.487629Z","iopub.status.idle":"2023-09-23T20:44:02.674987Z","shell.execute_reply.started":"2023-09-23T20:44:01.487556Z","shell.execute_reply":"2023-09-23T20:44:02.673185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ls","metadata":{"execution":{"iopub.status.busy":"2023-09-23T20:44:04.275332Z","iopub.execute_input":"2023-09-23T20:44:04.275777Z","iopub.status.idle":"2023-09-23T20:44:05.373918Z","shell.execute_reply.started":"2023-09-23T20:44:04.275735Z","shell.execute_reply":"2023-09-23T20:44:05.372496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cd datasettry","metadata":{"execution":{"iopub.status.busy":"2023-09-23T18:31:00.372763Z","iopub.execute_input":"2023-09-23T18:31:00.373229Z","iopub.status.idle":"2023-09-23T18:31:00.380696Z","shell.execute_reply.started":"2023-09-23T18:31:00.373189Z","shell.execute_reply":"2023-09-23T18:31:00.379458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"/kaggle/input/rsna-miccai-brain-tumor-radiogenomic-classification/sample_submission.csv\n/kaggle/input/rsna-miccai-brain-tumor-radiogenomic-classification/train_labels.csv\n/kaggle/input/rsna-miccai-brain-tumor-radiogenomic-classification/test/00114/T2w/Image-4.dcm","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install dicom\n!pip install pydicom","metadata":{"execution":{"iopub.status.busy":"2024-04-07T09:51:02.632755Z","iopub.execute_input":"2024-04-07T09:51:02.633109Z","iopub.status.idle":"2024-04-07T09:51:30.687736Z","shell.execute_reply.started":"2024-04-07T09:51:02.633082Z","shell.execute_reply":"2024-04-07T09:51:30.686577Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import dicom as dcm","metadata":{"execution":{"iopub.status.busy":"2024-04-07T09:51:30.690241Z","iopub.execute_input":"2024-04-07T09:51:30.690589Z","iopub.status.idle":"2024-04-07T09:51:30.776626Z","shell.execute_reply.started":"2024-04-07T09:51:30.690559Z","shell.execute_reply":"2024-04-07T09:51:30.775487Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pydicom\n\n# Read DICOM file\ndataset = pydicom.dcmread(\"/kaggle/input/rsna-miccai-brain-tumor-radiogenomic-classification/test/00114/T2w/Image-4.dcm\")\n\n# Access metadata and pixel data\npatient_name = dataset.ori\npixel_array = dataset.pixel_array","metadata":{"execution":{"iopub.status.busy":"2023-09-23T20:44:42.430491Z","iopub.execute_input":"2023-09-23T20:44:42.431437Z","iopub.status.idle":"2023-09-23T20:44:43.399982Z","shell.execute_reply.started":"2023-09-23T20:44:42.431392Z","shell.execute_reply":"2023-09-23T20:44:43.397935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_ori = dataset.ImageOrientationPatient","metadata":{"execution":{"iopub.status.busy":"2023-09-23T20:45:08.03063Z","iopub.execute_input":"2023-09-23T20:45:08.031097Z","iopub.status.idle":"2023-09-23T20:45:08.037036Z","shell.execute_reply.started":"2023-09-23T20:45:08.031058Z","shell.execute_reply":"2023-09-23T20:45:08.035528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(image_ori)\nimport matplotlib.pyplot as plt\nplt.imshow(dataset.pixel_array)","metadata":{"execution":{"iopub.status.busy":"2023-09-23T20:45:09.931869Z","iopub.execute_input":"2023-09-23T20:45:09.932399Z","iopub.status.idle":"2023-09-23T20:45:10.327297Z","shell.execute_reply.started":"2023-09-23T20:45:09.932346Z","shell.execute_reply":"2023-09-23T20:45:10.326072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import glob\ntraint1w = (glob.glob(\"/kaggle/input/rsna-miccai-brain-tumor-radiogenomic-classification/*/*/T1w\"))\ntraint2w = (glob.glob(\"/kaggle/input/rsna-miccai-brain-tumor-radiogenomic-classification/*/*/T2w\"))\n(traint1w).sort()\n(traint2w).sort()\npatient_list = []\nfor i in range(len(traint1w)):\n    patient_list.append(traint1w[i].split(\"/\")[-2])","metadata":{"execution":{"iopub.status.busy":"2024-04-07T09:51:47.475671Z","iopub.execute_input":"2024-04-07T09:51:47.476028Z","iopub.status.idle":"2024-04-07T09:51:52.318008Z","shell.execute_reply.started":"2024-04-07T09:51:47.476002Z","shell.execute_reply":"2024-04-07T09:51:52.317096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(patient_list)","metadata":{"execution":{"iopub.status.busy":"2024-04-07T09:52:05.444445Z","iopub.execute_input":"2024-04-07T09:52:05.445076Z","iopub.status.idle":"2024-04-07T09:52:05.454511Z","shell.execute_reply.started":"2024-04-07T09:52:05.445036Z","shell.execute_reply":"2024-04-07T09:52:05.453584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"oriented_list=[]\nfor e in patient_list:\n    try:\n        dataset1 = pydicom.dcmread(\"/kaggle/input/rsna-miccai-brain-tumor-radiogenomic-classification/train/{}/T1w/Image-1.dcm\".format(e))\n        dataset2 = pydicom.dcmread(\"/kaggle/input/rsna-miccai-brain-tumor-radiogenomic-classification/train/{}/T2w/Image-1.dcm\".format(e))\n        dataset3 = pydicom.dcmread(\"/kaggle/input/rsna-miccai-brain-tumor-radiogenomic-classification/train/{}/T1wCE/Image-1.dcm\".format(e))\n    except: continue\n    if float(str((dataset1.ImageOrientationPatient)[0]))>=0.98 and float(str((dataset2.ImageOrientationPatient)[0]))>=0.98 and float(str((dataset3.ImageOrientationPatient)[0]))>=0.98 and float(str((dataset1.ImageOrientationPatient)[-2]))>=0.95 and float(str((dataset2.ImageOrientationPatient)[-2]))>=0.95 and float(str((dataset3.ImageOrientationPatient)[-2]))>=0.95:\n        oriented_list.append(e)","metadata":{"execution":{"iopub.status.busy":"2023-09-23T20:45:15.163742Z","iopub.execute_input":"2023-09-23T20:45:15.16414Z","iopub.status.idle":"2023-09-23T20:45:30.634183Z","shell.execute_reply.started":"2023-09-23T20:45:15.164107Z","shell.execute_reply":"2023-09-23T20:45:30.632991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfor e in patient_list:\n    try:\n        dataset1 = pydicom.dcmread(\"/kaggle/input/rsna-miccai-brain-tumor-radiogenomic-classification/test/{}/T1w/Image-1.dcm\".format(e))\n        dataset2 = pydicom.dcmread(\"/kaggle/input/rsna-miccai-brain-tumor-radiogenomic-classification/test/{}/T2w/Image-1.dcm\".format(e))\n        dataset3 = pydicom.dcmread(\"/kaggle/input/rsna-miccai-brain-tumor-radiogenomic-classification/test/{}/T1wCE/Image-1.dcm\".format(e))\n    except: continue\n    if float(str((dataset1.ImageOrientationPatient)[0]))>=0.98 and float(str((dataset2.ImageOrientationPatient)[0]))>=0.98 and float(str((dataset3.ImageOrientationPatient)[0]))>=0.98 and float(str((dataset1.ImageOrientationPatient)[-2]))>=0.95 and float(str((dataset2.ImageOrientationPatient)[-2]))>=0.95 and float(str((dataset3.ImageOrientationPatient)[-2]))>=0.95:\n        oriented_list.append(e)","metadata":{"execution":{"iopub.status.busy":"2023-09-23T20:45:30.636473Z","iopub.execute_input":"2023-09-23T20:45:30.636905Z","iopub.status.idle":"2023-09-23T20:45:32.746536Z","shell.execute_reply.started":"2023-09-23T20:45:30.636872Z","shell.execute_reply":"2023-09-23T20:45:32.745249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"oriented_list= oriented_list_train+oriented_list_test","metadata":{"execution":{"iopub.status.busy":"2023-09-23T17:22:56.413409Z","iopub.execute_input":"2023-09-23T17:22:56.413786Z","iopub.status.idle":"2023-09-23T17:22:56.419669Z","shell.execute_reply.started":"2023-09-23T17:22:56.413756Z","shell.execute_reply":"2023-09-23T17:22:56.418512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a=glob.glob(\"/kaggle/input/rsna-miccai-brain-tumor-radiogenomic-classification/train/{}/T1w/*.dcm\".format(\"00011\"))\nnp.zeros((256,256,len(a),1))","metadata":{"execution":{"iopub.status.busy":"2023-09-13T12:35:09.742962Z","iopub.execute_input":"2023-09-13T12:35:09.743443Z","iopub.status.idle":"2023-09-13T12:35:09.768446Z","shell.execute_reply.started":"2023-09-13T12:35:09.743407Z","shell.execute_reply":"2023-09-13T12:35:09.766948Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(oriented_list)*32*3","metadata":{"execution":{"iopub.status.busy":"2023-09-23T20:45:32.75025Z","iopub.execute_input":"2023-09-23T20:45:32.75117Z","iopub.status.idle":"2023-09-23T20:45:32.759211Z","shell.execute_reply.started":"2023-09-23T20:45:32.751133Z","shell.execute_reply":"2023-09-23T20:45:32.758038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(glob.glob(\"/kaggle/working/dataset/*\"))","metadata":{"execution":{"iopub.status.busy":"2023-09-23T21:16:42.838715Z","iopub.execute_input":"2023-09-23T21:16:42.839282Z","iopub.status.idle":"2023-09-23T21:16:42.994056Z","shell.execute_reply.started":"2023-09-23T21:16:42.839098Z","shell.execute_reply":"2023-09-23T21:16:42.992669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ls","metadata":{"execution":{"iopub.status.busy":"2023-09-23T19:30:09.30932Z","iopub.execute_input":"2023-09-23T19:30:09.309782Z","iopub.status.idle":"2023-09-23T19:30:10.444465Z","shell.execute_reply.started":"2023-09-23T19:30:09.309743Z","shell.execute_reply":"2023-09-23T19:30:10.443309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!git mv -f datasettry working","metadata":{"execution":{"iopub.status.busy":"2023-09-23T19:30:02.832679Z","iopub.execute_input":"2023-09-23T19:30:02.834029Z","iopub.status.idle":"2023-09-23T19:30:03.940657Z","shell.execute_reply.started":"2023-09-23T19:30:02.833985Z","shell.execute_reply":"2023-09-23T19:30:03.939057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pip install natsort","metadata":{"execution":{"iopub.status.busy":"2023-09-23T20:45:32.762044Z","iopub.execute_input":"2023-09-23T20:45:32.763376Z","iopub.status.idle":"2023-09-23T20:45:47.086387Z","shell.execute_reply.started":"2023-09-23T20:45:32.763299Z","shell.execute_reply":"2023-09-23T20:45:47.085148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from natsort import natsorted \nfrom skimage import data, color\nfrom skimage.transform import rescale, resize, downscale_local_mean\nfrom PIL import Image\n\n\n\nfor i in range(len(oriented_list)):\n    idx = oriented_list[i]\n    b = os.path.join(\"/kaggle/input/rsna-miccai-brain-tumor-radiogenomic-classification/*\",oriented_list[i])\n    t1 = natsorted(glob.glob(b+\"/T1w/*.dcm\"))\n    t2 = natsorted(glob.glob(b+\"/T2w/*.dcm\"))\n    t1ce = natsorted(glob.glob(b+\"/T1wCE/*.dcm\"))\n\n    t1list = []\n    t2list = []\n    t1celist = []\n    \n    for i in range(len(t1)):\n        image = pydicom.dcmread(t1[i]) \n        t1list.append(image.pixel_array)\n\n    t1_3d = np.array(t1list)\n    t1_3d_r = resize(t1_3d, (32,256,256),\n                       anti_aliasing=True)\n    for i in range(len(t1_3d_r)):\n        index = str(i+1)\n        if len(index) == 1: index = \"0\" + index\n        data = t1_3d_r[i]\n        out=(data-np.min(data))/(np.max(data)-np.min(data))\n        im = Image.fromarray(out*256)\n        im = im.convert('L')\n        im.save((\"/kaggle/working/dataset/t1w\"+ \"_\"+index+ idx+\".png\"))\n        \n        \n    for i in range(len(t2)):\n        image = pydicom.dcmread(t2[i]) \n        t2list.append(image.pixel_array)\n\n    t2_3d = np.array(t2list)\n    t2_3d_r = resize(t2_3d, (32,256,256),\n                       anti_aliasing=True)\n    for i in range(len(t2_3d_r)):\n        index = str(i+1)\n        if len(index) == 1: index = \"0\" + index\n        data = t2_3d_r[i]\n        out=(data-np.min(data))/(np.max(data)-np.min(data))\n        im = Image.fromarray(out*256)\n        im = im.convert('L')\n        im.save((\"/kaggle/working/dataset/t2w\"+ \"_\"+index+ idx+\".png\"))\n        \n    \n    for i in range(len(t1ce)):\n        image = pydicom.dcmread(t1ce[i]) \n        t1celist.append(image.pixel_array)\n\n    t1ce_3d = np.array(t1celist)\n    t1ce_3d_r = resize(t1ce_3d, (32,256,256),\n                       anti_aliasing=True)\n    for i in range(len(t1ce_3d_r)):\n        index = str(i+1)\n        if len(index) == 1: index = \"0\" + index\n        data = t1ce_3d_r[i]\n        out=(data-np.min(data))/(np.max(data)-np.min(data))\n        im = Image.fromarray(out*256)\n        im = im.convert('L')\n        im.save((\"/kaggle/working/dataset/t1ce\"+ \"_\"+index+ idx+\".png\"))","metadata":{"execution":{"iopub.status.busy":"2023-09-23T20:45:51.583694Z","iopub.execute_input":"2023-09-23T20:45:51.584123Z","iopub.status.idle":"2023-09-23T21:13:22.34511Z","shell.execute_reply.started":"2023-09-23T20:45:51.58408Z","shell.execute_reply":"2023-09-23T21:13:22.343821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = t1_3d_r[10]\nout=(data-np.min(data))/(np.max(data)-np.min(data))\nim = Image.fromarray(out*256)","metadata":{"execution":{"iopub.status.busy":"2023-09-23T18:38:39.215049Z","iopub.execute_input":"2023-09-23T18:38:39.216041Z","iopub.status.idle":"2023-09-23T18:38:39.222241Z","shell.execute_reply.started":"2023-09-23T18:38:39.216002Z","shell.execute_reply":"2023-09-23T18:38:39.221202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nim = im.convert('L')\nim.save(\"annn.png\")","metadata":{"execution":{"iopub.status.busy":"2023-09-23T18:38:45.306537Z","iopub.execute_input":"2023-09-23T18:38:45.306934Z","iopub.status.idle":"2023-09-23T18:38:45.315056Z","shell.execute_reply.started":"2023-09-23T18:38:45.306902Z","shell.execute_reply":"2023-09-23T18:38:45.314117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Image.open(\"tw_2200151.png\")","metadata":{"execution":{"iopub.status.busy":"2023-09-23T18:42:27.524418Z","iopub.execute_input":"2023-09-23T18:42:27.524833Z","iopub.status.idle":"2023-09-23T18:42:27.538421Z","shell.execute_reply.started":"2023-09-23T18:42:27.5248Z","shell.execute_reply":"2023-09-23T18:42:27.537243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(t1_3d_r[10])","metadata":{"execution":{"iopub.status.busy":"2023-09-23T18:34:29.183709Z","iopub.execute_input":"2023-09-23T18:34:29.184525Z","iopub.status.idle":"2023-09-23T18:34:29.511957Z","shell.execute_reply.started":"2023-09-23T18:34:29.184488Z","shell.execute_reply":"2023-09-23T18:34:29.510915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport subprocess\nfrom IPython.display import FileLink, display\n\ndef download_file(path, download_file_name):\n    os.chdir('/kaggle/working/')\n    zip_name = f\"/kaggle/working/{download_file_name}.zip\"\n    command = f\"zip {zip_name} {path} -r\"\n    result = subprocess.run(command, shell=True, capture_output=True, text=True)\n    if result.returncode != 0:\n        print(\"Unable to run zip command!\")\n        print(result.stderr)\n        return\n    display(FileLink(f'{download_file_name}.zip'))","metadata":{"execution":{"iopub.status.busy":"2023-09-23T21:15:00.686144Z","iopub.execute_input":"2023-09-23T21:15:00.687188Z","iopub.status.idle":"2023-09-23T21:15:00.695006Z","shell.execute_reply.started":"2023-09-23T21:15:00.687147Z","shell.execute_reply":"2023-09-23T21:15:00.693813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"download_file('/kaggle/working/dataset', 'data')","metadata":{"execution":{"iopub.status.busy":"2023-09-23T21:15:56.62336Z","iopub.execute_input":"2023-09-23T21:15:56.623836Z","iopub.status.idle":"2023-09-23T21:16:14.668658Z","shell.execute_reply.started":"2023-09-23T21:15:56.623799Z","shell.execute_reply":"2023-09-23T21:16:14.667371Z"},"trusted":true},"execution_count":null,"outputs":[]}]}