from soilspecdl.datasets import load_toy_ksslDataLoader
The examples use the toy dataset from soilspecdl.datasets: 500 KSSL MIR spectra, here with their exchangeable potassium (Kex) values:
toy = load_toy_kssl()
X, y, ids = toy.xy('k.ext_usda.a725_cmolc.kg')
y, wns = y.flatten(), toy.wavenumbers
X.shape, y.shape, wns[[0, -1]]((500, 1701), (500,), array([ 600, 4000]))
Splitters
A splitter takes the spectra X and the targets y, and returns train, validation and test indices. get_ks_splitter uses the Kennard-Stone algorithm, which selects samples by their distances in spectral space:
get_ks_splitter
def get_ks_splitter(
test_size:float=0.1, # Proportion of data to hold out for testing
valid_size:float=0.1, # Proportion of remaining data after test holdout for validation
groups:NoneType=None, # Group of each sample, such as its soil; Kennard-Stone then selects whole groups by their mean spectrum
)-><built-in function callable>: # Function splitting (X,y) into train/valid/test idxsCreate a Kennard-Stone splitter returning train, validation and test indices
train_idx, valid_idx, test_idx = get_ks_splitter()(X, y)
test_eq(len(train_idx) + len(valid_idx) + len(test_idx), len(X))
len(train_idx), len(valid_idx), len(test_idx)(400, 50, 50)
Replicates, or near-duplicate soils, must stay on one side of a split. Otherwise the test set holds soils that the model has seen in training. Pass groups to keep each group together. Kennard-Stone then selects groups by their mean spectrum, and test_size and valid_size become proportions of groups. Here samples 2i and 2i+1 form one group, as two replicates of one soil would:
groups = np.arange(len(X)) // 2
train_idx, valid_idx, test_idx = get_ks_splitter(groups=groups)(X, y)
test_eq(set(groups[train_idx]) & set(groups[test_idx]), set())
len(train_idx), len(valid_idx), len(test_idx)(400, 50, 50)
get_splitter shuffles the samples with a fixed seed, then cuts them into train, validation and test sets. The test set takes the samples that train and validation leave:
get_splitter
def get_splitter(
train_pct:float=0.8, # Proportion of data for training
valid_pct:float=0.1, # Proportion of data for validation
seed:int=42, # Random seed for reproducibility
)-><built-in function callable>: # Function splitting (X,y) into train/valid/test idxsCreate a random splitter returning train, validation and test indices
train_idx, valid_idx, test_idx = get_splitter()(X, y)
test_eq((len(train_idx), len(valid_idx), len(test_idx)), (400, 50, 50))
len(train_idx), len(valid_idx), len(test_idx)(400, 50, 50)
Proportions whose sum exceeds 1 leave no valid test set, so get_splitter raises a ValueError as soon as it is created:
with expect_fail(contains='must not exceed 1'): get_splitter(train_pct=0.8, valid_pct=0.5)get_mask_splitter
def get_mask_splitter(
is_test, # True for the samples of a fixed test set
valid_pct:float=0.1, # Proportion of the other samples for validation
seed:int=42, # Random seed for choosing the validation samples
)-><built-in function callable>: # Function splitting (X,y) into train/valid/test idxsCreate a splitter for a fixed test set, given as a boolean mask
get_mask_splitter keeps a fixed test set, such as the test samples of an earlier study. The other samples are shuffled with seed, and the first valid_pct of them go to validation. Here every fifth sample is a test sample:
is_test = np.arange(len(X)) % 5 == 0
train_idx, valid_idx, test_idx = get_mask_splitter(is_test)(X, y)
test_eq(test_idx, np.flatnonzero(is_test))
len(train_idx), len(valid_idx), len(test_idx)(360, 40, 100)
get_pre_splitter
def get_pre_splitter(
n_train, # Number of rows in the existing training set
valid_pct:float=0.2, # Proportion of training samples for validation
seed:int=42, # Random seed for reproducibility
)-><built-in function callable>: # Function splitting (X,y) into train/valid/test idxsCreate a splitter for data whose first n_train rows form the training set
get_pre_splitter suits datasets that come already split into train and test sets. Put the training rows first: the first n_train rows become train and validation, and the rest become the test set. With 100 samples and n_train=80, 20% of the training rows go to validation:
train_idx, valid_idx, test_idx = get_pre_splitter(n_train=80)(np.zeros((100, 5)), np.zeros(100))
test_eq(test_idx, np.arange(80, 100))
len(train_idx), len(valid_idx), len(test_idx)(64, 16, 20)
Cross-validation trains one model per fold. get_kfold_splitters shuffles the samples once with seed, cuts them into n_splits folds and returns one splitter per fold. Splitter i validates on fold i and trains on the other folds. Its test set is empty. Keep test samples out of X before splitting.
get_kfold_splitters
def get_kfold_splitters(
n_splits:int=5, # Number of folds
seed:int=42, # Random seed for the shuffle
)->list: # One function per fold, splitting (X,y) into train/valid/test idxsCreate one splitter per fold, each returning train and validation indices and an empty test set
With 500 samples and 5 folds, each splitter trains on 400 samples and validates on 100. Every sample is validated exactly once:
splits = [splitter(X, y) for splitter in get_kfold_splitters(n_splits=5)]
valid_idxs = [valid_idx for _, valid_idx, _ in splits]
test_eq(np.sort(np.concatenate(valid_idxs)), np.arange(len(X)))
[(len(train_idx), len(valid_idx), len(test_idx)) for train_idx, valid_idx, test_idx in splits][(400, 100, 0), (400, 100, 0), (400, 100, 0), (400, 100, 0), (400, 100, 0)]
group_folds
def group_folds(
groups, # Group of each sample, such as its field
k:int=5, # Number of folds
seed:int=0, # Seed for the order in which groups are dealt to folds
)->numpy.ndarray: # Fold of each sample, from 0 to `k` - 1Fold of each sample, with every sample of a group in the same fold
group_folds assigns folds to groups instead of samples, for cross-validation with scikit-learn tools such as PredefinedSplit. It shuffles the groups with seed and deals them to the k folds in turn. Both replicates of each soil are in the same fold:
folds = group_folds(groups, k=5)
test_eq(folds[::2], folds[1::2])
np.bincount(folds)array([100, 100, 100, 100, 100])
Data loader
TensorSpectrum holds spectra as a fastai tensor. show plots a spectrum against its wavenumbers, with the wavenumber axis decreasing from left to right, as is usual for infrared spectra:
TensorSpectrum
def TensorSpectrum(
*args, **kwargs
):Spectra tensor that plots each spectrum against its wavenumbers in wns
TensorSpectrum(X[0], wns=wns)/Users/franckalbinet/aai-ws/.venv/lib/python3.13/site-packages/fastai/torch_core.py:140: UserWarning: The given NumPy array is not writable, and PyTorch does not support non-writable tensors. This means writing to this tensor will result in undefined behavior. You may want to copy the array to protect its data or make it writable before converting it to a tensor. This type of warning will be suppressed for the rest of this program. (Triggered internally at /Users/runner/work/pytorch/pytorch/torch/csrc/utils/tensor_numpy.cpp:219.)
t = torch.as_tensor(x, **kwargs)
TensorSpectrum(shape=(1701,))
Each spectrum stores its own wavenumbers in wns. Spectra on different grids can therefore coexist in one session. This example creates an OSSL spectrum (600–4000 cm⁻¹, step 2) and a Fukushima spectrum (685–3965 cm⁻¹, step 5). Tensor operations, batching and indexing keep wns:
ossl_spc = TensorSpectrum(torch.randn(1, 1701), wns=np.arange(600, 4001, 2))
fuku_spc = TensorSpectrum(torch.randn(1, 657), wns=np.arange(685, 3966, 5))
scaled = ossl_spc * 2
test_eq(scaled.wns, ossl_spc.wns)
test_eq(len(fuku_spc.wns), 657)
scaled.wns[[0, -1]], fuku_spc.wns[[0, -1]](array([ 600, 4000]), array([ 685, 3965]))
show_batch plots a batch of spectra in a grid, with each sample’s target as its title. fastai calls it from dls.show_batch:
SpectralBlock is the fastai block for spectra. It turns each row of the spectra matrix into a TensorSpectrum that carries the wavenumbers:
SpectralBlock
def SpectralBlock(
wns, # Wavenumbers, attached to each spectrum as `wns`
):Block turning each spectrum array into a TensorSpectrum carrying wns
get_spectral_dls
def get_spectral_dls(
X, # Spectral data matrix
y, # Target values
wns, # Wavenumbers
splitter:NoneType=None, # Function returning train/valid/test idxs
bs:int=32, # Batch size
batch_tfms:NoneType=None, # Batch transforms, e.g. [BatchSNV()]
)->tuple: # (DataLoaders with the grid in `dls.wns` and the test indices in `dls.test_idx`, test set indices)Create DataLoaders for spectra, with train, validation and test sets
By default, get_spectral_dls splits the samples at random with get_splitter. show_batch plots spectra with their target values as titles:
dls, test_idx = get_spectral_dls(X, y, wns)
dls.show_batch(max_n=6, ncols=3)
Pass splitter to split another way, for example with Kennard-Stone:
dls_ks, test_idx_ks = get_spectral_dls(X, y, wns, splitter=get_ks_splitter())
len(dls_ks.train_ds), len(dls_ks.valid_ds), len(test_idx_ks)(400, 50, 50)
For cross-validation, build one DataLoaders per fold:
dls_folds = [get_spectral_dls(X, y, wns, splitter=splitter)[0] for splitter in get_kfold_splitters(n_splits=5)]
[(len(d.train_ds), len(d.valid_ds)) for d in dls_folds][(400, 100), (400, 100), (400, 100), (400, 100), (400, 100)]
get_spectral_dls checks that wns has one value per column of X. A mismatch raises a ValueError. Here an OSSL grid of 1,701 wavenumbers is passed with 657-column Fukushima-like spectra:
with expect_fail(contains='`wns` has 1701 values'): get_spectral_dls(np.random.rand(20, 657), np.random.rand(20), np.arange(600, 4001, 2))The grid is a property of the dataset, so the DataLoaders stores it once in dls.wns. Code that needs the grid reads it there. Each spectrum also carries a copy, because show_batch receives spectra without their DataLoaders. The DataLoaders also stores the test indices in dls.test_idx, for test_preds in soilspecdl.transfer:
fuku_wns = np.arange(685, 3966, 5)
dls_syn, test_idx_syn = get_spectral_dls(np.random.rand(100, 657), np.random.rand(100), fuku_wns)
test_eq(dls_syn.wns, fuku_wns)
test_eq(dls_syn.one_batch()[0].wns, dls_syn.wns)
test_eq(dls_syn.test_idx, test_idx_syn)
dls_syn.wns[[0, -1]]array([ 685, 3965])