{"cells":[{"cell_type":"markdown","metadata":{},"source":"# CoAtNet residual family standalone submission\n"},{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":"from pathlib import Path as _KePath\n\ndef _coat_substitute():\n    import hashlib as _h, os as _o, subprocess as _sp, sys as _sy\n    from pathlib import Path as _P\n    import pandas as _pd\n    import numpy as _np\n\n    MAN_SHA = '98511a8fdeb9da0e6e70c78d013ff636e1476f31c80b5dc134d294b18c3f284e'\n    WHL_SHA = '236c8df54a90f4d02076e6f9c1cc763d794542e886c576a6fee46ec8ff75a7a9'\n\n    def sha(p):\n        d = _h.sha256()\n        with _P(p).open('rb') as f:\n            for b in iter(lambda: f.read(8 << 20), b''):\n                d.update(b)\n        return d.hexdigest()\n\n    def find(name, want):\n        root = _P('/kaggle/input')\n        if not root.is_dir():\n            return None\n        for base in sorted(root.iterdir()):\n            if base.name in ('competitions', 'train_series', 'test_series'):\n                continue\n            for p in sorted(base.rglob(name)):\n                if p.is_file() and sha(p) == want:\n                    return p\n        return None\n\n    man = find('coat_resgated_ep10_top3_manifest.json', MAN_SHA)\n    if man is None:\n        raise RuntimeError('coat manifest absent or hash mismatch')\n    art = man.parent\n    whl = find('opencv_python_headless-4.12.0.88-*.whl', WHL_SHA)\n    if whl is None:\n        for c in sorted(_P('/kaggle/input').rglob('opencv_python_headless-4.12.0.88-*.whl')):\n            if sha(c) == WHL_SHA:\n                whl = c\n                break\n    if whl is None:\n        raise RuntimeError('pinned opencv wheel absent or hash mismatch')\n\n    envd = _P('/kaggle/working/_coat_env')\n    _sp.run([_sy.executable, '-m', 'pip', 'install', '--no-deps', '--quiet',\n             '--target', str(envd), str(whl)], check=True)\n\n    out = _P('/kaggle/working/_coat_arm.csv')\n    child = (\n        \"import sys, json\\n\"\n        f\"sys.path.insert(0, {str(envd)!r})\\n\"\n        f\"sys.path.insert(0, {str(art)!r})\\n\"\n        \"import cv2; assert cv2.__version__ == '4.12.0', cv2.__version__\\n\"\n        \"import torch; assert torch.cuda.device_count() == 2\\n\"\n        \"import coatnet_resgated_ep10_top3_inference as rt\\n\"\n        \"assert rt.base.cv2.__version__ == '4.12.0'\\n\"\n        \"from pathlib import Path\\n\"\n        \"r = rt.run_submission(competition_root=rt.base.find_competition_root(),\\n\"\n        f\"    artifact_root=Path({str(art)!r}), output_path=Path({str(out)!r}),\\n\"\n        \"    gpu_batch_studies=2, backbone_micro_images=8)\\n\"\n        \"assert r['status'] == rt.SUBMISSION_STATUS\\n\"\n        \"assert r['models'] == 3\\n\"\n        \"assert [i['epoch'] for i in r['checkpoints']] == [4, 6, 8]\\n\"\n        \"assert r['fallback_studies'] == 0, r['failures']\\n\"\n        \"Path('/kaggle/working/_coat_arm_receipt.json').write_text(json.dumps(r, indent=2))\\n\")\n    env = dict(_o.environ)\n    env['PYTHONPATH'] = f\"{envd}:{art}:\" + env.get('PYTHONPATH', '')\n    proc = _sp.run([_sy.executable, '-c', child], env=env, capture_output=True, text=True)\n    if proc.returncode != 0:\n        raise RuntimeError(f'coat child failed: {proc.stderr[-700:]}')\n\n    ours = _pd.read_csv(out, dtype={'StudyInstanceUID': str})\n    labels = [column for column in ours.columns if column != 'StudyInstanceUID']\n    if not labels or ours['StudyInstanceUID'].duplicated().any():\n        raise RuntimeError('CoAtNet arm output schema is invalid')\n    if not _np.isfinite(ours[labels].to_numpy(_np.float64)).all():\n        raise RuntimeError('CoAtNet arm output contains non-finite values')\n    primary = _P('/kaggle/working/submission.csv')\n    temporary = primary.with_suffix('.csv.tmp')\n    ours.to_csv(temporary, index=False)\n    _o.replace(temporary, primary)\n    return len(ours)\n\n\n_coat_n = _coat_substitute()\nprint(f'[coat-arm] wrote standalone residual CoAtNet submission for {_coat_n} studies)', flush=True)\n"}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.11.13"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":154281,"sourceType":"competition"},{"sourceId":18842180,"sourceType":"datasetVersion"},{"sourceId":18839182,"sourceType":"datasetVersion"},{"sourceId":18229736,"sourceType":"datasetVersion"},{"sourceId":18956429,"sourceType":"datasetVersion"},{"sourceId":19075719,"sourceType":"datasetVersion"},{"sourceId":18875869,"sourceType":"datasetVersion"},{"sourceId":18673646,"sourceType":"datasetVersion"},{"sourceId":18673450,"sourceType":"datasetVersion"},{"sourceId":18716507,"sourceType":"datasetVersion"},{"sourceId":18879001,"sourceType":"datasetVersion"},{"sourceId":18757740,"sourceType":"datasetVersion"},{"sourceId":342671664,"sourceType":"kernelVersion"},{"sourceId":342849430,"sourceType":"kernelVersion"},{"sourceId":4533,"sourceType":"modelInstanceVersion"}],"dockerImageVersionId":31430,"isGpuEnabled":true,"isInternetEnabled":false,"language":"python","sourceType":"notebook"},"rsna_optimization":{"official_source_score":0.891,"revision":"v66-v65-parent-legacy-dino-002","source":"pilkwang/rsna-knee-baseline-v1"}},"nbformat":4,"nbformat_minor":4}