{
 "nbformat": 4,
 "nbformat_minor": 5,
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  }
 },
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ParkFly P3: real FlyWire connectome\n",
    "No FlyWire token needed. Run cells in order. Six sanity checks must pass. Lesion cuts Giant Fibre input columns. Coarse results are exported before the full grid; raw runs checkpoint every ten completions.\n",
    "The connectome is a fixed spiking filter, not a trained parking model. Outputs are rates and their SD across fixed seeds."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "from pathlib import Path\n",
    "import os, sys\n",
    "if not Path('/content/parkfly/.git').exists():\n",
    "    !git clone -q https://github.com/momacaes9/parkfly.git /content/parkfly\n",
    "%cd /content/parkfly\n",
    "%pip -q install numpy pandas scipy pyarrow matplotlib\n",
    "sys.path.insert(0, '/content/parkfly/pipeline')\n",
    "import fly_surface as fs\n",
    "import pandas as pd\n",
    "Path(fs.OUT).mkdir(parents=True, exist_ok=True)\n",
    "sanity = fs.sanity()\n",
    "display(pd.DataFrame(sanity['checks']))\n",
    "assert sanity['all_pass'], 'STOP: fix wiring before the surface.'\n"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "import shutil\n",
    "from google.colab import files\n",
    "fs.GRID = {'sugar':[0,50,100,150,200], 'bitter':[0,50,100,200], 'loom':[0,50,100,150]}\n",
    "fs.surface(workers=2, seeds=3, coarse=True)\n",
    "coarse = Path(fs.OUT) / 'fly_surface.coarse.json'\n",
    "shutil.copyfile(Path(fs.OUT) / 'fly_surface.json', coarse)\n",
    "files.download(str(coarse))\n",
    "fs.surface(workers=2, seeds=3, coarse=False)\n",
    "files.download(str(Path(fs.OUT) / 'fly_surface.json'))\n",
    "files.download(str(Path(fs.OUT) / 'fly_sanity.json'))\n"
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "# Optional P3 step 3: recorded spike rasters for three canonical scenarios.\n",
    "fs.raster()\n",
    "files.download(str(Path(fs.OUT) / 'fly_raster.json'))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Optional Giant Fibre skeleton\n",
    "The uploaded NC State starter uses FAFBseg 3.2.3, Navis 1.12.0, Plotly and crackle-codec for this visualization. The full NumPy/SciPy simulation above needs no token. This optional cell remains off until you explicitly enable it. Never save a token in notebook source or output."
   ]
  },
  {
   "cell_type": "code",
   "metadata": {},
   "execution_count": null,
   "outputs": [],
   "source": [
    "RUN_SKELETON_VIEW = False\n",
    "if RUN_SKELETON_VIEW:\n",
    "    import subprocess, sys\n",
    "    subprocess.check_call([sys.executable, \"-m\", \"pip\", \"install\", \"fafbseg==3.2.3\", \"navis==1.12.0\", \"plotly\", \"crackle-codec\"])\n",
    "    from getpass import getpass\n",
    "    from fafbseg import flywire\n",
    "    import navis\n",
    "    token = getpass(\"Private FlyWire token (only for the optional skeleton): \").strip()\n",
    "    if not token:\n",
    "        print(\"No token entered; skipping visualization. Brain simulation remains available.\")\n",
    "    else:\n",
    "        flywire.set_chunkedgraph_secret(token, overwrite=True)\n",
    "        flywire.set_default_dataset(\"public\")\n",
    "        del token\n",
    "        annotations = flywire.search_annotations(flywire.NeuronCriteria(type=\"DNp01\"))\n",
    "        navis.plot3d(flywire.get_skeletons(annotations.root_id.iloc[0]))\n"
   ]
  }
 ]
}