Instr._repr_html_ Test Notebook¶
This notebook builds an example instrument that exercises:
DECLARE/INITIALIZE/SAVE/FINALLYC code blocks (collapsible via<details>)- Instrument parameters
- Multiple component instances in the
TRACEsection (collapsible list) - A
metadatagroup - An
InMemoryRegistry(custom in-memory component definitions) - A
RemoteRegistryreference (displayed with clickable URL)
The last cell puts the Instr object on its own line so Jupyter calls _repr_html_ automatically.
In [1]:
Copied!
from mccode_antlr.assembler import Assembler
from mccode_antlr.reader.registry import InMemoryRegistry
from mccode_antlr import Flavor
from mccode_antlr.assembler import Assembler
from mccode_antlr.reader.registry import InMemoryRegistry
from mccode_antlr import Flavor
In [2]:
Copied!
# A minimal in-memory component so we don't need an external registry
simple_detector = """
DEFINE COMPONENT SimpleDetector
DEFINITION PARAMETERS ()
SETTING PARAMETERS (xwidth=0.1, yheight=0.1, string filename="det.dat")
OUTPUT PARAMETERS ()
SHARE
%{
%}
DECLARE
%{
double counts;
%}
INITIALIZE
%{
counts = 0;
%}
TRACE
%{
SCATTER;
counts += 1;
%}
SAVE
%{
DETECTOR_OUT_2D("SimpleDetector", "X", "Y", -xwidth/2, xwidth/2,
-yheight/2, yheight/2, 100, 100, NULL, filename);
%}
FINALLY
%{
%}
END
"""
mem_registry = InMemoryRegistry('custom', SimpleDetector=simple_detector)
# A minimal in-memory component so we don't need an external registry
simple_detector = """
DEFINE COMPONENT SimpleDetector
DEFINITION PARAMETERS ()
SETTING PARAMETERS (xwidth=0.1, yheight=0.1, string filename="det.dat")
OUTPUT PARAMETERS ()
SHARE
%{
%}
DECLARE
%{
double counts;
%}
INITIALIZE
%{
counts = 0;
%}
TRACE
%{
SCATTER;
counts += 1;
%}
SAVE
%{
DETECTOR_OUT_2D("SimpleDetector", "X", "Y", -xwidth/2, xwidth/2,
-yheight/2, yheight/2, 100, 100, NULL, filename);
%}
FINALLY
%{
%}
END
"""
mem_registry = InMemoryRegistry('custom', SimpleDetector=simple_detector)
In [3]:
Copied!
a = Assembler('example_instrument', flavor=Flavor.MCSTAS)
# Give the assembler access to our custom in-memory component
a.reader.registries.append(mem_registry)
# Instrument parameters
a.parameter('double E0 = 10')
a.parameter('double dE = 1')
a.parameter('double dist = 10')
# DECLARE block
a.declare('double v0; /* neutron speed at source */')
# INITIALIZE block
a.initialize('v0 = SE2V * sqrt(E0); /* SE2V converts meV to m/s */')
# SAVE block
a.save('fprintf(stdout, "Run complete. E0=%g meV\\n", E0);')
# Metadata group
a.metadata('instrument_info', 'text/plain',
'Example instrument for testing mccode-antlr HTML output.\nBuilt programmatically with Assembler.')
# TRACE: component instances
origin = a.component('origin', 'Progress_bar', at=[0, 0, 0])
source = a.component(
'source', 'Source_simple',
at=([0, 0, 0], origin),
parameters={
'radius': 0.05,
'yheight': 0,
'xwidth': 0,
'dist': 'dist',
'focus_xw': 0.05,
'focus_yh': 0.05,
'E0': 'E0',
'dE': 'dE',
},
)
guide = a.component(
'guide', 'Guide_gravity',
at=([0, 0, 2], source),
parameters={
'w1': 0.05,
'h1': 0.05,
'w2': 0.03,
'h2': 0.03,
'l': 10,
'm': 3.5,
},
)
slit = a.component(
'slit', 'Slit',
at=([0, 0, 0.1], guide),
parameters={'xwidth': 0.02, 'yheight': 0.02},
)
sample = a.component(
'sample', 'V_sample',
at=([0, 0, 1], slit),
parameters={
'radius': 0.005,
'yheight': 0.05,
'focus_r': 0.3,
'target_index': 1,
},
)
# Use our custom in-memory component
detector = a.component(
'detector', 'SimpleDetector',
at=([0, 0, 0.5], sample),
parameters={'xwidth': 0.3, 'yheight': 0.3, 'filename': '"psd.dat"'},
)
instr = a.instrument
a = Assembler('example_instrument', flavor=Flavor.MCSTAS)
# Give the assembler access to our custom in-memory component
a.reader.registries.append(mem_registry)
# Instrument parameters
a.parameter('double E0 = 10')
a.parameter('double dE = 1')
a.parameter('double dist = 10')
# DECLARE block
a.declare('double v0; /* neutron speed at source */')
# INITIALIZE block
a.initialize('v0 = SE2V * sqrt(E0); /* SE2V converts meV to m/s */')
# SAVE block
a.save('fprintf(stdout, "Run complete. E0=%g meV\\n", E0);')
# Metadata group
a.metadata('instrument_info', 'text/plain',
'Example instrument for testing mccode-antlr HTML output.\nBuilt programmatically with Assembler.')
# TRACE: component instances
origin = a.component('origin', 'Progress_bar', at=[0, 0, 0])
source = a.component(
'source', 'Source_simple',
at=([0, 0, 0], origin),
parameters={
'radius': 0.05,
'yheight': 0,
'xwidth': 0,
'dist': 'dist',
'focus_xw': 0.05,
'focus_yh': 0.05,
'E0': 'E0',
'dE': 'dE',
},
)
guide = a.component(
'guide', 'Guide_gravity',
at=([0, 0, 2], source),
parameters={
'w1': 0.05,
'h1': 0.05,
'w2': 0.03,
'h2': 0.03,
'l': 10,
'm': 3.5,
},
)
slit = a.component(
'slit', 'Slit',
at=([0, 0, 0.1], guide),
parameters={'xwidth': 0.02, 'yheight': 0.02},
)
sample = a.component(
'sample', 'V_sample',
at=([0, 0, 1], slit),
parameters={
'radius': 0.005,
'yheight': 0.05,
'focus_r': 0.3,
'target_index': 1,
},
)
# Use our custom in-memory component
detector = a.component(
'detector', 'SimpleDetector',
at=([0, 0, 0.5], sample),
parameters={'xwidth': 0.3, 'yheight': 0.3, 'filename': '"psd.dat"'},
)
instr = a.instrument
2026-03-05 15:11:11.844 | DEBUG | mccode_antlr.reader.reader:add_component:243 - Component cache hit: /home/g/.cache/mccodeantlr/mcstas/v3.5.31/mcstas-comps/misc/Progress_bar.comp 2026-03-05 15:11:11.858 | DEBUG | mccode_antlr.reader.reader:add_component:243 - Component cache hit: /home/g/.cache/mccodeantlr/mcstas/v3.5.31/mcstas-comps/sources/Source_simple.comp 2026-03-05 15:11:11.864 | DEBUG | mccode_antlr.reader.reader:add_component:243 - Component cache hit: /home/g/.cache/mccodeantlr/mcstas/v3.5.31/mcstas-comps/optics/Guide_gravity.comp 2026-03-05 15:11:11.869 | DEBUG | mccode_antlr.reader.reader:add_component:243 - Component cache hit: /home/g/.cache/mccodeantlr/mcstas/v3.5.31/mcstas-comps/optics/Slit.comp 2026-03-05 15:11:11.874 | DEBUG | mccode_antlr.reader.reader:add_component:243 - Component cache hit: /home/g/.cache/mccodeantlr/mcstas/v3.5.31/mcstas-comps/obsolete/V_sample.comp
Display the instrument¶
Placing instr on the last line of a code cell triggers Jupyter's rich display — it calls _repr_html_() automatically.
Expect to see:
DECLARE,INITIALIZE,SAVEcode blocks collapsed under<details>(click to expand)TRACEsection as a collapsible open list of components- Registry headers with clickable links (for remote registries)
InMemoryRegistry: custom (1 components)label for the in-memory registry
In [4]:
Copied!
instr
instr
Out[4]:
Instrument example_instrument
Instrument: example_instrumentSource: interactive
Contains:
Registries: libc mcstas custom
Registry: libc https://github.com/mccode-dev/McCode v3.5.31 libc-registry.txt https://github.com/mccode-dev/mccode-pooch-registries
Registry: mcstas https://github.com/mccode-dev/McCode v3.5.31 mcstas-registry.txt https://github.com/mccode-dev/mccode-pooch-registries
InMemoryRegistry: custom (1 components)
…
E0=10, dE=1, dist=10METADATA
text/plain instrument_info…
Example instrument for testing mccode-antlr HTML output. Built programmatically with Assembler.
DECLARE
double v0; /* neutron speed at source */
INITIALIZE
v0 = SE2V * sqrt(E0); /* SE2V converts meV to m/s */
TRACE
-
COMPONENT origin = Progress_bar() AT (0, 0, 0) ABSOLUTE
-
COMPONENT source = Source_simple() AT (0, 0, 0) RELATIVE origin ROTATED (0.0, 0.0, 0.0) RELATIVE origin
…
radius=0.05, yheight=0, xwidth=0, dist=dist, focus_xw=0.05, focus_yh=0.05, E0=E0, dE=dE -
COMPONENT guide = Guide_gravity() AT (0, 0, 2) RELATIVE source ROTATED (0.0, 0.0, 0.0) RELATIVE source
…
w1=0.05, h1=0.05, w2=0.03, h2=0.03, l=10, m=3.5 -
COMPONENT slit = Slit() AT (0, 0, 0.1) RELATIVE guide ROTATED (0.0, 0.0, 0.0) RELATIVE guide
…
xwidth=0.02, yheight=0.02 -
COMPONENT sample = V_sample() AT (0, 0, 1) RELATIVE slit ROTATED (0.0, 0.0, 0.0) RELATIVE slit
…
radius=0.005, yheight=0.05, focus_r=0.3, target_index=1 -
COMPONENT detector = SimpleDetector() AT (0, 0, 0.5) RELATIVE sample ROTATED (0.0, 0.0, 0.0) RELATIVE sample
…
xwidth=0.3, yheight=0.3, filename="psd.dat"
SAVE
fprintf(stdout, "Run complete. E0=%g meV\n", E0);
In [ ]:
Copied!