Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

For some more useful analysis examples, in the following we'll stick to writing customized pyana modules and running pyana from the command line.
But before getting to the pyana modules, I'll briefly touch on a few items general to python that may be useful: saving files, matplotlib for plotting, and IPython for interactive work.

NumPy, SciPy and MatPlotLib

These are packages that you may want to look into. Pretty much all our examples here are using them:

Other useful links:

Saving data arrays

Here are a few examples of how you can save data arrays in python.

Panel
titlesaving numpy arrays to numpy file
Code Block

import numpy as np

myarray = np.arange(0,100)
np.save( "output_file_name.npy", myarray) 
np.savetxt( "output_
file_name.txt", myarray)

...

file_name.txt", myarray) 
Panel
saving to MatLab file
saving to MatLab file
Code Block

import scipy.io 

N_array = np.arange(0,100)
x_array = np.random(100)
y_array = np.random(100)
scipy.io.savemat( "output_file_name.mat", mdict={'N': N_array, 'x' : x_array, 'y' : y_array } )
Panel
saving to HDF5 file
saving to HDF5 file
Code Block
none
none

import h5py

ofile = h5py.File("output_file_name.h5",'w')
group = ofile.create_group("MyGroup")
group.create_dataset('delaytime',data=np.array(self.h_delaytime))
group.create_dataset('rawsignal',data=np.array(self.h_ipm_rsig))
group.create_dataset('normsignal',data=np.array(self.h_ipm_nsig))
ofile.close()

For more examples, see How to access HDF5 data from Python and http://code.google.com/p/h5py/Image Modified

Plotting with MatPlotLib

One of the most commonly used tools for plotting in python: matplotlib. Other alternatives exist too.

...

  • Or you can load arrays from a file and interactively plot them in iPython. The same ('recommended') syntax as above can be used, or if you use 'import *' you don't need to prepend the commands with the package name, which is handy when plotting interactively:
    Code Block
    from matplotlib.pyplot import *
    
    ion()
    plot(array)
    draw()
    

Interactive analysis with IPython

The LCLS offline analysis group does have plans for a real interactive pyana, but currently this is not available.
2011-11-04 iPsana Interactive Analysis Framework.pdf

...

Panel
titleLoading your arrays into (I)Python and plotting interactively:

This example reads in a file produced by the "point detector delay scan" example below.

Code Block
[ofte@psana0106 xpptutorial]$ ipython --pylab
Python 2.4.3 (#1, Nov  3 2010, 12:52:40)
Type "copyright", "credits" or "license" for more information.

IPython 0.9.1 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object'. ?object also works, ?? prints more.

In [1]: from numpy import *

In [2]: from matplotlib.pyplot import *

In [3]: ipm3 = load('point_scan_delay.npy')

In [42]: ipm3.shape
Out[42]: (200, 3)

In [53]: ion()

In [64]: delay = ipm3[:,0]

In [75]: ipmraw = ipm3[:,1]

In [86]: ipmnorm = ipm3[:,2]

nIn [97]: plot(delay,ipmnorm,'ro')
Out[97]: [<matplotlib.lines.Line2D object at 0x59c4c10>]

In [109]: draw()

In [1110]:

...

Sometimes you need to issue the draw() command twice, for some reason. After drawing you can keep working on the arrays and plot more...

Extracting the data with pyana, some examples

...

Cloak
iddelayscan
  • Fetching the ControlPV information:
    ControlPV is available from the env object, and since it only changes at the beginning
    of each calibration cycle, the begincalibcycle function is the appropriate place to get it:
    none
    The ControlConfig object may contain several pvControl and pvMonitor objects. In this case
    there's only one, but make sure the name matches anyway: none
  • Fetching the IPIMB and PhaseCavity information:
    All the other information that we need, is available through the evt object, and
    event member function is the place to get it:
    none
    Use "XppSb3Ipm-1|Ipimb-0" (a.k.a. IPM3) sum of all channels for normalization and filtering
    none
    Use "XppSb3Pim-1|Ipimb-0" (a.k.a. PIM3) channel 1 as signal
    none
    Get the phase cavity:
    none
    Compute delay time and fill histograms
    none

Image peak finding

Here are a collection of useful algorithms for image analysis: http://docs.scipy.org/doc/scipy/reference/ndimage.html

...