niess.nexus¶
McCode to ESS NeXus Structure JSON. See Produce NeXus Structure JSON for the task-oriented guide and Write NeXus translators for extending it.
nexus
¶
McCode to ESS NeXus Structure JSON conversion.
Dispatches translators over the assembled mccode_antlr Instance tree, the
same handoff point :mod:niess.brep uses, and builds NeXus Structure JSON dicts
directly -- there is no intermediate NeXus object model.
from niess.nexus import to_nexus_structure
structure = to_nexus_structure(assembler.instrument, origin='sample_origin')
Instrument-specific translators are opt-in per conversion, through a registry that extends the default one:
from niess.nexus.bifrost import BIFROST_REGISTRY
structure = to_nexus_structure(instr, origin='sample_origin',
registry=BIFROST_REGISTRY)
Modules:
-
bifrost–BIFROST-specific NeXus translators.
-
cli–Command-line conversion of an instrument to NeXus Structure JSON.
-
expression–Decide whether a component parameter is a literal or a runtime link.
-
instrument–Walk an assembled instrument and emit its NeXus Structure JSON.
-
nodes–NeXus Structure JSON node constructors.
-
off–NXoff_geometry construction.
-
orientation–Turn mccode-antlr's orientation algebra into a NeXus transformation chain.
-
registry–Translator registry for the NeXus target.
-
streams–Filewriter stream and link module directives.
-
translators–Default per-component-type NeXus translators.
-
variables–Recover instrument-scope variables for constant folding.
Classes:
-
NexusContext–Instrument-level state shared by every translator.
-
Translation–Everything a translator needs about one component instance.
-
NXoff–Object File Format geometry: a vertex list and polygonal faces.
-
NiessNexusRegistry–Three-tier translator lookup: niess source type, niess role, McCode type.
Functions:
-
component_body–What a translator returns: the class and contents of one component's group.
-
to_nexus_structure–Convert an assembled instrument into ESS NeXus Structure JSON.
-
load_instr–Load an
Instrfrom a McCode.instrfile or a serialized instrument. -
node_name–The name of a group, dataset, or named stream node.
-
stream–A filewriter module directive --
ev44,da00,f144,link, ... -
resolve_stream–The stream group for a component, or
Noneif it publishes nothing.
NexusContext
dataclass
¶
NexusContext(instr: Any, nxlog_root: str = DEFAULT_NXLOG_ROOT, origin_name: str | None = None, registry: Any = None, declared: dict = dict(), orientations: dict = dict(), origin: Any = None, nodes: dict = dict(), suppressed: set = set(), graph: Any = None)
Instrument-level state shared by every translator.
Methods:
-
literal–Reduce a value -- or an iterable of them -- to plain JSON-able data.
-
resolve_target–The absolute path a relative placement should depend on.
-
frame_rotation–The turn a component's emitted frame carries that the object's does not.
-
frame_offset–The displacement a component's emitted origin carries that the object's does not.
literal
¶
Reduce a value -- or an iterable of them -- to plain JSON-able data.
Used for attribute values such as transformation vectors, which have no node of their own to carry a link and so must fold to constants.
Source code in src/niess/nexus/instrument.py
resolve_target
¶
resolve_target(rel) -> str | None
The absolute path a relative placement should depend on.
Source code in src/niess/nexus/instrument.py
frame_rotation
¶
The turn a component's emitted frame carries that the object's does not.
None when there is none, which is everything but a disc chopper today.
Source code in src/niess/nexus/instrument.py
frame_offset
¶
The displacement a component's emitted origin carries that the object's does not.
None when there is none, which is everything but a disc chopper today.
Source code in src/niess/nexus/instrument.py
Translation
dataclass
¶
Translation(context: 'NexusContext', instance: Any, index: int, provenance: NiessProvenance | None = None)
Everything a translator needs about one component instance.
Methods:
-
resolve–Resolve a named instance parameter to a literal or a link description.
-
parameter–The literal value of a parameter, or
defaultif it is not constant. -
parameter_node–A node for a parameter: dataset when constant, link group when not.
-
siblings_in_group–Instances sharing this one's
nexus_group_idprovenance tag, in order.
Attributes:
-
instr–The whole instrument, for translators that must inspect sibling instances.
resolve
¶
resolve(name: str, default=None)
Resolve a named instance parameter to a literal or a link description.
Source code in src/niess/nexus/instrument.py
parameter
¶
parameter(name: str, default=None, dtype=None)
The literal value of a parameter, or default if it is not constant.
Source code in src/niess/nexus/instrument.py
parameter_node
¶
A node for a parameter: dataset when constant, link group when not.
dtype coerces a constant value only -- a runtime-linked parameter has
no value here to coerce.
Source code in src/niess/nexus/instrument.py
siblings_in_group
¶
siblings_in_group() -> list
Instances sharing this one's nexus_group_id provenance tag, in order.
Source code in src/niess/nexus/instrument.py
NXoff
¶
Object File Format geometry: a vertex list and polygonal faces.
Methods:
-
from_wedge–A trapezoidal prism, origin at the centre of the entry face, +z downbeam.
Source code in src/niess/nexus/off.py
from_wedge
classmethod
¶
A trapezoidal prism, origin at the centre of the entry face, +z downbeam.
Source code in src/niess/nexus/off.py
NiessNexusRegistry
¶
Bases: NiessRegistry[NexusTranslator]
Three-tier translator lookup: niess source type, niess role, McCode type.
Source code in src/niess/dispatch.py
component_body
¶
component_body(nx_class: str, children: list | None = None, attrs: dict | None = None, name: str | None = None) -> dict
What a translator returns: the class and contents of one component's group.
name overrides the group's name, which defaults to the McStas instance's. Use
it where the instance name is an artefact of how the instrument was built rather
than something a reader of the file should see -- a composite emitted as several
instances, say, whose NeXus group should carry the name of the thing itself.
Source code in src/niess/nexus/instrument.py
to_nexus_structure
¶
to_nexus_structure(instr, origin: str | None = None, nxlog_root: str | None = None, absolute_depends_on: bool = False, registry=None, graph=None) -> dict
Convert an assembled instrument into ESS NeXus Structure JSON.
Parameters:
-
instr–The
mccode_antlrInstranAssemblerproduced. -
origin(str | None, default:None) –Name of the component to treat as the coordinate origin. Defaults to the instrument's sample-category component.
-
nxlog_root(str | None, default:None) –Where runtime parameter values are published, for link directives.
-
absolute_depends_on(bool, default:False) –Rewrite relative
depends_onvalues as absolute NeXus paths. -
registry–Translator registry; defaults to :data:
DEFAULT_NEXUS_REGISTRY, which holds only the generic per-component-type translators. Pass an instrument-specific registry --niess.nexus.bifrost.BIFROST_REGISTRY, say -- to add its translators to this conversion alone. -
graph–A networkx DiGraph representing the possible particle path(s) through the instrument. A standard linear path will be constructed for @inputs and @outputs group attributes if this is not provided.
Source code in src/niess/nexus/instrument.py
load_instr
¶
Load an Instr from a McCode .instr file or a serialized instrument.
Source code in src/niess/nexus/cli.py
stream
¶
A filewriter module directive -- ev44, da00, f144, link, ...
Source code in src/niess/nexus/nodes.py
resolve_stream
¶
The stream group for a component, or None if it publishes nothing.
The protocol is never chosen here. Some monitors belong on da00 histograms
and some on ev44 events; which one is a property of the instrument setup, so
it is read from the instrument in priority order:
- a
METADATA "nexus_structure_stream_data"block on the component -- the escape hatch for instruments authored outside niess, emitted verbatim; - a
nexus_streamentry in the component's niess provenanceextra, which is how a niess component records the choice made when the instrument was built; default-- the component type's established behaviour, used only when the instrument expressed no preference at all.
A component with no selection and no default gets no stream group rather than a guessed one.