def read_focal_nodes(self, filename): """Loads list of focal nodes for arbitrary graph.""" focal_nodes = CSIO.load_graph(filename) try: if filename == self.options.habitat_file:#If graph was used as focal node file, then just want first two columns for focal_nodes. focal_nodes = ComputeBase.deletecol(focal_nodes, 2) focal_nodes = np.unique(np.asarray(focal_nodes)) except: raise RuntimeError('Error processing focal node file. Please check file format') return focal_nodes
def read_graph(self, filename): """Reads arbitrary graph from disk. Returns sparse adjacency matrix and node names .""" graph_list = CSIO.load_graph(filename) try: zeros_in_resistance_graph = False nodes = ComputeBase.deletecol(graph_list,2) node_names = np.unique(np.asarray(nodes, dtype='int32')) nodes[np.where(nodes>= 0)] = ComputeBase.relabel(nodes[np.where(nodes>= 0)], 0) node1 = nodes[:,0] node2 = nodes[:,1] data = graph_list[:,2] ######################## Reclassification code if self.options.use_reclass_table == True: try: reclass_table = CSIO.read_point_strengths(self.options.reclass_file) except: raise RuntimeError('Error reading reclass table. Please check file format.') for i in range (0,reclass_table.shape[0]): data = np.where(data==reclass_table[i,0], reclass_table[i,1],data) Compute.logger.info('Reclassified landscape graph using %s'%(self.options.reclass_file,)) ######################## if self.options.habitat_map_is_resistances == True: zeros_in_resistance_graph = (np.where(data==0, 1, 0)).sum() > 0 conductances = 1/data else: conductances = data numnodes = node_names.shape[0] G = sparse.csr_matrix((conductances, (node1, node2)), shape = (numnodes, numnodes)) Gdense=G.todense() g_graph = np.maximum(Gdense, Gdense.T) # To handle single or double entries for elements BHM 06/28/11 g_graph = sparse.csr_matrix(g_graph) except: raise RuntimeError('Error processing graph/network file. Please check file format') if zeros_in_resistance_graph == True: raise RuntimeError('Error: zero resistance values are not currently allowed in network/graph input file.') return g_graph, node_names