{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.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":22307,"databundleVersionId":1502524,"sourceType":"competition"}],"dockerImageVersionId":30152,"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# !conda install -c conda-forge gdcm -y\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nfrom os import *\nimport pydicom\nimport pydicom as dcm\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport imageio\nfrom IPython import display\nimport glob\n\nfrom skimage import measure \nfrom mpl_toolkits.mplot3d.art3d import Poly3DCollection\nfrom skimage.morphology import disk, opening, closing\nfrom tqdm import tqdm\n\nfrom IPython.display import HTML\nfrom PIL import Image\n\n\n\nimport plotly.express as px\nimport matplotlib.pyplot as plt\nfrom matplotlib import animation, rc\nimport glob\n\n# !conda install -c conda-forge gdcm -y\n# import gdcm\n\n# from matplotlib import animation, rc\nfrom plotly.subplots import make_subplots\nimport plotly.graph_objs as go\n\n\n\nrc('animation', html='jshtml')\n\nnp.random.seed(666)\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\n# import os\n# for 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-02-27T12:27:50.154261Z","iopub.execute_input":"2024-02-27T12:27:50.154835Z","iopub.status.idle":"2024-02-27T12:27:54.095136Z","shell.execute_reply.started":"2024-02-27T12:27:50.154719Z","shell.execute_reply":"2024-02-27T12:27:54.094226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TRAIN_DIR = \"../input/rsna-str-pulmonary-embolism-detection/train/\"\n# files = glob.glob('../input/rsna-str-pulmonary-embolism-detection/train/*/*/*.dcm')","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:27:54.097328Z","iopub.execute_input":"2024-02-27T12:27:54.097644Z","iopub.status.idle":"2024-02-27T12:27:54.101968Z","shell.execute_reply.started":"2024-02-27T12:27:54.097602Z","shell.execute_reply":"2024-02-27T12:27:54.101267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"basepath = \"../input/rsna-str-pulmonary-embolism-detection/\"\n\ntrain_df = pd.read_csv(basepath + \"train.csv\")\ntest_df = pd.read_csv(basepath + \"test.csv\")\n\nTRAIN_PATH = basepath + \"train/\"\nTEST_PATH = basepath + \"test/\"\n\ntrain_image_file_paths = glob.glob(TRAIN_PATH + '/*/*/*.dcm')\ntest_image_file_paths = glob.glob(TEST_PATH + '/*/*/*.dcm')\n\nprint(f'Number of train images : {len(train_image_file_paths)}')\nprint(f'Number of test images  : {len(test_image_file_paths)}')","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:27:54.103335Z","iopub.execute_input":"2024-02-27T12:27:54.10378Z","iopub.status.idle":"2024-02-27T12:36:30.600477Z","shell.execute_reply.started":"2024-02-27T12:27:54.103747Z","shell.execute_reply":"2024-02-27T12:36:30.599056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cols = [\n    'pe_present_on_image', 'negative_exam_for_pe', 'qa_motion', \n    'qa_contrast', 'flow_artifact', 'rv_lv_ratio_gte_1', \n    'rv_lv_ratio_lt_1', 'leftsided_pe', 'chronic_pe', \n    'true_filling_defect_not_pe', 'rightsided_pe', \n    'acute_and_chronic_pe', 'central_pe', 'indeterminate'\n]\n\nfig = make_subplots(rows=5, cols=3)\n\ntraces = [\n    go.Bar(\n        x=[0, 1], \n        y=[\n            len(train_df[train_df[col]==0]),\n            len(train_df[train_df[col]==1])\n        ], \n        name=col,\n        text = [\n            str(round(100 * len(train_df[train_df[col]==0]) / len(train_df), 2)) + '%',\n            str(round(100 * len(train_df[train_df[col]==1]) / len(train_df), 2)) + '%'\n        ],\n        textposition='auto'\n    ) for col in cols\n]\n\nfor i in range(len(traces)):\n    fig.append_trace(traces[i], (i // 3) + 1, (i % 3)  +1)\n\nfig.update_layout(\n    title_text='Train columns',\n    height=1200,\n    width=1000\n)\n\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:36:30.602416Z","iopub.execute_input":"2024-02-27T12:36:30.602779Z","iopub.status.idle":"2024-02-27T12:36:37.406Z","shell.execute_reply.started":"2024-02-27T12:36:30.602732Z","shell.execute_reply":"2024-02-27T12:36:37.405089Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = train_df.drop(['StudyInstanceUID', 'SeriesInstanceUID', 'SOPInstanceUID'], axis=1).sum(axis=0).sort_values().reset_index()\nx.columns = ['column', 'nonzero_records']\n\nfig = px.bar(\n    x, \n    x='nonzero_records', \n    y='column', \n    orientation='h', \n    title='Columns and non zero samples', \n    height=800, \n    width=800\n)\n\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:36:37.40838Z","iopub.execute_input":"2024-02-27T12:36:37.408661Z","iopub.status.idle":"2024-02-27T12:36:38.381552Z","shell.execute_reply.started":"2024-02-27T12:36:37.408626Z","shell.execute_reply":"2024-02-27T12:36:38.380674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = train_df.drop(['StudyInstanceUID', 'SeriesInstanceUID', 'SOPInstanceUID'], axis=1).astype(bool).sum(axis=1).reset_index()\ndata.columns = ['row', 'count']\ndata = data.groupby(['count'])['row'].count().reset_index()\n\nfig = px.bar(\n    data, \n    y=data['row'], \n    x=\"count\", \n    title='Number of activations in for every sample in training set', \n    width=800, \n    height=500\n)\n\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:36:38.382757Z","iopub.execute_input":"2024-02-27T12:36:38.383029Z","iopub.status.idle":"2024-02-27T12:36:38.617506Z","shell.execute_reply.started":"2024-02-27T12:36:38.382997Z","shell.execute_reply":"2024-02-27T12:36:38.616555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = train_df.drop(['StudyInstanceUID', 'SeriesInstanceUID', 'SOPInstanceUID'], axis=1).astype(bool).sum(axis=1).reset_index()\ndata.columns = ['row', 'count']\ndata = data.groupby(['count'])['row'].count().reset_index()\n\nfig = px.pie(\n    data, \n    values=round((100 * data['row'] / len(train_df)), 2), \n    names=\"count\", \n    title='Number of activations for every sample (Percent)', \n    width=800, \n    height=500\n)\n\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:36:38.6189Z","iopub.execute_input":"2024-02-27T12:36:38.619596Z","iopub.status.idle":"2024-02-27T12:36:38.860955Z","shell.execute_reply.started":"2024-02-27T12:36:38.619548Z","shell.execute_reply":"2024-02-27T12:36:38.859907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"We can see that major number of samples in training set have only 1 activation (~65%)","metadata":{}},{"cell_type":"code","source":"data = train_df[[\n    'pe_present_on_image', 'negative_exam_for_pe', 'qa_motion', \n    'qa_contrast', 'flow_artifact', 'rv_lv_ratio_gte_1', \n    'rv_lv_ratio_lt_1', 'leftsided_pe', 'chronic_pe', \n    'true_filling_defect_not_pe', 'rightsided_pe', \n    'acute_and_chronic_pe', 'central_pe', 'indeterminate'\n]]\n\nf = plt.figure(figsize=(16, 16))\nplt.matshow(data.corr(), fignum=f.number)\nplt.xticks(range(data.shape[1]), data.columns, fontsize=13, rotation=70)\nplt.yticks(range(data.shape[1]), data.columns, fontsize=13)\ncb = plt.colorbar()\ncb.ax.tick_params(labelsize=13)","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:36:38.86248Z","iopub.execute_input":"2024-02-27T12:36:38.862728Z","iopub.status.idle":"2024-02-27T12:36:41.074699Z","shell.execute_reply.started":"2024-02-27T12:36:38.862697Z","shell.execute_reply":"2024-02-27T12:36:41.073609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_df)\nprint(test_df)","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:36:41.076018Z","iopub.execute_input":"2024-02-27T12:36:41.076452Z","iopub.status.idle":"2024-02-27T12:36:41.098656Z","shell.execute_reply.started":"2024-02-27T12:36:41.076418Z","shell.execute_reply":"2024-02-27T12:36:41.097539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_df.shape)\nprint(test_df.shape)","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:36:41.100887Z","iopub.execute_input":"2024-02-27T12:36:41.101239Z","iopub.status.idle":"2024-02-27T12:36:41.107175Z","shell.execute_reply.started":"2024-02-27T12:36:41.10119Z","shell.execute_reply":"2024-02-27T12:36:41.106076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:36:41.108788Z","iopub.execute_input":"2024-02-27T12:36:41.1094Z","iopub.status.idle":"2024-02-27T12:36:41.13581Z","shell.execute_reply.started":"2024-02-27T12:36:41.109363Z","shell.execute_reply":"2024-02-27T12:36:41.134989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:36:41.137269Z","iopub.execute_input":"2024-02-27T12:36:41.137751Z","iopub.status.idle":"2024-02-27T12:36:41.149055Z","shell.execute_reply.started":"2024-02-27T12:36:41.137705Z","shell.execute_reply":"2024-02-27T12:36:41.148103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.describe()","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:36:41.150409Z","iopub.execute_input":"2024-02-27T12:36:41.150685Z","iopub.status.idle":"2024-02-27T12:36:41.698836Z","shell.execute_reply.started":"2024-02-27T12:36:41.150651Z","shell.execute_reply":"2024-02-27T12:36:41.697901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.describe()","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:36:41.703681Z","iopub.execute_input":"2024-02-27T12:36:41.703982Z","iopub.status.idle":"2024-02-27T12:36:41.923283Z","shell.execute_reply.started":"2024-02-27T12:36:41.703949Z","shell.execute_reply":"2024-02-27T12:36:41.922189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"basepath == \"../input/rsna-str-pulmonary-embolism-detection/\"\ntrain_df[\"dcm_path\"] = basepath + \"train/\" + train_df.StudyInstanceUID + \"/\" + train_df.SeriesInstanceUID ","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:36:41.924753Z","iopub.execute_input":"2024-02-27T12:36:41.925144Z","iopub.status.idle":"2024-02-27T12:36:43.000765Z","shell.execute_reply.started":"2024-02-27T12:36:41.92511Z","shell.execute_reply":"2024-02-27T12:36:42.999715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.dcm_path","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:36:43.003665Z","iopub.execute_input":"2024-02-27T12:36:43.003963Z","iopub.status.idle":"2024-02-27T12:36:43.012107Z","shell.execute_reply.started":"2024-02-27T12:36:43.003928Z","shell.execute_reply":"2024-02-27T12:36:43.011503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_scans(dcm_path):\n    if basepath == \"../input/rsna-str-pulmonary-embolism-detection/\":\n        # We sort by ImagePositionPatient (z-coordinate) or by SliceLocation\n        slices = [pydicom.dcmread(dcm_path + \"/\" + file) for file in listdir(dcm_path)]\n        slices.sort(key = lambda x: float(x.ImagePositionPatient[2]))\n        return slices    ","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:36:43.013146Z","iopub.execute_input":"2024-02-27T12:36:43.01398Z","iopub.status.idle":"2024-02-27T12:36:43.022069Z","shell.execute_reply.started":"2024-02-27T12:36:43.013929Z","shell.execute_reply":"2024-02-27T12:36:43.021116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"example = train_df.dcm_path.values[0]\nscans = load_scans(example)","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:36:43.023426Z","iopub.execute_input":"2024-02-27T12:36:43.023731Z","iopub.status.idle":"2024-02-27T12:36:44.507028Z","shell.execute_reply.started":"2024-02-27T12:36:43.023698Z","shell.execute_reply":"2024-02-27T12:36:44.505842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"example","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:36:44.508692Z","iopub.execute_input":"2024-02-27T12:36:44.509097Z","iopub.status.idle":"2024-02-27T12:36:44.521577Z","shell.execute_reply.started":"2024-02-27T12:36:44.509049Z","shell.execute_reply":"2024-02-27T12:36:44.520528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scans[0]","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:36:44.523101Z","iopub.execute_input":"2024-02-27T12:36:44.523515Z","iopub.status.idle":"2024-02-27T12:36:44.537314Z","shell.execute_reply.started":"2024-02-27T12:36:44.52348Z","shell.execute_reply":"2024-02-27T12:36:44.536178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(scans[0].Rows)\nprint(scans[0].Columns) #first dicom file of the patient","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:36:44.53929Z","iopub.execute_input":"2024-02-27T12:36:44.539941Z","iopub.status.idle":"2024-02-27T12:36:44.548395Z","shell.execute_reply.started":"2024-02-27T12:36:44.539891Z","shell.execute_reply":"2024-02-27T12:36:44.547751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(1,2,figsize=(20,5))\nfor n in range(10):\n    image = scans[n].pixel_array.flatten()\n    rescaled_image = image * scans[n].RescaleSlope + scans[n].RescaleIntercept\n    sns.distplot(image.flatten(), ax=ax[0]);\n    sns.distplot(rescaled_image.flatten(), ax=ax[1])\nax[0].set_title(\"Raw pixel array distributions for 10 examples\")\nax[1].set_title(\"HU unit distributions for 10 examples\");","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:36:44.54958Z","iopub.execute_input":"2024-02-27T12:36:44.553627Z","iopub.status.idle":"2024-02-27T12:37:07.116416Z","shell.execute_reply.started":"2024-02-27T12:36:44.553577Z","shell.execute_reply":"2024-02-27T12:37:07.115312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def set_outside_scanner_to_air(raw_pixelarrays):\n    # in OSIC we find outside-scanner-regions with raw-values of -2000. \n    # Let's threshold between air (0) and this default (-2000) using -1000\n    raw_pixelarrays[raw_pixelarrays <= -1000] = 0\n    return raw_pixelarrays","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:37:07.118201Z","iopub.execute_input":"2024-02-27T12:37:07.118723Z","iopub.status.idle":"2024-02-27T12:37:07.12383Z","shell.execute_reply.started":"2024-02-27T12:37:07.118683Z","shell.execute_reply":"2024-02-27T12:37:07.122773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def transform_to_hu(slices):\n    images = np.stack([file.pixel_array for file in slices])\n    images = images.astype(np.int16)\n\n    images = set_outside_scanner_to_air(images)\n    \n    # convert to HU\n    for n in range(len(slices)):\n        \n        intercept = slices[n].RescaleIntercept\n        slope = slices[n].RescaleSlope\n        \n        if slope != 1:\n            images[n] = slope * images[n].astype(np.float64)\n            images[n] = images[n].astype(np.int16)\n            \n        images[n] += np.int16(intercept)\n    \n    return np.array(images, dtype=np.int16)","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:37:07.125274Z","iopub.execute_input":"2024-02-27T12:37:07.125538Z","iopub.status.idle":"2024-02-27T12:37:07.138459Z","shell.execute_reply.started":"2024-02-27T12:37:07.125505Z","shell.execute_reply":"2024-02-27T12:37:07.137324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hu_scans = transform_to_hu(scans)\nhu_scans.shape","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:37:07.139939Z","iopub.execute_input":"2024-02-27T12:37:07.140279Z","iopub.status.idle":"2024-02-27T12:37:07.342321Z","shell.execute_reply.started":"2024-02-27T12:37:07.140232Z","shell.execute_reply":"2024-02-27T12:37:07.341398Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(1,4,figsize=(20,3))\nax[0].set_title(\"Original CT-scan\")\nax[0].imshow(scans[0].pixel_array, cmap=\"bone\")\nax[1].set_title(\"Pixelarray distribution\");\nsns.distplot(scans[0].pixel_array.flatten(), ax=ax[1]);\n\nax[2].set_title(\"CT-scan in HU\")\nax[2].imshow(hu_scans[0], cmap=\"bone\")\nax[3].set_title(\"HU values distribution\");\nsns.distplot(hu_scans[0].flatten(), ax=ax[3]);","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:37:07.343601Z","iopub.execute_input":"2024-02-27T12:37:07.343895Z","iopub.status.idle":"2024-02-27T12:37:10.255468Z","shell.execute_reply.started":"2024-02-27T12:37:07.343859Z","shell.execute_reply":"2024-02-27T12:37:10.254647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pydicom.multival.MultiValue","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:37:10.256733Z","iopub.execute_input":"2024-02-27T12:37:10.25724Z","iopub.status.idle":"2024-02-27T12:37:10.262695Z","shell.execute_reply.started":"2024-02-27T12:37:10.257189Z","shell.execute_reply":"2024-02-27T12:37:10.261863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"N = 1000;\ndef get_window_value(feature):\n    if type(feature) == pydicom.multival.MultiValue:\n        return np.int(feature[0])\n    else:\n        return np.int(feature)\n\npixelspacing_r = []\npixelspacing_c = []\nslice_thicknesses = []\npatient_id = []\npatient_pth = []\nrow_values = []\ncolumn_values = []\nwindow_widths = []\nwindow_levels = []\n\npatients = train_df.SeriesInstanceUID.unique()[0:N]\n\nfor patient in patients:\n    patient_id.append(patient)\n    \n    path = train_df[train_df.SeriesInstanceUID == patient].dcm_path.values[0]\n    \n    example_dcm = listdir(path)[0]\n    patient_pth.append(path)\n    dataset = pydicom.dcmread(path + \"/\" + example_dcm)\n    \n    window_widths.append(get_window_value(dataset.WindowWidth))\n    window_levels.append(get_window_value(dataset.WindowCenter))\n    \n    spacing = dataset.PixelSpacing\n    slice_thicknesses.append(dataset.SliceThickness)\n    \n    row_values.append(dataset.Rows)\n    column_values.append(dataset.Columns)\n    pixelspacing_r.append(spacing[0])\n    pixelspacing_c.append(spacing[1])\n    \nscan_properties = pd.DataFrame(data=patient_id, columns=[\"patient\"])\nscan_properties.loc[:, \"rows\"] = row_values\nscan_properties.loc[:, \"columns\"] = column_values\nscan_properties.loc[:, \"area\"] = scan_properties[\"rows\"] * scan_properties[\"columns\"]\nscan_properties.loc[:, \"pixelspacing_r\"] = pixelspacing_r\nscan_properties.loc[:, \"pixelspacing_c\"] = pixelspacing_c\nscan_properties.loc[:, \"pixelspacing_area\"] = scan_properties.pixelspacing_r * scan_properties.pixelspacing_c\nscan_properties.loc[:, \"slice_thickness\"] = slice_thicknesses\nscan_properties.loc[:, \"patient_pth\"] = patient_pth\nscan_properties.loc[:, \"window_width\"] = window_widths\nscan_properties.loc[:, \"window_level\"] = window_levels\nscan_properties.head()","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:37:10.264339Z","iopub.execute_input":"2024-02-27T12:37:10.265324Z","iopub.status.idle":"2024-02-27T12:42:36.715186Z","shell.execute_reply.started":"2024-02-27T12:37:10.265263Z","shell.execute_reply":"2024-02-27T12:42:36.713918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scan_properties.describe()","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:42:36.716738Z","iopub.execute_input":"2024-02-27T12:42:36.717051Z","iopub.status.idle":"2024-02-27T12:42:36.768226Z","shell.execute_reply.started":"2024-02-27T12:42:36.717017Z","shell.execute_reply":"2024-02-27T12:42:36.767128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(1,2,figsize=(20,5))\nsns.distplot(pixelspacing_r, ax=ax[0], color=\"Limegreen\", kde=True)\nax[0].set_title(\"Pixel spacing distribution \\n in row direction \")\nax[0].set_ylabel(\"Counts in train\")\nax[0].set_xlabel(\"mm\")\nsns.distplot(pixelspacing_c, ax=ax[1], color=\"Mediumseagreen\", kde= True)\nax[1].set_title(\"Pixel spacing distribution \\n in column direction\");\nax[1].set_ylabel(\"Counts in train\");\nax[1].set_xlabel(\"mm\");","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:42:36.76947Z","iopub.execute_input":"2024-02-27T12:42:36.769718Z","iopub.status.idle":"2024-02-27T12:42:37.471049Z","shell.execute_reply.started":"2024-02-27T12:42:36.769688Z","shell.execute_reply":"2024-02-27T12:42:37.470043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"counts = scan_properties.groupby([\"rows\", \"columns\"]).size()\nprint(counts)\ncounts = counts.unstack()\nprint(counts)\ncounts.fillna(0, inplace=True)\n\n\nfig, ax = plt.subplots(1,2,figsize=(20,5))\nsns.distplot(slice_thicknesses, color=\"orangered\", kde=False, ax=ax[0])\nax[0].set_title(\"Slice thicknesses of all patients\");\nax[0].set_xlabel(\"Slice thickness in mm\")\nax[0].set_ylabel(\"Counts in train\");\n\nfor n in counts.index.values:\n    for m in counts.columns.values:\n        ax[1].scatter(n, m, s=counts.loc[n,m], c=\"midnightblue\")\nax[1].set_xlabel(\"rows\")\nax[1].set_ylabel(\"columns\")\nax[1].set_title(\"Pixel area of ct-scan per patient\");","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:42:37.472806Z","iopub.execute_input":"2024-02-27T12:42:37.473088Z","iopub.status.idle":"2024-02-27T12:42:38.045118Z","shell.execute_reply.started":"2024-02-27T12:42:37.473056Z","shell.execute_reply":"2024-02-27T12:42:38.04415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scan_properties[\"r_distance\"] = scan_properties.pixelspacing_r * scan_properties.rows\nscan_properties[\"c_distance\"] = scan_properties.pixelspacing_c * scan_properties[\"columns\"]\nscan_properties[\"area_cm2\"] = 0.1* scan_properties[\"r_distance\"] * 0.1*scan_properties[\"c_distance\"]\nscan_properties[\"slice_volume_cm3\"] = 0.1*scan_properties.slice_thickness * scan_properties.area_cm2","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:42:38.046418Z","iopub.execute_input":"2024-02-27T12:42:38.046666Z","iopub.status.idle":"2024-02-27T12:42:38.058172Z","shell.execute_reply.started":"2024-02-27T12:42:38.046636Z","shell.execute_reply":"2024-02-27T12:42:38.056927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(1,2,figsize=(20,5))\nsns.distplot(scan_properties.area_cm2, ax=ax[0], color=\"purple\")\nsns.distplot(scan_properties.slice_volume_cm3, ax=ax[1], color=\"magenta\")\nax[0].set_title(\"CT-slice area in $cm^{2}$\")\nax[1].set_title(\"CT-slice volume in $cm^{3}$\")\nax[0].set_xlabel(\"$cm^{2}$\")\nax[1].set_xlabel(\"$cm^{3}$\");","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:42:38.059546Z","iopub.execute_input":"2024-02-27T12:42:38.059908Z","iopub.status.idle":"2024-02-27T12:42:39.352644Z","shell.execute_reply.started":"2024-02-27T12:42:38.059859Z","shell.execute_reply":"2024-02-27T12:42:39.351709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Smallest and larges CT-slice area**","metadata":{}},{"cell_type":"code","source":"max_path = scan_properties[\n    scan_properties.area_cm2 == scan_properties.area_cm2.max()].patient_pth.values[0]\nmin_path = scan_properties[\n    scan_properties.area_cm2 == scan_properties.area_cm2.min()].patient_pth.values[0]\n\nmin_scans = load_scans(min_path)\nmin_hu_scans = transform_to_hu(min_scans)\n\nmax_scans = load_scans(max_path)\nmax_hu_scans = transform_to_hu(max_scans)\n\nbackground_water_hu_scans = max_hu_scans.copy()","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:42:39.354366Z","iopub.execute_input":"2024-02-27T12:42:39.354907Z","iopub.status.idle":"2024-02-27T12:42:46.523311Z","shell.execute_reply.started":"2024-02-27T12:42:39.354856Z","shell.execute_reply":"2024-02-27T12:42:46.522231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def set_manual_window(hu_image, custom_center, custom_width):\n    w_image = hu_image.copy()\n    min_value = custom_center - (custom_width/2)\n    max_value = custom_center + (custom_width/2)\n    w_image[w_image < min_value] = min_value\n    w_image[w_image > max_value] = max_value\n    return w_image","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:42:46.526953Z","iopub.execute_input":"2024-02-27T12:42:46.527256Z","iopub.status.idle":"2024-02-27T12:42:46.53394Z","shell.execute_reply.started":"2024-02-27T12:42:46.527223Z","shell.execute_reply":"2024-02-27T12:42:46.532916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(1,2,figsize=(20,10))\nax[0].imshow(set_manual_window(min_hu_scans[np.int(len(min_hu_scans)/2)], -700, 255), cmap=\"YlGnBu\")\nax[1].imshow(set_manual_window(max_hu_scans[np.int(len(max_hu_scans)/2)], -700, 255), cmap=\"YlGnBu\");\n\nax[0].set_title(\"CT-scan with small slice area\")\nax[1].set_title(\"CT-scan with large slice area\");\n\nfor n in range(2):\n    ax[n].axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:42:46.5354Z","iopub.execute_input":"2024-02-27T12:42:46.535742Z","iopub.status.idle":"2024-02-27T12:42:47.00938Z","shell.execute_reply.started":"2024-02-27T12:42:46.535695Z","shell.execute_reply":"2024-02-27T12:42:47.008551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(1,2,figsize=(20,5))\nsns.distplot(max_hu_scans[np.int(len(max_hu_scans)/2)].flatten(), kde=False, ax=ax[1])\nax[1].set_title(\"Large area image\")\nsns.distplot(min_hu_scans[np.int(len(min_hu_scans)/2)].flatten(), kde=False, ax=ax[0])\nax[0].set_title(\"Small area image\")\nax[0].set_xlabel(\"HU values\")\nax[1].set_xlabel(\"HU values\");","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:42:47.010762Z","iopub.execute_input":"2024-02-27T12:42:47.01176Z","iopub.status.idle":"2024-02-27T12:42:47.926156Z","shell.execute_reply.started":"2024-02-27T12:42:47.011709Z","shell.execute_reply":"2024-02-27T12:42:47.925278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Smallest and largest CT-slice volume**","metadata":{}},{"cell_type":"code","source":"max_path = scan_properties[\n    scan_properties.slice_volume_cm3 == scan_properties.slice_volume_cm3.max()].patient_pth.values[0]\nmin_path = scan_properties[\n    scan_properties.slice_volume_cm3 == scan_properties.slice_volume_cm3.min()].patient_pth.values[0]\n\nmin_scans = load_scans(min_path)\nmin_hu_scans = transform_to_hu(min_scans)\n\nmax_scans = load_scans(max_path)\nmax_hu_scans = transform_to_hu(max_scans)","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:42:47.927249Z","iopub.execute_input":"2024-02-27T12:42:47.927475Z","iopub.status.idle":"2024-02-27T12:42:50.263288Z","shell.execute_reply.started":"2024-02-27T12:42:47.927446Z","shell.execute_reply":"2024-02-27T12:42:50.262072Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(1,2,figsize=(20,5))\nsns.distplot(max_hu_scans[np.int(len(max_hu_scans)/2)].flatten(), kde=False, ax=ax[1])\nax[1].set_title(\"Large slice volume\")\nsns.distplot(min_hu_scans[np.int(len(min_hu_scans)/2)].flatten(), kde=False, ax=ax[0])\nax[0].set_title(\"Small slice volume\")\nax[0].set_xlabel(\"HU values\")\nax[1].set_xlabel(\"HU values\");","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:42:50.26729Z","iopub.execute_input":"2024-02-27T12:42:50.267569Z","iopub.status.idle":"2024-02-27T12:42:50.965609Z","shell.execute_reply.started":"2024-02-27T12:42:50.267537Z","shell.execute_reply":"2024-02-27T12:42:50.964574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_3d(image, threshold=700, color=\"navy\"):\n    \n    # Position the scan upright, \n    # so the head of the patient would be at the top facing the camera\n    p = image.transpose(2,1,0)\n    \n    verts, faces,_,_ = measure.marching_cubes_lewiner(p, threshold)\n\n    fig = plt.figure(figsize=(10, 10))\n    ax = fig.add_subplot(111, projection='3d')\n\n    # Fancy indexing: `verts[faces]` to generate a collection of triangles\n    mesh = Poly3DCollection(verts[faces], alpha=0.2)\n    mesh.set_facecolor(color)\n    ax.add_collection3d(mesh)\n\n    ax.set_xlim(0, p.shape[0])\n    ax.set_ylim(0, p.shape[1])\n    ax.set_zlim(0, p.shape[2])\n\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:42:50.967138Z","iopub.execute_input":"2024-02-27T12:42:50.967982Z","iopub.status.idle":"2024-02-27T12:42:50.977959Z","shell.execute_reply.started":"2024-02-27T12:42:50.967936Z","shell.execute_reply":"2024-02-27T12:42:50.976774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_3d(max_hu_scans)","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:42:50.986452Z","iopub.execute_input":"2024-02-27T12:42:50.986746Z","iopub.status.idle":"2024-02-27T12:43:10.387257Z","shell.execute_reply.started":"2024-02-27T12:42:50.986714Z","shell.execute_reply":"2024-02-27T12:43:10.38618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"old_distribution = max_hu_scans.flatten()\nprint(old_distribution)","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:43:10.38887Z","iopub.execute_input":"2024-02-27T12:43:10.389654Z","iopub.status.idle":"2024-02-27T12:43:10.405246Z","shell.execute_reply.started":"2024-02-27T12:43:10.389615Z","shell.execute_reply":"2024-02-27T12:43:10.404278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"example = train_df.dcm_path.values[0]\nscans = load_scans(example)\nhu_scans = transform_to_hu(scans)\nprint(hu_scans)","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:43:10.407917Z","iopub.execute_input":"2024-02-27T12:43:10.408414Z","iopub.status.idle":"2024-02-27T12:43:10.960972Z","shell.execute_reply.started":"2024-02-27T12:43:10.408373Z","shell.execute_reply":"2024-02-27T12:43:10.959755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_3d(hu_scans)","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:43:10.96225Z","iopub.execute_input":"2024-02-27T12:43:10.962659Z","iopub.status.idle":"2024-02-27T12:44:11.069876Z","shell.execute_reply.started":"2024-02-27T12:43:10.962608Z","shell.execute_reply":"2024-02-27T12:44:11.068952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(20,5))\nsns.distplot(old_distribution, label=\"weak 3d plot\", kde= True)\nsns.distplot(hu_scans.flatten(), label=\"strong 3d plot\", kde= True)\nplt.title(\"HU value distribution\")\nplt.legend();","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:44:11.07153Z","iopub.execute_input":"2024-02-27T12:44:11.07198Z","iopub.status.idle":"2024-02-27T12:48:23.038373Z","shell.execute_reply.started":"2024-02-27T12:44:11.071937Z","shell.execute_reply":"2024-02-27T12:48:23.037032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Image Overview**","metadata":{}},{"cell_type":"code","source":"fig, ax = plt.subplots(figsize=(8, 8))\nax.imshow(\n    dcm.dcmread(\"../input/rsna-str-pulmonary-embolism-detection/train/4833c9b6a5d0/57e3e3c5f910/f4fdc88f2ace.dcm\").pixel_array\n)","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:48:23.040695Z","iopub.execute_input":"2024-02-27T12:48:23.044203Z","iopub.status.idle":"2024-02-27T12:48:23.442947Z","shell.execute_reply.started":"2024-02-27T12:48:23.044125Z","shell.execute_reply":"2024-02-27T12:48:23.441814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_image = dcm.dcmread(\"../input/rsna-str-pulmonary-embolism-detection/train/4833c9b6a5d0/57e3e3c5f910/f4fdc88f2ace.dcm\").pixel_array\nprint('Image shape: ', test_image.shape)","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:48:23.444407Z","iopub.execute_input":"2024-02-27T12:48:23.444655Z","iopub.status.idle":"2024-02-27T12:48:23.453506Z","shell.execute_reply.started":"2024-02-27T12:48:23.444626Z","shell.execute_reply":"2024-02-27T12:48:23.452594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Now let's have a look at the images of the training set. In the following image, 15 of the images are just displayed. As a title of the images, I have set the maximum and minimum value of the pixel which explains why some of the slices are darker than the others","metadata":{}},{"cell_type":"code","source":"def read_dicom(file_path, show = False, cmap = 'gray'):\n    im = pydicom.read_file(file_path)\n    image_unscaled = im.pixel_array\n    image_rescaled = im.pixel_array * im.RescaleSlope + im.RescaleIntercept\n    \n    image_rescaled[image_rescaled <-1500] = 0\n    \n    if show:\n        f, axarr = plt.subplots(1,2)\n        axarr[0].imshow(image_unscaled, cmap = cmap)\n        axarr[0].axis(False)\n        axarr[0].set_title('no_rescale')\n        \n        axarr[1].imshow(image_rescaled, cmap = cmap)\n        axarr[1].axis(False)\n        axarr[1].set_title('windowed')\n    return image_rescaled\n","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:48:23.455396Z","iopub.execute_input":"2024-02-27T12:48:23.455742Z","iopub.status.idle":"2024-02-27T12:48:23.466162Z","shell.execute_reply.started":"2024-02-27T12:48:23.455706Z","shell.execute_reply":"2024-02-27T12:48:23.465147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"counter  = 0\nrows = 3\ncols = 5\nfig = plt.figure(figsize=(25,15))\nfor i in range(1, rows*cols+1):\n    img = read_dicom(train_image_file_paths[counter + i])\n    fig.add_subplot(rows, cols, i)\n    plt.imshow(img, cmap='gray')\n    plt.title(f'[{img.min()} {img.max()}]')\n    plt.axis(False)\n    fig.add_subplot\ncounter += rows*cols","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:48:23.467739Z","iopub.execute_input":"2024-02-27T12:48:23.468106Z","iopub.status.idle":"2024-02-27T12:48:25.56248Z","shell.execute_reply.started":"2024-02-27T12:48:23.468061Z","shell.execute_reply":"2024-02-27T12:48:25.56138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Now we will just have a look at a single exam and will observe if the images are of same patient or different patient.","metadata":{}},{"cell_type":"code","source":"selected_exam = 10\nEXAM_IDs = os.listdir(TRAIN_PATH)\n# print(np.asarray(EXAM_IDs).shape)\nSERIES = os.listdir(TRAIN_PATH + '/' + EXAM_IDs[selected_exam])\n# print(SERIES)\nfiles = os.listdir(TRAIN_PATH + '/' + EXAM_IDs[selected_exam] + '/' + SERIES[0])\n# print(files)\nsingle_experiment_files = [TRAIN_PATH + '/' + EXAM_IDs[selected_exam] + '/' + SERIES[0] + '/' + file for file in files]","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:48:25.564022Z","iopub.execute_input":"2024-02-27T12:48:25.564364Z","iopub.status.idle":"2024-02-27T12:48:25.63876Z","shell.execute_reply.started":"2024-02-27T12:48:25.564322Z","shell.execute_reply":"2024-02-27T12:48:25.638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"counter  = 0\nrows = 3\ncols = 5\nfig = plt.figure(figsize=(25,15))\nfor i in range(1, rows*cols+1):\n    fig.add_subplot(rows, cols, i)\n    plt.imshow(read_dicom(single_experiment_files[counter + i]), cmap='gray')\n    plt.axis(False)\n    fig.add_subplot\ncounter += rows*cols","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:48:25.640187Z","iopub.execute_input":"2024-02-27T12:48:25.640665Z","iopub.status.idle":"2024-02-27T12:48:29.838195Z","shell.execute_reply.started":"2024-02-27T12:48:25.640611Z","shell.execute_reply":"2024-02-27T12:48:29.837437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Sequential Read:**\nSo far we have been able to print the random CT-Scan Slices of the patient. However just printing and looking at the random slices don't make any sense because they have a particular sequence and they only make sense in that particualr sequence. Now in the following part of this notebook we will try to print the scans in a sequence.","metadata":{}},{"cell_type":"code","source":"def load_slice(paths):\n    slices = [pydicom.read_file(path) for path in paths]\n#     labels = [train_df[train_df.SOPInstanceUID == path[-16:-4]].pe_present_on_image.values for path in patient_image_paths]\n#     labels = np.array(labels).squeeze()\n    slices.sort(key = lambda x: int(x.InstanceNumber), reverse = False)\n#     labels.sort(key = lambda x: int(x.InstanceNumber), reverse = False)\n    return slices\ndef transform_to_hu(slices):\n    images = np.stack([file.pixel_array for file in slices])\n    images = images.astype(np.int16)\n\n    # convert ouside pixel-values to air:\n    # I'm using <= -1000 to be sure that other defaults are captured as well\n    images[images <= -1000] = 0\n    \n    # convert to HU\n    for n in range(len(slices)):    \n        intercept = slices[n].RescaleIntercept\n        slope = slices[n].RescaleSlope\n        if slope != 1:\n            images[n] = slope * images[n].astype(np.float64)\n            images[n] = images[n].astype(np.int16)      \n        images[n] += np.int16(intercept)\n    return np.array(images, dtype=np.int16)","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:48:29.839555Z","iopub.execute_input":"2024-02-27T12:48:29.840058Z","iopub.status.idle":"2024-02-27T12:48:29.850576Z","shell.execute_reply.started":"2024-02-27T12:48:29.84002Z","shell.execute_reply":"2024-02-27T12:48:29.849886Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"stacked_dicoms = load_slice(single_experiment_files)\nstacked_patient_pixels = transform_to_hu(stacked_dicoms)\n\ndef sample_stack(stack, rows=6, cols=6, start_with=10, show_every=3):\n    fig,ax = plt.subplots(rows,cols,figsize=[20,22])\n    for i in range(rows*cols):\n        ind = start_with + i*show_every\n        ax[int(i/rows),int(i % rows)].set_title(f'slice {ind}')\n        ax[int(i/rows),int(i % rows)].imshow(stack[ind],cmap='gray')\n        ax[int(i/rows),int(i % rows)].axis('off')\n    plt.show()\n\nprint(f'Total Number of Slices: {len(stacked_patient_pixels)}')\nsample_stack(stacked_patient_pixels, \n             show_every = int((len(stacked_patient_pixels)-10)/36))","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:48:29.851976Z","iopub.execute_input":"2024-02-27T12:48:29.852438Z","iopub.status.idle":"2024-02-27T12:48:36.331227Z","shell.execute_reply.started":"2024-02-27T12:48:29.852379Z","shell.execute_reply":"2024-02-27T12:48:36.330146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imageio.mimsave(f'stacked_{EXAM_IDs[selected_exam]}.gif', stacked_patient_pixels, duration=0.1)\ndisplay.Image(f'stacked_{EXAM_IDs[selected_exam]}.gif', format='png')","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:48:36.332617Z","iopub.execute_input":"2024-02-27T12:48:36.333001Z","iopub.status.idle":"2024-02-27T12:48:47.878524Z","shell.execute_reply.started":"2024-02-27T12:48:36.332966Z","shell.execute_reply":"2024-02-27T12:48:47.876954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"CT-Scans, DICOM files, Windowing Explained","metadata":{}},{"cell_type":"code","source":"def window_image(img, window_center,window_width, intercept, slope, rescale=True):\n    img = (img*slope +intercept) #for translation adjustments given in the dicom file. \n    img_min = window_center - window_width//2 #minimum HU level\n    img_max = window_center + window_width//2 #maximum HU level\n    img[img<img_min] = img_min #set img_min for all HU levels less than minimum HU level\n    img[img>img_max] = img_max #set img_max for all HU levels higher than maximum HU level\n    if rescale: \n        img = (img - img_min) / (img_max - img_min)*255.0 \n    return img\n    \ndef get_first_of_dicom_field_as_int(x):\n    #get x[0] as in int is x is a 'pydicom.multival.MultiValue', otherwise get int(x)\n    if type(x) == dcm.multival.MultiValue: return int(x[0])\n    else: return int(x)\n    \ndef get_windowing(data):\n    dicom_fields = [data[('0028','1050')].value, #window center\n                    data[('0028','1051')].value, #window width\n                    data[('0028','1052')].value, #intercept\n                    data[('0028','1053')].value] #slope\n    return [get_first_of_dicom_field_as_int(x) for x in dicom_fields]","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:48:47.880655Z","iopub.execute_input":"2024-02-27T12:48:47.881241Z","iopub.status.idle":"2024-02-27T12:48:47.907468Z","shell.execute_reply.started":"2024-02-27T12:48:47.881107Z","shell.execute_reply":"2024-02-27T12:48:47.906161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def view_images(files, title = '', aug = None, windowing = True):\n    width = 2\n    height = 2\n    fig, axs = plt.subplots(height, width, figsize=(15,15))\n    \n    for im in range(0, height * width):\n        data = dcm.dcmread(train_image_file_paths[im])\n        image = data.pixel_array\n        window_center , window_width, intercept, slope = get_windowing(data)\n        if windowing:\n            output = window_image(image, window_center, window_width, intercept, slope, rescale = False)\n        else:\n            output = image\n        i = im // width\n        j = im % width\n        axs[i,j].imshow(output, cmap=plt.cm.gray) \n        axs[i,j].axis('off')\n        \n    plt.suptitle(title)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:48:47.91302Z","iopub.execute_input":"2024-02-27T12:48:47.914003Z","iopub.status.idle":"2024-02-27T12:48:47.932007Z","shell.execute_reply.started":"2024-02-27T12:48:47.91394Z","shell.execute_reply":"2024-02-27T12:48:47.931135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"view_images(train_image_file_paths[3200:], title = 'Images with Windowing', windowing=False)","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:48:47.933545Z","iopub.execute_input":"2024-02-27T12:48:47.934047Z","iopub.status.idle":"2024-02-27T12:48:48.952479Z","shell.execute_reply.started":"2024-02-27T12:48:47.934002Z","shell.execute_reply":"2024-02-27T12:48:48.951551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"view_images(train_image_file_paths[3200:], 'Images with Windowing')","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:48:48.953834Z","iopub.execute_input":"2024-02-27T12:48:48.954091Z","iopub.status.idle":"2024-02-27T12:48:49.789316Z","shell.execute_reply.started":"2024-02-27T12:48:48.95406Z","shell.execute_reply":"2024-02-27T12:48:49.788343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = dcm.dcmread(train_image_file_paths[3203])\nimage = data.pixel_array\nwindow_center , window_width, intercept, slope = get_windowing(data)\noutput = window_image(image, window_center, window_width, intercept, slope, rescale = False)\nf, axarr = plt.subplots(1,2, figsize=(15,10))\naxarr[0].imshow(image, cmap='gray')\naxarr[1].imshow(output, cmap = 'gray')\nplt.suptitle(\"same image with windowing & without windowing\")","metadata":{"execution":{"iopub.status.busy":"2024-02-27T12:48:49.790942Z","iopub.execute_input":"2024-02-27T12:48:49.791314Z","iopub.status.idle":"2024-02-27T12:48:50.424169Z","shell.execute_reply.started":"2024-02-27T12:48:49.791245Z","shell.execute_reply":"2024-02-27T12:48:50.423095Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}