Example #1
0
def from_config(filename):
    '''Opens a config file and reads the fields/subfields required for setting up 
    the analysis while ignoring the irrelavant ones. Returns a dictionary of the 
    collected information.
    
    :param filename: Name of file to read.
    :type filename: str
    '''
    config = configparser.RawConfigParser()
    config.optionxform = lambda option: option
    config_data = {}
    with open(filename, 'r') as file:
        config.read_file(file)

    n = 1
    for section in config.sections():
        if not is_analysis_settings(config, section):
            continue

        init = config[section]['init']
        params = config[section]['parameter_values']
        tspan = config[section]['tspan']
        solver_args = config[section].get('solver_args')
        fixed_parameters = config[section].get('fixed_parameters')
        parameter_bounds = config[section].get('parameter_bounds')
        units = config[section].get('units')

        init = eval_init_string(init)
        tspan = eval_tspan_string(tspan)
        params = eval_params_string(params)
        solver_args = string_to_dict(solver_args) if solver_args else {}
        fixed_parameters = string_to_list_string(
            fixed_parameters) if fixed_parameters else []
        parameter_bounds = string_to_dict_array(
            parameter_bounds) if parameter_bounds else {}
        units = string_to_dict(units) if units else {}

        config_data[n] = {
            'system_type': section,
            'init': init,
            'params': params,
            'tspan': tspan,
            'solver_args': solver_args,
            'fixed_parameters': fixed_parameters,
            'parameter_bounds': parameter_bounds,
            'units': units
        }

        n += 1

    return config_data
Example #2
0
def from_config(filename):
    '''Opens a config file and reads the fields/subfields required for setting up 
    the analysis while ignoring the irrelavant ones. Returns a dictionary of the 
    collected information.
    
    :param filename: Name of file to read.
    :type filename: str
    '''
    config             = configparser.RawConfigParser()
    config.optionxform = lambda option: option
    config_data               = {}
    with open(filename, 'r') as file:
        config.read_file(file)
    
    n = 1

    for section in config.sections():
        if not is_analysis_settings(config, section):
            continue
        
        parameter_values = config[section].get('parameter_values')
        measured_states  = config[section].get('measured_states')
        input_conditions = config[section].get('input_conditions')
        decomposition    = config[section].get('decomposition')
        init             = config[section].get('init')
        units            = config[section].get('units')
        
        measured_states  = string_to_list_string(measured_states)  if measured_states  else []
        input_conditions = string_to_dict(input_conditions)        if input_conditions else {}
        parameter_values = string_to_dict(parameter_values)        if parameter_values else {} 
        units            = string_to_dict(units)                   if units            else {}
        init             = eval_init_string(init)                  if init             else {}
        
        if decomposition:
            temp = ''.join([s for s in decomposition if s in ['[', ']']])
            if '[[' in temp:
                decomposition = decomposition.strip()
                decomposition = decomposition[1: len(decomposition)-1]
        else:
            decomposition = []
            
        decomposition    = [s.strip() for s in split_at_top_level(decomposition)]
        decomposition    = [[v.strip() for v in s.replace(']', '').replace('[', '').split(',')] for s in decomposition]
        
        for key in init:
            try: 
                next(iter(init[key]))
                if len(init[key]) != 1:
                    raise Exception('Error in init. Only one value allowed.')
            except:
                pass
            
        config_data[n] = {'system_type'      : section, 
                          'parameter_values' : parameter_values,
                          'init'             : init,
                          'input_conditions' : input_conditions,
                          'measured_states'  : measured_states, 
                          'decomposition'    : decomposition,
                          'units'            : units
                          }
        n += 1
        
    return config_data
Example #3
0
def from_config(filename, sampler='sa'):
    '''Opens a config file and reads the fields/subfields required for setting up 
    the analysis while ignoring the irrelavant ones. Returns a dictionary of the 
    collected information.
    
    :param filename: Name of file to read.
    :type filename: str
    :param sampler: "sa" for simulated annealing; "bh" for scipy's basin hopping;
        "da" for scipy's dual annealing; "de" for scipy's differential evolution; 
        defaults to "sa"
    :type sampler: str, optional
    '''
    config             = configparser.RawConfigParser()
    config.optionxform = lambda option: option
    config_data               = {}
    with open(filename, 'r') as file:
        config.read_file(file)
    
    n = 1
    for section in config.sections():
        if not is_analysis_settings(config, section):
            continue
        
        solver_args      = config[section].get('solver_args')
        fixed_parameters = config[section].get('fixed_parameters')
        parameter_bounds = config[section].get('parameter_bounds')
        priors           = config[section].get('priors')
        step_size        = config[section].get('step_size')
        init             = config[section].get('init')
        tspan            = config[section].get('tspan')
        sa_args          = config[section].get('sa_args')
        de_args          = config[section].get('de_args')
        bh_args          = config[section].get('bh_args')
        da_args          = config[section].get('da_args')
        guess            = config[section].get('guess')
        units            = config[section].get('units')
        
        tspan            = eval_tspan_string(tspan)
        init             = eval_init_string(init)                  if init             else {}
        solver_args      = string_to_dict(solver_args)             if solver_args      else {}
        fixed_parameters = string_to_list_string(fixed_parameters) if fixed_parameters else []
        parameter_bounds = string_to_dict_array(parameter_bounds)  if parameter_bounds else {}
        priors           = string_to_dict(priors)                  if priors           else {}
        step_size        = string_to_dict(step_size)               if step_size        else {}
        sa_args          = string_to_dict(sa_args)                 if sa_args          else {}
        de_args          = string_to_dict(de_args)                 if de_args          else {}
        bh_args          = string_to_dict(bh_args)                 if bh_args          else {}
        da_args          = string_to_dict(da_args)                 if da_args          else {}
        guess            = eval_params_string(guess)               if guess            else {}
        units            = string_to_dict(units)                   if units            else {}
        
        if sampler == 'sa':
            config_data[n] = {'system_type' : section,     'guess'  : guess,  'priors'           : priors, 
                              'solver_args' : solver_args, 'parameter_bounds' : parameter_bounds,
                              'step_size'   : step_size,   'fixed_parameters' : fixed_parameters,   
                              'units'       : units,       'init'   : init,   'tspan' : tspan, 'sa_args':sa_args
                              }
        
        elif sampler == 'de':
            config_data[n] = {'system_type' : section,     'guess'  : guess,  'priors' : priors, 
                              'solver_args' : solver_args, 'parameter_bounds' : parameter_bounds,  
                              'de_args'     : de_args,     'fixed_parameters' : fixed_parameters,
                              'units'       : units,       'init'   : init,   'tspan' : tspan        
                              }
            
        elif sampler == 'bh':
            config_data[n] = {'system_type' : section,     'guess'  : guess,  'priors' : priors, 
                              'solver_args' : solver_args, 'parameter_bounds' : parameter_bounds,  
                              'step_size'   : step_size,   'fixed_parameters' : fixed_parameters,   
                              'bh_args'     : bh_args,     'units'  : units,  'init'   : init,   'tspan' : tspan  
                              }
        elif sampler == 'da':
            config_data[n] = {'system_type' : section,     'guess'  : guess,  'priors' : priors, 
                              'solver_args' : solver_args, 'parameter_bounds' : parameter_bounds,   
                              'da_args'     : da_args,     'fixed_parameters' : fixed_parameters, 
                              'units'       : units,       'init'   : init,   'tspan' : tspan 
                              }
        n += 1
 
    return config_data