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

RF: Make sure name/dataSource are set the same, regardless of whether image is

created from a str or Path obj
parent f3f3d6a2
......@@ -40,14 +40,13 @@ import string
import logging
import tempfile
import six
import numpy as np
from pathlib import Path
from typing import Union
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
......@@ -58,6 +57,10 @@ import as constants
import as imagewrapper
PathLike = Union[str, Path]
ImageSource = Union[PathLike, nib.Nifti1Image, np.ndarray, 'Image']
log = logging.getLogger(__name__)
......@@ -993,15 +996,15 @@ class Image(Nifti):
def __init__(self,
image : ImageSource,
name : str = None,
header : nib.Nifti1Header = None,
xform : np.ndarray = None,
loadData : bool = True,
calcRange : bool = True,
threaded : bool = False,
dataSource : PathLike = None,
loadMeta : bool = False,
"""Create an ``Image`` object with the given image data or file name.
......@@ -1086,16 +1089,11 @@ class Image(Nifti):
header.set_qform(xform, code=qform)
# The image parameter may be the name of an image file
if isinstance(image, six.string_types):
if isinstance(image, (str, Path)):
image = op.abspath(addExt(image))
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
......@@ -1141,15 +1139,13 @@ class Image(Nifti):
nibImage = image
# Figure out the name of this image, if
# it has not beenbeen explicitly passed in
# it has not been explicitly passed in
if name is None:
# If this image was loaded
# from disk, use the file name.
if isinstance(image, six.string_types):
if isinstance(image, (str, Path)):
name = removeExt(op.basename(image))
elif isinstance(image, Path):
name =
# Or the image was created from a numpy array
elif isinstance(image, np.ndarray):
Supports Markdown
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