def create_dataset(self, dataset_file): """Create a dataset for testing this module. Only called with setUp detects that `dataset_file` has been deleted. """ import libimage amplitude = libimage.load("cryptomeria", 128) phase = libimage.load("bombus", 128) original = amplitude * np.exp(1j * phase * np.pi) self.original = np.expand_dims(original, axis=0).astype('complex64') np.random.seed(0) self.flow = np.empty((*self.original.shape, 2), dtype='float32') self.flow[..., :] = 5 * (np.random.rand(2) - 0.5) self.shift = 2 * (np.random.rand(*self.original.shape[:-2], 2) - 0.5) self.data = tike.align.simulate( original=self.original, flow=self.flow, shift=self.shift, padded_shape=None, angle=None, ) setup_data = [ self.data, self.original, self.flow, self.shift, ] with lzma.open(dataset_file, 'wb') as file: pickle.dump(setup_data, file)
def test_image(self, s=32, ntheta=16 * 16 * 16): import libimage import matplotlib.pyplot as plt f = libimage.load('satyre', s) f = np.tile(f, (s, 1, 1)) f = self.xp.asarray(f, dtype='complex64') x = [ g.ravel() for g in np.meshgrid( np.linspace(-0.5, 0.5, s), np.linspace(-0.5, 0.5, s), np.linspace(-0.5, 0.5, s), ) ] x = np.stack(x, -1) print(x.shape) x = self.xp.asarray(x, dtype='float32') d = self.operator.fwd(f, x, s) m = self.operator.adj(d, x, s) plt.figure() plt.imshow(m[s // 2].real.get()) plt.figure() plt.imshow(f[s // 2].real.get()) plt.show()
def debug_show(self): import libimage import matplotlib.pyplot as plt x = self.xp.asarray(libimage.load('coins', 256), dtype='complex64') y = self.operator.fwd(x[None], 4 * np.pi) print(x.shape, y.shape) plt.figure() plt.imshow(x.real.get()) plt.figure() plt.imshow(y[0].real.get()) plt.show()
def create_dataset( self, dataset_file, pw=16, eigen=1, width=128, ): """Create a dataset for testing this module. Only called with setUp detects that `dataset_file` has been deleted. """ import libimage # Create a stack of phase-only images phase = np.stack( [libimage.load('satyre', width), libimage.load('satyre', width)], axis=0, ) amplitude = np.stack( [ 1 - 0 * libimage.load('coins', width), 1 - libimage.load('coins', width) ], axis=0, ) original = amplitude * np.exp(1j * phase * np.pi) self.original = original.astype('complex64') leading = self.original.shape[:-2] # Create a multi-probe with gaussian amplitude decreasing as 1/N phase = np.stack( [ 1 - libimage.load('cryptomeria', pw), 1 - libimage.load('bombus', pw) ], axis=0, ) weights = 1.0 / np.arange(1, len(phase) + 1)[:, None, None] weights = weights * tike.ptycho.probe.gaussian(pw, rin=0.8, rout=1.0) probe = weights * np.exp(1j * phase * np.pi) self.probe = np.tile( probe.astype('complex64'), (*leading, 1, eigen, 1, 1, 1), ) pad = 2 v, h = np.meshgrid( np.linspace(pad, original.shape[-2] - pw - pad, 13, endpoint=True), np.linspace(pad, original.shape[-1] - pw - pad, 13, endpoint=True), indexing='ij', ) scan = np.stack((np.ravel(v), np.ravel(h)), axis=1) self.scan = np.tile( scan.astype('float32'), (*leading, 1, 1), ) self.data = tike.ptycho.simulate( detector_shape=pw * 2, probe=self.probe, scan=self.scan, psi=self.original, ) assert self.data.shape == (*leading, 13 * 13, pw * 2, pw * 2) assert self.data.dtype == 'float32', self.data.dtype setup_data = [ self.data, self.scan, self.probe, self.original, ] with lzma.open(dataset_file, 'wb') as file: pickle.dump(setup_data, file)