The Command Line
Starting an experiment always involves the same questions: which settings file,
where the runs go, a new run or the continuation of a stopped one, a trial without
instruments. ExperimentCLI (from plesty.lib.experiment) answers them the same way
for every experiment, so each experiment only supplies what is its own: which devices
it uses, and how it is built.
A minimal command line
# power_scan.py
import sys
from plesty.lib.device.composite_device import CompositeDevice
from plesty.lib.experiment import ExperimentCLI, Manifest
cli = ExperimentCLI(Manifest(module="power_scan", title="Power scan"))
@cli.rig
def rig(args, config, skip):
return CompositeDevice(["pm"])
@cli.build
def build(args, config, run_root, devices):
return PowerScan(config, devices=devices, run_root=run_root)
if __name__ == "__main__":
sys.exit(cli.main())
PowerScan is the experiment class from
Write the scan. The two functions are
required:
@cli.rigbuilds the devices the experiment uses (PLESTY calls them its rig). It receives the parsed command-line arguments (args), the settings (config), andskip, the names of optional devices the operator chose to go without (see Checking the Devices Before a Run). It is called again each time the operator retries after a device failed to answer. ReturnNonefor an experiment that uses no devices.@cli.buildbuilds the experiment. It receivesargs,config, the folder to save runs in (run_root) and the devices;devicesisNonein a dry run, where nothing is connected.
Options
Option |
Does |
|---|---|
|
read the settings from |
|
save the run in |
|
continue a stopped run (see Stopping and Continuing a Run) |
|
name the new run |
|
build and print the plan, connect nothing |
|
print what the experiment declares, as JSON (see What the experiment declares) |
|
print the devices of |
A settings file that does not exist counts as empty settings, so an experiment whose defaults are all in its code runs without one.
When a setup.yaml exists (see The Setup File), the command line
reads it before anything connects. If a variable it names has no value, the run stops
there and says which variables to set in which file. Only the benches of the devices
the run uses count: see @cli.needs below.
Exit codes
Code |
Means |
|---|---|
|
the run completed, or the plan or manifest was printed |
|
a device did not answer and the operator chose to stop (see Checking the Devices Before a Run), or the run was aborted |
|
a variable named by |
|
the run was interrupted with |
Optional functions
Register these the same way, as decorators, when you need them:
Decorator |
Called |
Use it to |
|---|---|---|
|
with |
change the settings from command-line options; return the settings to use |
|
with |
refuse incomplete settings: raise an exception with the reason |
|
with |
choose another settings file from other options, such as a |
|
with |
print a summary of what was measured; return the text |
|
with the settings, before anything connects |
name the devices of |
@cli.needs
def needs(config):
# Measuring in place uses one bench; navigating to dots uses the camera bench too.
return ["hwp", "qwp", "spec", "pm"] if not config.get("dots") else None
cli.optional("pm", …) names devices the run can do without: if only these fail to
answer, the operator may continue without them.
Your own options
cli.add_argument adds an option, with the same arguments as
argparse.ArgumentParser.add_argument. sets= names the setting the option
replaces, so that programs reading the manifest know what it does:
cli.add_argument("--points", type=int, sets="points", help="Number of scan points.")
@cli.configure
def configure(args, config):
if args.points is not None:
config["points"] = args.points
return config
What the experiment declares
Manifest describes the experiment to programs that start it for you, such as a
console with a form for the settings. --manifest prints it without connecting
anything or reading any settings, so such a program can ask an experiment what it
takes before anything is set up.
Field |
Holds |
|---|---|
|
the name the experiment is started by, as in |
|
a human-readable name |
|
the settings the experiment takes, each with its type, unit and description, in the same form as a device’s parameters |
|
the environment variables it reads; |
|
the extra command-line options; filled in by |
|
the default settings file, |
|
the default folder for runs, |
|
the module of the experiment’s viewer, if it has one (see 3. Watch the data) |