HEXRD / hexrd

A cross-platform, open-source library for the analysis of X-ray diffraction data.
Other
56 stars 25 forks source link

Automate calibration process on multiple images #655

Closed DSimonne closed 2 months ago

DSimonne commented 3 months ago

Hello,

I would like to use the instrument calibration file that I have created with hexrdgui to then properly extract 2theta vs intensity numpy arrays from my powder measurements.

I have many experimental files and going through the GUI to extract them as h5 one by one while correcting for the calibration is slightly time consuming.

Below is then the script I use to load the .h5 files in Jupyter.

Thank you!!

# %%
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import tables as tb
import glob

# %% [markdown]
# # What I have done
# 
# I have used the `hexrdgui` and `hexrd` packages to:
# * Calibrate the detector using the CeO2 dataset measured at 0 and 180 degrees sample rotations.
# * Use that calibration to then properly extract the data files in a 2$\theta$ vs intensity fashion.
# 
# PS: the calibration correction is saved in the `InstrumentCalibrated.yml` file.
# 

# %% [markdown]
# Below is a list of the h5 files, I put them in the subfolder that corresponds to their respective dataset

# %%
h5_files = glob.glob("s7/**/*h5")
h5_files

# %% [markdown]
# The h5 files have different attributes
# * azimuthal_integration
# * eta_coordinates
# * extent
# * intensities
# * q_coordinates
# * tth_coordinates

# %% [markdown]
# Below is some code that shows you how to extract the data using python, with a small figure in which you can see the intensity evolution

# %%
fig, ax = plt.subplots()

for file in h5_files:
    with tb.open_file(file) as f:
        x = f.root.azimuthal_integration[...][:, 0]
        y = f.root.azimuthal_integration[...][:, 1]
        ax.plot(x, y, linewidth = 0.5, label=file)

ax.set_ylabel("Azimuthal intensities")
ax.set_xlabel(r"2$\theta$")
ax.semilogy()
ax.grid()
ax.set_xlim(1.5, 17)
ax.legend()
fig.tight_layout()
fig.savefig("PowderSignals.png")
psavery commented 3 months ago

@DSimonne If you want to generate two theta vs intensity numpy arrays using hexrd, you can do something like the following:

  1. Create an HEDMInstrument using your calibrated yaml file
  2. Create a dict of images (numpy arrays) where the keys are the detector names. If you have only one detector, you will only have one image in this dict.

Then you would do something like this:

from hexrd.projections.polar import PolarView

# instrument is the HEDMInstrument
# image_dict is the images dict

tth_min = 0
tth_max = 20
pv = PolarView((tth_min, tth_max), instrument, eta_min=0, eta_max=360, pixel_size=(0.1, 0.25))
img = pv.warp_image(image_dict, pad_with_nans=False, do_interpolation=True)

# Now sum along valid pixels to create the intensity array
intensities = np.nansum(img, axis=0) / np.sum(~np.isnan(img), axis=0)
ZackAttack614 commented 2 months ago

@psavery can I mark this as complete?

DSimonne commented 2 months ago

Yes, thank you!