def test_non_square1(self): image = np.zeros((40, 80)) y, x = 38, 11 image[y, x] = 10. cy, cx = afr.calculate_center_of_mass(image) assert cy == float(y) assert cx == float(x)
def _get_center_position_com( self, image_data, percent_to_nn=0.40, mask_radius=None): '''Get new atom position based on the center of mass approach Parameters ---------- image_data : Numpy 2D array percent_to_nn : float, optional The percent of the distance to the nearest neighbor atom in the same sub lattice. The distance times this percentage defines the mask around the atom where the Gaussian will be fitted. A smaller value can reduce the effect from neighboring atoms, but might also decrease the accuracy of the fitting due to less data to fit to. Default 0.4 (40%). mask_radius : float, optional Radius of the mask around each atom. If this is not set, the radius will be the distance to the nearest atom in the same sublattice times the `percent_to_nn` value. Note: if `mask_radius` is not specified, the Atom_Position objects must have a populated nearest_neighbor_list. Returns ------- new_position : tuple (new x, new y) ''' if mask_radius is None: closest_neighbor = 100000000000000000 for neighbor_atom in self.nearest_neighbor_list: distance = self.get_pixel_distance_from_another_atom( neighbor_atom) if distance < closest_neighbor: closest_neighbor = distance mask_radius = closest_neighbor * percent_to_nn cx, cy = int(round(self.pixel_x)), int(round(self.pixel_y)) crop_radius = np.ceil(mask_radius).astype(int) data = _crop_array(image_data, cx, cy, crop_radius+1) edgeX, edgeY = cx - crop_radius, cy - crop_radius data2 = zero_array_outside_circle(data, mask_radius) new_y, new_x = calculate_center_of_mass(data2) new_x, new_y = edgeX + new_x, edgeY + new_y return new_x, new_y
def test_compare_center_of_mass(self): from scipy.ndimage import center_of_mass rand = np.random.random((5, 5)) center_of_mass(rand) == afr.calculate_center_of_mass(rand)
def test_find_center(self): center = np.zeros((5, 5)) center[1, 1] = 1 assert afr.calculate_center_of_mass(center) == (1, 1)