|
7 | 7 | "id": "view-in-github" |
8 | 8 | }, |
9 | 9 | "source": [ |
10 | | - "<a href=\"https://colab.research.google.com/github/MMathisLab/CellSeg3d/blob/main/Notebooks/Colab_inference_demo.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" |
| 10 | + "<a href=\"https://colab.research.google.com/github/AdaptiveMotorControlLab/CellSeg3D/blob/main/notebooks/Colab_inference_demo.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" |
11 | 11 | ] |
12 | 12 | }, |
13 | 13 | { |
|
53 | 53 | }, |
54 | 54 | "outputs": [], |
55 | 55 | "source": [ |
56 | | - "# @markdown ##Install CellSeg3D and grab demo data\n", |
57 | | - "!git clone https://github.com/AdaptiveMotorControlLab/CellSeg3d.git --branch main --single-branch ./CellSeg3D\n", |
58 | | - "!pip install napari-cellseg3d" |
| 56 | + "# @markdown ## Install CellSeg3D and ensure demo data\n", |
| 57 | + "\n", |
| 58 | + "import os\n", |
| 59 | + "import shutil\n", |
| 60 | + "import subprocess\n", |
| 61 | + "import sys\n", |
| 62 | + "import time\n", |
| 63 | + "from pathlib import Path\n", |
| 64 | + "\n", |
| 65 | + "REPO_DIR = Path(\"/content/CellSeg3D\")\n", |
| 66 | + "DEMO_FILE = REPO_DIR / \"examples\" / \"c5image.tif\"\n", |
| 67 | + "REPO_URL = \"https://github.com/AdaptiveMotorControlLab/CellSeg3D.git\"\n", |
| 68 | + "\n", |
| 69 | + "\n", |
| 70 | + "def run(cmd, **kwargs):\n", |
| 71 | + " print(\"+\", \" \".join(map(str, cmd)))\n", |
| 72 | + " subprocess.check_call(cmd, **kwargs)\n", |
| 73 | + "\n", |
| 74 | + "\n", |
| 75 | + "def cellseg3d_ok():\n", |
| 76 | + " try:\n", |
| 77 | + " import napari_cellseg3d\n", |
| 78 | + " from napari_cellseg3d.dev_scripts import remote_inference\n", |
| 79 | + "\n", |
| 80 | + " print(\"CellSeg3D import OK.\")\n", |
| 81 | + " return True\n", |
| 82 | + "\n", |
| 83 | + " except Exception as e:\n", |
| 84 | + " print(\"CellSeg3D import failed:\")\n", |
| 85 | + " print(type(e).__name__, e)\n", |
| 86 | + " return False\n", |
| 87 | + "\n", |
| 88 | + "\n", |
| 89 | + "def ensure_demo_data():\n", |
| 90 | + " if DEMO_FILE.is_file():\n", |
| 91 | + " print(f\"Demo data found: {DEMO_FILE}\")\n", |
| 92 | + " return\n", |
| 93 | + "\n", |
| 94 | + " print(\"Demo data not found. Cloning CellSeg3D repository...\")\n", |
| 95 | + "\n", |
| 96 | + " if REPO_DIR.exists():\n", |
| 97 | + " print(f\"Removing incomplete existing directory: {REPO_DIR}\")\n", |
| 98 | + " shutil.rmtree(REPO_DIR)\n", |
| 99 | + "\n", |
| 100 | + " run(\n", |
| 101 | + " [\n", |
| 102 | + " \"git\",\n", |
| 103 | + " \"clone\",\n", |
| 104 | + " REPO_URL,\n", |
| 105 | + " \"--branch\",\n", |
| 106 | + " \"main\",\n", |
| 107 | + " \"--single-branch\",\n", |
| 108 | + " str(REPO_DIR),\n", |
| 109 | + " ]\n", |
| 110 | + " )\n", |
| 111 | + "\n", |
| 112 | + " if not DEMO_FILE.is_file():\n", |
| 113 | + " raise FileNotFoundError(\n", |
| 114 | + " f\"Expected demo file was not found after cloning: {DEMO_FILE}\"\n", |
| 115 | + " )\n", |
| 116 | + "\n", |
| 117 | + " print(f\"Demo data ready: {DEMO_FILE}\")\n", |
| 118 | + "\n", |
| 119 | + "\n", |
| 120 | + "package_ok = cellseg3d_ok()\n", |
| 121 | + "installed_package = False\n", |
| 122 | + "\n", |
| 123 | + "if not package_ok:\n", |
| 124 | + " print(\"Installing CellSeg3D...\")\n", |
| 125 | + "\n", |
| 126 | + " if shutil.which(\"uv\") is None:\n", |
| 127 | + " run([sys.executable, \"-m\", \"pip\", \"install\", \"-q\", \"uv\"])\n", |
| 128 | + " else:\n", |
| 129 | + " print(\"uv already installed.\")\n", |
| 130 | + "\n", |
| 131 | + " run(\n", |
| 132 | + " [\n", |
| 133 | + " \"uv\",\n", |
| 134 | + " \"pip\",\n", |
| 135 | + " \"install\",\n", |
| 136 | + " \"--system\",\n", |
| 137 | + " \"napari-cellseg3d\",\n", |
| 138 | + " ]\n", |
| 139 | + " )\n", |
| 140 | + "\n", |
| 141 | + " installed_package = True\n", |
| 142 | + "\n", |
| 143 | + "else:\n", |
| 144 | + " print(\"CellSeg3D already available — skipping package install.\")\n", |
| 145 | + "\n", |
| 146 | + "\n", |
| 147 | + "ensure_demo_data()\n", |
| 148 | + "\n", |
| 149 | + "\n", |
| 150 | + "if installed_package:\n", |
| 151 | + " print(\"Restarting runtime to reload installed binary dependencies...\")\n", |
| 152 | + " print(\n", |
| 153 | + " \"Colab may show this as a crash, but it is expected. \"\n", |
| 154 | + " \"After the runtime reconnects, run the notebook again from the beginning. \"\n", |
| 155 | + " \"This cell should then skip installation and continue normally.\"\n", |
| 156 | + " )\n", |
| 157 | + "\n", |
| 158 | + " time.sleep(1)\n", |
| 159 | + " os.kill(os.getpid(), 9)\n", |
| 160 | + "\n", |
| 161 | + "else:\n", |
| 162 | + " print(\"Setup complete.\")" |
| 163 | + ] |
| 164 | + }, |
| 165 | + { |
| 166 | + "cell_type": "markdown", |
| 167 | + "metadata": {}, |
| 168 | + "source": [ |
| 169 | + "\n", |
| 170 | + "<div style=\"\n", |
| 171 | + " padding: 12px 16px;\n", |
| 172 | + " border-left: 5px solid #f59e0b;\n", |
| 173 | + " border-radius: 6px;\n", |
| 174 | + " margin: 12px 0;\n", |
| 175 | + "\">\n", |
| 176 | + " <b>⚠️ Runtime restart is required after installation</b><br>\n", |
| 177 | + "The runtime must be restarted after installing CellSeg3D to use the newly installed package. \n", |
| 178 | + "\n", |
| 179 | + "<b>The notebook will \"crash\" after installing, but this is expected, you may simply run the notebook again from the beginning after the runtime restarts.</b>\n", |
| 180 | + "</div>" |
59 | 181 | ] |
60 | 182 | }, |
61 | 183 | { |
|
0 commit comments