{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2129cdd4",
   "metadata": {},
   "source": [
    "# Pulmonary nodule detection\n",
    "\n",
    "## Goal\n",
    "Run an end-to-end OrcaImage `detection-v1` smoke baseline inspired by TCIA LIDC-IDRI. The bundled cohort is synthetic; it contains no patient data."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2aa6b11c",
   "metadata": {},
   "source": [
    "## Submission contract\n",
    "`detections.json with case_id, label, score, box=[x1,y1,z1,x2,y2,z2]`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1300fe0f",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pathlib import Path\n",
    "import importlib.util,subprocess,sys\n",
    "missing=[p for p in ['numpy','nibabel','scipy'] if importlib.util.find_spec(p) is None]\n",
    "if missing: subprocess.check_call([sys.executable,'-m','pip','install','numpy','nibabel','scipy'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "53cf5ffd",
   "metadata": {},
   "source": [
    "## Bundled smoke cohort\n",
    "This cell restores the small dataset carried inside the notebook."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fce9a2cc",
   "metadata": {},
   "outputs": [],
   "source": [
    "import base64,io,zipfile\n",
    "payload='UEsDBBQAAAAIABemFl1KIw323wAAAFECAAAYAAAAaW1hZ2VzL25vZHVsZV8wMDEubmlpLmd6k+/mYAABlv9vbxzkbTIQYbiwiLviy4u7k4U9Hkmde8DTd4ij70HGtib26GSbrWq186c4NNb8iuKb/1mgrqv0xtFc6bbU6voM4fVLd8g579DX7flc+ap66+1Mu7p7f9h3X51e7vH4xOPgeKl0yel/F7654tzAwMjEwiGg4EApw0FkxrFNV1smzq/fx5a1zn4nsuyFboe46qWfZynu/2Ufz2x6fX4MsuYXrgrr9175X5o+r/72f/nS9GgzZLO3CAfUb1tr/3P71/zvT0/6UM29w5lRJVDBcOaRz6OERkYGAFBLAwQUAAAACAAXphZd3AJdx8kAAABNAgAAGAAAAGltYWdlcy9ub2R1bGVfMDAyLm5paS5nepPv5mAAAZb/b28c5G0yEGG40K/34OLicn8/hc5si2Zjiy5li6bFnqz8iwznRU3+b6fZyPTzy1Q5u+N6dV2pOW25sne37LXbIrVuqsV75bzg7job/drzZbY9x/tn/0/Y/uMTX6rM5x9Xwo/9lFv+9t3eYH5GJhYOAQWHBoZBy2j8wjTdLule1qIzxdM/z1Ksf/dz/c+1J12RVUxjenX/ztf8bz/zd357ooak2cG38c3X+R+f//sfWEysnqHOsGR5p/tW9mNCIyMDAFBLAwQUAAAACAAXphZdjzY4EuEAAABPAgAAGAAAAGltYWdlcy9ub2R1bGVfMDAzLm5paS5nepPv5mAAAZb/b28c5G0yEGG40K/34OLicn8/hc5si2ZjCy5li6bFnqz8TYrzopLvq6sGKMzfd+nw8ztHwjf2xe7PXSy5/Xu72d+tIteTj9TmXOR7vvL91bzlD39UfuZ/E8b9fsIxw17/2sw5n3e84f/sZ8vCIaDg0MDAyEQvxoN8ll1mSbeK+PJmWc99zvPz9bz/c+7PikFWpKG54N/a3f/W1v7zXme7kxNJ94TgBd9+nf/x+t//icVrT7oimd44ienV/V2v7996vv/Jrcmz6eihwcdoVG1kFwy9lp/QyMgAAFBLAQIUAxQAAAAIABemFl1KIw323wAAAFECAAAYAAAAAAAAAAAAAACkgQAAAABpbWFnZXMvbm9kdWxlXzAwMS5uaWkuZ3pQSwECFAMUAAAACAAXphZd3AJdx8kAAABNAgAAGAAAAAAAAAAAAAAApIEVAQAAaW1hZ2VzL25vZHVsZV8wMDIubmlpLmd6UEsBAhQDFAAAAAgAF6YWXY82OBLhAAAATwIAABgAAAAAAAAAAAAAAKSBFAIAAGltYWdlcy9ub2R1bGVfMDAzLm5paS5nelBLBQYAAAAAAwADANIAAAArAwAAAAA='\n",
    "Path('data').mkdir(exist_ok=True)\n",
    "with zipfile.ZipFile(io.BytesIO(base64.b64decode(payload))) as z:z.extractall('data')\n",
    "[str(p) for p in Path('data').rglob('*') if p.is_file()]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e1a137c0",
   "metadata": {},
   "source": [
    "## Baseline\n",
    "Replace this transparent heuristic with your training and inference pipeline, while keeping the output contract unchanged."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "97fdebf3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import json, zipfile\n",
    "import nibabel as nib\n",
    "import numpy as np\n",
    "detections=[]\n",
    "for path in sorted(Path('data/images').glob('*.nii.gz')):\n",
    "    points=np.argwhere(np.asarray(nib.load(path).dataobj)>2)\n",
    "    lo=points.min(0); hi=points.max(0)+1\n",
    "    detections.append({'case_id':path.name.removesuffix('.nii.gz'),'label':1,'score':.95,'box':[*(int(x) for x in lo),*(int(x) for x in hi)]})\n",
    "Path('detections.json').write_text(json.dumps({'detections':detections}))\n",
    "with zipfile.ZipFile('submission.zip','w',zipfile.ZIP_DEFLATED) as z:z.write('detections.json')\n",
    "detections"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7cbf4dba",
   "metadata": {},
   "source": [
    "## Checks\n",
    "Inspect the archive before upload. Hidden reference values are never included."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "235d5f35",
   "metadata": {},
   "outputs": [],
   "source": [
    "with zipfile.ZipFile('submission.zip') as z: print(z.namelist())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "970bb4e3",
   "metadata": {},
   "source": [
    "## Next steps\n",
    "Import the full TCIA LIDC-IDRI cohort only after reviewing its current license and citation requirements in `datasets/CATALOG.md`."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
