{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":39272,"databundleVersionId":4629629,"sourceType":"competition"}],"dockerImageVersionId":30301,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install pylibjpeg\n!pip install python-gdcm\n!pip install plotly==5.11.0","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2024-01-05T01:53:21.602996Z","iopub.execute_input":"2024-01-05T01:53:21.603508Z","iopub.status.idle":"2024-01-05T01:54:30.700435Z","shell.execute_reply.started":"2024-01-05T01:53:21.603423Z","shell.execute_reply":"2024-01-05T01:54:30.699073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nfrom collections import Counter\nfrom pathlib import Path\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport plotly.express as px\nfrom mpl_toolkits.axes_grid1 import ImageGrid\nimport pydicom\nimport pylibjpeg\nsns.set_style(\"darkgrid\")","metadata":{"execution":{"iopub.status.busy":"2024-01-05T01:54:56.556288Z","iopub.execute_input":"2024-01-05T01:54:56.556798Z","iopub.status.idle":"2024-01-05T01:54:58.451637Z","shell.execute_reply.started":"2024-01-05T01:54:56.556757Z","shell.execute_reply":"2024-01-05T01:54:58.450077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images = Path(\"/kaggle/input/rsna-breast-cancer-detection/train_images\")\ntest_images = Path(\"/kaggle/input/rsna-breast-cancer-detection/test_images\")\ntrain_df = pd.read_csv(\"/kaggle/input/rsna-breast-cancer-detection/train.csv\")\ntest_df = pd.read_csv(\"/kaggle/input/rsna-breast-cancer-detection/test.csv\")\nsample_submission_df = pd.read_csv(\"/kaggle/input/rsna-breast-cancer-detection/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-01-05T01:55:09.564637Z","iopub.execute_input":"2024-01-05T01:55:09.56501Z","iopub.status.idle":"2024-01-05T01:55:09.669468Z","shell.execute_reply.started":"2024-01-05T01:55:09.564973Z","shell.execute_reply":"2024-01-05T01:55:09.668283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df","metadata":{"execution":{"iopub.status.busy":"2024-01-05T01:55:16.792356Z","iopub.execute_input":"2024-01-05T01:55:16.792828Z","iopub.status.idle":"2024-01-05T01:55:16.835793Z","shell.execute_reply.started":"2024-01-05T01:55:16.79279Z","shell.execute_reply":"2024-01-05T01:55:16.835059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.hist","metadata":{"execution":{"iopub.status.busy":"2024-01-05T01:55:40.148021Z","iopub.execute_input":"2024-01-05T01:55:40.148386Z","iopub.status.idle":"2024-01-05T01:55:40.166072Z","shell.execute_reply.started":"2024-01-05T01:55:40.148354Z","shell.execute_reply":"2024-01-05T01:55:40.164913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df","metadata":{"execution":{"iopub.status.busy":"2024-01-05T01:55:50.49839Z","iopub.execute_input":"2024-01-05T01:55:50.499495Z","iopub.status.idle":"2024-01-05T01:55:50.512435Z","shell.execute_reply.started":"2024-01-05T01:55:50.499445Z","shell.execute_reply":"2024-01-05T01:55:50.511389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.info()","metadata":{"execution":{"iopub.status.busy":"2024-01-05T01:56:01.553486Z","iopub.execute_input":"2024-01-05T01:56:01.553869Z","iopub.status.idle":"2024-01-05T01:56:01.579597Z","shell.execute_reply.started":"2024-01-05T01:56:01.553838Z","shell.execute_reply":"2024-01-05T01:56:01.578206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission_df","metadata":{"execution":{"iopub.status.busy":"2024-01-05T01:56:15.517489Z","iopub.execute_input":"2024-01-05T01:56:15.517893Z","iopub.status.idle":"2024-01-05T01:56:15.529157Z","shell.execute_reply.started":"2024-01-05T01:56:15.517859Z","shell.execute_reply":"2024-01-05T01:56:15.527593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images_counter = train_df[\"patient_id\"].value_counts().sort_index()\n\nfig = px.histogram(images_counter, text_auto=True, title=\"Number of images per patient\")\nfig.update_layout(bargap=0.2)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-05T01:56:26.332752Z","iopub.execute_input":"2024-01-05T01:56:26.333129Z","iopub.status.idle":"2024-01-05T01:56:26.745363Z","shell.execute_reply.started":"2024-01-05T01:56:26.333096Z","shell.execute_reply":"2024-01-05T01:56:26.744174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"is_cancer_person = train_df.groupby(\"patient_id\")['cancer'].max().sort_index()","metadata":{"execution":{"iopub.status.busy":"2024-01-05T01:56:34.332287Z","iopub.execute_input":"2024-01-05T01:56:34.332687Z","iopub.status.idle":"2024-01-05T01:56:34.34519Z","shell.execute_reply.started":"2024-01-05T01:56:34.332645Z","shell.execute_reply":"2024-01-05T01:56:34.344388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images_counter.index.tolist() == is_cancer_person.index.tolist()","metadata":{"execution":{"iopub.status.busy":"2024-01-05T01:56:40.205066Z","iopub.execute_input":"2024-01-05T01:56:40.205459Z","iopub.status.idle":"2024-01-05T01:56:40.213334Z","shell.execute_reply.started":"2024-01-05T01:56:40.205428Z","shell.execute_reply":"2024-01-05T01:56:40.212207Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.corrcoef(images_counter.values, is_cancer_person.values)","metadata":{"execution":{"iopub.status.busy":"2024-01-05T01:56:43.653621Z","iopub.execute_input":"2024-01-05T01:56:43.653982Z","iopub.status.idle":"2024-01-05T01:56:43.664091Z","shell.execute_reply.started":"2024-01-05T01:56:43.653954Z","shell.execute_reply":"2024-01-05T01:56:43.663119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"person_age = train_df.groupby(\"patient_id\")['age'].max().sort_index().fillna(0).astype('int64')\n\nfig = px.histogram(person_age, title=\"Distribution of patients' age\")\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2024-01-05T01:56:49.255771Z","iopub.execute_input":"2024-01-05T01:56:49.257159Z","iopub.status.idle":"2024-01-05T01:56:49.321245Z","shell.execute_reply.started":"2024-01-05T01:56:49.257007Z","shell.execute_reply":"2024-01-05T01:56:49.320501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_correlation_heatmap(df):\n    df = pd.get_dummies(df, columns=['laterality', 'view', 'density'])\n    df['difficult_negative_case'] = df['difficult_negative_case'].astype(int)\n\n    corr = df.corr()\n    mask = np.triu(np.ones_like(corr, dtype=bool))\n    fig, ax = plt.subplots(figsize=(11, 9))\n    cmap = sns.diverging_palette(230, 20, as_cmap=True)\n    sns.heatmap(corr, mask=mask, cmap=cmap, vmax=.3, center=0, square=True, linewidths=.5, cbar_kws={\"shrink\": .5})\n","metadata":{"execution":{"iopub.status.busy":"2024-01-05T01:56:53.836122Z","iopub.execute_input":"2024-01-05T01:56:53.836556Z","iopub.status.idle":"2024-01-05T01:56:53.844379Z","shell.execute_reply.started":"2024-01-05T01:56:53.836503Z","shell.execute_reply":"2024-01-05T01:56:53.843615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_correlation_heatmap(train_df)","metadata":{"execution":{"iopub.status.busy":"2024-01-05T01:56:58.220644Z","iopub.execute_input":"2024-01-05T01:56:58.221078Z","iopub.status.idle":"2024-01-05T01:56:58.9938Z","shell.execute_reply.started":"2024-01-05T01:56:58.221041Z","shell.execute_reply":"2024-01-05T01:56:58.992059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_image = \"/kaggle/input/rsna-breast-cancer-detection/train_images/10006/1459541791.dcm\"\npydicom.dcmread(sample_image)","metadata":{"execution":{"iopub.status.busy":"2024-01-05T01:57:02.835794Z","iopub.execute_input":"2024-01-05T01:57:02.83618Z","iopub.status.idle":"2024-01-05T01:57:02.910223Z","shell.execute_reply.started":"2024-01-05T01:57:02.83615Z","shell.execute_reply":"2024-01-05T01:57:02.908835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df[\"patient_id\"]","metadata":{"execution":{"iopub.status.busy":"2024-01-05T01:57:06.701733Z","iopub.execute_input":"2024-01-05T01:57:06.702135Z","iopub.status.idle":"2024-01-05T01:57:06.710981Z","shell.execute_reply.started":"2024-01-05T01:57:06.702101Z","shell.execute_reply":"2024-01-05T01:57:06.709615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_images_of_patient(patient_id):\n    patient_df = test_df[test_df[\"patient_id\"] == patient_id] if patient_id in test_df[\"patient_id\"].values else train_df[train_df[\"patient_id\"] == patient_id] if patient_id in train_df[\"patient_id\"].values else None\n    if patient_df is None:\n        print(f\"No patient with id {patient_id}\")\n        return\n    \n    patient_folder = test_images / f\"{patient_id}\" if patient_id in test_df[\"patient_id\"].values else train_images / f\"{patient_id}\"\n    num_images = patient_df.shape[0]\n    n_rows, n_cols = (2, 2) if num_images <= 4 else (4, 4) if num_images <= 8 else (8, 8)\n    \n    fig, ax = plt.subplots(figsize=(24., 24.), nrows=n_rows, ncols=n_cols, gridspec_kw={'wspace': 0.1, 'hspace': 0.1})\n    fig.suptitle(f\"{patient_id} - {num_images} images\", fontsize=16)\n    \n    for idx, curr_image in enumerate(patient_folder.iterdir()):\n        curr_image = Path(curr_image)\n        image_data = patient_df[patient_df[\"image_id\"] == int(curr_image.stem)].squeeze()\n        ds = pydicom.dcmread(curr_image)\n        image_as_np = ds.pixel_array.astype(np.float32)\n        curr_ax = ax[idx // n_cols, idx % n_cols]\n        curr_ax.imshow(image_as_np)\n        curr_ax.set_title(\"image_id: {}\\n laterality: {}\\n view {}\\n machine_id {}\".format(image_data[\"image_id\"], image_data[\"laterality\"], image_data[\"view\"], image_data[\"machine_id\"]), fontdict={\"fontsize\": 9})\n","metadata":{"execution":{"iopub.status.busy":"2024-01-05T01:59:45.9412Z","iopub.execute_input":"2024-01-05T01:59:45.941562Z","iopub.status.idle":"2024-01-05T01:59:45.953556Z","shell.execute_reply.started":"2024-01-05T01:59:45.941507Z","shell.execute_reply":"2024-01-05T01:59:45.951775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_images_of_patient(32751)","metadata":{"execution":{"iopub.status.busy":"2024-01-05T01:57:21.178945Z","iopub.execute_input":"2024-01-05T01:57:21.179309Z","iopub.status.idle":"2024-01-05T01:57:26.663121Z","shell.execute_reply.started":"2024-01-05T01:57:21.179282Z","shell.execute_reply":"2024-01-05T01:57:26.661872Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_images_of_patient(2518)","metadata":{"execution":{"iopub.status.busy":"2024-01-05T02:00:31.589244Z","iopub.execute_input":"2024-01-05T02:00:31.589696Z","iopub.status.idle":"2024-01-05T02:00:46.536163Z","shell.execute_reply.started":"2024-01-05T02:00:31.589664Z","shell.execute_reply":"2024-01-05T02:00:46.53521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_images_of_patient(10179)","metadata":{"execution":{"iopub.status.busy":"2024-01-05T01:59:50.95632Z","iopub.execute_input":"2024-01-05T01:59:50.956713Z","iopub.status.idle":"2024-01-05T01:59:55.571997Z","shell.execute_reply.started":"2024-01-05T01:59:50.956682Z","shell.execute_reply":"2024-01-05T01:59:55.571216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}