Skip to main content

McStasScript

McStas API for creating and running McStas/McXtrace instruments from Python scripting.

Prototype for an API that allows interaction with McStas through an interface like Jupyter Notebooks, created under WP5 of PaNOSC.

Full documentation can be found here!

Installation

McStasScript can be installed from conda-forge or pip:

conda install -c conda-forge mcstasscript
pip install McStasScript --upgrade

When McStas is installed via conda-forge, McStasScript is included automatically.

Configuration

In most cases, no configuration is needed. If the MCSTAS environment variable is set (e.g., after running eval $(mcstas)), McStasScript will automatically detect the McStas installation. If mcrun is available on the PATH, it will be used directly.

For manual configuration or troubleshooting, see the online documentation.

Instructions for basic use

This section provides a quick way to get started; a more in-depth tutorial using Jupyter Notebooks is available in the tutorial folder.

Import the package:

import mcstasscript as ms

Create a new instrument (use McXtrace_instr for McXtrace):

my_instrument = ms.McStas_instr("my_instrument")

Add components:

my_source = my_instrument.add_component("source", "Source_simple")
my_source.show_parameters()  # Show available parameters for Source_simple

Set parameters as attributes, in jupyter notebooks these are autocompleted if the object has been created:

my_source.set_parameters(xwidth=0.12, yheight=0.12,
                         lambda0=3, dlambda=2.2,
                         focus_xw=0.05, focus_yh=0.05)

Add a monitor (notice the use of single and double quotes to set a string literal in the file):

PSD = my_instrument.add_component("PSD", "PSD_monitor", AT=[0,0,1], RELATIVE="source")
PSD.set_parameters(xwidth=0.1, yheight=0.1, nx=5, ny=5, filename='"PSD.dat"')

Set simulation options and run:

my_instrument.settings(output_path="first_run", ncount=1E7)
data = my_instrument.backengine()

Access and manipulate results:

data[0].Intensity

Plot results:

ms.make_sub_plot(data)

Widgets in Jupyter Notebooks

Interactive widget interface for plotting:

import mcstasscript.jb_interface as ms_widget
ms_widget.show(data)

Interactive simulation widget (alternative to backengine):

ms_widget.show(instr)

Programmatic access to widget-generated data:

sim_widget = ms_widget.SimInterface(instr)
sim_widget.show_interface()
data = sim_widget.get_data()

Use existing instrument files

The McStas package now includes the ability to create McStasScript python files from instrument files.

mcstas-pygen my_isntrument.instr

This can also be done through mcgui using the Pylab button.

Method overview

Instrument (McStas_instr / McXtrace_instr)

McStas_instr(name)  # Returns instrument object
├── show_parameters()           # Print instrument parameters
├── show_settings()             # Print current run settings
├── show_variables()            # Print declared and user variables
├── show_components()           # Print components and their positions
├── show_instrument()           # Show instrument in mcdisplay
├── show_diagram()              # Show instrument layout diagram
├── set_parameters(**kwargs)    # Set instrument parameters (dict or kwargs, autocompletes)
├── available_components([category])  # Show available components
├── component_help(name)        # Show parameters for a component type
├── add_component(name, type, **kwargs)  # Add component, returns component object
├── copy_component(name, original, **kwargs)  # Copy a component
├── remove_component(name)       # Remove a component
├── move_component(name, before=None, after=None)  # Move component
├── get_component(name)          # Get component object by name
├── get_last_component()         # Get last added component
├── add_parameter(*args, **kwargs)  # Add instrument parameter
├── add_declare_var(type, name, **kwargs)  # Add declared variable
├── add_user_var(type, name, **kwargs)     # Add user variable
├── append_declare(string)       # Append raw C code to declare section
├── append_initialize(string)    # Append raw C code to initialize section
├── append_finally(string)       # Append raw C code to finally section
├── write_full_instrument()      # Write instrument file to disk
├── show_diagram()               # Show instrument layout diagram
├── settings(**kwargs)           # Set simulation options
├── backengine()                 # Run simulation, returns data
├── run_to(component, ...)       # Set simulation end point (saves MCPL dump)
├── run_from(component, ...)     # Set simulation start point (loads MCPL dump)
└── show_dumps()                 # Show available beam dumps

Component (returned by add_component)

Component parameters are set directly as attributes. Additional methods:

├── show_parameters()           # Show component parameters
├── set_parameters(**kwargs)    # Set parameters (dict or kwargs)
├── set_AT(list 3, RELATIVE)    # Set position
├── set_ROTATED(list 3, RELATIVE) # Set rotation
├── set_RELATIVE(name)          # Set position and rotation reference
├── set_WHEN(string)            # Set WHEN condition
├── append_EXTEND(string)       # Append C code to EXTEND section
├── set_GROUP(string)           # Set GROUP name
├── set_JUMP(string)            # Set JUMP target
├── set_SPLIT(value)            # Set SPLIT value
├── set_comment(string)         # Set component comment
├── set_c_code_before(string)   # Set C code before component
├── set_c_code_after(string)    # Set C code after component
└── print_long()                # Print full component info

Placement attributes (AT, ROTATED, RELATIVE, WHEN, EXTEND, GROUP, JUMP, SPLIT) can also be set via keyword arguments in add_component().

Package-level functions

ms.load_data(folder)            # Load simulation data from a McStas output folder
ms.load_metadata(folder)        # Load metadata (mccode.sim) from a data folder
ms.load_monitor(metadata, folder)  # Load single monitor data
ms.name_search(name, data_list) # Find dataset by component or filename
ms.name_plot_options(name, data_list, **kwargs)  # Set plot options for a dataset

Plotting

ms.make_plot(data_list)         # Plot each dataset in a separate figure
ms.make_sub_plot(data_list)     # Plot all datasets as subplots in one figure
ms.make_animation(data_list)    # Create an animation from a list of datasets

Diagnostics

ms.Diagnostics(instr)           # Beam and intensity diagnostics tool

Tools

ms.Cryostat()                   # Cryostat builder for Union-based instruments
ms.has_component(instr, ...)    # Check if instrument has a given component
ms.has_parameter(instr, ...)    # Check if instrument has a given parameter
ms.all_parameters_set(instr)    # Check if all parameters have values

Configuration

ms.Configurator()
├── set_mcrun_path(path)       # Set path to mcrun directory
├── set_mcstas_path(path)      # Set path to McStas resources
├── set_mxrun_path(path)       # Set path to mxrun directory
├── set_mcxtrace_path(path)    # Set path to McXtrace resources
└── set_line_length(length)    # Set maximum line length for output

Jupyter widgets

import mcstasscript.jb_interface as ms_widget
ms_widget.show(data_or_instr)   # Show plot or simulation widget
ms_widget.SimInterface(instr)   # Programmatic simulation widget
ms_widget.PlotInterface(data)   # Programmatic plotting widget

Release files for McStasScript 0.0.86

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for McStasScript 0.0.86
File Size Uploaded
mcstasscript-0.0.86.tar.gz 2.5 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for McStasScript 0.0.86
File Interpreter ABI Platform
mcstasscript-0.0.86-py3-none-any.whl Python 3 none any Details

Total release size: 5.0 MB

Release files / mcstasscript-0.0.86.tar.gz

Download URL mcstasscript-0.0.86.tar.gz
Size 2.5 MB
Tags Source
SHA-256 checksum
How to use checksums
e792dc7c30b73e354aee8576b5f9f7a270485a40b1bfcb343b6ba9c0dfe04ce3
BLAKE2b-256 checksum
How to use checksums
8233acbbb7fe8475d2f677e827f69ec385503ba867d02252604eb0ecc12e02ce
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 24, 2026.

Transparency log

Release files / mcstasscript-0.0.86-py3-none-any.whl

Download URL mcstasscript-0.0.86-py3-none-any.whl
Size 2.4 MB
Tags Python 3
SHA-256 checksum
How to use checksums
e0778b01f0801da695fc4c976a48ff7a8e12f3c6e15f7efa811595b35dafdd5f
BLAKE2b-256 checksum
How to use checksums
97adbfbe11a3b65da13577f4fb6cbbb3c2e5fb27a5b1a5f15de5deae0b8e42c5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 24, 2026.

Transparency log

Release history Release notifications | RSS feed

0.0.93

2 release files

0.0.91

2 release files

0.0.90

2 release files

0.0.89

2 release files

0.0.88

2 release files

This release

0.0.86 This release

2 release files

0.0.85

2 release files

0.0.84

2 release files

0.0.83

2 release files

0.0.82

2 release files

0.0.80

2 release files

0.0.79

2 release files

0.0.78

2 release files

0.0.75

1 release file

0.0.74

1 release file

0.0.73

1 release file

0.0.72

1 release file

0.0.71

1 release file

0.0.70

1 release file

0.0.69

1 release file

0.0.68

1 release file

0.0.67

1 release file

0.0.66

1 release file

0.0.65

1 release file

0.0.64

1 release file

0.0.63

1 release file

0.0.62

1 release file

0.0.60

1 release file

0.0.58

1 release file

0.0.55

1 release file

0.0.54

1 release file

0.0.53

1 release file

0.0.52

1 release file

0.0.51

1 release file

0.0.50

1 release file

0.0.49

1 release file

0.0.48

1 release file

0.0.46

1 release file

0.0.45

1 release file

0.0.44

1 release file

0.0.43

1 release file

0.0.42

1 release file

0.0.41

1 release file

0.0.40

1 release file

0.0.39

1 release file

0.0.38

1 release file

0.0.37

1 release file

0.0.36

1 release file

0.0.34

1 release file

0.0.33

1 release file

0.0.32

1 release file

0.0.31

1 release file

0.0.29

1 release file

0.0.28

1 release file

0.0.27

1 release file

0.0.26

1 release file

0.0.24

1 release file

0.0.23

1 release file

0.0.22

1 release file

0.0.21

1 release file

0.0.20

1 release file

0.0.19

1 release file

0.0.18

1 release file

0.0.17

1 release file

0.0.16

1 release file

0.0.15

1 release file

0.0.14

1 release file

0.0.13

1 release file

0.0.12

1 release file

0.0.11

1 release file

0.0.10

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page