tfields.lib package¶
Submodules¶
tfields.lib.decorators module¶
Function decoration
- class tfields.lib.decorators.cached_property(ttl=0)[source]¶
Bases:
object
Decorator for read-only properties evaluated only once within TTL period.
It can be used to create a cached property like this:
import random # the class containing the property must be a new-style class class MyClass(object): # create property whose value is cached for ten minutes @cached_property(ttl=600) def randint(self): # will only be evaluated every 10 min. at maximum. return random.randint(0, 100)
The value is cached in the ‘_cache’ attribute of the object instance that has the property getter method wrapped by this decorator. The ‘_cache’ attribute value is a dictionary which has a key for every property of the object which is wrapped by this decorator. Each entry in the cache is created only when the property is accessed for the first time and is a two-element tuple with the last computed property value and the last time it was updated in seconds since the epoch.
The default time-to-live (TTL) is zero seconds. Set the TTL to zero for the cached value to never expire.
To expire a cached property value manually just do:
del instance._cache[<property name>]
tfields.lib.grid module¶
- tfields.lib.grid.base_vectors(array, rtol=None, atol=None)[source]¶
describe the array in terms of base vectors Inverse function of igrid
Examples
>>> import tfields >>> grid = tfields.lib.grid.igrid((3, 5, 5j)) >>> tfields.lib.grid.base_vectors(grid[:, 0]) (3.0, 5.0, 5j) >>> grid2 = tfields.lib.grid.igrid((3, 5, 5j), ... (1, 2, 2j)) >>> grid_circle = tfields.lib.grid.igrid( ... *tfields.lib.grid.base_vectors(grid2)) >>> assert tfields.Tensors(grid_circle).equal(grid2)
- tfields.lib.grid.change_iter_order(bv_lengths: List[int], iter_order: List[int], iter_order_new: List[int])[source]¶
- Parameters:
bv_lengths – lenghts of basis vectors
- Returns:
indices for changeing fields generated with iter_order to iter_order_new
- tfields.lib.grid.compare_permutations(permut1, permut2)[source]¶
Return what rows you need to switch in order to make permut1 become permut2
Examples
>>> import tfields >>> a = [1, 2, 0, 4, 3] >>> b = [0, 1, 2, 3, 4] >>> si = tfields.lib.grid.compare_permutations(a, b) >>> si [(0, 2), (1, 2), (3, 4)] >>> tfields.lib.grid.swap_rows(a, *si) >>> a [0, 1, 2, 3, 4] >>> a == b True
- tfields.lib.grid.ensure_complex(*base_vectors) List[Tuple[float, float, complex]] [source]¶
Ensure, that the third entry in base_vector of type tuple becomes a complex type. The first two are mapped to float if they they are complex but with imag == 0.
- tfields.lib.grid.igrid(*base_vectors, **kwargs)[source]¶
- Parameters:
*base_vectors (tuple, list or np.array) –
base vectors spaning the grid behaviour for different input types:
tuple: will be transformed to slices and given to np.mgrid list or np.array: arbitrary base vectors
**kwargs –
- iter_order (list): order in which the iteration will be done.
Frequency rises with position in list. default is [0, 1, 2] iteration will be done like:
- for v0 in base_vectors[iter_order[0]]:
- for v1 in base_vectors[iter_order[1]]:
- for v2 in base_vectors[iter_order[2]]:
…
Examples
Initilaize using the mgrid notation >>> import numpy as np >>> import tfields >>> assert np.array_equal(tfields.lib.grid.igrid((0, 1, 2j), … (3, 4, 2j), … (6, 7, 2j)), … [[ 0., 3., 6.], … [ 0., 3., 7.], … [ 0., 4., 6.], … [ 0., 4., 7.], … [ 1., 3., 6.], … [ 1., 3., 7.], … [ 1., 4., 6.], … [ 1., 4., 7.]])
>>> assert np.array_equal(tfields.lib.grid.igrid([3, 4], ... np.linspace(0, 1, 2), ... (6, 7, 2), ... iter_order=[1, 0, 2]), ... [[ 3., 0., 6.], ... [ 3., 0., 7.], ... [ 4., 0., 6.], ... [ 4., 0., 7.], ... [ 3., 1., 6.], ... [ 3., 1., 7.], ... [ 4., 1., 6.], ... [ 4., 1., 7.]]) >>> assert np.array_equal(tfields.lib.grid.igrid(np.linspace(0, 1, 2), ... np.linspace(3, 4, 2), ... np.linspace(6, 7, 2), ... iter_order=[2, 0, 1]), ... [[ 0., 3., 6.], ... [ 0., 4., 6.], ... [ 1., 3., 6.], ... [ 1., 4., 6.], ... [ 0., 3., 7.], ... [ 0., 4., 7.], ... [ 1., 3., 7.], ... [ 1., 4., 7.]])
- tfields.lib.grid.swap_columns(array, *index_tuples)[source]¶
- Parameters:
array (list or array) –
arguments (expects tuples with indices to swap as) –
Examples
>>> import numpy as np >>> import tfields >>> l = np.array([[3, 2, 1, 0], [6, 5, 4, 3]]) >>> tfields.lib.grid.swap_columns(l, (1, 2), (0, 3)) >>> l array([[0, 1, 2, 3], [3, 4, 5, 6]])
- tfields.lib.grid.swap_rows(array, *args)[source]¶
- Parameters:
array (list) –
arguments (expects tuples with indices to swap as) –
Examples
>>> import tfields >>> l = [[3,3,3], [2,2,2], [1,1,1], [0, 0, 0]] >>> tfields.lib.grid.swap_rows(l, (1, 2), (0, 3)) >>> l [[0, 0, 0], [1, 1, 1], [2, 2, 2], [3, 3, 3]]
tfields.lib.io module¶
- tfields.lib.io.bytes_to_numpy(serialized_arr: bytearray) array [source]¶
Convert back from numpy_to_bytes
- tfields.lib.io.get_module_and_name(type_) Tuple[str, str] [source]¶
Examples
>>> import numpy as np >>> import tfields
This function can be used to ban your type to file as a string and (with the get_type method) get it back. >>> [tfields.lib.io.get_type(*tfields.lib.io.get_module_and_name(type_)) … for type_ in (int, np.ndarray, str)] [<class ‘int’>, <class ‘numpy.ndarray’>, <class ‘str’>]
- tfields.lib.io.get_type(module, name)[source]¶
Inverse to :fun:`get_module_and_name`
- tfields.lib.io.numpy_to_bytes(arr: array) bytearray [source]¶
Convert to bytest array
Examples
>>> import numpy as np >>> import tfields >>> a = np.ones((23, 23), dtype = 'int') >>> a_b = tfields.lib.io.numpy_to_bytes(a) >>> a1 = tfields.lib.io.bytes_to_numpy(a_b) >>> assert np.array_equal(a, a1) and a.shape == a1.shape and a.dtype == a1.dtype
tfields.lib.sets module¶
Algorithms around set operations
- class tfields.lib.sets.UnionFind[source]¶
Bases:
object
This algorithm and data structure are primarily used for Kruskal’s Minimum Spanning Tree algorithm for graphs, but other uses have been found.
The Union Find data structure is not a universal set implementation, but can tell you if two objects are in the same set, in different sets, or you can combine two sets. ufset.find(obja) == ufset.find(objb) ufset.find(obja) != ufset.find(objb) ufset.union(obja, objb)
- find(obj)[source]¶
Find the root of the set that an object ‘obj’ is in. If the object was not known, will make it known, and it becomes its own set. Object must be Python hashable.’’’
- tfields.lib.sets.disjoint_group_indices(iterator)[source]¶
Examples
>>> import tfields >>> tfields.lib.sets.disjoint_group_indices([[0, 0, 0, 'A'], [1, 2, 3], ... [3, 0], [4, 4, 4], [5, 4], ['X', 0.42]]) [[0, 1, 2], [3, 4], [5]] >>> tfields.lib.sets.disjoint_group_indices([[0], [1], [2], [3], [0, 1], [1, 2], [3, 0]]) [[0, 1, 2, 3, 4, 5, 6]]
- Returns:
indices of iterator items grouped in disjoint sets
- Return type:
list
- tfields.lib.sets.disjoint_groups(iterator)[source]¶
Disjoint groups implementation
Examples
>>> import tfields >>> tfields.lib.sets.disjoint_groups([[0, 0, 0, 'A'], [1, 2, 3], [3, 0], ... [4, 4, 4], [5, 4], ['X', 0.42]]) [[[0, 0, 0, 'A'], [1, 2, 3], [3, 0]], [[4, 4, 4], [5, 4]], [['X', 0.42]]] >>> tfields.lib.sets.disjoint_groups([[0], [1], [2], [3], [0, 1], [1, 2], [3, 0]]) [[[0], [1], [2], [3], [0, 1], [1, 2], [3, 0]]]
- Returns:
iterator items grouped in disjoint sets
- Return type:
list
- tfields.lib.sets.remap(arr: ndarray, keys: ndarray, values: ndarray, inplace=False) ndarray [source]¶
Given an input array, remap its entries corresponding to ‘keys’ to ‘values’
- Parameters:
input – array to remap
keys – values to be replaced
values – values to replace ‘keys’ with
- Returns:
like ‘input’, but with elements remapped according to the mapping defined by ‘keys’ and ‘values’
- Return type:
output
tfields.lib.stats module¶
Author: Daniel Boeckenhoff Mail: daniel.boeckenhoff@ipp.mpg.de
part of tfields library
- tfields.lib.stats.mode(array, axis=0, bins='auto', range=None)[source]¶
generalisation of the scipy.stats.mode function for floats with binning .. rubric:: Examples
Forwarding usage: >>> import tfields # NOQA >>> import numpy as np >>> tfields.lib.stats.mode([[2,2,3], [4,5,3]]) array([[2, 2, 3]]) >>> tfields.lib.stats.mode([[2,2,3], [4,5,3]], axis=1) array([[2],
[3]])
Float usage: >>> np.random.seed(seed=0) # deterministic random >>> n = np.random.normal(3.1, 2., 1000) >>> assert np.isclose(tfields.lib.stats.mode(n), [ 2.30838613]) >>> assert np.isclose(tfields.lib.stats.mode(n, bins=’sturges’), … [ 2.81321206]) >>> assert np.allclose(tfields.lib.stats.mode(np.array([n, n]), axis=1), … [[ 2.30838613], … [ 2.30838613]]) >>> tfields.lib.stats.mode(np.array([n, n]), axis=0).shape (1000, 1) >>> tfields.lib.stats.mode(np.array([n, n]), axis=1).shape (2, 1) >>> assert np.isclose(tfields.lib.stats.mode(np.array([n, n]), … axis=None), … [ 2.81321206])
- tfields.lib.stats.moment(a, moment=1, axis=0, weights=None, nan_policy='propagate')[source]¶
Calculate the nth moment about the mean for a sample. A moment is a specific quantitative measure of the shape of a set of points. It is often used to calculate coefficients of skewness and kurtosis due to its close relationship with them. :param a: data :type a: array_like :param moment: order of central moment that is returned. Default is 1. :type moment: int or array_like of ints, optional :param axis: Axis along which the central moment is computed. Default is 0.
If None, compute over the whole array a.
- Parameters:
nan_policy ({'propagate', 'raise', 'omit'}, optional) – Defines how to handle when input contains nan. ‘propagate’ returns nan, ‘raise’ throws an error, ‘omit’ performs the calculations ignoring nan values. Default is ‘propagate’.
- Returns:
n-th central moment – The appropriate moment along the given axis or over all values if axis is None. The denominator for the moment calculation is the number of observations, no degrees of freedom correction is done.
- Return type:
ndarray or float
See also
kurtosis
,skew
,describe
Notes
The k-th weighted central moment of a data sample is: .. math:
m_k = \frac{1}{\sum_{j = 1}^n w_i} \sum_{i = 1}^n w_i (x_i - \bar{x})^k
Where n is the number of samples and x-bar is the mean. This function uses exponentiation by squares [1] for efficiency.
References
Examples
>>> from tfields.lib.stats import moment >>> moment([1, 2, 3, 4, 5], moment=0) 1.0 >>> moment([1, 2, 3, 4, 5], moment=1) 0.0 >>> moment([1, 2, 3, 4, 5], moment=2) 2.0
Expansion of the scipy.stats moment function by weights: >>> moment([1, 2, 3, 4, 5], moment=1, weights=[-2, -1, 20, 1, 2]) 0.5
>>> moment([1, 2, 3, 4, 5], moment=2, weights=[5, 4, 3, 2, 1]) 2.0 >>> moment([1, 2, 3, 4, 5], moment=2, weights=[5, 4, 3, 2, 1]) 2.0 >>> assert moment([1, 2, 3, 4, 5], moment=2, ... weights=[0.25, 1, 17.5, 1, 0.25]) == 0.2 >>> moment([1, 2, 3, 4, 5], moment=2, weights=[0, 0, 1, 0, 0]) 0.0
tfields.lib.symbolics module¶
sympy helper functions
- tfields.lib.symbolics.split_expression(expr)[source]¶
Return the expression split up in the basic boolean functions.
- tfields.lib.symbolics.to_plane(expr)[source]¶
Tranlate the expression (coordinate form) to normal form and return as Plane .. rubric:: Examples
Get 3-d plane for linear equations >>> import sympy >>> from tfields.lib.symbolics import to_plane >>> x, y, z = sympy.symbols(‘x y z’) >>> eq1 = 2*x - 4 >>> p1 = to_plane(eq1) >>> assert eq1, p1.equation()
# multiple dimensions work >>> eq2 = x + 2*y + 3*z - 4 >>> p2 = to_plane(eq2) >>> assert eq2, p2.equation()
The base point is calculated independent of the coords >>> eq3 = 2*y + 3*z - 4 >>> p3 = to_plane(eq3) >>> assert eq3, p3.equation()
Inequalities will be treated like equations >>> ie1 = 2*y + 3*z > 4 >>> p4 = to_plane(ie1) >>> assert ie1.lhs - ie1.rhs, p4.equation()
Returns: sympy.Plane
- tfields.lib.symbolics.to_planes(expr)[source]¶
Resolve BooleanFunctions to retrieve multiple planes .. rubric:: Examples
>>> import sympy >>> from tfields.lib.symbolics import to_planes, to_plane >>> x, y, z = sympy.symbols('x y z') >>> eq1 = 2*x > 0 >>> eq2 = 2*y + 3*z <= 4 >>> p12 = to_planes(eq1 & eq2) >>> p1 = to_plane(eq1) >>> p12[0] == p1 True >>> p2 = to_plane(eq2) >>> p12[1] == p2 True
tfields.lib.util module¶
Various utility functions
- tfields.lib.util.duplicates(arr, axis=None)[source]¶
View1D version of duplicate search Speed up version after https://stackoverflow.com/questions/46284660 /python-numpy-speed-up-2d-duplicate-search/46294916#46294916
- Parameters:
arr (array_like) – array
args (other) – see np.isclose
Examples
>>> import tfields >>> import numpy as np >>> a = np.array([[1, 0, 0], [1, 0, 0], [2, 3, 4]]) >>> tfields.lib.util.duplicates(a, axis=0) array([0, 0, 2])
An empty sequence will not throw errors >>> assert np.array_equal(tfields.lib.util.duplicates([], axis=0), [])
- Returns:
int is pointing to first occurence of unique value
- Return type:
list of int
- tfields.lib.util.flatten(seq, container=None, keep_types=None, key: Optional[Callable] = None)[source]¶
Approach to flatten a nested sequence.
- Parameters:
seq (iterable) – iterable to be flattened
containter (iterable) – iterable defining an append method. Values will be appended there
keep_types (list of type) – types that should not be flattened but kept in nested form
key (callable) – callable with the signature key(iterable) -> iterable
Examples
>>> from tfields.lib.util import flatten >>> import numpy as np >>> flatten([[1,2,3],4,[[5,[6]]]]) [1, 2, 3, 4, 5, 6] >>> flatten([[1,2,3],4,[[5,[{6:1}]]]], keep_types=[dict]) [1, 2, 3, 4, 5, {6: 1}] >>> flatten([[1,2,3],4,[[5,[np.array([6])]]]], keep_types=[np.ndarray]) [1, 2, 3, 4, 5, array([6])]
Strings work although they have the __iter__ attribute in python3 >>> flatten([[0, 0, 0, ‘A’], [1, 2, 3]]) [0, 0, 0, ‘A’, 1, 2, 3]
Dictionaries will return flattened keys >>> flatten({“a”: 1, “b”: 2}) [‘a’, ‘b’]
You can use the key keyword to specify a transformation on the iterable: >>> flatten({“a”: {“a1”: 1, “a2”: 4}, “b”: 2}, key=dict.values) [1, 4, 2]
>>> def dict_flat_key(item): ... if isinstance(item, dict): ... return item.values() ... return item >>> flatten({"a": {"a1": 1, "a2": [3, 4]}, "b": 2}, key=dict_flat_key) [1, 3, 4, 2]
- tfields.lib.util.index(arr, entry, rtol=0, atol=0, equal_nan=False, axis=None)[source]¶
Examples
>>> import numpy as np >>> import tfields >>> a = np.array([[1, 0, 0], [1, 0, 0], [2, 3, 4]]) >>> tfields.lib.util.index(a, [2, 3, 4], axis=0) 2
>>> a = np.array([[1, 0, 0], [2, 3, 4]]) >>> tfields.lib.util.index(a, 4) 5
- Returns:
index of entry in arr
- Return type:
int
- tfields.lib.util.is_full_slice(index, shape)[source]¶
Determine if an index is the full slice (i.e. __getitem__ with this index returns the full array) w.r.t the shape given.
Examples
>>> import numpy as np >>> import tfields >>> class index_getter: ... def __getitem__(self, index): ... return index >>> get_index = index_getter() >>> a = np.array([[1, 0, 0], [1, 0, 0], [2, 3, 4]]) >>> shape = a.shape >>> tfields.lib.util.is_full_slice(get_index[:], shape) True >>> tfields.lib.util.is_full_slice(get_index[:, :], shape) True >>> tfields.lib.util.is_full_slice(get_index[:, 1], shape) False >>> tfields.lib.util.is_full_slice(get_index[1:, :], shape) False >>> tfields.lib.util.is_full_slice(get_index[:1, :], shape) False >>> tfields.lib.util.is_full_slice(get_index[:, 1:], shape) False >>> tfields.lib.util.is_full_slice(get_index[:, :1], shape) False >>> tfields.lib.util.is_full_slice(get_index[:, :-1], shape) True >>> tfields.lib.util.is_full_slice(get_index[np.array([True, True, True])], shape) True >>> tfields.lib.util.is_full_slice(get_index[np.array([True, True, False])], shape) False
- tfields.lib.util.multi_sort(array, *others, **kwargs)[source]¶
Sort all given lists parralel with array sorting, ie rearrange the items in the other lists in the same way, you rearrange them for array due to array sorting
- Parameters:
array (iterable) –
*others (iterable) –
**kwargs –
method (function): sorting function. Default is ‘sorted’ …: further arguments are passed to method. Default rest is
’key=array[0]’
reversed (bool): wether to reverse the results or not cast_type (type): type of returned iterables
Examples
>>> from tfields.lib.util import multi_sort >>> multi_sort([1,2,3,6,4], [1,2,3,4,5]) ([1, 2, 3, 4, 6], [1, 2, 3, 5, 4]) >>> a, b = multi_sort([1,2,3,6,4], [1,2,3,4,5]) >>> b [1, 2, 3, 5, 4]
Expanded to sort as many objects as needed >>> multi_sort([1,2,3,6,4], [1,2,3,4,5], [6,5,4,3,2]) ([1, 2, 3, 4, 6], [1, 2, 3, 5, 4], [6, 5, 4, 2, 3])
Reverse argument >>> multi_sort([1,2,3,6,4], [1,2,3,4,5], [6,5,4,3,2], reverse=True) ([6, 4, 3, 2, 1], [4, 5, 3, 2, 1], [3, 2, 4, 5, 6])
- Returns:
One iterable for each >>> multi_sort([], [], []) ([], [], []) >>> multi_sort([], [], [], cast_type=tuple) ((), (), ())
- Return type:
tuple(cast_type)
- tfields.lib.util.pairwise(iterable)[source]¶
iterator s -> (s0,s1), (s1,s2), (s2, s3), … Source:
- Returns:
two iterators, one ahead of the other
- tfields.lib.util.view_1d(arr)[source]¶
Delete duplicate columns of the input array https://stackoverflow.com/a/44999009/ @Divakar
Module contents¶
Author: Daniel Boeckenhoff Mail: daniel.boeckenhoff@ipp.mpg.de
Collection of additional numpy functions part of tfields library