def get_homogeneity_overlapping_domain(self, ov_domain): """ homogeneity for an overlapping domain object """ indices = np.argwhere(ov_domain.map > 0); homogeneity = 0; N = len(indices); ##compute correlation between all pairs of grid cells for i in range(N): ts_a = self.data[:,indices[i][0],indices[i][1]]; for j in range(i+1, N): ts_b = self.data[:,indices[j][0],indices[j][1]]; corr = lagged_correlation(ts_a, ts_b,tau=0,normed=True); homogeneity += corr; ##normalize n_pairs = N*(N-1)/2.; homogeneity = homogeneity/n_pairs; return homogeneity;
def get_homogeneity_union(self, domain_a, domain_b): """ Returns the homogeneity of the union (merging) of two domains """ ##get the overlap map overlap_map = domain_a.map+domain_b.map; ##get the indices of the grid cells of both domains indices = np.argwhere(overlap_map > 0); homogeneity = 0; N = len(indices); ##compute correlation between all pairs of grid cells for i in range(N): ts_a = self.data[:,indices[i][0],indices[i][1]]; for j in range(i+1, N): ts_b = self.data[:,indices[j][0],indices[j][1]]; corr = lagged_correlation(ts_a, ts_b,tau=0,normed=True); homogeneity += corr; ##normalize n_pairs = N*(N-1)/2. homogeneity = homogeneity/n_pairs; return homogeneity;
def domain_initialization(self): ##get the locations of the seeds seed_locations = np.where(self.seed_positions == 1); domains = []; for i in range(len(seed_locations[0])): ## 1 domain per seed location domain = Domain(i); ##init empty domain domain.map = np.zeros((self.data.shape[1], self.data.shape[2]), dtype = np.int32); ##init domain map; ##add to the domain map the seed location init_index = np.asarray([seed_locations[0][i],seed_locations[1][i]]) domain.map[init_index[0], init_index[1]] = 1; ##get the corresponding lat/lon coordinates coord_index = np.where( (self.indices[:,0] == init_index[0]) & (self.indices[:,1] == init_index[1]) )[0]; init_coords = self.coords[coord_index]; ##corresponding lat,lon of init_index ##and no go and get the k nearest neighbors and add them as well _,k_neighborhood_indices = self.nearest_neighbors.kneighbors(init_coords); k_neighborhood_indices = k_neighborhood_indices[0]; for k_index in k_neighborhood_indices: neigh_indices = self.indices[k_index]; ##get the corresponding grid cell try: grid_cell_id = self.pos_to_grid_cell_id[str(neigh_indices[0])+","+str(neigh_indices[1])]; grid_cell = self.grid_cells[grid_cell_id]; ##add it to the domain domain.grid_cells[grid_cell_id] = grid_cell; ##update domain map domain.map[neigh_indices[0], neigh_indices[1]] = 1; except KeyError: pass; ##grid cell in land ##final step, initialize the local homogeneity of the domain local_homogeneity = 0; valid_pairs = 0; for i in range(len(k_neighborhood_indices)): indices_i = self.indices[k_neighborhood_indices[i]]; timeseries_i = self.data[:, indices_i[0],indices_i[1]]; if(np.isnan(timeseries_i[0])): ##in land... continue; for j in range(i+1,len(k_neighborhood_indices)): indices_j = self.indices[k_neighborhood_indices[j]]; timeseries_j = self.data[:, indices_j[0],indices_j[1]]; if(np.isnan(timeseries_j[0])): continue; corr = lagged_correlation(timeseries_i, timeseries_j,tau=0,normed=True); local_homogeneity += corr; valid_pairs += 1; ##get the average correlations (local homogeneity) if(valid_pairs == 0): ##grid cell inland local_homogeneity = np.nan; else: local_homogeneity = local_homogeneity/valid_pairs; domain.homogeneity = local_homogeneity; domains.append(domain); return domains;
def expand_domain(self, domain): """ Performs one step of domain expansion. Returns: domain: The expanded domain (if domain could be expanded) is_expanded: (boolean) True if domain is expanded False if not """ is_expanded = False; ##generate a set that will have all grid cell ids that are ##already inside the domain domain_grid_cells = set(); for grid_cell_id, grid_cell in domain.grid_cells.items(): domain_grid_cells.add(grid_cell); ##set that will hold all candidate grid cells. candidate_grid_cells = set(); ##list with GridCell objects ##get candidate grid cells for expansion for grid_cell_id, grid_cell in domain.grid_cells.items(): ##for each grid cell get its k=8 nearest neighbors coords = np.expand_dims(np.asarray([grid_cell.lat,grid_cell.lon]),0); _,k_neighborhood_indices = self.nearest_neighbors.kneighbors(coords,n_neighbors=8+1); k_neighborhood_indices = k_neighborhood_indices[0]; ##remoove self k_neighborhood_indices = k_neighborhood_indices[1:]; for k_index in k_neighborhood_indices: neigh_indices = self.indices[k_index]; ##x,y index of neighboring grid cell ##get the corresponding grid cell try: ##neighboring grid cell id neigh_grid_cell_id = self.pos_to_grid_cell_id[str(neigh_indices[0])+","+str(neigh_indices[1])]; candidate_grid_cell = self.grid_cells[neigh_grid_cell_id]; if(candidate_grid_cell not in domain_grid_cells): ##grid cell not in domain = grid cell in border candidate_grid_cells.add(candidate_grid_cell); except KeyError: pass; ##grid cell in land ##compute average correlation between each candidate ##grid cell and grid cells inside the domain ##check number 1. do we have any candidate grid cells if(len(candidate_grid_cells) == 0): return domain, is_expanded; for candidate_grid_cell in candidate_grid_cells: candidate_grid_cell_ts = self.data[:,candidate_grid_cell.index[0],candidate_grid_cell.index[1]]; score = 0.; for domain_grid_cell in domain_grid_cells: domain_grid_cell_ts = self.data[:,domain_grid_cell.index[0],domain_grid_cell.index[1]]; correlation = lagged_correlation(candidate_grid_cell_ts, domain_grid_cell_ts, tau=0,normed=True); score += correlation; score = score/len(domain_grid_cells); candidate_grid_cell.score = score; ##sort grid cells by score in descending order. candidate_grid_cells = list(candidate_grid_cells); candidate_grid_cells.sort(key=lambda x: x.score, reverse=True); ##get the best candidate best_candidate = candidate_grid_cells[0]; if(best_candidate.score > self.delta): ##then add the grid cell ##update domain homogeneity domain_pairs = len(domain.grid_cells)*(len(domain.grid_cells)-1)/2. current_homogeneity = domain.homogeneity*domain_pairs; bc_score = best_candidate.score*len(domain.grid_cells); domain.homogeneity = (bc_score+current_homogeneity)/(len(domain.grid_cells)*(len(domain.grid_cells)+1)/2.); domain.grid_cells[best_candidate.id] = best_candidate; ##update domain map domain.map[best_candidate.index[0], best_candidate.index[1]] = 1; is_expanded = True; return domain, is_expanded; else: ##this domain can not be expanded return domain, is_expanded;