Skip to content

Commit

Permalink
Merge pull request #153 from VinceBaz/ref-2d_load_data
Browse files Browse the repository at this point in the history
[REF] Refactor load_data for gifti images with more than one data array
  • Loading branch information
VinceBaz authored May 22, 2024
2 parents cc2d294 + b2043b3 commit af78397
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions neuromaps/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ def fix_coordsys(fn, val=3):

def load_nifti(img):
"""
Load nifti file `img`.
Load nifti file `img`.
If `img` is already a loaded (i.e. is a nib.Nifti1Image object),
it is returned as-is.
Expand Down Expand Up @@ -196,14 +196,23 @@ def load_data(data):
or not isinstance(data, Iterable)):
data = (data,)

try:
# giimg_like or path_like (gifti)
out = np.hstack([load_gifti(img).agg_data() for img in data])
try: # giimg_like or path_like (gifti)
data_gii = []
for img in data:
data_hemi = load_gifti(img).agg_data()
# More than one data arrays without NIFTI_INTENT_TIMESERIES
if isinstance(data_hemi, tuple):
data_hemi = np.asarray(data_hemi).T
data_gii += [data_hemi]
if np.ndim(data_gii) > 2:
out = np.vstack(data_gii)
else:
out = np.hstack(data_gii)
except (AttributeError, TypeError, ValueError, OSError) as err:
# niimg_like or path_like (nifti)
if (isinstance(err, AttributeError)
or (
"os.PathLike" in str(err)
"os.PathLike" in str(err)
and "not Nifti1Image" in str(err)
)
):
Expand Down

0 comments on commit af78397

Please sign in to comment.