{"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":"import os\nimport numpy as np\nimport pydicom as dicom\nimport matplotlib.pyplot as plt\nimport scipy.ndimage","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def load_patient(path):\n    slices = [dicom.read_file(path+'/'+sl) for sl in os.listdir(path)]\n    slice_thickness = slices[0].SliceLocation - slices[1].SliceLocation\n    for sl in slices:\n        sl.slice_thickness = slice_thickness\n    \n    return slices","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"patient = load_patient(\"../input/osic-pulmonary-fibrosis-progression/train/ID00007637202177411956430\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"patient.sort(key = lambda x: x.InstanceNumber)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"patient[0].slice_thickness","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"patient[1].pixel_array","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def convert_HU(patient_slice):\n    image = patient_slice.pixel_array\n    image[image==-2000] = 0\n    \n    slope = float(patient_slice.RescaleSlope)\n    intersept = float(patient_slice.RescaleSlope)\n    image = slope*image\n    image = image + intersept\n    \n    return image","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"patient_slices = [convert_HU(sl) for sl in patient]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"patient_slices[1]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(patient_slices[1])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(patient_slices)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(6,5, figsize = [20,20])\nfor i,sl in enumerate(patient_slices):\n    ax[int(i/5),i%5].imshow(sl,cmap = 'winter')\n    \nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image3d = np.stack([sl for sl in patient_slices])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_image = scipy.ndimage.zoom(image3d, [12.8, 3/4,3/4])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_image.shape","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from skimage import measure","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vertex, faces, norm, val = measure.marching_cubes(new_image, allow_degenerate = True)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vertex, faces, norm, val = measure.marching_cubes(new_image, allow_degenerate = True)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from plotly.offline import iplot","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import plotly.figure_factory as FF","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x,y,z = zip(*vertex)\ncolormap=['rgb(236, 236, 212)','rgb(236, 236, 212)']\nfig = FF.create_trisurf(x=x, y= y, z=z, colormap = colormap, simplices = faces)\n\niplot(fig)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}