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
19 changes: 13 additions & 6 deletions marge/seq/pulserver_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@
#: WebSocket address of ``pulserver console``; the console mode is off without it.
GATEWAY = os.environ.get("MARGE_PULSERVER", "")

PROTOCOL_BEGIN = "[NimPulseqGUI Protocol]"
PROTOCOL_END = "[NimPulseqGUI Protocol End]"
PROTOCOL_BEGIN = "[Protocol]"
PROTOCOL_END = "[Protocol End]"
#: The delimiters of a console that predates pulserver's ``[Protocol]`` block, also read.
_FORMER = ("[NimPulseqGUI Protocol]", "[NimPulseqGUI Protocol End]")
FOV_OFFSET = ("fov_offset_x", "fov_offset_y", "fov_offset_z")
FOV_ROTATION = tuple(f"fov_rotation_{i}{j}" for i in (1, 2, 3) for j in (1, 2, 3))
PRESCRIPTION = FOV_OFFSET + FOV_ROTATION
Expand Down Expand Up @@ -79,10 +81,10 @@ def parse_listing(reply: str) -> dict[str, dict[str, Any]]:
inside = False
for line in reply.splitlines():
text = line.strip()
if text == PROTOCOL_BEGIN:
if text in (PROTOCOL_BEGIN, _FORMER[0]):
inside = True
continue
if text == PROTOCOL_END:
if text in (PROTOCOL_END, _FORMER[1]):
break
if not inside or text.startswith("#") or ": " not in text:
continue
Expand Down Expand Up @@ -150,7 +152,9 @@ def format_values(
if entry["kind"] not in _EDITABLE or name in PRESCRIPTION:
continue
if entry["kind"] == "bool":
text = "true" if value else "false"
# MaRGE's parameter tabs hand an edited boolean back as its text.
on = value.strip().lower() == "true" if isinstance(value, str) else bool(value)
text = "true" if on else "false"
elif entry["kind"] == "stringlist":
text = str(entry["options"].index(value))
elif entry["kind"] == "int":
Expand Down Expand Up @@ -183,7 +187,10 @@ def prescription(
cross = np.array([[0.0, -k[2], k[1]], [k[2], 0.0, -k[0]], [-k[1], k[0], 0.0]])
theta = math.radians(angle_deg)
turn = np.eye(3) + math.sin(theta) * cross + (1 - math.cos(theta)) * cross @ cross
rotation = turn @ ORIENTATIONS[orientation]
name = orientation.strip().lower()
if name not in ORIENTATIONS:
raise ValueError(f"orientation is one of {', '.join(ORIENTATIONS)}, not {orientation!r}")
rotation = turn @ ORIENTATIONS[name]
return rotation, rotation.T @ np.asarray(dfov_mm, dtype=float)


Expand Down
30 changes: 28 additions & 2 deletions tests/test_pulserver_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#: pulserver's listing of its gre2d test plugin, with a boolean, a string list
#: and a description added.
LISTING = """PROTOCOL
[NimPulseqGUI Protocol]
[Protocol]
TE: int|dropdown|8000|1000|80000|10|us|-2|5000|8000
bandwidth: float|typein|250000.0|1000.0|1000000.0|1.0|Hz
nx: int|typein|128|32|512|2|
Expand All @@ -29,7 +29,7 @@
note: description|two\\nlines
fov_offset_x: float|off|0.0|-1000.0|1000.0|0.1|mm
fov_rotation_11: float|off|1.0|-1.0|1.0|1e-06|
[NimPulseqGUI Protocol End]
[Protocol End]
"""


Expand All @@ -56,6 +56,32 @@ def test_a_listing_reads_as_the_interpreter_reads_it():
assert entries["note"]["value"] == "two\nlines"


def test_a_boolean_edited_as_text_in_marges_tabs_travels_as_its_value():
entries = console.parse_listing(LISTING)
rotation = console.ORIENTATIONS["axial"]

for text, sent in (("False", "false"), ("True", "true")):
block = console.format_values({"fatsat": text}, entries, rotation, (0.0, 0.0, 0.0))
assert block.splitlines()[1] == f"fatsat: {sent}"


def test_an_orientation_typed_in_marges_image_tab_is_read_whatever_its_case():
rotation, _ = console.prescription(" Coronal", 0.0, (1.0, 0.0, 0.0), (0.0, 0.0, 0.0))

np.testing.assert_array_equal(rotation, console.ORIENTATIONS["coronal"])
with pytest.raises(ValueError, match="axial, coronal, sagittal"):
console.prescription("oblique", 0.0, (1.0, 0.0, 0.0), (0.0, 0.0, 0.0))


def test_a_listing_of_an_older_console_reads_the_same():
former = LISTING.replace("[Protocol]", "[NimPulseqGUI Protocol]").replace(
"[Protocol End]", "[NimPulseqGUI Protocol End]"
)

assert console.parse_listing(former) == console.parse_listing(LISTING)
assert console.parse_listing(LISTING)


def test_the_console_shows_the_editable_entries_outside_the_prescription():
entries = console.parse_listing(LISTING)

Expand Down
Loading