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
...@@ -32,22 +32,21 @@ and file names: ...@@ -32,22 +32,21 @@ and file names:
""" """
import os import os
import os.path as op import os.path as op
import itertools as it import itertools as it
import json import json
import string import string
import logging import logging
import tempfile import tempfile
import six from pathlib import Path
import numpy as np from typing import Union
import numpy as np
import nibabel as nib import nibabel as nib
import nibabel.fileslice as fileslice import nibabel.fileslice as fileslice
from pathlib import Path
import fsl.utils.meta as meta import fsl.utils.meta as meta
import fsl.transform.affine as affine import fsl.transform.affine as affine
import fsl.utils.notifier as notifier import fsl.utils.notifier as notifier
...@@ -58,6 +57,10 @@ import fsl.data.constants as constants ...@@ -58,6 +57,10 @@ import fsl.data.constants as constants
import fsl.data.imagewrapper as imagewrapper import fsl.data.imagewrapper as imagewrapper
PathLike = Union[str, Path]
ImageSource = Union[PathLike, nib.Nifti1Image, np.ndarray, 'Image']
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
...@@ -993,21 +996,21 @@ class Image(Nifti): ...@@ -993,21 +996,21 @@ class Image(Nifti):
def __init__(self, def __init__(self,
image, image : ImageSource,
name=None, name : str = None,
header=None, header : nib.Nifti1Header = None,
xform=None, xform : np.ndarray = None,
loadData=True, loadData : bool = True,
calcRange=True, calcRange : bool = True,
threaded=False, threaded : bool = False,
dataSource=None, dataSource : PathLike = None,
loadMeta=False, loadMeta : bool = False,
**kwargs): **kwargs):
"""Create an ``Image`` object with the given image data or file name. """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, :arg image: A string containing the name of an image file to load,
or a Path object pointing to an image file, or a or a Path object pointing to an image file, or a
:mod:`numpy` array, or a :mod:`nibabel` image object, :mod:`numpy` array, or a :mod:`nibabel` image object,
or an ``Image`` object. or an ``Image`` object.
:arg name: A name for the image. :arg name: A name for the image.
...@@ -1086,16 +1089,11 @@ class Image(Nifti): ...@@ -1086,16 +1089,11 @@ class Image(Nifti):
header.set_qform(xform, code=qform) header.set_qform(xform, code=qform)
# The image parameter may be the name of an image file # 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)) image = op.abspath(addExt(image))
nibImage = nib.load(image, **kwargs) nibImage = nib.load(image, **kwargs)
dataSource = image dataSource = image
saved = True 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, # Or a numpy array - we wrap it in a nibabel image,
# with an identity transformation (each voxel maps # with an identity transformation (each voxel maps
...@@ -1141,15 +1139,13 @@ class Image(Nifti): ...@@ -1141,15 +1139,13 @@ class Image(Nifti):
nibImage = image nibImage = image
# Figure out the name of this image, if # 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 name is None:
# If this image was loaded # If this image was loaded
# from disk, use the file name. # from disk, use the file name.
if isinstance(image, six.string_types): if isinstance(image, (str, Path)):
name = removeExt(op.basename(image)) name = removeExt(op.basename(image))
elif isinstance(image, Path):
name = image.name
# Or the image was created from a numpy array # Or the image was created from a numpy array
elif isinstance(image, np.ndarray): 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