def test_num_digits(self): from jicbioimage.core.image import Image3D self.assertEqual(Image3D._num_digits(1), 1) self.assertEqual(Image3D._num_digits(9), 1) self.assertEqual(Image3D._num_digits(10), 2) self.assertEqual(Image3D._num_digits(99), 2) self.assertEqual(Image3D._num_digits(100), 3)
def test_scaling_of_written_files(self): from jicbioimage.core.image import Image3D, Image directory = os.path.join(TMP_DIR, "im3d") z0 = np.zeros((50, 50), dtype=np.uint8) z1 = np.ones((50, 50), dtype=np.uint8) stack = np.dstack([z0, z1]) im3d = Image3D.from_array(stack) im3d.to_directory(directory) im0 = Image.from_file(os.path.join(directory, "z0.png")) im1 = Image.from_file(os.path.join(directory, "z1.png")) self.assertTrue(np.array_equal(z0, im0)) self.assertTrue(np.array_equal(z1 * 255, im1)) z2 = np.ones((50, 50), dtype=np.uint8) * 255 stack = np.dstack([z0, z1, z2]) im3d = Image3D.from_array(stack) im3d.to_directory(directory) im0 = Image.from_file(os.path.join(directory, "z0.png")) im1 = Image.from_file(os.path.join(directory, "z1.png")) im2 = Image.from_file(os.path.join(directory, "z2.png")) self.assertTrue(np.array_equal(z0, im0)) self.assertTrue(np.array_equal(z1, im1)) self.assertTrue(np.array_equal(z2 * 255, im1))
def test_scaling_of_written_files(self): from jicbioimage.core.image import Image3D, Image directory = os.path.join(TMP_DIR, "im3d") z0 = np.zeros((50,50), dtype=np.uint8) z1 = np.ones((50, 50), dtype=np.uint8) stack = np.dstack([z0, z1]) im3d = Image3D.from_array(stack) im3d.to_directory(directory) im0 = Image.from_file(os.path.join(directory, "z0.png")) im1 = Image.from_file(os.path.join(directory, "z1.png")) self.assertTrue(np.array_equal(z0, im0)) self.assertTrue(np.array_equal(z1*255, im1)) z2 = np.ones((50, 50), dtype=np.uint8) * 255 stack = np.dstack([z0, z1, z2]) im3d = Image3D.from_array(stack) im3d.to_directory(directory) im0 = Image.from_file(os.path.join(directory, "z0.png")) im1 = Image.from_file(os.path.join(directory, "z1.png")) im2 = Image.from_file(os.path.join(directory, "z2.png")) self.assertTrue(np.array_equal(z0, im0)) self.assertTrue(np.array_equal(z1, im1)) self.assertTrue(np.array_equal(z2*255, im1))
def test_from_array_with_name(self): from jicbioimage.core.image import Image3D ar = np.zeros((50, 50, 50), dtype=np.uint8) im = Image3D.from_array(ar, name='Test1') self.assertEqual(len(im.history), 0) self.assertEqual(im.history.creation, 'Created Image3D from array as Test1')
def test_from_array(self): from jicbioimage.core.image import Image3D ar = np.zeros((50,50,50), dtype=np.uint8) im = Image3D.from_array(ar) self.assertTrue(isinstance(im, Image3D)) self.assertEqual(len(im.history), 0) self.assertEqual(im.history.creation, 'Created Image3D from array')
def discrete_gaussian_filter(im3d, variance): itk_im = sitk.GetImageFromArray(im3d) gaussian_filter = sitk.DiscreteGaussianImageFilter() gaussian_filter.SetVariance(variance) itk_im = gaussian_filter.Execute(itk_im) return Image3D.from_array(sitk.GetArrayFromImage(itk_im), log_in_history=False)
def test_from_array(self): from jicbioimage.core.image import Image3D ar = np.zeros((50, 50, 50), dtype=np.uint8) im = Image3D.from_array(ar) self.assertTrue(isinstance(im, Image3D)) self.assertEqual(len(im.history), 0) self.assertEqual(im.history.creation, 'Created Image3D from array')
def test_instantiation_from_shape_with_name(self): from jicbioimage.core.image import Image3D image = Image3D((50, 50, 50), name='test') self.assertEqual(image.name, 'test') self.assertEqual(len(image.history), 0) self.assertEqual( image.history.creation, 'Instantiated Image3D from shape (50, 50, 50) as test')
def test_instantiation_from_shape(self): from jicbioimage.core.image import Image3D image = Image3D((50, 50, 50)) self.assertTrue(isinstance(image, np.ndarray)) self.assertEqual(image.shape, (50, 50, 50)) self.assertEqual(len(image.history), 0) self.assertEqual(image.history.creation, 'Instantiated Image3D from shape (50, 50, 50)')
def to_directory(self, directory): if not os.path.isdir(directory): os.mkdir(directory) xdim, ydim, zdim = self.shape num_digits = Image3D._num_digits(zdim-1) for z in range(zdim): num = str(z).zfill(num_digits) fname = "z{}.png".format(num) fpath = os.path.join(directory, fname) with open(fpath, "wb") as fh: im = Image.from_array(unique_color_array(self[:, :, z])) fh.write(im.png())
def test_to_directory(self): from jicbioimage.core.image import Image3D directory = os.path.join(TMP_DIR, "im3d") im3d = Image3D((50, 50, 10)) im3d.to_directory(directory) expected = [ "z0.png", "z1.png", "z2.png", "z3.png", "z4.png", "z5.png", "z6.png", "z7.png", "z8.png", "z9.png" ] actual = os.listdir(directory) for f in expected: self.assertTrue(f in actual)
def test_naming_with_two_digit_number(self): from jicbioimage.core.image import Image3D directory = os.path.join(TMP_DIR, "im3d") im3d = Image3D((50, 50, 11)) im3d.to_directory(directory) expected = [ "z00.png", "z01.png", "z02.png", "z03.png", "z04.png", "z05.png", "z06.png", "z07.png", "z08.png", "z09.png", "z10.png" ] actual = os.listdir(directory) for f in expected: self.assertTrue(f in actual)
def test_from_directory(self): from jicbioimage.core.image import Image3D, Image directory = os.path.join(TMP_DIR) with open(os.path.join(directory, 'junk.txt'), "w") as fh: fh.write("junk") z0 = np.zeros((50, 50), dtype=np.uint8) z1 = np.ones((50, 50), dtype=np.uint8) * 255 for i, z in enumerate([z0, z1]): im = Image.from_array(z) fpath = os.path.join(directory, "z{}.png".format(i)) with open(fpath, "wb") as fh: fh.write(im.png()) im3d = Image3D.from_directory(TMP_DIR) self.assertTrue(isinstance(im3d, Image3D)) self.assertEqual(im3d.shape, (50, 50, 2)) stack = np.dstack([z0, z1]) self.assertTrue(np.array_equal(im3d, stack))
def test_from_directory(self): from jicbioimage.core.image import Image3D, Image directory = os.path.join(TMP_DIR) with open(os.path.join(directory, 'junk.txt'), "w") as fh: fh.write("junk") z0 = np.zeros((50,50), dtype=np.uint8) z1 = np.ones((50, 50), dtype=np.uint8) * 255 for i, z in enumerate([z0, z1]): im = Image.from_array(z) fpath = os.path.join(directory, "z{}.png".format(i)) with open(fpath, "wb") as fh: fh.write(im.png()) im3d = Image3D.from_directory(TMP_DIR) self.assertTrue(isinstance(im3d, Image3D)) self.assertEqual(im3d.shape, (50, 50, 2)) stack = np.dstack([z0, z1]) self.assertTrue(np.array_equal(im3d, stack))
def test_from_array_no_history(self): from jicbioimage.core.image import Image3D ar = np.zeros((50, 50, 50), dtype=np.uint8) im = Image3D.from_array(ar, log_in_history=False) self.assertEqual(len(im.history), 0)
def filter_median(im3d): itk_im = sitk.GetImageFromArray(im3d) median_filter = sitk.MedianImageFilter() itk_im = median_filter.Execute(itk_im) return Image3D.from_array(sitk.GetArrayFromImage(itk_im), log_in_history=False)
def get_stack(self): return Image3D.from_directory(self.directories[self.current])
def add_height(image): stack = np.dstack([image, image, image]) return Image3D.from_array(stack)
def gradient_magnitude(im3d): itk_im = sitk.GetImageFromArray(im3d) itk_im = sitk.GradientMagnitude(itk_im) return Image3D.from_array(sitk.GetArrayFromImage(itk_im), log_in_history=False)
def test_from_array_no_history(self): from jicbioimage.core.image import Image3D ar = np.zeros((50,50,50), dtype=np.uint8) im = Image3D.from_array(ar, log_in_history=False) self.assertEqual(len(im.history), 0)
def test_from_array_with_name(self): from jicbioimage.core.image import Image3D ar = np.zeros((50,50,50), dtype=np.uint8) im = Image3D.from_array(ar, name='Test1') self.assertEqual(len(im.history), 0) self.assertEqual(im.history.creation, 'Created Image3D from array as Test1')
def test_instantiation_from_shape_no_history(self): from jicbioimage.core.image import Image3D image = Image3D((50, 50, 50), log_in_history=False) self.assertEqual(len(image.history), 0)
def test_default_type(self): from jicbioimage.core.image import Image3D image = Image3D((50, 50, 50)) self.assertEqual(image.dtype, np.uint8, 'Image type not np.uint8 but {}'.format(image.dtype))
def test_default_name(self): from jicbioimage.core.image import Image3D image = Image3D((50, 50, 50)) self.assertTrue(image.name is None)