Draft, in work.

Topics

Go back to the Top of this tree.

IPython has several features useful for inspecting Python objects and object hierarchies, in particular Tab completion/expansion of attributes, very handy in this case for inspecting the structure of Xtc data.

Once you've cloned the LCLS2 PSDM repository and built the executables therein, generate sample Xtc data files and inspect with IPython and psana. These notes are current as of tag Ric-180309c.

Setting Up

Switch to PSDM conda Environment

Return to the PSDM conda environment, if necessary.

# At the top of the PSDM lcls2 git repo
user@host> bash
user@host-BASH> source ./setup_env.sh
(ps-0.1.2) [user@host-BASH]$

Generate a Sample Data File

Generate a file of simple Xtc-formatted dummy data.

In the current release, this generates a 2-event data file.

# Usage: xtcwriter [-f <filename> -t -h]
(ps-0.1.2) [user@host-BASH]$ xtcwriter -f test.xtc

Create a Basic psana Data Import Script

We need a simple script, let's call it quick.py, to execute in the IPython session.

quick.py
from psana import DataSource

ds = DataSource('test.xtc')

for evt in ds.events():
	pass

Inspecting Xtc Data in IPython

Start IPython and Run Import Script

  1. Start IPython.

    (ps-0.1.2) [user@host-BASH]$ IPython
    
    Python 3.6.6 |Anaconda, Inc.| (default, Jun 28 2018, 17:14:51) 
    Type 'copyright', 'credits' or 'license' for more information
    IPython 6.5.0 -- An enhanced Interactive Python. Type '?' for help.
    
    In [1]:
  2. Run quick.py.

    # In IPython session
    In [1]: run quick.py

Examine Data Per Event

With the import script executed, examine the events in the DataSource object. The IPython %who and %whos magic functions are useful at this stage.

  1. Take a look at what's in scope.

    # In IPython session
    In [2]: %whos
    Variable     Type          Data/Info
    ------------------------------------
    DataSource   type          <class 'psana.datasource.DataSource'>
    ds           DataSource    <psana.datasource.DataSou<...>object at 0x7fa4e8838a58>
    evt          Event         <psana.event.Event object at 0x7fa4e8838ba8>
  2. Store a couple events so we can inspect them.

    # In IPython session
    In []: ev1 = next(ds.events())
    In []: ev2 = next(ds.events())
    
  3. Use Tab completion to look at the psana representation of these events and drill down. Look at the structure inside ev1 (event1) by typing "ev1." at the console and hitting the Tab key to see a list of attributes and methods inside the ev1 object.

    # In IPython session
    In []: ev1.
                 ev1.dgrams      ev1.next        ev1.replace     ev1.to_bytes    
                 ev1.from_bytes  ev1.offsets     ev1.seconds                     
                 ev1.nanoseconds ev1.position    ev1.size
  4. To access detector data inside this event, we'll drill down into the datagrams associated with the event.

    # In IPython session
    In []: ev1.dgrams
    Out []: [<dgram.Dgram at 0x7f73938206c0>]
    
    # ev1.dgrams is a single-item list. Store the sole datagram at index 0.
    In []: ev1dg1 = ev1.dgrams[0]
    In []: ev1dg1
    Out []: <dgram.Dgram at 0x7f73938206c0>
  5. Continue the drill-down into ev1dg1 by entering "ev1dg1." then the Tab key. Now we reach attributes that publicize themselves as XPP science data, so we don't have to use a detector lookup table in the source or in documentation to find what we're after.

    # In IPython session
    In []: ev1dg1.
                        ev1dg1.seq      
                        ev1dg1.xppcspad 
                        ev1dg1.xpphsd
  6. Use Tab completion multiple times to check what's in the xppcspad hierarchy.

    # In IPython session
    # Single Tab to reveal a raw data node
    In []: ev1dg1.xppcspad.raw
    # Tab again to reveal an array of the raw data, Enter to output to terminal
    In []: ev1dg1.xppcspad.raw.arrayRaw
    Out[]: 
    array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
           17], dtype=uint8)
  7. Now look through the xpphsd hierarchy.

    # In IPython session
    # Single Tab to reveal a lower nodes
    In []: ev1dg1.xpphsd.              
                               ev1dg1.xpphsd.fex 
                               ev1dg1.xpphsd.raw
    
    # Choose a branch and continue Tab-ing
    In []: ev1dg1.xpphsd.fex.          
                                   ev1dg1.xpphsd.fex.arrayFex 
                                   ev1dg1.xpphsd.fex.floatFex 
                                   ev1dg1.xpphsd.fex.intFex
    # Pick the floatFex node and output contents with Enter key
    In []: ev1dg1.xpphsd.fex.floatFex
    Out[]: 41.0

Examine Data Per Detector

TBW. Drill-down by detector rather than by event.

 

  • No labels