Commit f53bb6f8 authored by Paul McCarthy's avatar Paul McCarthy 🚵
Browse files

Merge branch 'patch-image' into 'master'

ENH: accept pathlib objects for fsl.data.image.Image

See merge request fsl/fslpy!292
parents 35fae740 cf109cfa
......@@ -46,6 +46,8 @@ import numpy as np
import nibabel as nib
import nibabel.fileslice as fileslice
from pathlib import Path
import fsl.utils.meta as meta
import fsl.transform.affine as affine
import fsl.utils.notifier as notifier
......@@ -1004,8 +1006,9 @@ class Image(Nifti):
"""Create an ``Image`` object with the given image data or file name.
:arg image: A string containing the name of an image file to load,
or a :mod:`numpy` array, or a :mod:`nibabel` image
object, or an ``Image`` object.
or a Path object pointing to an image file, or a
:mod:`numpy` array, or a :mod:`nibabel` image object,
or an ``Image`` object.
:arg name: A name for the image.
......@@ -1088,6 +1091,11 @@ class Image(Nifti):
nibImage = nib.load(image, **kwargs)
dataSource = image
saved = True
# The image parameter may be a Path object pointing to an image file
elif isinstance(image, Path):
nibImage = nib.load(image, **kwargs)
dataSource = str(image)
saved = True
# Or a numpy array - we wrap it in a nibabel image,
# with an identity transformation (each voxel maps
......@@ -1140,6 +1148,8 @@ class Image(Nifti):
# from disk, use the file name.
if isinstance(image, six.string_types):
name = removeExt(op.basename(image))
elif isinstance(image, Path):
name = image.name
# Or the image was created from a numpy array
elif isinstance(image, np.ndarray):
......
......@@ -12,6 +12,8 @@ import json
import os.path as op
import itertools as it
from pathlib import Path
import pytest
import numpy as np
......@@ -148,6 +150,7 @@ def test_load():
# Not raising an error means the test passes
for fname in shouldPass:
fslimage.Image(op.join(testdir, fname))
fslimage.Image(Path(testdir) / fname)
# These should raise an error
for fname, exc in shouldRaise:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment