def test_small_radius_1(self): imX, imY = 3, 3 mask = afr._make_circular_mask(1, 1, imX, imY, 1) assert mask.size == imX * imY assert mask.sum() == 5 true_index = [[1, 0], [0, 1], [1, 1], [2, 1], [1, 2]] false_index = [[0, 0], [0, 2], [2, 0], [2, 2]] for index in true_index: assert mask[index[0], index[1]] for index in false_index: assert not mask[index[0], index[1]]
def find_atom_intensity_inside_mask(self, image_data, radius): """Find the average intensity inside a circle. The circle is defined by the atom position, and the given radius (in pixels). The outside this area is covered by a mask. The average intensity is saved to self.intensity_mask. """ if radius is None: radius = 1 centerX, centerY = self.pixel_x, self.pixel_y mask = _make_circular_mask(centerY, centerX, image_data.shape[0], image_data.shape[1], radius) data_mask = image_data * mask self.intensity_mask = np.mean(data_mask[np.nonzero(mask)])
def calculate_total_intensity(self, image_data, percent_to_nn=0.40, mask_radius=None): """Find the min intensity of the atom. The min is found within the the distance to the nearest neighbor times percent_to_nn. """ #### if (mask_radius is None) and (percent_to_nn is None): raise ValueError( "Both mask_radius and percent_to_nn is None, one of them must " "be set") elif (mask_radius is not None) and (percent_to_nn is not None): raise ValueError( "Both mask_radius and percent_to_nn are set, only one of them " "can be set") elif mask_radius is None: closest_neighbor = self.get_closest_neighbor() slice_size = closest_neighbor * percent_to_nn * 2 data_slice, x0, y0 = self._get_image_slice_around_atom( image_data, slice_size) data_slice_total = data_slice.sum() elif mask_radius is not None: mask = _make_circular_mask(centerX=self.pixel_y, centerY=self.pixel_x, imageSizeX=image_data.shape[0], imageSizeY=image_data.shape[1], radius=mask_radius) data_slice_total = image_data[mask].sum() #### self.amplitude_total_intensity = data_slice_total return (data_slice_total)
def get_center_position_com(self, image_data, percent_to_nn=0.40, mask_radius=None): 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 mask = _make_circular_mask(self.pixel_y, self.pixel_x, image_data.shape[0], image_data.shape[1], mask_radius) data = copy.deepcopy(image_data) mask = np.invert(mask) data[mask] = 0 center_of_mass = self._calculate_center_of_mass(data) new_x, new_y = center_of_mass[1], center_of_mass[0] return (new_x, new_y)
def remove_local_background(sublattice, background_sub, intensity_type, num_points=3, percent_to_nn=0.40, mask_radius=None): ''' Remove the local background from a sublattice intensity using a background sublattice. Parameters ---------- sublattice : sublattice object The sublattice whose intensities are of interest. intensity_type : string Determines the method used to find the sublattice intensities. The available methods are 'max', 'mean', 'min', 'total' and 'all'. background_sub : sublattice object The sublattice used to find the local backgrounds. num_points : int, default 3 The number of nearest neighbour values averaged from background_sub percent_to_nn : float, default 0.40 Determines the boundary of the area surrounding each atomic column, as fraction of the distance to the nearest neighbour. Returns ------- 2D numpy array Examples -------- >>> from temul.intensity_tools import remove_local_background >>> import atomap.dummy_data as dummy_data >>> sublattice = dummy_data.get_simple_cubic_sublattice() >>> sublattice.find_nearest_neighbors() >>> intensities_total = remove_local_background( ... sublattice, intensity_type='total', ... background_sub=sublattice) >>> intensities_max = remove_local_background( ... sublattice, intensity_type='max', ... background_sub=sublattice) ''' # get background_sub intensity list if percent_to_nn is not None: sublattice.find_nearest_neighbors() background_sub.find_nearest_neighbors() else: pass background_sub.get_atom_column_amplitude_min_intensity( percent_to_nn=percent_to_nn, mask_radius=mask_radius) background_sub_min_intensity_list = [] background_sub_min_intensity_list.append( background_sub.atom_amplitude_min_intensity) background_sub_min_intensity_list = background_sub_min_intensity_list[0] if intensity_type == 'all': raise ValueError("All intensities has not yet been implemented. " "Use max, mean or total instead") if num_points < 1: raise ValueError( "num_points cannot be less than 1 if you wish to locally " "remove the background") if intensity_type == 'max': # get list of sublattice and background_sub atom positions # np.array().T will not be needed in newer versions of atomap sublattice_atom_pos = np.array(sublattice.atom_positions).T background_sub_atom_pos = np.array(background_sub.atom_positions).T # get sublattice intensity list # could change to my function, which allows choice of intensity type sublattice.get_atom_column_amplitude_max_intensity( percent_to_nn=percent_to_nn, mask_radius=mask_radius) sublattice_max_intensity_list = [] sublattice_max_intensity_list.append( sublattice.atom_amplitude_max_intensity) sublattice_max_intensity_list = sublattice_max_intensity_list[0] # create list which will be output # therefore the original data is not changed! sublattice_max_intensity_list_bksubtracted = [] # for each sublattice atom position, calculate the nearest # background_sub atom positions. for p in range(0, len(sublattice_atom_pos)): xy_distances = background_sub_atom_pos - \ sublattice_atom_pos[p] # put all distances in this array with this loop vector_array = [] for i in range(0, len(xy_distances)): # get distance from sublattice position to every # background_sub position vector = np.sqrt((xy_distances[i][0]**2) + (xy_distances[i][1]**2)) vector_array.append(vector) # convert to numpy array vector_array = np.array(vector_array) # sort through the vector_array and find the 1st to kth # smallest distance and find the # corressponding index # num_points is the number of nearest points from which the # background will be averaged k = num_points min_indices = list(np.argpartition(vector_array, k)[:k]) # sum the chosen intensities and find the mean # (or median - add this) local_bkgnd = 0 for index in min_indices: local_bkgnd += background_sub.atom_amplitude_min_intensity[ index] local_background_mean = local_bkgnd / k # subtract this mean local background intensity from the sublattice # atom position intensity # indexing here is the loop digit p sublattice_bksubtracted_atom = np.array( sublattice.atom_amplitude_max_intensity[p]) - \ np.array(local_background_mean) sublattice_max_intensity_list_bksubtracted.append( [sublattice_bksubtracted_atom]) sublattice_max_intensity_list_bksubtracted = np.array( sublattice_max_intensity_list_bksubtracted) return (sublattice_max_intensity_list_bksubtracted[:, 0]) elif intensity_type == 'mean': # get list of sublattice and background_sub atom positions # np.array().T will not be needed in newer versions of atomap sublattice_atom_pos = np.array(sublattice.atom_positions).T background_sub_atom_pos = np.array(background_sub.atom_positions).T # get sublattice intensity list # could change to my function, which allows choice of intensity type sublattice.get_atom_column_amplitude_mean_intensity( percent_to_nn=percent_to_nn, mask_radius=mask_radius) sublattice_mean_intensity_list = [] sublattice_mean_intensity_list.append( sublattice.atom_amplitude_mean_intensity) sublattice_mean_intensity_list = sublattice_mean_intensity_list[0] # create list which will be output # therefore the original data is not changed! sublattice_mean_intensity_list_bksubtracted = [] # for each sublattice atom position, calculate the nearest # background_sub atom positions. for p in range(0, len(sublattice_atom_pos)): xy_distances = background_sub_atom_pos - \ sublattice_atom_pos[p] # put all distances in this array with this loop vector_array = [] for i in range(0, len(xy_distances)): # get distance from sublattice position to every # background_sub position vector = np.sqrt((xy_distances[i][0]**2) + (xy_distances[i][1]**2)) vector_array.append(vector) # convert to numpy array vector_array = np.array(vector_array) # sort through the vector_array and find the 1st to kth smallest # distance and find the # corressponding index # num_points is the number of nearest points from which the # background will be averaged k = num_points min_indices = list(np.argpartition(vector_array, k)[:k]) # sum the chosen intensities and find the mean # (or median - add this) local_bkgnd = 0 for index in min_indices: local_bkgnd += background_sub.atom_amplitude_min_intensity[ index] local_background_mean = local_bkgnd / k # subtract this mean local background intensity from the sublattice # atom position intensity # indexing here is the loop digit p sublattice_bksubtracted_atom = np.array( sublattice.atom_amplitude_mean_intensity[p]) - \ local_background_mean sublattice_mean_intensity_list_bksubtracted.append( [sublattice_bksubtracted_atom]) sublattice_mean_intensity_list_bksubtracted = np.array( sublattice_mean_intensity_list_bksubtracted) return (sublattice_mean_intensity_list_bksubtracted[:, 0]) elif intensity_type == 'total': # get list of sublattice and background_sub atom positions # np.array().T will not be needed in newer versions of atomap sublattice_atom_pos = np.array(sublattice.atom_positions).T background_sub_atom_pos = np.array(background_sub.atom_positions).T # get sublattice intensity list # could change to my function, which allows choice of intensity type sublattice.get_atom_column_amplitude_total_intensity( percent_to_nn=percent_to_nn, mask_radius=mask_radius) sublattice_total_intensity_list = [] sublattice_total_intensity_list.append( sublattice.atom_amplitude_total_intensity) sublattice_total_intensity_list = sublattice_total_intensity_list[0] # create list which will be output # therefore the original data is not changed! sublattice_total_intensity_list_bksubtracted = [] # for each sublattice atom position, calculate the nearest # background_sub atom positions. for p in range(0, len(sublattice_atom_pos)): xy_distances = background_sub_atom_pos - \ sublattice_atom_pos[p] # put all distances in this array with this loop vector_array = [] for i in range(0, len(xy_distances)): # get distance from sublattice position to every # background_sub position vector = np.sqrt((xy_distances[i][0]**2) + (xy_distances[i][1]**2)) vector_array.append(vector) # convert to numpy array vector_array = np.array(vector_array) # sort through the vector_array and find the 1st to # kth smallest distance and find the # corressponding index # num_points is the number of nearest points from which # the background will be averaged k = num_points min_indices = list(np.argpartition(vector_array, range(k))[:k]) # if you want the values rather than the indices, use: # vector_array[np.argpartition(vector_array, range(k))[:k]] # sum the chosen intensities and find the total # (or median - add this) local_bkgnd = 0 for index in min_indices: local_bkgnd += background_sub.atom_amplitude_min_intensity[ index] local_background_mean = local_bkgnd / k # for summing pixels around atom if mask_radius is None: pixel_count_in_region = get_pixel_count_from_image_slice( sublattice.atom_list[p], sublattice.image, percent_to_nn) elif mask_radius is not None: mask = _make_circular_mask( centerX=sublattice.atom_list[p].pixel_x, centerY=sublattice.atom_list[p].pixel_y, imageSizeX=sublattice.image.shape[0], imageSizeY=sublattice.image.shape[1], radius=mask_radius) pixel_count_in_region = len(sublattice.image[mask]) local_background_mean_summed = pixel_count_in_region * \ local_background_mean # subtract this mean local background intensity from the sublattice # atom position intensity # indexing here is the loop digit p sublattice_bksubtracted_atom = np.array( sublattice.atom_amplitude_total_intensity[p]) - \ local_background_mean_summed sublattice_total_intensity_list_bksubtracted.append( [sublattice_bksubtracted_atom]) sublattice_total_intensity_list_bksubtracted = np.array( sublattice_total_intensity_list_bksubtracted) return (sublattice_total_intensity_list_bksubtracted[:, 0]) else: raise ValueError( "You must choose a valid intensity_type. Try max, mean or total")
def _get_dumbbell_arrays( s, dumbbell_positions, dumbbell_vector, show_progressbar=True): """ Parameters ---------- s : HyperSpy 2D signal dumbbell_positions : list of atomic positions In the form [[x0, y0], [x1, y1], [x2, y2], ...] dumbbell_vector : tuple show_progressbar : bool, default True Returns ------- Dumbbell lists : tuple of lists Examples -------- >>> import atomap.initial_position_finding as ipf >>> s = am.dummy_data.get_dumbbell_signal() >>> dumbbell_positions = am.get_atom_positions(s, separation=16) >>> atom_positions = am.get_atom_positions(s, separation=4) >>> dumbbell_vector = ipf.find_dumbbell_vector(atom_positions) >>> d0, d1 = ipf._get_dumbbell_arrays(s, dumbbell_positions, ... dumbbell_vector) """ next_pos_list0 = [] next_pos_list1 = [] for x, y in zip(dumbbell_positions[:, 0], dumbbell_positions[:, 1]): next_pos_list0.append([dumbbell_vector[0]+x, dumbbell_vector[1]+y]) next_pos_list1.append([-dumbbell_vector[0]+x, -dumbbell_vector[1]+y]) next_pos_list0 = np.array(next_pos_list0) next_pos_list1 = np.array(next_pos_list1) mask_radius = 0.5*(dumbbell_vector[0]**2+dumbbell_vector[1]**2)**0.5 iterator = zip( dumbbell_positions[:, 0], dumbbell_positions[:, 1], next_pos_list0, next_pos_list1) total_num = len(next_pos_list0) dumbbell_list0, dumbbell_list1 = [], [] for x, y, next_pos0, next_pos1 in progressbar( iterator, total=total_num, desc="Finding dumbbells", disable=not show_progressbar): mask1 = _make_circular_mask( next_pos0[1], next_pos0[0], s.data.shape[0], s.data.shape[1], mask_radius) mask2 = _make_circular_mask( next_pos1[1], next_pos1[0], s.data.shape[0], s.data.shape[1], mask_radius) pos1_sum = (s.data*mask1).sum() pos2_sum = (s.data*mask2).sum() if pos1_sum > pos2_sum: dumbbell_list0.append([x, y]) dumbbell_list1.append(next_pos0) else: dumbbell_list0.append(next_pos1) dumbbell_list1.append([x, y]) dumbbell_list0 = np.array(dumbbell_list0) dumbbell_list1 = np.array(dumbbell_list1) return(dumbbell_list0, dumbbell_list1)
def test_all_false_mask(self): mask = afr._make_circular_mask(10, 10, 5, 5, 3) assert not mask.any()
def test_all_true_mask(self): imX, imY = 5, 5 mask = afr._make_circular_mask(1, 1, imX, imY, 5) assert mask.all() assert mask.size == imX * imY assert mask.sum() == imX * imY