Example #1
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)
Example #2
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()
Example #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()
Example #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()
Example #5
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()        
Example #6
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()
Example #7
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()
# 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
Example #9
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()