def set_node_transfers(self): for index, mapping in enumerate(self.tmap): for pair, node in mapping.items(): i, j = pair comm = int(node.comm) comm_elev = node.elev neighbors = Grid._select_surround_ravel( self, comm, self.dem.shape) ser = pd.DataFrame( np.column_stack([ neighbors, self.dem.flat[neighbors], self.ws[index].flat[neighbors] ])) ser = ser[ser[2].isin(list(pair))] g = ser.groupby(2).idxmin()[1].apply(lambda x: ser.loc[x, 0]) fullix = self.drop.flat[g.values.astype(int)] lv = self.dropmap.loc[fullix][0].values nm = self.dropmap.loc[fullix][1].values g = pd.DataFrame(np.column_stack([lv, nm]), index=g.index.values.astype(int), columns=['level', 'name']).to_dict(orient='index') # Children will always be in numeric order from left to right lt, rt = g[j], g[i] node.l.t = self.nodes[lt['level']][lt['name']] node.r.t = self.nodes[rt['level']][rt['name']] self.set_singleton_transfer(self.root)
def set_node_multitransfers(self): for index, value in self.c.items(): for pair, comm in value.items(): i, j = pair lnode = self.nodes[index][i] rnode = self.nodes[index][j] comm_elev = self.dem.flat[comm] neighbors = Grid._select_surround_ravel(self, comm, self.dem.shape) ser = pd.DataFrame(np.column_stack([neighbors, self.dem.flat[neighbors], self.ws[index].flat[neighbors]])) ser = ser[ser[2].isin(list(pair))] g = ser.groupby(2).idxmin()[1].apply(lambda x: ser.loc[x, 0]) fullix = self.drop.flat[g.values.astype(int)] lv = self.dropmap.loc[fullix][0].values nm = self.dropmap.loc[fullix][1].values g = pd.DataFrame(np.column_stack([lv, nm]), index=g.index.values.astype(int), columns=['level', 'name']).to_dict(orient='index') # Children will always be in numeric order from left to right lt, rt = g[j], g[i] if lnode.s: lnode.s.append(self.nodes[lt['level']][lt['name']]) lnode.si.append(comm_elev) else: lnode.s = [self.nodes[lt['level']][lt['name']]] lnode.si = [comm_elev] if rnode.s: rnode.s.append(self.nodes[rt['level']][rt['name']]) rnode.si.append(comm_elev) else: rnode.s = [self.nodes[rt['level']][rt['name']]] rnode.si = [comm_elev] self.set_singleton_multitransfer(self.root)
def find_connections(self): c = {} b = {} inside = np.zeros(self.shape, dtype=bool) inside[1:-1, 1:-1] = True for index in range(len(self.levels) - 1): c[index] = {} b[index] = {} comm = np.flatnonzero((self.ws[index] == 0) & inside) # TODO: Not super elegant neighbors = self.ws[index].flat[Grid._select_surround_ravel( self, comm, self.ws[index].shape)] comms = dict(zip(comm, [set() for i in comm])) for region in self.lup[index].keys(): for elem in comm[(neighbors == region).any(axis=1)]: comms[elem].add(region) for elem in comms: comms[elem] = tuple(comms[elem]) for comm, pair in comms.items(): if len(pair) > 2: for combination in combinations(pair, 2): subpair = tuple(sorted(combination)) if subpair in c[index]: b[index][subpair].append(comm) if self.dem.flat[comm] < self.dem.flat[c[index] [subpair]]: c[index][subpair] = comm else: c[index][subpair] = comm b[index][subpair] = [comm] elif len(pair) < 2: # TODO: Not really sure what's happening in this case pass else: if pair in c[index]: b[index][pair].append(comm) if self.dem.flat[comm] < self.dem.flat[c[index][pair]]: c[index][pair] = comm else: c[index][pair] = comm b[index][pair] = [comm] self.c = c self.b = b
def find_connections(self): c = {} b = {} inside = np.zeros(self.shape, dtype=bool) inside[1:-1, 1:-1] = True for index in range(len(self.levels) - 1): c[index] = {} b[index] = {} comm = np.flatnonzero((self.ws[index] == 0) & inside) # TODO: Not super elegant neighbors = self.ws[index].flat[Grid._select_surround_ravel(self, comm, self.ws[index].shape)] comms = dict(zip(comm, [set() for i in comm])) for region in self.lup[index].keys(): for elem in comm[(neighbors == region).any(axis=1)]: comms[elem].add(region) for elem in comms: comms[elem] = tuple(comms[elem]) for comm, pair in comms.items(): if len(pair) > 2: for combination in combinations(pair, 2): subpair = tuple(sorted(combination)) if subpair in c[index]: b[index][subpair].append(comm) if self.dem.flat[comm] < self.dem.flat[c[index][subpair]]: c[index][subpair] = comm else: c[index][subpair] = comm b[index][subpair] = [comm] elif len(pair) < 2: # TODO: Not really sure what's happening in this case pass else: if pair in c[index]: b[index][pair].append(comm) if self.dem.flat[comm] < self.dem.flat[c[index][pair]]: c[index][pair] = comm else: c[index][pair] = comm b[index][pair] = [comm] self.c = c self.b = b
def set_node_transfers(self): for index, mapping in enumerate(self.tmap): for pair, node in mapping.items(): i, j = pair comm = int(node.comm) comm_elev = node.elev neighbors = Grid._select_surround_ravel(self, comm, self.dem.shape) ser = pd.DataFrame(np.column_stack([neighbors, self.dem.flat[neighbors], self.ws[index].flat[neighbors]])) ser = ser[ser[2].isin(list(pair))] g = ser.groupby(2).idxmin()[1].apply(lambda x: ser.loc[x, 0]) fullix = self.drop.flat[g.values.astype(int)] lv = self.dropmap.loc[fullix][0].values nm = self.dropmap.loc[fullix][1].values g = pd.DataFrame(np.column_stack([lv, nm]), index=g.index.values.astype(int), columns=['level', 'name']).to_dict(orient='index') # Children will always be in numeric order from left to right lt, rt = g[j], g[i] node.l.t = self.nodes[lt['level']][lt['name']] node.r.t = self.nodes[rt['level']][rt['name']] self.set_singleton_transfer(self.root)