Skip to content

Commit

Permalink
Merge branch 'fix/masked_registration' of https://github.com/chrisfil…
Browse files Browse the repository at this point in the history
  • Loading branch information
alexis-roche committed Apr 27, 2012
2 parents 08651e1 + 454e3e4 commit 2d62b47
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
3 changes: 2 additions & 1 deletion nipy/algorithms/registration/histogram_registration.py
Expand Up @@ -368,7 +368,8 @@ def clamp(x, bins=BINS, mask=None):
y, bins = _clamp(x, y, bins)
else:
ym = y[mask]
ym, bins = _clamp(x, ym, bins)
xm = x[mask]
ym, bins = _clamp(xm, ym, bins)
y[mask] = ym
return y, bins

Expand Down
29 changes: 27 additions & 2 deletions nipy/algorithms/registration/tests/test_histogram_registration.py
Expand Up @@ -12,6 +12,9 @@

dummy_affine = np.eye(4)

def make_data_bool(dx=100, dy=100, dz=50):
return (np.random.rand(dx, dy, dz)
- np.random.rand()) > 0

def make_data_uint8(dx=100, dy=100, dz=50):
return (256 * (np.random.rand(dx, dy, dz)
Expand All @@ -28,8 +31,8 @@ def make_data_float64(dx=100, dy=100, dz=50):
- np.random.rand())).astype('float64')


def _test_clamping(I, thI=0.0, clI=256):
R = HistogramRegistration(I, I, from_bins=clI)
def _test_clamping(I, thI=0.0, clI=256, mask=None):
R = HistogramRegistration(I, I, from_bins=clI, from_mask=mask, to_mask=mask)
R.subsample(spacing=[1, 1, 1])
Ic = R._from_data
Ic2 = R._to_data[1:-1, 1:-1, 1:-1]
Expand All @@ -55,6 +58,12 @@ def test_clamping_int16():
_test_clamping(I)


def test_masked_clamping_int16():
I = AffineImage(make_data_int16(), dummy_affine, 'ijk')
mask = AffineImage(make_data_bool(), dummy_affine, 'ijk')
_test_clamping(I, mask=mask)


def test_clamping_int16_nonstd():
I = AffineImage(make_data_int16(), dummy_affine, 'ijk')
_test_clamping(I, 10, 165)
Expand Down Expand Up @@ -143,6 +152,22 @@ def test_histogram_registration():
J = AffineImage(I.get_data().copy(), dummy_affine, 'ijk')
R = HistogramRegistration(I, J)
assert_raises(ValueError, R.subsample, spacing=[0, 1, 3])

def test_histogram_masked_registration():
""" Test the histogram registration class.
"""
I = AffineImage(make_data_int16(dx=100, dy=100, dz=50), dummy_affine, 'ijk')
J = AffineImage(make_data_int16(dx=100, dy=100, dz=50), dummy_affine, 'ijk')
mask = (np.zeros((100,100,50)) == 1)
mask[10:20,10:20,10:20] = True
mask_img = AffineImage(mask, dummy_affine, 'ijk')
R = HistogramRegistration(I, J, to_mask=mask_img, from_mask=mask_img)
sim1 = R.eval(Affine())
I = AffineImage(I.get_data()[mask].reshape(10,10,10), dummy_affine, 'ijk')
J = AffineImage(J.get_data()[mask].reshape(10,10,10), dummy_affine, 'ijk')
R = HistogramRegistration(I, J)
sim2 = R.eval(Affine())
assert_equal(sim1, sim2)


if __name__ == "__main__":
Expand Down

0 comments on commit 2d62b47

Please sign in to comment.