{"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":"!pip3 install -q python-gdcm\nimport gdcm\n\nimport 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\n# import pylibjpeg\n# import libjpeg\n\nimport pydicom\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut","metadata":{"execution":{"iopub.status.busy":"2022-11-29T07:23:10.490753Z","iopub.execute_input":"2022-11-29T07:23:10.491402Z","iopub.status.idle":"2022-11-29T07:23:26.967635Z","shell.execute_reply.started":"2022-11-29T07:23:10.491278Z","shell.execute_reply":"2022-11-29T07:23:26.96623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def rescale_img_to_hu(dcm_ds):\n    \"\"\"Rescales the image to Hounsfield unit.\"\"\"\n    return dcm_ds.pixel_array# * dcm_ds.RescaleSlope + dcm_ds.RescaleIntercept\n\ndef read_dicom_image(image_path):\n    x = rescale_img_to_hu(pydicom.dcmread(image_path))\n    x = x - np.min(x)\n    x = x / (np.max(x) + 1e-4)\n    x = (x * 255).astype(np.uint8)\n    return x\n\ndef crop_out_image(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 img","metadata":{"execution":{"iopub.status.busy":"2022-11-29T07:23:26.969898Z","iopub.execute_input":"2022-11-29T07:23:26.971292Z","iopub.status.idle":"2022-11-29T07:23:26.983175Z","shell.execute_reply.started":"2022-11-29T07:23:26.97124Z","shell.execute_reply":"2022-11-29T07:23:26.981652Z"},"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-29T07:23:36.994883Z","iopub.execute_input":"2022-11-29T07:23:36.995326Z","iopub.status.idle":"2022-11-29T07:23:37.14874Z","shell.execute_reply.started":"2022-11-29T07:23:36.995292Z","shell.execute_reply":"2022-11-29T07:23:37.147691Z"},"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-29T07:23:50.858884Z","iopub.execute_input":"2022-11-29T07:23:50.859349Z","iopub.status.idle":"2022-11-29T07:23:55.486997Z","shell.execute_reply.started":"2022-11-29T07:23:50.859314Z","shell.execute_reply":"2022-11-29T07:23:55.485574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def read_crop_save(patient_id, image_id):\n    img = read_dicom_image(os.path.join('../input/rsna-breast-cancer-detection/train_images', f\"{patient_id}/{image_id}.dcm\"))\n    img = crop_out_image(img)\n    Image.fromarray(img).resize((1024, 1024)).save(f\"train_images/{patient_id}/{image_id}.png\")","metadata":{"execution":{"iopub.status.busy":"2022-11-29T07:24:42.600465Z","iopub.execute_input":"2022-11-29T07:24:42.600849Z","iopub.status.idle":"2022-11-29T07:24:42.608229Z","shell.execute_reply.started":"2022-11-29T07:24:42.600817Z","shell.execute_reply":"2022-11-29T07:24:42.606942Z"},"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-29T07:24:44.165347Z","iopub.execute_input":"2022-11-29T07:24:44.1658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}