{"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":"code","source":"import pandas as pd\nimport numpy as np\nfrom fastcore.all import Path\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2023-09-10T22:36:41.280448Z","iopub.execute_input":"2023-09-10T22:36:41.28093Z","iopub.status.idle":"2023-09-10T22:36:42.060775Z","shell.execute_reply.started":"2023-09-10T22:36:41.280894Z","shell.execute_reply":"2023-09-10T22:36:42.059282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IN = Path('../input')\nRAW = IN/'rsna-2023-abdominal-trauma-detection'\nsns.set_style('whitegrid')","metadata":{"execution":{"iopub.status.busy":"2023-09-10T22:36:42.062879Z","iopub.execute_input":"2023-09-10T22:36:42.063438Z","iopub.status.idle":"2023-09-10T22:36:42.069767Z","shell.execute_reply.started":"2023-09-10T22:36:42.063407Z","shell.execute_reply":"2023-09-10T22:36:42.068541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# [x.stem + x.suffix for x in RAW.ls()] # to get file names\nsample_submission = pd.read_csv(RAW/'sample_submission.csv')\ntrain_series_meta = pd.read_csv(RAW/'train_series_meta.csv')\ntrain = pd.read_csv(RAW/'train.csv')\ntest_series_meta = pd.read_csv(RAW/'test_series_meta.csv')\nimage_level_labels = pd.read_csv(RAW/'image_level_labels.csv')\n\ntest_dicom_tags = pd.read_parquet(RAW/'test_dicom_tags.parquet')\ntrain_dicom_tags = pd.read_parquet(RAW/'train_dicom_tags.parquet')","metadata":{"execution":{"iopub.status.busy":"2023-09-10T22:36:42.266296Z","iopub.execute_input":"2023-09-10T22:36:42.266956Z","iopub.status.idle":"2023-09-10T22:36:48.630132Z","shell.execute_reply.started":"2023-09-10T22:36:42.26692Z","shell.execute_reply":"2023-09-10T22:36:48.628902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"assert train.patient_id.value_counts().max() == 1\nassert sample_submission.patient_id.value_counts().max() == 1\nprint('only one patient per submission')","metadata":{"execution":{"iopub.status.busy":"2023-09-10T22:36:48.632431Z","iopub.execute_input":"2023-09-10T22:36:48.632894Z","iopub.status.idle":"2023-09-10T22:36:48.646047Z","shell.execute_reply.started":"2023-09-10T22:36:48.632852Z","shell.execute_reply":"2023-09-10T22:36:48.644867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"TARGETS = [\n 'bowel_healthy',\n 'bowel_injury',\n 'extravasation_healthy',\n 'extravasation_injury',\n 'kidney_healthy',\n 'kidney_low',\n 'kidney_high',\n 'liver_healthy',\n 'liver_low',\n 'liver_high',\n 'spleen_healthy',\n 'spleen_low',\n 'spleen_high']","metadata":{"execution":{"iopub.status.busy":"2023-09-10T22:36:50.70586Z","iopub.execute_input":"2023-09-10T22:36:50.707167Z","iopub.status.idle":"2023-09-10T22:36:50.713378Z","shell.execute_reply.started":"2023-09-10T22:36:50.707114Z","shell.execute_reply":"2023-09-10T22:36:50.712156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'***** There are {len(sample_submission.columns[1:])} binary targets\\n', '\\n'.join(TARGETS))\ndisplay(sample_submission.columns[1:].str.split('_').str[0].value_counts().rename(\n    'counts').to_frame().reset_index(names='target_type'))","metadata":{"execution":{"iopub.status.busy":"2023-09-10T22:36:53.247954Z","iopub.execute_input":"2023-09-10T22:36:53.248573Z","iopub.status.idle":"2023-09-10T22:36:53.270717Z","shell.execute_reply.started":"2023-09-10T22:36:53.24853Z","shell.execute_reply":"2023-09-10T22:36:53.26924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('asserting `any_injury` is 1 with an injury of any type')\ninjury_cols = ['bowel_injury',\n    'extravasation_injury', 'kidney_low', 'kidney_high',\n    'liver_low', 'liver_high',\n    'spleen_low', 'spleen_high']\nassert all(train[injury_cols].any(axis=1) == train['any_injury'])\nprint('asserting targets for each type sum to 1')\nassert all(train.bowel_healthy + train.bowel_injury == 1)\nassert all(train.extravasation_healthy + train.extravasation_injury == 1)\nassert all(train.kidney_healthy + train.kidney_low + train.kidney_high == 1)\nassert all(train.liver_healthy + train.liver_low + train.liver_high == 1)\nassert all(train.spleen_healthy + train.spleen_low + train.spleen_high == 1)","metadata":{"execution":{"iopub.status.busy":"2023-09-10T22:36:56.686704Z","iopub.execute_input":"2023-09-10T22:36:56.687155Z","iopub.status.idle":"2023-09-10T22:36:56.707712Z","shell.execute_reply.started":"2023-09-10T22:36:56.687119Z","shell.execute_reply":"2023-09-10T22:36:56.706288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import sklearn.metrics","metadata":{"execution":{"iopub.status.busy":"2023-09-10T22:36:58.34965Z","iopub.execute_input":"2023-09-10T22:36:58.350525Z","iopub.status.idle":"2023-09-10T22:36:58.521497Z","shell.execute_reply.started":"2023-09-10T22:36:58.350481Z","shell.execute_reply":"2023-09-10T22:36:58.519887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_weighted_mean = sklearn.metrics.log_loss(\n    [[1, 0], [0, 1]], [[.6, .4], [.5, .5]], sample_weight=[10, 1])\na = sklearn.metrics.log_loss([[1, 0]], [[.6, .4]])\nb = sklearn.metrics.log_loss([[0, 1]], [[.5, .5]])\nprint(sample_weighted_mean)\nprint(a, b)\nprint((a * 10 + b) / 11, 'same as the sample weighted mean')","metadata":{"execution":{"iopub.status.busy":"2023-09-10T22:36:59.310853Z","iopub.execute_input":"2023-09-10T22:36:59.311299Z","iopub.status.idle":"2023-09-10T22:36:59.331614Z","shell.execute_reply.started":"2023-09-10T22:36:59.311265Z","shell.execute_reply":"2023-09-10T22:36:59.330128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('There are', train.shape[0], 'patients in the training set.')\nprint('We are told there will be about 1100 patients in the test set.')","metadata":{"execution":{"iopub.status.busy":"2023-09-10T22:36:59.84187Z","iopub.execute_input":"2023-09-10T22:36:59.842246Z","iopub.status.idle":"2023-09-10T22:36:59.848537Z","shell.execute_reply.started":"2023-09-10T22:36:59.842216Z","shell.execute_reply":"2023-09-10T22:36:59.847284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Train images ","metadata":{}},{"cell_type":"code","source":"train_image_path = Path('../input/rsna-2023-abdominal-trauma-detection/train_images')","metadata":{"execution":{"iopub.status.busy":"2023-09-10T22:37:10.48263Z","iopub.execute_input":"2023-09-10T22:37:10.485772Z","iopub.status.idle":"2023-09-10T22:37:10.491878Z","shell.execute_reply.started":"2023-09-10T22:37:10.485709Z","shell.execute_reply":"2023-09-10T22:37:10.490569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_image_series = pd.Series([x.stem for x in train_image_path.ls()]).astype(int)","metadata":{"execution":{"iopub.status.busy":"2023-09-10T22:37:14.356395Z","iopub.execute_input":"2023-09-10T22:37:14.356796Z","iopub.status.idle":"2023-09-10T22:37:14.470693Z","shell.execute_reply.started":"2023-09-10T22:37:14.356766Z","shell.execute_reply":"2023-09-10T22:37:14.469415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"assert set(train_image_series) == set(train.patient_id)\nprint('Each patient has a single corresponding folder with their dicoms')","metadata":{"execution":{"iopub.status.busy":"2023-09-10T22:37:15.205367Z","iopub.execute_input":"2023-09-10T22:37:15.205789Z","iopub.status.idle":"2023-09-10T22:37:15.213474Z","shell.execute_reply.started":"2023-09-10T22:37:15.205755Z","shell.execute_reply":"2023-09-10T22:37:15.212185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Train/test meta series info\n* `incomplete_organ` column only available in train data.","metadata":{"execution":{"iopub.status.busy":"2023-09-10T22:37:42.702869Z","iopub.execute_input":"2023-09-10T22:37:42.703382Z","iopub.status.idle":"2023-09-10T22:37:42.709115Z","shell.execute_reply.started":"2023-09-10T22:37:42.70334Z","shell.execute_reply":"2023-09-10T22:37:42.707615Z"}}},{"cell_type":"code","source":"display(train_series_meta.head())\ndisplay(test_series_meta.head())","metadata":{"execution":{"iopub.status.busy":"2023-09-10T22:38:50.939854Z","iopub.execute_input":"2023-09-10T22:38:50.940311Z","iopub.status.idle":"2023-09-10T22:38:50.961909Z","shell.execute_reply.started":"2023-09-10T22:38:50.940276Z","shell.execute_reply":"2023-09-10T22:38:50.960368Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('There are 1564 patients that have two scans')\ntrain_series_meta.patient_id.value_counts().value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-09-10T22:39:16.965255Z","iopub.execute_input":"2023-09-10T22:39:16.965689Z","iopub.status.idle":"2023-09-10T22:39:16.976729Z","shell.execute_reply.started":"2023-09-10T22:39:16.965656Z","shell.execute_reply":"2023-09-10T22:39:16.975624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train.set_index('patient_id').join(train_series_meta.groupby('patient_id').patient_id.count().rename('n_scans'))\ntrain","metadata":{"execution":{"iopub.status.busy":"2023-09-10T22:39:53.604774Z","iopub.execute_input":"2023-09-10T22:39:53.605227Z","iopub.status.idle":"2023-09-10T22:39:53.635742Z","shell.execute_reply.started":"2023-09-10T22:39:53.605191Z","shell.execute_reply":"2023-09-10T22:39:53.634799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.groupby('n_scans').mean()","metadata":{"execution":{"iopub.status.busy":"2023-09-10T22:40:41.730209Z","iopub.execute_input":"2023-09-10T22:40:41.730651Z","iopub.status.idle":"2023-09-10T22:40:41.761451Z","shell.execute_reply.started":"2023-09-10T22:40:41.730618Z","shell.execute_reply":"2023-09-10T22:40:41.760514Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.groupby('bowel_injury').n_scans.mean()","metadata":{"execution":{"iopub.status.busy":"2023-09-10T22:40:47.803445Z","iopub.execute_input":"2023-09-10T22:40:47.803882Z","iopub.status.idle":"2023-09-10T22:40:47.814444Z","shell.execute_reply.started":"2023-09-10T22:40:47.803846Z","shell.execute_reply":"2023-09-10T22:40:47.812904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"healthy_cols = [c for c in list(train) if 'healthy' in c]\nhealthy_cols","metadata":{"execution":{"iopub.status.busy":"2023-09-10T22:40:48.197082Z","iopub.execute_input":"2023-09-10T22:40:48.197908Z","iopub.status.idle":"2023-09-10T22:40:48.206158Z","shell.execute_reply.started":"2023-09-10T22:40:48.19787Z","shell.execute_reply":"2023-09-10T22:40:48.204849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[healthy_cols].sum(axis=1).value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-09-10T22:41:23.32929Z","iopub.execute_input":"2023-09-10T22:41:23.329721Z","iopub.status.idle":"2023-09-10T22:41:23.344017Z","shell.execute_reply.started":"2023-09-10T22:41:23.329686Z","shell.execute_reply":"2023-09-10T22:41:23.341628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Dicom tags","metadata":{}},{"cell_type":"code","source":"[(a, b )for a, b in zip(sorted(train_dicom_tags.columns), sorted(test_dicom_tags.columns)) if a != b]","metadata":{"execution":{"iopub.status.busy":"2023-09-10T22:49:07.437632Z","iopub.execute_input":"2023-09-10T22:49:07.438875Z","iopub.status.idle":"2023-09-10T22:49:07.447456Z","shell.execute_reply.started":"2023-09-10T22:49:07.438793Z","shell.execute_reply":"2023-09-10T22:49:07.446097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display(test_dicom_tags.head(3))\ndisplay(train_dicom_tags.head(3))\nassert sorted(test_dicom_tags.columns) == sorted(train_dicom_tags.columns)\ndisplay(train_dicom_tags.info())","metadata":{"execution":{"iopub.status.busy":"2023-09-10T22:49:31.93311Z","iopub.execute_input":"2023-09-10T22:49:31.933512Z","iopub.status.idle":"2023-09-10T22:49:41.727397Z","shell.execute_reply.started":"2023-09-10T22:49:31.93348Z","shell.execute_reply":"2023-09-10T22:49:41.726181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dicom_tags[['SOPInstanceUID', 'SeriesNumber', 'PatientID', 'InstanceNumber']]","metadata":{"execution":{"iopub.status.busy":"2023-09-10T22:59:17.470218Z","iopub.execute_input":"2023-09-10T22:59:17.47068Z","iopub.status.idle":"2023-09-10T22:59:17.544217Z","shell.execute_reply.started":"2023-09-10T22:59:17.470633Z","shell.execute_reply":"2023-09-10T22:59:17.542947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.set_option('display.max_columns', 100)\n","metadata":{"execution":{"iopub.status.busy":"2023-09-10T23:10:47.177028Z","iopub.execute_input":"2023-09-10T23:10:47.17746Z","iopub.status.idle":"2023-09-10T23:10:47.182525Z","shell.execute_reply.started":"2023-09-10T23:10:47.177426Z","shell.execute_reply":"2023-09-10T23:10:47.181399Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"p10004","metadata":{"execution":{"iopub.status.busy":"2023-09-10T23:10:49.719428Z","iopub.execute_input":"2023-09-10T23:10:49.719917Z","iopub.status.idle":"2023-09-10T23:10:49.778235Z","shell.execute_reply.started":"2023-09-10T23:10:49.719881Z","shell.execute_reply":"2023-09-10T23:10:49.776771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"p10004 = train_dicom_tags.query('PatientID == \"10004\"')\np10004['InstanceNumber'].astype(int).sort_values()","metadata":{"execution":{"iopub.status.busy":"2023-09-10T23:09:24.770886Z","iopub.execute_input":"2023-09-10T23:09:24.772095Z","iopub.status.idle":"2023-09-10T23:09:24.851737Z","shell.execute_reply.started":"2023-09-10T23:09:24.772044Z","shell.execute_reply":"2023-09-10T23:09:24.850383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"p10004['InstanceNumber'].duplicated()","metadata":{"execution":{"iopub.status.busy":"2023-09-10T23:09:50.128079Z","iopub.execute_input":"2023-09-10T23:09:50.128465Z","iopub.status.idle":"2023-09-10T23:09:50.140428Z","shell.execute_reply.started":"2023-09-10T23:09:50.128434Z","shell.execute_reply":"2023-09-10T23:09:50.13856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inst_nums = sorted([int(x.stem) for x in (train_image_path/\"10004\"/'51033').ls()])","metadata":{"execution":{"iopub.status.busy":"2023-09-10T23:07:41.269182Z","iopub.execute_input":"2023-09-10T23:07:41.269579Z","iopub.status.idle":"2023-09-10T23:07:41.462205Z","shell.execute_reply.started":"2023-09-10T23:07:41.269548Z","shell.execute_reply":"2023-09-10T23:07:41.461052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inst_nums[0], inst_nums[-1]","metadata":{"execution":{"iopub.status.busy":"2023-09-10T23:07:42.497602Z","iopub.execute_input":"2023-09-10T23:07:42.498038Z","iopub.status.idle":"2023-09-10T23:07:42.506306Z","shell.execute_reply.started":"2023-09-10T23:07:42.497996Z","shell.execute_reply":"2023-09-10T23:07:42.504969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_series_meta.series_id.sort_values()","metadata":{"execution":{"iopub.status.busy":"2023-09-10T22:57:48.400897Z","iopub.execute_input":"2023-09-10T22:57:48.401359Z","iopub.status.idle":"2023-09-10T22:57:48.412785Z","shell.execute_reply.started":"2023-09-10T22:57:48.401326Z","shell.execute_reply":"2023-09-10T22:57:48.411354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_series_meta.query('series_id == 6')","metadata":{"execution":{"iopub.status.busy":"2023-09-10T22:57:29.381169Z","iopub.execute_input":"2023-09-10T22:57:29.382254Z","iopub.status.idle":"2023-09-10T22:57:29.396556Z","shell.execute_reply.started":"2023-09-10T22:57:29.382216Z","shell.execute_reply":"2023-09-10T22:57:29.395416Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dicom_tags","metadata":{"execution":{"iopub.status.busy":"2023-09-10T22:52:20.413503Z","iopub.execute_input":"2023-09-10T22:52:20.413986Z","iopub.status.idle":"2023-09-10T22:52:22.054611Z","shell.execute_reply.started":"2023-09-10T22:52:20.413949Z","shell.execute_reply":"2023-09-10T22:52:22.053244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Image level labels","metadata":{}},{"cell_type":"code","source":"image_level_labels.injury_name.value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-09-10T22:43:43.284047Z","iopub.execute_input":"2023-09-10T22:43:43.284561Z","iopub.status.idle":"2023-09-10T22:43:43.299629Z","shell.execute_reply.started":"2023-09-10T22:43:43.284519Z","shell.execute_reply":"2023-09-10T22:43:43.298601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_level_labels.head(3)","metadata":{"execution":{"iopub.status.busy":"2023-09-10T22:43:29.013059Z","iopub.execute_input":"2023-09-10T22:43:29.013466Z","iopub.status.idle":"2023-09-10T22:43:29.025174Z","shell.execute_reply.started":"2023-09-10T22:43:29.013425Z","shell.execute_reply":"2023-09-10T22:43:29.024027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Segmentations","metadata":{}},{"cell_type":"code","source":"seg = RAW/'segmentations'","metadata":{"execution":{"iopub.status.busy":"2023-09-10T21:19:36.341988Z","iopub.execute_input":"2023-09-10T21:19:36.342374Z","iopub.status.idle":"2023-09-10T21:19:36.347256Z","shell.execute_reply.started":"2023-09-10T21:19:36.342343Z","shell.execute_reply":"2023-09-10T21:19:36.346316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"seg_paths = [int(x.stem) for x in seg.ls()]\n\nset(train_image_series)\n\nset(train_series_meta.patient_id) & set(seg_paths)\n\nset(seg_paths) & set(train_image_series)","metadata":{"execution":{"iopub.status.busy":"2023-09-10T22:27:39.455629Z","iopub.status.idle":"2023-09-10T22:27:39.456639Z","shell.execute_reply.started":"2023-09-10T22:27:39.456342Z","shell.execute_reply":"2023-09-10T22:27:39.456371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"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"}}