spectrochempy.fromfunction
- 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)