def test_save_inputs_as_dat(self): import switch_mod.solve from pyomo.environ import DataPortal from testfixtures import compare (model, instance) = switch_mod.solve.load("test_dat") # To do: create a temporary file name for dat_path and delete it # when the test is complete. dat_path = "test_dat/complete_inputs.dat" utilities.save_inputs_as_dat(model, instance, save_path=dat_path) reloaded_data = DataPortal(model=model) reloaded_data.load(filename=dat_path) compare(reloaded_data.data(), instance.DataPortal.data())
def test_save_inputs_as_dat(self): (model, instance) = switch_model.solve.main( args=["--inputs-dir", os.path.join('examples', '3zone_toy', 'inputs')], return_model=True, return_instance=True ) temp_dir = tempfile.mkdtemp(prefix="switch_test_") try: dat_path = os.path.join(temp_dir, "complete_inputs.dat") utilities.save_inputs_as_dat(model, instance, save_path=dat_path) reloaded_data = DataPortal(model=model) reloaded_data.load(filename=dat_path) compare(reloaded_data.data(), instance.DataPortal.data()) finally: shutil.rmtree(temp_dir)
def test_save_inputs_as_dat(self): (model, instance) = switch_model.solve.main(args=[ "--inputs-dir", os.path.join('examples', '3zone_toy', 'inputs') ], return_model=True, return_instance=True) temp_dir = tempfile.mkdtemp(prefix="switch_test_") try: dat_path = os.path.join(temp_dir, "complete_inputs.dat") utilities.save_inputs_as_dat(model, instance, save_path=dat_path) reloaded_data = DataPortal(model=model) reloaded_data.load(filename=dat_path) compare(reloaded_data.data(), instance.DataPortal.data()) finally: shutil.rmtree(temp_dir)
def test_save_inputs_as_dat(self): import switch_mod.solve from pyomo.environ import DataPortal from testfixtures import compare (model, instance) = switch_mod.solve.main(args=["--inputs-dir", "test_dat"], return_model=True, return_instance=True) temp_dir = tempfile.mkdtemp(prefix="switch_test_") try: dat_path = os.path.join(temp_dir, "complete_inputs.dat") utilities.save_inputs_as_dat(model, instance, save_path=dat_path) reloaded_data = DataPortal(model=model) reloaded_data.load(filename=dat_path) compare(reloaded_data.data(), instance.DataPortal.data()) finally: shutil.rmtree(temp_dir)
def convert_dat_to_csv(old_path, new_path): # define a dummy "model" where every "parameter" reports a dimension of 0. # otherwise Pyomo assumes they have dim=1 and looks for index values. class DummyModel(): def __getattr__(self, pname): return DummyParam() class DummyParam(): def dim(self): return 0 try: data = DataPortal(model=DummyModel()) data.load(filename=old_path) # this happens to be in a pandas-friendly format df = pandas.DataFrame(data.data()) df.to_csv(new_path, sep=',', na_rep='.', index=False) os.remove(old_path) except Exception as e: print('\nERROR converting {} to {}:\n{}'.format( old_path, new_path, e.message)) raise