Beispiel #1
0
    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()
Beispiel #2
0
    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)
Beispiel #3
0
    def setUp(self):

        # The next N times, that a question is being asked interactively,
        # we will automatically answer y.
        #
        # It doesn't matter if N exceeds the numer of questions
        # NOTE: this may affect interactive debugging tools
        N = 1000
        f = StringIO("y\n" * N)
        sys.stdin = f

        self.cwd = getcwd()
        # sims contains the simualations we expect to be in self.simdex in the
        # current work directory
        self.sims = []
        self.filenames = ['Array.mat', 'LinkedCapacities.mat', \
                   'LinkedCapacities_A.mat',  'LinkedCapacities_B.mat', \
                   'LinkedCapacities_C.mat', 'LinkedCapacities_D.mat', \
                   'LinkedCapacities_E.mat', 'LinkedCapacities_F.mat']
        self.filenames.sort()
        for fn in self.filenames:
            self.sims.append(path.join(self.cwd, fn))

        self.simdex = Simdex()
        self.simdex.scan()
Beispiel #4
0
    def test_init_subfolder_with_crappy_files(self):
        """ Test initiation from a folder including wrong .mat files"""

        folder = path.join(self.cwd, 'SubfolderWithCrappyFiles')
        self.simdex = Simdex(folder)
        filenames = self.simdex.get_filenames()
        filenames.sort()
        self.assertEqual(self.filenames, filenames)
        self.simdex.h5.close()
 def setUp(self):
   
     # The next N times, that a question is being asked interactively,
     # we will automatically answer y.
     # 
     # It doesn't matter if N exceeds the numer of questions
     # NOTE: this may affect interactive debugging tools
     N = 1000
     f = StringIO("y\n" * N)
     sys.stdin = f
     
     self.cwd = getcwd()
     # sims contains the simualations we expect to be in self.simdex in the 
     # current work directory        
     self.sims = []
     self.filenames = ['Array.mat', 'LinkedCapacities.mat', \
                'LinkedCapacities_A.mat',  'LinkedCapacities_B.mat', \
                'LinkedCapacities_C.mat', 'LinkedCapacities_D.mat', \
                'LinkedCapacities_E.mat', 'LinkedCapacities_F.mat']
     self.filenames.sort()
     for fn in self.filenames:
         self.sims.append(path.join(self.cwd, fn))
         
     self.simdex = Simdex()
     self.simdex.scan()    
Beispiel #6
0
    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()        
Beispiel #7
0
    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_init_subfolder_with_crappy_files(self):
    """ Test initiation from a folder including wrong .mat files"""
    
    folder = path.join(self.cwd, 'SubfolderWithCrappyFiles')
    self.simdex = Simdex(folder)
    filenames = self.simdex.get_filenames()
    filenames.sort()
    self.assertEqual(self.filenames, filenames)
    self.simdex.h5.close()
Beispiel #9
0
    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_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_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_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_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()
# in big simulation files, it's not always easy to find the right parameter 
# or variable
sim.exist('q_flow')

time = sim.get_value('Time')
Q = sim.get_value(u'r.heatPort_a.Q_flow')
Q_sum = np.trapz(Q, time)
print "The total energy that flowed through the resistance is %.1f J" %Q_sum

for p,v in zip(sim.parameters, sim.parametervalues):
    print ''.join([p, ' = ', str(v)])


# there are different ways to create a simdex

s1 = Simdex() # an empty simdex object
s1.scan() # scan current folder and add all found simulation files to the simdex

s2 = Simdex('SubfolderWithCrappyFiles')
    # create a simdex directly from a folder
    
print s1
print s2

# now, let's look at some attributes
s1.get_filenames() # method, so add ()
s1.get_filenames('path')
s1.parameters # attribute, so no ()
s1.parametermap
s1.parametervalues
s1.variables
Beispiel #16
0
                  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
        return a + b


dummyprocess = DummyProcess()

myclass = MyClass(dummyprocess)

c = myclass.my_method(3, 5.67)
print c

sim = Simulation('LinkedCapacities.mat', verbose=True)
processed = sim.postprocess(process)

simdex = Simdex(process=process, verbose=False)
simdex.scan()
class SimdexTest(unittest.TestCase):
    """
    Class for testing the class simman.Simdex
    """

    def setUp(self):
      
        # The next N times, that a question is being asked interactively,
        # we will automatically answer y.
        # 
        # It doesn't matter if N exceeds the numer of questions
        # NOTE: this may affect interactive debugging tools
        N = 1000
        f = StringIO("y\n" * N)
        sys.stdin = f
        
        self.cwd = getcwd()
        # sims contains the simualations we expect to be in self.simdex in the 
        # current work directory        
        self.sims = []
        self.filenames = ['Array.mat', 'LinkedCapacities.mat', \
                   'LinkedCapacities_A.mat',  'LinkedCapacities_B.mat', \
                   'LinkedCapacities_C.mat', 'LinkedCapacities_D.mat', \
                   'LinkedCapacities_E.mat', 'LinkedCapacities_F.mat']
        self.filenames.sort()
        for fn in self.filenames:
            self.sims.append(path.join(self.cwd, fn))
            
        self.simdex = Simdex()
        self.simdex.scan()    
                       
    def tearDown(self):
        """ Restore back interactivity of keyboard and close h5 file """
        sys.stdin = sys.__stdin__
        try:
            self.simdex.h5.close()
        except:
            pass
        if path.exists(path.join(getcwd(), 'simdex.h5')):
            remove(path.join(getcwd(), 'simdex.h5'))
        
    
    def test_init(self):
        """
        Tests if a Simdex object is created correctly based on the following
        files in the current work directory, containing:
            - 'Array.mat',
            - 'LinkedCapacities.mat',
            - 'LinkedCapacities_A.mat',
            - 'LinkedCapacities_B.mat',
            - 'LinkedCapacities_C.mat',
            - 'LinkedCapacities_D.mat',
            - 'LinkedCapacities_E.mat'
            - 'LinkedCapacities_F.mat']
      
        """
        filenames = self.simdex.get_filenames('path')
        filenames.sort()
        self.assertEqual(self.sims, filenames)
        self.simdex.h5.close()

    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_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_init_subfolder_with_crappy_files(self):
       """ Test initiation from a folder including wrong .mat files"""
       
       folder = path.join(self.cwd, 'SubfolderWithCrappyFiles')
       self.simdex = Simdex(folder)
       filenames = self.simdex.get_filenames()
       filenames.sort()
       self.assertEqual(self.filenames, filenames)
       self.simdex.h5.close()
       
    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_exist(self):
        """
        Test if :
        1) running Simdex.exist() gives a match when it should
           (different cases are tested)
        2) running Simdex.exist() gives a zero list for non-present regex
        
        """
        
        self.assertEqual(u'c1.C', 
                         self.simdex.exist('C1.c')[0][0], 
                         'Simdex.exist() does NOT return a present name')
        self.assertEqual(1, len(self.simdex.exist('c1')[0]), 
                         "sim.exist('c1') should return 1 parameter")
        self.assertEqual(1, len(self.simdex.exist('c1', 'par')), 
                         "sim.exist('c1', 'par') should return 1 parameter")                 
        self.assertEqual(4, len(self.simdex.exist('c1')[1]), 
                         "sim.exist('c1') should return 4 variables")
        self.assertEqual(4, len(self.simdex.exist('c1', 'var')), 
                         "sim.exist('c1', 'var') should return 4 variables")                 
        self.assertEqual(u'Time', self.simdex.exist('time')[1][0], 
                         'Simulation.exist() should ignore case' )
        self.assertEqual([[],[]], self.simdex.exist('this does not exist'))
        self.simdex.h5.close()
        
    def test_cleanup_1(self):
        """Check if parameters are removed if not present"""
                
        
        n = 1
        # set 1 value in the parametermap to 0
        par = self.simdex.parameters[n]
        npars = len(self.simdex.parameters)
        nvars = len(self.simdex.variables)
        self.simdex.parametermap[n, 0] = 0
        self.simdex.cleanup()
        self.assertEqual(npars-1, len(self.simdex.parameters))
        self.assertEqual(npars-1, self.simdex.parametermap.shape[0])
        self.assertEqual(npars-1, self.simdex.parametervalues.shape[0])
        self.assertEqual(nvars, len(self.simdex.variables))
        
        self.simdex.variablemap[44, 0] = 0
        self.simdex.cleanup()
        self.assertEqual(nvars-1, len(self.simdex.variables))
        self.assertEqual(nvars-1, self.simdex.variablemap.shape[0])
        self.assertEqual(npars-1, self.simdex.parametervalues.shape[0])
        self.simdex.h5.close()


    def test_filter_selection(self):
        """Simdex.filter_selection() should return only selected SIDs"""
        
        selection = ['SID0001', 'SID0003', 'SID0004']
        c1_C = self.simdex.get('c1.C')
        self.simdex_filtered = self.simdex.filter_selection(selection)
        exp_results = ['LinkedCapacities.mat', \
                   'LinkedCapacities_B.mat', 'LinkedCapacities_C.mat']
        exp_results.sort()
        self.simdex_filtered_fn = self.simdex_filtered.get_filenames()
        self.simdex_filtered_fn.sort()
        self.assertEqual(exp_results, self.simdex_filtered_fn, 
                         'get_identical on LinkedCapacities_C.mat should \
                         return the files mentioned in exp_results')
        c1_C_f = self.simdex_filtered.get('c1.C')                 
        for sid in c1_C_f:
            self.assertEqual(c1_C[sid], c1_C_f[sid])
                 
    def test_filter_remove(self):
        """Simdex.filter_remove() should remove only selected SIDs"""
        
        selection = ['SID0001', 'SID0003', 'SID0004', 'SID0006']
        self.simdex_filtered = self.simdex.filter_remove(selection)
        exp_results = ['Array.mat', 
                   'LinkedCapacities_A.mat',  'LinkedCapacities_D.mat', \
                   'LinkedCapacities_F.mat']
        exp_results.sort()
        self.simdex_filtered_fn = self.simdex_filtered.get_filenames()
        self.simdex_filtered_fn.sort()
        self.assertEqual(exp_results, self.simdex_filtered_fn, 
                         'get_identical on LinkedCapacities_C.mat should \
                         return the files mentioned in exp_results')
        
    
    def test_filter_remove_2(self):
        """Check if colums are removed from parametermap if needed"""
                
        # we test the shape and the values
        shape = self.simdex.parametermap.shape
        c1_C = self.simdex.get('c1.C')
        filtered = self.simdex.filter_remove(['SID0002'])
        shape_filtered = filtered.parametermap.shape
        self.assertEqual(shape[1]-1, shape_filtered[1])
        c1_C_f = filtered.get('c1.C')
        for sid in c1_C_f:
            self.assertEqual(c1_C[sid], c1_C_f[sid])


        self.simdex.h5.close()  
        filtered.h5.close()


    def test_filter_similar_single_result(self):
        """ Simdex.get_identical() for Array.mat should return only Array.mat"""
        

        SID_array = self.simdex.get_SID('array')
        self.simdex_array = self.simdex.filter_similar(SID_array[0])
        self.assertEqual(['Array.mat'], self.simdex_array.get_filenames(), 
                         'get_identical on array should only return Array.mat')
        
        # check if the self.simdex and sim objects have the same parameters
        sim_array = Simulation('Array.mat')
        sim_array.separate()
        sim_array_pars = sim_array.parameters
        sim_array_pars.sort()
        self.simdex_array_pars = self.simdex_array.parameters
        self.simdex_array_pars.sort()
        self.assertEqual(self.simdex_array_pars, sim_array_pars,
                         'the self.simdex and sim objects should have the same \
                         parameters')
        
        # check if the self.simdex and sim objects have the same variables
        sim_array_vars = sim_array.variables
        sim_array_vars.sort()
        self.simdex_array_vars = self.simdex_array.variables
        self.simdex_array_vars.sort()
        self.assertEqual(self.simdex_array_vars, sim_array_vars,
                         'the self.simdex and sim objects should have the same \
                         variables')
        self.simdex.h5.close()

    def test_filter_similar_multiple_results(self):
        """ 
        Simdex.get_identical() for LinkedCapacities_C.mat should return 
        all LinkedCapacities* except LinkedCapacities_F 
        
        Attention: currently, the String parameter that is defined in the 
        Modelica files is NOT present in the .mat files.  If this would be 
        changed (desirable) than the result of this test should also 
        exclude LinkedCapacities.mat
                
        """
        

        SID_lc = self.simdex.get_SID('_C')
        
        self.simdex_lc = self.simdex.filter_similar(SID_lc[0])
        exp_results = ['LinkedCapacities.mat', \
                   'LinkedCapacities_A.mat',  'LinkedCapacities_B.mat', \
                   'LinkedCapacities_C.mat', 'LinkedCapacities_D.mat', \
                   'LinkedCapacities_E.mat']
        exp_results.sort()
        self.simdex_lc_fn = self.simdex_lc.get_filenames()
        self.simdex_lc_fn.sort()
        self.assertEqual(exp_results, self.simdex_lc_fn, 
                         'get_identical on LinkedCapacities_C.mat should \
                         return the files mentioned in exp_results')
        
        # check if the self.simdex and sim objects have the same parameters
        sim_lc = Simulation('LinkedCapacities_A.mat')
        sim_lc.separate()
        sim_lc_pars = sim_lc.parameters
        sim_lc_pars.sort()
        self.simdex_lc_pars = self.simdex_lc.parameters
        self.simdex_lc_pars.sort()
        self.assertEqual(self.simdex_lc_pars, sim_lc_pars,
                         'the self.simdex and sim objects should have the same \
                         parameters')
        
        # check if the self.simdex and sim objects have the same variables
        sim_lc_vars = sim_lc.variables
        sim_lc_vars.sort()
        self.simdex_lc_vars = self.simdex_lc.variables
        self.simdex_lc_vars.sort()
        self.assertEqual(self.simdex_lc_vars, sim_lc_vars,
                         'the self.simdex and sim objects should have the same \
                         variables')
                         
        self.simdex.h5.close()
    
  
    
    def test_filter_intvalues(self):
        """Simdex.filter() with integer values should work well"""
        

        filt_dic = {'c1.C': 800}
        self.simdex_filtered = self.simdex.filter(filt_dic)
        self.simdex_filtered_fn = self.simdex_filtered.get_filenames()
        self.simdex_filtered_fn.sort()
        exp_result = ['LinkedCapacities_A.mat',  'LinkedCapacities_D.mat', \
                   'LinkedCapacities_E.mat', 'LinkedCapacities_F.mat']
        exp_result.sort()
        self.assertEqual(exp_result, self.simdex_filtered_fn,
                         'filtering self.simdex with c1.C = 800 should return\
                         exp_result')
        self.assertEqual(filt_dic, self.simdex_filtered.filterset,
                         'After filtering, filterset has to be updated')
        self.simdex.h5.close()
        
       
    def test_filter_nonexist_values(self):
        """Simdex.filter() with non-existent values should return False"""
        pass
        
        filt_dic = {'c1.C': 850} 
        filt_dic2 = {'c1.C': 800, 'r.R': 800}
        self.assertRaises(ValueError, self.simdex.filter, filt_dic)
        self.assertRaises(ValueError, self.simdex.filter, filt_dic2)
        self.simdex.h5.close()
        
    def test_filter_wildcard(self):
        """Simdex.filter() with '' values should return any sim having \
        that parameter"""
        
        filt_dic = {'c1.C': ''}
        self.simdex_filtered = self.simdex.filter(filt_dic)
        self.simdex_filtered_fn = self.simdex_filtered.get_filenames()
        self.simdex_filtered_fn.sort()
        exp_result = ['LinkedCapacities.mat', \
                   'LinkedCapacities_A.mat',  'LinkedCapacities_B.mat', \
                   'LinkedCapacities_C.mat', 'LinkedCapacities_D.mat', \
                   'LinkedCapacities_E.mat', 'LinkedCapacities_F.mat']
        exp_result.sort()
        self.assertEqual(exp_result, self.simdex_filtered_fn,
                         'filtering self.simdex with c1.C = '' should return\
                         all LinkedCapacities*')
        self.assertEqual(filt_dic, self.simdex_filtered.filterset,
                         'After filtering, filterset has to be updated')
        self.simdex.h5.close()
    
    def test_filter_twice(self):
        """ filtering twice should give correct end results and filterset"""

        filt_dic = {'c1.C': 800}
        filt_dic2 = {'r.R': 3}
        self.simdex_filtered1 = self.simdex.filter(filt_dic)
        self.simdex_filtered = self.simdex_filtered1.filter(filt_dic2)
        self.simdex_filtered_fn = self.simdex_filtered.get_filenames()
        self.simdex_filtered_fn.sort()
        exp_result = ['LinkedCapacities_A.mat',  'LinkedCapacities_F.mat']
        exp_result.sort()
        self.assertEqual(exp_result, self.simdex_filtered_fn,
                         "filtering self.simdex with c1.C = 800 then with r.R = 3 \
                         should return LinkedCapacities_A and _F")
        self.assertEqual({'c1.C': 800, 'r.R': 3}, self.simdex_filtered.filterset,
                         'After filtering, filterset has to be updated')
        self.simdex.h5.close()
        
    def test_filter_unchanged_original(self):
        """self.simdex.filter() should not change self.simdex (bug and issue on github)"""

        filt_dic = {'c1.C': 800}
        self.simdex_filtered = self.simdex.filter(filt_dic)
        
        self.assertEqual({}, self.simdex.filterset,
                        'Simdex.filter() should NOT change filterset of Simdex')
        self.simdex.h5.close()

    def test_filter_floatvalues(self):
        """Simdex.filter() with float values should work well"""
        

        filt_dic = {'r.R': 8.15}
        self.simdex_filtered = self.simdex.filter(filt_dic)
        self.simdex_filtered_fn = self.simdex_filtered.get_filenames()
        self.simdex_filtered_fn.sort()
        exp_result = ['LinkedCapacities_E.mat']
        exp_result.sort()
        self.assertEqual(exp_result, self.simdex_filtered_fn,
                         'filtering self.simdex with r.R = 8.15 should return\
                         exp_result')
        self.assertEqual(filt_dic, self.simdex_filtered.filterset,
                         'After filtering, filterset has to be updated')
        self.simdex.h5.close()
    
    def test_plot(self):
        """Simdex.plot() should return [fig, lines, leg]"""
        
        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)        
        
        self.simdex_filtered = self.simdex.filter({'c1.C': ''})
        [fig, lines, leg] = self.simdex_filtered.plot('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_save_and_load(self):
        """Saving and loading a self.simdex object should return exactly the same object"""
        
        self.simdex.save('Test_save.dat')
        loaded = load_simdex('Test_save.dat')
        for attr in self.simdex.__dict__:
            exec("s = self.simdex." + attr)
            exec("l = loaded." + attr)
                   
            if isinstance(s, np.ndarray):
                self.assertTrue((l == s).all())
            else: 
                self.assertEqual(s, l)

    
    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_fullnames(self):
        """Simdex.get() should return correctly for variables and parameters"""
        
#        result = self.simdex.get('c2.T')
#        expectedsids = self.simdex.get_SID('linkedcapacities')
#        self.assertEqual(sorted(result.keys()), sorted(expectedsids))
#        sim = Simulation('LinkedCapacities_B.mat')
#        sid = self.simdex.get_SID('_B')[0]
#        self.assertTrue(np.all(result[sid]==sim.get_value('c2.T')))

        # now a parameter
        c1_C = self.simdex.get('c1.C').values()
        c1_C.sort()
        print '\n'*10, 'c1_C = ', c1_C
        exp_result_sorted = [None,   600.,   600.,   800.,  
                                      800.,   800.,   800., 1000.]
        
        self.assertEqual(exp_result_sorted , c1_C)
        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()        
Beispiel #18
0
class SimdexTest(unittest.TestCase):
    """
    Class for testing the class simman.Simdex
    """
    def setUp(self):

        # The next N times, that a question is being asked interactively,
        # we will automatically answer y.
        #
        # It doesn't matter if N exceeds the numer of questions
        # NOTE: this may affect interactive debugging tools
        N = 1000
        f = StringIO("y\n" * N)
        sys.stdin = f

        self.cwd = getcwd()
        # sims contains the simualations we expect to be in self.simdex in the
        # current work directory
        self.sims = []
        self.filenames = ['Array.mat', 'LinkedCapacities.mat', \
                   'LinkedCapacities_A.mat',  'LinkedCapacities_B.mat', \
                   'LinkedCapacities_C.mat', 'LinkedCapacities_D.mat', \
                   'LinkedCapacities_E.mat', 'LinkedCapacities_F.mat']
        self.filenames.sort()
        for fn in self.filenames:
            self.sims.append(path.join(self.cwd, fn))

        self.simdex = Simdex()
        self.simdex.scan()

    def tearDown(self):
        """ Restore back interactivity of keyboard and close h5 file """
        sys.stdin = sys.__stdin__
        try:
            self.simdex.h5.close()
        except:
            pass
        if path.exists(path.join(getcwd(), 'simdex.h5')):
            remove(path.join(getcwd(), 'simdex.h5'))

    def test_init(self):
        """
        Tests if a Simdex object is created correctly based on the following
        files in the current work directory, containing:
            - 'Array.mat',
            - 'LinkedCapacities.mat',
            - 'LinkedCapacities_A.mat',
            - 'LinkedCapacities_B.mat',
            - 'LinkedCapacities_C.mat',
            - 'LinkedCapacities_D.mat',
            - 'LinkedCapacities_E.mat'
            - 'LinkedCapacities_F.mat']
      
        """
        filenames = self.simdex.get_filenames('path')
        filenames.sort()
        self.assertEqual(self.sims, filenames)
        self.simdex.h5.close()

    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_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_init_subfolder_with_crappy_files(self):
        """ Test initiation from a folder including wrong .mat files"""

        folder = path.join(self.cwd, 'SubfolderWithCrappyFiles')
        self.simdex = Simdex(folder)
        filenames = self.simdex.get_filenames()
        filenames.sort()
        self.assertEqual(self.filenames, filenames)
        self.simdex.h5.close()

    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_exist(self):
        """
        Test if :
        1) running Simdex.exist() gives a match when it should
           (different cases are tested)
        2) running Simdex.exist() gives a zero list for non-present regex
        
        """

        self.assertEqual(u'c1.C',
                         self.simdex.exist('C1.c')[0][0],
                         'Simdex.exist() does NOT return a present name')
        self.assertEqual(1, len(self.simdex.exist('c1')[0]),
                         "sim.exist('c1') should return 1 parameter")
        self.assertEqual(1, len(self.simdex.exist('c1', 'par')),
                         "sim.exist('c1', 'par') should return 1 parameter")
        self.assertEqual(4, len(self.simdex.exist('c1')[1]),
                         "sim.exist('c1') should return 4 variables")
        self.assertEqual(4, len(self.simdex.exist('c1', 'var')),
                         "sim.exist('c1', 'var') should return 4 variables")
        self.assertEqual(u'Time',
                         self.simdex.exist('time')[1][0],
                         'Simulation.exist() should ignore case')
        self.assertEqual([[], []], self.simdex.exist('this does not exist'))
        self.simdex.h5.close()

    def test_cleanup_1(self):
        """Check if parameters are removed if not present"""

        n = 1
        # set 1 value in the parametermap to 0
        par = self.simdex.parameters[n]
        npars = len(self.simdex.parameters)
        nvars = len(self.simdex.variables)
        self.simdex.parametermap[n, 0] = 0
        self.simdex.cleanup()
        self.assertEqual(npars - 1, len(self.simdex.parameters))
        self.assertEqual(npars - 1, self.simdex.parametermap.shape[0])
        self.assertEqual(npars - 1, self.simdex.parametervalues.shape[0])
        self.assertEqual(nvars, len(self.simdex.variables))

        self.simdex.variablemap[44, 0] = 0
        self.simdex.cleanup()
        self.assertEqual(nvars - 1, len(self.simdex.variables))
        self.assertEqual(nvars - 1, self.simdex.variablemap.shape[0])
        self.assertEqual(npars - 1, self.simdex.parametervalues.shape[0])
        self.simdex.h5.close()

    def test_filter_selection(self):
        """Simdex.filter_selection() should return only selected SIDs"""

        selection = ['SID0001', 'SID0003', 'SID0004']
        c1_C = self.simdex.get('c1.C')
        self.simdex_filtered = self.simdex.filter_selection(selection)
        exp_results = ['LinkedCapacities.mat', \
                   'LinkedCapacities_B.mat', 'LinkedCapacities_C.mat']
        exp_results.sort()
        self.simdex_filtered_fn = self.simdex_filtered.get_filenames()
        self.simdex_filtered_fn.sort()
        self.assertEqual(
            exp_results, self.simdex_filtered_fn,
            'get_identical on LinkedCapacities_C.mat should \
                         return the files mentioned in exp_results')
        c1_C_f = self.simdex_filtered.get('c1.C')
        for sid in c1_C_f:
            self.assertEqual(c1_C[sid], c1_C_f[sid])

    def test_filter_remove(self):
        """Simdex.filter_remove() should remove only selected SIDs"""

        selection = ['SID0001', 'SID0003', 'SID0004', 'SID0006']
        self.simdex_filtered = self.simdex.filter_remove(selection)
        exp_results = ['Array.mat',
                   'LinkedCapacities_A.mat',  'LinkedCapacities_D.mat', \
                   'LinkedCapacities_F.mat']
        exp_results.sort()
        self.simdex_filtered_fn = self.simdex_filtered.get_filenames()
        self.simdex_filtered_fn.sort()
        self.assertEqual(
            exp_results, self.simdex_filtered_fn,
            'get_identical on LinkedCapacities_C.mat should \
                         return the files mentioned in exp_results')

    def test_filter_remove_2(self):
        """Check if colums are removed from parametermap if needed"""

        # we test the shape and the values
        shape = self.simdex.parametermap.shape
        c1_C = self.simdex.get('c1.C')
        filtered = self.simdex.filter_remove(['SID0002'])
        shape_filtered = filtered.parametermap.shape
        self.assertEqual(shape[1] - 1, shape_filtered[1])
        c1_C_f = filtered.get('c1.C')
        for sid in c1_C_f:
            self.assertEqual(c1_C[sid], c1_C_f[sid])

        self.simdex.h5.close()
        filtered.h5.close()

    def test_filter_similar_single_result(self):
        """ Simdex.get_identical() for Array.mat should return only Array.mat"""

        SID_array = self.simdex.get_SID('array')
        self.simdex_array = self.simdex.filter_similar(SID_array[0])
        self.assertEqual(['Array.mat'], self.simdex_array.get_filenames(),
                         'get_identical on array should only return Array.mat')

        # check if the self.simdex and sim objects have the same parameters
        sim_array = Simulation('Array.mat')
        sim_array.separate()
        sim_array_pars = sim_array.parameters
        sim_array_pars.sort()
        self.simdex_array_pars = self.simdex_array.parameters
        self.simdex_array_pars.sort()
        self.assertEqual(
            self.simdex_array_pars, sim_array_pars,
            'the self.simdex and sim objects should have the same \
                         parameters')

        # check if the self.simdex and sim objects have the same variables
        sim_array_vars = sim_array.variables
        sim_array_vars.sort()
        self.simdex_array_vars = self.simdex_array.variables
        self.simdex_array_vars.sort()
        self.assertEqual(
            self.simdex_array_vars, sim_array_vars,
            'the self.simdex and sim objects should have the same \
                         variables')
        self.simdex.h5.close()

    def test_filter_similar_multiple_results(self):
        """ 
        Simdex.get_identical() for LinkedCapacities_C.mat should return 
        all LinkedCapacities* except LinkedCapacities_F 
        
        Attention: currently, the String parameter that is defined in the 
        Modelica files is NOT present in the .mat files.  If this would be 
        changed (desirable) than the result of this test should also 
        exclude LinkedCapacities.mat
                
        """

        SID_lc = self.simdex.get_SID('_C')

        self.simdex_lc = self.simdex.filter_similar(SID_lc[0])
        exp_results = ['LinkedCapacities.mat', \
                   'LinkedCapacities_A.mat',  'LinkedCapacities_B.mat', \
                   'LinkedCapacities_C.mat', 'LinkedCapacities_D.mat', \
                   'LinkedCapacities_E.mat']
        exp_results.sort()
        self.simdex_lc_fn = self.simdex_lc.get_filenames()
        self.simdex_lc_fn.sort()
        self.assertEqual(
            exp_results, self.simdex_lc_fn,
            'get_identical on LinkedCapacities_C.mat should \
                         return the files mentioned in exp_results')

        # check if the self.simdex and sim objects have the same parameters
        sim_lc = Simulation('LinkedCapacities_A.mat')
        sim_lc.separate()
        sim_lc_pars = sim_lc.parameters
        sim_lc_pars.sort()
        self.simdex_lc_pars = self.simdex_lc.parameters
        self.simdex_lc_pars.sort()
        self.assertEqual(
            self.simdex_lc_pars, sim_lc_pars,
            'the self.simdex and sim objects should have the same \
                         parameters')

        # check if the self.simdex and sim objects have the same variables
        sim_lc_vars = sim_lc.variables
        sim_lc_vars.sort()
        self.simdex_lc_vars = self.simdex_lc.variables
        self.simdex_lc_vars.sort()
        self.assertEqual(
            self.simdex_lc_vars, sim_lc_vars,
            'the self.simdex and sim objects should have the same \
                         variables')

        self.simdex.h5.close()

    def test_filter_intvalues(self):
        """Simdex.filter() with integer values should work well"""

        filt_dic = {'c1.C': 800}
        self.simdex_filtered = self.simdex.filter(filt_dic)
        self.simdex_filtered_fn = self.simdex_filtered.get_filenames()
        self.simdex_filtered_fn.sort()
        exp_result = ['LinkedCapacities_A.mat',  'LinkedCapacities_D.mat', \
                   'LinkedCapacities_E.mat', 'LinkedCapacities_F.mat']
        exp_result.sort()
        self.assertEqual(
            exp_result, self.simdex_filtered_fn,
            'filtering self.simdex with c1.C = 800 should return\
                         exp_result')
        self.assertEqual(filt_dic, self.simdex_filtered.filterset,
                         'After filtering, filterset has to be updated')
        self.simdex.h5.close()

    def test_filter_nonexist_values(self):
        """Simdex.filter() with non-existent values should return False"""
        pass

        filt_dic = {'c1.C': 850}
        filt_dic2 = {'c1.C': 800, 'r.R': 800}
        self.assertRaises(ValueError, self.simdex.filter, filt_dic)
        self.assertRaises(ValueError, self.simdex.filter, filt_dic2)
        self.simdex.h5.close()

    def test_filter_wildcard(self):
        """Simdex.filter() with '' values should return any sim having \
        that parameter"""

        filt_dic = {'c1.C': ''}
        self.simdex_filtered = self.simdex.filter(filt_dic)
        self.simdex_filtered_fn = self.simdex_filtered.get_filenames()
        self.simdex_filtered_fn.sort()
        exp_result = ['LinkedCapacities.mat', \
                   'LinkedCapacities_A.mat',  'LinkedCapacities_B.mat', \
                   'LinkedCapacities_C.mat', 'LinkedCapacities_D.mat', \
                   'LinkedCapacities_E.mat', 'LinkedCapacities_F.mat']
        exp_result.sort()
        self.assertEqual(
            exp_result, self.simdex_filtered_fn,
            'filtering self.simdex with c1.C = '
            ' should return\
                         all LinkedCapacities*')
        self.assertEqual(filt_dic, self.simdex_filtered.filterset,
                         'After filtering, filterset has to be updated')
        self.simdex.h5.close()

    def test_filter_twice(self):
        """ filtering twice should give correct end results and filterset"""

        filt_dic = {'c1.C': 800}
        filt_dic2 = {'r.R': 3}
        self.simdex_filtered1 = self.simdex.filter(filt_dic)
        self.simdex_filtered = self.simdex_filtered1.filter(filt_dic2)
        self.simdex_filtered_fn = self.simdex_filtered.get_filenames()
        self.simdex_filtered_fn.sort()
        exp_result = ['LinkedCapacities_A.mat', 'LinkedCapacities_F.mat']
        exp_result.sort()
        self.assertEqual(
            exp_result, self.simdex_filtered_fn,
            "filtering self.simdex with c1.C = 800 then with r.R = 3 \
                         should return LinkedCapacities_A and _F")
        self.assertEqual({
            'c1.C': 800,
            'r.R': 3
        }, self.simdex_filtered.filterset,
                         'After filtering, filterset has to be updated')
        self.simdex.h5.close()

    def test_filter_unchanged_original(self):
        """self.simdex.filter() should not change self.simdex (bug and issue on github)"""

        filt_dic = {'c1.C': 800}
        self.simdex_filtered = self.simdex.filter(filt_dic)

        self.assertEqual(
            {}, self.simdex.filterset,
            'Simdex.filter() should NOT change filterset of Simdex')
        self.simdex.h5.close()

    def test_filter_floatvalues(self):
        """Simdex.filter() with float values should work well"""

        filt_dic = {'r.R': 8.15}
        self.simdex_filtered = self.simdex.filter(filt_dic)
        self.simdex_filtered_fn = self.simdex_filtered.get_filenames()
        self.simdex_filtered_fn.sort()
        exp_result = ['LinkedCapacities_E.mat']
        exp_result.sort()
        self.assertEqual(
            exp_result, self.simdex_filtered_fn,
            'filtering self.simdex with r.R = 8.15 should return\
                         exp_result')
        self.assertEqual(filt_dic, self.simdex_filtered.filterset,
                         'After filtering, filterset has to be updated')
        self.simdex.h5.close()

    def test_plot(self):
        """Simdex.plot() should return [fig, lines, leg]"""

        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)

        self.simdex_filtered = self.simdex.filter({'c1.C': ''})
        [fig, lines, leg] = self.simdex_filtered.plot('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_save_and_load(self):
        """Saving and loading a self.simdex object should return exactly the same object"""

        self.simdex.save('Test_save.dat')
        loaded = load_simdex('Test_save.dat')
        for attr in self.simdex.__dict__:
            exec("s = self.simdex." + attr)
            exec("l = loaded." + attr)

            if isinstance(s, np.ndarray):
                self.assertTrue((l == s).all())
            else:
                self.assertEqual(s, l)

    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_fullnames(self):
        """Simdex.get() should return correctly for variables and parameters"""

        #        result = self.simdex.get('c2.T')
        #        expectedsids = self.simdex.get_SID('linkedcapacities')
        #        self.assertEqual(sorted(result.keys()), sorted(expectedsids))
        #        sim = Simulation('LinkedCapacities_B.mat')
        #        sid = self.simdex.get_SID('_B')[0]
        #        self.assertTrue(np.all(result[sid]==sim.get_value('c2.T')))

        # now a parameter
        c1_C = self.simdex.get('c1.C').values()
        c1_C.sort()
        print '\n' * 10, 'c1_C = ', c1_C
        exp_result_sorted = [None, 600., 600., 800., 800., 800., 800., 1000.]

        self.assertEqual(exp_result_sorted, c1_C)
        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()
# in big simulation files, it's not always easy to find the right parameter
# or variable
sim.exist('q_flow')

time = sim.get_value('Time')
Q = sim.get_value(u'r.heatPort_a.Q_flow')
Q_sum = np.trapz(Q, time)
print "The total energy that flowed through the resistance is %.1f J" % Q_sum

for p, v in zip(sim.parameters, sim.parametervalues):
    print ''.join([p, ' = ', str(v)])

# there are different ways to create a simdex

s1 = Simdex()  # an empty simdex object
s1.scan(
)  # scan current folder and add all found simulation files to the simdex

s2 = Simdex('SubfolderWithCrappyFiles')
# create a simdex directly from a folder

print s1
print s2

# now, let's look at some attributes
s1.get_filenames()  # method, so add ()
s1.get_filenames('path')
s1.parameters  # attribute, so no ()
s1.parametermap
s1.parametervalues
Beispiel #20
0
                    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        
        return a+b
        

dummyprocess=DummyProcess()

myclass=MyClass(dummyprocess)

c= myclass.my_method(3, 5.67)
print c

sim=Simulation('LinkedCapacities.mat', verbose = True)
processed = sim.postprocess(process)

simdex=Simdex(process=process, verbose = False)
simdex.scan()