{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":10338,"databundleVersionId":862042,"sourceType":"competition"}],"dockerImageVersionId":30746,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"from pathlib import Path\nimport pydicom\nimport numpy as np\nimport pandas as pd\nimport cv2\nimport matplotlib.pyplot as plt\nfrom tqdm.notebook import tqdm","metadata":{"execution":{"iopub.status.busy":"2024-07-17T00:36:21.347441Z","iopub.execute_input":"2024-07-17T00:36:21.347970Z","iopub.status.idle":"2024-07-17T00:36:21.355631Z","shell.execute_reply.started":"2024-07-17T00:36:21.347929Z","shell.execute_reply":"2024-07-17T00:36:21.354145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = pd.read_csv('/kaggle/input/rsna-pneumonia-detection-challenge/stage_2_train_labels.csv')\nlabels.head(6)","metadata":{"execution":{"iopub.status.busy":"2024-07-17T00:36:21.679426Z","iopub.execute_input":"2024-07-17T00:36:21.681372Z","iopub.status.idle":"2024-07-17T00:36:21.748278Z","shell.execute_reply.started":"2024-07-17T00:36:21.681320Z","shell.execute_reply":"2024-07-17T00:36:21.746561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = labels.drop_duplicates(subset=['patientId'])\nlabels.head(6)","metadata":{"execution":{"iopub.status.busy":"2024-07-17T00:36:23.011378Z","iopub.execute_input":"2024-07-17T00:36:23.012049Z","iopub.status.idle":"2024-07-17T00:36:23.042797Z","shell.execute_reply.started":"2024-07-17T00:36:23.012007Z","shell.execute_reply":"2024-07-17T00:36:23.041057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ROOT_PATH = Path('/kaggle/input/rsna-pneumonia-detection-challenge/stage_2_train_images')\nSAVE_PATH = Path('/kaggle/working/Processed')","metadata":{"execution":{"iopub.status.busy":"2024-07-17T00:36:06.168254Z","iopub.execute_input":"2024-07-17T00:36:06.168659Z","iopub.status.idle":"2024-07-17T00:36:06.175128Z","shell.execute_reply.started":"2024-07-17T00:36:06.168630Z","shell.execute_reply":"2024-07-17T00:36:06.173573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axis = plt.subplots(3, 3, figsize=(9, 9))\ncount = 0\nfor row in range(3):\n    for column in range(3):\n        patient_id = labels['patientId'].iloc[count]\n        label = labels['Target'].iloc[count]\n        \n        dcm_path = ROOT_PATH/ patient_id\n        dcm_path = dcm_path.with_suffix('.dcm')\n        img_array = pydicom.read_file(dcm_path).pixel_array\n        \n        axis[row, column].imshow(img_array, cmap='gray')\n        axis[row, column].set_title(label)\n        \n        fig.tight_layout()\n        count += 1\n        ","metadata":{"execution":{"iopub.status.busy":"2024-07-17T00:37:12.718083Z","iopub.execute_input":"2024-07-17T00:37:12.718575Z","iopub.status.idle":"2024-07-17T00:37:18.175748Z","shell.execute_reply.started":"2024-07-17T00:37:12.718536Z","shell.execute_reply":"2024-07-17T00:37:18.174412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sums = 0\nsums_squared = 0\n\nfor index in tqdm(range(len(labels))):\n    patient_id = labels['patientId'].iloc[index]\n    label = labels['Target'].iloc[index]\n    \n    dcm_path = ROOT_PATH/ patient_id\n    dcm_path = dcm_path.with_suffix('.dcm')\n    \n    img_array = pydicom.read_file(dcm_path).pixel_array\n    img_array = img_array / 255\n    img_array = cv2.resize(img_array, (224, 224)).astype(np.float32)\n    \n    train_or_val = 'train' if index < 24000 else 'val'\n    if train_or_val == 'train':\n        normalizer = 224*224\n        sums += np.sum(img_array) / normalizer\n        sums_squared += np.sum(img_array**2) / normalizer\n    \n    current_save_path = SAVE_PATH/train_or_val/str(label)\n    current_save_path.mkdir(parents=True, exist_ok=True)\n    np.save(current_save_path/patient_id, img_array.astype(np.float16))\n","metadata":{"execution":{"iopub.status.busy":"2024-07-17T00:51:49.528952Z","iopub.execute_input":"2024-07-17T00:51:49.529820Z","iopub.status.idle":"2024-07-17T00:51:49.579730Z","shell.execute_reply.started":"2024-07-17T00:51:49.529778Z","shell.execute_reply":"2024-07-17T00:51:49.578287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mean = sums / 24000\nstd = np.sqrt(sums_squared/24000 - mean**2)\nprint('Mean: ', mean)\nprint('Std: ', std)","metadata":{"execution":{"iopub.status.busy":"2024-07-15T08:38:18.253122Z","iopub.execute_input":"2024-07-15T08:38:18.253661Z","iopub.status.idle":"2024-07-15T08:38:18.264686Z","shell.execute_reply.started":"2024-07-15T08:38:18.253617Z","shell.execute_reply":"2024-07-15T08:38:18.262720Z"},"trusted":true},"execution_count":null,"outputs":[]}]}