{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceType":"competition","sourceId":20561}],"dockerImageVersionId":31429,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nimport kagglehub\nimport matplotlib.pyplot as plt\n\n\n# ============================================================\n# 1. DOWNLOAD THE COMPETITION DATA\n# ============================================================\n\npath = kagglehub.competition_download('covid-segmentation')\n\nprint(\"Path to competition files:\", path)\n\n\n# ============================================================\n# 2. SHOW THE DOWNLOADED FILES\n# ============================================================\n\nprint(\"\\n================ FILES ================\")\n\nfor dirname, _, filenames in os.walk(path):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n\n# ============================================================\n# 3. LOAD THE DATASETS\n# ============================================================\n\nimages_medseg = np.load(\n    os.path.join(path, 'images_medseg.npy')\n)\n\nimages_radiopedia = np.load(\n    os.path.join(path, 'images_radiopedia.npy')\n)\n\nmasks_medseg = np.load(\n    os.path.join(path, 'masks_medseg.npy')\n)\n\nmasks_radiopedia = np.load(\n    os.path.join(path, 'masks_radiopedia.npy')\n)\n\ntest_images_medseg = np.load(\n    os.path.join(path, 'test_images_medseg.npy')\n)\n\n\n# ============================================================\n# 4. CHECK THE DATASET STRUCTURE\n# ============================================================\n\nprint(\"\\n================ DATASET SHAPES ================\")\n\nprint(\"images_medseg:      \", images_medseg.shape)\nprint(\"images_radiopedia:  \", images_radiopedia.shape)\n\nprint(\"masks_medseg:       \", masks_medseg.shape)\nprint(\"masks_radiopedia:   \", masks_radiopedia.shape)\n\nprint(\"test_images_medseg: \", test_images_medseg.shape)\n\n\n# ============================================================\n# 5. CHECK DATA TYPES\n# ============================================================\n\nprint(\"\\n================ DATA TYPES ================\")\n\nprint(\"images_medseg:      \", images_medseg.dtype)\nprint(\"images_radiopedia:  \", images_radiopedia.dtype)\n\nprint(\"masks_medseg:       \", masks_medseg.dtype)\nprint(\"masks_radiopedia:   \", masks_radiopedia.dtype)\n\nprint(\"test_images_medseg: \", test_images_medseg.dtype)\n\n\n# ============================================================\n# 6. CHECK IMAGE VALUES\n# ============================================================\n\nprint(\"\\n================ IMAGE VALUES ================\")\n\nprint(\"MedSeg image min:\", images_medseg.min())\nprint(\"MedSeg image max:\", images_medseg.max())\n\nprint(\"Radiopedia image min:\", images_radiopedia.min())\nprint(\"Radiopedia image max:\", images_radiopedia.max())\n\n\n# ============================================================\n# 7. CHECK MASK VALUES\n# ============================================================\n\nprint(\"\\n================ MASK VALUES ================\")\n\nprint(\"MedSeg mask values:\")\nprint(np.unique(masks_medseg))\n\nprint(\"\\nRadiopedia mask values:\")\nprint(np.unique(masks_radiopedia))\n\n\n# ============================================================\n# 8. VISUALIZE MULTIPLE MEDSEG SAMPLES\n# ============================================================\n\n# Choose three different samples\nindices = [0, 1, 2]\n\nfig, axes = plt.subplots(\n    len(indices),\n    3,\n    figsize=(15, 12)\n)\n\n\nfor row, idx in enumerate(indices):\n\n    # Convert to float32 so matplotlib can display them\n    image = images_medseg[idx].astype(np.float32)\n    mask = masks_medseg[idx].astype(np.float32)\n\n    # --------------------------------------------------------\n    # Original CT image\n    # --------------------------------------------------------\n\n    axes[row, 0].imshow(\n        image,\n        cmap='gray'\n    )\n\n    axes[row, 0].set_title(\n        f'CT Image - Sample {idx}'\n    )\n\n    axes[row, 0].axis('off')\n\n\n    # --------------------------------------------------------\n    # Ground truth mask\n    # --------------------------------------------------------\n\n    axes[row, 1].imshow(\n        mask,\n        cmap='gray'\n    )\n\n    axes[row, 1].set_title(\n        f'Ground Truth Mask - Sample {idx}'\n    )\n\n    axes[row, 1].axis('off')\n\n\n    # --------------------------------------------------------\n    # CT + mask\n    # --------------------------------------------------------\n\n    axes[row, 2].imshow(\n        image,\n        cmap='gray'\n    )\n\n    axes[row, 2].imshow(\n        mask,\n        alpha=0.5\n    )\n\n    axes[row, 2].set_title(\n        f'CT + Mask - Sample {idx}'\n    )\n\n    axes[row, 2].axis('off')\n\n\nplt.tight_layout()\nplt.show()\n\n\n# ============================================================\n# 9. VISUALIZE MULTIPLE RADIOPAEDIA SAMPLES\n# ============================================================\n\nindices = [0, 1, 2]\n\nfig, axes = plt.subplots(\n    len(indices),\n    3,\n    figsize=(15, 12)\n)\n\n\nfor row, idx in enumerate(indices):\n\n    image = images_radiopedia[idx].astype(np.float32)\n    mask = masks_radiopedia[idx].astype(np.float32)\n\n\n    # --------------------------------------------------------\n    # Original CT image\n    # --------------------------------------------------------\n\n    axes[row, 0].imshow(\n        image,\n        cmap='gray'\n    )\n\n    axes[row, 0].set_title(\n        f'Radiopaedia CT - Sample {idx}'\n    )\n\n    axes[row, 0].axis('off')\n\n\n    # --------------------------------------------------------\n    # Ground truth mask\n    # --------------------------------------------------------\n\n    axes[row, 1].imshow(\n        mask,\n        cmap='gray'\n    )\n\n    axes[row, 1].set_title(\n        f'Radiopaedia Mask - Sample {idx}'\n    )\n\n    axes[row, 1].axis('off')\n\n\n    # --------------------------------------------------------\n    # CT + mask\n    # --------------------------------------------------------\n\n    axes[row, 2].imshow(\n        image,\n        cmap='gray'\n    )\n\n    axes[row, 2].imshow(\n        mask,\n        alpha=0.5\n    )\n\n    axes[row, 2].set_title(\n        f'Radiopaedia CT + Mask - Sample {idx}'\n    )\n\n    axes[row, 2].axis('off')\n\n\nplt.tight_layout()\nplt.show()\n\n    # --------------------------------------------------------\n    # PLOT IMAGES HISTOGRAMS\n    # --------------------------------------------------------\n\ndef plot_hists(images1, images2=None):\n    plt.hist(images1.ravel(), bins=100, density=True, color='b', alpha=1 if images2 is None else 0.5)\n    if images2 is not None:\n        plt.hist(images2.ravel(), bins=100, density=True, alpha=0.5, color='orange')\n    plt.title(\"Histogram \")\n    plt.show();\n\n# images_radiopedia: blue  images_medseg: orange\nplot_hists(images_radiopedia, images_medseg)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2026-09-07T19:51:09.236420Z","iopub.execute_input":"2026-09-07T19:51:09.236778Z","iopub.status.idle":"2026-09-07T19:51:40.045903Z","shell.execute_reply.started":"2026-09-07T19:51:09.236751Z","shell.execute_reply":"2026-09-07T19:51:40.044968Z"}},"outputs":[],"execution_count":null}]}