{"cells":[{"metadata":{},"cell_type":"markdown","source":"This tutorial refers to using the `fmi` library which can be found [here](https://github.com/asvcode/fmi) and the [fastai](https://github.com/fastai) medical imaging module"},{"metadata":{},"cell_type":"markdown","source":"## Goal"},{"metadata":{},"cell_type":"markdown","source":"This quick tutorial goes through how `instance_show` and how `get_dicom_image` works.\n\n- Typically when you view slices of dicom images they do not display in sequence.  `instance_show` adds the functionality of displaying images by instance number\n\n- From a dataframe you can view dicom images by specifying the `key`"},{"metadata":{},"cell_type":"markdown","source":"## Dependancies"},{"metadata":{},"cell_type":"markdown","source":"Install the gdcm library - use the `pe-models` database to access gdcm.tar file"},{"metadata":{"trusted":true},"cell_type":"code","source":"!cp ../input/pe-models/gdcm.tar .\n!tar -xvzf gdcm.tar\n!conda install --offline ./gdcm/gdcm-2.8.9-py37h71b2a6d_0.tar.bz2\nprint(\"done\")","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"import dependancies"},{"metadata":{"trusted":true},"cell_type":"code","source":"from fastai.torch_core import set_seed\nfrom fastai.vision.all import *\nfrom fastai.medical.imaging import *\n\nimport gdcm\nmatplotlib.rcParams['image.cmap'] = 'bone'\n\nimport matplotlib as plt\nplt.rcParams.update({'figure.max_open_warning': 0})","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Currently the `fmi` package is still under construction and not pip installable. Instead install via the `fmipackage` dataset"},{"metadata":{"trusted":true},"cell_type":"code","source":"!cp -r ../input/fmipackage/fmi-master/* ./","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from fmi.explore import *\nfrom fmi.preprocessing import *\nfrom fmi.pipeline import *","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Sort Example"},{"metadata":{},"cell_type":"markdown","source":"To see how the sorting works we use one patient (ID00007637202177411956430) as an example.  There are 30 slices in this patient's folder"},{"metadata":{"trusted":true},"cell_type":"code","source":"source = '../input/osic-pulmonary-fibrosis-progression/train'\nos_items = get_dicom_files(source, folders='ID00007637202177411956430')\nos_items","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"We can view the images by using as example such as this:"},{"metadata":{"trusted":true},"cell_type":"code","source":"imgs = []\nfor filename in os_items:\n    file = dcmread(filename).pixel_array\n    img = TensorDicom(file)\n    imgs.append(img)\nshow_images(imgs, nrows=3)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"What you notice is that the images are not in sequence.  This can be easily rectified by using the `instance_show` function which also displays the instance number on the image."},{"metadata":{"trusted":true},"cell_type":"code","source":"instance_show(os_items, nrows=3)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## View example"},{"metadata":{},"cell_type":"markdown","source":"`get_dicom_image` easily allows you to view images based on the chosen dataframe key. For example we can reduce the dataframe created in [this](https://www.kaggle.com/avirdee/dicom-dataframe-tutorial) kernel"},{"metadata":{"trusted":true},"cell_type":"code","source":"dicom_dataframe = pd.read_csv('../input/pct-5/dicom_dataframe.csv', low_memory=False)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Condense the dataframe to some key values"},{"metadata":{"trusted":true},"cell_type":"code","source":"pct = dicom_dataframe[['PatientID', 'InstanceNumber', 'img_pct_window', 'img_mean', 'img_std']].sort_values(by=['img_pct_window'], ascending=False).reset_index(drop=True)\npct[:5]","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Now to view the images you can:"},{"metadata":{"trusted":true},"cell_type":"code","source":"get_dicom_image(pct[:30], 'img_pct_window', source=source) ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"This displays the images based in descending order (highest to lowest `img_pct_window` values)"},{"metadata":{},"cell_type":"markdown","source":"Sort by `img_mean`"},{"metadata":{"trusted":true},"cell_type":"code","source":"mean = dicom_dataframe[['PatientID', 'InstanceNumber', 'img_pct_window', 'img_mean', 'img_std']].sort_values(by=['img_mean'], ascending=False).reset_index(drop=True)\nmean[:5]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"get_dicom_image(mean[:30], 'img_mean', source=source) ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"For more about the `fmi` library: [view on Github](https://github.com/asvcode/fmi)"}],"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":4,"nbformat_minor":4}