Skip to content

Commit a2ea2cf

Browse files
authored
Merge pull request #93 from rmarkello/hemispins
[ENH] Allows spins with only one hemisphere
2 parents 4fb4f78 + e9e6ab2 commit a2ea2cf

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

netneurotools/stats.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,7 @@ def gen_spinsamples(coords, hemiid, n_rotate=1000, check_duplicates=True,
691691
raise ValueError('Provided `coords` and `hemiid` must have the same '
692692
'length. Provided lengths: coords = {}, hemiid = {}'
693693
.format(len(coords), len(hemiid)))
694-
if np.max(hemiid) != 1 or np.min(hemiid) != 0:
694+
if np.max(hemiid) > 1 or np.min(hemiid) < 0:
695695
raise ValueError('Hemiid must have values in {0, 1} denoting left and '
696696
'right hemisphere coordinates, respectively. '
697697
+ 'Provided array contains values: {}'
@@ -719,6 +719,8 @@ def gen_spinsamples(coords, hemiid, n_rotate=1000, check_duplicates=True,
719719
for h, rot in enumerate(_gen_rotation(seed=seed)):
720720
hinds = (hemiid == h)
721721
coor = coords[hinds]
722+
if len(coor) == 0:
723+
continue
722724

723725
# if we need an "exact" mapping (i.e., each node needs to be
724726
# assigned EXACTLY once) then we have to calculate the full

netneurotools/tests/test_stats.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -137,20 +137,24 @@ def test_gen_spinsamples():
137137
# generate "normal" test spins
138138
spins, cost = stats.gen_spinsamples(coords, hemi, n_rotate=10, seed=1234,
139139
return_cost=True)
140-
assert spins.shape == (len(coords), 10)
141-
assert cost.shape == (len(coords), 10)
140+
assert spins.shape == spins.shape == (len(coords), 10)
142141

143-
# confirm that `exact` parameter functions as desired
142+
# confirm that `method` parameter functions as desired
144143
for method in ['vasa', 'hungarian']:
145144
spin_exact, cost_exact = stats.gen_spinsamples(coords, hemi,
146145
n_rotate=10, seed=1234,
147146
method=method,
148147
return_cost=True)
149-
assert spin_exact.shape == (len(coords), 10)
150-
assert cost.shape == (len(coords), 10)
148+
assert spin_exact.shape == cost.shape == (len(coords), 10)
151149
for s in spin_exact.T:
152150
assert len(np.unique(s)) == len(s)
153151

152+
# check that one hemisphere works
153+
mask = hemi == 0
154+
spins, cost = stats.gen_spinsamples(coords[mask], hemi[mask], n_rotate=10,
155+
seed=1234, return_cost=True)
156+
assert spins.shape == cost.shape == (len(coords[mask]), 10)
157+
154158
# confirm that check_duplicates will raise warnings
155159
# since spins aren't exact permutations we need to use 4C4 with repeats
156160
# and then perform one more rotation than that number (i.e., 35 + 1)
@@ -169,8 +173,3 @@ def test_gen_spinsamples():
169173
# different length coords and hemi
170174
with pytest.raises(ValueError):
171175
stats.gen_spinsamples(coords, hemi[:-1])
172-
173-
# only one hemisphere
174-
# TODO: should this be allowed?
175-
with pytest.raises(ValueError):
176-
stats.gen_spinsamples(coords[hemi == 0], hemi[hemi == 0])

0 commit comments

Comments
 (0)