Skip to content

Commit 7f06c11

Browse files
committed
Merge branch 'enh/Similarity4DMetrics' into enh/EvaluationAlgorithms
Conflicts: CHANGES
2 parents 13aee60 + f94e4dc commit 7f06c11

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

CHANGES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ Next Release
22
============
33

44
* API: Interfaces to external packages are no longer available in the top-level ``nipype`` namespace, and must be imported directly (e.g. ``from nipype.interfaces import fsl``).
5+
* ENH: nipy.utils.Similarity has been deprecated by algorithms.metrics.Similarity
6+
which is an extension of the former to allow 4D images.
57
* ENH: New ANTs interface: ApplyTransformsToPoints
68
* ENH: New metrics group in algorithms. Now Distance, Overlap, and FuzzyOverlap
79
are found in nipype.algorithms.metrics instead of misc

nipype/algorithms/metrics.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,3 +495,93 @@ def _list_outputs(self):
495495
outputs['out_map'] = self._out_file
496496

497497
return outputs
498+
499+
500+
class SimilarityInputSpec(BaseInterfaceInputSpec):
501+
502+
volume1 = File(exists=True, desc="3D/4D volume", mandatory=True)
503+
volume2 = File(exists=True, desc="3D/4D volume", mandatory=True)
504+
mask1 = File(exists=True, desc="3D volume")
505+
mask2 = File(exists=True, desc="3D volume")
506+
metric = traits.Either(traits.Enum('cc', 'cr', 'crl1', 'mi', 'nmi', 'slr'),
507+
traits.Callable(),
508+
desc="""str or callable
509+
Cost-function for assessing image similarity. If a string,
510+
one of 'cc': correlation coefficient, 'cr': correlation
511+
ratio, 'crl1': L1-norm based correlation ratio, 'mi': mutual
512+
information, 'nmi': normalized mutual information, 'slr':
513+
supervised log-likelihood ratio. If a callable, it should
514+
take a two-dimensional array representing the image joint
515+
histogram as an input and return a float.""", usedefault=True)
516+
517+
518+
class SimilarityOutputSpec(TraitedSpec):
519+
similarity = traits.List( traits.Float(desc="Similarity between volume 1 and 2, frame by frame"))
520+
521+
522+
class Similarity(BaseInterface):
523+
"""Calculates similarity between two 3D or 4D volumes. Both volumes have to be in
524+
the same coordinate system, same space within that coordinate system and
525+
with the same voxel dimensions.
526+
527+
.. note:: This interface is an extension of nipype.interfaces.nipy.utils.Similarity
528+
to support 4D files.
529+
530+
Example
531+
-------
532+
>>> from nipype.interfaces.nipy.utils import Similarity
533+
>>> similarity = Similarity()
534+
>>> similarity.inputs.volume1 = 'rc1s1.nii'
535+
>>> similarity.inputs.volume2 = 'rc1s2.nii'
536+
>>> similarity.inputs.mask1 = 'mask.nii'
537+
>>> similarity.inputs.mask2 = 'mask.nii'
538+
>>> similarity.inputs.metric = 'cr'
539+
>>> res = similarity.run() # doctest: +SKIP
540+
"""
541+
542+
input_spec = SimilarityInputSpec
543+
output_spec = SimilarityOutputSpec
544+
545+
def _run_interface(self, runtime):
546+
547+
vol1_nii = nb.load(self.inputs.volume1)
548+
vol2_nii = nb.load(self.inputs.volume2)
549+
550+
dims = vol1_nii.get_data().ndim
551+
552+
if dims==3 or dims==2:
553+
vols1 = [ vol1_nii ]
554+
vols2 = [ vol2_nii ]
555+
if dims==4:
556+
vols1 = nb.four_to_three( vol1_nii )
557+
vols2 = nb.four_to_three( vol2_nii )
558+
559+
if dims<2 or dims>4:
560+
raise RuntimeError( 'Image dimensions not supported (detected %dD file)' % dims )
561+
562+
if isdefined(self.inputs.mask1):
563+
mask1 = nb.load(self.inputs.mask1).get_data() == 1
564+
else:
565+
mask1 = None
566+
567+
if isdefined(self.inputs.mask2):
568+
mask2 = nb.load(self.inputs.mask2).get_data() == 1
569+
else:
570+
mask2 = None
571+
572+
self._similarity = []
573+
574+
for ts1,ts2 in zip( vols1, vols2 ):
575+
histreg = HistogramRegistration(from_img = ts1,
576+
to_img = ts2,
577+
similarity=self.inputs.metric,
578+
from_mask = mask1,
579+
to_mask = mask2)
580+
self._similarity.append( histreg.eval(Affine()) )
581+
582+
return runtime
583+
584+
def _list_outputs(self):
585+
outputs = self._outputs().get()
586+
outputs['similarity'] = self._similarity
587+
return outputs

nipype/interfaces/nipy/utils.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ class Similarity(BaseInterface):
6868
input_spec = SimilarityInputSpec
6969
output_spec = SimilarityOutputSpec
7070

71+
def __init__(self, **inputs):
72+
warnings.warn(("This interface is deprecated since 0.10.0."
73+
" Please use nipype.algorithms.metrics.Similarity"),
74+
DeprecationWarning)
75+
super(BaseInterface,self).__init__(**inputs)
76+
7177
def _run_interface(self, runtime):
7278

7379
vol1_nii = nb.load(self.inputs.volume1)

0 commit comments

Comments
 (0)