Policy Studio

from medaugmentx.studio import StudioServer, StudioSession, launch

The studio is a local web application for designing, previewing, and stress-testing policies. See the user guide for the workflow; this page documents the Python entry points for notebooks, scripts, and embedding.

Launching

medaugmentx.studio.launch(policy=None, volume=None, *, host='127.0.0.1', port=0, open_browser=True, block=True, token=None, allow_remote=False, verbose=False, source=None)[source]

Open the policy studio, optionally starting from a policy and volume.

With block=False (handy in notebooks) the server runs in the background; call server.current_policy() to retrieve the edited policy and server.stop() when done. With block=True it serves until Ctrl+C.

Parameters:
  • policy (Transform | Mapping[str, Any] | None) – Transform or to_dict() mapping to start editing.

  • volume (MedVolume | None) – In-memory volume to preview; load data from the UI otherwise.

  • host (str) – Interface to bind (loopback by default).

  • port (int) – TCP port, or 0 for any free port.

  • open_browser (bool) – Open the studio in the default web browser.

  • block (bool) – Serve in the foreground until interrupted.

  • token (str | None) – Fixed API token instead of a random one.

  • allow_remote (bool) – Permit a non-loopback host.

  • verbose (bool) – Log requests to stderr.

  • source (str | None) – Label shown for volume (for example its filename).

Return type:

StudioServer

class medaugmentx.studio.StudioServer(session=None, *, host='127.0.0.1', port=0, token=None, allow_remote=False, verbose=False)[source]

Bases: object

A running policy studio. Use launch() for the common case.

Parameters:
  • session (StudioSession | None) – State to serve; a fresh StudioSession by default.

  • host (str) – Interface to bind. Non-loopback hosts expose your images to the network and are only accepted with allow_remote=True.

  • port (int) – TCP port, or 0 to choose a free one.

  • token (str | None) – API token; a random one is generated by default.

  • allow_remote (bool) – Permit binding a non-loopback interface.

  • verbose (bool) – Log every request to stderr.

property url: str

Address to open, including the session token.

start()[source]

Serve in a background daemon thread and return immediately.

Return type:

StudioServer

serve_forever()[source]

Serve in the calling thread until interrupted or stop().

stop()[source]

Stop serving and delete uploaded files.

current_policy()[source]

The policy currently in the editor, as a ready-to-use transform.

Return type:

Transform

Session state

StudioSession holds the loaded volume and the edited policy, independently of the HTTP layer. Policies travel in the canonical to_dict() form.

class medaugmentx.studio.StudioSession(volume=None, policy=None, *, source=None)[source]

Bases: object

State for one studio: a loaded volume, its source, and the edited policy.

Parameters:
  • volume (MedVolume | None) – Optional in-memory volume to start with.

  • policy (Transform | Mapping[str, Any] | None) – Optional transform or to_dict() mapping to start editing.

  • source (str | None) – Human-readable label for volume.

close()[source]

Delete uploaded files held for this session.

load_phantom(name, *, demo_targets=True)[source]
Return type:

dict[str, Any]

load_path(image, mask=None, *, resample_mask=False, strict=True)[source]

Load NIfTI (.nii/.nii.gz), NumPy (.npy) or DICOM data.

Return type:

dict[str, Any]

add_channel(name, path)[source]

Attach a registered image as a named channel of the loaded volume.

Return type:

dict[str, Any]

remove_channel(name)[source]
Return type:

dict[str, Any]

state()[source]

Loaded-data summary, axis names, channels and the current policy.

Return type:

dict[str, Any]

compile(node)[source]

Build a transform, reporting the deepest failing step on error.

Return type:

Transform

set_policy(node)[source]

Store the edited policy and report whether it builds.

Return type:

dict[str, Any]

current_policy()[source]

The edited policy as a fresh transform (raises if it does not build).

Return type:

Transform

new_step(name)[source]

Canonical node for a newly added step, with volume-aware defaults.

Return type:

dict[str, Any]

preset(name, options=None)[source]
Return type:

dict[str, Any]

preview(request)[source]

Run one seeded draw and render matching input/output slices.

Return type:

dict[str, Any]

gallery(request)[source]

Render consecutive augmentation indices for a quick variability scan.

Return type:

dict[str, Any]

stress(request)[source]

Run many draws and aggregate validation, failures, timing and branches.

Return type:

dict[str, Any]

record(request)[source]

Exact replay record for one draw (see replay_augmentation).

Return type:

dict[str, Any]

export(node, fmt)[source]
Return type:

dict[str, Any]

import_text(text)[source]

Parse a JSON or YAML policy document into the canonical editor form.

Return type:

dict[str, Any]

exception medaugmentx.studio.PolicyError(message, path)[source]

Bases: ValueError

A policy that cannot be built, with the path of the failing step.

medaugmentx.studio.python_snippet(node)[source]

Readable Python that constructs the same policy, omitting defaults.

Return type:

str