Input/Output

Spike/event Input Output and visualization module.

class lava.lib.dl.slayer.io.Event(x_event, y_event, c_event, t_event, payload=None)

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

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

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

  • c (numpy int array): channel index of spike event.

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

  • p (numpy int or double array): payload of spike event. None for binary spike.

  • graded (bool): flag to indicate graded or binary spike.

Parameters
  • x_event (int array) – x location of event

  • y_event (int array or None) – y location of event

  • c_event (int array) – c location of event

  • t_event (int array or float array) – time of event

  • payload (int array or float array or None) – payload of event. None for binary event. Defaults to None.

Examples

>>> td_event = Event(x_event, y_event, c_event, t_event)
anim(fig=None, frame_rate=24, pre_compute_frames=True, repeat=True)

Get animation object for spike event.

Parameters
  • fig (int) – plot figure ID. Defaults to None.

  • frame_rate (int) – frame rate of visualization. Defaults to 24.

  • pre_compute_frames (bool) – flag to enable precomputation of frames for faster visualization. Defaults to True.

  • repeat (bool) – flag to enable repeat of animation. Defaults to False.

Returns

matplotlib anim object.

Return type

anim

Examples

>>> anim = self.anim()
fill_tensor(empty_tensor, sampling_time=1, random_shift=False, binning_mode='OR')

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

Parameters
  • empty_tensor (numpy or torch tensor) – an empty tensor to hold spike data .

  • sampling_time (float) – the width of time bin. Defaults to 1.

  • random_shift (bool) – flag to randomly shift the sample in time. Defaults to False.

  • binning_mode (str) – the way spikes are binned. Options are ‘OR’|’SUM’. If the event is graded binning mode is overwritten to ‘SUM’. Defaults to ‘OR’.

Returns

spike tensor.

Return type

numpy or torch tensor

Examples

>>> spike = td_event.fill_tensor( torch.zeros((2, 240, 180, 5000)) )
show(fig=None, frame_rate=24, pre_compute_frames=True, repeat=False)

Visualizes spike event.

Parameters
  • fig (int) – plot figure ID. Defaults to None.

  • frame_rate (int) – frame rate of visualization. Defaults to 24.

  • pre_compute_frames (bool) – flag to enable precomputation of frames for faster visualization. Defaults to True.

  • repeat (bool) – flag to enable repeat of animation. Defaults to False.

Examples

>>> self.show()
to_tensor(sampling_time=1, dim=None)

Returns a numpy tensor that contains the spike events 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.

Parameters
  • sampling_time (int) – event data sampling time. Defaults to 1.

  • dim (int or None) – desired dimension. It is inferred if None. Defaults to None.

Returns

spike tensor.

Return type

np array

Examples

>>> spike = td_event.to_tensor()
lava.lib.dl.slayer.io.encode_1d_spikes(filename, td_event)

Writes one dimensional binary spike file from a td_event event.

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

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

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

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

Parameters
  • filename (str) – name of spike file.

  • td_event (event) – spike event object

Examples

>>> encode_1d_spikes(file_path, td_event)
lava.lib.dl.slayer.io.encode_2d_spikes(filename, td_event)

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

The binary file is encoded as follows:
  • Each spike event 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 event: 0=>OFF event, 1=>ON event.

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

Parameters
  • filename (str) – name of spike file.

  • td_event (event) – spike event object

Examples

>>> encode_2d_spikes(file_path, td_event)
lava.lib.dl.slayer.io.encode_np_spikes(filename, td_event, fmt='xypt', time_unit=0.001)

Writes td_event event into numpy file.

Parameters
  • filename (str) – name of spike file.

  • td_event (event) – spike event.

  • fmt (str) – format of numpy event ordering. Options are ‘xypt’. Defaults to ‘xypt’.

  • time_unit (float) – scale factor that converts the data to seconds. Defaults to 1e-3.

Examples

>>> encode_np_spikes(file_path, td_event)
>>> encode_np_spikes(file_path, td_event, fmt='xypt')
lava.lib.dl.slayer.io.read_1d_spikes(filename)

Reads one dimensional binary spike file and returns a td_event event.

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

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

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

  • the last 23 bits (bits 22-0) represent the spike event timestamp in

    microseconds.

Parameters

filename (str) – name of spike file.

Returns

spike event.

Return type

Event

Examples

>>> td_event = read_1d_spikes(file_path)
lava.lib.dl.slayer.io.read_2d_spikes(filename)

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

The binary file is encoded as follows:
  • Each spike event 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 event: 0=>OFF event, 1=>ON event.

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

Parameters

filename (str) – name of spike file.

Returns

spike event.

Return type

Event

Examples

>>> td_event = read_2d_spikes(file_path)
lava.lib.dl.slayer.io.read_np_spikes(filename, fmt='xypt', time_unit=0.001)

Reads numpy spike event and returns a td_event event. The numpy array is assumed to be of nEvent x event dimension.

Parameters
  • filename (str) – name of spike file.

  • fmt (str) – format of numpy event ordering. Options are ‘xypt’. Defaults to ‘xypt’.

  • time_unit (float) – scale factor that converts the data to seconds. Defaults to 1e-3.

Returns

spike object.

Return type

Event

Examples

>>> td_event = read_np_spikes(file_path)
>>> td_event = read_np_spikes(file_path, fmt='xypt')
>>> td_event = read_np_spikes(file_path, time_unit=1e-6)
lava.lib.dl.slayer.io.tensor_to_event(spike_tensor, sampling_time=1)

Returns td_event event 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.

Parameters
  • spike_tensor (numpy or torch tensor) – spike tensor.

  • sampling_time (float) – the width of time bin. Defaults to 1.

Returns

spike event

Return type

Event

Examples

>>> td_event = tensor_to_Event(spike)