Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MRG] Fix average_dipoles to allow for one trial #368

Merged
merged 9 commits into from Jun 27, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/whats_new.rst
Expand Up @@ -41,6 +41,8 @@ Bug

- Fix bug where depth of L5 and L2 cells were swapped, by `Christopher Bailey`_ in `#352 <https://github.com/jonescompneurolab/hnn-core/pull/352>`_

- Fix bug where :func:`~hnn_core.average_dipole` failed when there were less than two dipoles in the input dipole list, by `Kenneth Loi`_ in `#368 <https://github.com/jonescompneurolab/hnn-core/pull/368>`_

API
~~~
- New API for defining cell-cell connections. Custom connections can be added with :func:`~hnn_core.Network.add_connection`, by `Nick Tolley`_ in `#276 <https://github.com/jonescompneurolab/hnn-core/pull/276>`_
Expand Down
9 changes: 4 additions & 5 deletions hnn_core/dipole.py
Expand Up @@ -112,12 +112,11 @@ def average_dipoles(dpls):
A new dipole object with each component of `dpl.data` representing the
average over the same components in the input list
"""
# need at least one Dipole to get times
if len(dpls) < 2:
raise ValueError("Need at least two dipole object to compute an"
" average")

for dpl_idx, dpl in enumerate(dpls):
if not isinstance(dpl, Dipole):
raise ValueError(
f"All elements in the list should be instances of "
f"Dipole. Got {type(dpl)}")
if dpl.nave > 1:
raise ValueError("Dipole at index %d was already an average of %d"
" trials. Cannot reaverage" %
Expand Down
20 changes: 20 additions & 0 deletions hnn_core/tests/test_dipole.py
Expand Up @@ -56,7 +56,27 @@ def test_dipole(tmpdir, run_hnn_core_fixture):
"average of 2 trials"):
dipole_avg = average_dipoles([dipole_avg, dipole_read])

# average an n_of_1 dipole list
single_dpl_avg = average_dipoles([dipole])
for dpl_key in single_dpl_avg.data.keys():
assert_allclose(
dipole_read.data[dpl_key],
single_dpl_avg.data[dpl_key],
rtol=0,
atol=0.000051)

# average dipole list with one dipole object and a zero dipole object
n_times = len(dipole_read.data['agg'])
dpl_null = Dipole(np.zeros(n_times, ), np.zeros((n_times, 3)))
dpl_1 = [dipole, dpl_null]
dpl_avg = average_dipoles(dpl_1)
for dpl_key in dpl_avg.data.keys():
assert_allclose(dpl_1[0].data[dpl_key] / 2., dpl_avg.data[dpl_key])

# test postproc

kenloi marked this conversation as resolved.
Show resolved Hide resolved
# XXX all below to be deprecated in 0.3

kenloi marked this conversation as resolved.
Show resolved Hide resolved
dpls_raw, net = run_hnn_core_fixture(backend='joblib', n_jobs=1,
reduced=True, record_isoma=True,
record_vsoma=True)
Expand Down