Reading datasets

In this example, we show the use of the generic read method to create dataset either from local or remote files.

First we need to import the spectrochempy API package

import os
from pathlib import Path

import spectrochempy as scp

TEST_FILE = Path(os.environ.get("TEST_FILE", "irdata/nh4y-activation.spg"))
TEST_FOLDER = Path(os.environ.get("TEST_FOLDER", "irdata"))

Import dataset from local files

Read a IR data recorded in Omnic format (.spg extension). We just pass the file name as parameter.

NDDataset [nh4y-activation] — float64, shape: (y:55, x:5549), a.u.
name
:
nh4y-activation
author
:
runner@runnervmzvulz
created
:
2026-08-18 21:26:06+00:00
description
:
Omnic title: NH4Y-activation.SPG
Omnic filename: /home/runner/.spectrochempy/testdata/irdata/nh4y-activation.spg
history
:
2026-08-18 21:26:06+00:00> Imported from spg file /home/runner/.spectrochempy/testdata/irdata/nh4y-activation.spg.
2026-08-18 21:26:06+00:00> Sorted by date
Data
title
:
absorbance
values
:
[[ 2.057 2.061 ... 2.013 2.012]
[ 2.033 2.037 ... 1.913 1.911]
...
[ 1.794 1.791 ... 1.198 1.198]
[ 1.816 1.815 ... 1.24 1.238]] a.u.
shape
:
(y:55, x:5549)
Dimension `x`
size
:
5549
title
:
wavenumbers
coordinates
:
[ 6000 5999 ... 650.9 649.9] cm⁻¹
Dimension `y`
size
:
55
title
:
acquisition timestamp (GMT)
coordinates
:
[1.468e+09 1.468e+09 ... 1.468e+09 1.468e+09] s
labels
:
[[ 2016-07-06 19:03:14+00:00 2016-07-06 19:13:14+00:00 ... 2016-07-07 04:03:17+00:00 2016-07-07 04:13:17+00:00]
[ vz0466.spa, Wed Jul 06 21:00:38 2016 (GMT+02:00) vz0467.spa, Wed Jul 06 21:10:38 2016 (GMT+02:00) ...
vz0520.spa, Thu Jul 07 06:00:41 2016 (GMT+02:00) vz0521.spa, Thu Jul 07 06:10:41 2016 (GMT+02:00)]]


if dataset is not None:
    _ = dataset.plot(style="paper")
plot generic read

When using read, we can pass filename as a str or a pathlib.Path object.

Note that is the file is not found in the current working directory, SpectroChemPy will try to find it in the datadir directory defined in preferences :

PosixPath('/home/runner/.spectrochempy/testdata')

If the supplied argument is a directory, then the whole directory is read at once. By default, the different files will be merged along the first dimension (y). However, for this to work, the second dimension (x) must be compatible (same size) or else a WARNING appears. To avoid the warning and get individual spectra, you can set merge to False . This test-data directory may also contain historical trusted native .scp archives, which require explicit legacy opt-in.

dataset_list = scp.read(TEST_FOLDER, merge=False, allow_unsafe_legacy=True)
print(dataset_list)
[NDDataset: [float64] a.u. (shape: (y:19, x:3112)), NDDataset: [float64] a.u. (shape: (y:55, x:5549)), NDDataset: [float64] unitless (shape: (y:1, x:3736))]

to get full details on the parameters that can be used, look at the API documentation: spectrochempy.read .

Import dataset from remote files

To download and read file from remote server you can use urls.

try:
    dataset_list = scp.read("http://www.eigenvector.com/data/Corn/corn.mat")
except FileNotFoundError:
    dataset_list = None
    print("Eigenvector corn dataset not reachable; skipping remote import examples.")
else:
    # %%
    # In this case the matlab data contains 7 arrays that have been automatically
    # transformed to ``NDDataset`` .
    for nd in dataset_list:
        print(f"{nd.name} : {nd.shape}")

    # %%
    # The `eigenvector.com <eigenvector.com>`__ website contains the same data in a
    # compressed (zipped) format:
    # `corn.mat_.zip <https://eigenvector.com/wp-content/uploads/2019/06/corn.mat_.zip>`__ .
    # This can also be used by the ``read`` method.
    dataset_list = scp.read(
        "https://eigenvector.com/wp-content/uploads/2019/06/corn.mat_.zip"
    )
    print(dataset_list)

    # %%
    # Plot each of the datasets
    _ = dataset_list[-1].plot()
    _ = dataset_list[-2].plot()
    _ = dataset_list[-3].plot()
    _ = dataset_list[-4].plot()
  • plot generic read
  • plot generic read
  • plot generic read
  • plot generic read
NDDataset_6c97c29d : (240, 700)
m5nbs : (3, 700)
NDDataset_6c97c311 : (8, 700)
propvals : (80, 4)
[NDDataset: [float64] unitless (shape: (y:240, x:700)), NDDataset: [float64] unitless (shape: (y:3, x:700)), NDDataset: [float64] unitless (shape: (y:8, x:700)), NDDataset: [float64] unitless (shape: (y:80, x:4))]

This ends the example ! The following line can be uncommented if no plot shows when running the .py script with python

# scp.show()

Total running time of the script: (0 minutes 3.039 seconds)