Skip to content

Analysis#

The Analysis page runs Python against data you have already recorded. Use it for tasks the built-in tools do not cover: a fit the fitting panel does not offer, a metric computed across several scans, a tuning routine of your own.

The Analysis page with a script and its output

Nothing here acquires data by itself unless your script asks for it — run_sequence is available, but the page is built around working on what is already loaded.

Writing a script#

The editor holds one script. New starts an empty one and Open loads a saved analysis, built-in ones included; Save and Save As store it by name. Built-in analyses cannot be overwritten, so use Save As to keep your edits, and saving under an existing name asks you to confirm the replacement. Start runs it.

Scripts run with the project's data in scope. The objects you will use most:

  • SampleInfo — the loaded sample, including t1_info and t2_info with their curves and fitted values.
  • DeviceSettings — the current device parameters, readable and writable.
  • run_sequence(...) — runs a pulse sequence and returns the signal and its spectrum.
  • viz.plot(...) — draws into the page's plot, which updates as the script runs.
  • viz.show_figure(fig) and viz.show_table(...) — show a matplotlib figure or a table in the Figures panel.
  • clear_annotations(...) and the annotation helpers — mark up that plot.

The scripting guide provides examples, and the backend API reference documents them in full.

Multi-scan workbench#

The toolbar button at the top right of the editor switches the page into the multi-scan workbench: a scan picker on the left, the script and its Figures panel on the right, and a Script / Series tab pair above the editor. Series builds a result across the selected scans (for example a value against temperature) without writing code. The button shows a dot while figures are waiting and the workbench is closed.

Figures from viz.show_figure are listed in the Figures panel. Outside the workbench the panel appears below the editor as soon as a script produces a figure. Clear figures can be undone from the notification that follows it; figures are not saved with the project and are gone once the app closes.

The download button on a figure saves it as PNG, SVG or PDF, named after the sample, scan and figure; a notification gives the path with Show in folder. PNG is rendered at the size set in Report Settings. SVG and PDF are vector files and exist only for matplotlib figures; other figures report that they can only be exported as PNG.

Libraries#

numpy, scipy, pandas, matplotlib, nmrglue and tqt_nmr are available, alongside the rest of the standard library. Install anything else from PyPI on the Settings page; packages installed there are visible to analysis scripts, custom pages, plugins and processor code snippets alike.

Example: tune the frequency from the spectrum#

Run a short FID, find where the peak actually sits, and correct the transmitter frequency by that offset.

ACQ_TIME = 3000

SEQUENCE = [
    Pulse("x+", 3.6),
    Silence(11),
    ADC(ACQ_TIME),
]

_, spectrum = run_sequence(SEQUENCE)

shift_mhz = spectrum.frequencies[spectrum.amplitudes.real.argmax()]
shift_hz = shift_mhz / 1e6

tuned_freq = DeviceSettings.frequency + shift_hz
print("New Frequency:", tuned_freq)

DeviceSettings.frequency = tuned_freq

Example: sweep the phase#

Step the transmitter phase through a full turn, measure the response at each step, and keep the angle that maximises the real part.

import numpy as np
from tqt_nmr import Signal

NUM_STEPS = 50
ECHO_TIME = 200
RING_TIME = 11
START_SLEEP = 2
MAX_PHASE_ANGLE = 360

SEQUENCE = [
    Silence(START_SLEEP),
    Pulse("x+", DeviceSettings.p90_us),
    Silence(RING_TIME),
    ADC(ECHO_TIME),
    Silence(START_SLEEP),
]


def tune_iteration(phase_angle: float) -> tuple[float, float]:
    DeviceSettings.phase_deg = phase_angle

    acq_start_time = START_SLEEP + RING_TIME + DeviceSettings.p90_us
    signal, _ = run_sequence(SEQUENCE, seq_null=acq_start_time)

    mean_real = -np.mean(signal.amplitudes[4:11].real)
    mean_imag = -np.mean(signal.amplitudes[4:11].imag)

    return mean_real, mean_imag


phase_angles = np.linspace(0, MAX_PHASE_ANGLE, NUM_STEPS)
amplitudes = []

for i, phase_angle in enumerate(phase_angles):
    print(f"{i + 1} / {len(phase_angles)}")
    mean_real, mean_imag = tune_iteration(phase_angle)

    amplitudes.append(mean_real + 1j * mean_imag)

    viz.plot(Signal(phase_angles[: i + 1], np.array(amplitudes)))

opt_phase = phase_angles[np.array(amplitudes).real.argmax()]
print("Optimal Phase [deg]:", opt_phase)

DeviceSettings.phase_deg = opt_phase

The Home page does both of these for you. They are worth reading as templates for a sweep of your own.

When not to use a script#

Repeatable signal processing belongs in the Processor, where it is a saved graph applied to every scan rather than a script somebody has to remember to run.