srxraylib.util package

Submodules

srxraylib.util.chemical_formula module

Utilities for parsing and computing properties of chemical formulas.

class srxraylib.util.chemical_formula.ChemicalFormulaParser[source]

Bases: object

classmethod parse_formula(formula)[source]
class srxraylib.util.chemical_formula.Element(symbol, name, atomicnumber, molweight)[source]

Bases: object

addsyms(weight, result)[source]
getweight()[source]
class srxraylib.util.chemical_formula.ElementInFormula(element, atomic_number, n_atoms, molecular_weight)[source]

Bases: object

class srxraylib.util.chemical_formula.ElementSequence(*seq)[source]

Bases: object

addsyms(weight, result)[source]
append(thing)[source]
displaysyms()[source]
getsyms()[source]
getweight()[source]
set_count(n)[source]
class srxraylib.util.chemical_formula.Tokenizer(input)[source]

Bases: object

error(msg)[source]
gettoken()[source]
srxraylib.util.chemical_formula.build_dict(s)[source]
srxraylib.util.chemical_formula.parse(s)[source]
srxraylib.util.chemical_formula.parse_sequence()[source]

srxraylib.util.custom_distribution module

Custom probability distribution sampler via inverse CDF.

class srxraylib.util.custom_distribution.CustomDistribution(pdf, sort=False, interpolation=False, transform=<function CustomDistribution.<lambda>>, seed=0)[source]

Bases: object

draws samples from a one dimensional probability distribution, by means of inversion of a discrete inverstion of a cumulative density function

the pdf can be sorted first to prevent numerical error in the cumulative sum this is set as default; for big density functions with high contrast, it is absolutely necessary, and for small density functions, the overhead is minimal

a call to this distibution object returns indices into density array

property ndim
property sum

cached sum of all pdf values; the pdf need not sum to one, and is imlpicitly normalized

srxraylib.util.data_structures module

Utility to mimic IGOR-like vector and matrix classes and functions.

class srxraylib.util.data_structures.ScaledArray(np_array=array([], dtype=float64), scale=array([], dtype=float64))[source]

Bases: object

Stores a 1D array and the abscissas information. Constructor.

Parameters:
  • np_array (numpy array) – The array to be stored.

  • scale (float) – The scale values (np_array and scale must have the same dimension).

delta()[source]

Returns the delta (step) of the array.

Return type:

float

get_abscissas()[source]

Return an array with the abscissas.

Return type:

numpy array

get_scale_value(index)[source]

Returns the scale at a particular index.

Parameters:

index (int) –

Return type:

float

get_value(index)[source]

Gets the value for a given index.

Parameters:

index (int) –

Return type:

float

get_values()[source]

Returns the array.

Return type:

numpy array

classmethod initialize(np_array=array([], dtype=float64))[source]

Initializes a ScaledArray instance.

Parameters:

np_array (numpy array) – The array to be stored.

Return type:

ScaledArray instance

classmethod initialize_from_range(np_array, min_scale_value, max_scale_value)[source]
Parameters:
  • np_array (numpy array) – The array to be stored.

  • min_scale_value (float) –

  • max_scale_value (float) –

Return type:

ScaledArray instance

classmethod initialize_from_steps(np_array, initial_scale_value, scale_step)[source]
Parameters:
  • np_array (numpy array) – The array to be stored.

  • initial_scale_value (float) –

  • scale_step (float) –

Return type:

A ScaledArray instance.

interpolate_scale_value(value)[source]

Interpolate only for monotonic np_array.

Parameters:

value (float) –

Return type:

float

interpolate_value(scale_value)[source]

Get the interpolated value for a given scale or abscissas value.

Parameters:

scale_value (int) –

Return type:

float

interpolate_values(scale_values)[source]

Get the interpolated values for given scale or abscissas values.

Parameters:

scale_values (numpy array) –

Return type:

numpy array

offset()[source]

Returns the offset of the array.

Return type:

float

set_scale_from_range(min_scale_value, max_scale_value)[source]

Equivalent to the IGOR command: SetScale /I (wave, min value, max value).

Parameters:
  • min_scale_value (float) –

  • max_scale_value (float) –

set_scale_from_steps(initial_scale_value, scale_step)[source]

Equivalent to the IGOR command: SetScale /P (wave, offset, step).

Parameters:
  • initial_scale_value (float) –

  • scale_step (float) –

set_value(index, value)[source]

Sets a value at a particular index.

Parameters:
  • index (int) –

  • value (float) –

set_values(value)[source]

Sets an array.

Parameters:

value (numpy array) –

size()[source]

Returns the size of the array.

Return type:

int

class srxraylib.util.data_structures.ScaledMatrix(x_coord=array([], dtype=float64), y_coord=array([], dtype=float64), z_values=array([], shape=(0, 0), dtype=float64), interpolator=False)[source]

Bases: object

ScaledMatrix stores a 2D array and its axes. Constructor.

Parameters:
  • x_coord (numpy array) – the array for X.

  • y_coord (numpy array) – the array for Y.

  • z_values (numpy array) – the 2D array for Z.

  • interpolator (boolean, optional) – True means that the interpolator has been calculates and it is ready. False means that is not ready. interpolator=True means that the interpolator is up to date and stored in the interpolated value. interpolator must be changed to False when something of the data is changed. When interpolation is required, compute_interpolator() is called. Withouth loss of generality, Wavefront2D can always be initialize with interpolator=False. Then it will be switched on when needed. It has been implemented for saving time: the interpolator is recomputed only if needed.

compute_interpolator()[source]

Calculates and stores the interpolation object.

delta_x()[source]

Returns the delta (step) of the X axis.

Return type:

int

delta_y()[source]

Returns the delta (step) of the Y axis.

Return type:

float

get_x_value(index)[source]

Returns the X value for a given index.

Parameters:

index (int) –

Return type:

float

get_x_values()[source]

Returns the X array.

Return type:

numpy array

get_y_value(index)[source]

Returns the Y value for a given index.

Parameters:

index (int) –

Return type:

float

get_y_values()[source]

Returns the Y array.

Return type:

numpy array

get_z_value(x_index, y_index)[source]

Returns the Z (2D array) value for given X,Y indices.

Parameters:
  • x_index (int) – The index for axis 0.

  • y_index (int) – The index for axis 1.

Return type:

float

get_z_values()[source]

Returns the Z (2D array) array.

Return type:

numpy array

classmethod initialize(np_array_z=array([], shape=(0, 0), dtype=float64), interpolator=False)[source]

Initialize a ScaledMatrix instance from a 2D array. :param np_array_z: The 2D array. :type np_array_z: numpy array :param interpolator: True means that the interpolator has been calculates and it is ready. False means that is not ready. :type interpolator: boolean, optional

Return type:

ScaledMatrix instance

classmethod initialize_from_range(np_array, min_scale_value_x, max_scale_value_x, min_scale_value_y, max_scale_value_y, interpolator=False)[source]

Initializes a ScaledMatrix instance from a 2D array and the range of the axes.

Parameters:
  • np_array (numpy array) – The 2D array.

  • min_scale_value_x (float) –

  • max_scale_value_x (float) –

  • min_scale_value_y (float) –

  • max_scale_value_y (float) –

  • interpolator (boolean, optional) – True means that the interpolator has been calculates and it is ready. False means that is not ready.

Return type:

ScaledMatrix instance

classmethod initialize_from_steps(np_array, initial_scale_value_x, scale_step_x, initial_scale_value_y, scale_step_y, interpolator=False)[source]

Initializes a ScaledMatrix instance from a 2D array and the steps of the axes.

Parameters:
  • np_array (numpy array) – The 2D array.

  • initial_scale_value_x (float) –

  • scale_step_x (float) –

  • initial_scale_value_y (float) –

  • scale_step_y (float) –

  • interpolator (boolean, optional) – True means that the interpolator has been calculates and it is ready. False means that is not ready.

Return type:

ScaledMatrix instance

interpolate_value(x_coord, y_coord)[source]

Gives the Z value interpolated at given coordinates (x_coord, y_coord).

Parameters:
  • x_coord (float) – The coordinate at axis 0.

  • y_coord (float) – The coordinate at axis 1.

Return type:

float

is_complex_matrix()[source]

Returns True if the data stored is of complex type.

Return type:

boolean

offset_x()[source]

returns the offset of the X axis.

Return type:

float

offset_y()[source]

Returns the offset of the Y axis.

Return type:

float

set_scale_from_range(axis, min_scale_value, max_scale_value)[source]

Equivalent to the IGOR command: SetScale /I (wave, min value, max value).

Parameters:
  • axis (int) –

  • min_scale_value (float) –

  • max_scale_value (float) –

set_scale_from_steps(axis, initial_scale_value, scale_step)[source]

Equivalent to the IGOR command: SetScale /P (wave, min value, max value).

Parameters:
  • axis (int) –

  • initial_scale_value (float) –

  • scale_step (float) –

set_z_value(x_index, y_index, z_value)[source]

Sets a given Z value at given indices.

Parameters:
  • x_index (int) – The index for axis 0.

  • y_index (int) – The index for axis 1.

  • z_value (float) – The value to be stored.

set_z_values(new_value)[source]

Sets a given 2D array.

Parameters:

new_value (numpy array) – The 2D array.

shape()[source]

Returns the shape.

Return type:

tuple

size()[source]

Returns the size of the array.

Return type:

int

size_x()[source]

Returns the size of the X axis.

Return type:

int

size_y()[source]

Returns the size of the Y axis.

Return type:

int

x_coord = None
y_coord = None
z_values = None

srxraylib.util.h5_simple_writer module

srxraylib.util.histograms module

Histogram utilities for beam and profile data analysis.

srxraylib.util.histograms.get_average(histogram, bins, ret0=None)[source]
srxraylib.util.histograms.get_fwhm(histogram, bins, ret0=None)[source]
srxraylib.util.histograms.get_rms(histogram, bins, ret0=None)[source]
srxraylib.util.histograms.get_sigma(histogram, bins, ret0=None)[source]

srxraylib.util.inverse_method_sampler module

Classes for creating random points following a given numeric distribution using the inverse method. Covers 1D, 2D and 3D sampling.

See tests for examples of use.

class srxraylib.util.inverse_method_sampler.Sampler1D(pdf, pdf_x=None, cdf_interpolation_factor=1)[source]

Bases: object

Constructor.

Parameters:
  • pdf (numpy array) – 1D input probability distrubution function.

  • pdf_x (numpy array) – the abscissas of the odf.

  • cdf_interpolation_factor (float, optional) – interpolation factor for calculating the cdf (1 makes no interpolation)/

abscissas()[source]

Gets the abscissas array.

Returns:

The abscissas array (referenced, not copied).

Return type:

numpy array

cdf()[source]

Gets the cumulative distribution function (cdf).

Returns:

The cdf (referenced, not copied).

Return type:

numpy array

cdf_abscissas()[source]

Gets the abscissas of the cumulative distribution function (cdf).

Returns:

The cdf abscissas (referenced, not copied).

Return type:

numpy array

get_n_sampled_points(npoints, seed=None)[source]

Returns a given number points sampled points sampled with the pdf.

Parameters:
  • npoints (int) – The number of points.

  • seed (int, optional) – The seed (numpy generator is initialized with numpy.random.default_rng(seed))

Returns:

The sampled points.

Return type:

numpy array

get_n_sampled_points_and_histogram(npoints, bins=51, range=None, seed=None)[source]

Returns a given number points sampled points sampled with the pdf and the histogram.

Parameters:
  • npoints (int) – The number of points.

  • seed (int, optional) – The seed (numpy generator is initialized with numpy.random.default_rng(seed))

  • bins (int, optional) – Number of bins

  • range (list or tuple) – [min, max] the histogram limits.

Returns:

(s1, h, bin_edges) s1: the points sampled with the current pdf. The number of points is equal to the dimension of random_in_0_1, h: the array with the histogram values at the bin edges, bin_edges: the bin edges.

Return type:

tuple

get_sampled(random_in_0_1)[source]

Return an array with sampled points.

Parameters:

random_in_0_1 (float or numpy array) – Points sampled in a uniform interval.

Returns:

the points sampled with the current pdf. The number of points is equal to the dimension of random_in_0_1.

Return type:

numpy array

get_sampled_and_histogram(random_in_0_1, bins=51, range=None)[source]

Return an array with sampled points and the histogram.

Parameters:

random_in_0_1 (float or numpy array) –

Points sampled in a uniform interval. bins : int, optional

Number of bins

rangelist or tuple

[min, max] the histogram limits.

Returns:

(s1, h, bin_edges) s1: the points sampled with the current pdf. The number of points is equal to the dimension of random_in_0_1, h: the array with the histogram values at the bin edges, bin_edges: the bin edges.

Return type:

tuple

pdf()[source]

Gets the array with probability distribution function (pdf).

Returns:

The pdf array (referenced, not copied).

Return type:

numpy array

class srxraylib.util.inverse_method_sampler.Sampler2D(pdf, pdf_x0=None, pdf_x1=None)[source]

Bases: object

Constructor.

Parameters:
  • pdf (numpy array) – the 2D pdf.

  • pdf_x0 (numpy array) – A 1D array with the abscissas for axis 0.

  • pdf_x1 (numpy array) – A 1D array with the abscissas for axis 1.

abscissas()[source]

Gets the abscisas arrays.

Returns:

(x0, x1) The arrays for axes 0 and 1.

Return type:

tuple

cdf()[source]

Gets the array with cumulated distribution function (cdf).

Returns:

The cdf array (referenced, not copied).

Return type:

numpy array

get_n_sampled_points(npoints, seed=None)[source]

Samples n points (two coordinates) following the given pdf.

Parameters:
  • npoints (int) – The number of points.

  • seed (int, optional) – The seed (numpy generator is initialized with numpy.random.default_rng(seed))

Returns:

(x,y) the coordinates x (float or array) and y (float or array) of the sampled point(s).

Return type:

tuple

get_n_sampled_points_x2(npoints, seed=None)[source]

Samples n points (two coordinates, two times the second axis) following the given pdf.

Parameters:
  • npoints (int) – The number of points.

  • seed (int, optional) – The seed (numpy generator is initialized with numpy.random.default_rng(seed))

Returns:

(x, y0, y1) the coordinates x (float or array) on the axis 0 and and y0, y1 (float or array) of the sampled point(s) on axis 1.

Return type:

tuple

get_sampled(random0, random1)[source]

Samples a point or multiple points in 2D (two coordinates) following the given pdf.

Parameters:
  • random0 (float or numpy array) – The 1D array with values unifiormly samples in [0,1]

  • random1 (float or numpy array) – The 1D array with values unifiormly samples in [0,1]

Returns:

(x,y) the coordinates x (float or array) and y (float or array) of the sampled point(s).

Return type:

tuple

get_sampled_x2(random0, random10, random11)[source]

Samples a point or multiple points in 2D (two coordinates) following the given pdf. It samples one point in axis 0 and two points on the axis 1.

Parameters:
  • random0 (float or numpy array) – The 1D array with values unifiormly samples in [0,1]

  • random10 (float or numpy array) – The 1D array with values unifiormly samples in [0,1]

  • random11 (float or numpy array) – The 1D array with values unifiormly samples in [0,1]

Returns:

(x, y0, y1) the coordinates x (float or array) on the axis 0 and and y0, y1 (float or array) of the sampled point(s) in axis 1.

Return type:

tuple

pdf()[source]

Gets the array with probability distribution function (pdf).

Returns:

The pdf array (referenced, not copied).

Return type:

numpy array

class srxraylib.util.inverse_method_sampler.Sampler3D(pdf, pdf_x0=None, pdf_x1=None, pdf_x2=None)[source]

Bases: object

Constructor.

Parameters:
  • pdf (numpy array) – The 3D pdf.

  • pdf_x0 (numpy array) – The abscissas for axis 0.

  • pdf_x1 (numpy array) – The abscissas for axis 1.

  • pdf_x2 (numpy array) – The abscissas for axis 2.

abscissas()[source]

Gets the arrays with the abscissas.

Returns:

(x0, x1, x2) The arrays with the abscissas for axes 1, 2, and 3.

Return type:

tuple

cdf()[source]

Gets the array with cumulative distribution function (cdf).

Returns:

The cdf array (referenced, not copied).

Return type:

numpy array

get_n_sampled_points(npoints, seed=None)[source]

Get a given number of sampled 3D points.

Parameters:
  • npoints (int) – The number of points.

  • seed (int, optional) – The seed (numpy generator is initialized with numpy.random.default_rng(seed))

Returns:

(x,y,z) the coordinates x (float or array), y (float or array) and z (float or array) of the sampled point(s).

Return type:

tuple

get_sampled(random0, random1, random2)[source]

Get sampled 3D points.

Parameters:
  • random0 (float or numpy array) – The points or points sampled uniformly in a [0,1] interval.

  • random1 (float or numpy array) – The points or points sampled uniformly in a [0,1] interval.

  • random2 (float or numpy array) – The points or points sampled uniformly in a [0,1] interval.

Returns:

(x0,x1,x2) the coordinates (float or array) of the sampled points.

Return type:

tuple

pdf()[source]

Gets the array with probability distribution function (pdf).

Returns:

The pdf array (referenced, not copied).

Return type:

numpy array

srxraylib.util.random_distributions module

Random number generation with arbitrary probability distributions.

class srxraylib.util.random_distributions.AuxiliaryContainer[source]

Bases: object

class srxraylib.util.random_distributions.Container(dims)[source]

Bases: object

clone()[source]
copy_from(other)[source]
min_max()[source]
class srxraylib.util.random_distributions.Container1D(length)[source]

Bases: Container

cell_iter()[source]
index_iter()[source]
square_iter(x, n)[source]
window_iter(x1, x0)[source]
wrapped_square_iter(x, n)[source]
wrapped_window_iter(x1, x0)[source]
class srxraylib.util.random_distributions.Container2D(width, height)[source]

Bases: Container

cell_iter()[source]
index_iter()[source]
square_index_iter(p, n)[source]
square_iter(p, n)[source]
window_index_iter(p0, p1)[source]
window_iter(p0, p1)[source]
wrapped_square_index_iter(p, n)[source]
wrapped_square_iter(p, n)[source]
wrapped_window_index_iter(p0, p1)[source]
wrapped_window_iter(p0, p1)[source]
class srxraylib.util.random_distributions.Container3D(width, height, depth)[source]

Bases: Container

cell_iter()[source]
index_iter()[source]
square_iter(p, n)[source]
window_iter(p0, p1)[source]
wrapped_square_iter(p, n)[source]
wrapped_window_iter(p0, p1)[source]
class srxraylib.util.random_distributions.Distribution2D(prob_matrix, min_corner, max_corner)[source]

Bases: object

get_samples(N, seed=0)[source]
make_distribution(matrix)[source]
class srxraylib.util.random_distributions.Grid1D(dims, initial_item=None)[source]

Bases: Container1D, PrincipleContainer

cell_iter()[source]
class srxraylib.util.random_distributions.Grid2D(dims, initial_item=None)[source]

Bases: Container2D, PrincipleContainer

cell_iter()[source]
class srxraylib.util.random_distributions.Grid3D(dims, initial_item=None)[source]

Bases: Container3D, PrincipleContainer

class srxraylib.util.random_distributions.GridBar3D(grid, x, y, z)[source]

Bases: Container1D, AuxiliaryContainer

class srxraylib.util.random_distributions.GridCol2D(grid, col, row_slice)[source]

Bases: Container1D, AuxiliaryContainer

class srxraylib.util.random_distributions.GridCol3D(grid, x, y, z)[source]

Bases: Container1D, AuxiliaryContainer

class srxraylib.util.random_distributions.GridRow2D(grid, col_slice, row)[source]

Bases: Container1D, AuxiliaryContainer

class srxraylib.util.random_distributions.GridRow3D(grid, x, y, z)[source]

Bases: Container1D, AuxiliaryContainer

class srxraylib.util.random_distributions.GridSliceXY(grid, x, y, z)[source]

Bases: Container2D, AuxiliaryContainer

class srxraylib.util.random_distributions.GridSliceXZ(grid, x, y, z)[source]

Bases: Container2D, AuxiliaryContainer

class srxraylib.util.random_distributions.GridSliceYZ(grid, x, y, z)[source]

Bases: Container2D, AuxiliaryContainer

class srxraylib.util.random_distributions.GridWindow1D(grid, col_slice)[source]

Bases: Container1D, AuxiliaryContainer

class srxraylib.util.random_distributions.GridWindow2D(grid, x, y)[source]

Bases: Container2D

class srxraylib.util.random_distributions.GridWindow3D(grid, x, y, z)[source]

Bases: Container3D, AuxiliaryContainer

class srxraylib.util.random_distributions.ListGrid2D(dims)[source]

Bases: Grid2D

additem(p, b)[source]
class srxraylib.util.random_distributions.ListGrid3D(dims)[source]

Bases: Grid3D

additem(p, b)[source]
class srxraylib.util.random_distributions.NormalisedInputCurve(curve)[source]

Bases: object

class srxraylib.util.random_distributions.PrincipleContainer[source]

Bases: object

class srxraylib.util.random_distributions.RandomQueue[source]

Bases: object

empty()[source]
pop()[source]
push(x)[source]
class srxraylib.util.random_distributions.ResponseCurve(inputMin, inputMax, outputSamples)[source]

Bases: object

getInputMax()[source]
getInputMin()[source]
class srxraylib.util.random_distributions.XYResponseCurve(inputSamples, outputSamples)[source]

Bases: object

findInputIndex(input)[source]
makeInverse()[source]
srxraylib.util.random_distributions.complete_slice(s, length)[source]
srxraylib.util.random_distributions.distribution_from_grid(grid, x_cells, y_cells)[source]
srxraylib.util.random_distributions.int_point_2d(p)[source]
srxraylib.util.random_distributions.int_point_3d(p)[source]
srxraylib.util.random_distributions.is_slice(s)[source]
srxraylib.util.random_distributions.lerp(value, inputMin, inputMax, outputMin, outputMax)[source]
srxraylib.util.random_distributions.line(value, inputMin, inputMax, outputMin, outputMax)[source]
srxraylib.util.random_distributions.make_distribution_curve(inputSamples, outputSamples)[source]
srxraylib.util.random_distributions.make_grid_1d(width, initial_item=None)[source]
srxraylib.util.random_distributions.make_grid_2d(width, height, initial_item=None)[source]
srxraylib.util.random_distributions.make_grid_3d(width, height, depth, initial_item)[source]
srxraylib.util.random_distributions.points_to_grid(points, dimensions)[source]
srxraylib.util.random_distributions.points_to_grid_3d(points, dimensions)[source]
srxraylib.util.random_distributions.ramp(value, inputMin, inputMax, outputMin, outputMax)[source]
srxraylib.util.random_distributions.sigmoid(value, inputMin, inputMax, outputMin, outputMax)[source]
srxraylib.util.random_distributions.signum(x)[source]
srxraylib.util.random_distributions.slice_len(s, length)[source]
srxraylib.util.random_distributions.slice_mul(slice1, slice2, length)[source]
srxraylib.util.random_distributions.srange(s, length)[source]

srxraylib.util.threading module

Threading utilities for parallel calculations.

class srxraylib.util.threading.Singleton(decorated)[source]

Bases: object

Instance(*args, **kws)[source]
srxraylib.util.threading.synchronized_method(method)[source]

Module contents