Using Python to Automate MAS

From MCEWiki

The new MCE control module - pymce

The pymce module is the latest attempt to allow MCE control from python. Access to frame data is accomplished more efficiently (writing directly into numpy arrays) and the data reading functions are able to decode the data_mode and rectangle mode configuration to provide the same level of data interpretation that is found in mce_data.py.

Compilation and installation

The pymce module has basic functionality (equivalent to mce.py) starting with mas/trunk:r616. Higher-level functionality will probably be added in the future.

To install, from the MAS directory run:

cd python/mcelib
make
sudo make install

To see if that worked, first move out of the build directory (cd ..) and then attempt to execute the python code:

from pymce import MCE 
mce = MCE()

If that succeeded, the object "mce" can now be used to control the MCE.

Reading and writing MCE parameters

The syntax for 'read' and 'write' are the same as the old mce.py:

mce.write('cc', 'led', 7)
fw_rev = mce.read('cc', 'fw_rev')

Reading MCE frames

Frame data can be obtained by calling the "read_data" method.

d = mce.read_data(10)   # Read 10 frames from RCS
print d
 <pymce.basic.MCEBinaryData instance at 0xb7823a2c>

The returned object contains the binary data (in .data; a 2-d array of int32) and the frame headers (in .headers; a 2-d array of int32):

print d.data.shape
 (10, 528)

Note that for this default call, the data array has dimensions [time,detector].

Unpacking rectangle mode data

When running with non-trivial rectangle mode packing, the physical detector arrangement can be obtained by passing "extract=True" to the read_data call. For example, if we are reading out 2 detectors into frames of size 256, then we would get:

d = mce.read_data(100, extract=True)
print d.data.shape
 (2, 12800)

Note that when "extract=True" is passed, the dimensions of the data array are [detector,time].

Furthermore it can be convenient to have quick access to the detector row and column dimensions. To that end, pass "row_col=True":

d = mce.read_data(100, row_col=True)
print d.data.shape
 (2, 1, 12800)

Now the data array dimensions are [row,column,time].

Extracting signals based on data_mode

The MCEBinaryData object has an "extract" method for automatically extracting and rescaling data according to the data_mode. The operations require mce_data.py.

For example, in data mode 10 we might want to extract the filtered feedback and flux jump counter into separate arrays:

d = mce.read_data(100, extract=True)
fj = d.extract('fj')
fb = d.extract('fb_filt')

The old MCE library - mce.py

If possible, new applications should use the pymce packages described above. A number of existing applications may currently depend on mce.py, but they will be ported over in the near future.

Basic python access to the C language "mce_interface" library is provided by a swig wrapper. This produces mce_library.py. Higher level support is then exposed by mce.py, which exposes basic MCE objects.

Basic commanding

Note that mce.read and mce.write expect lists as arguments, and return lists by default, even for single-item parameters.

from mce import mce             # Import the basic MCE class
m = mce()                       # Get one
sa_bias = m.read('sa', 'bias')  # This will return a list of integers, one per column
m.write('cc', 'led', [7])       # Note that we pass a list containing the single item 7

Reading frames from the MCE

Frames can only be read directly into python; support for triggering writes to disk from python is not yet in place.

from mce import mce
m = mce()
data = m.read_frames(10, data_only=True)    # This will read in 10 frames in the form of a (10, n_channels) array.

When you do read the header (data_only=False), the data is returned as a tuple with all the headers in the first returned value and all the frame data in the second tuple element.