{"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":"markdown","source":"![](https://www.wias-berlin.de/research/ats/imaging/overview.png)wias-berlin.de","metadata":{}},{"cell_type":"markdown","source":"#Code by https://docs.fast.ai/medical.imaging.html#Dataset.set_pixels","metadata":{}},{"cell_type":"code","source":"!pip install fastai","metadata":{"execution":{"iopub.status.busy":"2021-07-20T00:58:43.548661Z","iopub.execute_input":"2021-07-20T00:58:43.549368Z","iopub.status.idle":"2021-07-20T00:59:46.208541Z","shell.execute_reply.started":"2021-07-20T00:58:43.549317Z","shell.execute_reply":"2021-07-20T00:59:46.207316Z"},"_kg_hide-output":true,"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install pydicom","metadata":{"execution":{"iopub.status.busy":"2021-07-20T00:59:57.567597Z","iopub.execute_input":"2021-07-20T00:59:57.568100Z","iopub.status.idle":"2021-07-20T01:00:05.042783Z","shell.execute_reply.started":"2021-07-20T00:59:57.568054Z","shell.execute_reply":"2021-07-20T01:00:05.041481Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings(\"ignore\", category=DeprecationWarning)","metadata":{"execution":{"iopub.status.busy":"2021-07-20T01:19:34.169672Z","iopub.execute_input":"2021-07-20T01:19:34.170268Z","iopub.status.idle":"2021-07-20T01:19:34.174430Z","shell.execute_reply.started":"2021-07-20T01:19:34.170214Z","shell.execute_reply":"2021-07-20T01:19:34.173698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pydicom\nfrom fastai.vision.all import *\nfrom fastai.medical.imaging import *\n\ntry:\n    import cv2\n    cv2.setNumThreads(0)\nexcept: pass\n\nimport seaborn as sns\nsns.set(style=\"whitegrid\")\nsns.set_context(\"paper\")","metadata":{"execution":{"iopub.status.busy":"2021-07-20T01:22:29.869977Z","iopub.execute_input":"2021-07-20T01:22:29.870467Z","iopub.status.idle":"2021-07-20T01:22:29.966680Z","shell.execute_reply.started":"2021-07-20T01:22:29.870428Z","shell.execute_reply":"2021-07-20T01:22:29.965264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install scikit-image","metadata":{"execution":{"iopub.status.busy":"2021-07-20T01:00:21.853163Z","iopub.execute_input":"2021-07-20T01:00:21.853591Z","iopub.status.idle":"2021-07-20T01:00:29.149395Z","shell.execute_reply.started":"2021-07-20T01:00:21.853554Z","shell.execute_reply":"2021-07-20T01:00:29.147852Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pip install kornia===0.2.0","metadata":{"execution":{"iopub.status.busy":"2021-07-20T01:01:36.646310Z","iopub.execute_input":"2021-07-20T01:01:36.646785Z","iopub.status.idle":"2021-07-20T01:01:44.855112Z","shell.execute_reply.started":"2021-07-20T01:01:36.646728Z","shell.execute_reply":"2021-07-20T01:01:44.853863Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Amrit Virdee  https://www.kaggle.com/avirdee/rsna-miccai-initial-fmi-fastai/notebook\n\nmri = '../input/rsna-miccai-brain-tumor-radiogenomic-classification'\ntrain_files = get_dicom_files(f'{source}/train')\nlabels = pd.read_csv(f'{source}/train_labels.csv')\nprint(os.listdir(source))","metadata":{"execution":{"iopub.status.busy":"2021-07-20T01:23:21.972083Z","iopub.execute_input":"2021-07-20T01:23:21.972495Z","iopub.status.idle":"2021-07-20T01:23:27.856991Z","shell.execute_reply.started":"2021-07-20T01:23:21.972460Z","shell.execute_reply":"2021-07-20T01:23:27.855972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#get dicom files\nitems = get_dicom_files(mri, recurse=True, folders='train')\nitems","metadata":{"execution":{"iopub.status.busy":"2021-07-20T01:23:42.492363Z","iopub.execute_input":"2021-07-20T01:23:42.492761Z","iopub.status.idle":"2021-07-20T01:23:47.423767Z","shell.execute_reply.started":"2021-07-20T01:23:42.492714Z","shell.execute_reply":"2021-07-20T01:23:47.423002Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Sample_DCM = Path('../input/rsna-miccai-brain-tumor-radiogenomic-classification/train/00688/T2w/Image-273.dcm')\ndcm = Sample_DCM.dcmread()\ndcm","metadata":{"execution":{"iopub.status.busy":"2021-07-20T01:38:53.504281Z","iopub.execute_input":"2021-07-20T01:38:53.505046Z","iopub.status.idle":"2021-07-20T01:38:53.557638Z","shell.execute_reply.started":"2021-07-20T01:38:53.504999Z","shell.execute_reply":"2021-07-20T01:38:53.556876Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(dcm)","metadata":{"execution":{"iopub.status.busy":"2021-07-20T01:37:56.403583Z","iopub.execute_input":"2021-07-20T01:37:56.404079Z","iopub.status.idle":"2021-07-20T01:37:56.412271Z","shell.execute_reply.started":"2021-07-20T01:37:56.404039Z","shell.execute_reply":"2021-07-20T01:37:56.410682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dcm.pixels","metadata":{"execution":{"iopub.status.busy":"2021-07-20T01:39:17.042362Z","iopub.execute_input":"2021-07-20T01:39:17.043071Z","iopub.status.idle":"2021-07-20T01:39:17.082762Z","shell.execute_reply.started":"2021-07-20T01:39:17.043007Z","shell.execute_reply":"2021-07-20T01:39:17.081459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#None\n\nscaled_px uses RescaleSlope and RescaleIntercept values to correctly scale the image so that they represent the correct tissue densities.","metadata":{}},{"cell_type":"code","source":"plt.hist(dcm.pixels.flatten().numpy());","metadata":{"execution":{"iopub.status.busy":"2021-07-20T01:39:35.247293Z","iopub.execute_input":"2021-07-20T01:39:35.247721Z","iopub.status.idle":"2021-07-20T01:39:35.533810Z","shell.execute_reply.started":"2021-07-20T01:39:35.247686Z","shell.execute_reply":"2021-07-20T01:39:35.532441Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.hist(dcm.scaled_px.flatten().numpy());","metadata":{"execution":{"iopub.status.busy":"2021-07-20T01:39:53.025733Z","iopub.execute_input":"2021-07-20T01:39:53.026156Z","iopub.status.idle":"2021-07-20T01:39:53.270276Z","shell.execute_reply.started":"2021-07-20T01:39:53.026122Z","shell.execute_reply":"2021-07-20T01:39:53.268642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"t_bin = dcm.pixels.freqhist_bins(n_bins=1)\nt_bin","metadata":{"execution":{"iopub.status.busy":"2021-07-20T01:40:14.329472Z","iopub.execute_input":"2021-07-20T01:40:14.329982Z","iopub.status.idle":"2021-07-20T01:40:14.364579Z","shell.execute_reply.started":"2021-07-20T01:40:14.329937Z","shell.execute_reply":"2021-07-20T01:40:14.363191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Tensor.freqhist_bins\n\nA function to split the range of pixel values into groups, such that each group has around the same number of pixels.","metadata":{}},{"cell_type":"code","source":"plt.hist(t_bin.numpy(), bins=t_bin, color='c')\nplt.plot(t_bin, torch.linspace(0,1,len(t_bin)));","metadata":{"execution":{"iopub.status.busy":"2021-07-20T01:40:31.911990Z","iopub.execute_input":"2021-07-20T01:40:31.912544Z","iopub.status.idle":"2021-07-20T01:40:32.176302Z","shell.execute_reply.started":"2021-07-20T01:40:31.912495Z","shell.execute_reply":"2021-07-20T01:40:32.175294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#with n_bins at 100","metadata":{}},{"cell_type":"code","source":"t_bin = dcm.pixels.freqhist_bins(n_bins=100)\nt_bin","metadata":{"execution":{"iopub.status.busy":"2021-07-20T01:43:10.609938Z","iopub.execute_input":"2021-07-20T01:43:10.610687Z","iopub.status.idle":"2021-07-20T01:43:10.636424Z","shell.execute_reply.started":"2021-07-20T01:43:10.610637Z","shell.execute_reply":"2021-07-20T01:43:10.635010Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.hist(t_bin.numpy(), bins=t_bin, color='c'); plt.plot(t_bin, torch.linspace(0,1,len(t_bin)));","metadata":{"execution":{"iopub.status.busy":"2021-07-20T01:43:49.258062Z","iopub.execute_input":"2021-07-20T01:43:49.258549Z","iopub.status.idle":"2021-07-20T01:43:49.538700Z","shell.execute_reply.started":"2021-07-20T01:43:49.258503Z","shell.execute_reply":"2021-07-20T01:43:49.537766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.hist(dcm.pixels.flatten().numpy(), bins=100);","metadata":{"execution":{"iopub.status.busy":"2021-07-20T01:44:10.043358Z","iopub.execute_input":"2021-07-20T01:44:10.044071Z","iopub.status.idle":"2021-07-20T01:44:10.482784Z","shell.execute_reply.started":"2021-07-20T01:44:10.044012Z","shell.execute_reply":"2021-07-20T01:44:10.481210Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Tensor.hist_scaled\n\nScales a tensor using freqhist_bins to values between 0 and 1\n\nThe test image has pixel values that range between -1000 and 2500","metadata":{}},{"cell_type":"code","source":"tensor_hists = dcm.pixels.hist_scaled()\nplt.hist(tensor_hists.flatten().numpy(), bins=100);","metadata":{"execution":{"iopub.status.busy":"2021-07-20T01:44:28.809353Z","iopub.execute_input":"2021-07-20T01:44:28.809824Z","iopub.status.idle":"2021-07-20T01:44:29.261038Z","shell.execute_reply.started":"2021-07-20T01:44:28.809786Z","shell.execute_reply":"2021-07-20T01:44:29.259781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Dataset.hist_scaled\n\nDataset.hist_scaled(brks=None, min_px=None, max_px=None)\n\nPixels scaled to a min_px and max_px value","metadata":{}},{"cell_type":"code","source":"data_scaled = dcm.hist_scaled()\nplt.imshow(data_scaled, cmap=plt.cm.bone);","metadata":{"execution":{"iopub.status.busy":"2021-07-20T01:46:07.498492Z","iopub.execute_input":"2021-07-20T01:46:07.499044Z","iopub.status.idle":"2021-07-20T01:46:07.793352Z","shell.execute_reply.started":"2021-07-20T01:46:07.498997Z","shell.execute_reply":"2021-07-20T01:46:07.792549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_scaled = dcm.hist_scaled(min_px=100, max_px=1000)\nplt.imshow(data_scaled, cmap=plt.cm.bone);","metadata":{"execution":{"iopub.status.busy":"2021-07-20T01:48:33.149653Z","iopub.execute_input":"2021-07-20T01:48:33.150523Z","iopub.status.idle":"2021-07-20T01:48:33.462521Z","shell.execute_reply.started":"2021-07-20T01:48:33.150472Z","shell.execute_reply":"2021-07-20T01:48:33.460939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Dicom images can contain a high amount of voxel values and windowing can be thought of as a means of manipulating these values in order to change the apperance of the image so particular structures are highlighted. A window has 2 values:\n\nl = window level or center aka brightness\n\nw = window width or range aka contrast","metadata":{}},{"cell_type":"markdown","source":"#Dataset.windowed\n\nDataset.windowed(w, l)","metadata":{}},{"cell_type":"code","source":"plt.imshow(dcm.windowed(*dicom_windows.brain), cmap=plt.cm.bone);","metadata":{"execution":{"iopub.status.busy":"2021-07-20T01:50:14.476927Z","iopub.execute_input":"2021-07-20T01:50:14.477394Z","iopub.status.idle":"2021-07-20T01:50:14.754081Z","shell.execute_reply.started":"2021-07-20T01:50:14.477357Z","shell.execute_reply":"2021-07-20T01:50:14.752715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Dataset.show\n\nDataset.show(frames=1, scale=True, cmap=<matplotlib.colors.LinearSegmentedColormap object at 0x7f1cb02ce6a0>, min_px=-1100, max_px=None, **kwargs)\n\nAdds functionality to view dicom images where each file may have more than 1 frame.","metadata":{}},{"cell_type":"code","source":"scales = False, True, dicom_windows.brain, dicom_windows.subdural\ntitles = 'raw','normalized','brain windowed','subdural windowed'\nfor s,a,t in zip(scales, subplots(2,2,imsize=4)[1].flat, titles):\n    dcm.show(scale=s, ax=a, title=t)","metadata":{"execution":{"iopub.status.busy":"2021-07-20T01:51:57.535971Z","iopub.execute_input":"2021-07-20T01:51:57.536443Z","iopub.status.idle":"2021-07-20T01:51:58.692187Z","shell.execute_reply.started":"2021-07-20T01:51:57.536406Z","shell.execute_reply":"2021-07-20T01:51:58.691002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dcm.show(cmap=plt.cm.gist_ncar, figsize=(6,6))","metadata":{"execution":{"iopub.status.busy":"2021-07-20T01:52:50.912302Z","iopub.execute_input":"2021-07-20T01:52:50.912714Z","iopub.status.idle":"2021-07-20T01:52:51.110295Z","shell.execute_reply.started":"2021-07-20T01:52:50.912676Z","shell.execute_reply":"2021-07-20T01:52:51.108980Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dcm.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-20T01:53:13.223304Z","iopub.execute_input":"2021-07-20T01:53:13.223783Z","iopub.status.idle":"2021-07-20T01:53:13.420802Z","shell.execute_reply.started":"2021-07-20T01:53:13.223729Z","shell.execute_reply":"2021-07-20T01:53:13.419470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Dataset.pct_in_window\n\nDataset.pct_in_window(dcm:Dataset, w, l)\n\n% of pixels in the window (w,l)","metadata":{}},{"cell_type":"code","source":"dcm.pct_in_window(*dicom_windows.brain)","metadata":{"execution":{"iopub.status.busy":"2021-07-20T01:54:01.462063Z","iopub.execute_input":"2021-07-20T01:54:01.462525Z","iopub.status.idle":"2021-07-20T01:54:01.472585Z","shell.execute_reply.started":"2021-07-20T01:54:01.462481Z","shell.execute_reply":"2021-07-20T01:54:01.471618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#uniform_blur2d\n\nuniform_blur2d(x, s)\n\nUniformly apply blurring","metadata":{}},{"cell_type":"code","source":"ims = dcm.hist_scaled(), uniform_blur2d(dcm.hist_scaled(), 20), uniform_blur2d(dcm.hist_scaled(), 50)\nshow_images(ims, titles=('original', 'blurred 20', 'blurred 50'))","metadata":{"execution":{"iopub.status.busy":"2021-07-20T01:54:55.842383Z","iopub.execute_input":"2021-07-20T01:54:55.842870Z","iopub.status.idle":"2021-07-20T01:54:56.444378Z","shell.execute_reply.started":"2021-07-20T01:54:55.842822Z","shell.execute_reply":"2021-07-20T01:54:56.443188Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#gauss_blur2d\n\ngauss_blur2d(x, s)\n\nApply gaussian_blur2d kornia filter","metadata":{}},{"cell_type":"code","source":"ims = dcm.hist_scaled(), gauss_blur2d(dcm.hist_scaled(), 20), gauss_blur2d(dcm.hist_scaled(), 50)\nshow_images(ims, titles=('original', 'gauss_blur 20', 'gauss_blur 50'))","metadata":{"execution":{"iopub.status.busy":"2021-07-20T01:55:54.569490Z","iopub.execute_input":"2021-07-20T01:55:54.570379Z","iopub.status.idle":"2021-07-20T01:55:55.119834Z","shell.execute_reply.started":"2021-07-20T01:55:54.570296Z","shell.execute_reply":"2021-07-20T01:55:55.118396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Tensor.mask_from_blur\n\nTensor.mask_from_blur(x:Tensor, window, sigma=0.3, thresh=0.05, remove_max=True)\n\nCreate a mask from the blurred image\n\n#Dataset.mask_from_blur\n\nDataset.mask_from_blur(x:Dataset, window, sigma=0.3, thresh=0.05, remove_max=True)\n\nCreate a mask from the blurred image.","metadata":{}},{"cell_type":"code","source":"mask = dcm.mask_from_blur(dicom_windows.brain, sigma=0.9, thresh=0.1, remove_max=True)\nwind = dcm.windowed(*dicom_windows.brain)\n\n_,ax = subplots(1,3)\nshow_image(wind, ax=ax[0], title='window')\nshow_image(mask, alpha=0.5, cmap=plt.cm.Reds, ax=ax[1], title='mask')\nshow_image(wind, ax=ax[2])\nshow_image(mask, alpha=0.5, cmap=plt.cm.Reds, ax=ax[2], title='window and mask');","metadata":{"execution":{"iopub.status.busy":"2021-07-20T01:56:51.076383Z","iopub.execute_input":"2021-07-20T01:56:51.077142Z","iopub.status.idle":"2021-07-20T01:56:55.843302Z","shell.execute_reply.started":"2021-07-20T01:56:51.077075Z","shell.execute_reply":"2021-07-20T01:56:55.841893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#mask2bbox\n\nmask2bbox(mask)","metadata":{}},{"cell_type":"code","source":"bbs = mask2bbox(mask)\nlo,hi = bbs\nshow_image(wind[lo[0]:hi[0],lo[1]:hi[1]]);","metadata":{"execution":{"iopub.status.busy":"2021-07-20T01:58:15.176199Z","iopub.execute_input":"2021-07-20T01:58:15.176710Z","iopub.status.idle":"2021-07-20T01:58:15.348435Z","shell.execute_reply.started":"2021-07-20T01:58:15.176662Z","shell.execute_reply":"2021-07-20T01:58:15.347699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#crop_resize\n\ncrop_resize(x, crops, new_sz)","metadata":{}},{"cell_type":"code","source":"px256 = crop_resize(to_device(wind[None]), bbs[...,None], 128)[0]\nshow_image(px256)\npx256.shape","metadata":{"execution":{"iopub.status.busy":"2021-07-20T02:02:11.678625Z","iopub.execute_input":"2021-07-20T02:02:11.679204Z","iopub.status.idle":"2021-07-20T02:02:11.838818Z","shell.execute_reply.started":"2021-07-20T02:02:11.679144Z","shell.execute_reply":"2021-07-20T02:02:11.837995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Comparing the original image with the image from using the mask and crop_resize function","metadata":{}},{"cell_type":"code","source":"_,axs = subplots(1,2)\ndcm.show(ax=axs[0])\nshow_image(px256, ax=axs[1]);","metadata":{"execution":{"iopub.status.busy":"2021-07-20T02:03:09.259217Z","iopub.execute_input":"2021-07-20T02:03:09.259778Z","iopub.status.idle":"2021-07-20T02:03:09.515627Z","shell.execute_reply.started":"2021-07-20T02:03:09.259724Z","shell.execute_reply":"2021-07-20T02:03:09.514020Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Tensor.to_nchan\n\nTensor.to_nchan(x:Tensor, wins, bins=None)\n\n#Dataset.to_nchan\n\nDataset.to_nchan(x:Dataset, wins, bins=None)\n\nto_nchan takes a tensor or a dicom as the input and returns multiple one channel images (the first depending on the choosen windows and a normalized image). Setting bins to 0 only returns the windowed image.","metadata":{}},{"cell_type":"code","source":"show_images(dcm.to_nchan([dicom_windows.brain], bins=0))","metadata":{"execution":{"iopub.status.busy":"2021-07-20T02:03:48.813764Z","iopub.execute_input":"2021-07-20T02:03:48.814231Z","iopub.status.idle":"2021-07-20T02:03:48.980591Z","shell.execute_reply.started":"2021-07-20T02:03:48.814192Z","shell.execute_reply":"2021-07-20T02:03:48.979632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_images(dcm.to_nchan([dicom_windows.brain], bins=None))","metadata":{"execution":{"iopub.status.busy":"2021-07-20T02:04:56.290405Z","iopub.execute_input":"2021-07-20T02:04:56.291043Z","iopub.status.idle":"2021-07-20T02:04:56.589557Z","shell.execute_reply.started":"2021-07-20T02:04:56.290997Z","shell.execute_reply":"2021-07-20T02:04:56.588710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Tensor.to_3chan\n\nTensor.to_3chan(x:Tensor, win1, win2, bins=None)\n\n#Dataset.to_3chan\n\nDataset.to_3chan(x:Dataset, win1, win2, bins=None)","metadata":{}},{"cell_type":"code","source":"show_images(dcm.to_nchan([dicom_windows.brain,dicom_windows.subdural,dicom_windows.abdomen_soft]))","metadata":{"execution":{"iopub.status.busy":"2021-07-20T02:05:20.119961Z","iopub.execute_input":"2021-07-20T02:05:20.120687Z","iopub.status.idle":"2021-07-20T02:05:20.667650Z","shell.execute_reply.started":"2021-07-20T02:05:20.120625Z","shell.execute_reply":"2021-07-20T02:05:20.666818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Dataset.save_tif16\n\nDataset.save_tif16(x:Dataset'>), path, bins=None, compress=True)\n\nSave tensor or dicom image into tiff format.","metadata":{}},{"cell_type":"code","source":"_,axs=subplots(1,2)\nwith tempfile.TemporaryDirectory() as f:\n    f = Path(f)\n    dcm.save_jpg(f/'test.jpg', [dicom_windows.brain,dicom_windows.subdural])\n    show_image(Image.open(f/'test.jpg'), ax=axs[0])\n    dcm.save_tif16(f/'test.tif')\n    show_image(Image.open(str(f/'test.tif')), ax=axs[1]);","metadata":{"execution":{"iopub.status.busy":"2021-07-20T02:06:22.100133Z","iopub.execute_input":"2021-07-20T02:06:22.100546Z","iopub.status.idle":"2021-07-20T02:06:22.449650Z","shell.execute_reply.started":"2021-07-20T02:06:22.100511Z","shell.execute_reply":"2021-07-20T02:06:22.446833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Dataset.set_pixels","metadata":{}},{"cell_type":"code","source":"dcm.pixel_array.shape","metadata":{"execution":{"iopub.status.busy":"2021-07-20T02:07:19.152772Z","iopub.execute_input":"2021-07-20T02:07:19.153256Z","iopub.status.idle":"2021-07-20T02:07:19.161371Z","shell.execute_reply.started":"2021-07-20T02:07:19.153217Z","shell.execute_reply":"2021-07-20T02:07:19.160194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Dataset.zoom\n\nDataset.zoom(ratio)\n\nZoom image by specified ratio\n\nCheck to see the current size of the dicom image","metadata":{}},{"cell_type":"code","source":"dcm.zoom(7.0)\ndcm.show(); dcm.pixel_array.shape","metadata":{"execution":{"iopub.status.busy":"2021-07-20T02:07:37.152821Z","iopub.execute_input":"2021-07-20T02:07:37.153284Z","iopub.status.idle":"2021-07-20T02:07:42.080004Z","shell.execute_reply.started":"2021-07-20T02:07:37.153235Z","shell.execute_reply":"2021-07-20T02:07:42.078612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Dataset.zoom_to\n\nDataset.zoom_to(sz)\n\nChange image size to specified pixel size","metadata":{}},{"cell_type":"code","source":"dcm.zoom_to(200); dcm.pixel_array.shape","metadata":{"execution":{"iopub.status.busy":"2021-07-20T02:09:16.760478Z","iopub.execute_input":"2021-07-20T02:09:16.761111Z","iopub.status.idle":"2021-07-20T02:09:17.469430Z","shell.execute_reply.started":"2021-07-20T02:09:16.761055Z","shell.execute_reply":"2021-07-20T02:09:17.467961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dcm2 = TEST_DCM.dcmread()\ndcm2.zoom_to(90)\ntest_eq(dcm2.shape, (90,90))","metadata":{"execution":{"iopub.status.busy":"2021-07-20T02:09:59.940921Z","iopub.execute_input":"2021-07-20T02:09:59.941332Z","iopub.status.idle":"2021-07-20T02:09:59.978586Z","shell.execute_reply.started":"2021-07-20T02:09:59.941298Z","shell.execute_reply":"2021-07-20T02:09:59.977389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dcm2 = TEST_DCM.dcmread()\ndcm2.zoom(0.25)\ndcm2.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-20T02:10:19.162964Z","iopub.execute_input":"2021-07-20T02:10:19.163505Z","iopub.status.idle":"2021-07-20T02:10:19.334424Z","shell.execute_reply.started":"2021-07-20T02:10:19.163472Z","shell.execute_reply":"2021-07-20T02:10:19.333538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#All script by Fastai Medical Imaging. Helpers for working with DICOM files\n\nhttps://docs.fast.ai/medical.imaging.html#Dataset.set_pixels","metadata":{}}]}