From 10d152a9aa43c3c458dae3cbbe029a1da05001ef Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 13 Oct 2026 14:33:22 +0000 Subject: [PATCH] State each pulserver plugin's scan time in MaRGE MaRGE asks a sequence for its scan time whenever a value is typed. The console's plugin sequences now ask pulserver to validate the values and return the scan time its reply states, in minutes; the localizer states none. Asynchronous gateways ask in the background and print the time once it arrives. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01XYJ3anWEs1W1EFN1m1fTmX --- marge/seq/pulserver_console.py | 46 ++++++++++++++++++++++++++++++--- tests/test_pulserver_console.py | 29 +++++++++++++++++++++ 2 files changed, 72 insertions(+), 3 deletions(-) diff --git a/marge/seq/pulserver_console.py b/marge/seq/pulserver_console.py index 315fc3c..adb38d1 100644 --- a/marge/seq/pulserver_console.py +++ b/marge/seq/pulserver_console.py @@ -523,6 +523,12 @@ class ConsoleSequence(base): titles: Sequence[str] | None = None + def sequenceInfo(self) -> None: # noqa: N802 -- MaRGE's API + print(f"{self.mapVals['seqName']}: run by pulserver's virtual scanner") + + def sequenceTime(self) -> float: # noqa: N802 -- MaRGE's API + return 0.0 + def sequenceAnalysis(self, mode=None) -> list: # noqa: N802 -- MaRGE's API """Save the run; show its images in the workspace as a series of the exam, else as MaRGE's output.""" self.mode = mode @@ -647,6 +653,42 @@ def __init__(self) -> None: self.files: list[bytes] = [] self.clock = (0.0, 0.0) self.prepared = 0 + self.scan_time = 0.0 + + def sequenceTime(self) -> float: # noqa: N802 -- MaRGE's API + """Return the scan time pulserver states for the values, in minutes; 0 where it states none. + + On an asynchronous gateway the request runs in the background: the + last scan time known is returned, and the new one printed once it arrives. + """ + if not getattr(gateway, "asynchronous", False): + self.scan_time = asyncio.run(self._scan_time()) + return self.scan_time + + async def background() -> None: + scan_time = await self._scan_time() + if scan_time != self.scan_time: + self.scan_time = scan_time + print(f"Sequence time {scan_time:0.1f} minutes") + + asyncio.ensure_future(background()) + return self.scan_time + + async def _scan_time(self) -> float: + """Ask pulserver to validate the values; return the scan time it states, in minutes.""" + _, block = self._block() + reply = await _answer(gateway.request("validate", plugin=plugin, block=block)) + word, _, seconds = reply["reply"].partition("\n")[0].partition(" ") + if reply["status"] != 0 or word != "VALID" or seconds in ("", "?"): + return 0.0 + return float(seconds) / 60.0 + + def _block(self): + """Return the prescription the values state and the value block that sends it.""" + planned = self.planned() + rotation, centre = planned.rotation, planned.centre + values = {name: self.mapVals[_key(name)] for name in names} + return planned, format_values(values, entries, rotation, rotation.T @ centre) def sequenceRun(self, plotSeq=0, demo=False) -> bool: # noqa: N802 -- MaRGE's API return _run(self, gateway, self._scan) @@ -676,12 +718,10 @@ def parameterChanged(self, key: str) -> None: # noqa: N802 -- MaRGE's naming WORKSPACE.redraw() async def _scan(self) -> bool: - planned = self.planned() + planned, block = self._block() rotation, centre = planned.rotation, planned.centre self.mapVals["fov"] = [0.1 * planned.fov[0], 0.1 * planned.fov[1], 0.2 * planned.extent] self.mapVals["dfov"] = [float(c) for c in centre] - values = {name: self.mapVals[_key(name)] for name in names} - block = format_values(values, entries, rotation, rotation.T @ centre) generated = await _answer(gateway.request("generate", plugin=plugin, block=block)) if generated["status"] != 0: raise RuntimeError(generated["reply"].strip()) diff --git a/tests/test_pulserver_console.py b/tests/test_pulserver_console.py index 9b9669c..7d87d56 100644 --- a/tests/test_pulserver_console.py +++ b/tests/test_pulserver_console.py @@ -545,3 +545,32 @@ def test_an_imaging_mode_the_sequence_states_is_not_offered_for_editing(): assert "imaging_mode" in entries assert "imaging_mode" not in console.shown(entries) + + +def _validated(status_line): + return {"validate": {"reply": {"status": 0, "reply": f"{status_line}\nINFO \n[Protocol]\n[Protocol End]\n"}}} + + +@pytest.mark.parametrize(("status_line", "minutes"), [("VALID 90.0", 1.5), ("VALID ?", 0.0), ("INVALID", 0.0)]) +def test_a_plugin_sequence_states_the_scan_time_pulserver_validates_in_minutes(status_line, minutes): + gateway = _Scripted(_validated(status_line)) + sequence = console._plugin_class(_Base, gateway, "gre2d", console.parse_listing(LISTING))() + + assert sequence.sequenceTime() == minutes + ((call, fields),) = gateway.calls + assert (call, fields["plugin"]) == ("validate", "gre2d") + assert "TE: 8000" in fields["block"] + + +def test_through_an_asynchronous_gateway_the_scan_time_is_printed_once_pulserver_states_it(capsys): + gateway = _AsyncScripted(_validated("VALID 120.0")) + sequence = console._plugin_class(_Base, gateway, "gre2d", console.parse_listing(LISTING))() + + async def run(): + known = sequence.sequenceTime() + await asyncio.sleep(0) + return known + + assert asyncio.run(run()) == 0.0 + assert sequence.scan_time == 2.0 + assert "Sequence time 2.0 minutes" in capsys.readouterr().out