Changeset 3323

Show
Ignore:
Timestamp:
05/11/08 19:04:57 (2 months ago)
Author:
borning
Message:

added initialize_from_xml_file method to XMLConfiguration; used it in reestimate method in EstimationRunner? to allow reloading the xml file

Location:
trunk
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/opus_core/configurations/xml_configuration.py

    r3316 r3323  
    2929        the Opus code is stored.  If is_parent is true, mark all of the nodes as inherited 
    3030        (either from this configuration or a grandparent).""" 
    31         self._filepath = None 
     31        self.full_filename = None 
    3232        if default_directory is not None: 
    3333            f = os.path.join(default_directory, filename) 
    3434            if os.path.exists(f): 
    35                 self._filepath = f 
    36         if self._filepath is None: 
     35                self.full_filename = f 
     36        if self.full_filename is None: 
    3737            opus_core_dir = __import__('opus_core').__path__[0] 
    3838            workspace_dir = os.path.split(opus_core_dir)[0] 
    39             self._filepath = os.path.join(workspace_dir, filename) 
    40         # if self._filepath doesn't exist, ElementTree will raise an IOError 
     39            self.full_filename = os.path.join(workspace_dir, filename) 
     40        # if self.full_filename doesn't exist, ElementTree will raise an IOError 
    4141        # self.tree is the xml tree (without any inherited nodes);  
    4242        # self.full_tree is the xml tree with all the inherited nodes as well 
     
    4646        self.full_tree = None 
    4747        self.parent_map = None 
    48         self.name = os.path.basename(self._filepath).split('.')[0] 
     48        self.name = os.path.basename(self.full_filename).split('.')[0] 
    4949        self.pp = pprint.PrettyPrinter(indent=4) 
    50         self._initialize(ElementTree(file=self._filepath), is_parent) 
     50        self.initialize_from_xml_file(is_parent) 
     51         
     52    def initialize_from_xml_file(self, is_parent=False): 
     53        """initialize (or re-initialize) the contents of this configuration from the xml file. 
     54        If is_parent is true, mark all of the nodes as inherited 
     55        (either from this configuration or a grandparent).""" 
     56        self._initialize(ElementTree(file=self.full_filename), is_parent) 
    5157         
    5258    def update(self, newconfig_str): 
    5359        """Update the contents of this configuration from the string newconfig_str  
    5460        (a string representing an xml configuration).  Ignore any inherited nodes in newconfig_str.""" 
    55         # Note that this doesn't change the name of this configuration, or the _filepath 
     61        # Note that this doesn't change the name of this configuration, or the full_filename 
    5662        str_io = StringIO.StringIO(newconfig_str) 
    5763        etree = ElementTree(file=str_io) 
     
    110116    def save(self): 
    111117        """save this configuration in a file with the same name as the original""" 
    112         self.save_as(self._filepath) 
     118        self.save_as(self.full_filename) 
    113119         
    114120    def save_as(self, name): 
     
    134140            raise ValueError, "malformed xml - expected to find a root element named 'opus_project'" 
    135141        parent_nodes = full_root.findall('general/parent') 
    136         default_dir = os.path.split(self._filepath)[0] 
     142        default_dir = os.path.split(self.full_filename)[0] 
    137143        for p in parent_nodes: 
    138144            x = XMLConfiguration(p.text, default_directory=default_dir, is_parent=True) 
     
    645651        est_file.close() 
    646652         
    647     def test_update_1(self): 
    648         # try update with a completely different project - make sure stuff gets replaced 
     653    def test_update_and_initialize(self): 
     654        # Try update with a completely different project - make sure stuff gets replaced. 
     655        # Then reinitialize from the file and check that it reverts. 
    649656        f = os.path.join(self.test_configs, 'estimation_grandchild.xml') 
    650657        config = XMLConfiguration(f) 
     
    658665            </opus_project>""" 
    659666        config.update(update_str) 
    660         run_config = config.get_run_configuration('test_scenario') 
    661         self.assertEqual(run_config, {'i': 42}) 
    662  
    663     def test_update_2(self): 
    664         # make sure nodes marked as inherited are filtered out when doing the update 
     667        section1 = config.get_section('scenario_manager/test_scenario') 
     668        self.assertEqual(section1, {'i': 42}) 
     669        # no model_manager section in the updated configuration 
     670        section2 = config.get_section('model_manager/estimation') 
     671        self.assertEqual(section2, None) 
     672        # now re-initialize from the original xml file, which has a model manager section and no scenario manager section 
     673        config.initialize_from_xml_file() 
     674        section3 = config.get_section('scenario_manager/test_scenario') 
     675        self.assertEqual(section3, None) 
     676        # no model_manager section in the updated configuration 
     677        section4 = config.get_section('model_manager/estimation') 
     678        self.assertEqual(section4['real_estate_price_model']['all_variables']['ln_cost'], 'ln_cost=ln(psrc.parcel.cost+100)') 
     679 
     680    def test_update_and_save(self): 
     681        # make sure nodes marked as inherited are filtered out when doing an update and a save 
    665682        f = os.path.join(self.test_configs, 'estimation_grandchild.xml') 
    666683        config = XMLConfiguration(f) 
  • trunk/urbansim/estimation/estimation_runner.py

    r3311 r3323  
    8787        Estimator.__init__(self, config, save_estimation_results=save_estimation_results) 
    8888 
    89     def reestimate(self, submodels=None): 
     89    def reestimate(self, submodels=None, reload_xml_file=True): 
    9090        """Launch a re-estimation without recomputing all variables. 'submodels' is a list or single number of submodels to re-estimate. 
    91         If it is None, all submodels are re-estimated. 
     91        If it is None, all submodels are re-estimated.  If self.xml_configuration is not None and reload_xml_file is True, re-read 
     92        the contents of the xml configuration file in case it has changed.  This will usually be what you want to do when  
     93        using reestimate from the command line -- but not from the GUI (since with the GUI, the XMLConfiguration might have been  
     94        edited but not yet saved back to the file). 
    9295        """ 
    9396        specification_dict=None 
    9497        if self.xml_configuration is not None: 
     98            if reload_xml_file: 
     99                self.xml_configuration.initialize_from_xml_file() 
    95100            specification_dict = self.xml_configuration.get_estimation_specification(self.estimated_model) 
    96101        Estimator.reestimate(self, self.specification_module, specification_dict=specification_dict, type=self.model_group, submodels=submodels)