Trace Data Conversion (sanafe.data)
The sanafe.data module converts SANA-FE trace outputs into other useful
formats, e.g., Pandas.
Conversion functions accept inputs using many different types:
a path or
Pathto a CSV produced bychip.sim()the dict returned by
chip.sim()the raw in-memory values
a
DataFrame(returned as-is)
Quickstart
Build a small network, run a simulation with all traces enabled, and convert each trace into a DataFrame:
import sanafe
import sanafe.data
import pandas
arch, _ = sanafe.load_example()
snn = sanafe.Network()
group = snn.create_neuron_group(
"in", 2, {"bias": 0.5, "threshold": 1.0, "reset": 0.0},
log_spikes=True, log_potential=True)
for neuron in group:
neuron.map_to_core(arch.tiles[0].cores[0])
chip = sanafe.SpikingChip(arch)
chip.load(snn)
results_dict = chip.sim(5, spike_trace=True, potential_trace=True,
perf_trace=True, message_trace=True,
neuron_trace=True)
sanafe.data.spikes_to_raster(results_dict, n_timesteps=5)
sanafe.data.spikes_to_dataframe(results_dict)
sanafe.data.potentials_to_dataframe(results_dict)
sanafe.data.performance_to_dataframe(results_dict)
sanafe.data.messages_to_dataframe(results_dict)
sanafe.data.neuron_traces_to_dataframe(results_dict)
- sanafe.data.spikes_to_raster(source: Any, groups: Sequence[str] | None = None, time_range: Tuple[int, int] | None = None, n_timesteps: int | None = None) Tuple[np.ndarray, list[str], np.ndarray][source]
Convert a spike trace into a dense 2D raster matrix.
- Parameters:
source – The spike trace data. May be a
pandas.DataFrame, a path to a CSV file, the dict returned bychip.sim(), or the rawspike_tracevalues i.e., a per-timestep list, where each element is a list of spiking neurons for that timestep.groups – Optional subset of neuron groups to include. Row order in the returned matrix follows the order given here; when omitted, all groups are included in sorted order.
time_range (tuple(int, int), optional) – Optional
(start, stop)half-open interval of timesteps to include. When omitted, spans from the minimum to maximum timestep present in the data.n_timesteps (int, optional) – Optional explicit number of columns in the matrix. Useful when the trace ends before the simulation does (e.g., no spikes in the final timesteps) and you want the matrix to span the full run. Ignored when
time_rangeis given.
- Returns:
A 3-tuple
(matrix, neuron_ids, timesteps)wherematrixis a 2D booleannumpy.ndarrayof shape(n_neurons, n_timesteps)withTruewhere a spike occurred.neuron_idsis a list of"group.offset"strings labelling each row of the matrix.timestepsis a 1Dnumpy.ndarraygiving the timestep index for each column.
- Return type:
tuple
- Raises:
ValueError – If no spike trace data can be found in
source, or ifgroupscontains unknown group names.
- sanafe.data.spikes_to_dataframe(source: Any) DataFrame[source]
Convert a spike trace into a pandas DataFrame.
- Parameters:
source (pandas.DataFrame or Path or str or dict or list[list[NeuronAddress]]) – The spike trace data. May be a
pandas.DataFrame, a path to a CSV file, the dict returned bychip.sim(), or the rawspike_tracevalue. In memory, spike traces are a per-timestep list, with each entry a list ofNeuronAddressof neurons that fired that timestep.- Returns:
A DataFrame with columns
timestep,group,neuron_offset, andneuron_id.- Return type:
pandas.DataFrame
- Raises:
ValueError – If no spike trace data can be found in
source.
- sanafe.data.potentials_to_dataframe(source: Any, neuron_ids: Sequence[str] | None = None) DataFrame[source]
Convert a membrane-potential trace into a pandas DataFrame indexed by timestep.
- Parameters:
source (pandas.DataFrame or str or Path or dict or list[list[float]]) – The potential trace data. May be a
pandas.DataFrame, a path to a CSV file, the dict returned bychip.sim(), or a 2D array ofpotential_tracevalues (i.e., a per-timestep list, containing per-neuron lists of potentials).neuron_ids (List[str]) – Optional column labels for the neurons. When omitted, columns are named
Neuron 0,Neuron 1, and so on. Only used whensourceis in-memory trace data.
- Returns:
A DataFrame whose index is named
timestepand whose columns correspond to individual neurons.- Return type:
pandas.DataFrame
- Raises:
ValueError – If no potential trace data is found, or if
neuron_idslength does not match the number of trace columns.
- sanafe.data.performance_to_dataframe(source: Any) DataFrame[source]
Convert a performance trace into a DataFrame.
- Parameters:
dict (source (pandas.DataFrame or Path or str or) – The performance trace data. May be a
pandas.DataFrame, a path to a CSV file, the dict from chip.sim() or a dict containing performance data (dict[str, list], i.e. lists of per-timestep values for each metric).- Returns:
A DataFrame of per-timestep performance metrics.
- Return type:
pandas.DataFrame
- Raises:
ValueError – If no performance trace data can be found in
source.
- sanafe.data.messages_to_dataframe(source: Any) DataFrame[source]
Convert a message trace into a flat pandas DataFrame, one row per message.
In addition, convenience identifier columns are derived when their components are present:
src_neuronfromsrc_neuron_group_idandsrc_neuron_offsetsrc_hwfromsrc_tile_idandsrc_core_offsetdest_hwfromdest_tile_idanddest_core_offset
- Parameters:
source (pandas.DataFrame or Path or str or dict or list[list[dict]]) – The message trace data. May be a
pandas.DataFrame, a path to a CSV file, the dict returned bychip.sim(), or the rawmessage_tracevalues.- Returns:
A DataFrame with one row per message.
- Return type:
pandas.DataFrame
- Raises:
ValueError – If no message trace data can be found in
source.