Skip to content

Commit 99a589c

Browse files
committed
REWORK: Drop surface density input, add grayordinates input
1 parent e2827cf commit 99a589c

File tree

2 files changed

+25
-29
lines changed

2 files changed

+25
-29
lines changed

niworkflows/interfaces/cifti.py

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ class _GenerateCiftiInputSpec(BaseInterfaceInputSpec):
8282
usedefault=True,
8383
desc="CIFTI surface target space",
8484
)
85-
surface_density = traits.Enum(
86-
"32k", "59k", desc="Surface vertices density."
85+
grayordinates = traits.Enum(
86+
"91k", "170k", usedefault=True, desc="Final CIFTI grayordinates"
8787
)
8888
TR = traits.Float(mandatory=True, desc="Repetition time")
8989
surface_bolds = traits.List(
@@ -94,10 +94,8 @@ class _GenerateCiftiInputSpec(BaseInterfaceInputSpec):
9494

9595

9696
class _GenerateCiftiOutputSpec(TraitedSpec):
97-
out_file = File(exists=True, desc="generated CIFTI file")
98-
out_metadata = File(exists=True, desc="variant metadata JSON")
99-
variant = traits.Str(desc="Name of variant space")
100-
density = traits.Str(desc="Total number of grayordinates")
97+
out_file = File(desc="generated CIFTI file")
98+
out_metadata = File(desc="CIFTI metadata JSON")
10199

102100

103101
class GenerateCifti(SimpleInterface):
@@ -108,7 +106,8 @@ class GenerateCifti(SimpleInterface):
108106
output_spec = _GenerateCiftiOutputSpec
109107

110108
def _run_interface(self, runtime):
111-
surface_labels, volume_labels, metadata = _prepare_cifti(self.inputs.surface_density)
109+
110+
surface_labels, volume_labels, metadata = _prepare_cifti(self.inputs.grayordinates)
112111
self._results["out_file"] = _create_cifti_image(
113112
self.inputs.bold_file,
114113
volume_labels,
@@ -167,14 +166,14 @@ def _run_interface(self, runtime):
167166
return runtime
168167

169168

170-
def _prepare_cifti(surface_density: str) -> typing.Tuple[list, str, dict]:
169+
def _prepare_cifti(grayordinates: str) -> typing.Tuple[list, str, dict]:
171170
"""
172171
Fetch the required templates needed for CIFTI-2 generation, based on input surface density.
173172
174173
Parameters
175174
----------
176-
surface_density :
177-
Surface density (required for `fsLR` surfaces)
175+
grayordinates :
176+
Total CIFTI grayordinates (91k, 170k)
178177
179178
Returns
180179
-------
@@ -187,7 +186,7 @@ def _prepare_cifti(surface_density: str) -> typing.Tuple[list, str, dict]:
187186
188187
Examples
189188
--------
190-
>>> surface_labels, volume_labels, metadata = _prepare_cifti('32k')
189+
>>> surface_labels, volume_labels, metadata = _prepare_cifti('91k')
191190
>>> surface_labels # doctest: +ELLIPSIS
192191
['.../tpl-fsLR_hemi-L_den-32k_desc-nomedialwall_dparc.label.gii', \
193192
'.../tpl-fsLR_hemi-R_den-32k_desc-nomedialwall_dparc.label.gii']
@@ -199,24 +198,27 @@ def _prepare_cifti(surface_density: str) -> typing.Tuple[list, str, dict]:
199198
200199
"""
201200

202-
vertices_key = {
203-
"32k": {
201+
grayord_key = {
202+
"91k": {
203+
"surface-den": "32k",
204204
"tf-res": "02",
205205
"grayords": "91,282",
206206
"res-mm": "2mm"
207207
},
208-
"59k": {
208+
"170k": {
209+
"surface-den": "59k",
209210
"tf-res": "06",
210211
"grayords": "170,494",
211212
"res-mm": "1.6mm"
212213
}
213214
}
214-
if surface_density not in vertices_key:
215-
raise NotImplementedError("Density {surface_density} is not supported.")
215+
if grayordinates not in grayord_key:
216+
raise NotImplementedError("Grayordinates {grayordinates} is not supported.")
216217

217-
tf_vol_res = vertices_key[surface_density]['tf-res']
218-
grayords = vertices_key[surface_density]['grayords']
219-
res_mm = vertices_key[surface_density]['res-mm']
218+
tf_vol_res = grayord_key[grayordinates]['tf-res']
219+
total_grayords = grayord_key[grayordinates]['grayords']
220+
res_mm = grayord_key[grayordinates]['res-mm']
221+
surface_density = grayord_key[grayordinates]['surface-den']
220222
# Fetch templates
221223
surface_labels = [
222224
str(
@@ -248,7 +250,7 @@ def _prepare_cifti(surface_density: str) -> typing.Tuple[list, str, dict]:
248250
)
249251
metadata = {
250252
"Density": (
251-
f"{grayords} grayordinates corresponding to all of the grey matter sampled at a "
253+
f"{total_grayords} grayordinates corresponding to all of the grey matter sampled at a "
252254
f"{res_mm} average vertex spacing on the surface and as {res_mm} voxels subcortically"
253255
),
254256
"SpatialReference": {
@@ -257,7 +259,6 @@ def _prepare_cifti(surface_density: str) -> typing.Tuple[list, str, dict]:
257259
"CIFTI_STRUCTURE_RIGHT_CORTEX": surfaces_url % "R",
258260
}
259261
}
260-
261262
return surface_labels, volume_label, metadata
262263

263264

@@ -319,13 +320,8 @@ def _create_cifti_image(
319320
# currently only supports L/R cortex
320321
surf_ts = nb.load(bold_surfs[hemi == "RIGHT"])
321322
surf_verts = len(surf_ts.darrays[0].data)
322-
if surface_labels[0].endswith(".annot"):
323-
annot = nb.freesurfer.read_annot(surface_labels[hemi == "RIGHT"])
324-
# remove medial wall
325-
medial = np.nonzero(annot[0] != annot[2].index(b"unknown"))[0]
326-
else:
327-
annot = nb.load(surface_labels[hemi == "RIGHT"])
328-
medial = np.nonzero(annot.darrays[0].data)[0]
323+
labels = nb.load(surface_labels[hemi == "RIGHT"])
324+
medial = np.nonzero(labels.darrays[0].data)[0]
329325
# extract values across volumes
330326
ts = np.array([tsarr.data[medial] for tsarr in surf_ts.darrays])
331327

niworkflows/interfaces/tests/test_cifti.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def test_GenerateCifti(tmpdir, cifti_data):
3636
gen = GenerateCifti(
3737
bold_file=bold_volume,
3838
surface_bolds=bold_surfaces,
39-
surface_density='32k',
39+
grayordinates="91k",
4040
TR=1,
4141
)
4242
res = gen.run().outputs

0 commit comments

Comments
 (0)