Encoders

Spike encoders for Spiking Neural Network (spatio-temporal data)

There are three types of encoders:

BSA and Step Forward run faster when Numba is installed (pip install pyspikes[numba]). The spikes are the same either way.

Bens Spiker Algorithm

class spikes.encoder.BSA(data, threshold=0.75, filter_length=12, cutoff=0.2)[source]

Bens Spiker Algorithm (BSA) uses Finite Impulse Response (FIR) filter to try a reverse convolution of the stimulation (converting spikes to analog values). A principle borrowed from unpublished modified Hough Spiker Algorithm (HSA) [2].

The defaults for threshold, filter_length and cutoff are not taken from the paper; the best values depend on the signal. They were chosen empirically, by how well the spikes reconstruct EEG (tests/test-data) and smooth synthetic signals. For other data, tune them by reconstruction error, for example the signal-to-noise ratio between the normalised input and the spikes convolved with the filter.

Parameters:
  • data (ndarray) – See :py:mod: nucube.utils.ReadCSV.get_samples()

  • threshold (float) – Threshold level for spike. It can also be a list of threshold according to the numbers of input features. It is subtracted, as in the paper: a spike fires when error1 <= error2 - threshold. Some other implementations multiply instead (error2 * threshold), so their thresholds are not comparable.

  • filter_length (int) – Number of taps in the FIR filter. Longer filters follow slower changes in the signal.

  • cutoff (float) – Cutoff frequency of the FIR filter, as a fraction of the Nyquist frequency.

get_spike_time(offset=100)

Merges the samples and returns the time at which the spike happens. Spike time always startes with 1 and increases with :param: offset.

Parameters:

offset (int) – Shifting the spike index to the given number, if the first index is 0, then it will always be 1

Return type:

list

Returns:

Spike time with an offset.

get_spikes()[source]

Returns BSA spikes.

Return type:

ndarray

Return output:

Returns an multidimensional ndarray where each ndarray of ndarray is a sample.

See ReadData on how to setup the files.

>>> spike_object = BSA()
>>> spike_object.get_spikes()
[[[1 1 1 ... 0 0 0]
  [1 1 1 ... 1 1 1]
  [1 1 1 ... 0 1 1]
  ...
  [1 1 1 ... 1 1 1]
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]]
 [[0 1 1 ... 1 1 1]
  [1 1 1 ... 1 1 1]
  [0 1 0 ... 1 0 1]
  ...
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]
  [0 0 0 ... 0 0 0]]]

Threshold Based Representation

class spikes.encoder.TBR(data, threshold=0.5)[source]

Threshold Based Representation encoding [1]

Parameters:
  • data (numpy.ndarray) – See :py:mod: nucube.utils.ReadCSV

  • threshold (float) – Threshold to cut off spike detection.

get_spike_time(offset=100)

Merges the samples and returns the time at which the spike happens. Spike time always startes with 1 and increases with :param: offset.

Parameters:

offset (int) – Shifting the spike index to the given number, if the first index is 0, then it will always be 1

Return type:

list

Returns:

Spike time with an offset.

get_spikes()[source]

Returns TBR spikes.

Return type:

numpy.ndarray

Returns:

spikes

>>> spikes = TBR()
>>> spikes.get_spikes()
    [[[0 0 0 ..., 0 0 0]
    [1 1 1 ..., 1 1 1]
    [0 0 1 ..., 1 0 1]
    ...,
    [0 0 0 ..., 1 0 0]
    [0 0 0 ..., 0 0 1]
    [0 0 0 ..., 0 0 0]]
    |
    [[0 0 0 ..., 0 0 0]
    [0 0 0 ..., 0 0 0]
    [0 1 0 ..., 0 1 1]
    ...,
    [0 0 0 ..., 0 0 0]
    [0 0 0 ..., 0 0 1]
    [1 0 1 ..., 0 0 1]]]

Step Forward

class spikes.encoder.StepForward(data, threshold=None)[source]

Step Forward (SF) encoding [3].

Each feature has a baseline that starts at its first value. When the signal rises above baseline + threshold a positive spike (1) is emitted and the baseline moves up by threshold; when it falls below baseline - threshold a negative spike (-1) is emitted and the baseline moves down. The spikes can be decoded back to a staircase approximation of the signal: first value + threshold * cumulative sum of spikes.

The threshold is in the units of the data and is the only parameter. By default it is set for each feature of every sample to 1.5 times the mean absolute change between time steps. This factor is not taken from the paper; it was chosen empirically, by how well the spikes reconstruct EEG (tests/test-data).

Parameters:
  • data (ndarray) – See :py:mod: nucube.utils.ReadCSV.get_samples()

  • threshold (float) – Threshold in the units of the data. It can also be a list of threshold according to the numbers of input features. If not given, it is 1.5 times the mean absolute change between time steps, for each feature of every sample.

get_spike_time(offset=100)

Merges the samples and returns the time at which the spike happens. Spike time always startes with 1 and increases with :param: offset.

Parameters:

offset (int) – Shifting the spike index to the given number, if the first index is 0, then it will always be 1

Return type:

list

Returns:

Spike time with an offset.

get_spikes()[source]

Returns Step Forward spikes.

Return type:

ndarray

Returns:

Spikes (1, -1 or 0) with the same shape as the data. The first time step never spikes.