{"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":"gpu","dataSources":[{"sourceId":39272,"databundleVersionId":4629629,"sourceType":"competition"},{"sourceId":4696088,"sourceType":"datasetVersion","datasetId":2687741}],"dockerImageVersionId":30236,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"ls ../input/rsna-breast-cancer-detection/train_images | head -1 -n 4","metadata":{"execution":{"iopub.status.busy":"2024-05-22T09:29:19.700824Z","iopub.execute_input":"2024-05-22T09:29:19.701561Z","iopub.status.idle":"2024-05-22T09:29:22.486164Z","shell.execute_reply.started":"2024-05-22T09:29:19.701525Z","shell.execute_reply":"2024-05-22T09:29:22.484949Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls ../input/rsna-breast-cancer-detection/train_images/57175","metadata":{"execution":{"iopub.status.busy":"2024-05-22T09:29:22.48823Z","iopub.execute_input":"2024-05-22T09:29:22.488587Z","iopub.status.idle":"2024-05-22T09:29:23.48934Z","shell.execute_reply.started":"2024-05-22T09:29:22.488552Z","shell.execute_reply":"2024-05-22T09:29:23.488068Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\nfrom matplotlib import pyplot as plt\n\nsample_sub = pd.read_csv('../input/rsna-breast-cancer-detection/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2024-05-22T09:29:23.491024Z","iopub.execute_input":"2024-05-22T09:29:23.491368Z","iopub.status.idle":"2024-05-22T09:29:23.500661Z","shell.execute_reply.started":"2024-05-22T09:29:23.491312Z","shell.execute_reply":"2024-05-22T09:29:23.499709Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample_sub","metadata":{"execution":{"iopub.status.busy":"2024-05-22T09:29:23.503021Z","iopub.execute_input":"2024-05-22T09:29:23.50334Z","iopub.status.idle":"2024-05-22T09:29:23.513429Z","shell.execute_reply.started":"2024-05-22T09:29:23.503296Z","shell.execute_reply":"2024-05-22T09:29:23.512425Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!ls -l ../input/rsna-breast-cancer-detection/train_images | wc -l # wc -l outputs one a count of one row too many when used like this! hence we need to subtract one to get the true count","metadata":{"execution":{"iopub.status.busy":"2024-05-22T09:29:23.51472Z","iopub.execute_input":"2024-05-22T09:29:23.515011Z","iopub.status.idle":"2024-05-22T09:29:24.585243Z","shell.execute_reply.started":"2024-05-22T09:29:23.514984Z","shell.execute_reply":"2024-05-22T09:29:24.5841Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Source: https://www.kaggle.com/code/bobdegraaf/dicomsdl-voi-lut\ndef voi_lut(image, dicom):\n    # Additional Checks\n    if 'WindowWidth' not in dicom.getPixelDataInfo() or 'WindowWidth' not in dicom.getPixelDataInfo():\n        return image\n    \n    # Load only the variables we need\n    center = dicom['WindowCenter']\n    width = dicom['WindowWidth']\n    bits_stored = dicom['BitsStored']\n    voi_lut_function = dicom['VOILUTFunction']\n\n    # For sigmoid it's a list, otherwise a single value\n    if isinstance(center, list):\n        center = center[0]\n    if isinstance(width, list):\n        width = width[0]\n\n    # Set y_min, max & range\n    y_min = 0\n    y_max = float(2**bits_stored - 1)\n    y_range = y_max\n\n    # Function with default LINEAR (so for Nan, it will use linear)\n    if voi_lut_function == 'SIGMOID':\n        image = y_range / (1 + np.exp(-4 * (image - center) / width)) + y_min\n    else:\n        # Checks width for < 1 (in our case not necessary, always >= 750)\n        center -= 0.5\n        width -= 1\n\n        below = image <= (center - width / 2)\n        above = image > (center + width / 2)\n        between = np.logical_and(~below, ~above)\n\n        image[below] = y_min\n        image[above] = y_max\n        if between.any():\n            image[between] = (\n                ((image[between] - center) / width + 0.5) * y_range + y_min\n            )\n\n    return image","metadata":{"execution":{"iopub.status.busy":"2024-05-22T09:29:24.587303Z","iopub.execute_input":"2024-05-22T09:29:24.587776Z","iopub.status.idle":"2024-05-22T09:29:24.599828Z","shell.execute_reply.started":"2024-05-22T09:29:24.587729Z","shell.execute_reply":"2024-05-22T09:29:24.598801Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls ../input/rsna-breast-cancer-detection/test_images/10008","metadata":{"execution":{"iopub.status.busy":"2024-05-22T09:29:24.601212Z","iopub.execute_input":"2024-05-22T09:29:24.601872Z","iopub.status.idle":"2024-05-22T09:29:25.613451Z","shell.execute_reply.started":"2024-05-22T09:29:24.601834Z","shell.execute_reply":"2024-05-22T09:29:25.612299Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pydicom\nimport numpy as np\nfrom pydicom.pixel_data_handlers.util import apply_voi_lut\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport os\nfrom pathlib import Path\nimport glob","metadata":{"execution":{"iopub.status.busy":"2024-05-22T09:29:25.615069Z","iopub.execute_input":"2024-05-22T09:29:25.615442Z","iopub.status.idle":"2024-05-22T09:29:25.622135Z","shell.execute_reply.started":"2024-05-22T09:29:25.615406Z","shell.execute_reply":"2024-05-22T09:29:25.621118Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"example = '../input/rsna-breast-cancer-detection/train_images/10006/1459541791.dcm'\npydicom.dcmread(example)","metadata":{"execution":{"iopub.status.busy":"2024-05-22T09:29:25.623555Z","iopub.execute_input":"2024-05-22T09:29:25.623889Z","iopub.status.idle":"2024-05-22T09:29:25.642817Z","shell.execute_reply.started":"2024-05-22T09:29:25.623859Z","shell.execute_reply":"2024-05-22T09:29:25.641618Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# source: https://www.kaggle.com/code/allunia/rsna-csf-cervical-spine-fracture-eda/notebook\ndef rescale_img_to_hu(dcm_ds):\n    \"\"\"Rescales the image to Hounsfield unit.\"\"\"\n    data = dcm_ds.pixel_array\n    if dcm_ds.PhotometricInterpretation == \"MONOCHROME1\":\n        data = np.amax(data) - data\n    return data * dcm_ds.RescaleSlope + dcm_ds.RescaleIntercept\n","metadata":{"execution":{"iopub.status.busy":"2024-05-22T09:29:25.64819Z","iopub.execute_input":"2024-05-22T09:29:25.648576Z","iopub.status.idle":"2024-05-22T09:29:25.65407Z","shell.execute_reply.started":"2024-05-22T09:29:25.648546Z","shell.execute_reply":"2024-05-22T09:29:25.653095Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def show_images_for_patient(patient_id):\n    patient_dir = os.path.join('../input/rsna-breast-cancer-detection/train_images', str(patient_id))\n    num_images = len(glob.glob(f\"{patient_dir}/*\"))\n    print(f\"Number of images for patient: {num_images}\")\n    fig, axs = plt.subplots(2, 2, figsize=(24,15))\n    axs = axs.flatten()\n    for i, img_path in enumerate(list(Path(patient_dir).iterdir())):\n        ds = pydicom.dcmread(img_path)\n        axs[i].imshow(rescale_img_to_hu(ds), cmap=\"bone\")","metadata":{"execution":{"iopub.status.busy":"2024-05-22T09:29:25.655505Z","iopub.execute_input":"2024-05-22T09:29:25.655875Z","iopub.status.idle":"2024-05-22T09:29:25.668018Z","shell.execute_reply.started":"2024-05-22T09:29:25.655837Z","shell.execute_reply":"2024-05-22T09:29:25.667148Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"show_images_for_patient(10006)","metadata":{"execution":{"iopub.status.busy":"2024-05-22T09:29:25.669203Z","iopub.execute_input":"2024-05-22T09:29:25.66953Z","iopub.status.idle":"2024-05-22T09:29:43.177372Z","shell.execute_reply.started":"2024-05-22T09:29:25.669501Z","shell.execute_reply":"2024-05-22T09:29:43.176389Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_csv = pd.read_csv('../input/rsna-breast-cancer-detection/train.csv')\ntrain_csv.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-22T09:29:43.178738Z","iopub.execute_input":"2024-05-22T09:29:43.179058Z","iopub.status.idle":"2024-05-22T09:29:43.261934Z","shell.execute_reply.started":"2024-05-22T09:29:43.179027Z","shell.execute_reply":"2024-05-22T09:29:43.26092Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_csv = pd.read_csv('../input/rsna-breast-cancer-detection/test.csv')\ntest_csv","metadata":{"execution":{"iopub.status.busy":"2024-05-22T09:29:43.263233Z","iopub.execute_input":"2024-05-22T09:29:43.263564Z","iopub.status.idle":"2024-05-22T09:29:43.27962Z","shell.execute_reply.started":"2024-05-22T09:29:43.263535Z","shell.execute_reply":"2024-05-22T09:29:43.278578Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_csv.shape[0], train_csv.patient_id.nunique()","metadata":{"execution":{"iopub.status.busy":"2024-05-22T09:29:43.281032Z","iopub.execute_input":"2024-05-22T09:29:43.281529Z","iopub.status.idle":"2024-05-22T09:29:43.289569Z","shell.execute_reply.started":"2024-05-22T09:29:43.28149Z","shell.execute_reply":"2024-05-22T09:29:43.288545Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize=(20,6))\nplt.subplot(1,2,1)\nax1 = sns.countplot(data=train_csv, x='cancer')\nfor container in ax1.containers:\n    ax1.bar_label(container)\nplt.title('Distribution of targets');","metadata":{"execution":{"iopub.status.busy":"2024-05-22T09:29:43.290778Z","iopub.execute_input":"2024-05-22T09:29:43.291616Z","iopub.status.idle":"2024-05-22T09:29:43.503936Z","shell.execute_reply.started":"2024-05-22T09:29:43.291585Z","shell.execute_reply":"2024-05-22T09:29:43.502855Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from fastai.data.all import *\nfrom fastai.vision.all import *\n\npath = '../input/rsna-mammography-images-as-pngs/images_as_pngs/train_images_processed'\n\ntrain_csv = pd.read_csv('../input/rsna-breast-cancer-detection/train.csv')\nfn2label = {fn: cancer_or_not for fn, cancer_or_not in zip(train_csv['image_id'].astype('str'), train_csv['cancer'])}\n\ndef label_func(path):\n    return fn2label[path.stem]\n\ndblock = DataBlock(\n    blocks    = (ImageBlock, CategoryBlock),\n    get_items = get_image_files,\n    get_y = label_func,\n    splitter  = RandomSplitter()\n)\ndsets = dblock.datasets(path)\ndls = dblock.dataloaders(path)\n","metadata":{"execution":{"iopub.status.busy":"2024-05-22T09:29:43.505415Z","iopub.execute_input":"2024-05-22T09:29:43.5062Z","iopub.status.idle":"2024-05-22T09:30:04.935178Z","shell.execute_reply.started":"2024-05-22T09:29:43.506159Z","shell.execute_reply":"2024-05-22T09:30:04.934275Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"dls.show_batch()","metadata":{"execution":{"iopub.status.busy":"2024-05-22T09:30:04.936758Z","iopub.execute_input":"2024-05-22T09:30:04.937448Z","iopub.status.idle":"2024-05-22T09:30:05.845384Z","shell.execute_reply.started":"2024-05-22T09:30:04.937403Z","shell.execute_reply":"2024-05-22T09:30:05.844432Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"learn = vision_learner(dls, resnet18, metrics=error_rate, pretrained=False)\nlearn.fit_one_cycle(1, 1e-2)","metadata":{"execution":{"iopub.status.busy":"2024-05-22T09:30:05.846716Z","iopub.execute_input":"2024-05-22T09:30:05.847049Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ls ../input/rsna-breast-cancer-detection/test_images","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission = pd.DataFrame(data={'prediction_id': test_csv['prediction_id'], 'cancer': np.random.rand(test_csv.shape[0])}).drop_duplicates(subset='prediction_id')\nsubmission.head()","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}