Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

"slr" similarity in HistogramRegistration appears to be broken #273

Closed
matthew-brett opened this Issue May 17, 2013 · 2 comments

Comments

Projects
None yet
1 participant
Owner

matthew-brett commented May 17, 2013

In [29]: import nipy

In [30]: from nipy.testing import funcfile, anatfile

In [32]: anat_img = nipy.load_image(anatfile)

In [33]: func_img = nipy.load_image(funcfile)

In [34]: func_vol0 = func_img[..., 0]

In [35]: register_obj = nar.HistogramRegistration(anat_img, func_vol0, similarity='slr')

In [36]: register_obj.optimize()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-36-2e999d7264a3> in <module>()
----> 1 register_obj.optimize()

TypeError: optimize() takes at least 2 arguments (1 given)

In [37]: register_obj.optimize('rigid')
Initial guess...
translation : [ 0.  0.  0.]
rotation    : [ 0.  0.  0.]

Optimizing using fmin_powell
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-37-9d9d4b937805> in <module>()
----> 1 register_obj.optimize('rigid')

/home/mb312/usr/local/lib/python2.7/site-packages/nipy/algorithms/registration/histogram_registration.pyc in optimize(self, T, optimizer, **kwargs)
    282             print ('Optimizing using %s' % fmin.__name__)
    283         kwargs['callback'] = callback
--> 284         Tv.param = fmin(cost, tc0, *args, **kwargs)
    285         return Tv.optimizable
    286 

/usr/lib/python2.7/dist-packages/scipy/optimize/optimize.pyc in fmin_powell(func, x0, args, xtol, ftol, maxiter, maxfun, full_output, disp, retall, callback, direc)
1608         direc = asarray(direc, dtype=float)
1609 
-> 1610     fval = squeeze(func(x))
1611     x1 = x.copy()
1612     iter = 0;

/usr/lib/python2.7/dist-packages/scipy/optimize/optimize.pyc in function_wrapper(x)
    174     def function_wrapper(x):
    175         ncalls[0] += 1
--> 176         return function(x, *args)
    177     return ncalls, function_wrapper
    178 

/home/mb312/usr/local/lib/python2.7/site-packages/nipy/algorithms/registration/histogram_registration.pyc in cost(tc)
    251             # This is where the similarity function is calculcated
    252             Tv.param = tc
--> 253             return -self._eval(Tv)
    254 
    255         # Callback during optimization

/home/mb312/usr/local/lib/python2.7/site-packages/nipy/algorithms/registration/histogram_registration.pyc in _eval(self, Tv)
    215                          trans_vox_coords,
    216                          interp)
--> 217         return self._similarity_call(self._joint_hist)
    218 
    219     def optimize(self, T, optimizer=OPTIMIZER, **kwargs):

/home/mb312/usr/local/lib/python2.7/site-packages/nipy/algorithms/registration/similarity_measures.pyc in __call__(self, H)
    48 
    49     def __call__(self, H):
---> 50         return -self.averaged_loss(H)
    51 
    52 

/home/mb312/usr/local/lib/python2.7/site-packages/nipy/algorithms/registration/similarity_measures.pyc in averaged_loss(self, H)
    45 
    46     def averaged_loss(self, H):
---> 47         return np.sum(H * self.loss(H)) / nonzero(self.npoints(H))
    48 
    49     def __call__(self, H):

/home/mb312/usr/local/lib/python2.7/site-packages/nipy/algorithms/registration/similarity_measures.pyc in loss(self, H)
    55     def loss(self, H):
    56         if not hasattr(self, 'L'):
---> 57             self.L = dist2loss(self.dist)
    58         return self.L
    59 

AttributeError: 'SupervisedLikelihoodRatio' object has no attribute 'dist'
Owner

matthew-brett commented Oct 3, 2015

Alexis - this one is still broken, but with a different error. Minimal test case:

import nipy

from nipy.testing import funcfile, anatfile

import nipy.algorithms.registration as nar

anat_img = nipy.load_image(anatfile)
func_img = nipy.load_image(funcfile)
func_vol0 = func_img[..., 0]
register_obj = nar.HistogramRegistration(anat_img, func_vol0, similarity='slr')
register_obj.optimize('rigid')

This gives:

Initial guess...
translation : [ 0.  0.  0.]
rotation    : [ 0.  0.  0.]

Optimizing using fmin_powell
Traceback (most recent call last):
  File "check_slr.py", line 11, in <module>
    register_obj.optimize('rigid')
  File "/Users/mb312/dev_trees/nipy/nipy/algorithms/registration/histogram_registration.py", line 372, in optimize
    Tv.param = fmin(cost, tc0, *args, **kwargs)
  File "/Users/mb312/.virtualenvs/test3/lib/python3.4/site-packages/scipy/optimize/optimize.py", line 2272, in fmin_powell
    res = _minimize_powell(func, x0, args, callback=callback, **opts)
  File "/Users/mb312/.virtualenvs/test3/lib/python3.4/site-packages/scipy/optimize/optimize.py", line 2331, in _minimize_powell
    fval = squeeze(func(x))
  File "/Users/mb312/.virtualenvs/test3/lib/python3.4/site-packages/scipy/optimize/optimize.py", line 285, in function_wrapper
    return function(*(wrapper_args + args))
  File "/Users/mb312/dev_trees/nipy/nipy/algorithms/registration/histogram_registration.py", line 341, in cost
    return -self._eval(Tv)
  File "/Users/mb312/dev_trees/nipy/nipy/algorithms/registration/histogram_registration.py", line 300, in _eval
    return self._similarity_call(self._joint_hist)
  File "/Users/mb312/dev_trees/nipy/nipy/algorithms/registration/similarity_measures.py", line 80, in __call__
    total_loss = np.sum(H * self.loss(H))
  File "/Users/mb312/dev_trees/nipy/nipy/algorithms/registration/similarity_measures.py", line 92, in loss
    self.L = dist2loss(self.dist)
  File "/Users/mb312/dev_trees/nipy/nipy/algorithms/registration/similarity_measures.py", line 53, in dist2loss
    qT = q.T
AttributeError: 'NoneType' object has no attribute 'T'

The problem is that the similarity property of HistogramRegistration ends up creating a SupervisedLikelihoodRatio with a default dist=None, so when the loss method gets called, it passes this dist==None to the dist2loss function, where that function appears to be expecting a numpy array.

Owner

matthew-brett commented Oct 6, 2015

@alexis-roche - would you mind having a look at this one? I don't know how to fix.

@matthew-brett matthew-brett closed this in #367 Oct 7, 2015

matthew-brett added a commit that referenced this issue Oct 7, 2015

Merge pull request #367 from alexis-roche/fix-issue-273
MRG: add error checking for slr similarity measure

Checks that input parameter `dist` is not None when using the similarity measure.

This should fix #273
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment