Difference between revisions of "Using Python to Automate MAS"

From MCEWiki
(Getting Started with Python)
 
(7 intermediate revisions by 3 users not shown)
Line 1: Line 1:
= The MCE library - mce.py =
+
= 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 Data | 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', '{{param||led}}', 7)
 +
fw_rev = mce.read('cc', '{{param||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 Data | 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 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 reading and writing ==
+
== Basic commanding ==
  
 
Note that mce.read and mce.write expect '''lists''' as arguments, and return lists by default, even for single-item parameters.  
 
Note that mce.read and mce.write expect '''lists''' as arguments, and return lists by default, even for single-item parameters.  
Line 21: Line 87:
 
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.
 
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.
  
= Getting Started with Python =
+
[[Category:MAS]]
To install the Emacs template file for python, type the following:
+
[[Category:Python]]
sudo apt-get install python-mode
 
 
 
Here is another way to command the MCE from python (discouraged...):
 
mce@mce-ubc-2:~$ python
 
Python 2.4.3 (#2, Mar  7 2008, 01:58:20)
 
[GCC 4.0.3 (Ubuntu 4.0.3-1ubuntu5)] on linux2
 
Type "help", "copyright", "credits" or "license" for more information.
 
>>> import os
 
>>> os.system('mce_cmd -x rb cc fw_rev')
 
This is mce_cmd version MAS/trunk/230
 
Line  0 : ok : 0x400000b
 
Processed 0 lines, exiting.
 
0
 
>>>
 
 
 
When creating a python script, for the script to be executable, you must put the following line of code at the top:
 
#!/bin/python
 
or possibly
 
#!/usr/bin/python
 
 
 
And you must change the permissions of the file to the following:
 
chmod a+x <python_script_name>
 
 
 
In Emacs, to get high-lighting, type
 
<alt>+<x>
 
python-mode
 

Latest revision as of 18:01, 30 August 2016

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.