{"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":"!pip install SimpleITK vtk pydicom ipympl itkwidgets numpy-stl -q","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-08-25T09:09:50.332730Z","iopub.execute_input":"2021-08-25T09:09:50.333319Z","iopub.status.idle":"2021-08-25T09:10:28.790618Z","shell.execute_reply.started":"2021-08-25T09:09:50.333273Z","shell.execute_reply":"2021-08-25T09:10:28.789483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!git clone https://github.com/dave3d/dicom2stl.git\n!wget https://raw.githubusercontent.com/InsightSoftwareConsortium/SimpleITK-Notebooks/master/Python/gui.py -O gui.py","metadata":{"execution":{"iopub.status.busy":"2021-08-25T09:10:28.792170Z","iopub.execute_input":"2021-08-25T09:10:28.792467Z","iopub.status.idle":"2021-08-25T09:10:33.004021Z","shell.execute_reply.started":"2021-08-25T09:10:28.792438Z","shell.execute_reply":"2021-08-25T09:10:33.002964Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nimport sys\nimport itkwidgets\nimport SimpleITK as sitk\nimport glob\nimport re\nfrom gui import MultiImageDisplay\nsys.path.append('dicom2stl')\nfrom utils import dicomutils\nfrom tqdm import tqdm\nfrom tqdm.contrib.concurrent import process_map","metadata":{"execution":{"iopub.status.busy":"2021-08-25T09:10:33.005909Z","iopub.execute_input":"2021-08-25T09:10:33.006218Z","iopub.status.idle":"2021-08-25T09:10:38.109623Z","shell.execute_reply.started":"2021-08-25T09:10:33.006174Z","shell.execute_reply":"2021-08-25T09:10:38.108604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DS_PATH = r'../input/rsna-miccai-brain-tumor-radiogenomic-classification'\ndef make_image(patient_id, split='train'):\n    path_flair = os.path.join(DS_PATH, split, patient_id, 'FLAIR')\n    path_t1w = os.path.join(DS_PATH, split, patient_id, 'T1w')\n    path_t1wce = os.path.join(DS_PATH, split, patient_id, 'T1wCE')\n    path_t2w = os.path.join(DS_PATH, split, patient_id, 'T2w')\n    \n    flair_dicom, flair_modality = dicomutils.loadLargestSeries(path_flair)\n    t1w_dicom, t1w_modality = dicomutils.loadLargestSeries(path_t1w)\n    t1wce_dicom, t1wce_modality = dicomutils.loadLargestSeries(path_t1wce)\n    t2w_dicom, t2w_modality = dicomutils.loadLargestSeries(path_t2w)\n    return flair_dicom, t1w_dicom, t1wce_dicom, t2w_dicom\n\nflair_dicom, t1w_dicom, t1wce_dicom, t2w_dicom = make_image('00003')","metadata":{"execution":{"iopub.status.busy":"2021-08-25T09:10:38.111252Z","iopub.execute_input":"2021-08-25T09:10:38.111550Z","iopub.status.idle":"2021-08-25T09:10:47.281552Z","shell.execute_reply.started":"2021-08-25T09:10:38.111521Z","shell.execute_reply":"2021-08-25T09:10:47.280346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%matplotlib widget\nMultiImageDisplay(image_list=[flair_dicom, t1w_dicom], \n                  title_list=['FLAIR', 'T1w'])","metadata":{"execution":{"iopub.status.busy":"2021-08-25T09:10:47.285385Z","iopub.execute_input":"2021-08-25T09:10:47.285722Z","iopub.status.idle":"2021-08-25T09:10:47.808954Z","shell.execute_reply.started":"2021-08-25T09:10:47.285690Z","shell.execute_reply":"2021-08-25T09:10:47.807796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%matplotlib widget\nMultiImageDisplay(image_list=[t1wce_dicom, t2w_dicom], \n                  title_list=['T1wCE', 'T2w'])","metadata":{"execution":{"iopub.status.busy":"2021-08-25T09:10:47.810811Z","iopub.execute_input":"2021-08-25T09:10:47.811280Z","iopub.status.idle":"2021-08-25T09:10:49.011786Z","shell.execute_reply.started":"2021-08-25T09:10:47.811234Z","shell.execute_reply":"2021-08-25T09:10:49.010587Z"},"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['BraTS21ID5'] = train_df.BraTS21ID.apply(lambda x: format(x, '05d'))\n\ntest_df = pd.read_csv(\"../input/rsna-miccai-brain-tumor-radiogenomic-classification/sample_submission.csv\")\ntest_df['BraTS21ID5'] = test_df.BraTS21ID.apply(lambda x: format(x, '05d'))","metadata":{"execution":{"iopub.status.busy":"2021-08-25T09:10:49.013510Z","iopub.execute_input":"2021-08-25T09:10:49.014157Z","iopub.status.idle":"2021-08-25T09:10:49.057521Z","shell.execute_reply.started":"2021-08-25T09:10:49.014100Z","shell.execute_reply":"2021-08-25T09:10:49.056298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['scan'] = [['FLAIR', 'T1w', 'T1wCE', 'T2w']] * len(train_df)\ntrain_df = train_df.explode('scan').reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2021-08-25T09:24:51.000440Z","iopub.execute_input":"2021-08-25T09:24:51.000841Z","iopub.status.idle":"2021-08-25T09:24:51.013059Z","shell.execute_reply.started":"2021-08-25T09:24:51.000807Z","shell.execute_reply":"2021-08-25T09:24:51.011755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df['scan'] = [['FLAIR', 'T1w', 'T1wCE', 'T2w']] * len(test_df)\ntest_df = test_df.explode('scan').reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2021-08-25T09:27:40.435751Z","iopub.execute_input":"2021-08-25T09:27:40.436178Z","iopub.status.idle":"2021-08-25T09:27:40.449446Z","shell.execute_reply.started":"2021-08-25T09:27:40.436144Z","shell.execute_reply":"2021-08-25T09:27:40.448430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"OUTPUT_DIR = './mesh'\ndef save_point_cloud(patient_id, scan, split='train'):\n    output = os.path.join(OUTPUT_DIR, patient_id)\n    os.makedirs(output, exist_ok=True)\n    output = os.path.join(output, scan)\n    path = os.path.join(DS_PATH, split, patient_id, scan)\n    os.system('python dicom2stl/dicom2stl.py -i 400 -o {output}.stl {path}'.format(output=output, path=path))\n\n\nprocess_map(save_point_cloud, train_df.BraTS21ID5.values, train_df.scan.values)\nprocess_map(save_point_cloud, test_df.BraTS21ID5.values, test_df.scan.values, ['test']*len(test_df))\n\n# for patient in tqdm(train_df.BraTS21ID5.values):\n#     save_point_cloud(patient, 'FLAIR')\n#     save_point_cloud(patient, 'T1w')\n#     save_point_cloud(patient, 'T1wCE')\n#     save_point_cloud(patient, 'T2w')\n    ","metadata":{"execution":{"iopub.status.busy":"2021-08-25T09:11:15.902129Z","iopub.execute_input":"2021-08-25T09:11:15.902556Z","iopub.status.idle":"2021-08-25T09:11:15.909670Z","shell.execute_reply.started":"2021-08-25T09:11:15.902523Z","shell.execute_reply":"2021-08-25T09:11:15.908451Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from stl import mesh\nfrom mpl_toolkits import mplot3d\nfrom matplotlib import pyplot\n\n# Create a new plot\nfigure = pyplot.figure()\naxes = mplot3d.Axes3D(figure)\n\n# Load the STL files and add the vectors to the plot\nyour_mesh = mesh.Mesh.from_file('./mesh/00000/T1w.stl')\naxes.add_collection3d(mplot3d.art3d.Poly3DCollection(your_mesh.vectors))\n\n# Auto scale to the mesh size\nscale = your_mesh.points.flatten()\naxes.auto_scale_xyz(scale, scale, scale)\n\n# Show the plot to the screen\npyplot.show()\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -r dicom2stl gui.py","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}