Skip to content
Snippets Groups Projects
Commit fae775a5 authored by Paul McCarthy's avatar Paul McCarthy :mountain_bicyclist:
Browse files

Image class can now have metadata added to it. This is used by DicomImage

class.
parent 72612635
No related branches found
No related tags found
No related merge requests found
...@@ -32,6 +32,7 @@ import re ...@@ -32,6 +32,7 @@ import re
import glob import glob
import json import json
import logging import logging
import deprecation
import nibabel as nib import nibabel as nib
...@@ -48,44 +49,63 @@ class DicomImage(fslimage.Image): ...@@ -48,44 +49,63 @@ class DicomImage(fslimage.Image):
DICOM metadata. DICOM metadata.
The ``Image`` class is used to manage the data and the voxel-to-world The ``Image`` class is used to manage the data and the voxel-to-world
transformation. Additional DICOM metadata may be accessed via TODO transformation. Additional DICOM metadata may be accessed via the
:class:`.Image` metadata access methods.
""" """
def __init__(self, image, meta, *args, **kwargs):
def __init__(self, image, metadata, dicomDir, *args, **kwargs):
"""Create a ``DicomImage``. """Create a ``DicomImage``.
:arg image: Passed through to :meth:`.Image.__init__`. :arg image: Passed through to :meth:`.Image.__init__`.
:arg meta: Dictionary containing DICOM meta-data. :arg metadata: Dictionary containing DICOM meta-data.
:arg dicomDir: Directory that the dicom image was loaded from.
""" """
fslimage.Image.__init__(self, image, *args, **kwargs) fslimage.Image.__init__(self, image, *args, **kwargs)
self.__meta = meta
self.__dicomDir = dicomDir
if metadata is not None:
for k, v in metadata.items():
self.addMeta(k, v)
@property
def dicomDir(self):
"""Returns the directory that the DICOM image data was loaded from. """
return self.__dicomDir
@deprecation.deprecated(deprecated_in='1.6.0',
removed_in='2.0.0',
details='Use metaKeys instead')
def keys(self): def keys(self):
"""Returns the keys contained in the DICOM metadata dictionary """Deprecated - use :meth:`.Image.metaKeys`. """
(``dict.keys``). return self.metaKeys()
"""
return self.__meta.keys()
@deprecation.deprecated(deprecated_in='1.6.0',
removed_in='2.0.0',
details='Use metaValues instead')
def values(self): def values(self):
"""Returns the values contained in the DICOM metadata dictionary """Deprecated - use :meth:`.Image.metaValues`. """
(``dict.values``). return self.metaValues()
"""
return self.__meta.values()
@deprecation.deprecated(deprecated_in='1.6.0',
removed_in='2.0.0',
details='Use metaItems instead')
def items(self): def items(self):
"""Returns the items contained in the DICOM metadata dictionary """Deprecated - use :meth:`.Image.metaItems`. """
(``dict.items``). return self.metaItems()
"""
return self.__meta.items()
@deprecation.deprecated(deprecated_in='1.6.0',
removed_in='2.0.0',
details='Use getMeta instead')
def get(self, *args, **kwargs): def get(self, *args, **kwargs):
"""Returns the metadata value with the specified key (``dict.get``). """Deprecated - use :meth:`.Image.getMeta`. """
""" return self.getMeta(*args, **kwargs)
return self.__meta.get(*args, **kwargs)
@memoize.memoize @memoize.memoize
...@@ -218,4 +238,4 @@ def loadSeries(series): ...@@ -218,4 +238,4 @@ def loadSeries(series):
# Force-load images into memory # Force-load images into memory
[i.get_data() for i in images] [i.get_data() for i in images]
return [DicomImage(i, series, name=desc) for i in images] return [DicomImage(i, series, dcmdir, name=desc) for i in images]
...@@ -36,6 +36,7 @@ import os ...@@ -36,6 +36,7 @@ import os
import os.path as op import os.path as op
import string import string
import logging import logging
import collections
import six import six
import deprecation import deprecation
...@@ -158,6 +159,23 @@ class Nifti(notifier.Notifier): ...@@ -158,6 +159,23 @@ class Nifti(notifier.Notifier):
:attr:`.constants.NIFTI_XFORM_ANALYZE`. :attr:`.constants.NIFTI_XFORM_ANALYZE`.
**Metadata**
The ``Image`` class has a handful of methods allowing you to add and access
additional metadata associated with the image. These methods are used by
the :class:`.DicomImage` and :class:`.MGHImage` to store additional
meta-data that cannot be stored in the NIFTI header:
.. autosummary::
:nosignatures:
metaKeys
metaValues
metaItems
getMeta
**Notification** **Notification**
...@@ -193,6 +211,7 @@ class Nifti(notifier.Notifier): ...@@ -193,6 +211,7 @@ class Nifti(notifier.Notifier):
worldToVoxMat = transform.invert(voxToWorldMat) worldToVoxMat = transform.invert(voxToWorldMat)
self.header = header self.header = header
self.__meta = collections.OrderedDict()
self.__shape = shape self.__shape = shape
self.__intent = header.get('intent_code', self.__intent = header.get('intent_code',
constants.NIFTI_INTENT_NONE) constants.NIFTI_INTENT_NONE)
...@@ -667,6 +686,33 @@ class Nifti(notifier.Notifier): ...@@ -667,6 +686,33 @@ class Nifti(notifier.Notifier):
return code return code
def metaKeys(self):
"""Returns the keys contained in the image metadata dictionary
(``dict.keys``).
"""
return self.__meta.keys()
def metaValues(self):
"""Returns the values contained in the image metadata dictionary
(``dict.values``).
"""
return self.__meta.values()
def metaItems(self):
"""Returns the items contained in the image metadata dictionary
(``dict.items``).
"""
return self.__meta.items()
def getMeta(self, *args, **kwargs):
"""Returns the metadata value with the specified key (``dict.get``).
"""
return self.__meta.get(*args, **kwargs)
class Image(Nifti): class Image(Nifti):
"""Class which represents a NIFTI image. Internally, the image is """Class which represents a NIFTI image. Internally, the image is
loaded/stored using a :mod:`nibabel.nifti1.Nifti1Image` or loaded/stored using a :mod:`nibabel.nifti1.Nifti1Image` or
...@@ -831,7 +877,7 @@ class Image(Nifti): ...@@ -831,7 +877,7 @@ class Image(Nifti):
# Otherwise we let nibabel # Otherwise we let nibabel
# manage the file reference(s) # manage the file reference(s)
else: else:
nibImage = nib.load(image, **kwargs) nibImage = nib.load(image, **kwargs)
dataSource = image dataSource = image
...@@ -1311,7 +1357,7 @@ Made available in this module for convenience. ...@@ -1311,7 +1357,7 @@ Made available in this module for convenience.
def looksLikeImage(filename, allowedExts=None): def looksLikeImage(filename, allowedExts=None):
"""Returns ``True`` if the given file looks like an image, ``False`` """Returns ``True`` if the given file looks like a NIFTI image, ``False``
otherwise. otherwise.
.. note:: The ``filename`` cannot just be a file prefix - it must .. note:: The ``filename`` cannot just be a file prefix - it must
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment