Midas Python API
This section contains the full API reference for the midas Python package.
For a first example program, see Hello world! Writing programs in midas. To install the package, see Set up python.
Each feature also has a "programming" page with examples, such as ODB: programming.
The midas Python package is in $MIDASSYS/python. Most of it wraps the midas C++ library, so you get pythonic access to an experiment without having to deal with handles, buffer sizes or status codes: functions return normal Python values (or numpy arrays), and errors are raised as exceptions. The file reader is pure Python, and works without the rest of midas.
Modules
| Module | Contents |
|---|---|
midas |
Constants and helpers shared by the other modules, similar to the definitions in midas.h (status codes, TID_xxx types etc.) |
midas.client |
MidasClient, the main class for talking to a running experiment: ODB access, run transitions, event buffers, messages, alarms and more |
midas.frontend |
A framework for writing frontends, built on midas.client |
midas.event |
Classes for midas events and banks, used when sending or reading events |
midas.file_reader |
Reading midas data files, for data analysis |
midas.sequencer |
Running the Python sequencer (PySequencer), and writing scripts for it |
mscb |
Accessing MSCB devices. Not part of the midas package: it's in $MIDASSYS/mscb/python |
Most programs only need midas.client (or midas.frontend for a frontend).
Creating a client
Always create your MidasClient (or your frontend object) with a with statement:
import midas.client
with midas.client.MidasClient("my_program") as client:
state = client.odb_get("/Runinfo/State")
This guarantees that your program disconnects cleanly from midas when the with block ends. It also guarantees that any cleanup functions you registered are called, even if there's an uncaught exception or your code calls exit(). Without the with statement, Python may not disconnect your program cleanly. See Doing work when the program exits for details.
Requirements
midas.client,midas.frontendandmidas.sequencerneed Python 3. They use the midas C++ library, so midas must have been compiled with CMake.midas.file_readerandmidas.eventare pure Python. They can read midas files on a machine without a midas installation.
The client and frontend libraries are designed to be easy to use, rather than as fast as possible. If performance is critical, write the program in C++, or use numpy arrays (see below).
Supported features
The Python modules cover almost everything that midas programs need. Only a few features of the C++ API are missing, e.g. writing elog entries. midas.client supports:
- ODB access: creating, deleting and renaming keys, getting, setting and watching values, and creating links
- run transitions: starting, stopping, pausing and resuming runs, and registering callback functions for each transition
- event buffers: connecting, sending events and receiving events
- remote procedure calls: connecting and talking to other midas clients
- alarms: creating and triggering alarms
- messages: sending messages and getting recent messages
- history: reading data
Using numpy
The Python libraries can use numpy arrays for ODB values and for the data in midas events (from event buffers or midas files). If the array has the numpy type that matches the midas type, the data is passed to the C++ library without any conversion, which is much faster.
Example from a python frontend
A one-line change can make a frontend 20 times faster. This frontend writes data to a TID_SHORT bank, and the data rate went from 330 MB/s to 7600 MB/s on a laptop.
def __init__(self):
- self.fake_data = [0] * 1250000
+ self.fake_data = np.ndarray(1250000, np.int16)
def readout_func(self):
event = midas.event.Event()
event.create_bank("MYBK", midas.TID_SHORT, self.fake_data)
return event
The dict midas.tid_np_formats gives the numpy type for each midas type. For example midas.tid_np_formats[midas.TID_SHORT] is np.int16. If you write an int16 array to a TID_SHORT bank, the fast path is used. If the types don't match, the data has to be converted, which is much slower.