Пример #1
0
 def emit(self, record):
     try:
         msg = self.format(record)
         msg = msg.strip('\r')
         status_msg = None
         if ('\n' not in msg and 'elapsed' not in msg): # dispay simple info messages in the status bar
             plain_msg = logging._defaultFormatter.format(record)
             if (record.levelname == 'INFO'):
                 status_msg = (plain_msg, 1)
             elif (record.levelname == 'DEBUG'):
                 status_msg = (plain_msg, 2) 
         
         evt = wxLogEvent(message=msg, levelname=record.levelname, status_msg=status_msg)
         wx.PostEvent(self.dest, evt) # @UndefinedVariable
     
         (hours,mins,secs) = ComputeBase.elapsed_time(self.last_gui_yield_time)
         if (secs > 0) or (mins > 0) or (hours > 0):
             self.last_gui_yield_time = time.time()
             wx.SafeYield(None, True)  # @UndefinedVariable
             wx.GetApp().Yield(True)  # @UndefinedVariable
         
     except (KeyboardInterrupt, SystemExit):
         raise
     except:
         self.handleError(record)
Пример #2
0
    def on_menuFileRunBatch_select(self, event):
        wildcard = "Options Files (*.ini)|*.ini" 
        result = dialog.fileDialog(self, 'Select any number of Circuitscape Options Files within one directory', '', '', wildcard ) 
        if result.accepted==True:
            wx.BeginBusyCursor()  # @UndefinedVariable
            GUI.logger.debug('Running Circuitscape in batch mode')
            startTime = time.time()
            startTimeHMS = time.strftime('%H:%M:%S')
            self.statusBar.SetStatusText('Batch start ' + str(startTimeHMS), 0)
            job = 0
            numjobs = len(result.paths)
            for selection in result.paths:
                job += 1
                _configDir, configFile = os.path.split(selection)
                GUI.logger.debug('Processing ' + configFile)
                self.statusBar.SetStatusText('Batch start ' + str(startTimeHMS) + '. Running job ' + str(job) +'/' + str(numjobs), 0)
                
                try:
                    cs = Compute(selection, GUI.log_handler)
                except RuntimeError as error:
                    wx.EndBusyCursor()  # @UndefinedVariable
                    message = str(error)
                    dial = wx.MessageDialog(None, message, 'Error', wx.OK | wx.ICON_ERROR)  # @UndefinedVariable
                    dial.ShowModal()
                    return
                except MemoryError:
                    wx.EndBusyCursor()  # @UndefinedVariable
                    self.memory_error_feedback()
                    return
                except:
                    wx.EndBusyCursor()  # @UndefinedVariable
                    self.unknown_exception()
                    return

                try:
                    self.statusBar.SetStatusText('',1)
                    self.statusBar.SetStatusText('',2)
                    result, _solver_failed = cs.compute()
                    GUI.logger.debug('Finished processing ' + configFile)
                except RuntimeError as error:
                    message = str(error)
                    dial = wx.MessageDialog(None, message, 'Error', wx.OK | wx.ICON_ERROR)  # @UndefinedVariable
                    dial.ShowModal()
                except MemoryError:
                    self.memory_error_feedback()
                    return
                except:
                    self.unknown_exception()
                    
            GUI.logger.debug('Done with batch operations.')
            wx.EndBusyCursor()  # @UndefinedVariable
            
            self.components.calcButton.SetFocus()
            self.reset_status_bar()

            (hours,mins,secs) = ComputeBase.elapsed_time(startTime)
            if hours > 0:
                self.statusBar.SetStatusText('Batch job took ' + str(hours) +' hours ' + str(mins) + ' minutes to complete.',2)
            else:
                self.statusBar.SetStatusText('Batch job took ' + str(mins) +' minutes ' + str(secs) + ' seconds to complete.',2)
Пример #3
0
    def single_ground_all_pair_resistances(self, g_habitat, fp, cs, report_status):
        """Handles pairwise resistance/current/voltage calculations.  
        
        Called once when focal points are used, called multiple times when focal regions are used.
        """
        options = self.options
        last_write_time = time.time()
        numpoints = fp.num_points()
        parallelize = options.parallelize

        # TODO: revisit to see if restriction can be removed 
        if options.low_memory_mode==True or self.state.point_file_contains_polygons==True:
            parallelize = False
        
        if (self.state.point_file_contains_polygons == True) or (options.write_cur_maps == True) or (options.write_volt_maps == True) or (options.use_included_pairs==True) or (options.data_type=='network'):
            use_resistance_calc_shortcut = False
        else:     
            use_resistance_calc_shortcut = True # We use this when there are no focal regions.  It saves time when we are also not creating maps
            shortcut_resistances = -1 * np.ones((numpoints, numpoints), dtype='float64') 
           
        solver_failed_somewhere = [False]
        
        Compute.logger.info('Graph has ' + str(g_habitat.num_nodes) + ' nodes, ' + str(numpoints) + ' focal nodes and '+ str(g_habitat.num_components)+ ' components.')
        resistances = -1 * np.ones((numpoints, numpoints), dtype = 'float64')         #Inf creates trouble in python 2.5 on Windows. Use -1 instead.
        
#         if use_resistance_calc_shortcut==True:
#             num_points_to_solve = numpoints
#         else:
#             num_points_to_solve = numpoints*(numpoints-1)/2
        
        num_points_to_solve = 0
        max_parallel = 0
        for c in range(1, int(g_habitat.num_components+1)):
            if not fp.exists_points_in_component(c, g_habitat):
                continue
            
            num_parallel = 0
            for (pt1_idx, pt2_idx) in fp.point_pair_idxs_in_component(c, g_habitat):
                if pt2_idx == -1:
                    if (use_resistance_calc_shortcut==True):
                        max_parallel = max(max_parallel, num_parallel)
                        num_parallel = 0
                        break
                    else:
                        max_parallel = max(max_parallel, num_parallel)
                        num_parallel = 0
                        continue
                num_parallel += 1
                num_points_to_solve += 1
        
        Compute.logger.debug('max parallel possible = ' + str(max_parallel) + ', will parallelize = ' + str(parallelize))
        
        num_points_solved = 0
        for c in range(1, int(g_habitat.num_components+1)):
            if not fp.exists_points_in_component(c, g_habitat):
                continue
                        
            G_pruned, local_node_map = g_habitat.prune_nodes_for_component(c)
            G = ComputeBase.laplacian(G_pruned)
            del G_pruned 
            
            if use_resistance_calc_shortcut:
                voltmatrix = np.zeros((numpoints,numpoints), dtype='float64')     #For resistance calc shortcut

            G_dst_dst = local_dst = None
            for (pt1_idx, pt2_idx) in fp.point_pair_idxs_in_component(c, g_habitat):
                if pt2_idx == -1:
                    if parallelize:
                        self.state.worker_pool_wait()
                        
                    self.state.del_amg_hierarchy()
                    
                    if (local_dst != None) and (G_dst_dst != None):
                        G[local_dst, local_dst] = G_dst_dst
                        local_dst = G_dst_dst = None
                    
                    if (use_resistance_calc_shortcut==True):
                        Compute.get_shortcut_resistances(pt1_idx, voltmatrix, numpoints, resistances, shortcut_resistances)
                        break #No need to continue, we've got what we need to calculate resistances
                    else:
                        continue

                if parallelize:
                    self.state.worker_pool_create(options.max_parallel, True)

                if report_status==True:
                    num_points_solved += 1
                    if use_resistance_calc_shortcut==True:
                        Compute.logger.info('solving focal node ' + str(num_points_solved) + ' of '+ str(num_points_to_solve))
                    else:
                        Compute.logger.info('solving focal pair ' + str(num_points_solved) + ' of '+ str(num_points_to_solve))
                
                local_src = fp.get_graph_node_idx(pt2_idx, local_node_map)
                if None == local_dst:
                    local_dst = fp.get_graph_node_idx(pt1_idx, local_node_map)
                    G_dst_dst = G[local_dst, local_dst]
                    G[local_dst, local_dst] = 0

                if self.state.amg_hierarchy == None:
                    self.state.create_amg_hierarchy(G, self.options.solver)

                if use_resistance_calc_shortcut:
                    post_solve = self._post_single_ground_solve(G, fp, cs, resistances, numpoints, pt1_idx, pt2_idx, local_src, local_dst, local_node_map, solver_failed_somewhere, use_resistance_calc_shortcut, voltmatrix)
                else:
                    post_solve = self._post_single_ground_solve(G, fp, cs, resistances, numpoints, pt1_idx, pt2_idx, local_src, local_dst, local_node_map, solver_failed_somewhere)
                
                if parallelize:
                    self.state.worker_pool.apply_async(Compute.parallel_single_ground_solver, args=(G, local_src, local_dst, options.solver, self.state.amg_hierarchy), callback=post_solve)
                    #post_solve(self.state.worker_pool.apply(Compute.parallel_single_ground_solver, args=(G, local_src, local_dst, options.solver, self.state.amg_hierarchy)))
                else:
                    try:
                        voltages = Compute.single_ground_solver(G, local_src, local_dst, options.solver, self.state.amg_hierarchy)
                    except:
                        voltages = None
                    post_solve(voltages)

                if options.low_memory_mode==True or self.state.point_file_contains_polygons==True:
                    self.state.del_amg_hierarchy()
    
            (hours,mins,_secs) = ComputeBase.elapsed_time(last_write_time)
            if mins > 2 or hours > 0: 
                last_write_time = time.time()
                CSIO.write_resistances(options.output_file, resistances, incomplete=True)# Save incomplete resistances    

        self.state.del_amg_hierarchy()

        # Finally, resistance to self is 0.
        if use_resistance_calc_shortcut==True: 
            resistances = shortcut_resistances
        for i in range(0, numpoints):
            resistances[i, i] = 0

        return resistances, solver_failed_somewhere[0]
Пример #4
0
    def one_to_all_module(self, g_map, poly_map, points_rc):
        """Overhead module for one-to-all AND all-to-one modes with raster data."""  
        last_write_time = time.time()

        out = Output(self.options, self.state, False)
        if self.options.write_cur_maps:
            out.alloc_c_map('')
            if self.options.write_max_cur_maps:
                out.alloc_c_map('max')
        
        fp = FocalPoints(points_rc, self.state.included_pairs, False)
        fp = FocalPoints(fp.get_unique_coordinates(), self.state.included_pairs, False)
        point_ids = fp.point_ids
        points_rc_unique = fp.points_rc
        included_pairs = self.state.included_pairs
            
        resistance_vector = np.zeros((point_ids.size,2), float)
        solver_failed_somewhere = False
        
        if self.options.use_included_pairs==False: #Will do this each time later if using included pairs
            point_map = np.zeros((self.state.nrows, self.state.ncols), int)
            point_map[points_rc[:,1], points_rc[:,2]] = points_rc[:,0]
       
            #combine point map and poly map
            poly_map_temp = self.get_poly_map_temp(poly_map, point_map, point_ids)
            unique_point_map = np.zeros((self.state.nrows, self.state.ncols), int)
            unique_point_map[points_rc_unique[:,1], points_rc_unique[:,2]] = points_rc_unique[:,0]

            (strength_map, strengths_rc) = self.get_strength_map(points_rc_unique, self.state.point_strengths)            

            g_habitat = HabitatGraph(g_map=g_map, poly_map=poly_map_temp, connect_using_avg_resistances=self.options.connect_using_avg_resistances, connect_four_neighbors_only=self.options.connect_four_neighbors_only)
            Compute.logger.info('Graph has ' + str(g_habitat.num_nodes) + ' nodes and '+ str(g_habitat.num_components) + ' components.')
            component_with_points = g_habitat.unique_component_with_points(unique_point_map)
        else:
            if Compute.logger.isEnabledFor(logging.DEBUG):
                g_habitat = HabitatGraph(g_map=g_map, poly_map=poly_map, connect_using_avg_resistances=self.options.connect_using_avg_resistances, connect_four_neighbors_only=self.options.connect_four_neighbors_only)
                Compute.logger.info('Graph has ' + str(g_habitat.num_nodes) + ' nodes and '+ str(g_habitat.num_components) + ' components.')
            component_with_points = None

        for pt_idx in range(0, point_ids.size): # These are the 'src' nodes, pt_idx.e. the 'one' in all-to-one and one-to-all

            Compute.logger.info('solving focal node ' + str(pt_idx+1) + ' of ' + str(point_ids.size))

            if self.options.use_included_pairs==True: # Done above otherwise    
                #######################   
                points_rc_unique_temp = np.copy(points_rc_unique)
                point_map = np.zeros((self.state.nrows, self.state.ncols), int)
                point_map[points_rc[:,1], points_rc[:,2]] = points_rc[:,0]       

                #loop thru exclude[point,:], delete included pairs of focal point from point_map and points_rc_unique_temp
                for pair in range(0, point_ids.size):
                    if (pt_idx !=  pair) and not self.state.included_pairs.is_included_pair(point_ids[pt_idx], point_ids[pair]):
                        pt_id = point_ids[pair]
                        point_map = np.where(point_map==pt_id, 0, point_map)
                        points_rc_unique_temp[pair, 0] = 0 #point will not be burned in to unique_point_map

                poly_map_temp = self.get_poly_map_temp2(poly_map, point_map, points_rc_unique_temp, included_pairs, pt_idx)
                g_habitat = HabitatGraph(g_map=g_map, poly_map=poly_map_temp, connect_using_avg_resistances=self.options.connect_using_avg_resistances, connect_four_neighbors_only=self.options.connect_four_neighbors_only)

                unique_point_map = np.zeros((self.state.nrows, self.state.ncols),int)
                unique_point_map[points_rc_unique_temp[:,1], points_rc_unique_temp[:,2]] = points_rc_unique_temp[:,0]
                
                (strength_map, strengths_rc) = self.get_strength_map(points_rc_unique_temp, self.state.point_strengths)
                ###########################################
                
            src = point_ids[pt_idx]
            if unique_point_map.sum() == src: # src is the only point
                resistance_vector[pt_idx,0] = src
                resistance_vector[pt_idx,1] = -1            
            else: # there are points to connect with src point
                if self.options.scenario == 'one-to-all':
                    strength = strengths_rc[pt_idx,0] if self.options.use_variable_source_strengths else 1
                    source_map = np.where(unique_point_map == src, strength, 0)
                    ground_map =  np.where(unique_point_map == src, 0, unique_point_map)
                    ground_map =  np.where(ground_map, np.Inf, 0) 
                    self.options.remove_src_or_gnd = 'rmvgnd'
                else: # all-to-one
                    if self.options.use_variable_source_strengths==True:
                        source_map = np.where(unique_point_map == src, 0, strength_map)
                    else:
                        source_map = np.where(unique_point_map, 1, 0)
                        source_map = np.where(unique_point_map == src, 0, source_map)
                    ground_map =  np.where(unique_point_map == src, np.Inf, 0)                    
                    self.options.remove_src_or_gnd = 'rmvsrc'
                #FIXME: right now one-to-all *might* fail if there is just one node that is not grounded (I haven't encountered this problem lately BHM Nov 2009).
                
                resistance, current_map, solver_failed = self.advanced_module(g_habitat, out, source_map, ground_map, src, component_with_points)
                if not solver_failed:
                    if self.options.write_cur_maps:
                        out.accumulate_c_map_with_values('', current_map)
                        if self.options.write_max_cur_maps:    
                            out.store_max_c_map_values('max', current_map)
                else:
                    Compute.logger.warning('Solver failed for at least one focal node. Focal nodes with failed solves will be marked with value of -777 in output resistance list.')
    
                resistance_vector[pt_idx,0] = src
                resistance_vector[pt_idx,1] = resistance
                    
                if solver_failed==True:
                    solver_failed_somewhere = True

            (hours,mins,_secs) = ComputeBase.elapsed_time(last_write_time)
            if mins > 2 or hours > 0: 
                last_write_time = time.time()
                CSIO.write_resistances(self.options.output_file, resistance_vector, incomplete=True)
      
        if not solver_failed_somewhere:
            if self.options.write_cur_maps:
                out.write_c_map('', True)
                if self.options.write_max_cur_maps:
                    out.write_c_map('max', True)

        CSIO.write_resistances(self.options.output_file, resistance_vector)
       
        return resistance_vector, solver_failed_somewhere