fmflow.utils package#
Module contents#
- class CStructReader(structure, ignored='$.', byteorder='<', encoding='utf-8')[source]#
Bases:
object
Read a binary file to unpack values in a C structure.
C structures can be expressed as a Python list that stores name, type, and shape of each member in a C structure. For example, the following C structure:
struct structure { int a[2]; float b[2][3]; char c[10]; };
will be expressed as the following Python list:
structure = [ ('a', 'i', 2), ('b', 'd', (2,3)), ('c', '10s') ]
where the first element of each member is name, the second one is the format character (https://docs.python.jp/3/library/struct.html#format-characters) that represents C type in Python, and the third one (optional) is shape.
Example
>>> structure = [('a', 'i', 2), ('b', 'd', (2,3)), ('c', '10s')] >>> reader = fm.utils.CStructReader(structure) >>> with open('binaryfile', 'rb') as f: ... reader.read(f) >>> reader.data OrderedDict([ ('a', array([0, 1])), ('b', array([[0, 1, 2],[3, 4, 5]])), ('c', 'some text') ])
- size#
A byte size of the structure.
- Type:
int
- data#
An ordered dictionary that stores unpacked values.
- Type:
OrderedDict
- jsondata#
A JSON string that stores unpacked values.
- Type:
OrderedDict
- params#
Stored information about the structure, ignored, and byteorder.
- Type:
dict
References
https://docs.python.jp/3/library/struct.html#module-struct
- property data#
An ordered dictionary that stores unpacked values.
- property jsondata#
An JSON string that stores unpacked values.
- read(f)[source]#
Sequentially read a file object to unpack values in a C structure.
Values are stored in the C structure reader instance as an ordered dictionary. Use data or jsondata attributes to access them.
- Parameters:
f (file) – A binary file object to be read. It must be open`ed with `b option.
- class Convergence(threshold=0.001, n_maxiters=300, n_miniters=2, *, centering=False, reuseable=True, raise_exception=False)[source]#
Bases:
object
- property status#
- class StructureReader(structure, skipname='$.', byteorder='<', encoding='utf-8')[source]#
Bases:
object
- property data#
- copy_function(func, name=None)[source]#
Copy a function object with different name.
- Parameters:
func (function) – A function to be copied.
name (string, optional) – A name of the new function. If not spacified, the same name of func will be used.
- Returns:
A new function with different name.
- Return type:
newfunc (function)
- ctype_to_tform(ctype, shape=None)[source]#
Convert Python C-type to FITS format.
- Parameters:
ctype (str) – A C-type format string in Python.
shape (int or tuple of int, optional) – shape of the C-type.
- Returns:
A format string of FITS (TFORM).
- Return type:
tform (str)
- dtype_to_tform(dtype, shape=None)[source]#
Convert NumPy dtype to FITS format.
- Parameters:
dtype (str) – A dtype string of NumPy.
shape (int or tuple of int, optional) – shape of the C-type.
- Returns:
A format string of FITS (TFORM).
- Return type:
tform (str)
- fmgf(array, sigma)[source]#
Apply the FMGF (Fast M-estimation based Gaussian Filter) to a 1D array.
- Parameters:
array (numpy.ndarray) – A 1D array.
sigma (int) – Standard deviation for Gaussian kernel.
- Returns:
A 1D array to which the FMGF is applied.
- Return type:
filtered (numpy.ndarray)
References
Journal of the Japan Society for Precision Engineering Vol.76 (2010) No.6 P684-688 A Proposal of Robust Gaussian Filter by Using Fast M-Estimation Method http://doi.org/10.2493/jjspe.76.684
- gaussian(x, x0=0.0, fwhm=1.0, ampl=1.0)[source]#
Return Gaussian with given parameters.
y = ampl * exp(-4ln2 * (x-x0)^2 / fwhm^2)
- Parameters:
x (numpy.ndarray) – An input 1D array.
x0 (float) – Center of the Gaussian.
fwhm (float) – FWHM of the Gaussian.
ampl (float) – Amplitude of the Gaussian.
- Returns:
An output 1D Gaussian.
- Return type:
y (numpy.ndarray)
- ignore_numpy_errors()[source]#
Return a context manager where all numpy errors are ignored.
This function is intended to be used as a with statement like:
>>> with ignore_numpy_errors(): ... np.arange(10) / 0 # no errors are displayed
- lorentzian(x, x0=0.0, fwhm=1.0, ampl=1.0)[source]#
Return Lorentzian with given parameters.
y = ampl / (1 + 4(x-x0)^2 / fwhm^2)
- Parameters:
x (numpy.ndarray) – An input 1D array.
x0 (float) – Center of the Lorentzian.
fwhm (float) – FWHM of the Lorentzian.
ampl (float) – Amplitude of the Lorentzian.
- Returns:
An output 1D Lorentzian.
- Return type:
y (numpy.ndarray)
- mad(array, axis=None, keepdims=False)[source]#
Compute the median absolute deviation (MAD) along the given axis.
- Parameters:
array (numpy.ndarray) – An input array.
axis (int, optional) – Axis along which the MADs are computed. The default is to compute the MAD along a flattened version of the array.
keepdims (bool, optional) – If True, the axes which are reduced are left in the result as dimensions with size one.
- Returns:
A new array holding the result.
- Return type:
mad (numpy.ndarray)
- one_thread_per_process()[source]#
Return a context manager where only one thread is allocated to a process.
This function is intended to be used as a with statement like:
>>> with process_per_thread(): ... do_something() # one thread per process
Notes
This function only works when MKL (Intel Math Kernel Library) is installed and used in, for example, NumPy and SciPy. Otherwise this function does nothing.
- pseudovoigt(x, x0=0.0, fwhm=1.0, ampl=1.0, frac=0.5)[source]#
Return Pseudo-Voigt with given parameters.
y = frac * gaussian(x, x0, fwhm) + (1-frac) * lorentzian(x, x0, fwhm)
- Parameters:
x (numpy.ndarray) – An input 1D array.
x0 (float) – Center of the Pseudo-Voigt.
fwhm (float) – FWHM of the Pseudo-Voigt.
ampl (float) – Amplitude of the Pseudo-Voigt.
frac (float) – Fraction of Gaussian and Lorentzian. Must be 0 <= frac <= 1.
- Returns:
An output 1D Pseudo-Voigt.
- Return type:
y (numpy.ndarray)
- rollrows(array, shifts)[source]#
Roll 2D array elements of each row by a given shifts.
- Parameters:
array (numpy.ndarray) – A 2D array.
shifts (int of list of int) – The number(s) of places by which elements of each row are shifted.
- Returns:
An output rolled array.
- Return type:
array (numpy.ndarray)
- slicewhere(condition)[source]#
Return slices of regions that fulfill condition.
Example
>>> cond = [False, True, True, False, False, True, False] >>> fm.utils.slicewhere(cond) [slice(1L, 3L, None), slice(5L, 6L, None)]
- Parameters:
condition (numpy.ndarray) – An array of booleans.
- Returns:
List of slice objects.
- Return type:
slices (list of slice)