utils.py
create_interval_labels(is_missing)
¶
Create interval labels from a missing data mask.
Uses scipy.ndimage.label to identify contiguous regions of valid data (where is_missing=False) and assigns sequential integer labels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
is_missing
|
(NDArray[bool_], shape(n_time))
|
Boolean mask where True indicates time points outside intervals |
required |
Returns:
| Name | Type | Description |
|---|---|---|
interval_labels |
(NDArray[intp], shape(n_time))
|
Integer labels where: - -1 indicates time points outside any interval - 0, 1, 2, ... indicate the 1st, 2nd, 3rd interval index |
Source code in src/spyglass/decoding/v1/utils.py
concatenate_interval_results(interval_results)
¶
Concatenate results from multiple intervals along time dimension.
All datasets must have compatible structure (same variables, compatible coordinates except time). Time coordinates will be concatenated and an interval_labels coordinate will be added to track which interval each time point belongs to.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
interval_results
|
List[xr.Dataset], length n_intervals
|
Results from each decoding interval. Each dataset must have a 'time' dimension and coordinate. Empty datasets should be filtered out before calling this function. |
required |
Returns:
| Type | Description |
|---|---|
Dataset
|
Concatenated results with interval_labels coordinate. The interval_labels coordinate contains integer values where each value indicates which interval the corresponding time point belongs to. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If interval_results is empty or contains empty datasets |
Examples:
>>> import xarray as xr
>>> import numpy as np
>>> ds1 = xr.Dataset({"x": ("time", [1, 2, 3])}, coords={"time": [0.0, 0.1, 0.2]})
>>> ds2 = xr.Dataset({"x": ("time", [4, 5])}, coords={"time": [1.0, 1.1]})
>>> result = concatenate_interval_results([ds1, ds2])
>>> result.interval_labels.values
array([0, 0, 0, 1, 1])
>>> result.time.values
array([0. , 0.1, 0.2, 1. , 1.1])
Source code in src/spyglass/decoding/v1/utils.py
get_valid_kwargs(classifier, decoding_kwargs, logger)
¶
Get valid fit and predict kwargs, warning about any ignored kwargs.
Inspects the classifier's fit and predict method signatures to determine which kwargs are valid. Logs a warning if any provided kwargs are not valid for either method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
classifier
|
object
|
Classifier instance with fit and predict methods |
required |
decoding_kwargs
|
dict
|
User-provided kwargs for fit/predict |
required |
logger
|
Logger
|
Logger for warnings |
required |
Returns:
| Name | Type | Description |
|---|---|---|
fit_kwargs |
dict
|
Kwargs valid for classifier.fit |
predict_kwargs |
dict
|
Kwargs valid for classifier.predict |