{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# One-Click Dependency Installer\n",
    "\n",
    "Run the code cell below once. It will:\n",
    "- locate `requirements.txt`\n",
    "- install all dependencies\n",
    "- verify required imports\n",
    "\n",
    "After installation, restart the kernel once if needed.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from __future__ import annotations\n",
    "\n",
    "import importlib\n",
    "import subprocess\n",
    "import sys\n",
    "from pathlib import Path\n",
    "\n",
    "\n",
    "def find_requirements_file() -> Path:\n",
    "    cwd = Path.cwd().resolve()\n",
    "    candidates = [cwd, *cwd.parents]\n",
    "\n",
    "    # Case 1: running from repo root\n",
    "    for base in candidates:\n",
    "        p = base / \"requirements.txt\"\n",
    "        if p.exists():\n",
    "            return p\n",
    "\n",
    "    # Case 2: running from the project root directly\n",
    "    for base in candidates:\n",
    "        p = base / \"requirements.txt\"\n",
    "        if p.exists() and ((base / \"Main_figures_codes\").exists()):\n",
    "            return p\n",
    "\n",
    "    raise FileNotFoundError(\n",
    "        \"Could not find requirements.txt. Expected requirements.txt near current directory.\"\n",
    "    )\n",
    "\n",
    "\n",
    "def run(cmd: list[str]) -> None:\n",
    "    print(\"$\", \" \".join(cmd))\n",
    "    subprocess.check_call(cmd)\n",
    "\n",
    "\n",
    "requirements_file = find_requirements_file()\n",
    "print(\"Python executable:\", sys.executable)\n",
    "print(\"Using requirements:\", requirements_file)\n",
    "\n",
    "# Upgrade pip then install project dependencies\n",
    "run([sys.executable, \"-m\", \"pip\", \"install\", \"--upgrade\", \"pip\"])\n",
    "run([sys.executable, \"-m\", \"pip\", \"install\", \"-r\", str(requirements_file)])\n",
    "\n",
    "# Quick import check\n",
    "packages = [\n",
    "    \"numpy\",\n",
    "    \"scipy\",\n",
    "    \"pandas\",\n",
    "    \"matplotlib\",\n",
    "    \"sklearn\",\n",
    "    \"h5py\",\n",
    "    \"hdmf\",\n",
    "    \"pynwb\",\n",
    "    \"tqdm\",\n",
    "]\n",
    "\n",
    "failed = []\n",
    "for pkg in packages:\n",
    "    try:\n",
    "        importlib.import_module(pkg)\n",
    "    except Exception as exc:\n",
    "        failed.append((pkg, str(exc)))\n",
    "\n",
    "if failed:\n",
    "    print(\"Some imports failed:\")\n",
    "    for pkg, err in failed:\n",
    "        print(f\"- {pkg}: {err}\")\n",
    "    raise RuntimeError(\"Dependency installation completed with import failures.\")\n",
    "\n",
    "print(\"All required packages installed and import checks passed.\")\n",
    "print(\"If this notebook kernel was already running before install, restart the kernel once.\")\n",
    "\n",
    "# Additional package installation check\n",
    "additional_packages = [\"numpy\", \"matplotlib\", \"scipy\", \"scikit-learn\", \"pandas\", \"joblib\"]\n",
    "run([sys.executable, \"-m\", \"pip\", \"install\"] + additional_packages)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9572a10b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv (3.9.6)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
