nervos.dataloader package#

Submodules#

nervos.dataloader.circles module#

This module implements the CirclesLoader class, which extends the Dataloader base class. It is specifically designed to handle scikit-learn’s circle toy dataset, and provides methods for:

  • Loading data

  • Converting points into spike trains using Gaussian receptive fields

  • Retrieving train and test data

The CirclesLoader supports preprocessing for Spiking Neural Network (SNN) training workflows.

class nervos.dataloader.circles.CirclesLoader(parameters: Parameters, num_rf: tuple[int, int], var: tuple[float, float], noise: float = 0.06, factor: float = 0.5)[source]#

Bases: Dataloader

A dataloader class specifically designed for loading and preprocessing the make_circles dataset.

This class provides functionality for:

  • Loading the dataset

  • Normalizing coordinates

  • Encoding coordinates into Gaussian probabilities

  • Generating spike trains based on Poisson processes

  • A dataloader method for retrieving data samples

parameters#

Configuration parameters for the loader.

Type:

Parameters

noise#

Standard deviation of Gaussian noise added to the data.

Type:

float

factor#

Scale factor between the inner and outer circle.

Type:

float

dataloader(size: int = None, train: bool = True, preprocess: bool = False, seed: int = 42) tuple[ndarray, ndarray][source]#

Loads and preprocesses the dataset.

Parameters:
  • train (bool, optional) – Placeholder for train/test split. Defaults to True.

  • size (int, optional) – Number of samples to load. Defaults to None (all samples).

  • preprocess (bool, optional) – Whether to preprocess the data into spike trains. Defaults to False.

  • seed (int, optional) – Seed for reproducibility. Defaults to 42.

Returns:

Preprocessed data and labels.

Return type:

tuple[np.ndarray, np.ndarray]

encode_coordinates(point: ndarray) ndarray[source]#

Encodes a coordinate into Gaussian probabilities for each neuron.

Parameters:

point (np.ndarray) – A 2D point (x, y) to encode.

Returns:

Encoded probabilities for each neuron.

Return type:

np.ndarray

generate_poisson_spikes(spike_probs: ndarray, time_steps: int) ndarray[source]#

Generates Poisson spike trains for a given probability distribution.

Parameters:
  • spike_probs (np.ndarray) – Probabilities for each neuron to fire.

  • time_steps (int) – Number of time steps for the spike train.

Returns:

Spike trains for all neurons.

Return type:

np.ndarray

plot_rf() None[source]#

Plots the receptive fields and covered area.

nervos.dataloader.iris module#

This module implements the IrisLoader class, which extends the Dataloader base class. It is specifically designed to handle the Iris dataset, providing methods for:

  • Loading the dataset

  • Generating more synthetic data in the dataset

  • Converting samples into spike trains

The IrisLoader supports preprocessing for Spiking Neural Network (SNN) training workflows.

class nervos.dataloader.iris.IrisLoader(parameters: Parameters, num_rf: tuple, var: tuple)[source]#

Bases: Dataloader

dataloader(size: int = None, train: bool = True, preprocess: bool = False, seed: int = 42) tuple[ndarray, ndarray][source]#

Loads and optionally preprocesses the Iris dataset for SNN training.

Parameters:
  • size (int, optional) – Number of samples to load (for test set only).

  • train (bool, optional) – Whether to load training or test data. Defaults to True.

  • preprocess (bool, optional) – Whether to convert features into spike trains. Defaults to False.

  • seed (int, optional) – Random seed for reproducibility. Defaults to 42.

Returns:

Feature matrix and labels.

Return type:

tuple[np.ndarray, np.ndarray]

encode_coordinates(point: ndarray) ndarray[source]#

Encodes a given feature vector using Gaussian receptive fields.

Parameters:

point (np.ndarray) – Feature vector to encode.

Returns:

Encoded representation using Gaussian functions.

Return type:

np.ndarray

fluff_data(samples_per_class: int) None[source]#

Generates additional synthetic data by adding noise to the existing samples.

Parameters:

samples_per_class (int) – Number of synthetic samples to generate per class.

generate_data(total_samples: int) tuple[ndarray, ndarray][source]#

Generates a new dataset with balanced classes using Gaussian-distributed noise.

Parameters:

total_samples (int) – Total number of samples to generate.

Returns:

Generated feature matrix and corresponding labels.

Return type:

tuple[np.ndarray, np.ndarray]

generate_poisson_spikes(spike_probs: ndarray, time_steps: int) ndarray[source]#

Converts encoded feature vectors into Poisson-distributed spike trains.

Parameters:
  • spike_probs (np.ndarray) – Probability values for spike generation.

  • time_steps (int) – Number of time steps for the spike train.

Returns:

Spike train representation of the input.

Return type:

np.ndarray

normalise() None[source]#

Normalizes the dataset features to the range [0, 1].

nervos.dataloader.loader module#

This module contains the base Dataloader class, which provides the foundation for implementing custom dataloaders for various datasets. It primarily serves as a blueprint for creating data loading mechanisms.

class nervos.dataloader.loader.Dataloader(parameters: Parameters)[source]#

Bases: object

A base class for creating a dataloader.

This class serves as a blueprint for implementing dataloaders to handle data loading tasks. It is initialized with a Parameters object containing configuration details.

parameters#

An object encapsulating the parameters required for the dataloader.

Type:

Parameters

dataloader() None[source]#

Placeholder method for loading data.

This method should be overridden by subclasses to define the specific data loading logic.

Returns:

None

nervos.dataloader.mnist module#

This module implements the MNISTLoader class, which extends the Dataloader base class. It is specifically designed to handle the MNIST dataset, providing methods for:

  • Loading and filtering digit classes

  • Normalizing images

  • Converting images into spike trains

  • Retrieving balanced or random samples

The MNISTLoader supports preprocessing for Spiking Neural Network (SNN) training workflows.

class nervos.dataloader.mnist.MNISTLoader(parameters: Parameters, classes: list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])[source]#

Bases: Dataloader

A dataloader class specifically designed for loading and preprocessing the MNIST dataset.

This class extends the Dataloader base class and provides functionality for loading MNIST data, normalizing images, converting images into spike trains, and retrieving balanced or random samples.

parameters#

Configuration parameters for the loader.

Type:

Parameters

classes#

List of digit classes to include in the dataset.

Type:

list

X_train#

Training images.

Type:

np.ndarray

Y_train#

Training labels.

Type:

np.ndarray

X_test#

Testing images.

Type:

np.ndarray

Y_test#

Testing labels.

Type:

np.ndarray

compress_image(image: ndarray, k: int) ndarray[source]#

Compress image by keeping top block of m x m = k coefficients.

Parameters:
  • image (np.ndarray) – 2D numpy array (e.g. 28x28 for MNIST)

  • k (int) – number of coefficients to keep.

  • sqrt ((k must be chosen so that)

Returns:

The DCT coefficient matrix with only the top-left m x m block kept.

Return type:

np.ndarray

dataloader(train: bool = True, preprocess: bool = False, random_single: bool = False, seed: int = 42, size: int = None, k: int = None, threshold: float = 0.5, pca=False) tuple[ndarray, ndarray | int][source]#

Loads and preprocesses the MNIST dataset.

Parameters:
  • train (bool, optional) – Whether to load training or testing data. Defaults to True.

  • preprocess (bool, optional) – Whether to preprocess the data into spike trains. Defaults to False.

  • random_single (bool, optional) – Whether to return a single random sample. Defaults to False.

  • seed (int, optional) – Seed for reproducibility. Defaults to 42.

  • size (int, optional) – Number of samples to load. Defaults to None.

  • k (int, optional) – The top k features to keep after taking DCT of the image. Defaults to None means take the whole image.

  • threshold (float) – Used only if DCT is used. The threshold below which all pixel values will be zero after normalization. Defaults to 0.5

  • pca (bool) – If compression should happen according to PCA.

Returns:

Preprocessed data and labels, or a single random sample.

Return type:

np.ndarray or tuple

get_random_image(get_spike_train: bool = False) tuple[ndarray, int, ndarray][source]#

Retrieves a random image and its label from the training dataset.

Parameters:

get_spike_train (bool, optional) – Whether to return the spike train of the image. Defaults to False.

Returns:

A tuple containing the image, label, and optionally the spike train.

Return type:

tuple

image2dct2image(image: ndarray, k: int, threshold: float) tuple[ndarray, ndarray][source]#

Compresses the image, then keeps top k DCT features, then uncompresses the image.

Parameters:
  • image (np.ndarray) – 2D numpy array (e.g. 28x28 for MNIST)

  • k (int) – number of coefficients to keep.

  • sqrt ((k must be chosen so that)

  • threshold (float) – The threshold below which all pixel values will be zero after normalization. Defaults to 0.5

Returns:

The DCT coefficient matrix with only the top-left m x m = k block kept. np.ndarray: The top k DCT features uncompressed image.

Return type:

np.ndarray

img2spiketrain(img: ndarray) ndarray[source]#

Converts a normalized image into a spike train representation.

Parameters:

img (np.ndarray) – The normalized image.

Returns:

The spike train representation of the image.

Return type:

np.ndarray

load_balanced_mnist(Y: ndarray, num_samples: int, seed: int = None) ndarray[source]#

Balances the dataset by selecting an equal number of samples for each label.

Parameters:
  • Y (np.ndarray) – The labels of the dataset.

  • num_samples (int) – Total number of samples to select.

  • seed (int, optional) – Seed for reproducibility. Defaults to None.

Returns:

Indices of the selected samples.

Return type:

np.ndarray

normalise(img: ndarray) ndarray[source]#

Normalizes an image to the range [0, 1].

Parameters:

img (np.ndarray) – The image to normalize.

Returns:

The normalized image.

Return type:

np.ndarray

uncompress_image(image: ndarray, k: int, threshold: float = 0.5) ndarray[source]#

Given the (possibly sparsified) DCT coefficients, reconstruct the image using the inverse DCT.

Parameters:
  • image (np.ndarray) – 2D numpy array.

  • k (int) – number of coefficients to keep.

  • threshold (float) – The threshold below which all pixel values will be zero after normalization. Defaults to 0.5

Returns:

The uncompressed image.

Return type:

np.ndarray

Module contents#

This package provides classes and methods for loading, preprocessing, and managing datasets, with a focus on Spiking Neural Network (SNN) applications.

Submodules:

mnist: MNIST-specific dataloader with SNN preprocessing support. loader: Base dataloader class for custom datasets. circles: Non-linear circle dataset loader with SNN preprocessing support. iris: Iris dataset loader with SNN preprocessing support.