Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 43 additions & 3 deletions marge/seq/pulserver_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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())
Expand Down
29 changes: 29 additions & 0 deletions tests/test_pulserver_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading