Skip to content

Commit 6feb581

Browse files
committed
add gifti image class function agg_data
1 parent 10ad38a commit 6feb581

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

nibabel/gifti/gifti.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,43 @@ def get_arrays_from_intent(self, intent):
680680
it = intent_codes.code[intent]
681681
return [x for x in self.darrays if x.intent == it]
682682

683+
def agg_data(self, intent_code=None):
684+
"""
685+
Retrun a numpy arrary of aggregated GiftiDataArray of the same intent code
686+
or
687+
Retrun GiftiDataArray in tuples for surface files
688+
689+
Parameters
690+
----------
691+
intent_code : None, string, integer or tuple of string, optional
692+
Intent code, or string describing code.
693+
Accept tuple that contains multiple intents to specify the order.
694+
695+
Returns
696+
-------
697+
all_data : tuple of ndarray or ndarray
698+
If the input is a tuple, the returned tuple will match the order.
699+
"""
700+
701+
# Allow multiple intents to specify the order
702+
# e.g., agg_data(('pointset', 'triangle')) ensures consistent order
703+
704+
if isinstance(intent_code, tuple):
705+
return tuple(self.agg_data(intent_code=code) for code in intent_code)
706+
707+
darrays = self.darrays if intent_code is None else self.get_arrays_from_intent(intent_code)
708+
all_data = tuple(da.data for da in darrays)
709+
all_intent = tuple(intent_codes.niistring[da.intent] for da in darrays)
710+
711+
# Gifti files allows usually one or more data array of the same intent code
712+
# surf.gii is a special case of having two data array of different intent code
713+
714+
if (self.numDA > 1 and all(el == all_intent[0] for el in all_intent)):
715+
return np.column_stack(all_data)
716+
else:
717+
return all_data
718+
719+
683720
@deprecate_with_version(
684721
'getArraysFromIntent method deprecated. '
685722
"Use get_arrays_from_intent instead.",

0 commit comments

Comments
 (0)