compatibility

module0_flow.util.compat.assert_compat_version(version, other_version)[source]

Raises an AssertionError if the requested version (version) is not compatible with other_version. Compatibility requires:

  • major versions are eqal

  • minor version of version is less than or equal to other_version

Parameters:
  • version – version str formatted 'major.minor.subminor'

  • other_version – version str formatted 'major.minor.subminor'

Returns:

None

units

Provides a common set of units for Module 0 data. Base units are:

us == mm == keV == e == K == 1

To convert from an external source into Module 0 flow units, multiply:

import module0_flow.util.units as units

ext_val = 1 # m/s
module0_val = ext_val * (units.m / units.s)
module0_val # 0.001 mm/us

To export a number from Module 0 flow units to a particular unit system, divide:

import module0_flow.util.units as units

module0_val = 0.05 # kV/mm
ext_val = module0_val / (units.kV / units.cm)
ext_val # 0.5 kV/cm
module0_flow.util.units.GeV
module0_flow.util.units.K = 1
module0_flow.util.units.MeV
module0_flow.util.units.V
module0_flow.util.units.cm
module0_flow.util.units.e = 1
module0_flow.util.units.eV
module0_flow.util.units.g = 1
module0_flow.util.units.kV
module0_flow.util.units.keV = 1
module0_flow.util.units.kg
module0_flow.util.units.km
module0_flow.util.units.m
module0_flow.util.units.mV
module0_flow.util.units.mm = 1
module0_flow.util.units.ms
module0_flow.util.units.ns
module0_flow.util.units.s
module0_flow.util.units.us = 1

fast lookup tables

class module0_flow.util.lut.LUT(dtype, *min_max_keys, default=None, shape=None)

Bases: object

Creates a lookup table that can be used to quickly access data based on tuples of integers. Works best if keys are contiguous within each position of the tuple. E.g.:

key0 = [0,1,2]
key1 = [30,31,32]

is 10x more memory efficient than:

key0 = [10,20,30]
key1 = [300,310,320]

Initialize with tuples of min and max values for each of the used key values:

key0 = [0,1,2,3]
key1 = [5,6,7,8]
shape = (2,)
dtype = 'f8'
lut = LUT(dtype, (min(key0), max(key0)), (min(key1), max(key1)), shape=shape)

Data can then be stored in the table using a tuple of key arrays:

lut[(key0,key1)] = np.array([[0,0],[1,1],[2,2],[3,3]])

and accessed:

lut[(key0,key1)] # np.array([[0,0],[1,1],[2,2],[3,3]])

A default value should be set for keys that are not found in the table:

lut.default = np.array([-1,-1])
lut[([0],[0])] # np.array([-1,-1])
clear(*keys)

Remove stored value for specified keys

Parameters:

*keys

arrays of key values, shape: (N,)

compress(sel=tuple())
Parameters:

sel – optional, for multi-dimensional LUT data apply this selection to the returned data

Returns:

compressed array of entry data that has been filled

property default

Default value to return if key not found in table. Datatype is same as lookup table

static from_array(meta_arr, data_arr)

Convert an array-based representation of a lookup table (as returned by LUT.to_array()) to a LUT object.

Parameters:
  • meta_arr – array containing meta data

  • data_arr – array containing lookup table data

Returns:

LUT object

hash(*keys)

Generate a hash index from key value arrays

Parameters:

*keys

arrays of each key value, shape: (N,)

Returns:

array of hash index, shape: (N,)

keys()

Return existing keys

Returns:

tuple of arrays, each shape: (N,)

max(sel=tuple())
Parameters:

sel – optional, for multi-dimensional LUT data apply this selection to the returned data

Returns:

maximum value of compressed LUT data

min(sel=tuple())
Parameters:

sel – optional, for multi-dimensional LUT data apply this selection to the returned data

Returns:

minimum value of compressed LUT data

property nbytes
Returns:

number of bytes used by underlying arrays

to_array()

Generate an array-based representation of a LUT object. Returns two arrays. The first has a datatype:

dtype([('min_max_keys', 'i8', ({nkeys}, 2)), ('default', {data_dtype}, {data_shape})])

and shape: (1,). This contains meta-data needed to reconstruct the LUT hashing function and the default value. The second has a datatype:

dtype([('data', {dtype}, {data_shape}), ('filled', bool, {data_shape})])

and shape: (N,). This represents the data stored in the lookup table.

Returns:

tuple of meta-array and data-array as described above

module0_flow.util.lut.read_lut(data_manager, path, name=None)
module0_flow.util.lut.write_lut(data_manager, path, lut, name=None)

other utility functions

module0_flow.util.func.condense_array(arr, mask)

Densify a masked array on last axis, throwing out invalid values (up to the size needed to keep the array regular). E.g.:

mask = [[False, True, True],
        [False, False, True],
        [True, False, True]]

will condense a 3x3 array to shape: (3, 2) and produce a final mask of:

new_mask = [[False, True],
            [False, False],
            [False, True]]

Note that this operation does not have an inverse.

Parameters:
  • arr – array shape: (..., N, M)

  • mask – boolean array shape: (..., N, M), True == invalid

Returns:

array shape: (..., N, m)

module0_flow.util.func.mode(arr)

Finds the most common element along the last dimension

Parameters:

arr – array shape: (..., N)

Returns:

array shape: (..., 1)