Vector¶
- class ompy.Vector(values=None, E=None, path=None, std=None, units='keV', **kwargs)[source]¶
Bases:
AbstractArray
Stores 1d array with energy axes (a vector)
- Variables:
There are several ways to initialize
An initialization with only E, or energy, defaults to zero values.
An initialization using a path loads a vector from said path.
If no std is given, it will default to None
- Parameters:
- Raises:
ValueError if the given arrays are of differing lenghts. –
Todo
Crean up initialization.
Attributes Summary
Methods Summary
Calculate and return the calibration coefficients of the energy axes
closest
(E_new[, side, inplace])Re-bin the vector without merging bins.
copy
()Return a deepcopy of the class
cumulative
([factor, inplace])Cumulative sum of the vector.
cut
([Emin, Emax, inplace])Cut the vector at the energy limits
cut_nan
([inplace])Cut the vector where elements are np.nan
error
(other[, std])Computes the (weighted) χ²
has_equal_binning
(other, **kwargs)Check whether other has equal_binning as self within precision.
index
(E)Returns the closest index corresponding to the E value
load
(path[, filetype])Load to a file of specified format
plot
([ax, scale, kind])Plots the vector
rebin
([mids, factor, inplace])Rebins vector, assuming equidistant binning
save
(path[, filetype, units])Save to a file of specified format
to_MeV
()Convert E from keV to MeV if necessary
to_keV
()Convert E from MeV to keV if necessary
transform
([const, alpha, inplace])Apply a normalization transformation.
verify_equdistant
(axis)Runs checks to verify if energy arrays are equidistant
verify_integrity
([check_equidistant])Verify the internal consistency of the vector
Attributes Documentation
- shape¶
Methods Documentation
- calibration()[source]¶
Calculate and return the calibration coefficients of the energy axes
Formatted as “a{axis}{power of E}”
- closest(E_new, side='right', inplace=False)[source]¶
Re-bin the vector without merging bins.
The resulting vector will have E_new as the x-axis while the jth y-value will be given by the ith value of the original y-values where E[i] < E_new[j] <= E[i+1] or E[i] <= E_new[j] < E[i+1].
- Parameters:
- Return type:
- Returns:
Vector with the new E axis and the bin content of the bins that contains E.
- Raises:
RuntimeError if the x-axis of the original vector is –
not sorted. –
- copy()¶
Return a deepcopy of the class
- Return type:
- cumulative(factor=None, inplace=False)[source]¶
Cumulative sum of the vector.
- Parameters:
- Return type:
- Returns:
The cumulative sum vector if inplace is ‘False’
- Raises:
RuntimeError if elements in self.E are not equidistant –
and factor='de'. –
ValueError if factor is a string other than 'de'. –
- has_equal_binning(other, **kwargs)[source]¶
Check whether other has equal_binning as self within precision.
- Parameters:
other (Vector) – Vector to compare to.
kwargs – Additional kwargs to np.allclose.
- Return type:
- Returns:
Returns True if both arrays are equal .
- Raises:
TypeError – If other is not a Vector.
ValueError – If any of the bins in any of the arrays are not equal.
- load(path, filetype=None, **kwargs)[source]¶
Load to a file of specified format
- Parameters:
- Raises:
ValueError – Filetype is not supported
- Return type:
- plot(ax=None, scale='linear', kind='line', **kwargs)[source]¶
Plots the vector
- Parameters:
ax (matplotlib axis, optional) – The axis to plot onto. If not provided, a new figure is created
scale (str, optional) – The scale to use. Can be linear (default), log, symlog or logit.
kind (str, optional) –
‘line’ : line plot (default) evokes ax.plot
’plot’ : same as ‘line’
’step’ : step plot
’bar’ : vertical bar plot
kwargs (optional) – Additional kwargs to plot command.
- Return type:
- Returns:
The figure and axis used.
- rebin(mids=None, factor=None, inplace=True)[source]¶
Rebins vector, assuming equidistant binning
- Parameters:
- Return type:
- Returns:
The rebinned vector if inplace is ‘False’.
- save(path, filetype=None, units='keV', **kwargs)[source]¶
Save to a file of specified format
- Parameters:
path (str or Path) – Path to save
filetype (str, optional) – Filetype. Default uses auto-recognition from suffix. Options: [“numpy”, “txt”, “tar”, “mama”, “csv”]
units (str, optional) – Units for the x-axis. Default uses keV. Options: [“keV”, “MeV”, “same”]. “same” option will use the current selected units of the vector. Note: This keyword is ignored when saving to “mama” type.
**kwargs – additional keyword arguments
- Raises:
ValueError – Filetype is not supported.
NotImplementedError – Unsupported units.
- Return type:
- transform(const=1, alpha=0, inplace=True)[source]¶
Apply a normalization transformation:
vector -> const * vector * exp(alpha*energy)
If the vector has std, the std will be transformed as well.
- Parameters:
- Return type:
- Returns:
Optional[Vector]
- verify_equdistant(axis)¶
Runs checks to verify if energy arrays are equidistant
- axis: The axis to project onto.
Can be either of (0, ‘Eg’, ‘x’), (1, ‘Ex’, ‘y’)
- Raises:
ValueError – If any check fails
- verify_integrity(check_equidistant=False)[source]¶
Verify the internal consistency of the vector
- Parameters:
check_equidistant (bool, optional) – Check whether energy array are equidistant spaced. Defaults to False.
- Raises:
AssertionError or ValueError if any test fails –