utils module

Utilities functions for trx.

utils.convert_units(value, unit, target_unit)[source]

Converts the value to the given target units.

The base units used are the SI units.

Parameters:
  • value (float) – The value to be converted.

  • unit (str) – The unit the provided value is given into. For instance, ‘m’, ‘eV’, ‘g/L’, J/m^2, m.s^(-1), m.s^{-1}, J/m^2/s.

  • target_unit (str) – The unit of the value to be returned.

utils.get_ai(energy, distance, center, pixel=None, detector=None, binning=None)[source]

Initialize pyFAI azimuthal integrator obj.

Parameters:
  • energy (float) – X-ray photon energy (eV).

  • distance (float) – Sample-to-detector distance (m).

  • center (tuple) – Coordinates of the image center (hor, ver) (pixel).

  • pixel (float or tuple or None, optional) – Pixel size (m). If tuple, first value is the horizontal pizel size and second value is the vertical pixel size. Default is None.

  • detector (str or None, optional) – Detector nickname. Default is None.

  • binning (tuple or None, optional) – Detector binning (hor, ver). Default is None.

Returns:

pyfai_ai – pyFAI ai

Return type:

pyFAI azimuthal integrator obj

Examples

>>> ai = get_ai(18e3, 0.05, detector='rayonix', binning=(2, 2))
utils.get_exponent_and_mantissa(f)[source]

Get exponent and mantissa from float.

utils.get_fnames(pattern, folder='./')[source]

Get sorted list of file names matching a pattern.

utils.load_hdf5_as_dict(h5fname, h5path='/', verbose=False)[source]

Read a HDF5 file and return a nested dictionary.

Parameters:
  • h5fname (str) – HDF5 file name.

  • h5path (str, optional) – Name of HDF5 group to use as dictionary root level. Default is ‘/’.

  • Credits

  • -------

  • silx.io.dictdump.h5todict (-)

utils.load_image(fname)[source]

Load image using fabio.

utils.load_mask(fname)[source]

Load mask using fabio and check if pyFAI compatible.

utils.save_dict_as_hdf5(d, h5fname, h5path='/', create_dataset_args=None, verbose=False)[source]

Save dictionary to hdf5 file.

Strings, scalars and array_like with size <= 2 are saved as attributes. Everything else is saved as dataset. If sub-dictionaries exist, corresponding sub-groups are created.

Parameters:
  • d (dict) – Data dictionary.

  • h5fname (str) – Output file name.

  • h5path (str, optional) – Target path in HDF5 file in which groups are created. Default is ‘/’.

  • create_dataset_args (dict or None, optional) – Parameters to pass to hdf5.create_dataset. This allows to specify filters and compression parameters. Default is None.

  • Credits

  • -------

  • silx.io.dictdump.dicttoh5 (-)

  • (https (- M. Cammarata)

Example

>> save_dict_as_hdf5(mydict, ‘test.h5’,

create_dataset_args={‘compression’: ‘gzip’, ‘compression_opts’: 9))

utils.sort_string_delays(delays, digits=None, return_indices=False)[source]

Sort string delays in ascending order.

Parameters:
  • delays (array-like) – Array of strings.

  • return_indices (bool) – If True, returns also the indices of the original array arranged according to the new, sorted array. Can be used to sort data as well later on.

Returns:

  • sorted_delays (array-like) – Array of strings.

  • sorted_indices (array-like) – Array of integer giving th position of the old array elements in the new, sorted array.

Notes

  • If a string that cannot be converted to float is present (e.g. ‘off’), it is placed at the beginning of the array.

utils.str2t(delay)[source]

Convert delay from string to float.

Parameters:

delay (str or array-like) – String delay.

Returns:

t – Float delay.

Return type:

float or array_like

utils.t2str(delay, digits=None, decimals=None, strip_spaces=True)[source]

Convert delay from float to str.

Parameters:
  • delay (float or array-like) – Float delay.

  • digits (int or None, optional) – Number of significant digits of the number without units.

  • decimals (int or None, optional) – Number of digits after the dot.

Returns:

t – String delay.

Return type:

str or array_like