Skip to content

Commit 4db1b4d

Browse files
committed
[FIX] Fixes parcel dropping in freesurfer.py
When names in `drop` parameter weren't present in provided annotation files the function(s) would errored; this is now resolved.
1 parent b9ab7ab commit 4db1b4d

File tree

2 files changed

+33
-13
lines changed

2 files changed

+33
-13
lines changed

netneurotools/freesurfer.py

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,10 @@ def find_parcel_centroids(*, lhannot, rhannot, version='fsaverage',
143143
"""
144144

145145
if drop is None:
146-
drop = ['unknown', 'corpuscallosum']
146+
drop = [
147+
'unknown', 'corpuscallosum', # default FreeSurfer
148+
'Background+FreeSurfer_Defined_Medial_Wall' # common alternative
149+
]
147150
drop = _decode_list(drop)
148151

149152
surfaces = fetch_fsaverage(version)[surf]
@@ -169,8 +172,7 @@ def parcels_to_vertices(data, *, lhannot, rhannot, drop=None):
169172
"""
170173
Projects parcellated `data` to vertices defined in annotation files
171174
172-
Assigns np.nan to 'unknown' and 'corpuscallosum' vertices in annotation
173-
files.
175+
Assigns np.nan to all ROIs in `drop`
174176
175177
Parameters
176178
----------
@@ -195,14 +197,20 @@ def parcels_to_vertices(data, *, lhannot, rhannot, drop=None):
195197
"""
196198

197199
if drop is None:
198-
drop = ['unknown', 'corpuscallosum']
200+
drop = [
201+
'unknown', 'corpuscallosum', # default FreeSurfer
202+
'Background+FreeSurfer_Defined_Medial_Wall' # common alternative
203+
]
199204
drop = _decode_list(drop)
200205

201206
start = end = 0
202207
projected = []
203208

204209
# check this so we're not unduly surprised by anything...
205-
expected = sum([len(read_annot(a)[-1]) - 2 for a in [lhannot, rhannot]])
210+
expected = 0
211+
for a in [lhannot, rhannot]:
212+
names = _decode_list(read_annot(a)[-1])
213+
expected += len(names) - len(set(drop) & set(names))
206214
if expected != len(data):
207215
raise ValueError('Number of parcels in provided annotation files '
208216
'differs from size of parcellated data array.\n'
@@ -214,10 +222,12 @@ def parcels_to_vertices(data, *, lhannot, rhannot, drop=None):
214222
# read files and update end index for `data`
215223
labels, ctab, names = read_annot(annot)
216224
names = _decode_list(names)
217-
end += len(names) - 2 # unknown and corpuscallosum
225+
todrop = set(names) & set(drop)
226+
end += len(names) - len(todrop) # unknown and corpuscallosum
218227

219228
# get indices of unknown and corpuscallosum and insert NaN values
220-
inds = [names.index(f) - n for n, f in enumerate(drop)]
229+
inds = sorted([names.index(f) for f in todrop])
230+
inds = [f - n for n, f in enumerate(inds)]
221231
currdata = np.insert(data[start:end], inds, np.nan)
222232

223233
# project to vertices and store
@@ -254,7 +264,10 @@ def vertices_to_parcels(data, *, lhannot, rhannot, drop=None):
254264
"""
255265

256266
if drop is None:
257-
drop = ['unknown', 'corpuscallosum']
267+
drop = [
268+
'unknown', 'corpuscallosum', # default FreeSurfer
269+
'Background+FreeSurfer_Defined_Medial_Wall' # common alternative
270+
]
258271
drop = _decode_list(drop)
259272

260273
start = end = 0
@@ -296,7 +309,7 @@ def vertices_to_parcels(data, *, lhannot, rhannot, drop=None):
296309
currdata = sums / counts
297310

298311
# get indices of unkown and corpuscallosum and delete from parcels
299-
inds = [names.index(f) for f in drop]
312+
inds = sorted([names.index(f) for f in set(drop) & set(names)])
300313
currdata = np.delete(currdata, inds)
301314

302315
# store parcellated data
@@ -387,7 +400,10 @@ def spin_data(data, *, lhannot, rhannot, version='fsaverage', n_rotate=1000,
387400
"""
388401

389402
if drop is None:
390-
drop = ['unknown', 'corpuscallosum']
403+
drop = [
404+
'unknown', 'corpuscallosum', # default FreeSurfer
405+
'Background+FreeSurfer_Defined_Medial_Wall' # common alternative
406+
]
391407

392408
# get coordinates and hemisphere designation for spin generation
393409
coords, hemiid = _get_fsaverage_coords(version, 'sphere')
@@ -472,15 +488,19 @@ def overlap(vals):
472488
return -1
473489

474490
if drop is None:
475-
drop = ['unknown', 'corpuscallosum']
491+
drop = [
492+
'unknown', 'corpuscallosum', # default FreeSurfer
493+
'Background+FreeSurfer_Defined_Medial_Wall' # common alternative
494+
]
476495
drop = _decode_list(drop)
477496

478497
# get vertex-level labels (set drop labels to - values)
479498
vertices, end = [], 0
480499
for n, annot in enumerate([lhannot, rhannot]):
481500
labels, ctab, names = read_annot(annot)
482501
names = _decode_list(names)
483-
inds = [names.index(f) - n for n, f in enumerate(drop)]
502+
todrop = set(names) & set(drop)
503+
inds = [names.index(f) - n for n, f in enumerate(todrop)]
484504
labs = np.arange(len(names) - len(inds)) + (end - (len(inds) * n))
485505
insert = np.arange(-1, -(len(inds) + 1), -1)
486506
vertices.append(np.insert(labs, inds, insert)[labels])

netneurotools/tests/test_freesurfer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
@pytest.fixture(scope='module')
1313
def cammoun_surf(tmp_path_factory):
1414
tmpdir = str(tmp_path_factory.getbasetemp())
15-
return datasets.fetch_cammoun2012('surface', data_dir=tmpdir, verbose=0)
15+
return datasets.fetch_cammoun2012('fsaverage', data_dir=tmpdir, verbose=0)
1616

1717

1818
@pytest.mark.parametrize('scale, parcels', [

0 commit comments

Comments
 (0)