def test_ssb_rotate(): ctx = lt.Context(executor=InlineJobExecutor()) dtype = np.float64 scaling = 4 det = 45 shape = (29, 30, det, det) # ? shape = np.random.uniform(1, 300, (4,1,)) # The acceleration voltage U in keV U = 300 lamb = wavelength(U) # STEM pixel size in m, here 50 STEM pixels on 0.5654 nm dpix = 0.5654 / 50 * 1e-9 # STEM semiconvergence angle in radians semiconv = 25e-3 # Diameter of the primary beam in the diffraction pattern in pixels semiconv_pix = 78.6649 / scaling cy = det // 2 cx = det // 2 input_data = (np.random.uniform(0, 1, np.prod(shape)) * np.linspace(1.0, 1000.0, num=np.prod(shape))) input_data = input_data.astype(np.float64).reshape(shape) data_90deg = np.zeros_like(input_data) # Rotate 90 degrees clockwise for y in range(det): for x in range(det): data_90deg[:, :, x, det - 1 - y] = input_data[:, :, y, x] udf = SSB_UDF(lamb=lamb, dpix=dpix, semiconv=semiconv, semiconv_pix=semiconv_pix, dtype=dtype, cy=cy, cx=cx, transformation=rotate_deg(-90.)) dataset = MemoryDataSet( data=data_90deg, tileshape=(20, shape[2], shape[3]), num_partitions=2, sig_dims=2, ) result = ctx.run_udf(udf=udf, dataset=dataset) result_f, _ = reference_ssb(input_data, U=U, dpix=dpix, semiconv=semiconv, semiconv_pix=semiconv_pix, cy=cy, cx=cx) assert np.allclose(result['pixels'].data, result_f)
def test_rotate_deg(): y, x = np.random.random((2, 7)) degrees = 23 r_y, r_x = c.rotate_deg(degrees) @ (y, x) r_y2, r_x2 = rotate_deg(y, x, degrees) assert np.allclose(r_y, r_y2) assert np.allclose(r_x, r_x2)
def get_generic_results(self, img_sum, img_y, img_x): ref_x = self.parameters["cx"] ref_y = self.parameters["cy"] y_centers_raw, x_centers_raw = center_shifts(img_sum, img_y, img_x, ref_y, ref_x) shape = y_centers_raw.shape if self.parameters["flip_y"]: transform = flip_y() else: transform = identity() # Transformations are applied right to left transform = rotate_deg(self.parameters["scan_rotation"]) @ transform y_centers, x_centers = transform @ (y_centers_raw.reshape(-1), x_centers_raw.reshape(-1)) y_centers = y_centers.reshape(shape) x_centers = x_centers.reshape(shape) if img_sum.dtype.kind == 'c': x_real, x_imag = np.real(x_centers), np.imag(x_centers) y_real, y_imag = np.real(y_centers), np.imag(y_centers) return COMResultSet([ AnalysisResult(raw_data=x_real, visualized=visualize_simple(x_real), key="x_real", title="x [real]", desc="x component of the center"), AnalysisResult(raw_data=y_real, visualized=visualize_simple(y_real), key="y_real", title="y [real]", desc="y component of the center"), AnalysisResult(raw_data=x_imag, visualized=visualize_simple(x_imag), key="x_imag", title="x [imag]", desc="x component of the center"), AnalysisResult(raw_data=y_imag, visualized=visualize_simple(y_imag), key="y_imag", title="y [imag]", desc="y component of the center"), ]) else: f = CMAP_CIRCULAR_DEFAULT.rgb_from_vector((y_centers, x_centers)) d = divergence(y_centers, x_centers) c = curl_2d(y_centers, x_centers) m = magnitude(y_centers, x_centers) return COMResultSet([ AnalysisResult(raw_data=(x_centers, y_centers), visualized=f, key="field", title="field", desc="cubehelix colorwheel visualization", include_in_download=False), AnalysisResult(raw_data=m, visualized=visualize_simple(m), key="magnitude", title="magnitude", desc="magnitude of the vector field"), AnalysisResult(raw_data=d, visualized=visualize_simple(d), key="divergence", title="divergence", desc="divergence of the vector field"), AnalysisResult(raw_data=c, visualized=visualize_simple(c), key="curl", title="curl", desc="curl of the 2D vector field"), AnalysisResult(raw_data=x_centers, visualized=visualize_simple(x_centers), key="x", title="x", desc="x component of the center"), AnalysisResult(raw_data=y_centers, visualized=visualize_simple(y_centers), key="y", title="y", desc="y component of the center"), ])
def test_transformation_rot90(): data_shape = np.random.randint(1, 77, 3, dtype=int) data = np.random.random(data_shape) source_shape = data_shape[1:] target_shape = tuple(reversed(data_shape[1:])) m = image_transformation_matrix( source_shape=source_shape, target_shape=target_shape, affine_transformation=lambda x: x @ rotate_deg(90), pre_transform=lambda x: x - np.array(target_shape) / 2 + 0.5, post_transform=lambda x: np.round(x + np.array(source_shape) / 2 - 0.5 ).astype(int)) res = apply_matrix(data, m, target_shape) # positive rotations are clockwise, upper right corner # is now lower right corner assert np.allclose(data[:, 0, -1], res[:, -1, -1]) # alternative rotation: transpose and flip assert np.allclose(np.flip(np.transpose(data, axes=(0, 2, 1)), axis=2), res)
def apply_correction(y_centers, x_centers, scan_rotation, flip_y, forward=True): shape = y_centers.shape if flip_y: transform = coordinates.flip_y() else: transform = coordinates.identity() # Transformations are applied right to left transform = coordinates.rotate_deg(scan_rotation) @ transform y_centers = y_centers.reshape(-1) x_centers = x_centers.reshape(-1) if not forward: transform = np.linalg.inv(transform) y_transformed, x_transformed = transform @ (y_centers, x_centers) y_transformed = y_transformed.reshape(shape) x_transformed = x_transformed.reshape(shape) return (y_transformed, x_transformed)
def get_generic_results(self, img_sum, img_y, img_x, damage): from libertem.viz import CMAP_CIRCULAR_DEFAULT, visualize_simple ref_x = self.parameters["cx"] ref_y = self.parameters["cy"] y_centers_raw, x_centers_raw = center_shifts(img_sum, img_y, img_x, ref_y, ref_x) shape = y_centers_raw.shape if self.parameters["flip_y"]: transform = flip_y() else: transform = identity() # Transformations are applied right to left transform = rotate_deg(self.parameters["scan_rotation"]) @ transform y_centers, x_centers = transform @ (y_centers_raw.reshape(-1), x_centers_raw.reshape(-1)) y_centers = y_centers.reshape(shape) x_centers = x_centers.reshape(shape) if img_sum.dtype.kind == 'c': x_real, x_imag = np.real(x_centers), np.imag(x_centers) y_real, y_imag = np.real(y_centers), np.imag(y_centers) return COMResultSet([ AnalysisResult(raw_data=x_real, visualized=visualize_simple(x_real, damage=damage), key="x_real", title="x [real]", desc="x component of the center"), AnalysisResult(raw_data=y_real, visualized=visualize_simple(y_real, damage=damage), key="y_real", title="y [real]", desc="y component of the center"), AnalysisResult(raw_data=x_imag, visualized=visualize_simple(x_imag, damage=damage), key="x_imag", title="x [imag]", desc="x component of the center"), AnalysisResult(raw_data=y_imag, visualized=visualize_simple(y_imag, damage=damage), key="y_imag", title="y [imag]", desc="y component of the center"), ]) else: damage = damage & np.isfinite(x_centers) & np.isfinite(y_centers) # Make sure that an all-False `damage` is handled since np.max() # trips on an empty array. # As a remark -- the NumPy error message # "zero-size array to reduction operation maximum which has no identity" # is probably wrong since -np.inf is the identity element for maximum on # floating point numbers and should be returned here. if np.count_nonzero(damage) > 0: vmax = np.sqrt( np.max(x_centers[damage]**2 + y_centers[damage]**2)) else: vmax = 1 f = CMAP_CIRCULAR_DEFAULT.rgb_from_vector( (x_centers, y_centers, 0), vmax=vmax) d = divergence(y_centers, x_centers) c = curl_2d(y_centers, x_centers) m = magnitude(y_centers, x_centers) return COMResultSet([ AnalysisResult(raw_data=(x_centers, y_centers), visualized=f, key="field", title="field", desc="cubehelix colorwheel visualization", include_in_download=False), AnalysisResult(raw_data=m, visualized=visualize_simple(m, damage=damage), key="magnitude", title="magnitude", desc="magnitude of the vector field"), AnalysisResult(raw_data=d, visualized=visualize_simple(d, damage=damage), key="divergence", title="divergence", desc="divergence of the vector field"), AnalysisResult(raw_data=c, visualized=visualize_simple(c, damage=damage), key="curl", title="curl", desc="curl of the 2D vector field"), AnalysisResult(raw_data=x_centers, visualized=visualize_simple(x_centers, damage=damage), key="x", title="x", desc="x component of the center"), AnalysisResult(raw_data=y_centers, visualized=visualize_simple(y_centers, damage=damage), key="y", title="y", desc="y component of the center"), ])