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 and inspect the dataset
NDDataset: [float64] a.u. (shape: (y:19, x:3112))
prefs = scp.preferences
prefs.figure.figsize = (7, 4)
_ = dataset.plot()

Integer-based slicing
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
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()

Selecting the closest spectrum
A single float selects the nearest 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.518 seconds)