Important

You can run this notebook in a live session Binder or view it on nbviewer or GitHub.

Slice and Interpolate Image Data

[1]:
import pathlib
from collections import defaultdict

import h5py
import pandas as pd
import numpy as np
import xarray as xr

import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm,Normalize
import matplotlib.cm as cm

from bluesky_tutorial_utils import nexus, fetch
/home/travis/virtualenv/python3.7.1/lib/python3.7/site-packages/pims/image_reader.py:26: RuntimeWarning: PIMS image_reader.py could not find scikit-image. Falling back to matplotlib's imread(), which uses floats instead of integers. This may break your scripts.
(To ignore this warning, include the line "warnings.simplefilter("ignore", RuntimeWarning)" in your script.)
  warnings.warn(RuntimeWarning(ski_preferred))
[2]:
# Un-comment this for better-looking plots on high-resolution screens.
# %config InlineBackend.figure_format = 'retina'

We have taken the original data and reduced its precision significnatly so that it will download faster for teaching purposes.

[3]:
fetch.rsoxs_simulation_data();

Working with a Single Image xArray

[4]:
fname = './rsoxs_simulation_data/512-512-128-5.0-40.0-00285-0360.nxs'
da_img = nexus.read_singleimg_nxs(fname)
da_img_chi = nexus.read_singleimg_nxs(fname,sasdata='unwrap')
da_img
[4]:
<xarray.DataArray 'I' (Qx: 512, Qy: 512)>
array([[0., 0., 0., ..., 0., 0., 0.],
       [0., 0., 0., ..., 0., 0., 0.],
       [0., 0., 0., ..., 0., 0., 0.],
       ...,
       [0., 0., 0., ..., 0., 0., 0.],
       [0., 0., 0., ..., 0., 0., 0.],
       [0., 0., 0., ..., 0., 0., 0.]], dtype=float16)
Coordinates:
  * Qx       (Qx) float16 -0.6284 -0.626 -0.6235 -0.621 ... 0.6235 0.626 0.6284
  * Qy       (Qy) float16 -0.6284 -0.626 -0.6235 -0.621 ... 0.6235 0.626 0.6284
Attributes:
    lazy:     False

Basic Slicing and Plotting

First just a simple image plot on a log scale

[5]:
da_img.plot(norm=LogNorm(1e-9,1),aspect=1.2,size=5)
[5]:
<matplotlib.collections.QuadMesh at 0x7f73bf2275c0>
_images/Slice_and_Interpolate_Image_Data_8_1.png

Comparison of nearest-pixel versus interpolated selection for a horizontal line cut

[6]:
da_img.sel(Qy=0,method='nearest').plot(yscale='log',xscale='log',label='nearest')
da_img.interp(Qy=0).plot(yscale='log',xscale='log',label='interp', ls='--')
plt.legend()
[6]:
<matplotlib.legend.Legend at 0x7f73bb6e1f98>
_images/Slice_and_Interpolate_Image_Data_10_1.png
[7]:
fig,axes = plt.subplots(1,3,figsize=(16,4))
da_img_chi.plot(norm=LogNorm(1e-9,1),ax=axes[0])
da_img_chi.sel(Q=0.25,method='nearest').plot(ax=axes[1], label='Q=.25')
da_img_chi.sel(Q=0.35,method='nearest').plot(ax=axes[1], label='Q=.35', yscale='log')

(da_img_chi.sel(Q=0.35,method='nearest')
          .coarsen({'chi':5})
          .mean()
          .plot(ax=axes[1],yscale='log', label="Q=.35 coarse"))

axes[1].legend()
axes[1].set_title("Q cuts")

da_img_chi.mean('chi').plot(ax=axes[2],xscale='log',yscale='log',label='Full Azimuthal')
da_img_chi.sel(chi=0,method='nearest').plot(ax=axes[2],xscale='log',yscale='log',label='Single χ')
da_img_chi.sel(chi=np.arange(-10,10,0.1),method='nearest').mean('chi').plot(ax=axes[2],xscale='log',yscale='log',label='Sector Average')
axes[2].legend()
plt.tight_layout()
_images/Slice_and_Interpolate_Image_Data_11_0.png

Remeshing

[8]:
qx = np.linspace(-0.5,0.5,512)
qy = np.linspace(-0.5,0.5,512)
da_img_remesh = da_img.interp(Qx=qx,Qy=qy)
da_img_remesh
[8]:
<xarray.DataArray 'I' (Qx: 512, Qy: 512)>
array([[2.00280920e-07, 2.38437260e-07, 2.48001998e-07, ...,
        2.88638076e-07, 2.50425783e-07, 2.40805093e-07],
       [2.38437260e-07, 2.38605153e-07, 2.86307313e-07, ...,
        2.98098241e-07, 2.98023370e-07, 2.50414233e-07],
       [2.48001998e-07, 2.86353970e-07, 2.95722309e-07, ...,
        3.36506932e-07, 2.98098241e-07, 2.88673183e-07],
       ...,
       [2.88626617e-07, 2.98098150e-07, 3.36460184e-07, ...,
        3.93752454e-07, 3.45975424e-07, 3.07595001e-07],
       [2.50367666e-07, 2.98023279e-07, 2.98051492e-07, ...,
        3.45928767e-07, 2.98151736e-07, 2.98030263e-07],
       [2.40793452e-07, 2.50367666e-07, 2.88626617e-07, ...,
        3.07559894e-07, 2.98041814e-07, 2.59838998e-07]])
Coordinates:
  * Qx       (Qx) float64 -0.5 -0.498 -0.4961 -0.4941 ... 0.4961 0.498 0.5
  * Qy       (Qy) float64 -0.5 -0.498 -0.4961 -0.4941 ... 0.4961 0.498 0.5
Attributes:
    lazy:     False
[9]:
da_img_remesh.plot(norm=LogNorm(1e-9,1),aspect=1.2,size=5)
[9]:
<matplotlib.collections.QuadMesh at 0x7f73b8414a20>
_images/Slice_and_Interpolate_Image_Data_14_1.png