Skip to content
Merged
Changes from 3 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
6feb581
add gifti image class function agg_data
htwangtw Aug 18, 2019
72471a7
PEP8
htwangtw Aug 18, 2019
98c68af
limit stacking to timeseries data only
htwangtw Aug 31, 2019
448e073
Update nibabel/gifti/gifti.py
htwangtw Sep 10, 2019
15d1dd7
Update nibabel/gifti/gifti.py
htwangtw Sep 10, 2019
85f65a4
Update nibabel/gifti/gifti.py
htwangtw Sep 10, 2019
de8f028
Update nibabel/gifti/gifti.py
htwangtw Sep 10, 2019
154321c
doc string draft 1, need to finish the examples
htwangtw Sep 12, 2019
0530484
ENH: Add general test data retriever
effigies Sep 12, 2019
8c6cd7d
DOCTEST: Retreive a surface file using test_data
effigies Sep 12, 2019
c54b696
DATA: Add 10 time point time series GIFTI in fsaverage3 space
effigies Sep 12, 2019
641feaa
TEST: Test new test_data function
effigies Sep 12, 2019
bee6065
Merge pull request #1 from effigies/enh/gifti_test_data
htwangtw Oct 8, 2019
bd95ce8
add doc and example for surface gii files
htwangtw Oct 8, 2019
ad3316f
Merge branch 'master' of https://github.com/htwangtw/nibabel into gif…
htwangtw Oct 10, 2019
aef5a4f
fix formatting; need timeseries example
htwangtw Oct 11, 2019
18501c4
add time series example
htwangtw Oct 11, 2019
51ff298
Correct more formatting usses
htwangtw Oct 11, 2019
b3adb15
docstring passed nosetests
htwangtw Oct 16, 2019
785a8d3
fix: trailing white space and os separator
htwangtw Oct 16, 2019
995d834
fix docstring output
htwangtw Oct 16, 2019
7ea7dec
fix os separater in the test
htwangtw Oct 16, 2019
f633676
Rename example file
htwangtw Oct 16, 2019
bf6eb97
Changing numpy float print style to 1.13
htwangtw Oct 16, 2019
a9471a0
Revert "Rename example file"
htwangtw Oct 21, 2019
9a52b78
Revert "Changing numpy float print style to 1.13"
htwangtw Oct 21, 2019
0447bbc
Revert "Revert "Rename example file""
htwangtw Oct 21, 2019
1ecaa26
Move the docstring to test
htwangtw Oct 21, 2019
384475b
Remove the actual docstring to prevent errors
htwangtw Oct 21, 2019
3fb7003
Revert "Remove the actual docstring to prevent errors"
htwangtw Oct 21, 2019
08a8752
Remove docstring in agg_data
htwangtw Oct 21, 2019
bb7517d
Remove docstring in test
htwangtw Oct 21, 2019
033ca51
add minimum example to docstring
htwangtw Oct 22, 2019
76efe61
add shape gifti
htwangtw Oct 22, 2019
c8c2c43
fix the test with shape gii
htwangtw Oct 23, 2019
654ee5b
delete trailing whitespace
htwangtw Oct 23, 2019
64b019a
DOC: More comprehensive agg_data examples
effigies Oct 27, 2019
5ea1e88
Merge pull request #2 from effigies/doc/agg_data
htwangtw Oct 28, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions nibabel/gifti/gifti.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,45 @@ def get_arrays_from_intent(self, intent):
it = intent_codes.code[intent]
return [x for x in self.darrays if x.intent == it]

def agg_data(self, intent_code=None):
"""
Retrun a numpy arrary of aggregated GiftiDataArray of the same intent code
or
Retrun GiftiDataArray in tuples for surface files

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to add some doctests that demonstrate how to use this. If we include a time series and a surface file, we could follow the examples of get_fdata():

The cache can effect the behavior of the image, because if the cache is
full, or you have an array image, then modifying the returned array
will modify the result of future calls to ``get_fdata()``. For example
you might do this:
>>> import os
>>> import nibabel as nib
>>> from nibabel.testing import data_path
>>> img_fname = os.path.join(data_path, 'example4d.nii.gz')
>>> img = nib.load(img_fname) # This is a proxy image
>>> nib.is_proxy(img.dataobj)
True
The array is not yet cached by a call to "get_fdata", so:
>>> img.in_memory
False
After we call ``get_fdata`` using the default `caching` == 'fill', the
cache contains a reference to the returned array ``data``:
>>> data = img.get_fdata()
>>> img.in_memory
True
We modify an element in the returned data array:
>>> data[0, 0, 0, 0]
0.0
>>> data[0, 0, 0, 0] = 99
>>> data[0, 0, 0, 0]
99.0
The next time we call 'get_fdata', the method returns the cached
reference to the (modified) array:
>>> data_again = img.get_fdata()
>>> data_again is data
True
>>> data_again[0, 0, 0, 0]
99.0
If you had *initially* used `caching` == 'unchanged' then the returned
``data`` array would have been loaded from file, but not cached, and:
>>> img = nib.load(img_fname) # a proxy image again
>>> data = img.get_fdata(caching='unchanged')
>>> img.in_memory
False
>>> data[0, 0, 0] = 99
>>> data_again = img.get_fdata(caching='unchanged')
>>> data_again is data
False
>>> data_again[0, 0, 0, 0]
0.0

I would show aggregating data:

  • without intent code
  • with matching intent codes
  • with mismatching intent codes (should return ())
  • with tuple intent codes

Parameters
----------
intent_code : None, string, integer or tuple of string, optional
Intent code, or string describing code.
Accept tuple that contains multiple intents to specify the order.

Returns
-------
all_data : tuple of ndarray or ndarray
If the input is a tuple, the returned tuple will match the order.
"""

# Allow multiple intents to specify the order
# e.g., agg_data(('pointset', 'triangle')) ensures consistent order

if isinstance(intent_code, tuple):
return tuple(self.agg_data(intent_code=code) for code in intent_code)

darrays = self.darrays if intent_code is None else self.get_arrays_from_intent(intent_code)
all_data = tuple(da.data for da in darrays)
all_intent = {da.intent for da in darrays}

# Gifti files allows usually one or more data array of the same intent code
# surf.gii is a special case of having two data array of different intent code

if self.numDA > 1 and len(all_intent) == 1:
if all_intent== 'NIFTI_INTENT_TIME_SERIES': # stack when the gifti is a timeseries
return np.column_stack(all_data)
else:
return all_data
else:
return all_data

@deprecate_with_version(
'getArraysFromIntent method deprecated. '
"Use get_arrays_from_intent instead.",
Expand Down