Changeset 3293

Show
Ignore:
Timestamp:
05/03/08 12:18:06 (2 months ago)
Author:
hana
Message:

(hana) EstimationRunner? allows for XML configuration

Location:
trunk/urbansim
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/urbansim/estimation/estimation_runner.py

    r3249 r3293  
    2222class EstimationRunner(Estimator): 
    2323      
    24     def __init__(self, model, specification_module=None, xml_specification=None, model_group=None, 
     24    def __init__(self, model, specification_module=None, xml_configuration=None, model_group=None, 
    2525                  configuration={}, save_estimation_results=False): 
    2626        """ 
    2727        If 'specification_module' is given, it contains the specification defined as a dictionary in a module. 
    28         Alternatively, the specification can be passed in an xml format in the 'xml_specification' argument (It is a full path to the xml file).  
     28        Alternatively, the specification can be passed in an xml format in the 'xml_configuration' argument (It is a full path to the xml file).  
    2929        If both of those arguments are None, the specification is taken from the cache. 
    30         'configuration' is an Opus configuration. It can contain an entry 'config_changes_for_estimation' which is a dictionary 
     30        'configuration' is an Opus configuration.  
     31        It can contain an entry 'config_changes_for_estimation' which is a dictionary 
    3132        where keys are model names and values are controller changes for that model. 
     33        If 'configuration' is None, it is taken from 'xml_configuration'. 
    3234        If save_estimation_results is True, the estimation results are saved in the oputput configuration  
    3335        (if given in 'configuration') and in the cache. 
    3436        """ 
    3537        self.specification_module = specification_module 
    36         self.xml_specification = xml_specification 
     38        self.xml_configuration = xml_configuration 
    3739        self.model_group = model_group 
    3840        self.estimated_model = model 
    3941         
    40         config = Configuration(configuration) 
     42        xml_config = None 
     43        if self.xml_configuration is not None: 
     44            xml_config = XMLConfiguration(self.xml_configuration) 
     45             
     46        if configuration is None: 
     47            if xml_config is None: 
     48                raise StandardError, "Either dictionary based or XML based configuration must be given." 
     49            estimation_section = xml_config.get_section('model_manager/estimation') 
     50            config = estimation_section['estimation_config'] 
     51            xml_config._merge_controllers(config) 
     52        else: 
     53            config = Configuration(configuration) 
    4154        config_changes = config.get('config_changes_for_estimation', {}) 
    4255         
    4356        specification_dict=None 
    44         if xml_specification is not None: 
    45             specification_dict = XMLConfiguration(xml_specification).get_estimation_specification(model) 
     57        if xml_config is not None: 
     58            specification_dict = xml_config.get_estimation_specification(model) 
    4659             
    4760        if model_group is None: 
    4861            if model in config_changes.keys(): 
    4962                config.merge(config_changes[model]) 
     63            else: 
     64                config['models'] = [{model: ["estimate"]}] 
    5065            if specification_module is not None: 
    5166                config = update_controller_by_specification_from_module( 
     
    5873                    config.merge(config_changes[model][model_group]) 
    5974                else: 
    60                     config.merge(config_changes[model])        
     75                    config.merge(config_changes[model]) 
     76            else: 
     77                config['models'] = [{model: {"group_members": [{model_group: ["estimate"]}]}}] 
    6178            if (specification_module is not None) or (specification_dict is not None): 
    6279                if '%s_%s' % (model_group, model) in config["models_configuration"].keys(): 
     
    7996        """ 
    8097        specification_dict=None 
    81         if self.xml_specification is not None: 
    82             specification_dict = XMLConfiguration(self.xml_specification).get_estimation_specification(self.estimated_model) 
     98        if self.xml_configuration is not None: 
     99            specification_dict = XMLConfiguration(self.xml_configuration).get_estimation_specification(self.estimated_model) 
    83100        Estimator.reestimate(self, self.specification_module, specification_dict=specification_dict, type=self.model_group, submodels=submodels) 
    84101         
  • trunk/urbansim/tools/start_estimation.py

    r3268 r3293  
    2626        self.parser.add_option("-s", "--specification", dest="specification", default = None, 
    2727                               action="store", help="Specification module containing model specification in a dictionary format") 
    28         self.parser.add_option("-x", "--xml-specification", dest="xml_specification", default = None, 
    29                                action="store", help="Full path to an XML file containing the specification. One of the options -s and -x should be given, otherwise the specification is taken from cache.") 
     28        self.parser.add_option("-x", "--xml-configuration", dest="xml_configuration", default = None, 
     29                               action="store", help="Full path to an XML configuration file. One of the options -s and -x should be given, otherwise the specification is taken from cache.") 
    3030        self.parser.add_option("-c", "--configuration-path", dest="configuration_path", default=None,  
    31                                help="Opus path to Python module defining a configuration.") 
     31                               help="Opus path to Python module defining a configuration in dictionary format. One of the options -s and -x must be given.") 
    3232        self.parser.add_option("--save-results", dest="save_results", default=False, action="store_true",  
    3333                               help="Results will be saved in the output configuration (if given) and in the cache") 
     
    4343    if options.model_name is None: 
    4444        raise StandardError, "Model name (argument -m) must be given." 
    45     if options.configuration_path is None: 
    46         raise StandardError, "Configuration path (argument -c) must be given." 
    47     if (options.specification is None) and (options.xml_specification is None): 
     45    if (options.configuration_path is None) and (options.xml_configuration is None): 
     46        raise StandardError, "Configuration path (argument -c) or XML configuration (argument -x) must be given." 
     47    if (options.specification is None) and (options.xml_configuration is None): 
    4848        logger.log_warning("No specification given (arguments -s or -x). Specification taken from the cache.") 
    4949 
    50     config = get_config_from_opus_path(options.configuration_path) 
    51     estimator = EstimationRunner(model=options.model_name, specification_module=options.specification, xml_specification=options.xml_specification,  
     50    if options.configuration_path is None: 
     51        config = None 
     52    else: 
     53        config = get_config_from_opus_path(options.configuration_path) 
     54    estimator = EstimationRunner(model=options.model_name, specification_module=options.specification, xml_configuration=options.xml_configuration,  
    5255                                 model_group=options.model_group, 
    53                                  configuration=config, save_estimation_results=options.save_results) 
     56                                 configuration=config, 
     57                                 save_estimation_results=options.save_results) 
    5458    estimator.estimate()