Developer Guide: Adding Features to pyIrena#
How to add a new analysis feature (a model, a calculation method, or a whole tool) so it works everywhere users expect: GUI, JSON scripting, batch/CLI, HDF5 persistence, MCP/AI tools, Data Selector (graphing/report/tabulation), HDF5 Data Explorer (trend plots, Igor export), and documentation.
pyIrena has grown a number of integration surfaces; forgetting one produces a feature that “works in the GUI” but silently vanishes from reports, batch runs, or Igor exports. This guide is the map. It was written while adding the Invariant method to Simple Fits (July 2026) — that commit is a good reference diff for the full wiring of a new Simple Fits entry.
Contents#
Architecture in one paragraph#
Every tool follows the same layering: a core module
(pyirena/core/<tool>.py) holds all math and a serialisable model object
(to_dict/from_dict); a GUI panel (pyirena/gui/<tool>_panel.py) is a
thin Qt shell over the core object whose full control state round-trips
through StateManager (_collect_state/load_state); an io module
(pyirena/io/nxcansas_<tool>.py) saves/loads results into the NXcanSAS HDF5
file under entry/<tool>_results and embeds the GUI state as
_pyirena_config for setup restore; a batch function
(pyirena/batch/<tool>.py) runs the same core headlessly from a dict or a
JSON config section; the api/mcp layer (pyirena/api/results.py,
pyirena/mcp/server.py) exposes read access to the AI tools; and the two
browsers — Data Selector (gui/data_selector/) and HDF5 Data
Explorer (gui/hdf5viewer/) — consume the saved HDF5 groups for graphing,
reports, CSV tabulation, trend plots, and Igor-experiment export
(io/h5xp_extractor.py + io/igor_names.py).
The golden rule: all state lives in the core model object and flows dict → JSON → HDF5 unchanged. If you add a control that only exists as a Qt widget, it will not survive scripting, batch, or setup restore.
Decision: what kind of feature is it?#
Feature type |
Effort |
Start at section |
|---|---|---|
New Simple Fits model (formula + params) |
Small — the registry does most wiring |
|
Calculation method (no least-squares, e.g. Invariant) |
Medium — registry + |
|
New option/control on an existing tool |
Small–medium — model attr + state + JSON + HDF5 attr |
|
New form/structure factor |
See |
— |
Entirely new tool |
Large — ~45 files, a dozen of them registries |
The master checklist#
Work top to bottom; check each box. Items marked (auto) come for free if you follow the registry/dict conventions — verify, don’t implement.
CORE
[ ] 1. Math in pyirena/core/<tool>.py — pure numpy/scipy, no Qt imports
[ ] 2. Registered: MODEL_REGISTRY entry (Simple Fits) or model-object attribute
[ ] 3. New state serialised in to_dict()/from_dict() (defaults for old files!)
[ ] 4. Exported from pyirena/__init__.py if part of the public API
GUI
[ ] 5. Controls in the panel; visibility handled on model/option change
[ ] 6. State: _collect_state() + load_state() include every new key
(this feeds StateManager, JSON export AND the HDF5-embedded setup)
[ ] 7. Plots updated + cleared on model change (no stale curves)
[ ] 8. Help button / tooltips updated
PERSISTENCE (HDF5, NXcanSAS)
[ ] 9. io/nxcansas_<tool>.py: save new scalars (params/, derived/, attrs)
and arrays (datasets with units attr); loader reads them back
[ ] 10. Setup round-trip: "Store in File" → "Load Setup from File…" restores
every new control (uses the state dict from step 6 — verify only)
[ ] 11. io/schema.py: add _scalar()/_plot() entries so generic consumers
(HDF5 viewer scalar browser, merge tool) know about the values
SCRIPTING / BATCH / CLI
[ ] 12. JSON: "Save/Load params to JSON" round-trips (auto via step 6)
[ ] 13. batch/<tool>.py: headless function honours the new option
(from_dict conventions make this automatic; check special routing)
[ ] 14. fit_pyirena / pipeline.py picks it up from the config section (auto)
MCP / AI API
[ ] 15. api/results.py read_<tool>: new fields returned (auto if generic
params/derived loops; add explicit fields otherwise)
[ ] 16. api/aggregate.py: parameter reachable by tabulate/trend tools
(fallback probes params/<name> and derived/<name> — auto)
[ ] 17. mcp/server.py: only needs changes for a genuinely new READ-ONLY
api function. A new pyirena.api.control function needs a schema
in api/control/schemas.py (TOOL_SCHEMA_BY_NAME) and an export
from api/control/__init__.py — pyirena/mcp/dispatch.py's registry
picks it up automatically, no new MCP tool needed (see the
"Control API — dispatcher" section of mcp/server.py)
DATA SELECTOR (Data Browser)
[ ] 18. Create Report (data_selector/report.py): new values in the Markdown
report (auto if stored under params/ or derived/)
[ ] 19. Create Graph (data_selector/results_windows.py): stored results
render sensibly (special-case if the tool has no model curve)
[ ] 20. Tabulate Results: columns appear (auto via params/derived)
[ ] 21. <Tool> (script) button path works (auto via step 13)
HDF5 DATA EXPLORER
[ ] 22. plot_controls.py: new item names in the "collect" dropdown for
trend plots (parameter vs file/temperature/time)
[ ] 23. pyirena_readers.py _collect_<tool>: new values readable
(params/ auto; derived/ needs the fallback added July 2026)
[ ] 24. Igor export: io/igor_names.py wave-name entries (Y wave and
RESULT_X_WAVE pairing) + io/h5xp_extractor.py writes the waves
and wave-note parameters
STANDARD UX CONTRACT (see below — every new GUI surface)
[ ] 25. Every table: attach_table_copy() (+ enable_table_sorting() where the
row order is not meaningful); CSV export for multi-row results
[ ] 26. Every plot: attach_plot_export() — or make_sas_plot(parent_widget=…)
[ ] 27. Every file list: shared filter (gui/file_filter.py), shared sort modes,
remembered folder
DOCS & TESTS
[ ] 28. docs/<tool>_gui.md: user documentation + scripting example
[ ] 29. CHANGELOG.md entry
[ ] 30. Tests: math vs analytic ground truth; registry/serialisation
round-trip; HDF5 save/load round-trip; batch-config path
[ ] 31. Run the full test suite, not just the new file
Adding a whole new tool#
Everything above assumes the tool’s files already exist and tells you what to wire. This section is the other half: which files to create, and which hand-maintained registries to add a key to.
Adding a tool touches around 45 files. Roughly a dozen of those are a key in a registry — a dict or list somewhere that enumerates the tools — and those are the ones that get missed, because nothing fails: the tool works perfectly in its own panel while silently vanishing from Igor export, the batch pipeline, setup restore or the agent surface.
Start with the test, not the checklist#
pyirena/tests/test_tool_registration.py holds the canonical tool table: one
row per tool, one column per surface, either the key that registers it there or
a written reason why it is deliberately absent.
Add your row first. Then run
pytest pyirena/tests/test_tool_registration.py
and work the failures. That converts the list below from something you have to
remember into something the build tells you. A gap you decided on is one line
in ABSENT_REASON; a gap you forgot is a red test. (When this test was
written it immediately found two live gaps, so this is not a theoretical
benefit.)
The registries#
Each row is a hand-maintained enumeration. “If you miss it” is the symptom the user reports six months later.
# |
Registry |
File |
If you miss it |
|---|---|---|---|
1 |
|
|
Generic consumers (HDF5 viewer scalar browser, merge tool, Igor export) cannot see the results. This is the source of truth for “what is a tool” — everything else is checked against it. |
2 |
|
|
Derived from (1) — nothing to do. Stale results leak into files seeded from a source file. |
3 |
|
|
Curves have no Igor wave name, so Igor-experiment export skips the tool. |
4 |
|
|
“Load Setup from File…” cannot restore the panel. |
5 |
|
|
|
6 |
defaults block |
|
Panel settings do not survive a restart. |
7 |
|
|
Results never appear in the Markdown report, the Data Selector report, or |
8 |
|
|
Invisible to scripting, MCP and the AI advisor. Readers are top-level MCP tools, not dispatched — without the wrapper an agent can fit and save the tool but not read a saved file. Bump the locked counts in |
9 |
control module + |
|
Agents can read the tool’s results but cannot drive it. Only needed if the tool is agent-drivable. |
10 |
|
|
The control functions exist but no MCP client can reach them. The dispatcher picks up individual functions automatically — the category is what you register. |
11 |
detection + |
|
Results invisible to the Data Explorer. |
12 |
collect-item lists |
|
Hardcoded — no trend plots (parameter vs file/temperature/time). |
13 |
wave names + writer |
|
Igor export writes nothing for the tool. |
14 |
checkbox, launcher, window, worker, results window, config dialog |
|
The tool cannot be opened from the Data Browser — the app’s main entry point. This is the single biggest wiring job; grep |
15 |
window registration |
|
Window position is not remembered; |
The keys are not the same everywhere, and cannot be made so.
size_distribution in the HDF5 schema is sizes to the batch pipeline and
setup loader, simple_fit to the Data Explorer, and simple to the MCP
dispatcher. These are baked into saved files and config sections, so the
registration table carries the alias per surface rather than pretending there
is one spelling. Pick your tool’s keys once and write all of them into the
test table.
The files to create#
Bottom-up — core first, because every other layer depends on the shape of
to_dict().
pyirena/core/<tool>.py math + model object with to_dict/from_dict
pyirena/io/nxcansas_<tool>.py save/load entry/<tool>_results
pyirena/batch/<tool>.py headless fit_<tool>(data, config)
pyirena/gui/<tool>_panel.py thin Qt panel + graph window
pyirena/api/control/<tool>.py agent control surface (if agent-drivable)
docs/<tool>_gui.md user documentation + scripting example
pyirena/tests/test_<tool>.py math vs analytic ground truth
Then the registry keys above, then the master checklist for everything else.
Order of work#
core — math, model object,
to_dict/from_dict. Test against analytic ground truth before anything else exists. Add the round-trip totests/test_core_serialization.py.io — the HDF5 group, then registry (1) and (3). Add a round-trip to
tests/test_nxcansas_roundtrips.py.batch —
fit_<tool>from a config dict, then registry (5). The core’sfrom_dictshould make this nearly free.gui — the panel, obeying the whole standard UX contract above, then registries (4), (6), (14), (15).
api — reader, then (7) and (8). Control surface and (9)/(10) only if the tool is something an agent should drive; decide on merit, not for completeness.
Data Explorer — (11), (12), (13).
docs —
docs/<tool>_gui.md, a row inAGENTS.md§3, a line indocs/developer/module-map.md, aCHANGELOG.mdentry.
At each step the registration test tells you what is still unwired.
Decide what the tool is not#
Not every tool needs every surface, and a deliberate gap is cheaper than a
half-built one. The existing decisions, all recorded in ABSENT_REASON:
Fractals and SAXS Morph are visualization rather than analysis, so they have no
agent control surface and Fractals has no batch path; Data Merge and Data
Manipulation produce data files consumed by a later pipeline stage, so they
run from their own config rather than the shared analysis setup. Write your
reason into the table when you make the call — that is what stops it being
re-litigated.
Serialising a tool’s state#
The core model owns the state; everything else asks it for a dict. Two methods, whatever the tool:
def to_dict(self) -> dict: # settings, not results — no data arrays, no χ²
def from_dict(cls, d: dict): # a default for *every* field
from_dict supplying a default for every field is not politeness, it is the
backwards-compatibility guarantee: a file written before a field existed has to
open with that field at its default. pyirena/tests/test_core_serialization.py
tests both directions for every tool that has the pair, through
json.dumps/loads — which is where tuples silently become lists. Bounds are
tuples in the model and lists in the file; restore them, because the fitter
unpacks them.
For a dataclass-based tool, inherit _SerialisableDataclass
(core/modeling.py) rather than writing the two methods out: it walks
dataclasses.fields(), so a new field is serialised the moment it is declared.
Modeling’s six population types share one implementation this way, and
population_from_dict dispatches on pop_type through the
POPULATION_CLASSES registry — an unknown type from a newer pyIrena loads as a
size distribution with a warning instead of raising.
The panel’s key names are not the model’s. RgCutoff, correlated,
estimate_B, Rg_low are baked into saved setups and batch config files and
cannot be renamed. Translate in exactly one place —
UnifiedLevel.from_panel_params() / .to_panel_params() are the pattern, one
pair, both directions — and never a second time in a panel or a batch module.
The four rules that keep this from diverging again#
This area has produced the same class of bug three times: a setting that
exists in one place and is quietly dropped in another, which returns a
plausible wrong number rather than an error. Sharing the builder
(core/tool_config.py) was necessary and was not sufficient. What closes it:
Write one dialect, read every dialect. Everything writes
model.to_dict(). Older shapes are read forever and written never. If you find yourself writing a second shape, you are adding the next bug.One reader for the wrapper, one for the model.
unwrap_config()takes all four envelopes pyIrena writes;build_setup()turns a section into a configured model. A panel, a batch module or an api function that parses a config itself is a bug in waiting — that is precisely how the batch path and the GUI drifted apart the first time.Detect by shape in one place, and state the version going forward.
is_panel_level()is the only shape test; new state carriesschema_versionso the next reader is told rather than left to sniff. Sniffing is what produced the worst of these bugs.A new setting is not done until the contract test covers it.
pyirena/tests/test_config_contract.pyloops over all six tools and requires every scalar setting to be serialised or named in the class’s_NOT_SERIALISEDallowlist with a reason; that a rebuilt model can run a fit; and that every fit flag inverted and every bound narrowed survivesto_dict() → build_setup() → to_dict()in all four envelopes. Add the field, run the test, work the failure. A values-only comparison passes every bug this area has ever had.
Do not confuse the three things called “config”. Settings
(model.to_dict()) are what this section is about. Results — the
entry/<tool>_results datasets and the load_*_results() dicts that mirror
them — are a separate, frozen vocabulary: RgCutoff and correlated are
the saved-file names there, and renaming one breaks every existing file, the
Igor export and the Data Explorer. Reports (get_<tool>_config()) are rows
for an agent to read and are deliberately not replayable.
Qt imports#
One import point, no exceptions:
from pyirena.gui._qt import QLabel, QTimer, Qt, Signal
If a class is missing, add it to pyirena/gui/_qt.py (and to its __all__) —
do not write a local try: from PySide6 … except ImportError: … block, not
even inside a function, and not in a test. Those blocks are how the project
ended up with three shims and a stale PyQt5 branch that would have failed on a
PyQt6-only install. pyirena/tests/test_gui_qt_contract.py enforces this: it
fails on any direct binding import in the package, on a second _qt.py
appearing in a subpackage, on a name imported from _qt.py that it does not
define, and on a name defined there but missing from __all__.
Lazy imports are still fine when you want them — from pyirena.gui._qt import QMenu inside a method is one line and costs nothing after the first call.
The standard UX contract#
Users come from Igor Pro, Origin, Excel and Matlab. Features they consider part of “a table” or “a graph” are not optional extras, and when they live in nobody’s spec they get skipped — twice now (the filename filter, then table clipboard copy). Shared helpers exist so each line below is one function call.
Every table (QTableWidget)
from pyirena.gui.table_utils import attach_table_copy, enable_table_sorting
attach_table_copy(my_table, on_save_csv=self._save_csv) # Ctrl+C, Ctrl+Shift+C,
# right-click menu
enable_table_sorting(my_table) # only if row order carries no meaning
Build numeric cells with make_numeric_item() so columns sort numerically, and
fill the table inside with populating(my_table): so an active sort cannot
scramble half-built rows. Write CSV with rows_to_csv_text() /
save_rows_as_csv() rather than ",".join(...) — quoting and precision are
then identical everywhere. pyirena/tests/test_gui_table_contract.py fails
the build if a new module constructs a table without the helper.
Every plot (pg.PlotItem)
from pyirena.gui.plot_export import attach_plot_export
attach_plot_export(my_plot, self, 'my_tool_iq',
window=self.graphics_layout, # whole-window image
folder=self._export_folder) # dialogs open next to data
Plots built with make_sas_plot(..., parent_widget=...) get this for free.
The menu is clipboard copy, image (PNG/JPEG/SVG), whole-window image, curve
CSV and Igor ITX — never add your own image-export action, and never import
ImageExporter outside plot_export.py; save dialogs must default to
export_folder(), not Path.home(). pyirena/tests/test_gui_plot_contract.py
enforces all of this.
Two things the exporters cannot infer, so the panel must say them:
Name every curve (
name=...). Unnamed curves are exported only as a fallback, labelled from the Y axis, and a plot mixing named and unnamed curves exports the named ones only — an unnamed residual trace next to a named one would silently vanish.Call
tag_curve_uncertainty(item, dI)wherever you draw your own error bars. Bars are NaN-separated line segments that no exporter can read back, so without this thedYcolumn disappears from CSV and ITX while the bars stay visible on screen.plot_iq_dataalready does it for you.
Every push button — colour it by role, not by stylesheet:
from pyirena.gui.theme import set_button_role
set_button_role(self.fit_btn, "green", size="large")
set_button_role(self.prefit_btn, None, size="small") # neutral helper chip
set_button_role(self.load_setup_btn, "soft-amber")
Buttons are drawn raised (gradient face, two-tone bevel, sunken when pressed)
by pyirena/gui/raised_buttons.py, whose rules apply_theme installs once
for the whole application. set_button_role only sets a dynamic property, so
it is cheap and can be called again at runtime (Fit → Cancel). Pick the role by
what the button does, so the same action has the same colour in every tool —
the table is in planning/raised-buttons/PLAN.md. Do not give a button its own
setStyleSheet with a background: each one is parsed and polished separately,
and it brings the flat look back. Never write a broad
QWidget { background: … } rule either; it flattens every button inside.
raised_buttons.py is vendored into Jan’s other Qt packages — edit it here,
bump __version__, and run scripts/sync_raised_buttons.py.
Every other colour you write into a stylesheet — name the text colour too:
from pyirena.gui.theme import readonly_field_css, status_css
self.readout.setStyleSheet(readonly_field_css())
self.banner.setStyleSheet(status_css("warning"))
Qt overrides only the properties a stylesheet actually names, so
setStyleSheet('background-color: #ecf0f1') leaves the text colour to the
platform palette. On a desktop set to a dark scheme that palette is near-white
— which is how the Modeling panel’s “Fit B/P btwn cursors” and “Fit Flat btwn
cursors” buttons shipped as invisible white boxes. pyirena.gui.theme exists
so the pairing is not something you have to remember: every helper emits a
background and a foreground, and the tokens (ACCENT_*, CHIP_*,
READONLY_*, OK_BG/OK_TEXT, …) are semantic, so a future dark variant is a
change to one table rather than to 300 call sites.
apply_theme(app) — called from every GUI entry point right after
QApplication(...) — installs the Fusion style, an explicit palette and a
baseline stylesheet, so pyIrena looks the same on every platform regardless of
the system setting, and so unstyled controls (combo boxes, spin boxes, list
views) cannot be rendered unreadably either. Never call app.setStyle('Fusion')
directly; PYIRENA_NATIVE_THEME=1 is the escape hatch for debugging.
pyirena/tests/test_gui_theme_contract.py fails the build on a new inline
style that sets a background without a colour.
Every cursor-driven Q range — one shared, editable control:
from pyirena.gui.q_range_ui import QRangeFields
self.q_range_fields = QRangeFields(
get_range=lambda: self.graph_window.get_cursor_range(),
set_range=lambda lo, hi: self.graph_window.set_cursor_range(lo, hi),
get_data_range=self._data_q_range, # for clamping typed values
)
self.q_range_fields.message.connect(self._set_status)
self.graph_window.cursor_moved.connect(self.q_range_fields.refresh)
Users come from Irena, where the fit limits could be typed as well as dragged.
QRangeFields gives Simple Fits, Modeling and Unified Fit an identical
editable Q min / Q max: it validates the pair (positive, distinct), swaps
values entered the wrong way round, clamps to the loaded data, then moves the
cursors — which stay the single source of truth, so the fields are always
re-read from them afterwards. Do not build another pair of read-only Q labels;
if your graph lacks set_cursor_range, add it (see
UnifiedFitGraphWindow.set_cursor_range for the re-entrancy guard the two
cursor lines need).
Every file list
from pyirena.core.file_sorting import SORT_LABELS, SORT_TOOLTIP, sort_names
from pyirena.core.file_types import FILE_TYPES, files_in_folder
from pyirena.gui.file_filter import FILTER_PLACEHOLDER, FILTER_TOOLTIP
self.type_combo.addItems(FILE_TYPES)
self.sort_combo.addItems(SORT_LABELS) # never a local label list
self.sort_combo.setToolTip(SORT_TOOLTIP)
...
files = files_in_folder(self.current_folder, self.type_combo.currentText())
files = sort_names(files, self.sort_combo.currentIndex())
plus the shared filter box and a remembered last folder via StateManager.
Never re-implement a _sort_key_* function, never hard-code an extension list,
and never write your own os.listdir loop — files_in_folder() (or
files_with_extensions(), when your dropdown is not FILE_TYPES) already
decides how sub-directories, letter case and an unreadable folder are handled,
and the four browsers disagreed about all three before it was shared.
pyirena/tests/test_gui_browser_contract.py fails the build on each of these.
There is deliberately no shared FileBrowserWidget. The logic is shared —
filtering, sorting, the file-type table, listing, drag-and-drop — while the
widget assembly is not, because the four browsers differ in ways a common
widget would have to be configured around anyway: the Data Explorer is a tree
with lazy sub-folder expansion, Data Merge shows two linked instances, Data
Manipulation adds a context menu, and the Data Selector alone offers text files
and the convert-on-load path. Duplicated assembly with no logic in it does not
drift; duplicated logic does.
A file list should also accept dropped files, in one call:
from pyirena.gui.file_drop import drop_hint, enable_file_drop, first_folder
enable_file_drop(self, self.open_dropped_paths) # whole panel, not just the list
self.file_list.setToolTip(drop_hint("a folder or data files"))
enable_file_drop installs an event filter (no subclassing), rejects the drag
while it is still over the window if nothing dropped is openable, expands a
dropped folder one level, and hands you absolute paths already de-duplicated
and sorted. Your handler switches folder with first_folder(paths) and selects
the rest; a single-dataset target uses paths[0]. Never parse the mime data
yourself — Finder sends URLs, some apps send text, and paths_from_mime()
already reads both.
Every top-level window — remember where it was:
from pyirena.gui.window_state import install_window_state
install_window_state(self, 'my_tool', splitters={'main': self.main_splitter})
and one line at the launch site, so the tool answers Shift-click:
reset_window_if_shift(self.my_tool_window) # before .show()
self.my_tool_window.show()
One call restores size, position and splitter sizes, and saves them on close.
Do not call resize()/move() from saved numbers yourself: window_state
owns the policy for a screen layout that has changed since the geometry was
saved (see resolve_geometry), and that is the part that goes wrong. Geometry
lives in its own window_geometry.json, not in the tool’s StateManager
section — panels each hold their own StateManager and a save() rewrites the
whole file, so geometry written there is clobbered by the next panel to close.
Register only real windows. A widget that ends up inside a splitter is a
pane, and setting a pane’s geometry fights the layout (the symptom is the
neighbouring pane changing width); window_state checks isWindow() on
every apply, so a mistake is a no-op rather than a puzzle. Pane widths are
recorded and restored at the first layout, because a splitter has no width in
the constructor — do not expect install_window_state to have touched the
splitter by the time it returns.
Panels are constructed once and reused, so the reset gesture cannot live in
the constructor: install_window_state records the window’s coded default
before applying anything saved, and reset_window() restores that. Adding a
tool without the launch-site line is caught by
pyirena/tests/test_window_state.py, which reads the Data Selector’s
launchers. Set GEOMETRY_PERSISTENCE_ENABLED = False in that module to switch
the whole feature off.
Every fit panel — results reachable as text, not only as HDF5:
from pyirena.gui.report_buttons import make_report_buttons
layout.addLayout(make_report_buttons(
self, self.results_for_report, tool_key='my_tool_results',
default_stem='my_tool', status_setter=lambda m: self.status_label.setText(m),
image_widget_provider=lambda: self.graph_window.graphics_layout,
))
image_widget_provider enables Ctrl/⌘-click → report plus embedded graph
figure; return the GraphicsLayoutWidget (the plots alone), not the window,
or the figure will contain tab bars and status lines.
results_for_report() returns the dict shape
pyirena.io.load_<tool>_results() produces — the saved key names, not the
fit object’s internal ones — and pyirena.core.reporting renders it. Add the
tool’s section there rather than formatting text in the panel, so the panel,
the Data Selector report and the MCP export_fit_report cannot drift apart.
Every fit panel — fits and Monte-Carlo run off the GUI thread:
from pyirena.gui.fit_worker import FitJobRunner
self._jobs = FitJobRunner(self, lock=[self.fit_btn, self.mc_btn, ...])
def job(progress_cb, cancel_cb): # worker thread: no widgets here
return model_copy.calculate_uncertainty_mc(q, I, dI, n_runs=n,
progress_cb=progress_cb,
cancel_cb=cancel_cb)
self._jobs.start(job, on_done=self._finish_mc, on_progress=self._show_progress,
button=self.mc_btn, cancellable=True, busy_text='Cancel MC')
Collect every input from widgets before start(), give the job copies of
anything the GUI may redraw from meanwhile, and apply the result in on_done
(GUI thread). A second click on a cancellable button calls
self._jobs.cancel(); closeEvent calls self._jobs.shutdown(); tests call
self._jobs.wait(). Never pump the event loop with
QApplication.processEvents() from inside a long computation — it lets a
second click start a second fit inside the first.
pyirena/tests/test_gui_fit_worker_contract.py enforces both rules.
Every panel — state methods use one naming convention, so a helper can call them and a reader of one panel knows the next:
Method |
Visibility |
Does |
|---|---|---|
|
public |
collect + write to |
|
public |
read from |
|
private |
return the state dict |
|
private |
apply a state dict to the widgets |
The private halves are optional for small panels, the public pair is not —
pyirena/tests/test_gui_state_contract.py fails the build if a panel is
missing it or brings back one of the retired names (_save_state,
_load_state, _restore_state, _get_current_state). An embedded
component whose parent owns the persistence lifecycle (the Diffraction Lines
tab inside WAXS) instead exposes collect_state() / apply_state() publicly,
because the parent is the caller.
UnifiedFitPanel additionally keeps get_current_state() / apply_state() as
public aliases: those names are quoted in pyirena/io/setup_config.py and the
api/control layer as the shape of the embedded _pyirena_config setup state,
and agent scripts call them.
Also on every panel: params-to-JSON, and tooltips on buttons.
Params-to-JSON — one deliberate split. Analysis tools append their section
to a shared pyirena_config.json that drives the batch pipeline. Data Merge
and Data Manipulation write their own config file instead, and that is
intentional, not an oversight to be tidied away: both produce new data files
that a later pipeline stage consumes, so their config has to live next to that
stage, in a different folder and a different pipeline step from the analysis
config. Unifying them would break instrument data-reduction pipelines.
Fractals is a visualization tool with no batch use case and no JSON by
design. Scattering Contrast persists compounds through its own HDF5 library
(io/contrast_io.py).
Every result — reachable via HDF5 and pyirena.api.
Wiring points in detail#
4.1 Core#
Simple Fits models live in
pyirena/core/simple_fits.py. Add a private formula function_my_model(q, ParamA, ParamB) -> np.ndarrayand oneMODEL_REGISTRYentry:'My Model': { 'params': [('ParamA', 1.0, 1e-30, None), # (name, default, lo, hi) ('ParamB', 50.0, 0.1, 10_000.0)], 'formula': _my_model, 'linearization': None, # or 'guinier' / 'porod' family key 'complex_bg': True, # allow B·Q⁻ᴾ + flat to be added # 'calculation': True, # only for no-fit methods (see case study) }
The registry automatically propagates the model into: the GUI model combo, parameter grid, state persistence, JSON configs, HDF5 params/, batch fitting, MC uncertainty, the report and tabulation tools, and the existing parametrised tests in
tests/test_simple_fits_models.py(add your model to the IDENTIFIABLE / DEGENERATE / CALCULATION list there).Derived quantities (computed from fitted params) go in
SimpleFitModel._compute_derived(); they are saved underderived/and surface everywhere automatically.Boolean/enum options are attributes on the model object (like
use_complex_bg,invariant_porod_tail). They MUST be added toto_dict()andfrom_dict()with a default that keeps old JSON/HDF5 files loading.
4.2 GUI panel#
gui/simple_fits_panel.py (other tools are analogous):
Add widgets in
_create_control_panel(); wrap option groups that only apply to one model in a containerQWidgetand toggle its visibility from a_update_<feature>_ui()helper called in both_on_model_changed()andload_state()(the two entry points into a model switch).Every new control needs three touch points: a change-handler syncing widget → model attribute, a line in
_collect_state(), and a restore (withblockSignals(True)) inload_state()._collect_state()is used for StateManager persistence, JSON export, and the setup embedded in HDF5 — one dict, three consumers.Clear your plot items in
clear_fit()/model change so nothing stale survives a model switch.
4.3 HDF5 (NXcanSAS)#
io/nxcansas_simple_fits.py layout under entry/simple_fit_results:
group attrs (model, success, options…), scalar datasets (chi_squared),
params/, params_std/, derived/ sub-groups (one scalar dataset each),
array datasets with a units attr, fit_quality/, and the embedded
_pyirena_config setup JSON. New scalars: prefer params/ (inputs) and
derived/ (results) — generic consumers pick those up. New arrays: add the
dataset with units in the saver, read it back in the loader, and document it
in the module docstring.
io/schema.py is the generic description used by the HDF5 viewer and the
merge tool — add _scalar() entries (path, units, label) and _plot()
entries for new arrays.
4.4 Batch / JSON#
batch/simple.py::fit_simple() builds the model with
SimpleFitModel.from_dict(config) — so a correctly serialised feature is
scriptable with zero batch changes. Watch for: GUI-state-only keys that
from_dict does not understand are stripped in fit_simple_from_config()
(q_min/q_max, param_fixed, no_limits, schema_version) — if you add
a state key that is not a model attribute, strip it there too.
4.5 MCP / AI API#
api/results.py readers return params/params_std/derived dicts
verbatim — new values flow through to pyirena_read_simple_fit MCP tool
automatically. api/aggregate.py::_fallback_lookup() resolves parameter
names against params/<name>, derived/<name>, top-level datasets and
attrs, so pyirena_tabulate_parameter / pyirena_plot_parameter_trend reach
new values without schema entries (schema entries still add units/labels).
4.6 Data Selector#
report.py: iteratesparamsandderivedgenerically — verify only.results_windows.py: plotsQ/I_modelfrom the stored group. If your feature has no meaningful model curve, add a special case (see the Invariant block there).Tabulate/CSV: generic over params/derived.
4.7 HDF5 Data Explorer + Igor export#
gui/hdf5viewer/plot_controls.py: the per-tool “collect item” dropdown lists are hardcoded — add your new parameter/derived names.gui/hdf5viewer/pyirena_readers.py::_collect_simple_fit(): readsparams/then falls back toderived/.Igor export (
io/h5xp_extractor.py+io/igor_names.py): every exported curve needs a Y-wave name and an X-wave pairing. Simple Fits model waves followSimFit<Name>I→SimFit<Name>Q(a regex handles pyirena-only names ending inI; other names need aRESULT_X_WAVEentry). Scalars go into the Igor wave note via theparamsdict passed towrite_result_wave()— includederived/values there.
Case study: the Invariant#
The Invariant (added July 2026) is the template for a calculation method
— a registry entry with 'calculation': True:
core/simple_fits.py: standalonecalculate_invariant()(pure function, analytic units test against 2π²Δρ²φ(1−φ)), a placeholder formula returning zeros (so “Graph model” shows just the background), andSimpleFitModel.fit()routing to_run_calculation()which returns a fit-shaped result dict:chi2/dof/residualsareNone, results inderived, running-integral arrays inextra_arrays(saved as datasets by the io layer), plus awarningstring (saved as a group attr).Because the result dict keeps the standard shape, the HDF5 saver, batch, report, tabulation, MCP and Igor export all worked with only additive changes (extra arrays, derived fallback, dropdown items, wave names).
GUI:
_update_invariant_ui()relabels the Fit button, hides the “Fit?” column, disables MC uncertainty, and shows the Porod-tail checkbox; the running integral is drawn on a second ViewBox tied to the right axis (log data must be log10-ed manually there — pyqtgraph does not transform items in a bare ViewBox).Guards added for calculation models: skip fit-quality metrics in the saver (
chi2 is None), skip MC uncertainty in batch.Prefit replay (a lesson learned mid-feature): the invariant depends critically on background subtraction, and exported B/P/flat values rarely transfer between files. The fix was to make the GUI’s “Fit … btwn cursors” buttons record their Q windows into a
bg_prefitdict on the model (serialised like everything else), plus aprefit_background(q, I)method that both the GUI Calculate andbatch.fit_simple()invoke with the FULL data before calculating. General principle: if a scripted run cannot reproduce an interactive step, persist the step’s inputs (ranges), not just its outputs (values).
Conventions#
Parameter names are case-sensitive everywhere; pick once, never rename (they are HDF5 dataset names and JSON keys).
Units: Q in Å⁻¹, I in cm⁻¹ (absolute), contrast Δρ² in 10²⁰ cm⁻⁴, invariant in cm⁻⁴. Always set the
unitsattr on new datasets.Igor parity: when porting from
IR3_*.ipf, replicate the algorithm (cite file + function in the docstring), then add improvements as off-by-default options so numbers match Igor unless the user opts in.No Qt imports in
core/orio/. Noprint()— uselogging.Old-file compatibility: every new dict key and HDF5 attr needs a default on read.
Testing requirements#
Minimum for a new model/method (see tests/test_invariant.py as template):
Analytic ground truth — synthesise data where the answer is known in closed form and assert recovery within tolerance.
Registry/serialisation —
to_dict/from_dictround-trip including new attributes; add the model to the categorised lists intest_simple_fits_models.py.HDF5 round-trip — save → load → compare scalars and arrays.
Batch config path —
fit_simple(file, config_dict)end-to-end on a temp NXcanSAS file, including re-loading the stored result.Run the whole suite:
python -m pytest pyirena/tests— registry- parametrised tests in other files will pick up your model.