utils¶
nrxrdct.utils ¶
Miscellaneous utility functions.
Covers zinger (hot-pixel) removal, percentile estimation for spot removal, circular mask generation, absorption coefficient estimation, and array padding helpers used by the reconstruction pipeline.
Powder XRD simulation and baseline fitting have moved to
:mod:nrxrdct.powder.simulation.
calculate_absorption_coefficient ¶
calculate_absorption_coefficient(compound: str, density: float, energy_keV: float, sample_diameter_mm: float, geometry: str = 'debye-scherrer') -> dict[str, float]
Estimate the GSAS-II absorption parameter for a powder sample using xraylib.
The function computes the linear attenuation coefficient μ (cm⁻¹) from the mass attenuation coefficient and density, then converts it to the dimensionless parameter expected by GSAS-II for the chosen geometry.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Note
xraylib's CS_Total_CP returns the total mass attenuation coefficient
(photoelectric + Compton + Rayleigh) in cm²/g. For multi-phase samples the
effective μ can be approximated as the volume-weighted average of the individual
phase μ values.
Example
result = calculate_absorption_coefficient( ... compound="Al2O3", density=3.99, energy_keV=44.0, ... sample_diameter_mm=0.3, geometry="debye-scherrer" ... ) print(f"μ = {result['mu_cm']:.2f} cm⁻¹") print(f"GSAS-II Absorption (μr) = {result['gsasii_absorption']:.4f}")
Source code in src/nrxrdct/utils.py
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 | |
calculate_padding_widths_2D ¶
calculate_padding_widths_2D(input_shape: tuple, desired_shape: tuple) -> tuple[tuple[int, int], tuple[int, int]]
Compute symmetric np.pad widths to centre a 2-D array in a larger shape.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in src/nrxrdct/utils.py
dezinger ¶
Apply zinger removal to a stack of detector images in parallel.
Each frame is processed independently via :func:zinger_remove using a
thread pool sized to os.cpu_count() - 1.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in src/nrxrdct/utils.py
estimate_percentile_from_separate ¶
estimate_percentile_from_separate(image: ndarray, poni_file: str, npt: int = 1000, unit: str = '2th_deg', mask: Optional[ndarray] = None, dark: Optional[ndarray] = None, flat: Optional[ndarray] = None, radial_range: Optional[Tuple[float, float]] = None, azimuth_range: Optional[Tuple[float, float]] = None, npt_azim: int = 360, threshold: float = 0.95, plot: bool = False) -> Tuple[float, float]
Estimate the optimal (low, high) percentile bounds for single-crystal
spot removal by analysing the amorphous/polycrystalline background
separated by pyFAI's AzimuthalIntegrator.separate.
The strategy is
- Run
ai.separateto decompose the image into a crystalline (Bragg spots) component and a smooth amorphous/powder background. - For each radial bin, compute the azimuthal intensity distribution of the original image and find the fraction of pixels that lie within the background signal range.
- The low percentile is set so that
thresholdof the background pixels are retained; the high percentile clips the Bragg-spot tail.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Note
ai.separate is relatively slow (it runs a full 2D integration internally).
Call this function on a representative image to obtain the percentile estimate,
then reuse the result across all frames via azimuthal_integration_1d_filter
or integrate_powder_parallel.
Example
low, high = estimate_percentile_from_separate(image, "det.poni") print(f"Recommended percentile: ({low:.1f}, {high:.1f})") q, I, _ = azimuthal_integration_1d_filter( ... image, "det.poni", percentile=(low, high) ... )
Source code in src/nrxrdct/utils.py
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | |
generate_circular_mask ¶
Generate a 2-D boolean circular mask for a given image or volume shape.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Source code in src/nrxrdct/utils.py
zinger_remove ¶
Remove zingers (hot pixels) from a single 2-D detector image.
Pixels whose deviation from the local median exceeds nsigma standard
deviations (computed over the whole residual image) are replaced by the
median-filtered value. The replacement region is dilated by one pixel to
catch ringing artefacts around bright zingers.
| Parameters: |
|
|---|
| Returns: |
|
|---|