Skip to content

Commit de24e72

Browse files
committed
two array combinations fix
1 parent 598d885 commit de24e72

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

neurodsp/rhythm/phase.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ def pairwise_phase_consistency(pha0, pha1=None, return_pairs=True, progress=None
2828
distance_avg : float
2929
Average pairwise circular distance index.
3030
distances : 2d array, optional
31-
Pairwise circular distance indices. Only returned if ``return_pairs` is True. If
32-
``memory_gb`` is less than required array size, None will be returned.
31+
Pairwise circular distance indices. Only returned if ``return_pairs` is True.
3332
3433
Notes
3534
-----
@@ -63,15 +62,15 @@ def pairwise_phase_consistency(pha0, pha1=None, return_pairs=True, progress=None
6362

6463
else:
6564

66-
n_combs = int((len(pha0) * (len(pha0) + 1)) / 2)
65+
# Include all combinations
66+
n_combs = int(len(pha0) ** 2)
6767

68-
# Include self-combinations
69-
iterable = enumerate(combinations_with_replacement(np.arange(len(pha0)), 2))
68+
iterable = enumerate((row, col) for row in range(len(pha0)) for col in range(len(pha1)))
7069

7170
# Initialize variables
7271
if return_pairs:
7372
cumulative = None
74-
distances = np.zeros(n_combs)
73+
distances = np.ones((len(pha0), len(pha0)))
7574
else:
7675
cumulative = 0
7776
distances = None
@@ -88,7 +87,7 @@ def pairwise_phase_consistency(pha0, pha1=None, return_pairs=True, progress=None
8887
# Compute distance indices
8988
for idx, pair in iterable:
9089

91-
phi0= pha0[pair[0]]
90+
phi0 = pha0[pair[0]]
9291

9392
if pha1 is None:
9493
phi1 = pha0[pair[1]]
@@ -108,8 +107,11 @@ def pairwise_phase_consistency(pha0, pha1=None, return_pairs=True, progress=None
108107
# Pairwise circular distance index (PCDI)
109108
distance = (np.pi - 2 * abs_dist) / np.pi
110109

111-
if isinstance(distances, np.ndarray):
112-
distances[idx] = distance
110+
if isinstance(distances, np.ndarray) and pha1 is None:
111+
distances[pair[0], pair[1]] = distance
112+
distances[pair[1], pair[0]] = distance
113+
elif isinstance(distances, np.ndarray) and pha1 is not None:
114+
distances[pair[0], pair[1]] = distance
113115
else:
114116
cumulative += distance
115117

neurodsp/tests/rhythm/test_phase.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def test_pairwise_phase_consistency(tsig_sine, return_pairs, phase_shift):
3232
dist_avg, dists = dist_avg[0], dist_avg[1]
3333

3434
assert isinstance(dists, np.ndarray)
35-
assert len(dists) == (len(peaks) * (len(peaks) + 1)) / 2
35+
assert len(dists[0]) * len(dists[1]) == len(peaks) ** 2
3636
assert np.mean(dists) == dist_avg
3737

3838
# Expected consistency
@@ -50,7 +50,7 @@ def test_pairwise_phase_consistency(tsig_sine, return_pairs, phase_shift):
5050
dist_avg, dists = pairwise_phase_consistency(pha0[peaks], return_pairs=True)
5151

5252
assert dist_avg == 1
53-
assert len(dists) == (len(peaks) * (len(peaks) - 1)) / 2
53+
assert len(dists[0]) == len(dists[1]) == len(peaks)
5454

5555
# Cases where arrays are invalid sizes
5656
try:

0 commit comments

Comments
 (0)