{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Draft-1 \n\n- saw sample images for DICOMs \n- tried to make fastai datablock but that failed.! (comment if someone can help)\n\nI am very new to fastai and I want to test it in this competition and other competitions too. kindly connect, if someone want to colab or learn together:)","metadata":{}},{"cell_type":"code","source":"! pip install pydicom kornia opencv-python scikit-image nbdev \n","metadata":{"execution":{"iopub.status.busy":"2023-09-11T06:49:51.171368Z","iopub.execute_input":"2023-09-11T06:49:51.171709Z","iopub.status.idle":"2023-09-11T06:50:04.717531Z","shell.execute_reply.started":"2023-09-11T06:49:51.171681Z","shell.execute_reply":"2023-09-11T06:50:04.716256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install fastai2 -q\n","metadata":{"execution":{"iopub.status.busy":"2023-09-11T07:33:35.579261Z","iopub.execute_input":"2023-09-11T07:33:35.579726Z","iopub.status.idle":"2023-09-11T07:33:53.489257Z","shell.execute_reply.started":"2023-09-11T07:33:35.57969Z","shell.execute_reply":"2023-09-11T07:33:53.487883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from fastai.vision.all import *\nfrom fastai.callback.all import *\nfrom fastai.basics import *\nfrom fastai.medical.imaging import *\n\nimport pydicom\nimport pandas as pd\n\nimport numpy as np\nimport seaborn as sns\nimport os","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-09-11T07:34:50.209682Z","iopub.execute_input":"2023-09-11T07:34:50.210125Z","iopub.status.idle":"2023-09-11T07:34:50.516367Z","shell.execute_reply.started":"2023-09-11T07:34:50.21009Z","shell.execute_reply":"2023-09-11T07:34:50.515359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## I saw train.csv holds many important columns, lets see head of it \ntrain = pd.read_csv('/kaggle/input/rsna-2023-abdominal-trauma-detection/train.csv')\ntrain.head()\n# interesting ","metadata":{"execution":{"iopub.status.busy":"2023-09-11T06:50:18.237675Z","iopub.execute_input":"2023-09-11T06:50:18.238391Z","iopub.status.idle":"2023-09-11T06:50:18.276309Z","shell.execute_reply.started":"2023-09-11T06:50:18.238354Z","shell.execute_reply":"2023-09-11T06:50:18.275176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# What files to use?\nI see sooo many files and I dont know which one to use ?\n\n## Train/Test Images\nthis is what I found from competition description-\n\n**[train/test]_images/[patient_id]/[series_id]/[image_instance_number].**\n\n**dcm The CT scan data, in DICOM format. Scans from dozens of different CT machines have been reprocessed to use the run length encoded lossless compression format but retain other differences such as the number of bits per pixel, pixel range, and pixel representation. Expect to see roughly 1,100 patients in the test set.**\n\n","metadata":{}},{"cell_type":"code","source":"doc(get_dicom_files)","metadata":{"execution":{"iopub.status.busy":"2023-09-11T06:50:18.279058Z","iopub.execute_input":"2023-09-11T06:50:18.279543Z","iopub.status.idle":"2023-09-11T06:50:19.128156Z","shell.execute_reply.started":"2023-09-11T06:50:18.279511Z","shell.execute_reply":"2023-09-11T06:50:19.127159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\nitems = get_dicom_files('/kaggle/input/rsna-2023-abdominal-trauma-detection/train_images', recurse=True)\n## it took 5 mins to load! this is okay","metadata":{"execution":{"iopub.status.busy":"2023-09-11T06:50:19.129707Z","iopub.execute_input":"2023-09-11T06:50:19.130349Z","iopub.status.idle":"2023-09-11T06:57:20.974322Z","shell.execute_reply.started":"2023-09-11T06:50:19.130314Z","shell.execute_reply":"2023-09-11T06:57:20.972906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"let the dicom images load and I also read a discussion by theo viel for loading PNG's instead of Dicom's \n\nI think I will try that too, though I have a hypothesis, \n\nIn PNG format the data is lost, and the NN's may not learn much. Let me test that too, but for NOW I will only use DICOMS","metadata":{}},{"cell_type":"code","source":"items[0].dcmread().PixelData[:100]","metadata":{"execution":{"iopub.status.busy":"2023-09-11T07:30:30.12212Z","iopub.execute_input":"2023-09-11T07:30:30.122689Z","iopub.status.idle":"2023-09-11T07:30:30.137265Z","shell.execute_reply.started":"2023-09-11T07:30:30.122649Z","shell.execute_reply":"2023-09-11T07:30:30.136267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"items[0].dcmread().pixel_array.shape","metadata":{"execution":{"iopub.status.busy":"2023-09-11T07:31:07.404577Z","iopub.execute_input":"2023-09-11T07:31:07.405702Z","iopub.status.idle":"2023-09-11T07:31:07.447805Z","shell.execute_reply.started":"2023-09-11T07:31:07.405664Z","shell.execute_reply":"2023-09-11T07:31:07.446899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"items[0].dcmread().show()","metadata":{"execution":{"iopub.status.busy":"2023-09-11T07:31:21.807038Z","iopub.execute_input":"2023-09-11T07:31:21.807556Z","iopub.status.idle":"2023-09-11T07:31:22.239248Z","shell.execute_reply.started":"2023-09-11T07:31:21.807517Z","shell.execute_reply":"2023-09-11T07:31:22.238268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## convert into tensor\nitems[0].dcmread().pixels\n\n\nprint(f'RescaleIntercept: {items[0].dcmread().RescaleIntercept:1f}\\nRescaleSlope: {items[0].dcmread().RescaleSlope:1f}\\nMax pixel: '\n      f'{items[0].dcmread().pixels.max()}\\nMin pixel: {items[0].dcmread().pixels.min()}\\nShape: {items[0].dcmread().pixels.shape}')","metadata":{"execution":{"iopub.status.busy":"2023-09-11T07:37:35.674256Z","iopub.execute_input":"2023-09-11T07:37:35.674735Z","iopub.status.idle":"2023-09-11T07:37:35.811645Z","shell.execute_reply.started":"2023-09-11T07:37:35.674696Z","shell.execute_reply":"2023-09-11T07:37:35.810696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## about dicom\nThis image has a RescaleIntercept of -1024 and a RescaleSlope of 1. These two values allows for transforming pixel values into Hounsfield Units(HU). Densities of different tissues on CT scans are measured in HUs\n\nThe Hounsfield scale is a quantitative scale for describing radiodensity in medical CT and provides an accurate density for the type of tissue. On the Hounsfield scale, air is represented by a value of −1000 (black on the grey scale) and bone between +300 (cancellous bone) to +3000 (dense bone) (white on the grey scale), water has a value of 0 HUs and metals have a much higher HU range +2000 HUs.\n\nThe pixel values in the histogram above do not correctly correspond to tissue densities. For example most of the pixels are between pixel values 0 and 200 which correspond to water but this image is predominantly showing the lungs which are filled with air. Air on the Hounsfield scale is -1000 HUs.\n\nThis is where RescaleIntercept and RescaleSlope are important. Fastai provides a convenient way by using a function scaled_px to rescale the pixels with respect to RescaleIntercept and RescaleSlope.\n\n**rescaled pixel = pixel * RescaleSlope + RescaleIntercept**","metadata":{}},{"cell_type":"code","source":"tensor_dicom_scaled = items[0].dcmread().scaled_px #convert into tensor taking RescaleIntercept and RescaleSlope into consideration\nplt.hist(tensor_dicom_scaled.flatten(), color='c')\n","metadata":{"execution":{"iopub.status.busy":"2023-09-11T07:40:07.465942Z","iopub.execute_input":"2023-09-11T07:40:07.466437Z","iopub.status.idle":"2023-09-11T07:40:14.268101Z","shell.execute_reply.started":"2023-09-11T07:40:07.466389Z","shell.execute_reply":"2023-09-11T07:40:14.267071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'Max pixel: {tensor_dicom_scaled.max()}\\nMin pixel: {tensor_dicom_scaled.min()}')\n","metadata":{"execution":{"iopub.status.busy":"2023-09-11T07:40:46.940779Z","iopub.execute_input":"2023-09-11T07:40:46.94127Z","iopub.status.idle":"2023-09-11T07:40:46.951909Z","shell.execute_reply.started":"2023-09-11T07:40:46.941208Z","shell.execute_reply":"2023-09-11T07:40:46.950855Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"items[0].dcmread().show(max_px=None, min_px=300, figsize=(7,7))","metadata":{"execution":{"iopub.status.busy":"2023-09-11T07:41:32.703313Z","iopub.execute_input":"2023-09-11T07:41:32.703761Z","iopub.status.idle":"2023-09-11T07:41:32.994903Z","shell.execute_reply.started":"2023-09-11T07:41:32.703728Z","shell.execute_reply":"2023-09-11T07:41:32.993616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## water based areas = 0 HU's\nitems[0].dcmread().show(max_px=100, min_px= -100 , figsize=(7,7))","metadata":{"execution":{"iopub.status.busy":"2023-09-11T07:42:18.91162Z","iopub.execute_input":"2023-09-11T07:42:18.912088Z","iopub.status.idle":"2023-09-11T07:42:19.25766Z","shell.execute_reply.started":"2023-09-11T07:42:18.91205Z","shell.execute_reply":"2023-09-11T07:42:19.256671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## air filled areas = -1000 HU's\nitems[0].dcmread().show(max_px=None, min_px= -1000 , figsize=(7,7))","metadata":{"execution":{"iopub.status.busy":"2023-09-11T07:42:46.342503Z","iopub.execute_input":"2023-09-11T07:42:46.342962Z","iopub.status.idle":"2023-09-11T07:42:46.747605Z","shell.execute_reply.started":"2023-09-11T07:42:46.342918Z","shell.execute_reply":"2023-09-11T07:42:46.746623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## even view CT chamber\nitems[0].dcmread().show(max_px=-1000, min_px= -2000 , figsize=(7,7))","metadata":{"execution":{"iopub.status.busy":"2023-09-11T07:43:15.253643Z","iopub.execute_input":"2023-09-11T07:43:15.25413Z","iopub.status.idle":"2023-09-11T07:43:15.558311Z","shell.execute_reply.started":"2023-09-11T07:43:15.254091Z","shell.execute_reply":"2023-09-11T07:43:15.557374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\ndicom_dataframe = pd.DataFrame.from_dicoms(items[:1000])\ndicom_dataframe[:5]\n## ran it for 20 mins and it took a lot of time !! \n#  I want some other library which could load the dicoms faster.","metadata":{"execution":{"iopub.status.busy":"2023-09-11T06:57:21.028827Z","iopub.execute_input":"2023-09-11T06:57:21.031104Z","iopub.status.idle":"2023-09-11T06:58:14.993205Z","shell.execute_reply.started":"2023-09-11T06:57:21.03107Z","shell.execute_reply":"2023-09-11T06:58:14.992227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# making datablocks","metadata":{}},{"cell_type":"code","source":"source = Path('/kaggle/input/rsna-2023-abdominal-trauma-detection')\nget_x = lambda x: Path(x['fname'])\nget_y=ColReader('any_injury')\nbatch_tfms = aug_transforms(flip_vert=True, max_lighting=0.1, max_zoom=1.05, max_warp=0.)\nblocks = (ImageBlock(cls=PILDicom), CategoryBlock)\n","metadata":{"execution":{"iopub.status.busy":"2023-09-11T08:24:49.162554Z","iopub.execute_input":"2023-09-11T08:24:49.163028Z","iopub.status.idle":"2023-09-11T08:24:49.175398Z","shell.execute_reply.started":"2023-09-11T08:24:49.16299Z","shell.execute_reply":"2023-09-11T08:24:49.174196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rsna = DataBlock(blocks = blocks,\n                get_x = get_x,\n                 splitter = RandomSplitter(),\n                 item_tfms = Resize(128),\n                 get_y = get_y,\n                 batch_tfms = batch_tfms\n                )","metadata":{"execution":{"iopub.status.busy":"2023-09-11T08:24:50.601167Z","iopub.execute_input":"2023-09-11T08:24:50.601655Z","iopub.status.idle":"2023-09-11T08:24:50.609282Z","shell.execute_reply.started":"2023-09-11T08:24:50.601619Z","shell.execute_reply":"2023-09-11T08:24:50.608272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rsna.datasets(dicom_dataframe)","metadata":{"execution":{"iopub.status.busy":"2023-09-11T08:25:06.460972Z","iopub.execute_input":"2023-09-11T08:25:06.461472Z","iopub.status.idle":"2023-09-11T08:25:07.530636Z","shell.execute_reply.started":"2023-09-11T08:25:06.461424Z","shell.execute_reply":"2023-09-11T08:25:07.527057Z"},"trusted":true},"execution_count":null,"outputs":[]}],"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"}}