Skip to content

Commit

Permalink
Return outcomes as first dimension in activation matrix
Browse files Browse the repository at this point in the history
Harmonizes return value dimension order with activation dict of dicts and weight
matrices.
So it's always weights[outcome][cue] and activations[outcome][event]
  • Loading branch information
dekuenstle committed Apr 18, 2017
1 parent c4ac540 commit 98f0301
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
12 changes: 6 additions & 6 deletions pyndl/activation.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def activation(event_list, weights, number_of_threads=1, remove_duplicates=None,
Returns
-------
activations : xarray.DataArray
with dimensions 'events' and 'outcomes'. Contains coords for the outcomes.
with dimensions 'outcomes' and 'events'. Contains coords for the outcomes.
returned if weights is instance of xarray.DataArray
or
Expand Down Expand Up @@ -84,7 +84,7 @@ def enforce_no_duplicates(cues):
coords={
'outcomes': outcomes
},
dims=('events', 'outcomes'))
dims=('outcomes', 'events'))
elif isinstance(weights, dict):
assert number_of_threads == 1, "Estimating activations with multiprocessing is not implemented for dicts."
activations = defaultdict(lambda: np.zeros(len(event_cues_list)))
Expand Down Expand Up @@ -118,7 +118,7 @@ def _run_mp_activation_matrix(event_index, cue_indices):
Calculate activation for all outcomes while a event.
"""
activations[event_index, :] = weights[:, cue_indices].sum(axis=1)
activations[:, event_index] = weights[:, cue_indices].sum(axis=1)


def _activation_matrix(indices_list, weights, number_of_threads):
Expand All @@ -139,16 +139,16 @@ def _activation_matrix(indices_list, weights, number_of_threads):
Returns
-------
activation_matrix : numpy.array
estimated activations as matrix with shape (events, outcomes)
estimated activations as matrix with shape (outcomes, events)
"""
assert number_of_threads >= 1, "Can't run with less than 1 thread"

activations_dim = (len(indices_list), weights.shape[0])
activations_dim = (weights.shape[0], len(indices_list))
if number_of_threads == 1:
activations = np.empty(activations_dim, dtype=np.float64)
for row, event_cues in enumerate(indices_list):
activations[row, :] = weights[:, event_cues].sum(axis=1)
activations[:, row] = weights[:, event_cues].sum(axis=1)
return activations
else:
shared_activations = mp.RawArray(ctypes.c_double, int(np.prod(activations_dim)))
Expand Down
4 changes: 2 additions & 2 deletions tests/test_activation.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def test_activation_matrix():
(['c1', 'c3'], []),
(['c2'], []),
(['c1', 'c1'], [])]
reference_activations = np.array([[1, 1], [0, 1], [1, 0], [0, 1]])
reference_activations = np.array([[1, 1], [0, 1], [1, 0], [0, 1]]).T

with pytest.raises(ValueError):
activations = activation(events, weights, number_of_threads=1)
Expand All @@ -70,7 +70,7 @@ def test_ignore_missing_cues():
(['c1', 'c3'], []),
(['c2', 'c4'], []),
(['c1', 'c1'], [])]
reference_activations = np.array([[1, 1], [0, 1], [1, 0], [0, 1]])
reference_activations = np.array([[1, 1], [0, 1], [1, 0], [0, 1]]).T

with pytest.raises(KeyError):
activations = activation(events, weights, number_of_threads=1,
Expand Down

0 comments on commit 98f0301

Please sign in to comment.