{"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"},{"sourceId":4432552,"sourceType":"datasetVersion","datasetId":2595934}],"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\n\nimport matplotlib.pyplot as plt\nimport matplotlib.patches as patches\nfrom tqdm.notebook import tqdm","metadata":{"execution":{"iopub.status.busy":"2024-07-19T01:40:41.037158Z","iopub.execute_input":"2024-07-19T01:40:41.037556Z","iopub.status.idle":"2024-07-19T01:40:41.043950Z","shell.execute_reply.started":"2024-07-19T01:40:41.037524Z","shell.execute_reply":"2024-07-19T01:40:41.042649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = pd.read_csv('/kaggle/input/rsnacardiacdetectionlabels/rsna_heart_detection.csv')\nprint(labels.shape)\nlabels.head()","metadata":{"execution":{"iopub.status.busy":"2024-07-19T01:40:41.272977Z","iopub.execute_input":"2024-07-19T01:40:41.273381Z","iopub.status.idle":"2024-07-19T01:40:41.294997Z","shell.execute_reply.started":"2024-07-19T01:40:41.273349Z","shell.execute_reply":"2024-07-19T01:40:41.293844Z"},"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-Heart-Detection')","metadata":{"execution":{"iopub.status.busy":"2024-07-19T01:41:16.426013Z","iopub.execute_input":"2024-07-19T01:41:16.426399Z","iopub.status.idle":"2024-07-19T01:41:16.431691Z","shell.execute_reply.started":"2024-07-19T01:41:16.426370Z","shell.execute_reply":"2024-07-19T01:41:16.430386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axis = plt.subplots(2, 2, figsize=(9, 9))\n\ncount = 0\n\nfor row in range(2):\n    for column in range(2):\n        data = labels.iloc[count]\n        patient_id = data['name']\n        x = data['x0']\n        y = data['y0']\n        width = data['w']\n        height = data['h']\n        \n        dcm_path = ROOT_PATH/str(patient_id)\n        dcm_path = dcm_path.with_suffix('.dcm')\n        \n        image_array = pydicom.read_file(dcm_path).pixel_array\n        image_array = cv2.resize(image_array, (224, 224))\n        \n        rect = patches.Rectangle((x, y), width, height, edgecolor='r', facecolor='none', linewidth=1)\n        axis[row, column].add_patch(rect)\n        axis[row, column].imshow(image_array, cmap='bone')\n        count += 1\n    ","metadata":{"execution":{"iopub.status.busy":"2024-07-19T01:51:14.199303Z","iopub.execute_input":"2024-07-19T01:51:14.199732Z","iopub.status.idle":"2024-07-19T01:51:15.419918Z","shell.execute_reply.started":"2024-07-19T01:51:14.199697Z","shell.execute_reply":"2024-07-19T01:51:15.418754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sums = 0\nsums_squared = 0\ntrain_ids = []\nval_ids = []\n\nfor count in tqdm(range(len(labels))):\n    data = labels.iloc[count]\n    \n    patient_id = data['name']\n    \n    dcm_path = ROOT_PATH/str(patient_id)\n    dcm_path = dcm_path.with_suffix('.dcm')\n    \n    image = pydicom.read_file(dcm_path)\n    image_array = image.pixel_array.astype(np.float32)\n    image_array = image_array / 255.0\n    image_array = cv2.resize(image_array, (224, 224))\n    \n    train_or_val = 'train' if count < 400 else 'val'\n    \n    if train_or_val == 'train':\n        normalizer = 224*224\n        sums += np.sum(image_array) / normalizer\n        sums_squared += np.sum(image_array**2) / normalizer\n        \n    current_save_path = SAVE_PATH/train_or_val\n    current_save_path.mkdir(parents=True, exist_ok=True)\n    np.save(current_save_path/patient_id, image_array.astype(np.float16))\n    \n    if train_or_val == 'train':\n        train_ids.append(patient_id)\n    else:\n        val_ids.append(patient_id)\n    \n    ","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.save('/kaggle/working/Processed-Heart-Detection/train_subjects', train_ids)\nnp.save('/kaggle/working/Processed-Heart-Detection/val_subjects', val_ids)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mean = sums / len(train_ids)\nstd = np.sqrt(sums_squared / len(train_ids) - mean**2)\nprint(mean, std)","metadata":{"execution":{"iopub.status.busy":"2024-07-17T13:06:56.706254Z","iopub.execute_input":"2024-07-17T13:06:56.706858Z","iopub.status.idle":"2024-07-17T13:06:56.715489Z","shell.execute_reply.started":"2024-07-17T13:06:56.706816Z","shell.execute_reply":"2024-07-17T13:06:56.713988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}