{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Install dicom-csv, deep-pipe.","execution_count":null},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"# Install dicom-csv, utils for gathering, aggregation and handling metadata from DICOM files.\n# This allows us to join DICOMs into volumes in a correct order without relying on file names.\n\n!pip install git+https://github.com/neuro-ml/dicom-csv.git","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# install deep-pipe, collection of tools for dl experiments,\n# here just for the vizualisation\n\n!pip install deep-pipe","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Collect metadata from all DICOMs.","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"from dicom_csv import join_tree, aggregate_images, load_series","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Single row of df corresponds to a single DICOM file (unique SOPInstanceUID in DICOM's slang)\n# First run takes ~3-4 mins, since it actually opens all available DICOMs inside a directory,\n# so I recommend you to store the resulting DataFrame\n\ndf = join_tree('/kaggle/input/osic-pulmonary-fibrosis-progression/train', verbose=True, relative=False, force=True)\ndf.head(3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df['NoError'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Single row of df_images corresponds to a single volume (unique SeriesInstanceUID in DICOM's slang)\n\n\ndf_images = aggregate_images(df.query('NoError == True'))\ndf_images.head(3)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Load images and visualize\n\n## RescaleSlope, RescaleIntercept\n\nRecall that there are two important parameters `RescaleSlope` and `RescaleIntercept`, which you would\ncompletely loose if you simply did pydicom.dcmread('image.dcm'), see for example https://stackoverflow.com/questions/10193971/rescale-slope-and-rescale-intercept .\n\n`load_series` takes them into account.","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"df_images.RescaleSlope.value_counts().sort_index()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_images.RescaleIntercept.value_counts().sort_index()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"image = load_series(df_images.iloc[0], orientation=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from dpipe.im.visualize import slice3d","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# There is a slide bar you could move to go over different slices (run notebook to see it).\n\nslice3d(image)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# DICOMs metadata\n\nPixel space (mm) distribution, SliceCount's distribution","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_images.columns","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_images['PixelArrayShape'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_images['PatientSex'].value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_images['PixelSpacing0'].value_counts().sort_index()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.hist(df_images['PixelSpacing0'], bins=15)\nplt.xlabel('Pixel spacing, mm.')\nplt.ylabel('Number of images.');","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_images['SlicesCount'].value_counts().sort_index()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.hist(df_images['SlicesCount'], bins=15)\nplt.xlabel('Number of slices in a single 3D image.')\nplt.ylabel('Number of images.');","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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}