Skip to content

Commit

Permalink
Add import_dipole function
Browse files Browse the repository at this point in the history
  • Loading branch information
rythorpe committed Apr 2, 2020
1 parent 26d23a0 commit a1d4219
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
2 changes: 1 addition & 1 deletion hnn_core/__init__.py
Expand Up @@ -2,7 +2,7 @@

load_custom_mechanisms()

from .dipole import simulate_dipole
from .dipole import simulate_dipole, import_dipole
from .feed import ExtFeed
from .params import Params, read_params
from .network import Network
Expand Down
20 changes: 19 additions & 1 deletion hnn_core/dipole.py
Expand Up @@ -133,6 +133,24 @@ def simulate_dipole(net, n_trials=1, n_jobs=1):
net.spikegids = spikegids
return dpl

def import_dipole(fname, units='nAm'):
"""Read dipole values from a file and create a Dipole instance.
Parameters
----------
fname : str
Full path to the input file (.txt)
Returns
-------
dpl : Dipole
The instance of Dipole class
"""
dpl_data = np.loadtxt(fname, dtype=float)
dpl = Dipole(dpl_data[:, 0], dpl_data[:, 1:4])
if units=='nAm':
dpl.units = units
return dpl

class Dipole(object):
"""Dipole class.
Expand Down Expand Up @@ -277,7 +295,7 @@ def write(self, fname):
Outputs
-------
txt file at fname where rows correspond to samples
and columns, delimited by \\t, correspond to
and columns, delimited by \\t, correspond to
1) time (s),
2) aggregate current dipole (scaled nAm),
3) L2/3 current dipole (scaled nAm), and
Expand Down
5 changes: 2 additions & 3 deletions hnn_core/network.py
Expand Up @@ -557,23 +557,22 @@ def write_spikes(self, fname, trial_idx=None):
with open(fname, 'w') as f:
for spk_idx in range(len(spiketimes)):
f.write('{:.3f}\t{}\t{}\n'.format(spiketimes[spk_idx],
int(spikegids[spk_idx]), gidtypes[spk_idx]))
int(spikegids[spk_idx]), gidtypes[spk_idx]))

def read_spikes(self, fname, append_trial=True):
"""Read spike times from a file.
Parameters
----------
fname : str
Full path to the output file (.txt)
Full path to the input file (.txt)
append_trials : bool
If True, append the contents of fname
(i.e., as a trial) to the spike-related
attributes of Network instance. If False,
all spikes of the Network instance will
be overwritten.
"""

spike_data = np.loadtxt(fname, dtype=str)
spiketimes = spike_data[:,0].astype(float)
spikegids = spike_data[:,1].astype(float)
Expand Down

0 comments on commit a1d4219

Please sign in to comment.