Utilities module¶
The utils
module offers the following utility functions:
Function |
Description |
---|---|
Returns version of araucaria. |
|
Check type and attributes of an object. |
|
Range values for an array. |
|
Index of values in given range for an array. |
|
Index of duplicate values in an array. |
|
Index of NaN values in an array. |
|
Index of nearest value in an array. |
|
Returns interpolated values for 1-D function. |
|
Reads file with multiple dictionaries. |
- araucaria.utils.get_version(dependencies=False)[source]¶
Returns installed version of araucaria.
- Parameters
dependencies (
bool
) – Condition to additionally get version of dependencies. The default is False.- Return type
- Returns
Printable string with version of araucaria.
Examples
>>> from araucaria.utils import get_version >>> print(get_version()) Araucaria version : ...
- araucaria.utils.check_objattrs(obj, objtype, attrlist=None, exceptions=False)[source]¶
Check type and attributes of an object.
- Parameters
- Return type
- Returns
List with booleans for each attribute of the object.
- Raises
TypeError – If
obj
is not an instance ofobjtype
.
Examples
>>> from araucaria import Group >>> from araucaria.utils import check_objattrs >>> group = Group(**{'energy': [1,2,3,4], 'mu': [2,2,3,1]}) >>> # checking class type >>> check_objattrs(group, Group) True
>>> # checking class type and attributes >>> alist = ['energy', 'mu', 'mu_ref'] >>> check_objattrs(group, Group, attrlist = alist) [True, True, False]
- araucaria.utils.check_xrange(x_range, x, refval=None)[source]¶
Returns range values inside an array.
- Parameters
- Return type
- Returns
New range values inside the array.
Examples
>>> from numpy import inf, linspace >>> from araucaria.utils import check_xrange >>> k_range = [-inf,inf] >>> k = linspace(0,15) >>> krange = check_xrange(k_range, k) >>> print(krange) [0.0, 15.0]
>>> # using a reference value >>> e_range = [-inf, -50] >>> energy = linspace(8900, 9100) >>> e0 = 8979 >>> erange = check_xrange(e_range, energy, refval=e0) >>> print(erange) [-79.0, -50]
- araucaria.utils.index_xrange(x_range, x, refval=None)[source]¶
Returns indexes of range values inside an array.
- Parameters
- Return type
- Returns
Array with indexes of range values inside the array.
Examples
>>> from numpy import arange, inf >>> from araucaria.utils import index_xrange >>> k_range = [4, 8] >>> k = arange(0,16) >>> index = index_xrange(k_range, k) >>> k[index] array([4, 5, 6, 7, 8])
>>> # using a reference value >>> e_range = [-inf, -50] >>> energy = arange(8900, 9100) >>> e0 = 8979 >>> index = index_xrange(e_range, energy, refval=e0) >>> energy[index][0], energy[index][-1] (8900, 8929)
- araucaria.utils.index_dups(data, tol=0.0001)[source]¶
Index of duplicate values.
- Parameters
- Return type
- Returns
Index array with the location of duplicates.
Notes
A value in
data
is considered a duplicate if the difference with respect to the previous value is strictly lower than the giventol
value.If the dimension of
data
is larger than 1 the array will be flattened by indexing the elements in row-major (i.e. C-style).Examples
index_dups()
is useful to remove duplicates and ensure monotonicity of a 1-D array.>>> from numpy import array, delete >>> from araucaria.utils import index_dups >>> energy = array([9000, 9000.1, 9005, 9005.1, 9008]) >>> index = index_dups(energy, tol=0.5) >>> print(index) [1 3]
>>> # duplicactes >>> print(energy[index]) [9000.1 9005.1]
>>> # removing duplicates >>> from numpy import delete >>> new_energy = delete(energy,index,0) >>> print(new_energy) [9000. 9005. 9008.]
- araucaria.utils.index_nans(data, axis=0)[source]¶
Index of NaN values in an array.
- Parameters
- Returns
Index array with NaN values in the given axis.
- Raises
IndexError – If
axis
is greater than the dimension ofdata
.
Examples
index_nans()
is useful to remove NaN values from arrays.>>> from numpy import arange, nan, delete >>> from araucaria.utils import index_nans >>> data = arange(20, dtype=float).reshape(5,4) >>> data[1,2] = nan; data[3,1] = nan >>> print(data) [[ 0. 1. 2. 3.] [ 4. 5. nan 7.] [ 8. 9. 10. 11.] [12. nan 14. 15.] [16. 17. 18. 19.]]
>>> # removing NaN values from rows >>> rindex = index_nans(data, 0) >>> print(rindex) [[1] [3]] >>> print(delete(data, rindex, 0)) [[ 0. 1. 2. 3.] [ 8. 9. 10. 11.] [16. 17. 18. 19.]]
>>> # removing NaN values from columns >>> cindex = index_nans(data, 1) >>> print(cindex) [[1] [2]] >>> print(delete(data, cindex, 1)) [[ 0. 3.] [ 4. 7.] [ 8. 11.] [12. 15.] [16. 19.]]
- araucaria.utils.index_nearest(data, val, kind='nearest')[source]¶
Index of nearest value in an array.
- Parameters
- Return type
- Returns
Index of nearest value in the array.
- Raises
ValueError – If
kind
is not recognized.
Notes
The
kind
parameter controls the returned index:If
kind='lower'
the returned index will be of a value strictly lower thanval
, or 0 ifval
if lower than the minimum value ofdata
.If
kind='nearest'
the returned index will be of the nearest value with respect toval
.If
kind='higher'
the returned index will be of a value strictly higher thanval
, or -1 ifval
is higher than the maximum value ofdata
.
Examples
>>> from numpy import linspace >>> from araucaria.utils import index_nearest >>> energy = linspace(8900, 9000, 6) >>> val = 8965 >>> # find nearest value >>> index = index_nearest(energy, val) >>> print(index, energy[index], val) 3 8960.0 8965
>>> # find strictly lower nearest value >>> index = index_nearest(energy, val, kind='lower') >>> print(index, energy[index], val) 3 8960.0 8965
>>> # find strictly higher nearest value >>> index = index_nearest(energy, val, kind='higher') >>> print(index, energy[index], val) 4 8980.0 8965
- araucaria.utils.interp_yvals(x, y, xnew, kind='cubic')[source]¶
Returns interpolated values for a 1-D function.
- Parameters
- Return type
- Returns
Array with interpolated values.
Example
>>> from numpy import linspace >>> from araucaria.utils import interp_yvals >>> x = linspace(0,10) >>> y = x**2 >>> xp = x[0:10] >>> yp = interp_yvals(x,y,xp) >>> print(len(yp)) 10
- araucaria.utils.read_fdicts(fpath)[source]¶
Reads file with multiple dictionaries
Example
>>> from os import remove >>> from araucaria.utils import read_fdicts >>> fpath ='file.txt' >>> data = "{'ener': [1,2,3], 'mu': [1,2,3]}" >>> # create file with dictionary data >>> with open(fpath, 'w') as f: ... fw = f.write(data) >>> # reading file with dictionary >>> dicts = read_fdicts(fpath) >>> remove(fpath) >>> for d in dicts: ... print(type(d), d) <class 'dict'> {'ener': [1, 2, 3], 'mu': [1, 2, 3]}