scipp.lookup#
- scipp.lookup(func, dim=None, *, mode=None, fill_value=None)#
Create a “lookup table” from a histogram (data array with bin-edge coord).
The lookup table can be used to map, e.g., time-stamps to corresponding values given by a time-series log.
- Parameters:
func (
DataArray) – Data array defining the lookup table.dim (
str|None, default:None) – Dimension along which the lookup occurs.mode (
Optional[Literal['previous','nearest']], default:None) – Mode used for looking up function values. Must beNonewhenfuncis a histogram. Otherwise this defaults to ‘nearest’.fill_value (
Variable|None, default:None) – Value to use for points outside the range of the function as well as points in masked regions of the function. If set to None (the default) this will use NaN for floating point types and 0 for integral types. Must have the same dtype and unit as the function values.
- Returns:
Lookup– The created lookup table.
Examples
Create a lookup table from a histogram (bin-edge coordinates):
>>> import scipp as sc >>> x = sc.linspace(dim='x', start=0.0, stop=1.0, num=4) >>> vals = sc.array(dims=['x'], values=[3, 2, 1]) >>> hist = sc.DataArray(data=vals, coords={'x': x}) >>> lut = sc.lookup(hist, 'x') >>> lut[sc.array(dims=['event'], values=[0.1, 0.4, 0.6, 0.9])] <scipp.Variable> (event: 4) int64 [dimensionless] [3, 2, 2, 1]
For point data (non-histogram), use
mode='previous'for step-like lookup:>>> x_points = sc.array(dims=['x'], values=[0.0, 0.25, 0.5, 0.75, 1.0], unit='s') >>> vals_points = sc.array(dims=['x'], values=[10, 20, 30, 40, 50]) >>> timeseries = sc.DataArray(data=vals_points, coords={'x': x_points}) >>> lut_prev = sc.lookup(timeseries, 'x', mode='previous') >>> query = sc.array(dims=['time'], values=[0.1, 0.3, 0.6, 0.9], unit='s') >>> lut_prev[query] <scipp.Variable> (time: 4) int64 [dimensionless] [10, 20, 30, 40]
Or use
mode='nearest'for nearest-neighbor interpolation:>>> lut_nearest = sc.lookup(timeseries, 'x', mode='nearest') >>> lut_nearest[query] <scipp.Variable> (time: 4) int64 [dimensionless] [10, 20, 30, 50]
Use
fill_valueto specify the value for out-of-range points:>>> x = sc.linspace(dim='x', start=0.0, stop=1.0, num=4) >>> vals = sc.array(dims=['x'], values=[3.0, 2.0, 1.0]) >>> hist = sc.DataArray(data=vals, coords={'x': x}) >>> lut = sc.lookup(hist, 'x', fill_value=sc.scalar(-1.0)) >>> lut[sc.array(dims=['event'], values=[-0.5, 0.5, 1.5])] <scipp.Variable> (event: 3) float64 [dimensionless] [-1, 2, -1]