Writing Scripts

Every script starts with import nexus. The nexus module is how your script exchanges data with the application: it reads the signals you bind, exposes the parameters you declare, and returns results.

NumPy is available by default. Signal data is returned as NumPy arrays (falling back to plain lists if NumPy is unavailable).

Declaring parameters, inputs and outputs

Three functions declare what the script needs. Each takes a dictionary of definitions and returns the resolved values you set in the interface. Declare them near the top so the controls appear in the Selection panel.

nexus.add_parameters(defs)

Returns a dict of name → value. Each definition has a type, a title (the label shown), and a default:

params = nexus.add_parameters({
    "window": {"type": "number", "title": "Window size", "default": 3, "min": 1, "max": 99, "step": 1},
    "method": {"type": "enum", "title": "Method", "default": "mean", "options": ["mean", "median"]},
    "normalize": {"type": "boolean", "title": "Normalize", "default": False},
})
window = int(params["window"])

See Parameters & Pickers for every parameter type, including the coordinate/intensity/timestamp/ROI pickers.

nexus.add_inputs(defs)

Returns a dict of name → signal id (empty string if unbound). signal_type filters which signals can be bound:

inp = nexus.add_inputs({
    "src": {"title": "Source", "signal_type": "0Dts", "default": ""},
})
signal = nexus.get_signal(inp["src"])

A script won’t run until every required input is bound. Mark an input "optional": True to let it run unbound: its value is then "", so check it before calling get_signal. Bind inputs in the Input Signals section of the Selection panel.

nexus.add_outputs(defs)

Returns a dict of name → chosen output name. The signal_type declares what the output will be; the user can rename it:

out = nexus.add_outputs({
    "result": {"title": "Result", "signal_type": "0Dts", "default": "result"},
})

Reading a signal

nexus.get_signal(id) returns a read-only signal proxy. The most important accessor is get_data(), whose shape depends on the signal type:

Signal type Meaning get_data() shape
0Dts Scalar value vs time (T,)
1Dts Spectrum vs time (T, channels)
2Dts Image stack vs time (T, height, width)

Other members of the signal proxy:

Member Description
.timestamps List of time values (one per frame/point), in seconds on the signal’s own relative scale (not wall-clock time)
.metadata Dict of metadata (includes units, name, signal_type, and — when the source has it — experiment_start_unix_time)
.axes List of axis descriptors (name, units, scale, offset, size, navigate)
.signal_type The type string, e.g. "2Dts"
.width, .height Frame dimensions (2Dts)
.channels Number of spectrum channels (1Dts)
.pixel_size {"x", "y", "units"} from the spatial axes (2Dts)
.get_frame(i), .get_frame_count() One 2D frame (height, width) / frame count (2Dts)
.get_spectrum(i), .get_spectrum_count() One spectrum (channels,) / spectrum count (1Dts)
.crop(x, y, width, height) A rectangular region of every frame, with its calibration kept (2Dts) — see below
signal = nexus.get_signal(inp["src"])
frames = signal.get_data()             # (T, H, W) for a 2Dts stack
first = signal.get_frame(0)            # (H, W)
print(signal.width, signal.height, signal.get_frame_count())

Cropping a region

signal.crop(x, y, width, height) cuts the same rectangle out of every frame. x and y are the top-left corner in pixels; a rectangle that runs off the edge is clamped to the frame. The crop keeps the signal’s pixel size and records where the region sits, so a cropped result lines up with the original when you overlay it:

region = signal.crop(100, 50, 256, 256)
nexus.set_signal(out["result"], region.data, signal_type="2Dts", source=region)

signal.timestamps are on the signal’s relative time scale, but a picked timestamp parameter is absolute Unix-epoch seconds — so you can’t compare the two directly. Convert the picked value first; see the helper under Parameters & Pickers → timestamp.

Writing a result

nexus.set_signal(name, data, **kwargs) produces an output signal. data may be a NumPy array or a list; its shape is used to infer the signal type when you don’t declare one.

Keyword Purpose
signal_type Force the output type (otherwise inferred, or taken from add_outputs)
times Explicit timestamps (one per frame/point)
units Units string
width, height Frame dimensions for image output
channels Channel count for spectrum output
color Suggested display colour
metadata Dict of extra metadata to attach
axes List of axis descriptors (as above)
source A signal (or a crop) to take axes, units and timestamps from
fps Evenly spaced timestamps at this frame rate
nexus.set_signal(out["result"], smoothed, units=signal.metadata.get("units", ""))

You can call set_signal more than once to produce several outputs from one run.

Keeping the calibration with source=

A result computed from an input usually has the same pixel size, units and time axis. Pass that input as source= and they carry across, so the result is the right physical size next to its source and sits in the same place on the timeline:

frames = signal.get_data()
nexus.set_signal(out["result"], frames - frames.mean(axis=0), signal_type="2Dts", source=signal)

Without source= or axes=, a derived image falls back to plain pixels at scale 1.

If you don’t pass times, a result inherits the timeline position of the bound input (or of source=), so it lines up on the timeline with the data it came from. See API Reference → Timestamp resolution for the exact rules.

Output panel

Anything a script prints — and any error traceback — appears in the Output panel below the editor, so print() is your main debugging tool.