Ejemplo n.º 1
0
    def test_image_to_file(self):
        for img_path in self.img_paths:
            img = ants.image_read(img_path)
            new_path = img_path.replace('.nii.gz', '_write.nii.gz')
            img.to_file(new_path)
            new_img = ants.image_read(new_path)

            self.assertEqual(img.pixeltype, new_img.pixeltype)
            self.assertEqual(img.dimension, new_img.dimension)
            self.assertEqual(img.components, new_img.components)

            self.assertEqual(img.spacing, new_img.spacing)
            self.assertEqual(img.origin, new_img.origin)
            nptest.assertEqual(img.direction, new_img.direction)

            nptest.assert_allclose(img.numpy(), new_img.numpy())
Ejemplo n.º 2
0
    def setUp(self):
        self.datadir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'data')

        img_paths = []
        for dtype in ['CHAR', 'DOUBLE', 'FLOAT', 'INT', 'SHORT', 
                      'UNSIGNEDCHAR', 'UNSIGNEDINT', 'UNSIGNEDSHORT']:
            for dim in [2,3]:
                img_paths.append('image_%s_%iD.nii.gz' % (dtype, dim))

        self.img_paths = [os.path.join(self.datadir, img_path) for img_path in img_paths]
        self.imgs = [ants.image_read(ip) for ip in self.img_paths]
Ejemplo n.º 3
0
 def test_image_read_then_to_numpy_WrapITK_equality(self):
     """
     Test that reading an image then converting it to numpy
     is the same in ANTsPy and the python version of ITK
     """
     for img_path in self.img_paths:
         # my itk doesnt wrap integer or unsigned integer...
         if 'INT' not in img_path:
             ants_array = ants.image_read(img_path).numpy()
             itk_array = itk.GetArrayViewFromImage(itk.imread(img_path))
             self.assertEqual(ants_array.sum(), itk_array.sum())
             self.assertEqual(ants_array.dtype, itk_array.dtype)
Ejemplo n.º 4
0
    def test_image_read(self):
        """
        reading images from file
        """
        for img_path in self.img_paths:
            img = ants.image_read(img_path)

            ptype = _parse_pixeltype_from_string(img_path)
            dim = _parse_dim_from_string(img_path)
            self.assertEqual(img.pixeltype, ptype)
            self.assertEqual(img.dimension, dim)
            self.assertEqual(img.components, 1)
Ejemplo n.º 5
0
    def test_image_read_then_to_numpy(self):
        """
        convert ANTsImage's to numpy arrays
        """
        for img_path in self.img_paths:
            img = ants.image_read(img_path)
            img_array = img.numpy()

            npy_dtype = _parse_numpy_pixeltype_from_string(img_path)
            dim = _parse_dim_from_string(img_path)

            self.assertGreater(img_array.sum(), 0)
            self.assertGreater(len(img_array), 0)
            self.assertEqual(img_array.dtype, npy_dtype)
            self.assertEqual(img_array.ndim, dim)
            self.assertEqual(tuple(img_array.shape), tuple(img.shape))
Ejemplo n.º 6
0
    def test_get_and_set_direction(self):
        for img_path in self.img_paths:
            ndim = _parse_dim_from_string(img_path)
            img = ants.image_read(img_path)

            direction = img.direction

            self.assertTrue(isinstance(direction, np.ndarray))
            self.assertEqual(direction.shape, (ndim, ndim))

            # try to set neworigin
            set_direction = np.eye(ndim) * 3.6
            img.set_direction(set_direction)
            get_direction = img.direction

            nptest.assert_allclose(set_direction, get_direction)
Ejemplo n.º 7
0
    def test_get_and_set_spacing(self):
        """
        test get and set of spacing
        """
        for img_path in self.img_paths:
            ndim = _parse_dim_from_string(img_path)
            img = ants.image_read(img_path)

            spacing = img.spacing
            # assert that there is one spacing value for each dimension
            self.assertEqual(len(spacing), ndim)

            # try to set neworigin
            img.set_spacing(tuple([3.6] * ndim))
            new_spacing = img.spacing

            self.assertEqual(new_spacing, tuple([3.6] * ndim))
Ejemplo n.º 8
0
    def test_get_and_set_origin(self):
        """
        test get and set of origin
        """
        for img_path in self.img_paths:
            ndim = _parse_dim_from_string(img_path)
            img = ants.image_read(img_path)

            origin = img.origin
            # assert that there is one origin value for each dimension
            self.assertEqual(len(origin), ndim)

            # try to set neworigin
            img.set_origin(tuple([3.6] * ndim))
            new_origin = img.origin

            self.assertEqual(new_origin, tuple([3.6] * ndim))