class ImageTestCase(TestCase): def setUp(self): # numpy array for top-level functions that directly expect it self.im_np = misc.face( gray=True).astype('float64')[:768, :768][:, :, np.newaxis] # Independent Image object for testing Image methods self.im = Image(misc.face(gray=True).astype('float64')[:768, :768]) def tearDown(self): pass def testImShift(self): # Ensure that the two separate im_translate functions we have return the same thing # A single shift applied to all images shifts = np.array([100, 200]) im = self.im.shift(shifts) im1 = _im_translate(self.im_np, shifts.reshape(1, 2)) # Note the difference in the concept of shifts for _im_translate2 - negative sign/transpose im2 = _im_translate2(self.im_np, -shifts.reshape(2, 1)) # Pure numpy 'shifting' # 'Shifting' an Image corresponds to a 'roll' of a numpy array - again, note the negated signs and the axes im3 = np.roll(self.im.asnumpy()[:, :, 0], -shifts, axis=(0, 1)) self.assertTrue(np.allclose(im.asnumpy(), im1)) self.assertTrue(np.allclose(im1, im2)) self.assertTrue(np.allclose(im1[:, :, 0], im3)) def testArrayImageSource(self): # An Image can be wrapped in an ArrayImageSource when we need to deal with ImageSource objects. src = ArrayImageSource(self.im) im = src.images(start=0, num=np.inf) self.assertTrue(np.allclose(im.asnumpy(), self.im_np))
rots = qrand_rots(num_imgs, seed=0) imgs_clean = vol2img(sim.vols[..., 0], rots) # Assign the CTF information and index for each image h_idx = np.array([filters.index(f) for f in sim.filters]) # Evaluate CTF in the 8X8 FB basis h_ctf_fb = [filt.fb_mat(ffbbasis) for filt in filters] # Apply the CTF to the clean images. logger.info('Apply CTF filters to clean images.') imgs_ctf_clean = Image(sim.eval_filters(imgs_clean)) sim.cache(imgs_ctf_clean) # imgs_ctf_clean is an Image object. Convert to numpy array for subsequent statements imgs_ctf_clean = imgs_ctf_clean.asnumpy() # Apply the noise at the desired singal-noise ratio to the filtered clean images logger.info('Apply noise filters to clean images.') power_clean = anorm(imgs_ctf_clean)**2 / np.size(imgs_ctf_clean) noise_var = power_clean / sn_ratio imgs_noise = imgs_ctf_clean + np.sqrt(noise_var) * randn( img_size, img_size, num_imgs, seed=0) # Expand the images, both clean and noisy, in the Fourier-Bessel basis. This # can be done exactly (that is, up to numerical precision) using the # `basis.expand` function, but for our purposes, an approximation will do. # Since the basis is close to orthonormal, we may approximate the exact # expansion by applying the adjoint of the evaluation mapping using # `basis.evaluate_t`. logger.info('Get coefficients of clean and noisy images in FFB basis.')
# ---------------------- # Now that we have an example array, we will begin using the ASPIRE toolkit. # First we will make an ASPIRE Image instance out of our data. # This is a light wrapper over the numpy array. Many ASPIRE internals # are built around an ``Image`` class. # Construct the Image class by passing it an array of data. img = Image(stock_img) # Downsample (just to speeds things up) new_resolution = img.res // 4 img = img.downsample(new_resolution) # We will begin processing by adding some noise. # We would like to create uniform noise for a 2d image with prescibed variance, noise_var = np.var(img.asnumpy()) * 5 noise_filter = ScalarFilter(dim=2, value=noise_var) # Then create a NoiseAdder. noise = NoiseAdder(seed=123, noise_filter=noise_filter) # We can apply the NoiseAdder to our image data. img_with_noise = noise.forward(img) # We will plot the original and first noisy image, # because we only have one image in our Image stack right now. fig, axs = plt.subplots(1, 2) axs[0].imshow(img[0], cmap=plt.cm.gray) axs[0].set_title("Starting Image") axs[1].imshow(img_with_noise[0], cmap=plt.cm.gray) axs[1].set_title("Noisy Image")
class ImageTestCase(TestCase): def setUp(self): # numpy array for top-level functions that directly expect it self.im_np = misc.face(gray=True).astype( np.float64)[np.newaxis, :768, :768] # Independent Image object for testing Image methods self.im = Image(misc.face(gray=True).astype(np.float64)[:768, :768]) # Construct a simple stack of Images self.n = 3 self.ims_np = np.empty((3, *self.im_np.shape[1:]), dtype=self.im_np.dtype) for i in range(self.n): self.ims_np[i] = self.im_np * (i + 1) / float(self.n) # Independent Image stack object for testing Image methods self.ims = Image(self.ims_np) def tearDown(self): pass def testImShift(self): # Ensure that the two separate im_translate functions we have return the same thing # A single shift applied to all images shifts = np.array([100, 200]) im = self.im.shift(shifts) im1 = self.im._im_translate(shifts) # Note the difference in the concept of shifts for _im_translate2 - negative sign im2 = _im_translate2(self.im_np, -shifts) # Pure numpy 'shifting' # 'Shifting' an Image corresponds to a 'roll' of a numpy array - again, note the negated signs and the axes im3 = np.roll(self.im.asnumpy()[0], -shifts, axis=(0, 1)) self.assertTrue(np.allclose(im.asnumpy(), im1.asnumpy())) self.assertTrue(np.allclose(im1.asnumpy(), im2.asnumpy())) self.assertTrue(np.allclose(im1.asnumpy()[0, :, :], im3)) def testArrayImageSource(self): # An Image can be wrapped in an ArrayImageSource when we need to deal with ImageSource objects. src = ArrayImageSource(self.im) im = src.images(start=0, num=np.inf) self.assertTrue(np.allclose(im.asnumpy(), self.im_np)) def testImageSqrt(self): self.assertTrue( np.allclose(self.im.sqrt().asnumpy(), np.sqrt(self.im_np))) self.assertTrue( np.allclose(self.ims.sqrt().asnumpy(), np.sqrt(self.ims_np))) def testImageTranspose(self): self.assertTrue( np.allclose(self.im.flip_axes().asnumpy(), np.transpose(self.im_np, (0, 2, 1)))) # This is equivalent to checking np.tranpose(..., (0, 2, 1)) for i in range(self.ims_np.shape[0]): self.assertTrue( np.allclose(self.ims.flip_axes()[i], self.ims_np[i].T)) # Check against the contruction. self.assertTrue( np.allclose(self.ims.flip_axes()[i], self.im_np[0].T * (i + 1) / float(self.n)))