def test_postprocess_nopp(self): """Try a basic extraction via a post processing on a simulation""" sim = Simulation('LinkedCapacities') process = Process(mothers=['c1', 'c2'], sub_vars={'T': 'T'}) result_pp = sim.postprocess(process) self.assertTrue(result_pp.has_key('c1_T'))
def test_init_with_process_pp(self): """Tests if vardic is updated correctly after postprocessing""" J2kWh = 1e-6 / 3.6 vars_to_integrate = {'Q': J2kWh, 'Time': 1} process = Process(mothers=['c1', 'c2'], sub_vars={ 'T': 'T', 'Q': 'heatPort.Q_flow' }, sub_pars={'cap': 'C'}, pp=[ 'T_degC = T + 273.15', 'T_max = np.amax( T_degC )', 'Thigh = np.nonzero( T_degC > 640)[0]', 'Qsel = Q [ Thigh ]' ], integrate=vars_to_integrate) self.simdex = Simdex(folder=getcwd(), process=process) filenames = self.simdex.get_filenames('path') filenames.sort() self.assertEqual(self.sims, filenames) for v in ['c1_Thigh', 'c2_Q', 'c2_Q_Int']: self.assertTrue(self.simdex.vardic.has_key(v)) self.simdex.h5.close()
def test_postprocess_integration(self): """Postprocessing with integration on standard variables and mothers""" J2kWh = 1e-6 / 3.6 vars_to_integrate = {'Q': J2kWh, 'Time': 1} sim = Simulation('LinkedCapacities') process = Process(mothers=['c1', 'c2'], sub_vars={ 'T': 'T', 'Q': 'heatPort.Q_flow' }, sub_pars={'cap': 'C'}, pp=[ 'T_degC = T + 273.15', 'T_max = np.amax( T_degC )', 'Thigh = np.nonzero( T_degC > 640)[0]', 'Qsel = Q [ Thigh ]' ], integrate=vars_to_integrate) result_pp = sim.postprocess(process) self.assertAlmostEqual(result_pp['c1_T_max'], 673.15, 2) self.assertEqual(len(result_pp['c1_Qsel']), len(result_pp['c1_Thigh'])) self.assertAlmostEqual(result_pp['c1_Q_Int'], -result_pp['c2_Q_Int'], 10) self.assertIsNotNone(result_pp['Time_Int'])
def test_scan_additional_folder(self): """Test adding files from another folder to existing simdex""" J2kWh = 1e-6 / 3.6 vars_to_integrate = {'Q': J2kWh, 'Time': 1} process = Process(mothers=['c1', 'c2'], sub_vars={ 'T': 'T', 'Q': 'heatPort.Q_flow' }, sub_pars={'cap': 'C'}, pp=[ 'T_degC = T + 273.15', 'T_max = np.amax( T_degC )', 'Thigh = np.nonzero( T_degC > 640)[0]', 'Qsel = Q [ Thigh ]' ], integrate=vars_to_integrate) self.simdex = Simdex(folder=getcwd(), process=process) # now add another folder (with exactly the same files) folder = path.join(self.cwd, 'SubfolderWithCrappyFiles') self.simdex.scan(folder=folder, process=process) self.assertEqual(len(self.simdex.simulations), 16)
def test_init_integration(self): """init process with variables to integrate""" J2kWh = 1e-6 / 3.6 vars_to_integrate = {'Qflow': J2kWh} p = Process(mothers=self.mothers, parameters=self.parameters, sub_pars=self.sub_pars, variables=self.variables, sub_vars=self.sub_vars, pp=self.pp, integrate=vars_to_integrate) self.assertEqual(p.parameters, { 'cap1': 'c1.C', 'res': 'r.R', 'c1_cap': 'c1.C', 'c2_cap': 'c2.C' }) self.assertEqual( p.variables, { 'Time': 'Time', 'c1_Qflow': 'c1.heatPort.Q_flow', 'c2_Qflow': 'c2.heatPort.Q_flow' }) self.assertEqual(p.pp, [ 'Qflow_Int = np.trapz( Qflow , Time ,axis=0)*' + str(J2kWh) + ' if Qflow .shape[0]== Time .shape[0] else np.array([0.0])', 'Qflow10 = 10 * Qflow' ])
def test_init_aggregation(self): """init process with variables to aggregate""" vars_to_aggregate = {'Qflow': 'sum', 'Qflow': 'mean'} p = Process(mothers=self.mothers, parameters=self.parameters, sub_pars=self.sub_pars, variables=self.variables, sub_vars=self.sub_vars, pp=self.pp, aggregate=vars_to_aggregate) self.assertEqual(p.parameters, { 'cap1': 'c1.C', 'res': 'r.R', 'c1_cap': 'c1.C', 'c2_cap': 'c2.C' }) self.assertEqual( p.variables, { 'Time': 'Time', 'c1_Qflow': 'c1.heatPort.Q_flow', 'c2_Qflow': 'c2.heatPort.Q_flow' }) self.assertEqual(p.pp, ['Qflow10 = 10 * Qflow'])
def test_init2(self): """init Process with only variables should work""" process = Process(mothers=['c1', 'c2'], sub_vars={'T': 'T'}) self.assertEqual(process.variables, { 'Time': 'Time', 'c1_T': 'c1.T', 'c2_T': 'c2.T' })
def test_postprocess_nomothers(self): """Try a basic postprocessing without mothers on a simulation""" sim = Simulation('LinkedCapacities') process = Process(mothers=['c1', 'c2'], sub_vars={'T':'T'}, pp = ['timehours = Time / 3600']) result_pp = sim.postprocess(process) self.assertTrue(np.all(result_pp['timehours']==result_pp['Time']/3600))
def test_postprocess_recurring(self): """Some advanced postprocessing using previous results with mothers""" sim = Simulation('LinkedCapacities') process = Process(mothers=['c1', 'c2'], sub_vars={'T':'T'}, sub_pars={'cap':'C'}, pp = ['T_degC = T + 273.15', 'T_degC2 = T_degC ']) result_pp = sim.postprocess(process) self.assertTrue(np.all(result_pp['c1_T_degC2']==result_pp['c1_T_degC']))
def test_postprocess_mothers(self): """Try a basic postprocessing with mothers on a simulation""" sim = Simulation('LinkedCapacities') process = Process(mothers=['c1', 'c2'], sub_vars={'T':'T'}, sub_pars={'cap':'C'}, pp = ['T_degC = T + 273.15']) result_pp = sim.postprocess(process) self.assertTrue(np.all(result_pp['c1_T_degC'] == result_pp['c1_T']+273.15)) self.assertEqual(result_pp['c1_cap'], 600.0)
def test_init3(self): """init process with variables and parameters""" p = Process(mothers=self.mothers, parameters=self.parameters, sub_pars=self.sub_pars, variables=self.variables, sub_vars=self.sub_vars, pp=self.pp) self.assertEqual(p.parameters, {'cap1':'c1.C', 'res':'r.R', 'c1_cap':'c1.C', 'c2_cap':'c2.C'}) self.assertEqual(p.variables, {'Time':'Time', 'c1_Qflow':'c1.heatPort.Q_flow', 'c2_Qflow':'c2.heatPort.Q_flow'}) print p
def test_postprocess_advanced(self): """Some more advanced postprocessing with mothers""" sim = Simulation('LinkedCapacities') process = Process(mothers=['c1', 'c2'], sub_vars={'T':'T', 'Q':'heatPort.Q_flow'}, sub_pars={'cap':'C'}, pp = ['T_degC = T + 273.15', 'T_max = np.amax( T_degC )', 'Thigh = np.nonzero( T_degC > 640)[0]', 'Qsel = Q [ Thigh ]']) result_pp = sim.postprocess(process) self.assertAlmostEqual(result_pp['c1_T_max'], 673.15, 2) self.assertEqual(len(result_pp['c1_Qsel']), len(result_pp['c1_Thigh']))
def test_init_4(self): """Process initialisation should not change original variables""" variables = {} parameters={'cap1':'c1.C', 'res':'r.R'} sub_pars={'cap':'C'} process = Process(mothers=['c1', 'c2'], variables = variables, parameters=parameters, sub_pars=sub_pars, sub_vars={'T':'T'}) self.assertEqual(process.variables, {'Time':'Time', 'c1_T':'c1.T', 'c2_T':'c2.T'}) self.assertEqual(variables, {}) self.assertEqual(parameters, {'cap1':'c1.C', 'res':'r.R'})
def test_init_with_process(self): """ Tests if a Simdex object is created correctly when a process is passed """ process=Process(variables={'T2':'c2.T', 'dt2':'c[2].der(T)'}) self.simdex=Simdex(folder = getcwd(), process=process) filenames = self.simdex.get_filenames('path') filenames.sort() self.assertEqual(self.sims, filenames) self.assertEqual(self.simdex.vardic, {'Time':'Time','T2':'c2.T', 'dt2':'c[2].der(T)'}) self.simdex.h5.close()
def test_scatterplot(self): """Simdex.scatterplot() should return [fig, lines, leg]""" self.simdex.h5.close() vardic = {'T1': 'c1.T', 'T2': 'c2.T'} pardic = {'parc': 'c1.C'} process = Process(parameters=pardic, variables=vardic) self.simdex = Simdex(folder=getcwd(), process=process) self.simdex_filtered = self.simdex.filter({'c1.C': ''}) [fig, lines, leg] = self.simdex_filtered.scatterplot('T1', 'T2') self.assertTrue(isinstance(fig, matplotlib.figure.Figure)) self.assertEqual(7, len(lines)) for line in lines: self.assertTrue(isinstance(line, matplotlib.lines.Line2D)) self.assertTrue(isinstance(leg, matplotlib.legend.Legend)) self.simdex.h5.close()
def test_get_shortnames(self): """Simdex.get() should return correctly for shortnames too""" self.simdex.h5.close() vardic = {'T2': 'c2.T'} pardic = {'parc': 'c1.C'} process = Process(parameters=pardic, variables=vardic) self.simdex = Simdex(folder=getcwd(), process=process) result = self.simdex.get('T2') expectedsids = self.simdex.get_SID('linkedcapacities') self.assertEqual(sorted(result.keys()), sorted(expectedsids)) sim = Simulation('LinkedCapacities_B.mat') sid = self.simdex.get_SID('LinkedCapacities_B')[0] self.assertTrue(np.all(result[sid] == sim.get_value('c2.T'))) c1_C = self.simdex.get('parc').values() c1_C.sort() exp_result_sorted = [None, 600., 600., 800., 800., 800., 800., 1000.] self.assertTrue(exp_result_sorted == c1_C) self.simdex.h5.close()
def test_postprocess_aggregation(self): """Postprocessing with aggregation""" vars_to_aggregate = {'Q': ['sum', 'mean', 'each'], 'T': 'mean'} J2kWh = 1e-6 / 3.6 vars_to_integrate = {'Q': J2kWh, 'Time': 1} sim = Simulation('LinkedCapacities') process = Process(mothers=['c1', 'c2'], sub_vars={ 'T': 'T', 'Q': 'heatPort.Q_flow' }, sub_pars={'cap': 'C'}, pp=[ 'T_degC = T + 273.15', 'T_max = np.amax( T_degC )', 'Thigh = np.nonzero( T_degC > 640)[0]', 'Qsel = Q [ Thigh ]' ], integrate=vars_to_integrate, aggregate=vars_to_aggregate) result_pp = sim.postprocess(process) self.assertAlmostEqual(result_pp['c1_T_max'], 673.15, 2) self.assertEqual(len(result_pp['c1_Qsel']), len(result_pp['c1_Thigh'])) self.assertAlmostEqual(result_pp['c1_Q_Int'], -result_pp['c2_Q_Int'], 10) self.assertIsNotNone(result_pp['Time_Int']) self.assertAlmostEqual(result_pp['Q_Total'][3], result_pp['c1_Q'][3] + result_pp['c2_Q'][3]) self.assertAlmostEqual(result_pp['Q_Mean'][3], (result_pp['c1_Q'][3] + result_pp['c2_Q'][3]) / 2.) self.assertEqual( np.sum(result_pp['Q_Each'], axis=0)[3], result_pp['Q_Total'][3]) self.assertAlmostEqual( result_pp['T_Mean'][16], (result_pp['c1_T'][16] + result_pp['c2_T'][16]) / 2.)
""" import simman reload(simman) from simman import Simulation, Simdex, Process mothers = ['c1', 'c2'] parameters = {'cap1': 'c1.C', 'res': 'r.R'} sub_pars = {'cap': 'C'} variables = {} sub_vars = {'Qflow': 'heatPort.Q_flow'} pp = ['Qflow_kW = 0.001 * Qflow', 'timehours = Time / 3600'] process = Process(mothers=mothers, parameters=parameters, sub_pars=sub_pars, variables=variables, sub_vars=sub_vars, pp=pp) class DummyProcess(object): def invoke_upper(self): print locals() class MyClass(object): def __init__(self, process): self.process = process def my_method(self, a, b): print 'return the sum of ', a, ' and ', b
s1.parametermap s1.parametervalues s1.variables s1.variablemap s1.h5_path # Now look at the h5 file (open it with ViTables) # Demonstrate natural naming of pytables s1.openh5() s1.h5.root.SID0001.r_dot_heatPort_a_dot_Q_flow s1.h5.root.SID0001.r_dot_heatPort_a_dot_Q_flow.read() s1.h5.close() # Next step: using a process p1 = Process(variables={'T2': 'c2.T', 'dt2': 'c[2].der(T)'}) s1 = Simdex(os.getcwd(), process=p1) mothers = ['c1', 'c2'] parameters = {'cap1': 'c1.C', 'res': 'r.R'} sub_pars = {'cap': 'C'} variables = {} sub_vars = {'Qflow': 'heatPort.Q_flow'} pp = ['Qflow10 = 10 * Qflow'] J2kWh = 1e-6 / 3.6 vars_to_integrate = {'Qflow': J2kWh} p = Process(mothers=mothers, parameters=parameters, sub_pars=sub_pars, variables=variables,
def test_init1(self): """init process without attributes""" p = Process() self.assertEqual(p.mothers, [])