Skip to content

Commit

Permalink
ENH: add new property to access spike times by cell type
Browse files Browse the repository at this point in the history
  • Loading branch information
jasmainak committed Oct 20, 2024
1 parent 18830b5 commit bdef5b9
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
18 changes: 17 additions & 1 deletion examples/workflows/plot_simulate_evoked.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,24 @@
from hnn_core import JoblibBackend

with JoblibBackend(n_jobs=2):
dpls = simulate_dipole(net, tstop=170., n_trials=2)
dpls = simulate_dipole(net, tstop=170., n_trials=1)

import numpy as np

spike_types_data = np.concatenate(np.array(net.cell_response.spike_types, dtype=object))
unique_cell_types = np.unique(spike_types_data) # net.cell_response.cell_types ?

spike_times_by_type = dict()
for cell_type in unique_cell_types:
spike_times_by_type[cell_type] = list()
for trial_spike_times, trial_spike_types in zip(net.cell_response.spike_times, net.cell_response.spike_types):
mask = np.isin(trial_spike_types, cell_type)
trial_cell_spike_times = np.array(trial_spike_times)[mask].tolist()
spike_times_by_type[cell_type].append(trial_cell_spike_times)

# spike_types_mask = {s_type: np.isin(spike_types_data, s_type)
# for s_type in unique_types}
sdfdff
###############################################################################
# Rather than reading smoothing and scaling parameters from file, we recommend
# explicit use of the :meth:`~hnn_core.dipole.Dipole.smooth` and
Expand Down
18 changes: 18 additions & 0 deletions hnn_core/cell_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,24 @@ def __eq__(self, other):
def spike_times(self):
return self._spike_times

@property
def cell_types(self):
"""Get unique cell types."""
spike_types_data = np.concatenate(np.array(self.spike_types, dtype=object))
return np.unique(spike_types_data).tolist()

@property
def spike_times_by_type(self):
"""Get a dictionary of spike times by cell type"""
spike_times = dict()
for cell_type in self.cell_types:
spike_times[cell_type] = list()
for trial_spike_times, trial_spike_types in zip(self.spike_times, self.spike_types):
mask = np.isin(trial_spike_types, cell_type)
trial_cell_spike_times = np.array(trial_spike_times)[mask].tolist()
spike_times[cell_type].append(trial_cell_spike_times)
return spike_times

@property
def spike_gids(self):
return self._spike_gids
Expand Down
5 changes: 5 additions & 0 deletions hnn_core/tests/test_cell_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ def test_cell_response(tmp_path):
spike_gids=spike_gids,
spike_types=spike_types,
times=sim_times)

assert set(cell_response.cell_types) == set(gid_ranges.keys())
assert cell_response.spike_times_by_type['L2_basket'] == [[7.89], []]
assert cell_response.spike_times_by_type['L5_pyramidal'] == [[], [4.2812]]

kwargs_hist = dict(alpha=0.25)
fig = cell_response.plot_spikes_hist(show=False, **kwargs_hist)
assert all(patch.get_alpha() == kwargs_hist['alpha']
Expand Down

0 comments on commit bdef5b9

Please sign in to comment.