PySNN Documentation

pysnn.file_io

pysnn.file_io

The following set of functions is adapted from the SLAYER repository, found at:

https://github.com/bamsumit/slayerPytorch

class pysnn.file_io.Events(x_events, y_events, p_events, t_events)

This class provides a way to store, read, write and visualize spike Events.

Members:
  • x (numpy int array): x index of spike Events.

  • y (numpy int array): y index of spike Events (not used if the spatial dimension is 1).

  • p (numpy int array): polarity or channel index of spike Events.

  • t (numpy double array): timestamp of spike Events. Time is assumend to be in ms.

Usage:

>>> TD = file_io.Events(x_events, y_events, p_events, t_events)
to_spike_array(sampling_time=1, dim=None)

Returns a numpy tensor that contains the spike Eventss sampled in bins of sampling_time. The array is of dimension (channels, height, time) or``CHT`` for 1D data. The array is of dimension (channels, height, width, time) or``CHWT`` for 2D data.

Arguments:
  • sampling_time: the width of time bin to use.

  • dim: the dimension of the desired tensor. Assignes dimension itself if not provided.

y

Usage:

>>> spike = TD.to_spike_array()
to_spike_tensor(empty_tensor, sampling_time=1)

Returns a numpy tensor that contains the spike Eventss sampled in bins of sampling_time. The tensor is of dimension (channels, height, width, time) or``CHWT``.

Arguments:
  • empty_tensor (numpy or torch tensor): an empty tensor to hold spike data

  • sampling_time: the width of time bin to use.

Usage:

>>> spike = TD.to_spike_tensor( torch.zeros((2, 240, 180, 5000)) )
pysnn.file_io.spike_array_to_events(spike_mat, sampling_time=1)

Returns TD Events from a numpy array (of dimension 3 or 4). The numpy array must be of dimension (channels, height, time) or``CHT`` for 1D data. The numpy array must be of dimension (channels, height, width, time) or``CHWT`` for 2D data.

Arguments:
  • spike_mat: numpy array with spike information.

  • sampling_time: time width of each time bin.

Usage:

>>> TD = file_io.spike_array_to_events(spike)
pysnn.file_io.read_1d_spikes(filename)

Reads one dimensional binary spike file and returns a TD Events.

The binary file is encoded as follows:
  • Each spike Events is represented by a 40 bit number.

  • First 16 bits (bits 39-24) represent the neuron_id.

  • Bit 23 represents the sign of spike Events: 0=>OFF Events, 1=>ON Events.

  • the last 23 bits (bits 22-0) represent the spike Events timestamp in microseconds.

Arguments:
  • filename (string): path to the binary file.

Usage:

>>> TD = file_io.read_1d_spikes(file_path)
pysnn.file_io.encode_1d_spikes(filename, TD)

Writes one dimensional binary spike file from a TD Events.

The binary file is encoded as follows:
  • Each spike Events is represented by a 40 bit number.

  • First 16 bits (bits 39-24) represent the neuron_id.

  • Bit 23 represents the sign of spike Events: 0=>OFF Events, 1=>ON Events.

  • the last 23 bits (bits 22-0) represent the spike Events timestamp in microseconds.

Arguments:
  • filename (string): path to the binary file.

  • TD (an file_io.Events): TD Events.

Usage:

>>> file_io.write1Dspikes(file_path, TD)
pysnn.file_io.read_2d_spikes(filename)

Reads two dimensional binary spike file and returns a TD Events. It is the same format used in neuromorphic datasets NMNIST & NCALTECH101.

The binary file is encoded as follows:
  • Each spike Events is represented by a 40 bit number.

  • First 8 bits (bits 39-32) represent the xID of the neuron.

  • Next 8 bits (bits 31-24) represent the yID of the neuron.

  • Bit 23 represents the sign of spike Events: 0=>OFF Events, 1=>ON Events.

  • The last 23 bits (bits 22-0) represent the spike Events timestamp in microseconds.

Arguments:
  • filename (string): path to the binary file.

Usage:

>>> TD = file_io.read_2d_spikes(file_path)
pysnn.file_io.encode_2d_spikes(filename, TD)

Writes two dimensional binary spike file from a TD Events. It is the same format used in neuromorphic datasets NMNIST & NCALTECH101.

The binary file is encoded as follows:
  • Each spike Events is represented by a 40 bit number.

  • First 8 bits (bits 39-32) represent the xID of the neuron.

  • Next 8 bits (bits 31-24) represent the yID of the neuron.

  • Bit 23 represents the sign of spike Events: 0=>OFF Events, 1=>ON Events.

  • The last 23 bits (bits 22-0) represent the spike Events timestamp in microseconds.

Arguments:
  • filename (string): path to the binary file.

  • TD (an file_io.Events): TD Events.

Usage:

>>> file_io.write2Dspikes(file_path, TD)
pysnn.file_io.read_3d_spikes(filename)

Reads binary spike file for spike Events in height, width and channel dimension and returns a TD Events.

The binary file is encoded as follows:
  • Each spike Events is represented by a 56 bit number.

  • First 12 bits (bits 56-44) represent the xID of the neuron.

  • Next 12 bits (bits 43-32) represent the yID of the neuron.

  • Next 8 bits (bits 31-24) represents the channel ID of the neuron.

  • The last 24 bits (bits 23-0) represent the spike Events timestamp in microseconds.

Arguments:
  • filename (string): path to the binary file.

Usage:

>>> TD = file_io.read_3d_spikes(file_path)
pysnn.file_io.encode_3d_spikes(filename, TD)

Writes binary spike file for TD Events in height, width and channel dimension.

The binary file is encoded as follows:
  • Each spike Events is represented by a 56 bit number.

  • First 12 bits (bits 56-44) represent the xID of the neuron.

  • Next 12 bits (bits 43-32) represent the yID of the neuron.

  • Next 8 bits (bits 31-24) represents the channel ID of the neuron.

  • The last 24 bits (bits 23-0) represent the spike Events timestamp in microseconds.

Arguments:
  • filename (string): path to the binary file.

  • TD (an file_io.Events): TD Events.

Usage:

>>> file_io.write3Dspikes(file_path, TD)
pysnn.file_io.read_1d_num_spikes(filename)

Reads a tuple specifying neuron, start of spike region, end of spike region and number of spikes from binary spike file.

The binary file is encoded as follows:
  • Number of spikes data is represented by an 80 bit number.

  • First 16 bits (bits 79-64) represent the neuron_id.

  • Next 24 bits (bits 63-40) represents the start time in microseconds.

  • Next 24 bits (bits 39-16) represents the end time in microseconds.

  • Last 16 bits (bits 15-0) represents the number of spikes.

Arguments:
  • filename (string): path to the binary file

Usage:

>>> n_id, t_start, t_end, n_spikes = file_io.read_1d_num_spikes(file_path)
``t_start`` and ``t_end`` are returned in milliseconds
pysnn.file_io.encode_1d_num_spikes(filename, n_id, t_start, t_end, n_spikes)

Writes binary spike file given a tuple specifying neuron, start of spike region, end of spike region and number of spikes.

The binary file is encoded as follows:
  • Number of spikes data is represented by an 80 bit number

  • First 16 bits (bits 79-64) represent the neuron_id

  • Next 24 bits (bits 63-40) represents the start time in microseconds

  • Next 24 bits (bits 39-16) represents the end time in microseconds

  • Last 16 bits (bits 15-0) represents the number of spikes

Arguments:
  • filename (string): path to the binary file

  • n_id (numpy array): neuron ID

  • t_start (numpy array): region start time (in milliseconds)

  • t_end (numpy array): region end time (in milliseconds)

  • n_spikes (numpy array): number of spikes in the region

Usage:

>>> file_io.encode_1d_num_spikes(file_path, n_id, t_start, t_end, n_spikes)
pysnn.file_io.show_td(TD, frame_rate=24, pre_compute_frames=True, repeat=False)

Visualizes TD Events.

Arguments:
  • TD: spike Events to visualize.

  • frame_rate: framerate of visualization.

  • pre_compute_frames: flag to enable precomputation of frames for faster visualization. Default is True.

  • repeat: flag to enable repeat of animation. Default is False.

Usage:

>>> show_td(TD)