{"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":"! pip install -U pylibjpeg pylibjpeg-openjpeg pylibjpeg-libjpeg pydicom python-gdcm\n! pip install --upgrade pydicom","metadata":{"execution":{"iopub.status.busy":"2023-10-22T20:08:53.568591Z","iopub.execute_input":"2023-10-22T20:08:53.569083Z","iopub.status.idle":"2023-10-22T20:09:23.552784Z","shell.execute_reply.started":"2023-10-22T20:08:53.568985Z","shell.execute_reply":"2023-10-22T20:09:23.551239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nimport torch\nfrom torchvision.io import read_image\nfrom sklearn.model_selection import train_test_split\nimport pydicom\nfrom pydicom.data import get_testdata_file\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom glob import glob","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-10-22T20:20:07.976864Z","iopub.execute_input":"2023-10-22T20:20:07.977342Z","iopub.status.idle":"2023-10-22T20:20:07.984318Z","shell.execute_reply.started":"2023-10-22T20:20:07.977303Z","shell.execute_reply":"2023-10-22T20:20:07.983221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = sorted(glob(\"/kaggle/input/rsna-breast-cancer-detection/train_images/*/*.dcm\"))","metadata":{"execution":{"iopub.status.busy":"2023-10-22T20:20:09.448802Z","iopub.execute_input":"2023-10-22T20:20:09.449246Z","iopub.status.idle":"2023-10-22T20:22:05.863194Z","shell.execute_reply.started":"2023-10-22T20:20:09.449195Z","shell.execute_reply":"2023-10-22T20:22:05.86216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv('/kaggle/input/rsna-breast-cancer-detection/train.csv')\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-10-22T20:09:26.41014Z","iopub.execute_input":"2023-10-22T20:09:26.410937Z","iopub.status.idle":"2023-10-22T20:09:26.54074Z","shell.execute_reply.started":"2023-10-22T20:09:26.41089Z","shell.execute_reply":"2023-10-22T20:09:26.539666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.read_csv('/kaggle/input/rsna-breast-cancer-detection/test.csv')\ntest_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-10-22T20:09:26.543239Z","iopub.execute_input":"2023-10-22T20:09:26.54356Z","iopub.status.idle":"2023-10-22T20:09:26.562936Z","shell.execute_reply.started":"2023-10-22T20:09:26.543532Z","shell.execute_reply":"2023-10-22T20:09:26.561965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.columns","metadata":{"execution":{"iopub.status.busy":"2023-10-22T20:36:32.549069Z","iopub.execute_input":"2023-10-22T20:36:32.549527Z","iopub.status.idle":"2023-10-22T20:36:32.557743Z","shell.execute_reply.started":"2023-10-22T20:36:32.549492Z","shell.execute_reply":"2023-10-22T20:36:32.556178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir('/kaggle/input/rsna-breast-cancer-detection/train_images')[:5]","metadata":{"execution":{"iopub.status.busy":"2023-10-22T20:10:02.284419Z","iopub.execute_input":"2023-10-22T20:10:02.284837Z","iopub.status.idle":"2023-10-22T20:10:02.297653Z","shell.execute_reply.started":"2023-10-22T20:10:02.284802Z","shell.execute_reply":"2023-10-22T20:10:02.296375Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"patient_id = '49211'\n\nos.listdir(f'/kaggle/input/rsna-breast-cancer-detection/train_images/{patient_id}')","metadata":{"execution":{"iopub.status.busy":"2023-10-22T20:10:10.61756Z","iopub.execute_input":"2023-10-22T20:10:10.61797Z","iopub.status.idle":"2023-10-22T20:10:10.633022Z","shell.execute_reply.started":"2023-10-22T20:10:10.617936Z","shell.execute_reply":"2023-10-22T20:10:10.632172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"idx = 5  # 0\n\nbase_img_dir = '/kaggle/input/rsna-breast-cancer-detection/train_images'\n\nimg_id = str(train_df['image_id'].iloc[idx])\nfull_img_id = img_id + '.dcm'\npat_id = str(train_df['patient_id'].iloc[idx])\n\nlabel = train_df['cancer'].iloc[idx]\n\nos.path.join(base_img_dir, pat_id, full_img_id)","metadata":{"execution":{"iopub.status.busy":"2023-10-22T20:10:19.212492Z","iopub.execute_input":"2023-10-22T20:10:19.212901Z","iopub.status.idle":"2023-10-22T20:10:19.223511Z","shell.execute_reply.started":"2023-10-22T20:10:19.212868Z","shell.execute_reply":"2023-10-22T20:10:19.222248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_path = os.path.join(base_img_dir, pat_id, full_img_id)","metadata":{"execution":{"iopub.status.busy":"2023-10-22T20:10:27.424892Z","iopub.execute_input":"2023-10-22T20:10:27.425654Z","iopub.status.idle":"2023-10-22T20:10:27.43062Z","shell.execute_reply.started":"2023-10-22T20:10:27.425606Z","shell.execute_reply":"2023-10-22T20:10:27.429454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dcm_img = pydicom.dcmread(img_path, force=True)\ndcm_img","metadata":{"execution":{"iopub.status.busy":"2023-10-22T20:10:28.46534Z","iopub.execute_input":"2023-10-22T20:10:28.465899Z","iopub.status.idle":"2023-10-22T20:10:28.477801Z","shell.execute_reply.started":"2023-10-22T20:10:28.465852Z","shell.execute_reply":"2023-10-22T20:10:28.476646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_array = dcm_img.pixel_array\n\nplt.imshow(img_array)\nif label == 0:\n    category = \"doesn't have cancer\"\nelif label == 1:\n    category = \"has cancer\"\n\nplt.title(f'Patient {pat_id} {category} in image: {img_id}');","metadata":{"execution":{"iopub.status.busy":"2023-10-22T20:10:29.485196Z","iopub.execute_input":"2023-10-22T20:10:29.48566Z","iopub.status.idle":"2023-10-22T20:10:30.312457Z","shell.execute_reply.started":"2023-10-22T20:10:29.485623Z","shell.execute_reply":"2023-10-22T20:10:30.311291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def random_9img_sample(\n    df: pd.DataFrame, \n    random_state: int = 101, \n    base_img_dir = '/kaggle/input/rsna-breast-cancer-detection/train_images'\n):\n    \"\"\"Function that shows 9 random images with labels and their img sizes\"\"\"\n    df_sample = df.sample(9, random_state=random_state).reset_index()\n    \n    fig, ax = plt.subplots(3, 3, figsize=(14, 14))\n    \n    for idx_num, row in df_sample.iterrows():\n        img_id = str(row['image_id'])\n        full_img_id = img_id + '.dcm'\n        pat_id = str(row['patient_id'])\n\n        label = row['cancer']\n\n        img_path = os.path.join(base_img_dir, pat_id, full_img_id)\n        q, r = divmod(idx_num, 3)\n        \n        img_array = pydicom.dcmread(img_path, force=True).pixel_array\n        \n        \n        ax[q][r].imshow(img_array)\n        ax[q][r].set_title(f'Image label is: {label}')","metadata":{"execution":{"iopub.status.busy":"2023-10-22T20:10:36.596068Z","iopub.execute_input":"2023-10-22T20:10:36.596555Z","iopub.status.idle":"2023-10-22T20:10:36.606096Z","shell.execute_reply.started":"2023-10-22T20:10:36.596515Z","shell.execute_reply":"2023-10-22T20:10:36.604991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random_9img_sample(train_df)","metadata":{"execution":{"iopub.status.busy":"2023-10-22T20:10:36.764327Z","iopub.execute_input":"2023-10-22T20:10:36.764733Z","iopub.status.idle":"2023-10-22T20:10:48.118442Z","shell.execute_reply.started":"2023-10-22T20:10:36.7647Z","shell.execute_reply":"2023-10-22T20:10:48.117278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n","metadata":{"execution":{"iopub.status.busy":"2023-10-22T20:36:00.112415Z","iopub.execute_input":"2023-10-22T20:36:00.112861Z","iopub.status.idle":"2023-10-22T20:36:00.12016Z","shell.execute_reply.started":"2023-10-22T20:36:00.112827Z","shell.execute_reply":"2023-10-22T20:36:00.119349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport os\nimport pydicom\nimport matplotlib.pyplot as plt\n\ndef fixed_label_images(\n    df: pd.DataFrame,\n    base_img_dir='/kaggle/input/rsna-breast-cancer-detection/train_images'\n):\n    \"\"\"Function that shows 9 images with specified labels and their img sizes\"\"\"\n    label_1_images = train_df[train_df['cancer'] == 1].sample(5).reset_index()\n    label_0_images = train_df[train_df['cancer'] == 0].sample(4).reset_index()\n    \n    fig, ax = plt.subplots(3, 3, figsize=(14, 14))\n    \n    for idx_num, row in pd.concat([label_1_images, label_0_images]).iterrows():\n        img_id = str(row['image_id'])\n        full_img_id = img_id + '.dcm'\n        pat_id = str(row['patient_id'])\n\n        label = row['cancer']\n\n        img_path = os.path.join(base_img_dir, pat_id, full_img_id)\n        q, r = divmod(idx_num, 3)\n        \n        img_array = pydicom.dcmread(img_path, force=True).pixel_array\n        \n        ax[q][r].imshow(img_array, cmap='gray')\n        ax[q][r].set_title(f'Image label is: {label}')\n        ax[q][r].axis('off')  # Turn off axis labels\n\n    plt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-10-22T20:35:30.192644Z","iopub.execute_input":"2023-10-22T20:35:30.193118Z","iopub.status.idle":"2023-10-22T20:35:30.205452Z","shell.execute_reply.started":"2023-10-22T20:35:30.193078Z","shell.execute_reply":"2023-10-22T20:35:30.204181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fixed_label_images(train_df)","metadata":{"execution":{"iopub.status.busy":"2023-10-22T20:35:30.365737Z","iopub.execute_input":"2023-10-22T20:35:30.366148Z","iopub.status.idle":"2023-10-22T20:35:42.447813Z","shell.execute_reply.started":"2023-10-22T20:35:30.366115Z","shell.execute_reply":"2023-10-22T20:35:42.446494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(data=train_df, x='cancer')","metadata":{"execution":{"iopub.status.busy":"2023-10-22T20:09:41.057268Z","iopub.execute_input":"2023-10-22T20:09:41.057781Z","iopub.status.idle":"2023-10-22T20:09:41.179805Z","shell.execute_reply.started":"2023-10-22T20:09:41.057733Z","shell.execute_reply":"2023-10-22T20:09:41.178143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['cancer'].value_counts(normalize=True)","metadata":{"execution":{"iopub.status.busy":"2023-10-22T20:09:41.181721Z","iopub.execute_input":"2023-10-22T20:09:41.18252Z","iopub.status.idle":"2023-10-22T20:09:41.198036Z","shell.execute_reply.started":"2023-10-22T20:09:41.182472Z","shell.execute_reply":"2023-10-22T20:09:41.196295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.info()","metadata":{"execution":{"iopub.status.busy":"2023-10-22T20:14:08.604148Z","iopub.execute_input":"2023-10-22T20:14:08.604875Z","iopub.status.idle":"2023-10-22T20:14:08.637935Z","shell.execute_reply.started":"2023-10-22T20:14:08.604795Z","shell.execute_reply":"2023-10-22T20:14:08.636876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp = train_df[\"cancer\"].value_counts()\ndf = pd.DataFrame({'labels': temp.index,\n                   'values': temp.values\n                  })\nplt.figure(figsize = (6,6))\nplt.title('Cancer distribution')\nsns.set_color_codes(\"pastel\")\nsns.barplot(x = 'labels', y=\"values\", data=df)\nlocs, labels = plt.xticks()\nplt.show()\n\n","metadata":{"execution":{"iopub.status.busy":"2023-10-22T20:13:34.635149Z","iopub.execute_input":"2023-10-22T20:13:34.635647Z","iopub.status.idle":"2023-10-22T20:13:34.764414Z","shell.execute_reply.started":"2023-10-22T20:13:34.635607Z","shell.execute_reply":"2023-10-22T20:13:34.763149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport matplotlib.pyplot as plt\n\n# Sort the ages in ascending order\nsorted_ages = np.sort(train_df[\"age\"].values)\n\n# Create a histogram of the sorted ages\nplt.style.use('seaborn-whitegrid')\nplt.figure(figsize=(17, 5))\nplt.hist(sorted_ages[:-2], bins=[i for i in range(100)])\n\n# Customize the plot\nplt.title(\"All Patients Age Histogram\", fontsize=18, pad=10)  # Corrected syntax\nplt.xlabel(\"Age\", labelpad=10)\nplt.xticks([i * 10 for i in range(11)])\nplt.ylabel(\"Count\", labelpad=10)\n\n# Display the plot\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-10-22T20:16:47.054067Z","iopub.execute_input":"2023-10-22T20:16:47.05449Z","iopub.status.idle":"2023-10-22T20:16:47.431326Z","shell.execute_reply.started":"2023-10-22T20:16:47.054458Z","shell.execute_reply":"2023-10-22T20:16:47.430079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[:10]","metadata":{"execution":{"iopub.status.busy":"2023-10-22T20:23:05.791151Z","iopub.execute_input":"2023-10-22T20:23:05.791618Z","iopub.status.idle":"2023-10-22T20:23:05.799404Z","shell.execute_reply.started":"2023-10-22T20:23:05.791577Z","shell.execute_reply":"2023-10-22T20:23:05.798114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pydicom\nimport matplotlib.pyplot as plt\n\n#displaying the image\nimg = pydicom.read_file(train[100]).pixel_array\nplt.imshow(img, cmap=plt.cm.bone)\nplt.grid(False)\n\n#displaying metadata\ndata = pydicom.dcmread(train[0])\nprint(data)","metadata":{"execution":{"iopub.status.busy":"2023-10-22T20:23:39.220788Z","iopub.execute_input":"2023-10-22T20:23:39.221292Z","iopub.status.idle":"2023-10-22T20:23:41.301026Z","shell.execute_reply.started":"2023-10-22T20:23:39.221239Z","shell.execute_reply":"2023-10-22T20:23:41.299844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# laterality - Whether the image is of the left or right breast\ntrain_df['laterality'].value_counts().plot(kind='bar',figsize = (10, 5));\nplt.title('laterality');","metadata":{"execution":{"iopub.status.busy":"2023-10-22T20:24:21.167058Z","iopub.execute_input":"2023-10-22T20:24:21.167539Z","iopub.status.idle":"2023-10-22T20:24:21.310678Z","shell.execute_reply.started":"2023-10-22T20:24:21.167501Z","shell.execute_reply":"2023-10-22T20:24:21.3093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# view - The orientation of the image. The default for a screening exam is to capture two views per breast.\ntrain_df['view'].value_counts().plot(kind='bar',figsize = (10, 5));\nplt.title('view');","metadata":{"execution":{"iopub.status.busy":"2023-10-22T20:25:41.055611Z","iopub.execute_input":"2023-10-22T20:25:41.056101Z","iopub.status.idle":"2023-10-22T20:25:41.540366Z","shell.execute_reply.started":"2023-10-22T20:25:41.056061Z","shell.execute_reply":"2023-10-22T20:25:41.53942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# density - A rating for how dense the breast tissue is, with A being the least dense and D being the most dense. -->\n# --> Extremely dense tissue can make diagnosis more difficult. Only provided for train.\ntrain_df['density'].value_counts().plot(kind='bar',figsize = (10, 5));\nplt.title('density');","metadata":{"execution":{"iopub.status.busy":"2023-10-22T20:26:38.469399Z","iopub.execute_input":"2023-10-22T20:26:38.470057Z","iopub.status.idle":"2023-10-22T20:26:38.663237Z","shell.execute_reply.started":"2023-10-22T20:26:38.470017Z","shell.execute_reply":"2023-10-22T20:26:38.661779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}