Exemplo n.º 1
0
    def compute_network(self): 
        """Solves arbitrary graphs instead of raster grids."""
        (g_graph, node_names) = self.read_graph(self.options.habitat_file)
        
        fp = None
        if self.options.scenario == 'pairwise':
            if self.options.use_included_pairs==True:
                self.state.included_pairs = IncludeExcludePairs(self.options.included_pairs_file)
            
            focal_nodes = self.read_focal_nodes(self.options.point_file)
            fp = FocalPoints(focal_nodes, self.state.included_pairs, True)            
        elif self.options.scenario == 'advanced':
            self.state.source_map = CSIO.read_point_strengths(self.options.source_file)
            self.state.ground_map = CSIO.read_point_strengths(self.options.ground_file)        
        
        g_habitat = HabitatGraph(g_graph=g_graph, node_names=node_names)
        out = Output(self.options, self.state, False, node_names)
        if self.options.write_cur_maps:
            out.alloc_c_map('')
        
        Compute.logger.info('Calling solver module.')
        Compute.logger.info('Graph has ' + str(g_habitat.num_nodes) + ' nodes and '+ str(g_habitat.num_components)+ ' components.')
        if self.options.scenario == 'pairwise':
            (resistances, solver_failed) = self.single_ground_all_pair_resistances(g_habitat, fp, out, True)
            if self.options.write_cur_maps:
                full_branch_currents, full_node_currents, _bca, _np = out.get_c_map('')            
            _resistances, resistances_3col = self.write_resistances(fp.point_ids, resistances)
            result1 = resistances_3col
        elif self.options.scenario == 'advanced':
            self.options.write_max_cur_maps = False
            voltages, current_map, solver_failed = self.advanced_module(g_habitat, out, self.state.source_map, self.state.ground_map)
            if self.options.write_cur_maps:
                full_branch_currents, full_node_currents, _bca, _np = current_map
            result1 = voltages
            
        if solver_failed == True:
            Compute.logger.error('Solver failed')
            
        if self.options.write_cur_maps:
            full_branch_currents = Output._convert_graph_to_3_col(full_branch_currents, node_names)
            full_node_currents = Output._append_names_to_node_currents(full_node_currents, node_names)

            ind = np.lexsort((full_branch_currents[:, 1], full_branch_currents[:, 0]))
            full_branch_currents = full_branch_currents[ind]

            ind = np.lexsort((full_node_currents[:, 1], full_node_currents[:, 0]))
            full_node_currents = full_node_currents[ind]

            CSIO.write_currents(self.options.output_file, full_branch_currents, full_node_currents, '',self.options)
            
        return result1, solver_failed
Exemplo n.º 2
0
    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
Exemplo n.º 3
0
    def load_maps(self):
        """Loads all raster maps into self.state."""  
        Compute.logger.info('Reading maps')
        reclass_file = self.options.reclass_file if self.options.use_reclass_table else None
        CSIO.read_cell_map(self.options.habitat_file, self.options.habitat_map_is_resistances, reclass_file, self.state)
        
        if self.options.use_polygons:
            self.state.poly_map = CSIO.read_poly_map(self.options.polygon_file, False, 0, self.state, True, "Short-circuit region", 'int32')
        else:
            self.state.poly_map = []
 
        if self.options.use_mask==True:
            mask = CSIO.read_poly_map(self.options.mask_file, True, 0, self.state, True, "Mask", 'int32')
            mask = np.where(mask !=  0, 1, 0) 
            self.state.g_map = np.multiply(self.state.g_map, mask)
            del mask
            
            sum_gmap = (self.state.g_map).sum()
            if sum_gmap==0:
                raise RuntimeError('All entries in landscape map have been dropped after masking with the mask file.  There is nothing to solve.')             
        else:
            self.state.mask = []

        if self.options.scenario=='advanced':
            self.state.points_rc = []
            (self.state.source_map, self.state.ground_map) = CSIO.read_source_and_ground_maps(self.options.source_file, self.options.ground_file, self.state, self.options)
        else:        
            self.state.points_rc = CSIO.read_point_map(self.options.point_file, "Focal node", self.state)
            self.state.source_map = []
            self.state.ground_map = []

        if self.options.use_included_pairs==True:
            self.state.included_pairs = IncludeExcludePairs(self.options.included_pairs_file)
        
        self.state.point_strengths = None
        if self.options.use_variable_source_strengths==True:
            self.state.point_strengths = CSIO.read_point_strengths(self.options.variable_source_file) 
        
        Compute.logger.info('Processing maps')
        return