{"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":"from getpass import getpass\nimport os\n\nimport pydicom, numpy as np\nimport pandas as pd","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-03-14T08:49:35.438106Z","iopub.execute_input":"2022-03-14T08:49:35.438497Z","iopub.status.idle":"2022-03-14T08:49:35.718396Z","shell.execute_reply.started":"2022-03-14T08:49:35.438398Z","shell.execute_reply":"2022-03-14T08:49:35.717182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"execution":{"iopub.status.busy":"2022-03-14T08:56:14.408516Z","iopub.execute_input":"2022-03-14T08:56:14.409454Z","iopub.status.idle":"2022-03-14T08:57:03.569842Z","shell.execute_reply.started":"2022-03-14T08:56:14.409398Z","shell.execute_reply":"2022-03-14T08:57:03.568895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/rsna-pneumonia-detection-challenge/stage_2_train_labels.csv')\nprint(df.head())","metadata":{"execution":{"iopub.status.busy":"2022-03-14T08:50:14.350959Z","iopub.execute_input":"2022-03-14T08:50:14.353083Z","iopub.status.idle":"2022-03-14T08:50:14.408392Z","shell.execute_reply.started":"2022-03-14T08:50:14.353020Z","shell.execute_reply":"2022-03-14T08:50:14.407703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.hist()","metadata":{"execution":{"iopub.status.busy":"2022-03-14T08:50:23.297147Z","iopub.execute_input":"2022-03-14T08:50:23.297430Z","iopub.status.idle":"2022-03-14T08:50:23.975834Z","shell.execute_reply.started":"2022-03-14T08:50:23.297400Z","shell.execute_reply":"2022-03-14T08:50:23.974839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir = '../input/rsna-pneumonia-detection-challenge/stage_2_train_images/'\n\npatientId = df['patientId'][0]\ndcm_file = train_dir + '%s.dcm' % patientId\ndcm_data = pydicom.read_file(dcm_file)\n\nprint(dcm_data)","metadata":{"execution":{"iopub.status.busy":"2022-03-14T09:08:14.164781Z","iopub.execute_input":"2022-03-14T09:08:14.165622Z","iopub.status.idle":"2022-03-14T09:08:14.178919Z","shell.execute_reply.started":"2022-03-14T09:08:14.165584Z","shell.execute_reply":"2022-03-14T09:08:14.178004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"im = dcm_data.pixel_array\nprint(type(im))\nprint(im.dtype)\nprint(im.shape)","metadata":{"execution":{"iopub.status.busy":"2022-03-14T09:10:04.829691Z","iopub.execute_input":"2022-03-14T09:10:04.830103Z","iopub.status.idle":"2022-03-14T09:10:04.844872Z","shell.execute_reply.started":"2022-03-14T09:10:04.830058Z","shell.execute_reply":"2022-03-14T09:10:04.844171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nplt.imshow(im, cmap= plt.cm.gist_gray)","metadata":{"execution":{"iopub.status.busy":"2022-03-14T09:10:24.772577Z","iopub.execute_input":"2022-03-14T09:10:24.773064Z","iopub.status.idle":"2022-03-14T09:10:25.113048Z","shell.execute_reply.started":"2022-03-14T09:10:24.773029Z","shell.execute_reply":"2022-03-14T09:10:25.112100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def parse_data(df, parent_dir):\n    \n    # --- Define lambda to extract coords in list [y, x, height, width]\n    extract_box = lambda row: [row['y'], row['x'], row['height'], row['width']]\n    \n    parsed = {}\n    for n, row in df.iterrows():\n        # --- Initialize patient entry into parsed \n        pid = row['patientId']\n        if pid not in parsed:\n            parsed[pid] = {\n                'dicom': parent_dir + '%s.dcm' % pid,\n                'label': row['Target'],\n                'boxes': []\n            }\n    \n        # --- Add box if opacity is present\n        if parsed[pid]['label'] == 1:\n            parsed[pid]['boxes'].append(extract_box(row))\n    \n    return parsed","metadata":{"execution":{"iopub.status.busy":"2022-03-14T09:35:09.119873Z","iopub.execute_input":"2022-03-14T09:35:09.120255Z","iopub.status.idle":"2022-03-14T09:35:09.129972Z","shell.execute_reply.started":"2022-03-14T09:35:09.120218Z","shell.execute_reply":"2022-03-14T09:35:09.128967Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"parsed = parse_data(df, train_dir)","metadata":{"execution":{"iopub.status.busy":"2022-03-14T09:35:16.477045Z","iopub.execute_input":"2022-03-14T09:35:16.477534Z","iopub.status.idle":"2022-03-14T09:35:19.239849Z","shell.execute_reply.started":"2022-03-14T09:35:16.477490Z","shell.execute_reply":"2022-03-14T09:35:19.238727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame(parsed)","metadata":{"execution":{"iopub.status.busy":"2022-03-14T09:35:32.740297Z","iopub.execute_input":"2022-03-14T09:35:32.740635Z","iopub.status.idle":"2022-03-14T09:35:34.002406Z","shell.execute_reply.started":"2022-03-14T09:35:32.740602Z","shell.execute_reply":"2022-03-14T09:35:34.001336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(parsed['00436515-870c-4b36-a041-de91049b9ab4'])","metadata":{"execution":{"iopub.status.busy":"2022-03-14T09:39:36.235021Z","iopub.execute_input":"2022-03-14T09:39:36.235767Z","iopub.status.idle":"2022-03-14T09:39:36.241402Z","shell.execute_reply.started":"2022-03-14T09:39:36.235708Z","shell.execute_reply":"2022-03-14T09:39:36.240419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def draw(data):\n    \n    # --- Open DICOM file\n    d = pydicom.read_file(data['dicom'])\n    im = d.pixel_array\n    \n    # --- Convert from single-channel grayscale to 3-channel RGB\n    im = np.stack([im] * 3, axis=2)\n    \n    # --- Add boxes with random color if present\n    for box in data['boxes']:\n        rgb = np.floor(np.random.rand(3) * 256).astype('int')\n        im = overlay_box(im= im, box= box, rgb= rgb, stroke= 6)\n    \n    plt.imshow(im, cmap=plt.cm.gist_gray)","metadata":{"execution":{"iopub.status.busy":"2022-03-14T09:45:22.029684Z","iopub.execute_input":"2022-03-14T09:45:22.030124Z","iopub.status.idle":"2022-03-14T09:45:22.038049Z","shell.execute_reply.started":"2022-03-14T09:45:22.030083Z","shell.execute_reply":"2022-03-14T09:45:22.037221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def overlay_box(im, box, rgb, stroke=1):\n    \n    # --- Convert coordinates to integers\n    box = [int(b) for b in box]\n    \n    # --- Extract coordinates\n    y1, x1, height, width = box\n    y2 = y1 + height\n    x2 = x1 + width\n    \n    im[y1:y1 + stroke, x1:x2] = rgb\n    im[y2:y2 + stroke, x1:x2] = rgb\n    im[y1:y2, x1:x1 + stroke] = rgb\n    im[y1:y2, x2:x2 + stroke] = rgb\n    \n    return im","metadata":{"execution":{"iopub.status.busy":"2022-03-14T09:46:28.644486Z","iopub.execute_input":"2022-03-14T09:46:28.645344Z","iopub.status.idle":"2022-03-14T09:46:28.653083Z","shell.execute_reply.started":"2022-03-14T09:46:28.645301Z","shell.execute_reply":"2022-03-14T09:46:28.652109Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[ df['patientId'] == '00436515-870c-4b36-a041-de91049b9ab4' ]","metadata":{"execution":{"iopub.status.busy":"2022-03-14T09:48:03.783080Z","iopub.execute_input":"2022-03-14T09:48:03.783431Z","iopub.status.idle":"2022-03-14T09:48:03.805028Z","shell.execute_reply.started":"2022-03-14T09:48:03.783394Z","shell.execute_reply":"2022-03-14T09:48:03.803745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"draw(parsed['00436515-870c-4b36-a041-de91049b9ab4'])","metadata":{"execution":{"iopub.status.busy":"2022-03-14T09:48:10.446543Z","iopub.execute_input":"2022-03-14T09:48:10.447099Z","iopub.status.idle":"2022-03-14T09:48:10.938052Z","shell.execute_reply.started":"2022-03-14T09:48:10.447065Z","shell.execute_reply":"2022-03-14T09:48:10.937096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[ df['patientId'] == '0004cfab-14fd-4e49-80ba-63a80b6bddd6' ]","metadata":{"execution":{"iopub.status.busy":"2022-03-14T09:48:59.930238Z","iopub.execute_input":"2022-03-14T09:48:59.930538Z","iopub.status.idle":"2022-03-14T09:48:59.948971Z","shell.execute_reply.started":"2022-03-14T09:48:59.930505Z","shell.execute_reply":"2022-03-14T09:48:59.948076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"draw(parsed['0004cfab-14fd-4e49-80ba-63a80b6bddd6'])","metadata":{"execution":{"iopub.status.busy":"2022-03-14T09:49:20.826066Z","iopub.execute_input":"2022-03-14T09:49:20.826392Z","iopub.status.idle":"2022-03-14T09:49:21.160074Z","shell.execute_reply.started":"2022-03-14T09:49:20.826352Z","shell.execute_reply":"2022-03-14T09:49:21.158941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[ df['patientId'] == '00704310-78a8-4b38-8475-49f4573b2dbb' ]","metadata":{"execution":{"iopub.status.busy":"2022-03-14T09:50:35.653435Z","iopub.execute_input":"2022-03-14T09:50:35.653790Z","iopub.status.idle":"2022-03-14T09:50:35.675867Z","shell.execute_reply.started":"2022-03-14T09:50:35.653754Z","shell.execute_reply":"2022-03-14T09:50:35.674645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"draw(parsed['00704310-78a8-4b38-8475-49f4573b2dbb'])","metadata":{"execution":{"iopub.status.busy":"2022-03-14T09:51:06.680056Z","iopub.execute_input":"2022-03-14T09:51:06.680612Z","iopub.status.idle":"2022-03-14T09:51:07.028634Z","shell.execute_reply.started":"2022-03-14T09:51:06.680574Z","shell.execute_reply":"2022-03-14T09:51:07.027571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2022-03-10T11:53:03.736686Z","iopub.execute_input":"2022-03-10T11:53:03.737061Z","iopub.status.idle":"2022-03-10T11:53:10.20281Z","shell.execute_reply.started":"2022-03-10T11:53:03.73702Z","shell.execute_reply":"2022-03-10T11:53:10.201067Z"},"trusted":true},"execution_count":null,"outputs":[]}]}