Changeset 3323
- Timestamp:
- 05/11/08 19:04:57 (2 months ago)
- Location:
- trunk
- Files:
-
- 2 modified
-
opus_core/configurations/xml_configuration.py (modified) (6 diffs)
-
urbansim/estimation/estimation_runner.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/opus_core/configurations/xml_configuration.py
r3316 r3323 29 29 the Opus code is stored. If is_parent is true, mark all of the nodes as inherited 30 30 (either from this configuration or a grandparent).""" 31 self. _filepath= None31 self.full_filename = None 32 32 if default_directory is not None: 33 33 f = os.path.join(default_directory, filename) 34 34 if os.path.exists(f): 35 self. _filepath= f36 if self. _filepathis None:35 self.full_filename = f 36 if self.full_filename is None: 37 37 opus_core_dir = __import__('opus_core').__path__[0] 38 38 workspace_dir = os.path.split(opus_core_dir)[0] 39 self. _filepath= os.path.join(workspace_dir, filename)40 # if self. _filepathdoesn't exist, ElementTree will raise an IOError39 self.full_filename = os.path.join(workspace_dir, filename) 40 # if self.full_filename doesn't exist, ElementTree will raise an IOError 41 41 # self.tree is the xml tree (without any inherited nodes); 42 42 # self.full_tree is the xml tree with all the inherited nodes as well … … 46 46 self.full_tree = None 47 47 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] 49 49 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) 51 57 52 58 def update(self, newconfig_str): 53 59 """Update the contents of this configuration from the string newconfig_str 54 60 (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 _filepath61 # Note that this doesn't change the name of this configuration, or the full_filename 56 62 str_io = StringIO.StringIO(newconfig_str) 57 63 etree = ElementTree(file=str_io) … … 110 116 def save(self): 111 117 """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) 113 119 114 120 def save_as(self, name): … … 134 140 raise ValueError, "malformed xml - expected to find a root element named 'opus_project'" 135 141 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] 137 143 for p in parent_nodes: 138 144 x = XMLConfiguration(p.text, default_directory=default_dir, is_parent=True) … … 645 651 est_file.close() 646 652 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. 649 656 f = os.path.join(self.test_configs, 'estimation_grandchild.xml') 650 657 config = XMLConfiguration(f) … … 658 665 </opus_project>""" 659 666 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 665 682 f = os.path.join(self.test_configs, 'estimation_grandchild.xml') 666 683 config = XMLConfiguration(f) -
trunk/urbansim/estimation/estimation_runner.py
r3311 r3323 87 87 Estimator.__init__(self, config, save_estimation_results=save_estimation_results) 88 88 89 def reestimate(self, submodels=None ):89 def reestimate(self, submodels=None, reload_xml_file=True): 90 90 """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). 92 95 """ 93 96 specification_dict=None 94 97 if self.xml_configuration is not None: 98 if reload_xml_file: 99 self.xml_configuration.initialize_from_xml_file() 95 100 specification_dict = self.xml_configuration.get_estimation_specification(self.estimated_model) 96 101 Estimator.reestimate(self, self.specification_module, specification_dict=specification_dict, type=self.model_group, submodels=submodels)
