{"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":"# 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":"2021-12-14T09:58:05.838176Z","iopub.execute_input":"2021-12-14T09:58:05.838501Z","iopub.status.idle":"2021-12-14T09:58:09.398009Z","shell.execute_reply.started":"2021-12-14T09:58:05.838468Z","shell.execute_reply":"2021-12-14T09:58:09.397316Z"},"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":"2021-12-14T09:58:11.16586Z","iopub.execute_input":"2021-12-14T09:58:11.166352Z","iopub.status.idle":"2021-12-14T09:58:11.170776Z","shell.execute_reply.started":"2021-12-14T09:58:11.166312Z","shell.execute_reply":"2021-12-14T09:58:11.169679Z"},"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":"2021-12-14T09:58:13.261967Z","iopub.execute_input":"2021-12-14T09:58:13.262259Z","iopub.status.idle":"2021-12-14T10:07:52.623513Z","shell.execute_reply.started":"2021-12-14T09:58:13.262231Z","shell.execute_reply":"2021-12-14T10:07:52.622815Z"},"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":"2021-12-14T04:58:31.642522Z","iopub.execute_input":"2021-12-14T04:58:31.642895Z","iopub.status.idle":"2021-12-14T04:58:37.350019Z","shell.execute_reply.started":"2021-12-14T04:58:31.642862Z","shell.execute_reply":"2021-12-14T04:58:37.34915Z"},"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":"2021-12-14T04:59:30.370052Z","iopub.execute_input":"2021-12-14T04:59:30.37038Z","iopub.status.idle":"2021-12-14T04:59:31.20254Z","shell.execute_reply.started":"2021-12-14T04:59:30.370346Z","shell.execute_reply":"2021-12-14T04:59:31.201618Z"},"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":"2021-12-14T05:01:14.264945Z","iopub.execute_input":"2021-12-14T05:01:14.265274Z","iopub.status.idle":"2021-12-14T05:01:14.520346Z","shell.execute_reply.started":"2021-12-14T05:01:14.265241Z","shell.execute_reply":"2021-12-14T05:01:14.519387Z"},"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":"2021-12-14T05:02:44.335435Z","iopub.execute_input":"2021-12-14T05:02:44.335708Z","iopub.status.idle":"2021-12-14T05:02:44.581409Z","shell.execute_reply.started":"2021-12-14T05:02:44.335679Z","shell.execute_reply":"2021-12-14T05:02:44.580435Z"},"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":"2021-12-14T05:03:51.813757Z","iopub.execute_input":"2021-12-14T05:03:51.814558Z","iopub.status.idle":"2021-12-14T05:03:53.933311Z","shell.execute_reply.started":"2021-12-14T05:03:51.814512Z","shell.execute_reply":"2021-12-14T05:03:53.932456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_df)\nprint(test_df)","metadata":{"execution":{"iopub.status.busy":"2021-12-14T05:50:00.320671Z","iopub.execute_input":"2021-12-14T05:50:00.321488Z","iopub.status.idle":"2021-12-14T05:50:00.347871Z","shell.execute_reply.started":"2021-12-14T05:50:00.321442Z","shell.execute_reply":"2021-12-14T05:50:00.34675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_df.shape)\nprint(test_df.shape)","metadata":{"execution":{"iopub.status.busy":"2021-12-14T05:50:00.350346Z","iopub.execute_input":"2021-12-14T05:50:00.350694Z","iopub.status.idle":"2021-12-14T05:50:00.356708Z","shell.execute_reply.started":"2021-12-14T05:50:00.350647Z","shell.execute_reply":"2021-12-14T05:50:00.355662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-14T05:50:00.358269Z","iopub.execute_input":"2021-12-14T05:50:00.35883Z","iopub.status.idle":"2021-12-14T05:50:00.3859Z","shell.execute_reply.started":"2021-12-14T05:50:00.358784Z","shell.execute_reply":"2021-12-14T05:50:00.385189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-14T05:50:00.387326Z","iopub.execute_input":"2021-12-14T05:50:00.38778Z","iopub.status.idle":"2021-12-14T05:50:00.399144Z","shell.execute_reply.started":"2021-12-14T05:50:00.387748Z","shell.execute_reply":"2021-12-14T05:50:00.398046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.describe()","metadata":{"execution":{"iopub.status.busy":"2021-12-14T05:50:00.40114Z","iopub.execute_input":"2021-12-14T05:50:00.401854Z","iopub.status.idle":"2021-12-14T05:50:01.42089Z","shell.execute_reply.started":"2021-12-14T05:50:00.401817Z","shell.execute_reply":"2021-12-14T05:50:01.419973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.describe()","metadata":{"execution":{"iopub.status.busy":"2021-12-14T05:50:01.422141Z","iopub.execute_input":"2021-12-14T05:50:01.422387Z","iopub.status.idle":"2021-12-14T05:50:01.679702Z","shell.execute_reply.started":"2021-12-14T05:50:01.422357Z","shell.execute_reply":"2021-12-14T05:50:01.678895Z"},"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":"2021-12-14T05:50:01.681199Z","iopub.execute_input":"2021-12-14T05:50:01.682159Z","iopub.status.idle":"2021-12-14T05:50:03.116213Z","shell.execute_reply.started":"2021-12-14T05:50:01.682102Z","shell.execute_reply":"2021-12-14T05:50:03.115235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.dcm_path","metadata":{"execution":{"iopub.status.busy":"2021-12-14T05:50:03.118964Z","iopub.execute_input":"2021-12-14T05:50:03.119618Z","iopub.status.idle":"2021-12-14T05:50:03.129428Z","shell.execute_reply.started":"2021-12-14T05:50:03.119577Z","shell.execute_reply":"2021-12-14T05:50:03.128409Z"},"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":"2021-12-14T05:50:03.130812Z","iopub.execute_input":"2021-12-14T05:50:03.131057Z","iopub.status.idle":"2021-12-14T05:50:03.144323Z","shell.execute_reply.started":"2021-12-14T05:50:03.131027Z","shell.execute_reply":"2021-12-14T05:50:03.14335Z"},"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":"2021-12-14T05:50:03.145603Z","iopub.execute_input":"2021-12-14T05:50:03.146078Z","iopub.status.idle":"2021-12-14T05:50:05.13658Z","shell.execute_reply.started":"2021-12-14T05:50:03.146042Z","shell.execute_reply":"2021-12-14T05:50:05.135568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"example","metadata":{"execution":{"iopub.status.busy":"2021-12-14T05:50:05.140747Z","iopub.execute_input":"2021-12-14T05:50:05.141146Z","iopub.status.idle":"2021-12-14T05:50:05.14841Z","shell.execute_reply.started":"2021-12-14T05:50:05.141074Z","shell.execute_reply":"2021-12-14T05:50:05.146851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scans[0]","metadata":{"execution":{"iopub.status.busy":"2021-12-14T05:50:05.149519Z","iopub.execute_input":"2021-12-14T05:50:05.149941Z","iopub.status.idle":"2021-12-14T05:50:05.169789Z","shell.execute_reply.started":"2021-12-14T05:50:05.149894Z","shell.execute_reply":"2021-12-14T05:50:05.166768Z"},"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":"2021-12-14T05:50:05.172836Z","iopub.execute_input":"2021-12-14T05:50:05.173313Z","iopub.status.idle":"2021-12-14T05:50:05.183911Z","shell.execute_reply.started":"2021-12-14T05:50:05.173252Z","shell.execute_reply":"2021-12-14T05:50:05.182592Z"},"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":"2021-12-14T05:50:05.186174Z","iopub.execute_input":"2021-12-14T05:50:05.186701Z","iopub.status.idle":"2021-12-14T05:50:26.387704Z","shell.execute_reply.started":"2021-12-14T05:50:05.186619Z","shell.execute_reply":"2021-12-14T05:50:26.386744Z"},"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":"2021-12-14T05:50:26.389262Z","iopub.execute_input":"2021-12-14T05:50:26.389823Z","iopub.status.idle":"2021-12-14T05:50:26.39568Z","shell.execute_reply.started":"2021-12-14T05:50:26.389775Z","shell.execute_reply":"2021-12-14T05:50:26.394748Z"},"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":"2021-12-14T05:50:26.397347Z","iopub.execute_input":"2021-12-14T05:50:26.397806Z","iopub.status.idle":"2021-12-14T05:50:26.410967Z","shell.execute_reply.started":"2021-12-14T05:50:26.397774Z","shell.execute_reply":"2021-12-14T05:50:26.410063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hu_scans = transform_to_hu(scans)\nhu_scans.shape","metadata":{"execution":{"iopub.status.busy":"2021-12-14T05:50:26.412441Z","iopub.execute_input":"2021-12-14T05:50:26.412704Z","iopub.status.idle":"2021-12-14T05:50:26.622918Z","shell.execute_reply.started":"2021-12-14T05:50:26.412665Z","shell.execute_reply":"2021-12-14T05:50:26.622044Z"},"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":"2021-12-14T05:50:26.624163Z","iopub.execute_input":"2021-12-14T05:50:26.624472Z","iopub.status.idle":"2021-12-14T05:50:29.525813Z","shell.execute_reply.started":"2021-12-14T05:50:26.624441Z","shell.execute_reply":"2021-12-14T05:50:29.524794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pydicom.multival.MultiValue","metadata":{"execution":{"iopub.status.busy":"2021-12-14T05:50:29.527418Z","iopub.execute_input":"2021-12-14T05:50:29.527665Z","iopub.status.idle":"2021-12-14T05:50:29.534052Z","shell.execute_reply.started":"2021-12-14T05:50:29.527626Z","shell.execute_reply":"2021-12-14T05:50:29.533395Z"},"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":"2021-12-14T05:50:29.535134Z","iopub.execute_input":"2021-12-14T05:50:29.535342Z","iopub.status.idle":"2021-12-14T05:55:43.548857Z","shell.execute_reply.started":"2021-12-14T05:50:29.535317Z","shell.execute_reply":"2021-12-14T05:55:43.54801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scan_properties.describe()","metadata":{"execution":{"iopub.status.busy":"2021-12-14T05:55:43.550266Z","iopub.execute_input":"2021-12-14T05:55:43.551157Z","iopub.status.idle":"2021-12-14T05:55:43.588805Z","shell.execute_reply.started":"2021-12-14T05:55:43.551098Z","shell.execute_reply":"2021-12-14T05:55:43.588103Z"},"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":"2021-12-14T05:55:43.589863Z","iopub.execute_input":"2021-12-14T05:55:43.590849Z","iopub.status.idle":"2021-12-14T05:55:44.224466Z","shell.execute_reply.started":"2021-12-14T05:55:43.590811Z","shell.execute_reply":"2021-12-14T05:55:44.223504Z"},"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":"2021-12-14T05:55:44.225729Z","iopub.execute_input":"2021-12-14T05:55:44.226051Z","iopub.status.idle":"2021-12-14T05:55:44.728356Z","shell.execute_reply.started":"2021-12-14T05:55:44.22602Z","shell.execute_reply":"2021-12-14T05:55:44.727697Z"},"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":"2021-12-14T05:55:44.729704Z","iopub.execute_input":"2021-12-14T05:55:44.729974Z","iopub.status.idle":"2021-12-14T05:55:44.742194Z","shell.execute_reply.started":"2021-12-14T05:55:44.729941Z","shell.execute_reply":"2021-12-14T05:55:44.741085Z"},"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":"2021-12-14T05:55:44.744501Z","iopub.execute_input":"2021-12-14T05:55:44.74522Z","iopub.status.idle":"2021-12-14T05:55:45.67058Z","shell.execute_reply.started":"2021-12-14T05:55:44.745172Z","shell.execute_reply":"2021-12-14T05:55:45.669757Z"},"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":"2021-12-14T05:55:45.672469Z","iopub.execute_input":"2021-12-14T05:55:45.673088Z","iopub.status.idle":"2021-12-14T05:55:58.675217Z","shell.execute_reply.started":"2021-12-14T05:55:45.673042Z","shell.execute_reply":"2021-12-14T05:55:58.674358Z"},"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":"2021-12-14T05:55:58.6763Z","iopub.execute_input":"2021-12-14T05:55:58.676525Z","iopub.status.idle":"2021-12-14T05:55:58.682709Z","shell.execute_reply.started":"2021-12-14T05:55:58.676498Z","shell.execute_reply":"2021-12-14T05:55:58.681742Z"},"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":"2021-12-14T05:55:58.688042Z","iopub.execute_input":"2021-12-14T05:55:58.688423Z","iopub.status.idle":"2021-12-14T05:55:59.12156Z","shell.execute_reply.started":"2021-12-14T05:55:58.688374Z","shell.execute_reply":"2021-12-14T05:55:59.120577Z"},"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":"2021-12-14T05:55:59.123046Z","iopub.execute_input":"2021-12-14T05:55:59.123376Z","iopub.status.idle":"2021-12-14T05:55:59.737448Z","shell.execute_reply.started":"2021-12-14T05:55:59.123334Z","shell.execute_reply":"2021-12-14T05:55:59.736541Z"},"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":"2021-12-14T05:55:59.738585Z","iopub.execute_input":"2021-12-14T05:55:59.738797Z","iopub.status.idle":"2021-12-14T05:56:03.224888Z","shell.execute_reply.started":"2021-12-14T05:55:59.73877Z","shell.execute_reply":"2021-12-14T05:56:03.223924Z"},"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":"2021-12-14T05:56:03.226383Z","iopub.execute_input":"2021-12-14T05:56:03.226642Z","iopub.status.idle":"2021-12-14T05:56:03.84195Z","shell.execute_reply.started":"2021-12-14T05:56:03.226614Z","shell.execute_reply":"2021-12-14T05:56:03.840849Z"},"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":"2021-12-14T05:56:03.843406Z","iopub.execute_input":"2021-12-14T05:56:03.84389Z","iopub.status.idle":"2021-12-14T05:56:03.852593Z","shell.execute_reply.started":"2021-12-14T05:56:03.843859Z","shell.execute_reply":"2021-12-14T05:56:03.851079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_3d(max_hu_scans)","metadata":{"execution":{"iopub.status.busy":"2021-12-14T05:56:03.854212Z","iopub.execute_input":"2021-12-14T05:56:03.854523Z","iopub.status.idle":"2021-12-14T05:56:20.665383Z","shell.execute_reply.started":"2021-12-14T05:56:03.854489Z","shell.execute_reply":"2021-12-14T05:56:20.664456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"old_distribution = max_hu_scans.flatten()\nprint(old_distribution)","metadata":{"execution":{"iopub.status.busy":"2021-12-14T05:56:20.666486Z","iopub.execute_input":"2021-12-14T05:56:20.666719Z","iopub.status.idle":"2021-12-14T05:56:20.686507Z","shell.execute_reply.started":"2021-12-14T05:56:20.66669Z","shell.execute_reply":"2021-12-14T05:56:20.685542Z"},"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":"2021-12-14T05:56:20.687935Z","iopub.execute_input":"2021-12-14T05:56:20.688398Z","iopub.status.idle":"2021-12-14T05:56:21.1794Z","shell.execute_reply.started":"2021-12-14T05:56:20.688366Z","shell.execute_reply":"2021-12-14T05:56:21.178464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_3d(hu_scans)","metadata":{"execution":{"iopub.status.busy":"2021-12-14T05:56:21.180806Z","iopub.execute_input":"2021-12-14T05:56:21.18133Z","iopub.status.idle":"2021-12-14T05:57:12.759082Z","shell.execute_reply.started":"2021-12-14T05:56:21.181279Z","shell.execute_reply":"2021-12-14T05:57:12.757925Z"},"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":"2021-12-14T05:57:12.761004Z","iopub.execute_input":"2021-12-14T05:57:12.761343Z","iopub.status.idle":"2021-12-14T06:01:01.40584Z","shell.execute_reply.started":"2021-12-14T05:57:12.761299Z","shell.execute_reply":"2021-12-14T06:01:01.404724Z"},"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":"2021-12-14T06:01:01.407733Z","iopub.execute_input":"2021-12-14T06:01:01.407974Z","iopub.status.idle":"2021-12-14T06:01:01.749821Z","shell.execute_reply.started":"2021-12-14T06:01:01.407942Z","shell.execute_reply":"2021-12-14T06:01:01.748919Z"},"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":"2021-12-14T06:01:01.751235Z","iopub.execute_input":"2021-12-14T06:01:01.751693Z","iopub.status.idle":"2021-12-14T06:01:01.759849Z","shell.execute_reply.started":"2021-12-14T06:01:01.751652Z","shell.execute_reply":"2021-12-14T06:01:01.759037Z"},"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":"2021-12-14T06:23:15.458965Z","iopub.execute_input":"2021-12-14T06:23:15.459328Z","iopub.status.idle":"2021-12-14T06:23:15.467969Z","shell.execute_reply.started":"2021-12-14T06:23:15.459292Z","shell.execute_reply":"2021-12-14T06:23:15.46734Z"},"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":"2021-12-14T06:23:17.126843Z","iopub.execute_input":"2021-12-14T06:23:17.127668Z","iopub.status.idle":"2021-12-14T06:23:18.987403Z","shell.execute_reply.started":"2021-12-14T06:23:17.127615Z","shell.execute_reply":"2021-12-14T06:23:18.986274Z"},"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":"2021-12-14T09:57:54.992595Z","iopub.execute_input":"2021-12-14T09:57:54.993026Z","iopub.status.idle":"2021-12-14T09:57:55.107056Z","shell.execute_reply.started":"2021-12-14T09:57:54.992888Z","shell.execute_reply":"2021-12-14T09:57:55.104983Z"},"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":"2021-12-14T06:38:46.656794Z","iopub.execute_input":"2021-12-14T06:38:46.657102Z","iopub.status.idle":"2021-12-14T06:38:49.218675Z","shell.execute_reply.started":"2021-12-14T06:38:46.657071Z","shell.execute_reply":"2021-12-14T06:38:49.218054Z"},"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":"2021-12-14T06:40:15.04165Z","iopub.execute_input":"2021-12-14T06:40:15.042063Z","iopub.status.idle":"2021-12-14T06:40:15.053079Z","shell.execute_reply.started":"2021-12-14T06:40:15.042021Z","shell.execute_reply":"2021-12-14T06:40:15.052374Z"},"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":"2021-12-14T06:40:58.880281Z","iopub.execute_input":"2021-12-14T06:40:58.880591Z","iopub.status.idle":"2021-12-14T06:41:07.007788Z","shell.execute_reply.started":"2021-12-14T06:40:58.88056Z","shell.execute_reply":"2021-12-14T06:41:07.006874Z"},"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":"2021-12-14T06:42:56.846334Z","iopub.execute_input":"2021-12-14T06:42:56.847199Z","iopub.status.idle":"2021-12-14T06:43:07.137914Z","shell.execute_reply.started":"2021-12-14T06:42:56.847145Z","shell.execute_reply":"2021-12-14T06:43:07.137232Z"},"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":"2021-12-14T10:11:21.888589Z","iopub.execute_input":"2021-12-14T10:11:21.889165Z","iopub.status.idle":"2021-12-14T10:11:22.133007Z","shell.execute_reply.started":"2021-12-14T10:11:21.889111Z","shell.execute_reply":"2021-12-14T10:11:22.131811Z"},"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":"2021-12-14T10:11:36.955945Z","iopub.execute_input":"2021-12-14T10:11:36.956252Z","iopub.status.idle":"2021-12-14T10:11:36.964671Z","shell.execute_reply.started":"2021-12-14T10:11:36.956204Z","shell.execute_reply":"2021-12-14T10:11:36.963741Z"},"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":"2021-12-14T10:13:14.248482Z","iopub.execute_input":"2021-12-14T10:13:14.24929Z","iopub.status.idle":"2021-12-14T10:13:15.144347Z","shell.execute_reply.started":"2021-12-14T10:13:14.249252Z","shell.execute_reply":"2021-12-14T10:13:15.143615Z"},"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":"2021-12-14T10:14:34.568013Z","iopub.execute_input":"2021-12-14T10:14:34.568285Z","iopub.status.idle":"2021-12-14T10:14:35.27543Z","shell.execute_reply.started":"2021-12-14T10:14:34.568257Z","shell.execute_reply":"2021-12-14T10:14:35.274461Z"},"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":"2021-12-14T10:17:41.685334Z","iopub.execute_input":"2021-12-14T10:17:41.685615Z","iopub.status.idle":"2021-12-14T10:17:42.237305Z","shell.execute_reply.started":"2021-12-14T10:17:41.685581Z","shell.execute_reply":"2021-12-14T10:17:42.236185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}