An initial attempt to make a Python package for X-ray fluorescence (XRF) and differential phase contrast (DPC) analysis. It is thin but will grow.
- Data Correction: Deadtime correction and i0 normalization
- Energy Calibration: Flexible energy axis calibration
- Peak Detection: Automated scatter and XRF peak detection
- Physics-Based Identification: XRF line identification using excitation physics
- Spatial Mapping: Element distribution visualization
- DPC Analysis: Beam deflection calculations
git clone https://github.com/yourusername/xrf-analysis.git
cd xrf-analysis
pip install -e .- Python >= 3.8
- numpy >= 1.20.0
- scipy >= 1.7.0
- matplotlib >= 3.3.0
- h5py >= 3.0.0
- xraylib >= 4.0.0
import xrf_analysis as xrf
# Initialize XRF database
xrf.initialize_combined_xrf_database()
# Load and correct data
results = xrf.correct_xrf_data('your_file.h5')
results = xrf.apply_energy_calibration(results, eV_per_channel=10)
# Detect peaks
detection_results = xrf.detect_and_identify_xrf_peaks(results)
# Visualize
xrf.plot_peak_detection_results(detection_results)
xrf.plot_element_distribution(results, 'Fe', 'Ka')xrf_analysis/
├── core/
│ ├── physics.py # XRF physics calculations
│ ├── database.py # XRF line database
│ ├── corrections.py # Data corrections
│ └── peak_detection.py # Peak detection algorithms
├── visualization/
│ ├── spectra.py # Spectrum plotting
│ └── spatial.py # Spatial distribution plots
└── dpc/
└── analysis.py # DPC calculations
# Load HDF5 file with deadtime and i0 correction
results = xrf.correct_xrf_data('data.h5')
# Apply energy calibration
results = xrf.apply_energy_calibration(
results,
eV_per_channel=10,
energy_offset=0
)# Plot integrated and maximum envelope spectra
xrf.plot_spectrum_with_energy(results)# Detect XRF peaks with scatter peak exclusion
detection_results = xrf.detect_and_identify_xrf_peaks(
results,
thresholds=[10, 50, 10], # [height, distance_eV, prominence]
tolerance_keV=0.060, # 60 eV matching tolerance
detect_scatter=True # Exclude elastic/Compton peaks
)
# Visualize detection results
xrf.plot_peak_detection_results(detection_results)# Create spatial distribution map
intensities = xrf.plot_element_distribution(
results,
element_name='Fe',
line_family='Ka',
integration_width_keV=0.15,
sensor_sum=True,
scale='log'
)detection_results = xrf.detect_and_identify_xrf_peaks(
results,
thresholds=[20, 100, 15], # Higher thresholds for cleaner data
smooth_window=7, # More smoothing
bkg_method='polynomial', # Polynomial background
bkg_radius=100 # Larger background filter
)# For samples measured at 45° to the beam
xrf.plot_element_distribution(
results,
'Cu', 'Ka',
correct_45deg=True,
expand_x=True # Expand to sample surface coordinates
)from xrf_analysis.dpc import calculate_dpc_deflections, plot_dpc_results
# Calculate beam deflections
dpc_results = calculate_dpc_deflections(
thor_x, thor_y, thor_Tot, i0,
Lx=10, Ly=10 # Detector dimensions in mm
)
# Visualize DPC results
plot_dpc_results(abs_x, abs_y, dpc_results['beam_x'],
dpc_results['beam_y'], thor_Tot)Load and correct XRF data from HDF5 file.
Apply energy calibration to create energy axis.
Pre-calculate XRF line database (call once at startup).
Detect and identify XRF peaks with physics-based filtering.
Plot integrated and envelope spectra.
Visualize peak detection with annotations.
Create spatial distribution maps.
MIT License - see LICENSE file for details.
Contributions welcome! Please open an issue or submit a pull request.
If you use this package in your research, please cite:
https://github.com/AustralianSynchrotron