Exemplo n.º 1
0
 def setUp(self):
     self.shape = (8, 9, 10)
     self.target = np.random.rand(*self.shape)
     self.corr = BaseCorrelator(self.target)
Exemplo n.º 2
0
 def setUp(self):
     self.shape = (8, 9, 10)
     self.target = np.random.rand(*self.shape)
     self.corr = BaseCorrelator(self.target)
Exemplo n.º 3
0
class TestBaseCorrelator(unittest.TestCase):
    def setUp(self):
        self.shape = (8, 9, 10)
        self.target = np.random.rand(*self.shape)
        self.corr = BaseCorrelator(self.target)

    def test___init__(self):
        self.assertEqual(self.corr._target.max(), 1)

    def test_rotations_setter(self):
        self.corr.rotations = [0] * 9
        self.assertIsInstance(self.corr._rotations, np.ndarray)
        with self.assertRaises(ValueError):
            self.corr.rotations = [0] * 3
        self.corr.rotations = [0] * 27
        self.assertEqual(self.corr._rotations.shape, (3, 3, 3))

    def test_template_setter(self):
        template = np.random.rand(*(3, 3, 3))
        with self.assertRaises(ValueError):
            self.corr.template = template

        # test resetting of mask
        self.corr._mask = 1
        self.corr.template = np.random.rand(*self.shape)
        self.assertTrue(self.corr._mask is None)

    def test_laplace_filter(self):
        template = np.random.rand(*self.shape)
        self.corr._template = self.corr._laplace_filter(template)
        self.assertTrue(
            np.allclose(self.corr._template, laplace(template,
                                                     mode='constant')))

    def test_get_center(self):
        center = self.corr._get_center(self.shape)
        self.assertTrue(np.allclose(center, (5, 4.5, 4)))

    def test_normalize_template(self):
        self.corr.template = np.random.rand(*self.shape)
        self.corr._mask = np.ones(self.shape)
        self.corr._normalize_template(self.corr._mask != 0)
        self.assertAlmostEqual(self.corr._template.mean(), 0)
        self.assertAlmostEqual(self.corr._template.std(), 1)

    #TODO
    @unittest.skip("Not yet implemented")
    def test_get_rmax(self):
        self.corr.template = np.random.rand(*self.shape)
        mask = np.zeros(self.shape, dtype=np.float64)
        mask[3:6] = 1
        self.corr._mask = mask
        self.corr._get_center(self.shape)
        self.corr._get_rmax()

    def test_mask_setter(self):
        # Template is not set, so ValueError should be raised no matter what
        with self.assertRaises(ValueError):
            self.corr.mask = None

        template = np.random.rand(*self.shape)
        self.corr._template = template
        # Shape of mask is not correct
        with self.assertRaises(ValueError):
            self.corr.mask = np.random.rand(10)

        # If mask only is zero, raise ValueError
        with self.assertRaises(ValueError):
            self.corr.mask = np.zeros(self.shape)

        self.corr._laplace = True
        self.corr._template = template.copy()
        mask = np.ones(self.shape)
        self.corr.mask = mask
        self.assertTrue(np.allclose(self.corr._mask, mask))

    def test_scan(self):
        # No requirement has been set
        with self.assertRaises(ValueError):
            self.corr.scan()
Exemplo n.º 4
0
class TestBaseCorrelator(unittest.TestCase):

    def setUp(self):
        self.shape = (8, 9, 10)
        self.target = np.random.rand(*self.shape)
        self.corr = BaseCorrelator(self.target)

    def test___init__(self):
        self.assertEqual(self.corr._target.max(), 1)

    def test_rotations_setter(self):
        self.corr.rotations = [0] * 9
        self.assertIsInstance(self.corr._rotations, np.ndarray)
        with self.assertRaises(ValueError):
            self.corr.rotations = [0] * 3
        self.corr.rotations = [0] * 27
        self.assertEqual(self.corr._rotations.shape, (3, 3, 3))

    def test_template_setter(self):
        template = np.random.rand(*(3, 3, 3))
        with self.assertRaises(ValueError):
            self.corr.template = template

        # test resetting of mask
        self.corr._mask = 1
        self.corr.template = np.random.rand(*self.shape)
        self.assertTrue(self.corr._mask is None)

    def test_laplace_filter(self):
        template = np.random.rand(*self.shape)
        self.corr._template = self.corr._laplace_filter(template)
        self.assertTrue(np.allclose(self.corr._template, laplace(template, mode='constant')))

    def test_get_center(self):
        center = self.corr._get_center(self.shape)
        self.assertTrue(np.allclose(center, (5, 4.5, 4)))

    def test_normalize_template(self):
        self.corr.template = np.random.rand(*self.shape)
        self.corr._mask = np.ones(self.shape)
        self.corr._normalize_template(self.corr._mask != 0)
        self.assertAlmostEqual(self.corr._template.mean(), 0)
        self.assertAlmostEqual(self.corr._template.std(), 1)

    #TODO
    @unittest.skip("Not yet implemented")
    def test_get_rmax(self):
        self.corr.template = np.random.rand(*self.shape)
        mask = np.zeros(self.shape, dtype=np.float64)
        mask[3: 6] = 1
        self.corr._mask = mask
        self.corr._get_center(self.shape)
        self.corr._get_rmax()

    def test_mask_setter(self):
        # Template is not set, so ValueError should be raised no matter what
        with self.assertRaises(ValueError):
            self.corr.mask = None

        template = np.random.rand(*self.shape)
        self.corr._template = template
        # Shape of mask is not correct
        with self.assertRaises(ValueError):
            self.corr.mask = np.random.rand(10)

        # If mask only is zero, raise ValueError
        with self.assertRaises(ValueError):
            self.corr.mask = np.zeros(self.shape)

        self.corr._laplace = True
        self.corr._template = template.copy()
        mask = np.ones(self.shape)
        self.corr.mask = mask
        self.assertTrue(np.allclose(self.corr._mask, mask))

    def test_scan(self):
        # No requirement has been set
        with self.assertRaises(ValueError):
            self.corr.scan()