{"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":"Based on the cropped images by Radek (https://www.kaggle.com/datasets/radek1/rsna-mammography-images-as-pngs/versions/2) and the code by Salman (https://www.kaggle.com/code/micheomaano/save-cropped-images)","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport os\nfrom pathlib import Path\nimport glob\nfrom PIL import Image\nfrom joblib import Parallel, delayed\nfrom tqdm import tqdm\nfrom fastai.vision.all import *\nimport pydicom\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def crop_out_image(img):\n    img = np.asarray(img)\n    av = np.mean(img, axis=0)\n    mi = np.min(img, axis=0)\n    ma = np.max(img, axis=0)\n    img = img[:, (((av - mi) > 2) + ((av - ma) > 2))]\n    return PILImage.create(img)","metadata":{"execution":{"iopub.status.busy":"2022-11-30T18:56:31.662292Z","iopub.execute_input":"2022-11-30T18:56:31.662853Z","iopub.status.idle":"2022-11-30T18:56:31.67472Z","shell.execute_reply.started":"2022-11-30T18:56:31.662805Z","shell.execute_reply":"2022-11-30T18:56:31.673037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"../input/rsna-breast-cancer-detection/train.csv\")\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-30T19:09:52.360917Z","iopub.execute_input":"2022-11-30T19:09:52.361384Z","iopub.status.idle":"2022-11-30T19:09:52.469665Z","shell.execute_reply.started":"2022-11-30T19:09:52.361348Z","shell.execute_reply":"2022-11-30T19:09:52.4677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.makedirs(\"train_images\", exist_ok=True)\nfor i, row in train.iterrows():\n    os.makedirs(\"train_images/\" + str(row['patient_id']), exist_ok=True)","metadata":{"execution":{"iopub.status.busy":"2022-11-30T19:09:53.082651Z","iopub.execute_input":"2022-11-30T19:09:53.083406Z","iopub.status.idle":"2022-11-30T19:09:53.108626Z","shell.execute_reply.started":"2022-11-30T19:09:53.08335Z","shell.execute_reply":"2022-11-30T19:09:53.106955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def read_crop_save(patient_id, image_id):\n    path = os.path.join('/kaggle/input/rsna-mammography-images-as-pngs/images_as_pngs/train_images_processed', f\"{patient_id}/{image_id}.png\")\n    img = PILImage.create(path)\n    img = crop_out_image(img)\n    img.resize((128, 256)).save(f\"train_images/{patient_id}/{image_id}.png\")","metadata":{"execution":{"iopub.status.busy":"2022-11-30T19:09:53.535395Z","iopub.execute_input":"2022-11-30T19:09:53.536275Z","iopub.status.idle":"2022-11-30T19:09:53.544172Z","shell.execute_reply.started":"2022-11-30T19:09:53.536229Z","shell.execute_reply":"2022-11-30T19:09:53.542418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"_ = Parallel(n_jobs=4)(\n    delayed(read_crop_save)(str(row['patient_id']), str(row['image_id'])) for i, row in tqdm(train.iterrows(), total=train.shape[0])\n)","metadata":{"execution":{"iopub.status.busy":"2022-11-30T19:09:53.930282Z","iopub.execute_input":"2022-11-30T19:09:53.931295Z","iopub.status.idle":"2022-11-30T19:09:59.741713Z","shell.execute_reply.started":"2022-11-30T19:09:53.93124Z","shell.execute_reply":"2022-11-30T19:09:59.740416Z"},"trusted":true},"execution_count":null,"outputs":[]}]}