spectrochempy.NDDatasetο
- class NDDataset(data=None, coordset=None, coordunits=None, coordtitles=None, **kwargs)[source]ο
The main N-dimensional dataset class used by
SpectroChemPy
.The
NDDataset
is the main object use by SpectroChemPy. Like numpyndarray
βs,NDDataset
have the capability to be sliced, sorted and subject to mathematical operations. But, in addition,NDDataset
may have units, can be masked and each dimensions can have coordinates also with units. This makeNDDataset
aware of unit compatibility, e.g., for binary operation such as additions or subtraction or during the application of mathematical operations. In addition or in replacement of numerical data for coordinates,NDDataset
can also have labeled coordinates where labels can be different kind of objects (str
,datetime
,ndarray
or otherNDDataset
βs, etcβ¦).- Parameters:
data (array-like) β Data array contained in the object. The data can be a list, a tuple, a
ndarray
, a subclass ofndarray
, anotherNDDataset
or aCoord
object. Any size or shape of data is accepted. If not given, an emptyNDDataset
will be inited. At the initialisation the provided data will be eventually cast to andarray
. If the provided objects is passed which already contains some mask, or units, these elements will be used if possible to accordingly set those of the created object. If possible, the provided data will not be copied fordata
input, but will be passed by reference, so you should make a copy of thedata
before passing them if thatβs the desired behavior or set thecopy
argument toTrue
.coordset (
CoordSet
instance, optional) β It contains the coordinates for the different dimensions of thedata
. ifCoordSet
is provided, it must specify thecoord
andlabels
for all dimensions of thedata
. Multiplecoord
βs can be specified in aCoordSet
instance for each dimension.coordunits (
list
, optional, default:None
) β A list of units corresponding to the dimensions in the order of the coordset.coordtitles (
list
, optional, default:None
) β A list of titles corresponding of the dimensions in the order of the coordset.**kwargs β Optional keyword parameters (see Other Parameters).
- Other Parameters:
dtype (
str
ordtype
, optional, default:np.float64
) β If specified, the data will be cast to this dtype, else the data will be cast to float64 or complex128.dims (
list
ofstr
, optional) β If specified the list must have a length equal to the number od data dimensions (ndim
) and the elements must be taken amongx,y,z,u,v,w or t
. If not specified, the dimension names are automatically attributed in this order.name (
str
, optional) β A user-friendly name for this object. If not given, the automaticid
given at the object creation will be used as a name.labels (array-like of objects, optional) β Labels for the
data
. labels can be used only for 1D-datasets. The labels array may have an additional dimension, meaning several series of labels for the same data. The given array can be a list, a tuple, andarray
, a ndarray-like, aNDArray
or any subclass ofNDArray
.mask (array-like of
bool
orNOMASK
, optional) β Mask for the data. The mask array must have the same shape as the data. The given array can be a list, a tuple, or andarray
. Each values in the array must beFalse
where the data are valid and True when they are not (like in numpy masked arrays). Ifdata
is already aMaskedArray
, or any array object (such as aNDArray
or subclass of it), providing amask
here, will cause the mask from the masked array to be ignored.units (
Unit
instance orstr
, optional) β Units of the data. If data is aQuantity
thenunits
is set to the unit of thedata
; if a unit is also explicitly provided an error is raised. Handling of units use the pint package.timezone (
datetime.tzinfo
, optional) β The timezone where the data were created. If not specified, the local timezone is assumed.title (
str
, optional) β The title of the data dimension. Thetitle
attribute should not be confused with thename
. Thetitle
attribute is used for instance for labelling plots of the data. It is optional but recommended to give a title to each ndarray data.meta (
dict
-like object, optional) β Additional metadata for this object. Must be dict-like but no further restriction is placed on meta.author (
str
, optional) β Name(s) of the author(s) of this dataset. By default, name of the computer note where this dataset is created.description (
str
, optional) β An optional description of the nd-dataset. A shorter alias isdesc
.origin (
str
, optional) β Origin of the data: Name of organization, address, telephone number, name of individual contributor, etc., as appropriate.roi (
list
) β Region of interest (ROI) limits.history (
str
, optional) β A string to add to the object history.copy (
bool
, optional) β Perform a copy of the passed object. Default is False.
Notes
The underlying array in a
NDDataset
object can be accessed through thedata
attribute, which will return a conventionalndarray
.Attributes Summary
The array with imaginary-imaginary component of hypercomplex 2D data.
The array with imaginary-real component of hypercomplex 2D
data
.The array with real-imaginary component of hypercomplex 2D
data
.The array with real component in both dimension of hypercomplex 2D
data
.Transposed
NDDataset
.Acquisition date.
Creator of the dataset (str).
The main matplotlib axe associated to this dataset.
The matplotlib axe associated to the transposed dataset.
Matplotlib colorbar axe associated to this dataset.
Matplotlib colorbar axe associated to the transposed dataset.
Matplotlib projection x axe associated to this dataset.
Matplotlib projection y axe associated to this dataset.
Provides a comment (Alias to the description attribute).
List of the
Coord
names.CoordSet
instance.List of the
Coord
titles.List of the
Coord
units.Creation date object (Datetime).
The
data
array.Provides a description of the underlying data (str).
True if the
data
array is dimensionless - Readonly property (bool).Names of the dimensions (list).
Get current directory for this dataset.
Matplotlib plot divider.
Return the data type.
Matplotlib figure associated to this dataset.
Matplotlib figure associated to this dataset.
Get current filename for this dataset.
Type of current file.
True if at least one of the
data
array dimension is complex.True if the
data
array is not empty.True is the name has been defined (bool).
True if the
data
array have units - Readonly property (bool).Describes the history of actions made on this array (List of strings).
Object identifier - Readonly property (str).
The array with imaginary component of the
data
.True if the
data
array has only one dimension (bool).True if the 'data' are complex (Readonly property).
True if the
data
array is empty or size=0, and if no label are present.True if the
data
are real values - Readonly property (bool).True if the
data
are integer values - Readonly property (bool).True if the
data
array is hypercomplex with interleaved data.True if the
data
array have labels - Readonly property (bool).True if the
data
array has masked values.True if the
data
array is hypercomplex .Range of the data.
Return the local timezone.
Data array (
ndarray
).Data array (
ndarray
).Mask for the data (
ndarray
of bool).The actual masked
data
array - Readonly property (numpy.ma.ndarray
).Additional metadata (
Meta
).ndarray
- models data.Date of modification (readonly property).
A user-friendly name (str).
A dictionary containing all the axes of the current figures.
The number of dimensions of the
data
array (Readonly property).Origin of the data.
Project
instance.The array with real component of the
data
.Region of interest (ROI) limits (list).
A tuple with the size of each dimension - Readonly property.
Size of the underlying
data
array - Readonly property (int).Filename suffix.
Return the timezone information.
An user-friendly title (str).
The actual array with mask and unit (
Quantity
).bool
- True if thedata
does not haveunits
(Readonly property).Unit
- The units of the data.Alias of
values
.Quantity
- The actual values (data, units) contained in this object (Readonly property).Methods Summary
abs
(dataset[,Β dtype])Calculate the absolute value of the given NDDataset element-wise.
absolute
(dataset[,Β dtype])Calculate the absolute value of the given NDDataset element-wise.
add_coordset
(*coords[,Β dims])Add one or a set of coordinates from a dataset.
all
(dataset[,Β dim,Β keepdims])Test whether all array elements along a given axis evaluate to True.
amax
(dataset[,Β dim,Β keepdims])Return the maximum of the dataset or maxima along given dimensions.
amin
(dataset[,Β dim,Β keepdims])Return the maximum of the dataset or maxima along given dimensions.
any
(dataset[,Β dim,Β keepdims])Test whether any array element along a given axis evaluates to True.
arange
([start,Β stop,Β step,Β dtype])Return evenly spaced values within a given interval.
argmax
(dataset[,Β dim])Indexes of maximum of data along axis.
argmin
(dataset[,Β dim])Indexes of minimum of data along axis.
around
(dataset[,Β decimals])Evenly round to the given number of decimals.
Make data and mask (ndim >= 1) laid out in Fortran order in memory.
astype
([dtype])Cast the data to a specified type.
atleast_2d
([inplace])Expand the shape of an array to make it at least 2D.
average
(dataset[,Β dim,Β weights,Β keepdims])Compute the weighted average along the specified axis.
clip
(dataset[,Β a_min,Β a_max])Clip (limit) the values in a dataset.
Close a Matplotlib figure associated to this dataset.
component
([select])Take selected components of an hypercomplex array (RRR, RIR, ...).
conj
(dataset[,Β dim])Conjugate of the NDDataset in the specified dimension.
conjugate
(dataset[,Β dim])Conjugate of the NDDataset in the specified dimension.
coord
([dim])Return the coordinates along the given dimension.
coordmax
(dataset[,Β dim])Find coordinates of the maximum of data along axis.
coordmin
(dataset[,Β dim])Find oordinates of the mainimum of data along axis.
copy
([deep,Β keepname])Make a disconnected copy of the current object.
cumsum
(dataset[,Β dim,Β dtype])Return the cumulative sum of the elements along a given axis.
Delete all coordinate settings.
diag
(dataset[,Β offset])Extract a diagonal or construct a diagonal array.
diagonal
(dataset[,Β offset,Β dim,Β dtype])Return the diagonal of a 2D array.
dump
(filename,Β **kwargs)Save the current object into compressed native spectrochempy format.
empty
(shape[,Β dtype])Return a new
NDDataset
of given shape and type, without initializing entries.empty_like
(dataset[,Β dtype])Return a new uninitialized
NDDataset
.eye
(N[,Β M,Β k,Β dtype])Return a 2-D array with ones on the diagonal and zeros elsewhere.
fromfunction
(cls,Β function[,Β shape,Β dtype,Β ...])Construct a nddataset by executing a function over each coordinate.
fromiter
(iterable[,Β dtype,Β count])Create a new 1-dimensional array from an iterable object.
full
(shape[,Β fill_value,Β dtype])Return a new
NDDataset
of given shape and type, filled withfill_value
.full_like
(dataset[,Β fill_value,Β dtype])Return a
NDDataset
of fill_value.geomspace
(start,Β stop[,Β num,Β endpoint,Β dtype])Return numbers spaced evenly on a log scale (a geometric progression).
get_axis
(*args,Β **kwargs)Determine an axis index whatever the syntax used (axis index or dimension names).
get_labels
([level])Get the labels at a given level.
identity
(n[,Β dtype])Return the identity
NDDataset
of a given shape.is_units_compatible
(other)Check the compatibility of units with another object.
ito
(other[,Β force])Inplace scaling to different units.
Inplace rescaling to base units.
Quantity scaled in place to reduced units, inplace.
linspace
(cls,Β start,Β stop[,Β num,Β endpoint,Β ...])Return evenly spaced numbers over a specified interval.
load
(filename,Β **kwargs)Open data from a '.scp' (NDDataset) or '.pscp' (Project) file.
loads
(js,Β Any])Deserialize dataset from JSON.
logspace
(cls,Β start,Β stop[,Β num,Β endpoint,Β ...])Return numbers spaced evenly on a log scale.
max
(dataset[,Β dim,Β keepdims])Return the maximum of the dataset or maxima along given dimensions.
mean
(dataset[,Β dim,Β dtype,Β keepdims])Compute the arithmetic mean along the specified axis.
min
(dataset[,Β dim,Β keepdims])Return the maximum of the dataset or maxima along given dimensions.
ones
(shape[,Β dtype])Return a new
NDDataset
of given shape and type, filled with ones.ones_like
(dataset[,Β dtype])Return
NDDataset
of ones.pipe
(func,Β *args,Β **kwargs)Apply func(self, *args, **kwargs).
plot
([method])Plot the dataset using the specified method.
ptp
(dataset[,Β dim,Β keepdims])Range of values (maximum - minimum) along a dimension.
random
([size,Β dtype])Return random floats in the half-open interval [0.0, 1.0).
Remove all masks previously set on this array.
round
(dataset[,Β decimals])Evenly round to the given number of decimals.
round_
(dataset[,Β decimals])Evenly round to the given number of decimals.
save
(**kwargs)Save dataset in native .scp format.
save_as
(filename,Β **kwargs)Save the current NDDataset in SpectroChemPy format (.scp).
set_complex
([inplace])Set the object data as complex.
set_coordset
(*args,Β **kwargs)Set one or more coordinates at once.
set_coordtitles
(*args,Β **kwargs)Set titles of the one or more coordinates.
set_coordunits
(*args,Β **kwargs)Set units of the one or more coordinates.
set_hypercomplex
([inplace])Alias of set_quaternion.
set_quaternion
([inplace])Alias of set_quaternion.
sort
(**kwargs)Return the dataset sorted along a given dimension.
squeeze
(*dims[,Β inplace])Remove single-dimensional entries from the shape of a NDDataset.
std
(dataset[,Β dim,Β dtype,Β ddof,Β keepdims])Compute the standard deviation along the specified axis.
sum
(dataset[,Β dim,Β dtype,Β keepdims])Sum of array elements over a given axis.
swapaxes
(dim1,Β dim2[,Β inplace])Alias of
swapdims
.swapdims
(dim1,Β dim2[,Β inplace])Interchange two dimensions of a NDDataset.
take
(indices,Β **kwargs)Take elements from an array.
to
(other[,Β inplace,Β force])Return the object with data rescaled to different units.
to_array
()Return a numpy masked array.
to_base_units
([inplace])Return an array rescaled to base units.
to_reduced_units
([inplace])Return an array scaled in place to reduced units.
Convert a NDDataset instance to an
DataArray
object.transpose
(*dims[,Β inplace])Permute the dimensions of a NDDataset.
var
(dataset[,Β dim,Β dtype,Β ddof,Β keepdims])Compute the variance along the specified axis.
zeros
(shape[,Β dtype])Return a new
NDDataset
of given shape and type, filled with zeros.zeros_like
(dataset[,Β dtype])Return a
NDDataset
of zeros.Attributes Documentation
- IIο
The array with imaginary-imaginary component of hypercomplex 2D data.
(Readonly property).
- RRο
The array with real component in both dimension of hypercomplex 2D
data
.This readonly property is equivalent to the
real
property.
- acquisition_dateο
Acquisition date.
- authorο
Creator of the dataset (str).
- axο
The main matplotlib axe associated to this dataset.
- axTο
The matplotlib axe associated to the transposed dataset.
- axecο
Matplotlib colorbar axe associated to this dataset.
- axecTο
Matplotlib colorbar axe associated to the transposed dataset.
- axexο
Matplotlib projection x axe associated to this dataset.
- axeyο
Matplotlib projection y axe associated to this dataset.
- commentο
Provides a comment (Alias to the description attribute).
- coordsetο
CoordSet
instance.Contains the coordinates of the various dimensions of the dataset. Itβs a readonly property. Use set_coords to change one or more coordinates at once.
- coordtitlesο
List of the
Coord
titles.Read only property. Use set_coordtitle to eventually set titles.
- coordunitsο
List of the
Coord
units.Read only property. Use set_coordunits to eventually set units.
- createdο
Creation date object (Datetime).
- dataο
The
data
array.If there is no data but labels, then the labels are returned instead of data.
- descriptionο
Provides a description of the underlying data (str).
- dimensionlessο
True if the
data
array is dimensionless - Readonly property (bool).Notes
Dimensionless
is different ofunitless
which means no unit.
- dimsο
Names of the dimensions (list).
The name of the dimensions are βxβ, βyβ, βzββ¦. depending on the number of dimension.
- directoryο
Get current directory for this dataset.
- dividerο
Matplotlib plot divider.
- dtypeο
Return the data type.
- figο
Matplotlib figure associated to this dataset.
- fignumο
Matplotlib figure associated to this dataset.
- filenameο
Get current filename for this dataset.
- filetypeο
Type of current file.
- has_complex_dimsο
True if at least one of the
data
array dimension is complex.(Readonly property).
- has_defined_nameο
True is the name has been defined (bool).
- has_unitsο
True if the
data
array have units - Readonly property (bool).See also
unitless
True if the data has no unit.
dimensionless
True if the data have dimensionless units.
- historyο
Describes the history of actions made on this array (List of strings).
- idο
Object identifier - Readonly property (str).
- is_complexο
True if the βdataβ are complex (Readonly property).
- is_emptyο
True if the
data
array is empty or size=0, and if no label are present.Readonly property (bool).
- is_interleavedο
True if the
data
array is hypercomplex with interleaved data.(Readonly property).
- labelsο
An array of labels for
data
(ndarray
of str).An array of objects of any type (but most generally string), with the last dimension size equal to that of the dimension of data. Note thatβs labelling is possible only for 1D data. One classical application is the labelling of coordinates to display informative strings instead of numerical values.
- limitsο
Range of the data.
- local_timezoneο
Return the local timezone.
- mο
Data array (
ndarray
).If there is no data but labels, then the labels are returned instead of data.
- magnitudeο
Data array (
ndarray
).If there is no data but labels, then the labels are returned instead of data.
- metaο
Additional metadata (
Meta
).
- modifiedο
Date of modification (readonly property).
- Returns:
str β Date of modification in isoformat.
- nameο
A user-friendly name (str).
When no name is provided, the
id
of the object is returned instead.
- ndaxesο
A dictionary containing all the axes of the current figures.
- originο
Origin of the data.
e.g. spectrometer or software
- roiο
Region of interest (ROI) limits (list).
- shapeο
A tuple with the size of each dimension - Readonly property.
The number of
data
element on each dimension (possibly complex). For only labelled array, there is no data, so it is the 1D and the size is the size of the array of labels.
- sizeο
Size of the underlying
data
array - Readonly property (int).The total number of data elements (possibly complex or hypercomplex in the array).
- suffixο
Filename suffix.
Read Only property - automatically set when the filename is updated if it has a suffix, else give the default suffix for the given type of object.
- timezoneο
Return the timezone information.
A timezoneβs offset refers to how many hours the timezone is from Coordinated Universal Time (UTC).
In spectrochempy, all datetimes are stored in UTC, so that conversion must be done during the display of these datetimes using tzinfo.
- titleο
An user-friendly title (str).
When the title is provided, it can be used for labeling the object, e.g., axe title in a matplotlib plot.
Methods Documentation
- abs(dataset, dtype=None)[source]ο
Calculate the absolute value of the given NDDataset element-wise.
abs
is a shorthand for this function. For complex input, a + ib, the absolute value is \(\sqrt{ a^2 + b^2}\) .- Parameters:
dataset (
NDDataset
or array-like) β Input array or object that can be converted to an array.dtype (dtype) β The type of the output array. If dtype is not given, infer the data type from the other input arguments.
- Returns:
NDDataset
β The absolute value of each element in dataset.
- absolute(dataset, dtype=None)[source]ο
Calculate the absolute value of the given NDDataset element-wise.
abs
is a shorthand for this function. For complex input, a + ib, the absolute value is \(\sqrt{ a^2 + b^2}\) .- Parameters:
dataset (
NDDataset
or array-like) β Input array or object that can be converted to an array.dtype (dtype) β The type of the output array. If dtype is not given, infer the data type from the other input arguments.
- Returns:
NDDataset
β The absolute value of each element in dataset.
- add_coordset(*coords, dims=None, **kwargs)[source]ο
Add one or a set of coordinates from a dataset.
- Parameters:
*coords (iterable) β Coordinates object(s).
dims (list) β Name of the coordinates.
**kwargs β Optional keyword parameters passed to the coordset.
- all(dataset, dim=None, keepdims=False)[source]ο
Test whether all array elements along a given axis evaluate to True.
- Parameters:
dataset (array_like) β Input array or object that can be converted to an array.
dim (None or int or str, optional) β Axis or axes along which a logical AND reduction is performed. The default (
axis=None
) is to perform a logical AND over all the dimensions of the input array.axis
may be negative, in which case it counts from the last to the first axis.keepdims (bool, optional) β If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array. If the default value is passed, then
keepdims
will not be passed through to theall
method of sub-classes ofndarray
, however any non-default value will be. If the sub-classβ method does not implementkeepdims
any exceptions will be raised.
- Returns:
all β A new boolean or array is returned unless
out
is specified, in which case a reference toout
is returned.
See also
any
Test whether any element along a given axis evaluates to True.
Notes
Not a Number (NaN), positive infinity and negative infinity evaluate to
True
because these are not equal to zero.
- amax(dataset, dim=None, keepdims=False, **kwargs)[source]ο
Return the maximum of the dataset or maxima along given dimensions.
- Parameters:
dataset (array_like) β Input array or object that can be converted to an array.
dim (None or int or dimension name or tuple of int or dimensions, optional) β Dimension or dimensions along which to operate. By default, flattened input is used. If this is a tuple, the maximum is selected over multiple dimensions, instead of a single dimension or all the dimensions as before.
keepdims (bool, optional) β If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.
- Returns:
amax β Maximum of the data. If
dim
is None, the result is a scalar value. Ifdim
is given, the result is an array of dimensionndim - 1
.
See also
amin
The minimum value of a dataset along a given dimension, propagating NaNs.
minimum
Element-wise minimum of two datasets, propagating any NaNs.
maximum
Element-wise maximum of two datasets, propagating any NaNs.
fmax
Element-wise maximum of two datasets, ignoring any NaNs.
fmin
Element-wise minimum of two datasets, ignoring any NaNs.
argmax
Return the indices or coordinates of the maximum values.
argmin
Return the indices or coordinates of the minimum values.
Notes
For dataset with complex or hypercomplex type type, the default is the value with the maximum real part.
- amin(dataset, dim=None, keepdims=False, **kwargs)[source]ο
Return the maximum of the dataset or maxima along given dimensions.
- Parameters:
dataset (array_like) β Input array or object that can be converted to an array.
dim (None or int or dimension name or tuple of int or dimensions, optional) β Dimension or dimensions along which to operate. By default, flattened input is used. If this is a tuple, the minimum is selected over multiple dimensions, instead of a single dimension or all the dimensions as before.
keepdims (bool, optional) β If this is set to True, the dimensions which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.
- Returns:
amin β Minimum of the data. If
dim
is None, the result is a scalar value. Ifdim
is given, the result is an array of dimensionndim - 1
.
See also
amax
The maximum value of a dataset along a given dimension, propagating NaNs.
minimum
Element-wise minimum of two datasets, propagating any NaNs.
maximum
Element-wise maximum of two datasets, propagating any NaNs.
fmax
Element-wise maximum of two datasets, ignoring any NaNs.
fmin
Element-wise minimum of two datasets, ignoring any NaNs.
argmax
Return the indices or coordinates of the maximum values.
argmin
Return the indices or coordinates of the minimum values.
- any(dataset, dim=None, keepdims=False)[source]ο
Test whether any array element along a given axis evaluates to True.
Returns single boolean unless
dim
is notNone
- Parameters:
dataset (array_like) β Input array or object that can be converted to an array.
dim (None or int or tuple of ints, optional) β Axis or axes along which a logical OR reduction is performed. The default (
axis=None
) is to perform a logical OR over all the dimensions of the input array.axis
may be negative, in which case it counts from the last to the first axis.keepdims (bool, optional) β If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array. If the default value is passed, then
keepdims
will not be passed through to theany
method of sub-classes ofndarray
, however any non-default value will be. If the sub-classβ method does not implementkeepdims
any exceptions will be raised.
- Returns:
any β A new boolean or
ndarray
is returned.
See also
all
Test whether all array elements along a given axis evaluate to True.
- arange(start=0, stop=None, step=None, dtype=None, **kwargs)[source]ο
Return evenly spaced values within a given interval.
Values are generated within the half-open interval [start, stop).
- Parameters:
start (number, optional) β Start of interval. The interval includes this value. The default start value is 0.
stop (number) β End of interval. The interval does not include this value, except in some cases where step is not an integer and floating point round-off affects the length of out. It might be prefereble to use inspace in such case.
step (number, optional) β Spacing between values. For any output out, this is the distance between two adjacent values, out[i+1] - out[i]. The default step size is 1. If step is specified as a position argument, start must also be given.
dtype (dtype) β The type of the output array. If dtype is not given, infer the data type from the other input arguments.
**kwargs β Keywords argument used when creating the returned object, such as units, name, title, β¦
- Returns:
arange β Array of evenly spaced values.
See also
linspace
Evenly spaced numbers with careful handling of endpoints.
Examples
>>> scp.arange(1, 20.0001, 1, units='s', name='mycoord') NDDataset: [float64] s (size: 20)
- around(dataset, decimals=0)[source]ο
Evenly round to the given number of decimals.
- Parameters:
dataset (
NDDataset
) β Input dataset.decimals (int, optional) β Number of decimal places to round to (default: 0). If decimals is negative, it specifies the number of positions to the left of the decimal point.
- Returns:
rounded_array β NDDataset containing the rounded values. The real and imaginary parts of complex numbers are rounded separately. The result of rounding a float is a float. If the dataset contains masked data, the mask remain unchanged.
See also
numpy.round
,around
,spectrochempy.round
,spectrochempy.around
,methods.
,ceil
,fix
,floor
,rint
,trunc
- astype(dtype=None, **kwargs)[source]ο
Cast the data to a specified type.
- Parameters:
dtype (str or dtype) β Typecode or data-type to which the array is cast.
- atleast_2d(inplace=False)[source]ο
Expand the shape of an array to make it at least 2D.
- Parameters:
inplace (bool, optional, default=`False`) β Flag to say that the method return a new object (default) or not (inplace=True).
- Returns:
NDDataset
β The input array, but with dimensions increased.
See also
squeeze
The inverse operation, removing singleton dimensions.
- average(dataset, dim=None, weights=None, keepdims=False)[source]ο
Compute the weighted average along the specified axis.
- Parameters:
dataset (array_like) β Array containing data to be averaged.
dim (None or int or dimension name or tuple of int or dimensions, optional) β Dimension or dimensions along which to operate. By default, flattened input is used. If this is a tuple, the minimum is selected over multiple dimensions, instead of a single dimension or all the dimensions as before.
weights (array_like, optional) β An array of weights associated with the values in
dataset
. Each value ina
contributes to the average according to its associated weight. The weights array can either be 1-D (in which case its length must be the size ofdataset
along the given axis) or of the same shape asdataset
. Ifweights=None
, then all data indataset
are assumed to have a weight equal to one. The 1-D calculation is:avg = sum(a * weights) / sum(weights)
The only constraint on
weights
is thatsum(weights)
must not be 0.
- Returns:
average, β Return the average along the specified axis.
- Raises:
ZeroDivisionError β When all weights along axis are zero. See
numpy.ma.average
for a version robust to this type of error.TypeError β When the length of 1D
weights
is not the same as the shape ofa
along axis.
See also
mean
Compute the arithmetic mean along the specified axis.
Examples
>>> nd = scp.read('irdata/nh4y-activation.spg') >>> nd NDDataset: [float64] a.u. (shape: (y:55, x:5549)) >>> scp.average(nd) <Quantity(1.25085858, 'absorbance')> >>> m = scp.average(nd, dim='y') >>> m NDDataset: [float64] a.u. (size: 5549) >>> m.x Coord: [float64] cmβ»ΒΉ (size: 5549) >>> m = scp.average(nd, dim='y', weights=np.arange(55)) >>> m.data array([ 1.789, 1.789, ..., 1.222, 1.22])
- clip(dataset, a_min=None, a_max=None, **kwargs)[source]ο
Clip (limit) the values in a dataset.
Given an interval, values outside the interval are clipped to the interval edges. For example, if an interval of
[0, 1]
is specified, values smaller than 0 become 0, and values larger than 1 become 1.No check is performed to ensure
a_min < a_max
.- Parameters:
dataset (array_like) β Input array or object that can be converted to an array.
a_min (scalar or array_like or None) β Minimum value. If None, clipping is not performed on lower interval edge. Not more than one of
a_min
anda_max
may be None.a_max (scalar or array_like or None) β Maximum value. If None, clipping is not performed on upper interval edge. Not more than one of
a_min
anda_max
may be None. Ifa_min
ora_max
are array_like, then the three arrays will be broadcasted to match their shapes.
- Returns:
clip β An array with the elements of
a
, but where values <a_min
are replaced witha_min
, and those >a_max
witha_max
.
- component(select='REAL')[source]ο
Take selected components of an hypercomplex array (RRR, RIR, β¦).
- Parameters:
select (str, optional, default=βREALβ) β If βREALβ, only real component in all dimensions will be selected. ELse a string must specify which real (R) or imaginary (I) component has to be selected along a specific dimension. For instance, a string such as βRRIβ for a 2D hypercomplex array indicated that we take the real component in each dimension except the last one, for which imaginary component is preferred.
- Returns:
component β Component of the complex or hypercomplex array.
- conj(dataset, dim='x')[source]ο
Conjugate of the NDDataset in the specified dimension.
- Parameters:
dataset (array_like) β Input array or object that can be converted to an array.
dim (int, str, optional, default=(0,)) β Dimension names or indexes along which the method should be applied.
- Returns:
conjugated β Same object or a copy depending on the
inplace
flag.
See also
conj
,real
,imag
,RR
,RI
,IR
,II
,part
,set_complex
,is_complex
- conjugate(dataset, dim='x')[source]ο
Conjugate of the NDDataset in the specified dimension.
- Parameters:
dataset (array_like) β Input array or object that can be converted to an array.
dim (int, str, optional, default=(0,)) β Dimension names or indexes along which the method should be applied.
- Returns:
conjugated β Same object or a copy depending on the
inplace
flag.
See also
conj
,real
,imag
,RR
,RI
,IR
,II
,part
,set_complex
,is_complex
- coord(dim='x')[source]ο
Return the coordinates along the given dimension.
- Parameters:
dim (int or str) β A dimension index or name, default index =
x
. If an integer is provided, it is equivalent to theaxis
parameter for numpy array.- Returns:
Coord
β Coordinates along the given axis.
- copy(deep=True, keepname=False, **kwargs)[source]ο
Make a disconnected copy of the current object.
- Parameters:
deep (bool, optional) β If True a deepcopy is performed which is the default behavior.
keepname (bool) β If True keep the same name for the copied object.
- Returns:
object β An exact copy of the current object.
Examples
>>> nd1 = scp.NDArray([1. + 2.j, 2. + 3.j]) >>> nd1 NDArray: [complex128] unitless (size: 2) >>> nd2 = nd1 >>> nd2 is nd1 True >>> nd3 = nd1.copy() >>> nd3 is not nd1 True
- cumsum(dataset, dim=None, dtype=None)[source]ο
Return the cumulative sum of the elements along a given axis.
- Parameters:
dataset (array_like) β Calculate the cumulative sum of these values.
dim (None or int or dimension name , optional) β Dimension or dimensions along which to operate. By default, flattened input is used.
dtype (dtype, optional) β Type to use in computing the standard deviation. For arrays of integer type the default is float64, for arrays of float types it is the same as the array type.
- Returns:
sum β A new array containing the cumulative sum.
See also
Examples
>>> nd = scp.read('irdata/nh4y-activation.spg') >>> nd NDDataset: [float64] a.u. (shape: (y:55, x:5549)) >>> scp.sum(nd) <Quantity(381755.783, 'absorbance')> >>> scp.sum(nd, keepdims=True) NDDataset: [float64] a.u. (shape: (y:1, x:1)) >>> m = scp.sum(nd, dim='y') >>> m NDDataset: [float64] a.u. (size: 5549) >>> m.data array([ 100.7, 100.7, ..., 74, 73.98])
- diag(dataset, offset=0, **kwargs)[source]ο
Extract a diagonal or construct a diagonal array.
See the more detailed documentation for
numpy.diagonal
if you use this function to extract a diagonal and wish to write to the resulting array; whether it returns a copy or a view depends on what version of numpy you are using.- Parameters:
dataset (array_like) β If
dataset
is a 2-D array, return a copy of itsk
-th diagonal. Ifdataset
is a 1-D array, return a 2-D array withv
on thek
-th. diagonal.offset (int, optional) β Diagonal in question. The default is 0. Use offset>0 for diagonals above the main diagonal, and offset<0 for diagonals below the main diagonal.
- Returns:
diag β The extracted diagonal or constructed diagonal array.
- diagonal(dataset, offset=0, dim='x', dtype=None, **kwargs)[source]ο
Return the diagonal of a 2D array.
As we reduce a 2D to a 1D we must specified which is the dimension for the coordinates to keep!.
- Parameters:
dataset (
NDDataset
or array-like) β Object from which to extract the diagonal.offset (int, optional) β Offset of the diagonal from the main diagonal. Can be positive or negative. Defaults to main diagonal (0).
dim (str, optional) β Dimension to keep for coordinates. By default it is the last (-1,
x
or another name if the default dimension name has been modified).dtype (dtype, optional) β The type of the returned array.
**kwargs β Additional keyword parameters to be passed to the NDDataset constructor.
- Returns:
diagonal β The diagonal of the input array.
See also
diag
Extract a diagonal or construct a diagonal array.
Examples
>>> nd = scp.full((2, 2), 0.5, units='s', title='initial') >>> nd NDDataset: [float64] s (shape: (y:2, x:2)) >>> nd.diagonal(title='diag') NDDataset: [float64] s (size: 2)
- dump(filename, **kwargs)[source]ο
Save the current object into compressed native spectrochempy format.
- Parameters:
filename (str of
pathlib
object) β File name where to save the current object.
- empty(shape, dtype=None, **kwargs)[source]ο
Return a new
NDDataset
of given shape and type, without initializing entries.- Parameters:
shape (int or tuple of int) β Shape of the empty array.
dtype (data-type, optional) β Desired output data-type.
**kwargs β Optional keyword parameters (see Other Parameters).
- Returns:
empty β Array of uninitialized (arbitrary) data of the given shape, dtype, and order. Object arrays will be initialized to None.
- Other Parameters:
units (str or ur instance) β Units of the returned object. If not provided, try to copy from the input object.
coordset (list or Coordset object) β Coordinates for the returned object. If not provided, try to copy from the input object.
See also
zeros_like
Return an array of zeros with shape and type of input.
ones_like
Return an array of ones with shape and type of input.
empty_like
Return an empty array with shape and type of input.
full_like
Fill an array with shape and type of input.
zeros
Return a new array setting values to zero.
ones
Return a new array setting values to 1.
full
Fill a new array.
Notes
empty
, unlikezeros
, does not set the array values to zero, and may therefore be marginally faster. On the other hand, it requires the user to manually set all the values in the array, and should be used with caution.Examples
>>> scp.empty([2, 2], dtype=int, units='s') NDDataset: [int64] s (shape: (y:2, x:2))
- empty_like(dataset, dtype=None, **kwargs)[source]ο
Return a new uninitialized
NDDataset
.The returned
NDDataset
have the same shape and type as a given array. Units, coordset, β¦ can be added in kwargs.- Parameters:
dataset (
NDDataset
or array-like) β Object from which to copy the array structure.dtype (data-type, optional) β Overrides the data type of the result.
**kwargs β Optional keyword parameters (see Other Parameters).
- Returns:
emptylike β Array of
fill_value
with the same shape and type asdataset
.- Other Parameters:
units (str or ur instance) β Units of the returned object. If not provided, try to copy from the input object.
coordset (list or Coordset object) β Coordinates for the returned object. If not provided, try to copy from the input object.
Notes
This function does not initialize the returned array; to do that use for instance
zeros_like
,ones_like
orfull_like
instead. It may be marginally faster than the functions that do set the array values.
- eye(N, M=None, k=0, dtype=float, **kwargs)[source]ο
Return a 2-D array with ones on the diagonal and zeros elsewhere.
- Parameters:
N (int) β Number of rows in the output.
M (int, optional) β Number of columns in the output. If None, defaults to
N
.k (int, optional) β Index of the diagonal: 0 (the default) refers to the main diagonal, a positive value refers to an upper diagonal, and a negative value to a lower diagonal.
dtype (data-type, optional) β Data-type of the returned array.
**kwargs β Other parameters to be passed to the object constructor (units, coordset, mask β¦).
- Returns:
eye β NDDataset of shape (N,M) An array where all elements are equal to zero, except for the
k
-th diagonal, whose values are equal to one.
See also
Examples
>>> scp.eye(2, dtype=int) NDDataset: [float64] unitless (shape: (y:2, x:2)) >>> scp.eye(3, k=1, units='km').values <Quantity([[ 0 1 0] [ 0 0 1] [ 0 0 0]], 'kilometer')>
- fromfunction(cls, function, shape=None, dtype=float, units=None, coordset=None, **kwargs)[source]ο
Construct a nddataset by executing a function over each coordinate.
The resulting array therefore has a value
fn(x, y, z)
at coordinate(x, y, z)
.- Parameters:
function (callable) β The function is called with N parameters, where N is the rank of
shape
or from the providedCoordSet
.shape ((N,) tuple of ints, optional) β Shape of the output array, which also determines the shape of the coordinate arrays passed to
function
. It is optional only ifCoordSet
is None.dtype (data-type, optional) β Data-type of the coordinate arrays passed to
function
. By default,dtype
is float.units (str, optional) β Dataset units. When None, units will be determined from the function results.
coordset (
CoordSet
instance, optional) β If provided, this determine the shape and coordinates of each dimension of the returnedNDDataset
. If shape is also passed it will be ignored.**kwargs β Other kwargs are passed to the final object constructor.
- Returns:
fromfunction β The result of the call to
function
is passed back directly. Therefore the shape offromfunction
is completely determined byfunction
.
See also
fromiter
Make a dataset from an iterable.
Examples
Create a 1D NDDataset from a function
>>> func1 = lambda t, v: v * t >>> time = scp.Coord.arange(0, 60, 10, units='min') >>> d = scp.fromfunction(func1, v=scp.Quantity(134, 'km/hour'), coordset=scp.CoordSet(t=time)) >>> d.dims ['t'] >>> d NDDataset: [float64] km (size: 6)
- fromiter(iterable, dtype=np.float64, count=-1, **kwargs)[source]ο
Create a new 1-dimensional array from an iterable object.
- Parameters:
iterable (iterable object) β An iterable object providing data for the array.
dtype (data-type) β The data-type of the returned array.
count (
int
, optional) β The number of items to read from iterable. The default is -1, which means all data is read.**kwargs β Other kwargs are passed to the final object constructor.
- Returns:
fromiter β The output nddataset.
See also
fromfunction
Construct a nddataset by executing a function over each coordinate.
Notes
- Specify count to improve performance. It allows fromiter to pre-allocate the
output array, instead of resizing it on demand.
Examples
>>> iterable = (x * x for x in range(5)) >>> d = scp.fromiter(iterable, float, units='km') >>> d NDDataset: [float64] km (size: 5) >>> d.data array([ 0, 1, 4, 9, 16])
- full(shape, fill_value=0.0, dtype=None, **kwargs)[source]ο
Return a new
NDDataset
of given shape and type, filled withfill_value
.- Parameters:
shape (int or sequence of ints) β Shape of the new array, e.g.,
(2, 3)
or2
.fill_value (scalar) β Fill value.
dtype (data-type, optional) β The desired data-type for the array, e.g.,
np.int8
. Default is fill_value.dtype.**kwargs β Optional keyword parameters (see Other Parameters).
- Returns:
full β Array of
fill_value
.- Other Parameters:
units (str or ur instance) β Units of the returned object. If not provided, try to copy from the input object.
coordset (list or Coordset object) β Coordinates for the returned object. If not provided, try to copy from the input object.
See also
zeros_like
Return an array of zeros with shape and type of input.
ones_like
Return an array of ones with shape and type of input.
empty_like
Return an empty array with shape and type of input.
full_like
Fill an array with shape and type of input.
zeros
Return a new array setting values to zero.
ones
Return a new array setting values to one.
empty
Return a new uninitialized array.
Examples
>>> scp.full((2, ), np.inf) NDDataset: [float64] unitless (size: 2) >>> scp.NDDataset.full((2, 2), 10, dtype=np.int) NDDataset: [int64] unitless (shape: (y:2, x:2))
- full_like(dataset, fill_value=0.0, dtype=None, **kwargs)[source]ο
Return a
NDDataset
of fill_value.The returned
NDDataset
have the same shape and type as a given array. Units, coordset, β¦ can be added in kwargs- Parameters:
dataset (
NDDataset
or array-like) β Object from which to copy the array structure.fill_value (scalar) β Fill value.
dtype (data-type, optional) β Overrides the data type of the result.
**kwargs β Optional keyword parameters (see Other Parameters).
- Returns:
fulllike β Array of
fill_value
with the same shape and type asdataset
.- Other Parameters:
units (str or ur instance) β Units of the returned object. If not provided, try to copy from the input object.
coordset (list or Coordset object) β Coordinates for the returned object. If not provided, try to copy from the input object.
See also
zeros_like
Return an array of zeros with shape and type of input.
ones_like
Return an array of ones with shape and type of input.
empty_like
Return an empty array with shape and type of input.
zeros
Return a new array setting values to zero.
ones
Return a new array setting values to one.
empty
Return a new uninitialized array.
full
Fill a new array.
Examples
3 possible ways to call this method
from the API
>>> x = np.arange(6, dtype=int) >>> scp.full_like(x, 1) NDDataset: [float64] unitless (size: 6)
as a classmethod
>>> x = np.arange(6, dtype=int) >>> scp.NDDataset.full_like(x, 1) NDDataset: [float64] unitless (size: 6)
as an instance method
>>> scp.NDDataset(x).full_like(1, units='km') NDDataset: [float64] km (size: 6)
- geomspace(start, stop, num=50, endpoint=True, dtype=None, **kwargs)[source]ο
Return numbers spaced evenly on a log scale (a geometric progression).
This is similar to
logspace
, but with endpoints specified directly. Each output sample is a constant multiple of the previous.- Parameters:
start (number) β The starting value of the sequence.
stop (number) β The final value of the sequence, unless
endpoint
is False. In that case,num + 1
values are spaced over the interval in log-space, of which all but the last (a sequence of lengthnum
) are returned.num (int, optional) β Number of samples to generate. Default is 50.
endpoint (bool, optional) β If true,
stop
is the last sample. Otherwise, it is not included. Default is True.dtype (dtype) β The type of the output array. If
dtype
is not given, infer the data type from the other input arguments.**kwargs β Keywords argument used when creating the returned object, such as units, name, title, β¦
- Returns:
geomspace β
num
samples, equally spaced on a log scale.
- get_axis(*args, **kwargs)[source]ο
Determine an axis index whatever the syntax used (axis index or dimension names).
- Parameters:
dim, axis, dims (str, int, or list of str or index) β The axis indexes or dimensions names - they can be specified as argument or using keyword βaxisβ, βdimβ or βdimsβ.
negative_axis (bool, optional, default=False) β If True a negative index is returned for the axis value (-1 for the last dimension, etcβ¦).
allows_none (bool, optional, default=False) β If True, if input is none then None is returned.
only_first (bool, optional, default: True) β By default return only information on the first axis if dim is a list. Else, return a list for axis and dims information.
- Returns:
axis (int) β The axis indexes.
dim (str) β The axis name.
- get_labels(level=0)[source]ο
Get the labels at a given level.
Used to replace
data
when only labels are provided, and/or for labeling axis in plots.- Parameters:
level (int, optional, default:0) β Label level.
- Returns:
ndarray
β The labels at the desired level or None.
- identity(n, dtype=None, **kwargs)[source]ο
Return the identity
NDDataset
of a given shape.The identity array is a square array with ones on the main diagonal.
- Parameters:
n (int) β Number of rows (and columns) in
n
xn
output.dtype (data-type, optional) β Data-type of the output. Defaults to
float
.**kwargs β Other parameters to be passed to the object constructor (units, coordset, mask β¦).
- Returns:
identity β
n
xn
array with its main diagonal set to one, and all other elements 0.
See also
Examples
>>> scp.identity(3).data array([[ 1, 0, 0], [ 0, 1, 0], [ 0, 0, 1]])
- is_units_compatible(other)[source]ο
Check the compatibility of units with another object.
- Parameters:
other (
ndarray
) β The ndarray object for which we want to compare units compatibility.- Returns:
result β True if units are compatible.
Examples
>>> nd1 = scp.NDDataset([1. + 2.j, 2. + 3.j], units='meters') >>> nd1 NDDataset: [complex128] m (size: 2) >>> nd2 = scp.NDDataset([1. + 2.j, 2. + 3.j], units='seconds') >>> nd1.is_units_compatible(nd2) False >>> nd1.ito('minutes', force=True) >>> nd1.is_units_compatible(nd2) True >>> nd2[0].values * 60. == nd1[0].values True
- ito(other, force=False)[source]ο
Inplace scaling to different units. (same as
to
with inplace= True).- Parameters:
See also
to
Rescaling of the current object data to different units.
to_base_units
Rescaling of the current object data to different units.
ito_base_units
Inplace rescaling of the current object data to different units.
to_reduced_units
Rescaling to reduced units.
ito_reduced_units
Rescaling to reduced units.
- ito_base_units()[source]ο
Inplace rescaling to base units.
See also
to
Rescaling of the current object data to different units.
ito
Inplace rescaling of the current object data to different units.
to_base_units
Rescaling of the current object data to different units.
to_reduced_units
Rescaling to redunced units.
ito_reduced_units
Inplace rescaling to reduced units.
- ito_reduced_units()[source]ο
Quantity scaled in place to reduced units, inplace.
Scaling to reduced units means one unit per dimension. This will not reduce compound units (e.g., βJ/kgβ will not be reduced to m**2/s**2).
See also
to
Rescaling of the current object data to different units.
ito
Inplace rescaling of the current object data to different units.
to_base_units
Rescaling of the current object data to different units.
ito_base_units
Inplace rescaling of the current object data to different units.
to_reduced_units
Rescaling to reduced units.
- linspace(cls, start, stop, num=50, endpoint=True, retstep=False, dtype=None, **kwargs)[source]ο
Return evenly spaced numbers over a specified interval.
Returns num evenly spaced samples, calculated over the interval [start, stop]. The endpoint of the interval can optionally be excluded.
- Parameters:
start (array_like) β The starting value of the sequence.
stop (array_like) β The end value of the sequence, unless endpoint is set to False. In that case, the sequence consists of all but the last of num + 1 evenly spaced samples, so that stop is excluded. Note that the step size changes when endpoint is False.
num (int, optional) β Number of samples to generate. Default is 50. Must be non-negative.
endpoint (bool, optional) β If True, stop is the last sample. Otherwise, it is not included. Default is True.
retstep (bool, optional) β If True, return (samples, step), where step is the spacing between samples.
dtype (dtype, optional) β The type of the array. If dtype is not given, infer the data type from the other input arguments.
**kwargs β Keywords argument used when creating the returned object, such as units,
name, title, β¦
- Returns:
linspace (ndarray) β There are num equally spaced samples in the closed interval [start, stop] or the half-open interval [start, stop) (depending on whether endpoint is True or False).
step (float, optional) β Only returned if retstep is True Size of spacing between samples.
- classmethod load(filename: str | pathlib.Path | BinaryIO, **kwargs: Any) -> Any: """ Open data from a '*.scp' (NDDataset) or '*.pscp' (Project) file. Parameters ---------- filename : `str`, `pathlib` or `file` objects The name of the file to read (or a file objects). **kwargs Optional keyword parameters (see Other Parameters). Other Parameters ---------------- content : str, optional The optional content of the file(s) to be loaded as a binary string. See Also -------- read : Import dataset from various orgines. save : Save the current dataset. Notes ----- Adapted from `numpy.load` . Examples -------- >>> nd1 = scp.read('irdata/nh4y-activation.spg') >>> f = nd1.save() >>> f.name 'nh4y-activation.scp' >>> nd2 = scp.load(f) Alternatively, this method can be called as a class method of NDDataset or Project object: >>> from spectrochempy import * >>> nd2 = NDDataset.load(f) """ content = kwargs.get("content") if content: fid = io.BytesIO(content) else: # be sure to convert filename to a pathlib object with the # default suffix filename = pathclean(filename) suffix = cls().suffix filename = filename.with_suffix(suffix) if kwargs.get("directory") is not None: filename = pathclean(kwargs.get("directory")) / filename if not filename.exists()[source]ο
Open data from a β.scpβ (NDDataset) or β.pscpβ (Project) file.
- Parameters:
- Other Parameters:
content (str, optional) β The optional content of the file(s) to be loaded as a binary string.
Notes
Adapted from
numpy.load
.Examples
>>> nd1 = scp.read('irdata/nh4y-activation.spg') >>> f = nd1.save() >>> f.name 'nh4y-activation.scp' >>> nd2 = scp.load(f)
Alternatively, this method can be called as a class method of NDDataset or Project object:
>>> from spectrochempy import * >>> nd2 = NDDataset.load(f)
- classmethod loads(js: dict[str, Any]) -> Any: """ Deserialize dataset from JSON. Parameters ---------- js : dict[str, Any] JSON object to deserialize Returns ------- Any Deserialized dataset object Raises ------ TypeError If JSON cannot be properly deserialized """ from spectrochempy.core.dataset.coord import Coord from spectrochempy.core.dataset.coordset import CoordSet from spectrochempy.core.dataset.nddataset import NDDataset from spectrochempy.core.project.project import Project from spectrochempy.core.script import Script # ......................... def item_to_attr(obj: Any, dic: dict[str, Any]) -> Any: for key, val in dic.items()[source]ο
Deserialize dataset from JSON.
- Parameters:
js (dict[str, Any]) β JSON object to deserialize
- Returns:
Any β Deserialized dataset object
- Raises:
TypeError β If JSON cannot be properly deserialized
- logspace(cls, start, stop, num=50, endpoint=True, base=10.0, dtype=None, **kwargs)[source]ο
Return numbers spaced evenly on a log scale.
In linear space, the sequence starts at
base ** start
(base
to the power ofstart
) and ends withbase ** stop
(seeendpoint
below).- Parameters:
start (array_like) β
base ** start
is the starting value of the sequence.stop (array_like) β
base ** stop
is the final value of the sequence, unlessendpoint
is False. In that case,num + 1
values are spaced over the interval in log-space, of which all but the last (a sequence of lengthnum
) are returned.num (int, optional) β Number of samples to generate. Default is 50.
endpoint (bool, optional) β If true,
stop
is the last sample. Otherwise, it is not included. Default is True.base (float, optional) β The base of the log space. The step size between the elements in
ln(samples) / ln(base)
(orlog_base(samples)
) is uniform. Default is 10.0.dtype (dtype) β The type of the output array. If
dtype
is not given, infer the data type from the other input arguments.**kwargs β Keywords argument used when creating the returned object, such as units, name, title, β¦
- Returns:
logspace β
num
samples, equally spaced on a log scale.
See also
arange
Similar to linspace, with the step size specified instead of the number of samples. Note that, when used with a float endpoint, the endpoint may or may not be included.
linspace
Similar to logspace, but with the samples uniformly distributed in linear space, instead of log space.
geomspace
Similar to logspace, but with endpoints specified directly.
- max(dataset, dim=None, keepdims=False, **kwargs)[source]ο
Return the maximum of the dataset or maxima along given dimensions.
- Parameters:
dataset (array_like) β Input array or object that can be converted to an array.
dim (None or int or dimension name or tuple of int or dimensions, optional) β Dimension or dimensions along which to operate. By default, flattened input is used. If this is a tuple, the maximum is selected over multiple dimensions, instead of a single dimension or all the dimensions as before.
keepdims (bool, optional) β If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.
- Returns:
amax β Maximum of the data. If
dim
is None, the result is a scalar value. Ifdim
is given, the result is an array of dimensionndim - 1
.
See also
amin
The minimum value of a dataset along a given dimension, propagating NaNs.
minimum
Element-wise minimum of two datasets, propagating any NaNs.
maximum
Element-wise maximum of two datasets, propagating any NaNs.
fmax
Element-wise maximum of two datasets, ignoring any NaNs.
fmin
Element-wise minimum of two datasets, ignoring any NaNs.
argmax
Return the indices or coordinates of the maximum values.
argmin
Return the indices or coordinates of the minimum values.
Notes
For dataset with complex or hypercomplex type type, the default is the value with the maximum real part.
- mean(dataset, dim=None, dtype=None, keepdims=False)[source]ο
Compute the arithmetic mean along the specified axis.
Returns the average of the array elements. The average is taken over the flattened array by default, otherwise over the specified axis.
- Parameters:
dataset (array_like) β Array containing numbers whose mean is desired.
dim (None or int or dimension name, optional) β Dimension or dimensions along which to operate.
dtype (data-type, optional) β Type to use in computing the mean. For integer inputs, the default is
float64
; for floating point inputs, it is the same as the input dtype.keepdims (bool, optional) β If this is set to True, the dimensions which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.
- Returns:
mean β A new array containing the mean values.
See also
Notes
The arithmetic mean is the sum of the elements along the axis divided by the number of elements.
Examples
>>> nd = scp.read('irdata/nh4y-activation.spg') >>> nd NDDataset: [float64] a.u. (shape: (y:55, x:5549)) >>> scp.mean(nd) <Quantity(1.25085858, 'absorbance')> >>> scp.mean(nd, keepdims=True) NDDataset: [float64] a.u. (shape: (y:1, x:1)) >>> m = scp.mean(nd, dim='y') >>> m NDDataset: [float64] a.u. (size: 5549) >>> m.x Coord: [float64] cmβ»ΒΉ (size: 5549)
- min(dataset, dim=None, keepdims=False, **kwargs)[source]ο
Return the maximum of the dataset or maxima along given dimensions.
- Parameters:
dataset (array_like) β Input array or object that can be converted to an array.
dim (None or int or dimension name or tuple of int or dimensions, optional) β Dimension or dimensions along which to operate. By default, flattened input is used. If this is a tuple, the minimum is selected over multiple dimensions, instead of a single dimension or all the dimensions as before.
keepdims (bool, optional) β If this is set to True, the dimensions which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.
- Returns:
amin β Minimum of the data. If
dim
is None, the result is a scalar value. Ifdim
is given, the result is an array of dimensionndim - 1
.
See also
amax
The maximum value of a dataset along a given dimension, propagating NaNs.
minimum
Element-wise minimum of two datasets, propagating any NaNs.
maximum
Element-wise maximum of two datasets, propagating any NaNs.
fmax
Element-wise maximum of two datasets, ignoring any NaNs.
fmin
Element-wise minimum of two datasets, ignoring any NaNs.
argmax
Return the indices or coordinates of the maximum values.
argmin
Return the indices or coordinates of the minimum values.
- ones(shape, dtype=None, **kwargs)[source]ο
Return a new
NDDataset
of given shape and type, filled with ones.- Parameters:
shape (int or sequence of ints) β Shape of the new array, e.g.,
(2, 3)
or2
.dtype (data-type, optional) β The desired data-type for the array, e.g.,
numpy.int8
. Default is**kwargs β Optional keyword parameters (see Other Parameters).
- Returns:
ones β Array of
ones
.- Other Parameters:
units (str or ur instance) β Units of the returned object. If not provided, try to copy from the input object.
coordset (list or Coordset object) β Coordinates for the returned object. If not provided, try to copy from the input object.
See also
zeros_like
Return an array of zeros with shape and type of input.
ones_like
Return an array of ones with shape and type of input.
empty_like
Return an empty array with shape and type of input.
full_like
Fill an array with shape and type of input.
zeros
Return a new array setting values to zero.
empty
Return a new uninitialized array.
full
Fill a new array.
Examples
>>> nd = scp.ones(5, units='km') >>> nd NDDataset: [float64] km (size: 5) >>> nd.values <Quantity([ 1 1 1 1 1], 'kilometer')> >>> nd = scp.ones((5,), dtype=np.int, mask=[True, False, False, False, True]) >>> nd NDDataset: [int64] unitless (size: 5) >>> nd.values masked_array(data=[ --, 1, 1, 1, --], mask=[ True, False, False, False, True], fill_value=999999) >>> nd = scp.ones((5,), dtype=np.int, mask=[True, False, False, False, True], units='joule') >>> nd NDDataset: [int64] J (size: 5) >>> nd.values <Quantity([ -- 1 1 1 --], 'joule')> >>> scp.ones((2, 2)).values array([[ 1, 1], [ 1, 1]])
- ones_like(dataset, dtype=None, **kwargs)[source]ο
Return
NDDataset
of ones.The returned
NDDataset
have the same shape and type as a given array. Units, coordset, β¦ can be added in kwargs.- Parameters:
dataset (
NDDataset
or array-like) β Object from which to copy the array structure.dtype (data-type, optional) β Overrides the data type of the result.
**kwargs β Optional keyword parameters (see Other Parameters).
- Returns:
oneslike β Array of
1
with the same shape and type asdataset
.- Other Parameters:
units (str or ur instance) β Units of the returned object. If not provided, try to copy from the input object.
coordset (list or Coordset object) β Coordinates for the returned object. If not provided, try to copy from the input object.
See also
full_like
Return an array with a given fill value with shape and type of the
input.
zeros_like
Return an array of zeros with shape and type of input.
empty_like
Return an empty array with shape and type of input.
zeros
Return a new array setting values to zero.
ones
Return a new array setting values to one.
empty
Return a new uninitialized array.
full
Fill a new array.
Examples
>>> x = np.arange(6) >>> x = x.reshape((2, 3)) >>> x = scp.NDDataset(x, units='s') >>> x NDDataset: [float64] s (shape: (y:2, x:3)) >>> scp.ones_like(x, dtype=float, units='J') NDDataset: [float64] J (shape: (y:2, x:3))
- pipe(func, *args, **kwargs)[source]ο
Apply func(self, *args, **kwargs).
- Parameters:
func (function) β Function to apply to the
NDDataset
. *args, and **kwargs are passed intofunc
. Alternatively a(callable, data_keyword)
tuple wheredata_keyword
is a string indicating the keyword ofcallable
that expects the array object.*args β Positional arguments passed into
func
.**kwargs β Keyword arguments passed into
func
.
- Returns:
pipe β The return type of
func
.
Notes
Use
pipe
when chaining together functions that expect aNDDataset
.
- plot(method=None, **kwargs)[source]ο
Plot the dataset using the specified method.
- Parameters:
dataset (
NDDataset
) β Source of data to plot.method (str, optional, default:
preference.method_1D
orpreference.method_2D
) β Name of plotting method to use. If None, method is chosen based on data dimensionality.1D plotting methods:
pen
: Solid line plotbar
: Bar graphscatter
: Scatter plotscatter+pen
: Scatter plot with solid line
2D plotting methods:
**kwargs (keyword parameters, optional) β See Other Parameters.
- Other Parameters:
ax (Axe, optional) β Axe where to plot. If not specified, create a new one.
clear (bool, optional, default: True) β If false, hold the current figure and ax until a new plot is performed.
color or c (color, optional, default: auto) β color of the line.
colorbar (bool, optional, default: True) β Show colorbar (2D plots only).
commands (str,) β matplotlib commands to be executed.
data_only (bool, optional, default: False) β Only the plot is done. No addition of axes or label specifications.
dpi (int, optional) β the number of pixel per inches.
figsize (tuple, optional, default is (3.4, 1.7)) β figure size.
fontsize (int, optional) β The font size in pixels, default is 10 (or read from preferences).
imag (bool, optional, default: False) β Show imaginary component for complex data. By default the real component is displayed.
linestyle or ls (str, optional, default: auto) β line style definition.
linewidth or lw (float, optional, default: auto) β line width.
marker, m (str, optional, default: auto) β marker type for scatter plot. If marker != ββ then the scatter type of plot is chosen automatically.
markeredgecolor or mec (color, optional)
markeredgewidth or mew (float, optional)
markerfacecolor or mfc (color, optional)
markersize or ms (float, optional)
markevery (None or int)
modellinestyle or modls (str) β line style of the model.
offset (float) β offset of the model individual lines.
output (str,) β name of the file to save the figure.
plot_model (Bool,) β plot model data if available.
plottitle (bool, optional, default: False) β Use the name of the dataset as title. Works only if title is not defined
projections (bool, optional, default: False) β Show projections on the axes (2D plots only).
reverse (bool or None [optional, default=None/False) β In principle, coordinates run from left to right, except for wavenumbers (e.g., FTIR spectra) or ppm (e.g., NMR), that spectrochempy will try to guess. But if reverse is set, then this is the setting which will be taken into account.
show_complex (bool, optional, default: False) β Show both real and imaginary component for complex data. By default only the real component is displayed.
show_mask (bool, optional) β Should we display the mask using colored area.
show_z (bool, optional, default: True) β should we show the vertical axis.
show_zero (bool, optional) β show the zero basis.
style (str, optional, default:
scp.preferences.style
(scpy)) β Matplotlib stylesheet (useavailable_style
to get a list of available styles for plotting.title (str) β Title of the plot (or subplot) axe.
transposed (bool, optional, default: False) β Transpose the data before plotting (2D plots only).
twinx (
Axes
instance, optional, default: None) β If this is not None, then a twin axes will be created with a common x dimension.uselabel_x (bool, optional) β use x coordinate label as x tick labels
vshift (float, optional) β vertically shift the line from its baseline.
xlim (tuple, optional) β limit on the horizontal axis.
xlabel (str, optional) β label on the horizontal axis.
x_reverse (bool, optional, default: False) β reverse the x axis. Equivalent to
reverse
.ylabel or zlabel (str, optional) β label on the vertical axis.
ylim or zlim (tuple, optional) β limit on the vertical axis.
y_reverse (bool, optional, default: False) β reverse the y axis (2D plot only).
- Returns:
Matplolib Axes or None β The matplotlib axes containing the plot if successful, None otherwise.
- ptp(dataset, dim=None, keepdims=False)[source]ο
Range of values (maximum - minimum) along a dimension.
The name of the function comes from the acronym for βpeak to peakβ .
- Parameters:
dim (None or int or dimension name, optional) β Dimension along which to find the peaks. If None, the operation is made on the first dimension.
keepdims (bool, optional) β If this is set to True, the dimensions which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input dataset.
- Returns:
ptp β A new dataset holding the result.
- random(size=None, dtype=None, **kwargs)[source]ο
Return random floats in the half-open interval [0.0, 1.0).
Results are from the βcontinuous uniformβ distribution over the stated interval.
Note
To sample \(\\mathrm{Uniform}[a, b)\) with \(b > a\), multiply the output of random by (b-a) and add a, i.e.: \((b - a) * \mathrm{random}() + a\).
- Parameters:
size (int or tuple of ints, optional) β Output shape. If the given shape is, e.g., (m, n, k), then m * n * k samples are drawn. Default is None, in which case a single value is returned.
dtype (dtype, optional) β Desired dtype of the result, only float64 and float32 are supported. The default value is np.float64.
**kwargs β Keywords argument used when creating the returned object, such as units, name, title, etcβ¦
- Returns:
random β Array of random floats of shape size (unless size=None, in which case a single float is returned).
- round(dataset, decimals=0)[source]ο
Evenly round to the given number of decimals.
- Parameters:
dataset (
NDDataset
) β Input dataset.decimals (int, optional) β Number of decimal places to round to (default: 0). If decimals is negative, it specifies the number of positions to the left of the decimal point.
- Returns:
rounded_array β NDDataset containing the rounded values. The real and imaginary parts of complex numbers are rounded separately. The result of rounding a float is a float. If the dataset contains masked data, the mask remain unchanged.
See also
numpy.round
,around
,spectrochempy.round
,spectrochempy.around
,methods.
,ceil
,fix
,floor
,rint
,trunc
- round_(dataset, decimals=0)[source]ο
Evenly round to the given number of decimals.
- Parameters:
dataset (
NDDataset
) β Input dataset.decimals (int, optional) β Number of decimal places to round to (default: 0). If decimals is negative, it specifies the number of positions to the left of the decimal point.
- Returns:
rounded_array β NDDataset containing the rounded values. The real and imaginary parts of complex numbers are rounded separately. The result of rounding a float is a float. If the dataset contains masked data, the mask remain unchanged.
See also
numpy.round
,around
,spectrochempy.round
,spectrochempy.around
,methods.
,ceil
,fix
,floor
,rint
,trunc
- save(**kwargs: Any)[source]ο
Save dataset in native .scp format.
- Parameters:
**kwargs (Any) β Optional arguments passed to save_as()
- Returns:
Optional[pathlib.Path] β Path to saved file if successful, None if save failed
- save_as(filename: str = "", **kwargs: Any) -> pathlib.Path | None: """ Save the current NDDataset in SpectroChemPy format (.scp). Parameters ---------- filename : str The filename of the file where to save the current dataset. **kwargs Optional keyword parameters (see Other Parameters). Other Parameters ---------------- directory : str, optional If specified, the given `directory` and the `filename` will be appended. See Also -------- save : Save current dataset. write : Export current dataset to different format. Notes ----- Adapted from :class:`numpy.savez` . Examples -------- Read some data from an OMNIC file >>> nd = scp.read_omnic('wodger.spg') >>> assert nd.name == 'wodger' Write it in SpectroChemPy format (.scp) (return a `pathlib` object) >>> filename = nd.save_as('new_wodger') Check the existence of the scp file >>> assert filename.is_file() >>> assert filename.name == 'new_wodger.scp' Remove this file >>> filename.unlink() """ if filename: # we have a filename # by default it use the saved directory filename = pathclean(filename) if self.directory and self.directory != filename.parent: filename = self.directory / filename else: filename = self.directory # suffix must be specified which correspond to the type of the # object to save default_suffix = SCPY_SUFFIX[self._implements()] if filename is not None and not filename.is_dir()[source]ο
Save the current NDDataset in SpectroChemPy format (.scp).
- Parameters:
filename (str) β The filename of the file where to save the current dataset.
**kwargs β Optional keyword parameters (see Other Parameters).
- Other Parameters:
directory (str, optional) β If specified, the given
directory
and thefilename
will be appended.
Notes
Adapted from
numpy.savez
.Examples
Read some data from an OMNIC file
>>> nd = scp.read_omnic('wodger.spg') >>> assert nd.name == 'wodger'
Write it in SpectroChemPy format (.scp) (return a
pathlib
object)>>> filename = nd.save_as('new_wodger')
Check the existence of the scp file
>>> assert filename.is_file() >>> assert filename.name == 'new_wodger.scp'
Remove this file
>>> filename.unlink()
- set_complex(inplace=False)[source]ο
Set the object data as complex.
When nD-dimensional array are set to complex, we assume that it is along the first dimension. Two succesives rows are merged to form a complex rows. This means that the number of row must be even If the complexity is to be applied in other dimension, either transpose/swapdims your data before applying this function in order that the complex dimension is the first in the array.
- Parameters:
inplace (bool, optional, default=False) β Flag to say that the method return a new object (default) or not (inplace=True).
- Returns:
NDComplexArray β Same object or a copy depending on the
inplace
flag.
See also
- set_coordset(*args, **kwargs)[source]ο
Set one or more coordinates at once.
- Parameters:
Warning
This method replace all existing coordinates.
See also
add_coordset
Add one or a set of coordinates from a dataset.
set_coordtitles
Set titles of the one or more coordinates.
set_coordunits
Set units of the one or more coordinates.
- sort(**kwargs)[source]ο
Return the dataset sorted along a given dimension.
By default, it is the last dimension [axis=-1]) using the numeric or label values.
- Parameters:
dim (str or int, optional, default=-1) β Dimension index or name along which to sort.
pos (int , optional) β If labels are multidimensional - allow to sort on a define row of labels : labels[pos]. Experimental : Not yet checked.
by (str among [βvalueβ, βlabelβ], optional, default=`value`) β Indicate if the sorting is following the order of labels or numeric coord values.
descend (
bool
, optional, default=`False`) β If true the dataset is sorted in a descending direction. Default is False except if coordinates are reversed.inplace (bool, optional, default=`False`) β Flag to say that the method return a new object (default) or not (inplace=True).
- Returns:
NDDataset
β Sorted dataset.
- squeeze(*dims, inplace=False)[source]ο
Remove single-dimensional entries from the shape of a NDDataset.
- Parameters:
*dims (None or int or tuple of ints, optional) β Selects a subset of the single-dimensional entries in the shape. If a dimension (dim) is selected with shape entry greater than one, an error is raised.
inplace (bool, optional, default=`False`) β Flag to say that the method return a new object (default) or not (inplace=True).
- Returns:
NDDataset
β The input array, but with all or a subset of the dimensions of length 1 removed.- Raises:
ValueError β If
dim
is notNone
, and the dimension being squeezed is not of length 1.
- std(dataset, dim=None, dtype=None, ddof=0, keepdims=False)[source]ο
Compute the standard deviation along the specified axis.
Returns the standard deviation, a measure of the spread of a distribution, of the array elements. The standard deviation is computed for the flattened array by default, otherwise over the specified axis.
- Parameters:
dataset (array_like) β Calculate the standard deviation of these values.
dim (None or int or dimension name , optional) β Dimension or dimensions along which to operate. By default, flattened input is used.
dtype (dtype, optional) β Type to use in computing the standard deviation. For arrays of integer type the default is float64, for arrays of float types it is the same as the array type.
ddof (int, optional) β Means Delta Degrees of Freedom. The divisor used in calculations is
N - ddof
, whereN
represents the number of elements. By defaultddof
is zero.keepdims (bool, optional) β If this is set to True, the dimensions which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.
- Returns:
std β A new array containing the standard deviation.
Notes
The standard deviation is the square root of the average of the squared deviations from the mean, i.e.,
std = sqrt(mean(abs(x - x.mean())**2))
.The average squared deviation is normally calculated as
x.sum() / N
, whereN = len(x)
. If, however,ddof
is specified, the divisorN - ddof
is used instead. In standard statistical practice,ddof=1
provides an unbiased estimator of the variance of the infinite population.ddof=0
provides a maximum likelihood estimate of the variance for normally distributed variables. The standard deviation computed in this function is the square root of the estimated variance, so even withddof=1
, it will not be an unbiased estimate of the standard deviation per se.Note that, for complex numbers,
std
takes the absolute value before squaring, so that the result is always real and nonnegative. For floating-point input, the std is computed using the same precision the input has. Depending on the input data, this can cause the results to be inaccurate, especially for float32 (see example below). Specifying a higher-accuracy accumulator using thedtype
keyword can alleviate this issue.Examples
>>> nd = scp.read('irdata/nh4y-activation.spg') >>> nd NDDataset: [float64] a.u. (shape: (y:55, x:5549)) >>> scp.std(nd) <Quantity(0.807972021, 'absorbance')> >>> scp.std(nd, keepdims=True) NDDataset: [float64] a.u. (shape: (y:1, x:1)) >>> m = scp.std(nd, dim='y') >>> m NDDataset: [float64] a.u. (size: 5549) >>> m.data array([ 0.08521, 0.08543, ..., 0.251, 0.2537])
- sum(dataset, dim=None, dtype=None, keepdims=False)[source]ο
Sum of array elements over a given axis.
- Parameters:
dataset (array_like) β Calculate the sum of these values.
dim (None or int or dimension name , optional) β
- Dimension or dimensions along which to operate. By default, flattened input
is used.
dtype (dtype, optional) β Type to use in computing the standard deviation. For arrays of integer type the default is float64, for arrays of float types it is the same as the array type.
keepdims (bool, optional) β If this is set to True, the dimensions which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.
- Returns:
sum β A new array containing the sum.
See also
mean
Compute the arithmetic mean along the specified axis.
trapz
Integration of array values using the composite trapezoidal rule.
Examples
>>> nd = scp.read('irdata/nh4y-activation.spg') >>> nd NDDataset: [float64] a.u. (shape: (y:55, x:5549)) >>> scp.sum(nd) <Quantity(381755.783, 'absorbance')> >>> scp.sum(nd, keepdims=True) NDDataset: [float64] a.u. (shape: (y:1, x:1)) >>> m = scp.sum(nd, dim='y') >>> m NDDataset: [float64] a.u. (size: 5549) >>> m.data array([ 100.7, 100.7, ..., 74, 73.98])
- swapdims(dim1, dim2, inplace=False)[source]ο
Interchange two dimensions of a NDDataset.
- Parameters:
dim1 (int) β First axis.
dim2 (int) β Second axis.
inplace (bool, optional, default=`False`) β Flag to say that the method return a new object (default) or not (inplace=True).
- Returns:
NDDataset
β Swaped dataset.
See also
transpose
Transpose a dataset.
- take(indices, **kwargs)[source]ο
Take elements from an array.
- Returns:
NDDataset
β A sub dataset defined by the input indices.
- to(other, inplace=False, force=False)[source]ο
Return the object with data rescaled to different units.
- Parameters:
other (
Quantity
or str) β Destination units.inplace (bool, optional, default=`False`) β Flag to say that the method return a new object (default) or not (inplace=True).
force (bool, optional, default=False) β If True the change of units is forced, even for incompatible units.
- Returns:
rescaled
See also
ito
Inplace rescaling of the current object data to different units.
to_base_units
Rescaling of the current object data to different units.
ito_base_units
Inplace rescaling of the current object data to different units.
to_reduced_units
Rescaling to reduced_units.
ito_reduced_units
Inplace rescaling to reduced units.
Examples
>>> np.random.seed(12345) >>> ndd = scp.NDArray(data=np.random.random((3, 3)), ... mask=[[True, False, False], ... [False, True, False], ... [False, False, True]], ... units='meters') >>> print(ndd) NDArray: [float64] m (shape: (y:3, x:3))
We want to change the units to seconds for instance but there is no relation with meters, so an error is generated during the change
>>> ndd.to('second') Traceback (most recent call last): ... pint.errors.DimensionalityError: Cannot convert from 'meter' ([length]) to 'second' ([time])
However, we can force the change
>>> ndd.to('second', force=True) NDArray: [float64] s (shape: (y:3, x:3))
By default the conversion is not done inplace, so the original is not modified :
>>> print(ndd) NDArray: [float64] m (shape: (y:3, x:3))
- to_array()[source]ο
Return a numpy masked array.
Other NDDataset attributes are lost.
- Returns:
ndarray
β The numpy masked array from the NDDataset data.
Examples
>>> dataset = scp.read('wodger.spg') >>> a = scp.to_array(dataset)
equivalent to:
>>> a = np.ma.array(dataset)
or
>>> a = dataset.masked_data
- to_base_units(inplace=False)[source]ο
Return an array rescaled to base units.
- Parameters:
inplace (bool) β If True the rescaling is done in place.
- Returns:
rescaled β A rescaled array.
- to_reduced_units(inplace=False)[source]ο
Return an array scaled in place to reduced units.
Reduced units means one unit per dimension. This will not reduce compound units (e.g., βJ/kgβ will not be reduced to m**2/s**2).
- Parameters:
inplace (bool) β If True the rescaling is done in place.
- Returns:
rescaled β A rescaled array.
- to_xarray()[source]ο
Convert a NDDataset instance to an
DataArray
object.Warning: the xarray library must be available.
- Returns:
object β A axrray.DataArray object.
- transpose(*dims, inplace=False)[source]ο
Permute the dimensions of a NDDataset.
- Parameters:
*dims (sequence of dimension indexes or names, optional) β By default, reverse the dimensions, otherwise permute the dimensions according to the values given.
inplace (bool, optional, default=`False`) β Flag to say that the method return a new object (default) or not (inplace=True).
- Returns:
NDDataset β Transposed NDDataset.
See also
swapdims
Interchange two dimensions of a NDDataset.
- var(dataset, dim=None, dtype=None, ddof=0, keepdims=False)[source]ο
Compute the variance along the specified axis.
Returns the variance of the array elements, a measure of the spread of a distribution. The variance is computed for the flattened array by default, otherwise over the specified axis.
- Parameters:
dataset (array_like) β Array containing numbers whose variance is desired.
dim (None or int or dimension name , optional) β Dimension or dimensions along which to operate. By default, flattened input is used.
dtype (dtype, optional) β Type to use in computing the standard deviation. For arrays of integer type the default is float64, for arrays of float types it is the same as the array type.
ddof (int, optional) β Means Delta Degrees of Freedom. The divisor used in calculations is
N - ddof
, whereN
represents the number of elements. By defaultddof
is zero.keepdims (bool, optional) β If this is set to True, the dimensions which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.
- Returns:
var β A new array containing the standard deviation.
See also
Notes
The variance is the average of the squared deviations from the mean, i.e.,
var = mean(abs(x - x.mean())**2)
.The mean is normally calculated as
x.sum() / N
, whereN = len(x)
. If, however,ddof
is specified, the divisorN - ddof
is used instead. In standard statistical practice,ddof=1
provides an unbiased estimator of the variance of a hypothetical infinite population.ddof=0
provides a maximum likelihood estimate of the variance for normally distributed variables.Note that for complex numbers, the absolute value is taken before squaring, so that the result is always real and nonnegative.
For floating-point input, the variance is computed using the same precision the input has. Depending on the input data, this can cause the results to be inaccurate, especially for
float32
(see example below). Specifying a higher-accuracy accumulator using thedtype
keyword can alleviate this issue.Examples
>>> nd = scp.read('irdata/nh4y-activation.spg') >>> nd NDDataset: [float64] a.u. (shape: (y:55, x:5549)) >>> scp.var(nd) <Quantity(0.652818786, 'absorbance')> >>> scp.var(nd, keepdims=True) NDDataset: [float64] a.u. (shape: (y:1, x:1)) >>> m = scp.var(nd, dim='y') >>> m NDDataset: [float64] a.u. (size: 5549) >>> m.data array([0.007262, 0.007299, ..., 0.06298, 0.06438])
- zeros(shape, dtype=None, **kwargs)[source]ο
Return a new
NDDataset
of given shape and type, filled with zeros.- Parameters:
shape (int or sequence of ints) β Shape of the new array, e.g.,
(2, 3)
or2
.dtype (data-type, optional) β The desired data-type for the array, e.g.,
numpy.int8
. Default isnumpy.float64
.**kwargs β Optional keyword parameters (see Other Parameters).
- Returns:
zeros β Array of zeros.
- Other Parameters:
units (str or ur instance) β Units of the returned object. If not provided, try to copy from the input object.
coordset (list or Coordset object) β Coordinates for the returned object. If not provided, try to copy from the input object.
See also
zeros_like
Return an array of zeros with shape and type of input.
ones_like
Return an array of ones with shape and type of input.
empty_like
Return an empty array with shape and type of input.
full_like
Fill an array with shape and type of input.
ones
Return a new array setting values to 1.
empty
Return a new uninitialized array.
full
Fill a new array.
Examples
>>> nd = scp.NDDataset.zeros(6) >>> nd NDDataset: [float64] unitless (size: 6) >>> nd = scp.zeros((5, )) >>> nd NDDataset: [float64] unitless (size: 5) >>> nd.values array([ 0, 0, 0, 0, 0]) >>> nd = scp.zeros((5, 10), dtype=np.int, units='absorbance') >>> nd NDDataset: [int64] a.u. (shape: (y:5, x:10))
- zeros_like(dataset, dtype=None, **kwargs)[source]ο
Return a
NDDataset
of zeros.The returned
NDDataset
have the same shape and type as a given array. Units, coordset, β¦ can be added in kwargs.- Parameters:
dataset (
NDDataset
or array-like) β Object from which to copy the array structure.dtype (data-type, optional) β Overrides the data type of the result.
**kwargs β Optional keyword parameters (see Other Parameters).
- Returns:
zeorslike β Array of
fill_value
with the same shape and type asdataset
.- Other Parameters:
units (str or ur instance) β Units of the returned object. If not provided, try to copy from the input object.
coordset (list or Coordset object) β Coordinates for the returned object. If not provided, try to copy from the input object.
See also
full_like
Return an array with a fill value with shape and type of the input.
ones_like
Return an array of ones with shape and type of input.
empty_like
Return an empty array with shape and type of input.
zeros
Return a new array setting values to zero.
ones
Return a new array setting values to one.
empty
Return a new uninitialized array.
full
Fill a new array.
Examples
>>> x = np.arange(6) >>> x = x.reshape((2, 3)) >>> nd = scp.NDDataset(x, units='s') >>> nd NDDataset: [float64] s (shape: (y:2, x:3)) >>> nd.values <Quantity([[ 0 1 2] [ 3 4 5]], 'second')> >>> nd = scp.zeros_like(nd) >>> nd NDDataset: [float64] s (shape: (y:2, x:3)) >>> nd.values <Quantity([[ 0 0 0] [ 0 0 0]], 'second')>
Examples using spectrochempy.NDDataset

Using plot_multiple to plot several datasets on the same figure

Sine bell and squared Sine bell window multiplication

Savitky-Golay and Whittaker-Eilers smoothing of a Raman spectrum

Processing NMR spectra (slicing, baseline correction, peak picking, peak fitting)