Note
Go to the end to download the full example code.
Slice an NDDataset with indices and coordinates
This example shows how to combine standard Python slicing with coordinate-aware slicing on an infrared time series.
import spectrochempy as scp
Load an IR dataset and express the acquisition axis in minutes.
NDDataset: [float64] a.u. (shape: (y:19, x:3112))
Plot the full dataset once for context.
prefs = scp.preferences
prefs.figure.figsize = (7, 4)
_ = dataset.plot()

Standard integer slices work as expected on both dimensions.
first_four = dataset[:4]
every_other_point = dataset[:, ::2]
print(first_four.shape)
print(every_other_point.shape)
(4, 3112)
(19, 1556)
Coordinate-aware slicing is often more convenient for spectroscopy work. Using floats slices directly on axis coordinates instead of integer indices.
carbonyl_region = dataset[:, 2300.0:1900.0]
_ = carbonyl_region.plot()

The same applies to the time axis.
window = dataset[80.0:180.0, 2300.0:1900.0]
_ = window.plot()

A single float selects the closest spectrum on that axis.
selected = dataset[60.0]
selected.y
This ends the example. Uncomment the next line to display the figures when running the script directly with Python.
# scp.show()
Total running time of the script: (0 minutes 0.549 seconds)