Utils
This module defines auxiliary funtions to process the data.
azimuthal_average_fast(data, center=None, mask=None)
Returns intensity over radius, where intensity is the mean per integer radius. Improve performance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
ndarray
|
Input data in which center of mass will be calculated. Values equal or less than zero will not be considered. |
required |
center
|
tuple
|
Center coordinates of the radial average (xc, yc)->(col, row). |
None
|
mask
|
ndarray
|
Corresponding mask of data, containing zeros for unvalid pixels and one for valid pixels. Mask shape should be same size of data. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
radius |
ndarray
|
Radial axis in pixels. |
intensity |
ndarray
|
Integrated intensity normalized by the number of valid pixels. |
Source code in bblib/utils.py
center_of_mass(data, mask=None)
Adapted from Robert Bücker work on diffractem (https://github.com/robertbuecker/diffractem/tree/master) Bücker, R., Hogan-Lamarre, P., Mehrabi, P. et al. Serial protein crystallography in an electron microscope. Nat Commun 11, 996 (2020). https://doi.org/10.1038/s41467-020-14793-0
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
ndarray
|
Input data in which center of mass will be calculated. Values equal or less than zero will not be considered. |
required |
mask
|
ndarray
|
Corresponding mask of data, containing zeros for unvalid pixels and one for valid pixels. Mask shape should be same size of data. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
xc |
int
|
Coordinate of the diffraction center in x, such that the image center corresponds to data [yc, xc]. |
yc |
int
|
Coordinate of the diffraction center in y, such that the image center corresponds to data [yc, xc]. |
Source code in bblib/utils.py
circle_mask(data, center, radius)
Make a circular mask for the data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
ndarray
|
Image in which mask will be shaped. |
required |
radius
|
int
|
Outer radius of the mask, in pixels. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
mask |
ndarray
|
Mask array containg zeros (pixels to be masked) and ones (valid pixels). |
Source code in bblib/utils.py
correct_polarization(x, y, dist, data, mask, polarization_axis='x', p=0.99)
Correct data for polarisation effect, version in Python. It is based on pMakePolarisationArray from https://github.com/galchenm/vdsCsPadMaskMaker/blob/main/new-versions/maskMakerGUI-v2.py#L234 Acknowledgements: Oleksandr Yefanov, Marina Galchenkova
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
Array containg pixels coordinates in x (pixels) distance from the direct beam. It has same shape of data. |
required |
y
|
ndarray
|
Array containg pixels coordinates in y (pixels) distance from the direct beam. It has same shape of data. |
required |
dist
|
float
|
z distance coordinates of the detector position in pixels. |
required |
data
|
ndarray
|
Raw data frame in which polarization correction will be applied. |
required |
mask
|
ndarray
|
Corresponding mask of data, containing zeros for unvalid pixels and one for valid pixels. Mask shape should be same size of data. |
required |
p
|
float
|
Polarization degree. |
0.99
|
Returns: corrected_data (np.ndarray): Corrected data frame for polarization effect. pol (np.ndarray): Polarization array for polarization correction.
Source code in bblib/utils.py
fsss_to_xy(point, m)
Transforms from the fast-scan/slow-scan basis to the x/y basis.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
point
|
tuple
|
Coordinates in the fast-scan/slow-scan basis (ss,fs). |
required |
m
|
list
|
A 2x2 transformation matrix. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
tuple
|
The corresponding (x, y) coordinates. |
Source code in bblib/utils.py
gaussian_lin(x, a, x0, sigma, m, n)
Gaussian function summed to a linear function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ndarray
|
x-axis. |
required |
a
|
float
|
Amplitude of the Gaussian. |
required |
x0
|
float
|
Average of the Gaussian. |
required |
sigma
|
float
|
Standard deviation of the Gaussian. |
required |
m
|
float
|
Angular coefficient. |
required |
n
|
float
|
Linear coefficient. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
y |
ndarray
|
y-axis. |
Source code in bblib/utils.py
get_fwhm_map_min_from_projection(lines, output_folder, label, pixel_step, plots_flag)
Open FWHM grid search optmization plot, then fit the projection in both axis to get the point of minimum FWHM of the azimuthal average.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lines
|
list
|
Output of grid search for FWHM optmization, each line must contain a values of xc,yc,fwhm,r_square. |
required |
output_folder
|
str
|
Path to the folder where plots are saved. |
required |
label
|
str
|
Plots filename label. |
required |
pixel_step
|
str
|
Step size between grid points in pixels. |
required |
plots_flag
|
bool
|
If True, plots can be generated. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
center |
list
|
Coordinates of the center of the diffraction pattern in x and y. |
Source code in bblib/utils.py
238 239 240 241 242 243 244 245 246 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 330 331 332 333 334 335 336 337 338 339 340 341 | |
make_polarization_array(pol, cox, coy, detdist, poldegree)
Create the polarization array for horizontal polarization correction, version in Python. It is based on pMakePolarisationArray from https://github.com/galchenm/vdsCsPadMaskMaker/blob/main/new-versions/maskMakerGUI-v2.py#L234 Acknowledgements: Oleksandr Yefanov, Marina Galchenkova
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pol
|
ndarray
|
An array where polarization array will be built based on its shape. Mask shape is the same size of data. Unvalid pixels (values containing 0) will be skipped from calculation and put 1. |
required |
cox
|
ndarray
|
Array containg pixels coordinates in x (pixels) distance from the direct beam. It has same shape of data. |
required |
coy
|
ndarray
|
Array containg pixels coordinates in y (pixels) distance from the direct beam. It has same shape of data. |
required |
detdist
|
float
|
Detector distance from the sample in meters . The detctor distance will be transformed in pixel units based on Res defined as global parameter. |
required |
poldegree
|
float
|
Polarization degree from [0,1]. If the polarization is completely horizontal (along the x-axis), then poldegree equals 1. |
required |
Returns: pol (np.ndarray): Polarization array for polarization correction.
Source code in bblib/utils.py
mask_peaks(mask, indexes, bragg, n)
Gather coordinates of a box of 1x1 pixels around each point from the indexes list. Bragg flag indicates if the mask returned will contain only bragg peaks regions (bragg =1), no bragg peaks regions (bragg=0), or both (bragg =-1).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mask
|
ndarray
|
An array where mask will be built based on its shape. Mask shape is the same size of data. |
required |
indexes
|
tuple
|
Bragg peaks coordinates, indexes[0] contains x-coordinates of Bragg peaks and indexes[1] the corresponding y-coordinates. |
required |
bragg
|
int
|
Bragg flag, choose between return only peaks, only background or both (bypass masking of peaks). |
required |
n
|
int
|
Number of pixels to build a 2*n box around the Bragg peaks. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
surrounding_mask |
ndarray
|
Corresponding mask according to bragg flag choice. It contains zeros for unvalid pixels and one for valid pixels. Mask shape is the same size of data. |
Source code in bblib/utils.py
ring_mask(data, center, inner_radius, outer_radius)
Make a ring mask for the data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
ndarray
|
Image in which mask will be shaped. |
required |
center
|
tuple
|
Center coordinates (xc,yc) of the concentric rings for the mask. |
required |
inner_radius
|
int
|
Inner radius of the mask, in pixels. |
required |
outer_radius
|
int
|
Outer radius of the mask, in pixels. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
mask |
ndarray
|
Mask array containg zeros (pixels to be masked) and ones (valid pixels). |
Source code in bblib/utils.py
visualize_single_panel(data, transformation_matrix, ss_in_rows)
Creates a visulization array for single panel detectors after applying the detector geometry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
ndarray
|
Image in which mask will be shaped |
required |
transformation_matrix
|
ndarray
|
A 2x2 transformation matrix used to map from fast-scan/slow-scan to x/y coordinates. |
required |
ss_in_rows
|
bool
|
If True, the slow-scan axis is mapped to rows; otherwise to columns. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: The transformed visualization array. |