An nibabel question, sorry if doesn’t belong here -
I’ve a cii dtseries file and I’d like to pull only a few brain structures of interest out of it and produce a smaller file with only those brain structures while keeping the format. There is probably an easy way to do that.
I think this post here might answer your question?
I saw this - not quite. It explains how to extract the timeseries data, which I’ve found pretty straightforward. However, unless I’ve missed dearly, it doesn’t explain how to set the header correctly so that data can be saved in a new file - which is what I’m not sure how to do.
If it’s not clear from the question I’ll edit…
And still thanks for the answer
Unfortunately, nothing with CIFTI is easy, but this shouldn’t be too difficult with the CIFTI Axes API.
The rough approach you’ll want is:
# This is a complete representation of the header
axes = [cifti.header.get_axis(i) for i in range(cifti.ndim)]
# You'll want the brain model axis
time_axis, brain_model_axis = axes
# Select the structures you want
structure_names = [...] # List of structure names
# Iter_structures takes concatenated structures, and returns one at a time
brain_models = [bm for bm in brain_model_axis.iter_structures()
if bm in structure_names]
# bm = (structure_name, slice object, BrainModelAxis)
# Slice your data object with each model, concatenate time series
new_dataobj = numpy.concatenate(
[cifti.dataobj[:, bm] for bm in brain_models], axis=1)
# BrainModelAxes can be added together to form a composite axis
new_brain_model_axis = sum(
(bm for bm in brain_models[1:]), brain_models)
# And we're done
new_cifti = Cifti2Image(new_dataobj,
I haven’t tested this, but it shouldn’t be terribly far off.