{"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":"# Simple Dicom Visualizer\n\nThis is a little visualizer to easily and quickly look at the training data. This should come in handy during this competition, when a quick look at the images of a specific patient is needed. Feel free to use and change this to your heart's content :)","metadata":{}},{"cell_type":"code","source":"import ipywidgets as widgets\nfrom ipywidgets import interact\nimport pydicom as dcm\nfrom pathlib import Path\nimport numpy as np\nimport pandas as pd\nimport plotly.express as px\n\nPath.ls = lambda p: list(p.iterdir())","metadata":{"execution":{"iopub.status.busy":"2021-07-19T13:13:37.976654Z","iopub.execute_input":"2021-07-19T13:13:37.977073Z","iopub.status.idle":"2021-07-19T13:13:40.000033Z","shell.execute_reply.started":"2021-07-19T13:13:37.976991Z","shell.execute_reply":"2021-07-19T13:13:39.998942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"p_data = Path(\"/kaggle/input/rsna-miccai-brain-tumor-radiogenomic-classification\")\np_train_labels = p_data/\"train_labels.csv\"\np_train = p_data/\"train\"","metadata":{"execution":{"iopub.status.busy":"2021-07-19T13:13:40.761617Z","iopub.execute_input":"2021-07-19T13:13:40.762031Z","iopub.status.idle":"2021-07-19T13:13:40.766351Z","shell.execute_reply.started":"2021-07-19T13:13:40.761994Z","shell.execute_reply":"2021-07-19T13:13:40.765129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_df = pd.read_csv(p_train_labels)","metadata":{"execution":{"iopub.status.busy":"2021-07-19T13:13:41.6716Z","iopub.execute_input":"2021-07-19T13:13:41.671973Z","iopub.status.idle":"2021-07-19T13:13:41.687034Z","shell.execute_reply.started":"2021-07-19T13:13:41.671942Z","shell.execute_reply":"2021-07-19T13:13:41.686071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type_idx = 0\npatient_idx = 0\npatient = None\n\nmodalities = [\"T2w\", \"T1wCE\", \"T1w\", \"FLAIR\"]\ntrain_patients = label_df[\"BraTS21ID\"].values\n\ndcm_images = None\n\n@interact\ndef selections(Modality=modalities, BraTS21ID=train_patients):\n    global type_idx, patient_idx, patient, dcm_images\n    \n    patient_idx = np.where(label_df[\"BraTS21ID\"].values == BraTS21ID)[0]\n    \n    assert len(patient_idx) < 2, f\"{BraTS21ID}\\n{patient_idx}\"\n    patient_idx = patient_idx[0]\n    \n    patient = sorted(p_train.ls(), key=lambda p: int(p.stem))[patient_idx]\n    m2i = {p.stem: i for i, p in enumerate(patient.ls())}\n    type_idx = m2i[Modality]\n    \n    dcm_images = (patient.ls()[type_idx]).ls()\n    dcm_images = sorted(dcm_images, key = lambda p: int(p.stem.split(\"-\")[-1]))\n    dcm_images = [dcm.dcmread(p).pixel_array for p in dcm_images]","metadata":{"execution":{"iopub.status.busy":"2021-07-19T13:13:42.968616Z","iopub.execute_input":"2021-07-19T13:13:42.969156Z","iopub.status.idle":"2021-07-19T13:13:46.737499Z","shell.execute_reply.started":"2021-07-19T13:13:42.969102Z","shell.execute_reply":"2021-07-19T13:13:46.736747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Rerun this cell everytime you change the values above\n\npatient_label = label_df[label_df[\"BraTS21ID\"] == int(patient.name)][\"MGMT_value\"].values[0]\n\ndef viewer(image_idx):\n    print(f\"BraTS21ID: {patient.name}\\tMGMT_value: {patient_label}\\tModality: {patient.ls()[type_idx].stem}\")\n    fig = px.imshow(dcm_images[image_idx], binary_string=True)\n    fig.show()\n\ninteract(viewer, \n         image_idx=widgets.IntSlider(min=0, max=len(dcm_images)-1, step=1, value=0));","metadata":{"execution":{"iopub.status.busy":"2021-07-19T13:15:06.046959Z","iopub.execute_input":"2021-07-19T13:15:06.047311Z","iopub.status.idle":"2021-07-19T13:15:06.131706Z","shell.execute_reply.started":"2021-07-19T13:15:06.04728Z","shell.execute_reply":"2021-07-19T13:15:06.130817Z"},"trusted":true},"execution_count":null,"outputs":[]}]}