Skip to content

Add the NIS-Elements bridge and MCP server, plus the Physarum and C. elegans Shorts - #3

Merged
qquais merged 17 commits into
mainfrom
physarum-imaging-and-estop
Oct 14, 2026
Merged

qquais merged 17 commits into
mainfrom
physarum-imaging-and-estop

Conversation

@qquais

@qquais qquais commented Oct 1, 2026 •

Copy link
Copy Markdown
Collaborator

Why
Some things can only be done through NIS-Elements: the AX confocal lasers and the ND experiments saved in its ND Acquisition window. The rest of this repo drives the Ti2 stand and the Baumer camera directly, so it could not reach them. The analysis output also needed a form that can be shared as video.

What

  • NIS bridge. A JOBS Python task in NIS serves a local HTTP API on 127.0.0.1:8766 over NIS macro calls, with a client and a command line for manual tests. Why a bridge and not code inside NIS: nothing has to be installed into NIS's own Python, and a crash in our code can't take NIS down.
  • NIS MCP server (confocal-mcp-nis), separate from server_loop. It offers status, relative XY moves, objective changes, capture and ND runs.
  • Motion needs confirm=True.
  • The e-stop is checked in the client and again in the bridge.
  • XY moves are capped at 1 mm, Z is never moved, and only the 4x and 10x objectives are allowed, because the longer working distances can't reach the dish.
  • Refusals and failures raise an error instead of looking like success.
  • Objective change turns the turret with Stg_SetNosepiecePosition first, then calls ChangeObjective. Why: ChangeObjective alone changed only NIS's calibration label and the turret did not move.
  • Shutdown. The JOBS Abort button cannot interrupt a Python task and NIS freezes, so the bridge has a shutdown command and ends itself after 4 hours with no request.
  • Capture folder. nis_capture writes only into data/NIS/captures by plain file name and never overwrites.
  • Shorts. Shared helpers for vertical YouTube Shorts, the Physarum Short (time-lapse, live growth curve, growth-map reveal), and the C. elegans Short from an AML18 NIS time-lapse. Why: to share results as short videos.
  • Docs and tests. docs/nis-bridge.md has the setup steps, the safety rules and the on-scope test plan. The tests run without NIS.

Testing
Off the scope only. The bridge was not run inside NIS when this was merged. The scope results came after, in #4.

A JOBS Python task that, while running, serves /status, /move (relative,
capped at 1 mm), /objective (4x/10x only), /capture (save as ND2),
/nd/run (load a saved ND experiment and run it), /nd/finish and
/shutdown on 127.0.0.1:8766. Motion and acquisition answer only POST;
nothing moves or starts while the e-stop file exists; Z is never moved.
NIS calls stay on the job thread, HTTP threads only queue them.

Macro signatures are from the NIS 6.20 reference on the microscope PC.
Checked off the scope over real HTTP with a stand-in DLL; not yet run
inside NIS.
NISBridge wraps the bridge job's HTTP API: status, move_relative,
change_objective, capture, nd_run, nd_finish, shutdown. The e-stop is
checked here before any motion or acquisition request is sent, so a
stopped microscope does not rely on the NIS side alone; ending an ND
run and shutting the bridge down are always allowed. An unreachable
bridge raises BridgeUnavailable with a pointer to the setup notes.
Tools that go through the NIS bridge: nis_status, nis_move_relative,
nis_change_objective, nis_capture, nis_run_experiment and
nis_finish_experiment, plus the shared estop. Everything that moves or
acquires needs confirm=True, like backend='sdk' in loop_tools; captures
go only to the data folder by plain file name; finishing an ND run and
the e-stop never need confirm.

A server of its own because server_loop's tool set is fixed by team
direction and deliberately free of NIS-Elements; loop_tools.py and
server_loop.py are unchanged.
Runs the real bridge HTTP server and queue, the real client and the
real MCP tools against a stand-in for g5_regprocs.dll that records
every macro call. Covers status, relative moves and the 1 mm cap,
GET never moving anything, the 4x/10x allow-list, the e-stop in both
client and bridge (with finishing an ND run still allowed), ND2
captures that never overwrite, ND run guards, the confirm gate,
capture file names confined to the capture folder, and the NIS
server's exact tool set.
docs/nis-bridge.md explains how the bridge, client and NIS MCP server
fit together, how to run the bridge as a JOBS task, and the step-by-step
test plan for the microscope - including the open questions (does NIS
stay usable, does ChangeObjective turn the nosepiece, does
ND_RunExperiment block). README and CHANGELOG point at it.
scipy, scikit-image, nd2, imageio-ffmpeg and opencv-python were imported by
the analysis scripts but not declared, so a fresh install could not run them.
They are now the 'analysis' group, included in 'all'.
The stage_positions demo loaded protocols/example_protocol.yaml, which is not
in this repo, so it always failed; that step is removed. Comments citing
docs/microscope-notes.md, run_protocol.py and the calibration tests now name
ConfocalOrchestrator, and old names (mcp_server/server.py,
acquisition_tools.py) are replaced with the current ones.
@qquais qquais self-assigned this Oct 1, 2026
Canvas, fonts, centred text and an ffmpeg writer (x264 + AAC in one pass,
or preview stills) used by the Physarum and C. elegans Shorts.
Runs the combined analysis script's alignment, draws the smoothed on-agar
area under the time-lapse and wipes to growth_start_to_end_colour.jpg,
which is the same frame so needs no registration.
Re-links tracks at 0.5 mm with a 3-frame position median, features the
longest-followed adults named by speed, and states the Explorer/Homebody
ratio as the smaller of the raw and filtered figures.
On the scope, ChangeObjective only switched NIS's calibration label and
left the turret where it was (the Ti2 SDK still read position 1). The
bridge now sets the nosepiece position by the objective's exact name,
waits until the turret and NIS's objective name agree, and reports
"arrived". The nosepiece index is 0-based. Record the 2026-10-13 test
results in docs/nis-bridge.md; step 5 needs a re-run on the scope.
…rash

On a cp1252 Windows console, 'status' fetched the answer and then died
with UnicodeEncodeError on the lambda in 'PLAN APO λD 4x'.
…ce and idle stop

limjob.RunContext has no abort flag (only savedFiles), so the shouldAbort
lookup never fired and the JOBS Abort button froze NIS. Drop the lookup,
document shutdown as the way to stop the job, and end the job on its own
after 4 hours with no request.
…eObjective

On the scope, Stg_SetNosepiecePosition turns the turret but NIS's objective
name and calibration stay on the old objective, and ChangeObjective does the
reverse. Do both in order: turn, wait for the turret, then ChangeObjective.
No calibration switch if the turret did not arrive. The fake NIS in the tests
now models both behaviours.
The bridge reports a refused move cap, an e-stop, a bad objective name, an
existing capture file or a running ND experiment as an ordinary answer with
an 'error' key, which an MCP client saw as success. A turret that did not
arrive and a capture that left no file are failures too. Checked over stdio
against the live bridge on 2026-10-14.
@qquais
qquais merged commit 3aef528 into main Oct 14, 2026
4 checks passed
@qquais qquais changed the title NIS Bridge Setup, Python Script Add e-stop, mosaic and time-lapse imaging, adaptive scheduler, worm follow, and NIS bridge Oct 17, 2026
@qquais qquais changed the title Add e-stop, mosaic and time-lapse imaging, adaptive scheduler, worm follow, and NIS bridge Add the NIS-Elements bridge and MCP server, plus the Physarum and C. elegans Shorts Oct 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant