a python-based approach to interactive x-ray and neutron ......nsls‐ii workshop, april 19‐21,...

27
A Python-based approach to interactive A Python based approach to interactive x-ray and neutron data analysis How an ex-programming scientist recovered his mojo Ray Osborn, Stephan Rosenkranz, JohnPaul Castellan Materials Science Division, Argonne National Laboratory, Argonne, IL Boyana Norris, Jason Sarich, Dmitry Karpeev Mathematics and Computer Science Division, Argonne National Laboratory, Argonne, IL ih ilh k with special thanks to Paul Kienzle NIST Center for Neutron Research, NIST, Gaithersburg, MD

Upload: others

Post on 16-Oct-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: A Python-based approach to interactive x-ray and neutron ......NSLS‐II Workshop, April 19‐21, 2010 Interactive style 9 NSLS‐II Workshop, April 19‐21, 2010 Issues ‣ Id tInadequate

A Python-based approach to interactive A Python based approach to interactive x-ray and neutron data analysis

How an ex-programming scientist recovered his mojo

Ray Osborn, Stephan Rosenkranz, John‐Paul Castellan Materials Science Division, Argonne National Laboratory, Argonne, IL

Boyana Norris, Jason Sarich, Dmitry Karpeev Mathematics and Computer Science Division, Argonne National Laboratory, Argonne, IL

i h i l h kwith special thanks to Paul Kienzle NIST Center for Neutron Research, NIST, Gaithersburg, MD

Page 2: A Python-based approach to interactive x-ray and neutron ......NSLS‐II Workshop, April 19‐21, 2010 Interactive style 9 NSLS‐II Workshop, April 19‐21, 2010 Issues ‣ Id tInadequate

Where we want to be

QuickTime™ and aMicrosoft Video 1 decompressorare needed to see this picture.

2

NSLS‐II Workshop, April 19‐21, 2010

2

Page 3: A Python-based approach to interactive x-ray and neutron ......NSLS‐II Workshop, April 19‐21, 2010 Interactive style 9 NSLS‐II Workshop, April 19‐21, 2010 Issues ‣ Id tInadequate

Where we are

3

NSLS‐II Workshop, April 19‐21, 2010

3

Page 4: A Python-based approach to interactive x-ray and neutron ......NSLS‐II Workshop, April 19‐21, 2010 Interactive style 9 NSLS‐II Workshop, April 19‐21, 2010 Issues ‣ Id tInadequate

4

NSLS‐II Workshop, April 19‐21, 2010

4

Page 5: A Python-based approach to interactive x-ray and neutron ......NSLS‐II Workshop, April 19‐21, 2010 Interactive style 9 NSLS‐II Workshop, April 19‐21, 2010 Issues ‣ Id tInadequate

Interactive Friction

5

NSLS‐II Workshop, April 19‐21, 2010

5

Page 6: A Python-based approach to interactive x-ray and neutron ......NSLS‐II Workshop, April 19‐21, 2010 Interactive style 9 NSLS‐II Workshop, April 19‐21, 2010 Issues ‣ Id tInadequate

Interactive Friction (Take II)

6

NSLS‐II Workshop, April 19‐21, 2010

6

Page 7: A Python-based approach to interactive x-ray and neutron ......NSLS‐II Workshop, April 19‐21, 2010 Interactive style 9 NSLS‐II Workshop, April 19‐21, 2010 Issues ‣ Id tInadequate

Interactive Friction (Take III)

7

NSLS‐II Workshop, April 19‐21, 2010

7

Page 8: A Python-based approach to interactive x-ray and neutron ......NSLS‐II Workshop, April 19‐21, 2010 Interactive style 9 NSLS‐II Workshop, April 19‐21, 2010 Issues ‣ Id tInadequate

Issues

R titi kfl‣ Repetitive workflow

‣ Insufficient data persistence

‣ Poor integration with previous analysis (or operating system)

‣ Lack of automation (scripting)

‣ No memory management

‣ Use of proprietary language

8

NSLS‐II Workshop, April 19‐21, 2010

8

Page 9: A Python-based approach to interactive x-ray and neutron ......NSLS‐II Workshop, April 19‐21, 2010 Interactive style 9 NSLS‐II Workshop, April 19‐21, 2010 Issues ‣ Id tInadequate

Interactive style

9

NSLS‐II Workshop, April 19‐21, 2010

9

Page 10: A Python-based approach to interactive x-ray and neutron ......NSLS‐II Workshop, April 19‐21, 2010 Interactive style 9 NSLS‐II Workshop, April 19‐21, 2010 Issues ‣ Id tInadequate

Issues

I d t d t d l‣ Inadequate data model

‣ Insufficient and inconsistent access to instrumental metadata

‣ Use of proprietary language

10

NSLS‐II Workshop, April 19‐21, 2010

10

Page 11: A Python-based approach to interactive x-ray and neutron ......NSLS‐II Workshop, April 19‐21, 2010 Interactive style 9 NSLS‐II Workshop, April 19‐21, 2010 Issues ‣ Id tInadequate

Measurements of S(Q,ω) at ISIS (Horace-mode)

Magnetic Fluctuations in MnSiR.A.Ewings, T.G.Perring (ISIS) unpublished

Lattice vibrations in calcite (CaCO3)Beth Cope, Martin Dove (Cambridge) unpublished

11

NSLS‐II Workshop, April 19‐21, 2010

11

Page 12: A Python-based approach to interactive x-ray and neutron ......NSLS‐II Workshop, April 19‐21, 2010 Interactive style 9 NSLS‐II Workshop, April 19‐21, 2010 Issues ‣ Id tInadequate

Issues

‣ Never, ever, define a “temporary” file format.

12

NSLS‐II Workshop, April 19‐21, 2010

12

Page 13: A Python-based approach to interactive x-ray and neutron ......NSLS‐II Workshop, April 19‐21, 2010 Interactive style 9 NSLS‐II Workshop, April 19‐21, 2010 Issues ‣ Id tInadequate

Problems to address

E th t th kfl d t ti l di d t l i‣ Ensure that the workflow does not actively discourage data analysis

‣ Store the results of data analysis as long as needed (not limited by memory)

‣ Ensure that all relevant instrumental metadata is carried with the analysis

‣ Allow all repetitive actions to be scripted by the user

‣ Do not use proprietary languages and packages unless the functionality really demands it

13

NSLS‐II Workshop, April 19‐21, 2010

13

Page 14: A Python-based approach to interactive x-ray and neutron ......NSLS‐II Workshop, April 19‐21, 2010 Interactive style 9 NSLS‐II Workshop, April 19‐21, 2010 Issues ‣ Id tInadequate

NeXpy - a Python-based approach

Wh t N X i t‣ What NeXpy is not:• a comprehensive solution to all the issues so far raised• particularly sophisticated or novel

Wh X i‣ What NeXpy is:• a toolbox for manipulating and visualizing arbitrary NeXus data • a possible scripting engine for GUI applications

d t ti f th l f bi i• a demonstration of the value of combining:‐ a flexible data model

‐ a powerful scripting language

++

14

NSLS‐II Workshop, April 19‐21, 2010

14

Page 15: A Python-based approach to interactive x-ray and neutron ......NSLS‐II Workshop, April 19‐21, 2010 Interactive style 9 NSLS‐II Workshop, April 19‐21, 2010 Issues ‣ Id tInadequate

NeXus

‣ The NeXus International Advisory Committee represents 16 international facilities and institutions

‣ If you want to use HDF5, there is no reason not to use the NeXus design principlesy g p p

‣ If there are deficiencies in NeXus, join the NeXus mailing list and make a nuisance of yourself

15

NSLS‐II Workshop, April 19‐21, 2010

15

Page 16: A Python-based approach to interactive x-ray and neutron ......NSLS‐II Workshop, April 19‐21, 2010 Interactive style 9 NSLS‐II Workshop, April 19‐21, 2010 Issues ‣ Id tInadequate

NeXus HierarchyScan1 (NXentry) sample (NXsample)Scan1 (NXentry) sample (NXsample)

SAXS (NXinstrument)

monitor1 (NXmonitor)

monitor2 (NXmonitor)

data (NXdata)

t t ti

Scan2 (NXentry) l (NX l )

start_time

Scan2 (NXentry) sample (NXsample)

SAXS (NXinstrument)

16

NSLS‐II Workshop, April 19‐21, 2010

16

Page 17: A Python-based approach to interactive x-ray and neutron ......NSLS‐II Workshop, April 19‐21, 2010 Interactive style 9 NSLS‐II Workshop, April 19‐21, 2010 Issues ‣ Id tInadequate

Simplest NeXus File

Scan (NXentry) data (NXdata)counts

Scan (NXentry) data (NXdata)

polar_angle

17

NSLS‐II Workshop, April 19‐21, 2010

17

Page 18: A Python-based approach to interactive x-ray and neutron ......NSLS‐II Workshop, April 19‐21, 2010 Interactive style 9 NSLS‐II Workshop, April 19‐21, 2010 Issues ‣ Id tInadequate

NXinstrument

NXsource

NXmonochromator

NXfilterNXinstrument

NXanalyzer

NXdetectorNXdetector

18

NSLS‐II Workshop, April 19‐21, 2010

18

Page 19: A Python-based approach to interactive x-ray and neutron ......NSLS‐II Workshop, April 19‐21, 2010 Interactive style 9 NSLS‐II Workshop, April 19‐21, 2010 Issues ‣ Id tInadequate

NXdata Structures

counts[n,m] (signal=1, units=“counts”, axes=“[polar_angle,time_of_flight]”)

data (NXdata) errors[n,m]

time_of_flight[m] (units=“microseconds”)

polar_angle[n] (units=“degrees”)

19

NSLS‐II Workshop, April 19‐21, 2010

19

Page 20: A Python-based approach to interactive x-ray and neutron ......NSLS‐II Workshop, April 19‐21, 2010 Interactive style 9 NSLS‐II Workshop, April 19‐21, 2010 Issues ‣ Id tInadequate

Numpy

20

NSLS‐II Workshop, April 19‐21, 2010

20

Page 21: A Python-based approach to interactive x-ray and neutron ......NSLS‐II Workshop, April 19‐21, 2010 Interactive style 9 NSLS‐II Workshop, April 19‐21, 2010 Issues ‣ Id tInadequate

21

NSLS‐II Workshop, April 19‐21, 2010

21

Page 22: A Python-based approach to interactive x-ray and neutron ......NSLS‐II Workshop, April 19‐21, 2010 Interactive style 9 NSLS‐II Workshop, April 19‐21, 2010 Issues ‣ Id tInadequate

Python API

Th t l l t th P th API‣ There are two levels to the Python API• A one‐to‐one mapping of the C‐API returning Numpy arrays (napi.py)• A one‐to‐one mapping of the NeXus data objects into Python classes (tree.py)

>>> f=nexus.open("data/chopper.nxs")>>> f.opengroup("entry","NXentry")>>> f.opendata("title")>>> f.getdata()'MgB2 PDOS 43.37g 8K 120meV E0@240Hz T0@120Hz'>>> f.closedata()

f ()

>>> a=nexus.load("data/chopper.nxs")>>> a.entry.data.nxtree()data:NXdata data = int32(148x750) @axes = polar angle:time of flight @long name = Neutron>>> f.closegroup()

>>> f.close()

int32(148x750) @axes polar_angle:time_of_flight @long_name Neutron Counts @signal = 1 @units = counts polar_angle = float32(148) @long_name = Polar Angle [degrees] @units = degrees time_of_flight = [ 1900. 1902. 1904. ..., 3396. 3398. 3400.] @long_name = Time-of-Flight [microseconds] @units = microseconds title = MgB2 PDOS 43.37g 8K 120meV E0@240Hz T0@120Hz>>> print a.entry.data.titleMgB2 PDOS 43.37g 8K 120meV E0@240Hz T0@120Hz

22

NSLS‐II Workshop, April 19‐21, 2010

22

Page 23: A Python-based approach to interactive x-ray and neutron ......NSLS‐II Workshop, April 19‐21, 2010 Interactive style 9 NSLS‐II Workshop, April 19‐21, 2010 Issues ‣ Id tInadequate

Features of NeXus tree interface

Th ti t t t f N X fil b l d d ith i l d‣ The entire tree structure of a NeXus file can be loaded with a single command

‣ The data values are not read unless directly referenced

‣ Each NeXus object is a sub‐class of the class NXnode• NeXus data have class SDS• NeXus groups have class NXgroup, with subclasses for each group class

‐ e.g., NXentry, NXdata, NXsample ...

ll bj f h l il b li d‣ All objects of the same class can easily be listed• e.g., entry.NXdata[0], entry.NXdata[1], ...

‣ All data attributes have a class NXattr• e.g., entry.data.polar_angle.units=”K”

‣ Command‐line assignments automatically convert Numpy data into NeXus objects• e.g., typing “entry.sample.temperature=40.0” converts the temperature into a valid SDS 

bjobject

• this is achieved by customizing __setattr__

23

NSLS‐II Workshop, April 19‐21, 2010

23

Page 24: A Python-based approach to interactive x-ray and neutron ......NSLS‐II Workshop, April 19‐21, 2010 Interactive style 9 NSLS‐II Workshop, April 19‐21, 2010 Issues ‣ Id tInadequate

Interactive manipulation of NeXus data

Th t k th ti f t d d f i N X d t t t i l‣ The syntax makes the creation of standard‐conforming NeXus data structures simple

>>> entry=NXentry()>>> x=np.linspace(0,2.*np.pi,101)>>> y=x

X Y h id( )>>> X,Y=np.meshgrid(x,y)>>> z=np.sin(X)*np.sin(Y)>>> entry.data=NXdata(z,(x,y))>>> entry.nxtree()entry:NXentry data:NXdata axis1 = float64(101) axis2 = float64(101) signal = float64(101x101) @axes = axis1:axis2 @signal = 1float64(101) signal float64(101x101) @axes axis1:axis2 @signal 1>>> entry.nxplot()

24

NSLS‐II Workshop, April 19‐21, 2010

24

Page 25: A Python-based approach to interactive x-ray and neutron ......NSLS‐II Workshop, April 19‐21, 2010 Interactive style 9 NSLS‐II Workshop, April 19‐21, 2010 Issues ‣ Id tInadequate

NXdata group manipulations

Th NXd t bj t d i d t b i l t d>>> data[1:2.].nxsum(0).nxtree()data:NXdata axis2 = float64(101) signal = float64(101) @axes = axis2 @long_name = Integral from 1.00530964915 to 2.0106192983 @signal = 1>>> data.nxtree()data:NXdata signal = [ 0 00000000 00 7 07106781 01 1 00000000 00 1 00000000 00

‣ The NXdata objects are designed to be manipulated:• Diced and sliced• Scaled

Add d d bt t d 0.00000000e+00 7.07106781e-01 1.00000000e+00 ..., -1.00000000e+00 -7.07106781e-01 -2.44929360e-16 ] @axes = x @signal = 1 x = float64(9)

• Added and subtracted

>>> (2*data).nxtree() # or (data+data).nxtree()data:NXdata signal = [ 0.00000000e+00 1.41421356e+00 2.00000000e+00 ..., -2.00000000e+00 -1.41421356e+00 -4.89858720e-16 ] @axes = x @signal = 1 x = float64(9)

(d t 1) t ()d t NXd t i l [ 1 1 70710678 2 0 >>> (data+1).nxtree()data:NXdata signal = [ 1. 1.70710678 2. ..., 0. 0.29289322 1. ] @axes = x @signal = 1 x = float64(9)

25

NSLS‐II Workshop, April 19‐21, 2010

25

Page 26: A Python-based approach to interactive x-ray and neutron ......NSLS‐II Workshop, April 19‐21, 2010 Interactive style 9 NSLS‐II Workshop, April 19‐21, 2010 Issues ‣ Id tInadequate

NeXpy GUI

26

NSLS‐II Workshop, April 19‐21, 2010

26

Page 27: A Python-based approach to interactive x-ray and neutron ......NSLS‐II Workshop, April 19‐21, 2010 Interactive style 9 NSLS‐II Workshop, April 19‐21, 2010 Issues ‣ Id tInadequate

On the To-Do List (near-term)

Addi d t dit‣ Adding a data editor

‣ Incorporating generalized coordinate transformations

‣ Incorporating Open GL modules to improve over current Matplotlib speeds 

‣ Adding full 3D plotting• Using Mayavi

‣ Adding a fitting pane• Using Paul Kienzle’s PARK framework

‣ Installing it as a standard part of the NeXus distribution

27

NSLS‐II Workshop, April 19‐21, 2010

27