<s> """<STR_LIT>""" <EOL> __author__ = '<STR_LIT>' <EOL> from brian2 import Network , second <EOL> from pypet . pypetlogging import HasLogger <EOL> class NetworkComponent ( HasLogger ) : <EOL> """<STR_LIT>""" <EOL> def add_parameters ( self , traj ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def pre_build ( self , traj , brian_list , network_dict ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def build ( self , traj , brian_list , network_dict ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def add_to_network ( self , traj , network , current_subrun , subrun_list , network_dict ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def remove_from_network ( self , traj , network , current_subrun , subrun_list , network_dict ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class NetworkAnalyser ( NetworkComponent ) : <EOL> """<STR_LIT>""" <EOL> def analyse ( self , traj , network , current_subrun , subrun_list , network_dict ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class NetworkRunner ( NetworkComponent ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , report = '<STR_LIT:text>' , report_period = None , <EOL> durations_group_name = '<STR_LIT>' , <EOL> pre_durations_group_name = '<STR_LIT>' ) : <EOL> if report_period is None : <EOL> report_period = <NUM_LIT:10> * second <EOL> self . _report = report <EOL> self . _report_period = report_period <EOL> self . _durations_group_name = durations_group_name <EOL> self . _pre_durations_group_name = pre_durations_group_name <EOL> self . _set_logger ( ) <EOL> def execute_network_pre_run ( self , traj , network , network_dict , component_list , analyser_list ) : <EOL> """<STR_LIT>""" <EOL> self . _execute_network_run ( traj , network , network_dict , component_list , analyser_list , <EOL> pre_run = True ) <EOL> def execute_network_run ( self , traj , network , network_dict , component_list , analyser_list ) : <EOL> """<STR_LIT>""" <EOL> self . _execute_network_run ( traj , network , network_dict , component_list , analyser_list , <EOL> pre_run = False ) <EOL> def _extract_subruns ( self , traj , pre_run = False ) : <EOL> """<STR_LIT>""" <EOL> if pre_run : <EOL> durations_list = traj . f_get_all ( self . _pre_durations_group_name ) <EOL> else : <EOL> durations_list = traj . f_get_all ( self . _durations_group_name ) <EOL> subruns = { } <EOL> orders = [ ] <EOL> for durations in durations_list : <EOL> for duration_param in durations . f_iter_leaves ( with_links = False ) : <EOL> if '<STR_LIT>' in duration_param . v_annotations : <EOL> order = duration_param . v_annotations . order <EOL> else : <EOL> raise RuntimeError ( '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> duration_param . v_full_name ) <EOL> if order in subruns : <EOL> raise RuntimeError ( '<STR_LIT>' <EOL> '<STR_LIT>' % order ) <EOL> else : <EOL> subruns [ order ] = duration_param <EOL> orders . append ( order ) <EOL> return [ subruns [ order ] for order in sorted ( orders ) ] <EOL> def _execute_network_run ( self , traj , network , network_dict , component_list , <EOL> analyser_list , pre_run = False ) : <EOL> """<STR_LIT>""" <EOL> subrun_list = self . _extract_subruns ( traj , pre_run = pre_run ) <EOL> subrun_number = <NUM_LIT:0> <EOL> while len ( subrun_list ) > <NUM_LIT:0> : <EOL> current_subrun = subrun_list . pop ( <NUM_LIT:0> ) <EOL> for component in component_list : <EOL> component . add_to_network ( traj , network , current_subrun , subrun_list , <EOL> network_dict ) <EOL> for analyser in analyser_list : <EOL> analyser . add_to_network ( traj , network , current_subrun , subrun_list , <EOL> network_dict ) <EOL> self . add_to_network ( traj , network , current_subrun , subrun_list , <EOL> network_dict ) <EOL> self . _logger . info ( '<STR_LIT>' % <EOL> ( current_subrun . v_name , subrun_number , str ( current_subrun . f_get ( ) ) ) ) <EOL> network . run ( duration = current_subrun . f_get ( ) , report = self . _report , <EOL> report_period = self . _report_period ) <EOL> for analyser in analyser_list : <EOL> analyser . analyse ( traj , network , current_subrun , subrun_list , <EOL> network_dict ) <EOL> self . remove_from_network ( traj , network , current_subrun , subrun_list , <EOL> network_dict ) <EOL> for analyser in analyser_list : <EOL> analyser . remove_from_network ( traj , network , current_subrun , subrun_list , <EOL> network_dict ) <EOL> for component in component_list : <EOL> component . remove_from_network ( traj , network , current_subrun , subrun_list , <EOL> network_dict ) <EOL> subrun_number += <NUM_LIT:1> <EOL> class NetworkManager ( HasLogger ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , network_runner , component_list , analyser_list = ( ) , network_constructor = None ) : <EOL> self . components = component_list <EOL> self . network_runner = network_runner <EOL> self . analysers = analyser_list <EOL> self . _network_dict = { } <EOL> self . _brian_list = [ ] <EOL> self . _set_logger ( ) <EOL> self . _pre_built = False <EOL> self . _pre_run = False <EOL> self . _network = None <EOL> if network_constructor is None : <EOL> self . _network_constructor = Network <EOL> else : <EOL> self . _network_constructor = network_constructor <EOL> def add_parameters ( self , traj ) : <EOL> """<STR_LIT>""" <EOL> self . _logger . info ( '<STR_LIT>' ) <EOL> for component in self . components : <EOL> component . add_parameters ( traj ) <EOL> if self . analysers : <EOL> self . _logger . info ( '<STR_LIT>' ) <EOL> for analyser in self . analysers : <EOL> analyser . add_parameters ( traj ) <EOL> self . _logger . info ( '<STR_LIT>' ) <EOL> self . network_runner . add_parameters ( traj ) <EOL> def pre_build ( self , traj ) : <EOL> """<STR_LIT>""" <EOL> self . _logger . info ( '<STR_LIT>' ) <EOL> for component in self . components : <EOL> component . pre_build ( traj , self . _brian_list , self . _network_dict ) <EOL> if self . analysers : <EOL> self . _logger . info ( '<STR_LIT>' ) <EOL> for analyser in self . analysers : <EOL> analyser . pre_build ( traj , self . _brian_list , self . _network_dict ) <EOL> self . _logger . info ( '<STR_LIT>' ) <EOL> self . network_runner . pre_build ( traj , self . _brian_list , self . _network_dict ) <EOL> self . _pre_built = True <EOL> def build ( self , traj ) : <EOL> """<STR_LIT>""" <EOL> self . _logger . info ( '<STR_LIT>' ) <EOL> for component in self . components : <EOL> component . build ( traj , self . _brian_list , self . _network_dict ) <EOL> if self . analysers : <EOL> self . _logger . info ( '<STR_LIT>' ) <EOL> for analyser in self . analysers : <EOL> analyser . build ( traj , self . _brian_list , self . _network_dict ) <EOL> self . _logger . info ( '<STR_LIT>' ) <EOL> self . network_runner . build ( traj , self . _brian_list , self . _network_dict ) <EOL> def pre_run_network ( self , traj ) : <EOL> """<STR_LIT>""" <EOL> self . pre_build ( traj ) <EOL> self . _logger . info ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . _network = self . _network_constructor ( * self . _brian_list ) <EOL> self . network_runner . execute_network_pre_run ( traj , self . _network , self . _network_dict , <EOL> self . components , self . analysers ) <EOL> self . _logger . info ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . _pre_run = True <EOL> if hasattr ( self . _network , '<STR_LIT:store>' ) : <EOL> self . _network . store ( '<STR_LIT>' ) <EOL> def run_network ( self , traj ) : <EOL> """<STR_LIT>""" <EOL> if self . _pre_built : <EOL> if self . _pre_run and hasattr ( self . _network , '<STR_LIT>' ) : <EOL> self . _network . restore ( '<STR_LIT>' ) <EOL> self . _network . store ( '<STR_LIT>' ) <EOL> self . _run_network ( traj ) <EOL> else : <EOL> self . _run_network ( traj ) <EOL> def _pretty_print_explored_parameters ( self , traj ) : <EOL> print_statement = '<STR_LIT>' + '<STR_LIT>' + '<STR_LIT>' + '<STR_LIT>' <EOL> explore_dict = traj . f_get_explored_parameters ( copy = False ) <EOL> for full_name in explore_dict : <EOL> parameter = explore_dict [ full_name ] <EOL> print_statement += '<STR_LIT>' % ( parameter . v_full_name , parameter . f_val_to_str ( ) ) <EOL> print_statement += '<STR_LIT>' <EOL> self . _logger . info ( print_statement ) <EOL> def _run_network ( self , traj ) : <EOL> """<STR_LIT>""" <EOL> self . build ( traj ) <EOL> self . _pretty_print_explored_parameters ( traj ) <EOL> if not self . _pre_run : <EOL> self . _network = self . _network_constructor ( * self . _brian_list ) <EOL> self . network_runner . execute_network_run ( traj , self . _network , self . _network_dict , <EOL> self . components , self . analysers ) <EOL> self . _logger . info ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) </s>
