{"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":"markdown","source":"# Downloading Libraries","metadata":{}},{"cell_type":"code","source":"!pip install python-gdcm\n!pip install -U pylibjpeg[all]","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-12-16T09:43:04.28915Z","iopub.execute_input":"2022-12-16T09:43:04.289706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Importing Libraries","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nimport pydicom\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport pathlib\nimport cv2\nfrom tqdm import tqdm\n\n%matplotlib inline","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-12-15T14:39:04.728228Z","iopub.execute_input":"2022-12-15T14:39:04.728697Z","iopub.status.idle":"2022-12-15T14:39:04.754828Z","shell.execute_reply.started":"2022-12-15T14:39:04.728654Z","shell.execute_reply":"2022-12-15T14:39:04.753463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"All the dicom images are cropped and converted into png images and can be downloaded from https://www.kaggle.com/datasets/anitho2910/rsna-mammography-breast-cancer-detection-png\n\n__NOTE:__ \nHaving some issues while creating dataset from kaggle notebook output, therefore not able to download it but can be used in the notebooks (getting some json unexpected error, if anyone can help me to resolve it feel free to ping) otherwise you can run the last section of the code (converting all dicom to pngs)","metadata":{}},{"cell_type":"markdown","source":"# Helper Functions","metadata":{}},{"cell_type":"code","source":"# Helper functions for loading images and its dicom data\ndef load_image(path, voi_lut = False):\n    data = pydicom.dcmread(path)\n    img = data.pixel_array\n    if voi_lut:\n        img = apply_voi_lut(img, data)\n    img = (img - np.min(img))/ (np.max(img) - np.min(img))\n    if data.PhotometricInterpretation == \"MONOCHROME1\":\n        img = 1 - img\n    return data, img\n\ndef fetch_L_roi(img):\n    for col_idx in range(img.shape[1]-1, 0, -1):\n        col = img[:, col_idx]\n        if np.any(col):\n            break\n    return img[:, :col_idx]\n\ndef fetch_R_roi(img):\n    for col_idx in range(img.shape[1]):\n        col = img[:, col_idx]\n        if np.any(col):\n            break\n    return img[:, col_idx:]\n\ndef resize_image(img, output_height = 1024):\n    h, w = img.shape[:2]\n    output_width = int(output_height/h*w)\n    resized_img = cv2.resize(img, (output_width, output_height), interpolation = cv2.INTER_AREA)\n    return resized_img","metadata":{"execution":{"iopub.status.busy":"2022-12-15T14:39:04.756596Z","iopub.execute_input":"2022-12-15T14:39:04.75712Z","iopub.status.idle":"2022-12-15T14:39:04.771848Z","shell.execute_reply.started":"2022-12-15T14:39:04.757058Z","shell.execute_reply":"2022-12-15T14:39:04.770548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Exploratory Data Analysis (Tabular)","metadata":{}},{"cell_type":"code","source":"base_path = pathlib.Path('/kaggle/input/rsna-breast-cancer-detection')\ntrain_images_path = base_path/'train_images'\ntest_images_path = base_path/'test_images'\ndata = pd.read_csv(base_path/'train.csv')\ndata['path'] = data.apply(lambda x: train_images_path/str(x['patient_id'])/(str(x['image_id'])+'.dcm'), axis = 1)\ndata.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-15T14:39:04.775128Z","iopub.execute_input":"2022-12-15T14:39:04.776319Z","iopub.status.idle":"2022-12-15T14:39:06.753878Z","shell.execute_reply.started":"2022-12-15T14:39:04.776267Z","shell.execute_reply":"2022-12-15T14:39:06.752654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.info()","metadata":{"execution":{"iopub.status.busy":"2022-12-15T14:39:06.755211Z","iopub.execute_input":"2022-12-15T14:39:06.755629Z","iopub.status.idle":"2022-12-15T14:39:06.787954Z","shell.execute_reply.started":"2022-12-15T14:39:06.755593Z","shell.execute_reply":"2022-12-15T14:39:06.786171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"__OBSERVATION:__ \n\n- Age, Birads and density columns consists of null values\n- Birads and density column is only for train dataset so it may not be beneficial in final prediction but age has to be taken care of","metadata":{}},{"cell_type":"code","source":"no_of_patient = len(data['patient_id'].unique())\nprint(f\"Dataset consists of: {no_of_patient} patients and it consists of {len(data)} scans\")","metadata":{"execution":{"iopub.status.busy":"2022-12-15T14:39:06.789743Z","iopub.execute_input":"2022-12-15T14:39:06.790517Z","iopub.status.idle":"2022-12-15T14:39:06.80087Z","shell.execute_reply.started":"2022-12-15T14:39:06.790458Z","shell.execute_reply":"2022-12-15T14:39:06.799376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Analysis on no of scans and laterality","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(10, 5))\nax1 = sns.countplot(x=list(data['patient_id'].value_counts()))\nfor container in ax1.containers:\n    ax1.bar_label(container)\nplt.title('Scans per patient')\n# plt.ylim([0, 800])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-12-15T14:39:06.802389Z","iopub.execute_input":"2022-12-15T14:39:06.803155Z","iopub.status.idle":"2022-12-15T14:39:07.142451Z","shell.execute_reply.started":"2022-12-15T14:39:06.803113Z","shell.execute_reply":"2022-12-15T14:39:07.141159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"__OBSERVATION:__ \n- Number of scans varies from 4 to 14 for a patient.\n- Since there are odd number of scans in some cases implies that in some case, number of scans for left or right mammograms are not same\n- Now to check whether do scans view affect cancer detection i.e. is value of cancer different for same patient in same laterality?","metadata":{}},{"cell_type":"code","source":"patient_ids = []\nfor patient_id in tqdm(set(data['patient_id'])):\n    subset_data = data[data['patient_id'] == patient_id]\n    L_laterality = subset_data[subset_data['laterality'] == 'L']\n    R_laterality = subset_data[subset_data['laterality'] == 'R']\n    L_cancer = set(L_laterality['cancer'])\n    R_cancer = set(R_laterality['cancer'])\n    if len(L_cancer) > 1 or len(R_cancer) > 1:\n        patient_ids.append(patient_id)\n        \nprint(patient_ids)","metadata":{"execution":{"iopub.status.busy":"2022-12-15T14:39:07.14433Z","iopub.execute_input":"2022-12-15T14:39:07.145125Z","iopub.status.idle":"2022-12-15T14:39:26.216604Z","shell.execute_reply.started":"2022-12-15T14:39:07.145077Z","shell.execute_reply":"2022-12-15T14:39:26.214942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"__OBSERVATION:__\n- Given a patient id and a laterality, view does not affect cancer detection i.e. cancer label is depending only on laterality of patient instead of view of the scan","metadata":{"execution":{"iopub.status.busy":"2022-12-14T17:45:48.165444Z","iopub.execute_input":"2022-12-14T17:45:48.16594Z","iopub.status.idle":"2022-12-14T17:45:48.174615Z","shell.execute_reply.started":"2022-12-14T17:45:48.165907Z","shell.execute_reply":"2022-12-14T17:45:48.173035Z"}}},{"cell_type":"markdown","source":"## Checking Imbalance","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(10, 5))\nax1 = sns.countplot(x = list(data.groupby(['patient_id', 'laterality'], axis = 0)['cancer'].max()))\nfor container in ax1.containers:\n    ax1.bar_label(container)\nplt.title('Cancer Type Distribution')\nax1.set(xlabel = 'Cancer Present')\n# plt.ylim([0, 800])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-12-15T14:39:26.217761Z","iopub.execute_input":"2022-12-15T14:39:26.218131Z","iopub.status.idle":"2022-12-15T14:39:26.457587Z","shell.execute_reply.started":"2022-12-15T14:39:26.218098Z","shell.execute_reply":"2022-12-15T14:39:26.455996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"__Observation:__\n- Data is highly imbalanced (obviously)","metadata":{}},{"cell_type":"markdown","source":"## Analysis on age","metadata":{}},{"cell_type":"code","source":"grouped_data = data.groupby(['patient_id'], axis = 0)[['age','cancer']].max()","metadata":{"execution":{"iopub.status.busy":"2022-12-15T14:39:26.46367Z","iopub.execute_input":"2022-12-15T14:39:26.464116Z","iopub.status.idle":"2022-12-15T14:39:26.47914Z","shell.execute_reply.started":"2022-12-15T14:39:26.464079Z","shell.execute_reply":"2022-12-15T14:39:26.477728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8, 8))\nax1 = sns.histplot(data = grouped_data, x = 'age')\nplt.title('Agewise number of scan')\nax1.set(ylabel = 'Age')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-12-15T14:39:26.480491Z","iopub.execute_input":"2022-12-15T14:39:26.480992Z","iopub.status.idle":"2022-12-15T14:39:26.766863Z","shell.execute_reply.started":"2022-12-15T14:39:26.480925Z","shell.execute_reply":"2022-12-15T14:39:26.765976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (8,8))\nsns.violinplot(x = 'cancer', y = 'age', data = grouped_data);","metadata":{"execution":{"iopub.status.busy":"2022-12-15T14:39:26.76869Z","iopub.execute_input":"2022-12-15T14:39:26.769579Z","iopub.status.idle":"2022-12-15T14:39:27.062092Z","shell.execute_reply.started":"2022-12-15T14:39:26.76952Z","shell.execute_reply":"2022-12-15T14:39:27.060466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (8,8))\nsns.boxplot(x = 'cancer', y = 'age', data = grouped_data);","metadata":{"execution":{"iopub.status.busy":"2022-12-15T14:39:27.063574Z","iopub.execute_input":"2022-12-15T14:39:27.06392Z","iopub.status.idle":"2022-12-15T14:39:27.296706Z","shell.execute_reply.started":"2022-12-15T14:39:27.063889Z","shell.execute_reply":"2022-12-15T14:39:27.295851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"__NOTE:__ Data is grouped on patient id to get patient level data rather than scan or laterality level\n\n__OBSERVATION:__\n\n- Age feature is almost normally distributed and most of the patient lies in the age of 40-80\n- Patient having cancer have higher median age compared to patient without cancer\n- Age is also almost normally distributed based on the cancer type","metadata":{}},{"cell_type":"markdown","source":"## Analysis on View","metadata":{}},{"cell_type":"code","source":"data['view'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-12-15T14:39:27.29789Z","iopub.execute_input":"2022-12-15T14:39:27.298847Z","iopub.status.idle":"2022-12-15T14:39:27.31186Z","shell.execute_reply.started":"2022-12-15T14:39:27.298808Z","shell.execute_reply":"2022-12-15T14:39:27.310392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"grouped_data = data.groupby(['patient_id', 'laterality'], axis = 0)['view'].agg(['unique'])\ngrouped_data","metadata":{"execution":{"iopub.status.busy":"2022-12-15T14:39:27.314149Z","iopub.execute_input":"2022-12-15T14:39:27.314545Z","iopub.status.idle":"2022-12-15T14:39:28.750181Z","shell.execute_reply.started":"2022-12-15T14:39:27.314509Z","shell.execute_reply":"2022-12-15T14:39:28.748992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Checking if MLO and CC view is part of each patient and laterality\nindices_without = []\nfor idx, views in enumerate(grouped_data['unique']):\n    if not(set(['MLO', 'CC']).issubset(set(views))):\n        indices_without.append(idx)\nprint(f\"No of patients without both MLO and CC view: {len(indices_without)}\")","metadata":{"execution":{"iopub.status.busy":"2022-12-15T14:39:28.752101Z","iopub.execute_input":"2022-12-15T14:39:28.752593Z","iopub.status.idle":"2022-12-15T14:39:28.803004Z","shell.execute_reply.started":"2022-12-15T14:39:28.752548Z","shell.execute_reply":"2022-12-15T14:39:28.801551Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"__OBSERVATION:__\n\n- There are 6 unique views\n- MLO and CC view is present for each patient and each laterality (L or R)","metadata":{}},{"cell_type":"markdown","source":"# Exploratory Data Analysis (DICOM Images)","metadata":{"execution":{"iopub.status.busy":"2022-12-14T18:42:36.645955Z","iopub.execute_input":"2022-12-14T18:42:36.646481Z","iopub.status.idle":"2022-12-14T18:42:36.652638Z","shell.execute_reply.started":"2022-12-14T18:42:36.646445Z","shell.execute_reply":"2022-12-14T18:42:36.651366Z"}}},{"cell_type":"code","source":"idx = 0\nsample_dicom_data, sample_dicom_image = load_image(data['path'].iloc[idx])\nprint(f\"Dicom Image Shape: {sample_dicom_image.shape}\")\nplt.figure(figsize = (10,10))\nplt.imshow(sample_dicom_image, cmap = 'gray')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-12-15T14:39:28.804949Z","iopub.execute_input":"2022-12-15T14:39:28.805507Z","iopub.status.idle":"2022-12-15T14:39:33.254363Z","shell.execute_reply.started":"2022-12-15T14:39:28.805447Z","shell.execute_reply":"2022-12-15T14:39:33.252923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"__NOTE:__ Image is grayscale and is very big. Can region of interest be extracted to only fetch the breast region instead of using entire image?","metadata":{}},{"cell_type":"code","source":"for col_idx in range(sample_dicom_image.shape[1]):\n    col = sample_dicom_image[:, col_idx]\n    if np.all(col):\n        print(col_idx)\n        break","metadata":{"execution":{"iopub.status.busy":"2022-12-15T14:39:33.256158Z","iopub.execute_input":"2022-12-15T14:39:33.256795Z","iopub.status.idle":"2022-12-15T14:39:33.340742Z","shell.execute_reply.started":"2022-12-15T14:39:33.256753Z","shell.execute_reply":"2022-12-15T14:39:33.339223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (10,10))\nplt.imshow(sample_dicom_image[:,:1726], cmap = 'gray')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-12-15T14:39:33.34315Z","iopub.execute_input":"2022-12-15T14:39:33.343898Z","iopub.status.idle":"2022-12-15T14:39:34.40232Z","shell.execute_reply.started":"2022-12-15T14:39:33.343831Z","shell.execute_reply":"2022-12-15T14:39:34.400965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Checking patient level mammogram images in different views","metadata":{}},{"cell_type":"code","source":"idx = 87\npatient_sub = data[data['patient_id'] == data['patient_id'].iloc[idx]]\npatient_sub","metadata":{"execution":{"iopub.status.busy":"2022-12-15T14:39:34.404166Z","iopub.execute_input":"2022-12-15T14:39:34.405301Z","iopub.status.idle":"2022-12-15T14:39:34.429674Z","shell.execute_reply.started":"2022-12-15T14:39:34.405247Z","shell.execute_reply":"2022-12-15T14:39:34.428165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(nrows = 1, ncols = len(patient_sub), figsize = (15, len(patient_sub)*15), sharex = True)\ni = 0\nfor index, row in patient_sub.iterrows():\n    dicom_data, dicom_img = load_image(row['path'])\n    if row['laterality'] == 'L':\n        dicom_img = fetch_L_roi(dicom_img)\n    if row['laterality'] == 'R':\n        dicom_img = fetch_R_roi(dicom_img)\n    axes[i].imshow(dicom_img, cmap = 'gray')\n    axes[i].axis('off')\n    i+=1","metadata":{"execution":{"iopub.status.busy":"2022-12-15T14:39:34.433347Z","iopub.execute_input":"2022-12-15T14:39:34.43378Z","iopub.status.idle":"2022-12-15T14:39:45.437113Z","shell.execute_reply.started":"2022-12-15T14:39:34.433744Z","shell.execute_reply":"2022-12-15T14:39:45.43599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Comparing VOI LUT transform","metadata":{}},{"cell_type":"code","source":"# Without VOI LUT\nfig, axes = plt.subplots(nrows = 1, ncols = len(patient_sub), figsize = (15, len(patient_sub)*15), sharex = True)\ni = 0\nfor index, row in patient_sub.iterrows():\n    dicom_data, dicom_img = load_image(row['path'])\n    if row['laterality'] == 'L':\n        dicom_img = fetch_L_roi(dicom_img)\n    if row['laterality'] == 'R':\n        dicom_img = fetch_R_roi(dicom_img)\n    axes[i].imshow(dicom_img, cmap = 'gray')\n    axes[i].axis('off')\n    i+=1","metadata":{"execution":{"iopub.status.busy":"2022-12-15T14:39:45.43873Z","iopub.execute_input":"2022-12-15T14:39:45.439163Z","iopub.status.idle":"2022-12-15T14:39:56.536528Z","shell.execute_reply.started":"2022-12-15T14:39:45.439129Z","shell.execute_reply":"2022-12-15T14:39:56.535021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# With VOI LUT\nfig, axes = plt.subplots(nrows = 1, ncols = len(patient_sub), figsize = (15, len(patient_sub)*15), sharex = True)\ni = 0\nfor index, row in patient_sub.iterrows():\n    \n    if row['laterality'] == 'L':\n        dicom_img = fetch_L_roi(dicom_img)\n    if row['laterality'] == 'R':\n        dicom_img = fetch_R_roi(dicom_img)\n    axes[i].imshow(dicom_img, cmap = 'gray')\n    axes[i].axis('off')\n    i+=1","metadata":{"execution":{"iopub.status.busy":"2022-12-15T14:39:56.538571Z","iopub.execute_input":"2022-12-15T14:39:56.538966Z","iopub.status.idle":"2022-12-15T14:40:02.862091Z","shell.execute_reply.started":"2022-12-15T14:39:56.538911Z","shell.execute_reply":"2022-12-15T14:40:02.860733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Comparing Resizing images","metadata":{}},{"cell_type":"code","source":"# Without VOI LUT\nfig, axes = plt.subplots(nrows = 1, ncols = len(patient_sub), figsize = (15, len(patient_sub)*15), sharex = True)\ni = 0\nfor index, row in patient_sub.iterrows():\n    dicom_data, dicom_img = load_image(row['path'])\n    if row['laterality'] == 'L':\n        dicom_img = fetch_L_roi(dicom_img)\n    if row['laterality'] == 'R':\n        dicom_img = fetch_R_roi(dicom_img)\n    axes[i].imshow(dicom_img, cmap = 'gray')\n    axes[i].axis('off')\n    i+=1","metadata":{"execution":{"iopub.status.busy":"2022-12-15T14:40:02.863685Z","iopub.execute_input":"2022-12-15T14:40:02.86472Z","iopub.status.idle":"2022-12-15T14:40:13.782697Z","shell.execute_reply.started":"2022-12-15T14:40:02.864684Z","shell.execute_reply":"2022-12-15T14:40:13.781371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Without VOI LUT\nfig, axes = plt.subplots(nrows = 1, ncols = len(patient_sub), figsize = (15, len(patient_sub)*15), sharex = True)\ni = 0\nfor index, row in patient_sub.iterrows():\n    dicom_data, dicom_img = load_image(row['path'])\n    dicom_img = resize_image(dicom_img)\n    if row['laterality'] == 'L':\n        dicom_img = fetch_L_roi(dicom_img)\n    if row['laterality'] == 'R':\n        dicom_img = fetch_R_roi(dicom_img)\n    axes[i].imshow(dicom_img, cmap = 'gray')\n    axes[i].axis('off')\n    i+=1","metadata":{"execution":{"iopub.status.busy":"2022-12-15T14:40:13.784209Z","iopub.execute_input":"2022-12-15T14:40:13.784563Z","iopub.status.idle":"2022-12-15T14:40:19.689347Z","shell.execute_reply.started":"2022-12-15T14:40:13.784533Z","shell.execute_reply":"2022-12-15T14:40:19.68808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Converting all images to png","metadata":{}},{"cell_type":"code","source":"data.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-15T14:40:19.690964Z","iopub.execute_input":"2022-12-15T14:40:19.691701Z","iopub.status.idle":"2022-12-15T14:40:19.712675Z","shell.execute_reply.started":"2022-12-15T14:40:19.691666Z","shell.execute_reply":"2022-12-15T14:40:19.711165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f\"No of unique ids: {len(data['image_id'].unique())}\")\nprint(f\"Shape of Data: {data.shape}\")","metadata":{"execution":{"iopub.status.busy":"2022-12-15T14:40:19.714325Z","iopub.execute_input":"2022-12-15T14:40:19.71478Z","iopub.status.idle":"2022-12-15T14:40:19.72878Z","shell.execute_reply.started":"2022-12-15T14:40:19.714744Z","shell.execute_reply":"2022-12-15T14:40:19.727252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"save_path = '/kaggle/working/png_images'\nif not(os.path.exists(save_path)):\n    os.makedirs(save_path)  ","metadata":{"execution":{"iopub.status.busy":"2022-12-15T14:40:19.73443Z","iopub.execute_input":"2022-12-15T14:40:19.734825Z","iopub.status.idle":"2022-12-15T14:40:19.74189Z","shell.execute_reply.started":"2022-12-15T14:40:19.734792Z","shell.execute_reply":"2022-12-15T14:40:19.740424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from multiprocessing import Pool\n\n# # Since Sequential was slow, using multiprocessing\n\n# def convert_to_png(row_idx):\n#     row = data.iloc[row_idx]\n#     file_path = os.path.join(save_path, str(row.patient_id))\n#     os.makedirs(file_path, exist_ok=True)\n#     file_name = os.path.join(file_path, str(row.image_id) + '.png')\n#     _, dicom_img = load_image(row['path'])\n#     dicom_img = resize_image(dicom_img)\n#     if row['laterality'] == 'L':\n#         dicom_img = fetch_L_roi(dicom_img)\n#     if row['laterality'] == 'R':\n#         dicom_img = fetch_R_roi(dicom_img)\n#     plt.imsave(file_name, dicom_img, cmap = 'gray')\n    \n    \n# # Reference: https://stackoverflow.com/questions/41920124/multiprocessing-use-tqdm-to-display-a-progress-bar\n# # Reference: https://stackoverflow.com/questions/20190668/multiprocessing-a-for-loop\n\n# with Pool(os.cpu_count()) as pool:\n#     max_iterations = len(data)\n#     with tqdm(total = max_iterations, leave = False) as pbar:\n#         for _ in pool.imap(convert_to_png, list(range(max_iterations))):\n#             pbar.update()","metadata":{"execution":{"iopub.status.busy":"2022-12-15T14:40:23.850149Z","iopub.execute_input":"2022-12-15T14:40:23.850589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}