From cb75c845cf4f3815dd1258465bd16fa7f6d4dda7 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 9 Oct 2026 12:04:34 +0000 Subject: [PATCH] Show and accept protocol times in ms in the pulserver console Times listed in us or s read in float ms and are sent back in the unit and kind the listing gives. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01B4nuUhxDhWwoCM2sCL1zQp --- marge/seq/pulserver_console.py | 37 ++++++++++++++++++++++++++++++--- tests/test_pulserver_console.py | 29 ++++++++++++++++++-------- 2 files changed, 54 insertions(+), 12 deletions(-) diff --git a/marge/seq/pulserver_console.py b/marge/seq/pulserver_console.py index 0576c25..315fc3c 100644 --- a/marge/seq/pulserver_console.py +++ b/marge/seq/pulserver_console.py @@ -54,6 +54,10 @@ _EDITABLE = ("float", "int", "bool", "stringlist") +#: Milliseconds per wire unit of the times the console shows in ms, as the +#: scanner's interpreter shows them. +_MS_PER = {"us": 1e-3, "s": 1e3} + def console_mode() -> bool: """Whether MaRGE runs as the console of pulserver's virtual scanner.""" @@ -65,7 +69,7 @@ def parse_listing(reply: str) -> dict[str, dict[str, Any]]: Each entry holds its ``kind`` and ``value``; a number also its ``mode``, ``min``, ``max``, ``step``, ``unit`` and dropdown ``options``, and a - stringlist its ``options``. + stringlist its ``options``. A time listed in us or s reads in float ms. """ entries: dict[str, dict[str, Any]] = {} inside = False @@ -93,6 +97,8 @@ def parse_listing(reply: str) -> dict[str, dict[str, Any]]: "unit": unit, "options": [cast(o) for o in options if o], } + if unit in _MS_PER: + entries[name] = _in_ms(entries[name]) elif kind == "bool": entries[name] = {"kind": kind, "value": fields[0] == "true"} elif kind == "stringlist": @@ -112,6 +118,26 @@ def parse_listing(reply: str) -> dict[str, dict[str, Any]]: return entries +def _in_ms(entry: dict[str, Any]) -> dict[str, Any]: + """Return a time entry restated in float ms, with its wire kind and ms per wire unit under ``wire``. + + Negative dropdown options are the interpreter's markers rather than times + and keep their values. + """ + scale = _MS_PER[entry["unit"]] + return { + **entry, + "kind": "float", + "value": entry["value"] * scale, + "min": entry["min"] * scale, + "max": entry["max"] * scale, + "step": entry["step"] * scale, + "unit": "ms", + "options": [o * scale if o >= 0 else float(o) for o in entry["options"]], + "wire": (entry["kind"], scale), + } + + #: Entries a sequence states once, as the interpreter reads them at its start, #: and a console does not edit. STATED = ("imaging_mode",) @@ -137,8 +163,9 @@ def format_values( ) -> str: """Return the value block of a request: the given values, then the prescription. - A stringlist travels as its option index and a boolean as ``true`` or - ``false``, as the interpreter sends them. ``rotation`` takes the logical + A stringlist travels as its option index, a boolean as ``true`` or + ``false`` and a time in its listed unit and kind, as the interpreter sends + them. ``rotation`` takes the logical readout, phase and slice axes to the physical ones, and ``offset_mm`` is the field-of-view offset along the logical axes, in mm. """ @@ -153,6 +180,10 @@ def format_values( text = "true" if on else "false" elif entry["kind"] == "stringlist": text = str(entry["options"].index(value)) + elif "wire" in entry: + kind, scale = entry["wire"] + sent = float(value) / scale + text = str(round(sent)) if kind == "int" else repr(sent) elif entry["kind"] == "int": text = str(int(value)) else: diff --git a/tests/test_pulserver_console.py b/tests/test_pulserver_console.py index b051726..9b9669c 100644 --- a/tests/test_pulserver_console.py +++ b/tests/test_pulserver_console.py @@ -38,14 +38,15 @@ def test_a_listing_reads_as_the_interpreter_reads_it(): entries = console.parse_listing(LISTING) assert entries["TE"] == { - "kind": "int", + "kind": "float", "mode": "dropdown", - "value": 8000, - "min": 1000, - "max": 80000, - "step": 10, - "unit": "us", - "options": [-2, 5000, 8000], + "value": 8.0, + "min": 1.0, + "max": 80.0, + "step": 0.01, + "unit": "ms", + "options": [-2.0, 5.0, 8.0], + "wire": ("int", 1e-3), } assert entries["bandwidth"]["value"] == 250000.0 assert entries["fatsat"] == {"kind": "bool", "value": False} @@ -57,6 +58,16 @@ def test_a_listing_reads_as_the_interpreter_reads_it(): assert entries["note"]["value"] == "two\nlines" +def test_a_time_shown_in_ms_travels_in_the_unit_and_kind_it_is_listed_in(): + entries = console.parse_listing( + LISTING.replace("[Protocol End]", "TR: float|typein|1.5|0.1|10.0|0.1|s\n[Protocol End]") + ) + + assert console._label("TE", entries) == "TE (ms)" + block = console.format_values({"TE": 7.5, "TR": 1200.0}, entries, np.eye(3), (0.0, 0.0, 0.0)) + assert block.splitlines()[1:3] == ["TE: 7500", "TR: 1.2"] + + def test_a_boolean_edited_as_text_in_marges_tabs_travels_as_its_value(): entries = console.parse_listing(LISTING) rotation = np.eye(3) @@ -88,7 +99,7 @@ def test_a_value_block_carries_the_values_then_the_prescription_as_the_interpret rotation = geometry.BASES["coronal"] block = console.format_values( - {"TE": 5000.0, "bandwidth": 1e5, "fatsat": True, "readout": "cartesian"}, + {"TE": 5.0, "bandwidth": 1e5, "fatsat": True, "readout": "cartesian"}, entries, rotation, (1.5, -2.0, 3.0), @@ -330,7 +341,7 @@ def test_the_listings_are_each_plugins_entries(): found = console.listings(gateway) assert list(found) == ["gre2d", "broken"] - assert found["gre2d"]["TE"]["value"] == 8000 + assert found["gre2d"]["TE"]["value"] == 8.0 def _scan_answers(status=0):