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
37 changes: 34 additions & 3 deletions marge/seq/pulserver_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand All @@ -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
Expand Down Expand Up @@ -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":
Expand All @@ -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",)
Expand All @@ -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.
"""
Expand All @@ -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:
Expand Down
29 changes: 20 additions & 9 deletions tests/test_pulserver_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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)
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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):
Expand Down
Loading