Skip to content

Commit 406c8b1

Browse files
authored
Update Colab demo notebook (#128)
* Fix URL * Add uv install * Add force restart after install * Add better checks for install * Fix URL again * Fix commands syntax * Update Colab_inference_demo.ipynb * Update Colab_inference_demo.ipynb * Colab demo: ensure demo data; refine install flow Update Colab_inference_demo.ipynb to ensure demo data and improve setup flow. Add REPO_DIR/DEMO_FILE/REPO_URL constants, import time, and a run() print fix. Introduce ensure_demo_data() to clone the CellSeg3D repo and verify the expected example file, removing previous ad-hoc cloning logic. Track whether the package was installed with installed_package, improve user messages about runtime restart, add a short sleep before killing the process, and print a concise completion message when setup is already satisfied. * URL case fix * Update test_plugins.py
1 parent a6ad800 commit 406c8b1

2 files changed

Lines changed: 129 additions & 6 deletions

File tree

napari_cellseg3d/_tests/test_plugins.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ def test_all_plugins_import(make_napari_viewer_proxy):
88
plugins.napari_experimental_provide_dock_widget()
99

1010

11-
def test_plugin_metrics(make_napari_viewer_proxy):
11+
def test_plugin_metrics(make_napari_viewer_proxy, qtbot):
1212
viewer = make_napari_viewer_proxy()
1313
w = m.MetricsUtils(viewer=viewer, parent=None)
14-
viewer.window.add_dock_widget(w)
14+
qtbot.addWidget(w)
15+
# viewer.window.add_dock_widget(w)
1516

1617
im_path = str(Path(__file__).resolve().parent / "res/test.tif")
1718
labels_path = im_path

notebooks/Colab_inference_demo.ipynb

Lines changed: 126 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"id": "view-in-github"
88
},
99
"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>"
1111
]
1212
},
1313
{
@@ -53,9 +53,131 @@
5353
},
5454
"outputs": [],
5555
"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>"
59181
]
60182
},
61183
{

0 commit comments

Comments
 (0)