コード例 #1
0
    def setUp(self):
        self.top = top = set_as_top(Assembly())
        driver = top.add('driver', SimpleCaseIterDriver())
        top.add('comp1', ExecComp(exprs=['z=x+y']))
        top.add('comp2', ExecComp(exprs=['z=x+1']))
        top.connect('comp1.z', 'comp2.x')
        top.comp1.add('a_string', Str("Hello',;','", iotype='out'))
        top.comp1.add('a_array', Array(array([1.0, 3.0, 5.5]), iotype='out'))
        top.comp1.add('x_array', Array(array([1.0, 1.0, 1.0]), iotype='in'))
        top.comp1.add('b_bool', Bool(False, iotype='in'))
        top.comp1.add('vt', VarTree(DumbVT(), iotype='out'))
        driver.workflow.add(['comp1', 'comp2'])

        # now create some Cases
        outputs = ['comp1.z', 'comp2.z', 'comp1.a_string', 'comp1.a_array[2]']
        cases = []
        for i in range(10):
            inputs = [('comp1.x', i+0.1), ('comp1.y', i*2 + .1),
                      ('comp1.x_array[1]', 99.88), ('comp1.b_bool', True)]
            cases.append(Case(inputs=inputs, outputs=outputs))

        Case.set_vartree_inputs(driver, cases)
        driver.add_responses(sorted(outputs))

        self.filename_json = "openmdao_test_csv_case_iterator.json"
        self.filename_csv = "openmdao_test_csv_case_iterator.csv"
コード例 #2
0
    def test_CSVCaseIterator_read_external_file_without_header(self):

        csv_data = [
            '33.5, 76.2, "Hello There"\n'
            '3.14159, 0, "Goodbye z"\n'
        ]

        outfile = open(self.filename, 'w')
        outfile.writelines(csv_data)
        outfile.close()

        header_dict = {
            0: "comp1.x",
            1: "comp1.y",
            2: "comp2.b_string"
        }

        self.top.comp2.add('b_string', Str("Hello',;','", iotype='in'))

        sout = StringIO.StringIO()
        cases = [case for case in CSVCaseIterator(filename=self.filename,
                                                  headers=header_dict)]
        self.top.driver.clear_parameters()
        Case.set_vartree_inputs(self.top.driver, cases)
        self.top.recorders = [DumpCaseRecorder(sout)]

        self.top.run()

        self.assertEqual(self.top.comp1.x, 3.14159)
        self.assertEqual(self.top.comp1.y, 0.0)
        self.assertEqual(self.top.comp2.b_string, "Goodbye z")
コード例 #3
0
    def setUp(self):
        self.startdir = os.getcwd()
        self.tempdir = tempfile.mkdtemp(prefix='test_csv-')
        os.chdir(self.tempdir)

        self.top = top = set_as_top(Assembly())
        driver = top.add('driver', SimpleCaseIterDriver())
        top.add('comp1', ExecComp(exprs=['z=x+y']))
        top.add('comp2', ExecComp(exprs=['z=x+1']))
        top.connect('comp1.z', 'comp2.x')
        top.comp1.add('a_string', Str("Hello',;','", iotype='out'))
        top.comp1.add('a_array', Array(array([1.0, 3.0, 5.5]), iotype='out'))
        top.comp1.add('x_array', Array(array([1.0, 1.0, 1.0]), iotype='in'))
        top.comp1.add('b_bool', Bool(False, iotype='in'))
        top.comp1.add('vt', VarTree(DumbVT(), iotype='out'))
        top.driver.workflow.add(['comp1', 'comp2'])

        # now create some Cases
        outputs = ['comp1.z', 'comp2.z', 'comp1.a_string', 'comp1.a_array[2]']
        cases = []
        for i in range(10):
            i = float(i)
            inputs = [('comp1.x', i + 0.1), ('comp1.y', i * 2 + .1),
                      ('comp1.x_array[1]', 99.88), ('comp1.b_bool', True)]
            cases.append(Case(inputs=inputs, outputs=outputs))

        Case.set_vartree_inputs(driver, cases)
        driver.add_responses(sorted(outputs))

        self.filename = "openmdao_test_csv_case_iterator.csv"
コード例 #4
0
    def test_inoutDB(self):
        # This test runs some cases, puts them in a DB using a DBCaseRecorder,
        # then runs the model again using the same cases, pulled out of the DB
        # by a DBCaseIterator.  Finally the cases are dumped to a string after
        # being run for the second time.

        self.top.recorders = [DBCaseRecorder()]
        self.top.run()

        # Gui pane stuff

        attrs = self.top.recorders[0].get_attributes()
        self.assertTrue("Inputs" in attrs.keys())
        self.assertTrue({'name': 'dbfile',
                         'id': 'dbfile',
                         'type': 'str',
                         'connected': '',
                         'value': ':memory:',
                         'desc': 'Name of the database file to be recorded. Default ' +
                       'is ":memory:", which writes the database to memory.'} in attrs['Inputs'])

        # now use the DB as source of Cases
        cases = [case for case in self.top.recorders[0].get_iterator()]
        Case.set_vartree_inputs(self.top.driver, cases)

        sout = StringIO.StringIO()
        self.top.recorders = [DumpCaseRecorder(sout)]
        self.top.run()
        expected = [
            'Case:',
            '   uuid: ad4c1b76-64fb-11e0-95a8-001e8cf75fe',
            '   timestamp: 1383239074.309192',
            '   inputs:',
            "      comp1.a_dict: {'a': 'b'}",
            "      comp1.a_list: ['a', 'b']",
            '      comp1.x: 8.0',
            '      comp1.y: 16.0',
            '   outputs:',
            '      Response(comp1.z): 24.0',
            '      Response(comp2.z): 25.0',
        ]
#        print sout.getvalue()
        lines = sout.getvalue().split('\n')
        count = 0
        for index, line in enumerate(lines):
            if line.startswith('Case:'):
                count += 1
                if count != 9:
                    continue
                for i in range(len(expected)):
                    if expected[i].startswith('   uuid:'):
                        self.assertTrue(lines[index+i].startswith('   uuid:'))
                    elif expected[i].startswith('   timestamp:'):
                        self.assertTrue(lines[index+i].startswith('   timestamp:'))
                    else:
                        self.assertEqual(lines[index+i], expected[i])
                break
        else:
            self.fail("couldn't find the expected Case")
コード例 #5
0
    def test_inoutDB(self):
        # This test runs some cases, puts them in a DB using a DBCaseRecorder,
        # then runs the model again using the same cases, pulled out of the DB
        # by a DBCaseIterator.  Finally the cases are dumped to a string after
        # being run for the second time.

        self.top.recorders = [DBCaseRecorder()]
        self.top.run()

        # now use the DB as source of Cases
        cases = [case for case in self.top.recorders[0].get_iterator()]
        Case.set_vartree_inputs(self.top.driver, cases)

        sout = StringIO.StringIO()
        self.top.recorders = [DumpCaseRecorder(sout)]
        self.top.run()
        expected = [
            "Case:",
            "   uuid: d99424f3-9c1b-11e4-801d-20c9d0478eff",
            "   timestamp: 1421260415.638640",
            "   inputs:",
            "      comp1.a_dict: {'a': 'b'}",
            "      comp1.a_list: ['a', 'b']",
            "      comp1.x: 8.0",
            "      comp1.y: 16.0",
            "   outputs:",
            "      _pseudo_0.out0: 24.0",
            "      _pseudo_1.out0: 25.0",
            "      comp1.derivative_exec_count: 0",
            "      comp1.exec_count: 19",
            "      comp1.itername: 9-comp1",
            "      comp1.z: 24.0",
            "      comp2.derivative_exec_count: 0",
            "      comp2.exec_count: 19",
            "      comp2.itername: 9-comp2",
            "      comp2.z: 25.0",
            "      driver.workflow.itername: 9",
           ]
        #print sout.getvalue()
        lines = sout.getvalue().split('\n')
        count = 0
        for index, line in enumerate(lines):
            if line.startswith('Case:'):
                count += 1
                if count != 9:
                    continue
                for i in range(len(expected)):
                    if expected[i].startswith('   uuid:'):
                        self.assertTrue(lines[index+i].startswith('   uuid:'))
                    elif expected[i].startswith('   timestamp:'):
                        self.assertTrue(lines[index+i].startswith('   timestamp:'))
                    else:
                        self.assertEqual(lines[index+i], expected[i])
                break
        else:
            self.fail("couldn't find the expected Case")
コード例 #6
0
    def test_inoutCSV_delimiter(self):

        # Repeat test above using semicolon delimiter and ' as quote char.

        self.top = set_as_top(TestAssembly())
        self.top.recorders = [CSVCaseRecorder(filename=self.filename,
                                              delimiter=';', quotechar="'")]
        self.top.run()

        # now use the CSV recorder as source of Cases
        cases = [case for case in self.top.recorders[0].get_iterator()]
        driver = self.top.add('driver', SimpleCaseIterDriver())
        Case.set_vartree_inputs(self.top.driver, cases)

        sout = StringIO.StringIO()
        self.top.recorders = [DumpCaseRecorder(sout)]
        driver.add_responses(['comp.x',])
        self.top.run()

        # Check the results
        expected = [
         "Case:",
        "   uuid: 2983e819-9c1b-11e4-804e-20c9d0478eff",
        "   timestamp: 1421260120.252554",
        "   inputs:",
        "      comp.dummy_data.dummy1: 0.0333333393814",
        "   outputs:",
        "      _pseudo_1.out0: -2.19888892477",
        "      comp.derivative_exec_count: 0",
        "      comp.exec_count: 46",
        "      comp.itername: 4-comp",
        "      comp.x: -2.19888892477",
        "      driver.workflow.itername: 4",
        ]

        lines = sout.getvalue().split('\n')
        count = 0
        for index, line in enumerate(lines):
            if line.startswith('Case:'):
                count += 1
                if count != 4:
                    continue
                for i in range(len(expected)):
                    prefix = expected[i].split(':')[0]
                    if prefix.lstrip() in ['uuid', 'timestamp', 'comp.exec_count']:
                        # these values vary, just check proper prefix & indentation
                        self.assertTrue(lines[index+i].startswith(prefix+':'))
                    else:
                        self.assertEqual(lines[index+i], expected[i])
                break
        else:
            self.fail("couldn't find the expected Case")
コード例 #7
0
    def test_inoutCSV(self):

        #This test runs some cases, puts them in a CSV file using a
        #CSVCaseRecorder, then runs the model again using the same cases,
        #pulled out of the CSV file by a CSVCaseIterator.  Finally the cases
        #are dumped to a string after being run for the second time.

        self.top.recorders = [CSVCaseRecorder(filename=self.filename)]
        self.top.recorders[0].num_backups = 0
        self.top.run()

        # now use the CSV recorder as source of Cases
        cases = [case for case in self.top.recorders[0].get_iterator()]
        Case.set_vartree_inputs(self.top.driver, cases)

        sout = StringIO.StringIO()
        self.top.recorders = [DumpCaseRecorder(sout)]
        self.top.run()
        expected = [
            'Case:',
            '   uuid: ad4c1b76-64fb-11e0-95a8-001e8cf75fe',
            '   timestamp: 1383239074.309192',
            '   inputs:',
            '      comp1.b_bool: True',
            '      comp1.x: 8.1',
            '      comp1.x_array[1]: 99.88',
            '      comp1.y: 16.1',
            '   outputs:',
            '      Response(comp1.a_array[2]): 5.5',
            "      Response(comp1.a_string): Hello',;','",
            '      Response(comp1.z): 24.2',
            '      Response(comp2.z): 25.2',
            ]
#        print sout.getvalue()
        lines = sout.getvalue().split('\n')
        count = 0
        for index, line in enumerate(lines):
            if line.startswith('Case:'):
                count += 1
                if count != 9:
                    continue
                for i in range(len(expected)):
                    if expected[i].startswith('   uuid:'):
                        self.assertTrue(lines[index+i].startswith('   uuid:'))
                    elif expected[i].startswith('   timestamp:'):
                        self.assertTrue(lines[index+i].startswith('   timestamp:'))
                    else:
                        self.assertEqual(lines[index+i], expected[i])
                break
        else:
            self.fail("couldn't find the expected Case")
コード例 #8
0
    def test_flatten(self):
        # create some Cases

        outputs = ['comp1.a_array', 'comp1.vt']
        inputs = [('comp1.x_array', array([2.0, 2.0, 2.0]))]
        cases = [Case(inputs=inputs, outputs=outputs)]
        self.top.driver.clear_parameters()
        Case.set_vartree_inputs(self.top.driver, cases)
        self.top.driver.clear_responses()
        self.top.driver.add_responses(outputs)
        self.top.recorders = [CSVCaseRecorder(filename=self.filename)]
        self.top.recorders[0].num_backups = 0
        self.top.run()

        # check recorded cases
        cases = [case for case in self.top.recorders[0].get_iterator()]
コード例 #9
0
    def setUp(self):
        self.top = top = set_as_top(Assembly())
        driver = top.add('driver', SimpleCaseIterDriver())
        top.add('comp1', ExecComp(exprs=['z=x+y']))
        top.add('comp2', ExecComp(exprs=['z=x+1']))
        top.connect('comp1.z', 'comp2.x')
        driver.workflow.add(['comp1', 'comp2'])

        # now create some Cases
        outputs = ['comp1.z', 'comp2.z']
        cases = []
        for i in range(10):
            inputs = [('comp1.x', i), ('comp1.y', i * 2)]
            cases.append(Case(inputs=inputs, outputs=outputs))

        Case.set_vartree_inputs(driver, cases)
        driver.add_responses(outputs)
コード例 #10
0
    def setUp(self):
        self.top = top = set_as_top(Assembly())
        driver = top.add('driver', CaseIteratorDriver())
        top.add('comp1', TExecComp(exprs=['z=x+y']))
        top.add('comp2', ExecComp(exprs=['z=x+1']))
        top.connect('comp1.z', 'comp2.x')
        driver.workflow.add(['comp1', 'comp2'])

        # now create some Cases
        outputs = ['comp1.z', 'comp2.z']
        cases = []
        for i in range(10):
            inputs = [('comp1.x', i), ('comp1.y', i*2)]
            cases.append(Case(inputs=inputs, outputs=outputs))

        Case.set_vartree_inputs(driver, cases)
        driver.add_responses(outputs)
コード例 #11
0
    def setUp(self):
        self.top = top = set_as_top(Assembly())
        driver = top.add('driver', SimpleCaseIterDriver())
        top.add('comp1', ExecComp(exprs=['z=x+y']))
        top.add('comp2', ExecComp(exprs=['z=x+1']))
        top.comp1.add('a_dict', Dict({}, iotype='in'))
        top.comp1.add('a_list', List([], iotype='in'))
        top.connect('comp1.z', 'comp2.x')
        driver.workflow.add(['comp1', 'comp2'])

        # now create some Cases
        outputs = ['comp1.z', 'comp2.z']
        cases = []
        for i in range(10):
            inputs = [('comp1.x', i), ('comp1.y', i*2),
                      ('comp1.a_dict', {'a': 'b'}),
                      ('comp1.a_list', ['a', 'b'])]
            cases.append(Case(inputs=inputs, outputs=outputs))
        Case.set_vartree_inputs(driver, cases)
        driver.add_responses(outputs)
コード例 #12
0
    def setUp(self):
        self.top = top = set_as_top(Assembly())
        driver = top.add('driver', CaseIteratorDriver())
        top.add('comp1', TExecComp(exprs=['z=x+y']))
        top.add('comp2', ExecComp(exprs=['z=x+1']))
        top.connect('comp1.z', 'comp2.x')
        driver.workflow.add(['comp1', 'comp2'])

        # now create some Cases
        outputs = ['comp1.z', 'comp2.z']
        cases = []
        for i in range(10):
            i = float(i)
            inputs = [('comp1.x', i), ('comp1.y', i * 2)]
            cases.append(Case(inputs=inputs, outputs=outputs))

        Case.set_vartree_inputs(driver, cases)
        driver.add_responses(outputs)

        self.tempdir = tempfile.mkdtemp(prefix='test_jsonrecorder-')
コード例 #13
0
    def test_flatten(self):
        # try it after creating some Cases
        # more rigorous checking of the csv

        outputs = ['comp1.a_array', 'comp1.vt']
        inputs = [('comp1.x_array', array([2.0, 2.0, 2.0]))]
        cases = [Case(inputs=inputs, outputs=outputs)]
        self.top.driver.clear_parameters()
        Case.set_vartree_inputs(self.top.driver, cases)
        self.top.driver.clear_responses()
        self.top.driver.add_responses(outputs)
        self.top.recorders = [JSONCaseRecorder(self.filename_json)]
        self.top.recorders[0].num_backups = 0
        self.top.run()

        cds = CaseDataset(self.filename_json, 'json')
        data = cds.data.fetch()  # results
        caseset_query_to_csv(data, self.filename_csv)

        # check recorded cases
        cases = [case for case in CSVCaseIterator(filename=self.filename_csv)]
コード例 #14
0
    def test_CSVCaseIterator_read_external_file_with_header(self):

        csv_data = ['"comp1.x", "comp1.y", "comp2.b_string"\n',
                    '33.5, 76.2, "Hello There"\n'
                    '3.14159, 0, "Goodbye z"\n'
                    ]

        outfile = open(self.filename, 'w')
        outfile.writelines(csv_data)
        outfile.close()

        self.top.comp2.add('b_string', Str("Hello',;','", iotype='in'))


        sout = StringIO.StringIO()
        cases = [case for case in CSVCaseIterator(filename=self.filename)]
        self.top.driver.clear_parameters()
        Case.set_vartree_inputs(self.top.driver, cases)
        self.top.recorders = [DumpCaseRecorder(sout)]
        self.top.run()

        self.assertEqual(self.top.comp1.x, 3.14159)
        self.assertEqual(self.top.comp1.y, 0.0)
        self.assertEqual(self.top.comp2.b_string, "Goodbye z")

        # Gui pane stuff

        iterator = CSVCaseIterator(filename=self.filename)
        attrs = iterator.get_attributes()
        self.assertTrue("Inputs" in attrs.keys())
        self.assertTrue({'name': 'filename',
                         'type': 'str',
                         'connected': '',
                         'value': 'openmdao_test_csv_case_iterator.csv',
                         'desc': 'Name of the CSV file to be iterated.'} in attrs['Inputs'])
        self.assertTrue({'name': 'headers',
                         'type': 'NoneType',
                         'connected': '',
                         'value': 'None',
                         'desc': 'Optional dictionary of header labels, where the key is the column number.'} in attrs['Inputs'])
コード例 #15
0
    def setUp(self):
        self.top = top = set_as_top(Assembly())
        driver = top.add('driver', SimpleCaseIterDriver())
        top.add('comp1', ExecComp(exprs=['z=x+y']))
        top.add('comp2', ExecComp(exprs=['z=x+1']))
        top.comp1.add('a_dict', Dict({}, iotype='in'))
        top.comp1.add('a_list', List([], iotype='in'))
        top.connect('comp1.z', 'comp2.x')
        driver.workflow.add(['comp1', 'comp2'])

        # now create some Cases
        outputs = ['comp1.z', 'comp2.z']
        cases = []
        for i in range(10):
            i = float(i)
            inputs = [('comp1.x', i), ('comp1.y', i * 2),
                      ('comp1.a_dict', {
                          'a': 'b'
                      }), ('comp1.a_list', ['a', 'b'])]
            cases.append(Case(inputs=inputs, outputs=outputs))
        Case.set_vartree_inputs(driver, cases)
        driver.add_responses(outputs)
コード例 #16
0
    def _create_nested_assemblies(self, dbname, drivertype):
        top = set_as_top(self._create_assembly(dbname, drivertype))
        top.add('asm', self._create_assembly(dbname, drivertype))
        top.driver.workflow.add('asm')
        top.asm.add('asm', self._create_assembly(dbname, drivertype))
        top.asm.driver.workflow.add('asm')

        top.recorders = [DBCaseRecorder(dbname, append=True)]
        Case.set_vartree_inputs(top.driver, self._create_cases(1))
        Case.set_vartree_inputs(top.asm.driver, self._create_cases(2))
        Case.set_vartree_inputs(top.asm.asm.driver, self._create_cases(3))

        return top
コード例 #17
0
    def _create_nested_assemblies(self, dbname, drivertype):
        top = set_as_top(self._create_assembly(dbname, drivertype))
        top.add('asm', self._create_assembly(dbname, drivertype))
        top.driver.workflow.add('asm')
        top.asm.add('asm', self._create_assembly(dbname, drivertype))
        top.asm.driver.workflow.add('asm')

        top.recorders = [DBCaseRecorder(dbname, append=True)]
        Case.set_vartree_inputs(top.driver, self._create_cases(1))
        Case.set_vartree_inputs(top.asm.driver, self._create_cases(2))
        Case.set_vartree_inputs(top.asm.asm.driver, self._create_cases(3))

        return top
コード例 #18
0
    def _create_nested_workflows(self, dbname, drivertype):
        # this is kind of bogus because the inner driver loops are
        # overwriting the values set by the outer loops, but for
        # this test I'm only interested in checking if the
        # Case hierarchy is structured properly
        top = set_as_top(self._create_assembly(dbname, drivertype))
        driver2 = top.add('driver2', drivertype())
        top.driver.workflow.add(['driver2'])
        driver3 = top.add('driver3', drivertype())
        top.driver2.workflow.add(['driver3'])
        top.driver3.workflow.add(['comp1', 'comp2'])

        Case.set_vartree_inputs(top.driver, self._create_cases(1))
        Case.set_vartree_inputs(top.driver2, self._create_cases(2))
        Case.set_vartree_inputs(top.driver3, self._create_cases(3))
        return top
コード例 #19
0
    def _create_nested_workflows(self, dbname, drivertype):
        # this is kind of bogus because the inner driver loops are
        # overwriting the values set by the outer loops, but for
        # this test I'm only interested in checking if the
        # Case hierarchy is structured properly
        top = set_as_top(self._create_assembly(dbname, drivertype))
        driver2 = top.add('driver2', drivertype())
        top.driver.workflow.add(['driver2'])
        driver3 = top.add('driver3', drivertype())
        top.driver2.workflow.add(['driver3'])
        top.driver3.workflow.add(['comp1', 'comp2'])

        Case.set_vartree_inputs(top.driver, self._create_cases(1))
        Case.set_vartree_inputs(top.driver2, self._create_cases(2))
        Case.set_vartree_inputs(top.driver3, self._create_cases(3))
        return top
コード例 #20
0
        self.top.run()
        cases = [case for case in self.top.recorders[0].get_iterator()]
        end_case = cases[-1]
        #end_case.get_input('comp.dummy_data.dummy1')
        self.assertAlmostEqual(end_case.get_input('comp.dummy_data.dummy1'),
                               3.0, 1)  #3.0 should be minimum

    def test_inoutCSV(self):
        self.top = set_as_top(TestAssembly())
        self.top.recorders = [CSVCaseRecorder(filename=self.filename)]
        self.top.run()

        # now use the CSV recorder as source of Cases
        cases = [case for case in self.top.recorders[0].get_iterator()]
        driver = self.top.add('driver', SimpleCaseIterDriver())
        Case.set_vartree_inputs(self.top.driver, cases)

        sout = StringIO.StringIO()
        self.top.recorders = [DumpCaseRecorder(sout)]
        driver.add_responses([
            'comp.x',
        ])
        self.top.run()

        # Check the results
        expected = [
            "Case:",
            "   uuid: 2983e819-9c1b-11e4-804e-20c9d0478eff",
            "   timestamp: 1421260120.252554",
            "   inputs:",
            "      comp.dummy_data.dummy1: 0.0333333393814",
コード例 #21
0
    def test_flatten(self):
        # create some Cases

        outputs = ['comp1.a_array', 'comp1.vt']
        inputs = [('comp1.x_array', array([2.0, 2.0, 2.0]))]
        cases = [Case(inputs=inputs, outputs=outputs)]
        self.top.driver.clear_parameters()
        Case.set_vartree_inputs(self.top.driver, cases)
        self.top.driver.clear_responses()
        self.top.driver.add_responses(outputs)
        self.top.recorders = [CSVCaseRecorder(filename=self.filename)]
        self.top.recorders[0].num_backups = 0
        self.top.run()

        # check recorded cases
        cases = [case for case in self.top.recorders[0].get_iterator()]
        sout = StringIO.StringIO()
        for case in cases:
            print >>sout, case
        expected = [
            'Case:',
            '   uuid: ad4c1b76-64fb-11e0-95a8-001e8cf75fe',
            '   timestamp: 1383238593.781986',
            '   inputs:',
            '      comp1.x_array[0]: 2.0',
            '      comp1.x_array[1]: 2.0',
            '      comp1.x_array[2]: 2.0',
            '   outputs:',
            '      Response(comp1.a_array)[0]: 1.0',
            '      Response(comp1.a_array)[1]: 3.0',
            '      Response(comp1.a_array)[2]: 5.5',
            '      Response(comp1.vt).data: ',
            '      Response(comp1.vt).v1: 1.0',
            '      Response(comp1.vt).v2: 2.0',
            '      Response(comp1.vt).vt2.data: ',
            '      Response(comp1.vt).vt2.vt3.a: 1.0',
            '      Response(comp1.vt).vt2.vt3.b: 12.0',
            '      Response(comp1.vt).vt2.vt3.data: ',
            '      Response(comp1.vt).vt2.x: -1.0',
            '      Response(comp1.vt).vt2.y: -2.0',
            '      comp1.a_array[0]: 1.0',
            '      comp1.a_array[1]: 3.0',
            '      comp1.a_array[2]: 5.5',
            "      comp1.a_string: Hello',;','",
            '      comp1.derivative_exec_count: 0.0',
            '      comp1.exec_count: 1.0',
            '      comp1.itername: 1-comp1',
            '      comp1.vt.data: ',
            '      comp1.vt.v1: 1.0',
            '      comp1.vt.v2: 2.0',
            '      comp1.vt.vt2.data: ',
            '      comp1.vt.vt2.vt3.a: 1.0',
            '      comp1.vt.vt2.vt3.b: 12.0',
            '      comp1.vt.vt2.vt3.data: ',
            '      comp1.vt.vt2.x: -1.0',
            '      comp1.vt.vt2.y: -2.0',
            '      comp1.z: 0.0',
            '      comp2.derivative_exec_count: 0.0',
            '      comp2.exec_count: 1.0',
            '      comp2.itername: 1-comp2',
            '      comp2.z: 1.0',
            '      driver.workflow.itername: 1',
            ]

#        print sout.getvalue()
        lines = sout.getvalue().split('\n')
        for index, line in enumerate(lines):
            if line.startswith('Case:'):
                for i in range(len(expected)):
                    if expected[i].startswith('   uuid:'):
                        self.assertTrue(lines[index+i].startswith('   uuid:'))
                    elif expected[i].startswith('   timestamp:'):
                        self.assertTrue(lines[index+i].startswith('   timestamp:'))
                    else:
                        self.assertEqual(lines[index+i], expected[i])
                break
        else:
            self.fail("couldn't find the expected Case")

        # now use the CSV recorder as source of Cases
        self.top.driver.clear_parameters()
        Case.set_vartree_inputs(self.top.driver, cases)
        sout = StringIO.StringIO()
        self.top.recorders = [DumpCaseRecorder(sout)]  # Dump not flattened.
        self.top.run()
        expected = [
            'Case:',
            '   uuid: ad4c1b76-64fb-11e0-95a8-001e8cf75fe',
            '   timestamp: 1383238593.781986',
            '   inputs:',
            '      comp1.x_array[0]: 2.0',
            '      comp1.x_array[1]: 2.0',
            '      comp1.x_array[2]: 2.0',
            '   outputs:',
            '      Response(comp1.a_array): [ 1.   3.   5.5]',
            '      Response(comp1.vt): <openmdao.main.test.test_vartree.DumbVT object',
            '      comp1.a_array: [ 1.   3.   5.5]',
            "      comp1.a_string: Hello',;','",
            '      comp1.derivative_exec_count: 0',
            '      comp1.exec_count: 2',
            '      comp1.itername: 1-comp1',
            '      comp1.vt: <openmdao.main.test.test_vartree.DumbVT object',
            '      comp1.z: 0.0',
            '      comp2.derivative_exec_count: 0',
            '      comp2.exec_count: 2',
            '      comp2.itername: 1-comp2',
            '      comp2.z: 1.0',
            '      driver.workflow.itername: 1',
            ]
#        print sout.getvalue()
        lines = sout.getvalue().split('\n')
        for index, line in enumerate(lines):
            if line.startswith('Case:'):
                for i in range(len(expected)):
                    if expected[i].startswith('   uuid:'):
                        self.assertTrue(lines[index+i].startswith('   uuid:'))
                    elif expected[i].startswith('   timestamp:'):
                        self.assertTrue(lines[index+i].startswith('   timestamp:'))
                    elif expected[i].startswith('      Response(comp1.vt):') or \
                         expected[i].startswith('      comp1.vt:'):
                        self.assertTrue(lines[index+i].startswith(expected[i]))
                    else:
                        self.assertEqual(lines[index+i], expected[i])
                break
        else:
            self.fail("couldn't find the expected Case")
コード例 #22
0
    def test_flatten(self):
        # try it after creating some Cases
        # more rigorous checking of the csv

        outputs = ['comp1.a_array', 'comp1.vt']
        inputs = [('comp1.x_array', array([2.0, 2.0, 2.0]))]
        cases = [Case(inputs=inputs, outputs=outputs)]
        self.top.driver.clear_parameters()
        Case.set_vartree_inputs(self.top.driver, cases)
        self.top.driver.clear_responses()
        self.top.driver.add_responses(outputs)
        self.top.recorders = [JSONCaseRecorder(self.filename_json)]
        self.top.recorders[0].num_backups = 0
        self.top.run()

        cds = CaseDataset(self.filename_json, 'json')
        data = cds.data.fetch() # results
        caseset_query_to_csv( data, self.filename_csv)

        # check recorded cases
        cases = [case for case in CSVCaseIterator(filename=self.filename_csv)]
        sout = StringIO.StringIO()
        for case in cases:
            print >>sout, case
        expected = [
            'Case:',
            '   uuid: ad4c1b76-64fb-11e0-95a8-001e8cf75fe',
            '   timestamp: 1383238593.781986',
            '   parent_uuid: ad4c1b76-64fb-11e0-95a8-001e8cf75fe',
            '   inputs:',
            '      comp1.x_array[0]: 2.0',
            '      comp1.x_array[1]: 2.0',
            '      comp1.x_array[2]: 2.0',
            '   outputs:',
            '      Response(comp1.a_array)[0]: 1.0',
            '      Response(comp1.a_array)[1]: 3.0',
            '      Response(comp1.a_array)[2]: 5.5',
            '      Response(comp1.vt).data: ',
            '      Response(comp1.vt).v1: 1.0',
            '      Response(comp1.vt).v2: 2.0',
            '      Response(comp1.vt).vt2.data: ',
            '      Response(comp1.vt).vt2.vt3.a: 1.0',
            '      Response(comp1.vt).vt2.vt3.b: 12.0',
            '      Response(comp1.vt).vt2.vt3.data: ',
            '      Response(comp1.vt).vt2.x: -1.0',
            '      Response(comp1.vt).vt2.y: -2.0',
            '      comp1.a_array[0]: 1.0',
            '      comp1.a_array[1]: 3.0',
            '      comp1.a_array[2]: 5.5',
            "      comp1.a_string: Hello',;','",
            '      comp1.derivative_exec_count: 0.0',
            '      comp1.exec_count: 1.0',
            '      comp1.itername: 1-comp1',
            '      comp1.vt.data: ',
            '      comp1.vt.v1: 1.0',
            '      comp1.vt.v2: 2.0',
            '      comp1.vt.vt2.data: ',
            '      comp1.vt.vt2.vt3.a: 1.0',
            '      comp1.vt.vt2.vt3.b: 12.0',
            '      comp1.vt.vt2.vt3.data: ',
            '      comp1.vt.vt2.x: -1.0',
            '      comp1.vt.vt2.y: -2.0',
            '      comp1.z: 0.0',
            '      comp2.derivative_exec_count: 0.0',
            '      comp2.exec_count: 1.0',
            '      comp2.itername: 1-comp2',
            '      comp2.z: 1.0',
            '      driver.workflow.itername: 1',
            ]

        #print sout.getvalue()
        lines = sout.getvalue().split('\n')
        for index, line in enumerate(lines):
            if line.startswith('Case:'):
                for i in range(len(expected)):
                    if expected[i].startswith('   uuid:'):
                        self.assertTrue(lines[index+i].startswith('   uuid:'))
                    elif expected[i].startswith('   parent_uuid:'):
                        self.assertTrue(lines[index+i].startswith('   parent_uuid:'))
                    elif expected[i].startswith('   timestamp:'):
                        self.assertTrue(lines[index+i].startswith('   timestamp:'))
                    else:
                        self.assertEqual(lines[index+i], expected[i])
                break
        else:
            self.fail("couldn't find the expected Case")
コード例 #23
0
    def test_flatten(self):
        # try it after creating some Cases
        # more rigorous checking of the csv

        outputs = ['comp1.a_array', 'comp1.vt']
        inputs = [('comp1.x_array', array([2.0, 2.0, 2.0]))]
        cases = [Case(inputs=inputs, outputs=outputs)]
        self.top.driver.clear_parameters()
        Case.set_vartree_inputs(self.top.driver, cases)
        self.top.driver.clear_responses()
        self.top.driver.add_responses(outputs)
        self.top.recorders = [JSONCaseRecorder(self.filename_json)]
        self.top.recorders[0].num_backups = 0
        self.top.run()

        cds = CaseDataset(self.filename_json, 'json')
        data = cds.data.fetch()  # results
        caseset_query_to_csv( data, self.filename_csv)

        # check recorded cases
        cases = [case for case in CSVCaseIterator(filename=self.filename_csv)]
        sout = StringIO.StringIO()
        for case in cases:
            print >>sout, case

        expected = \
'''Case:
   uuid: 07280785-9b76-11e4-800d-20c9d0478eff
   timestamp: 1421189195.646824
   parent_uuid: 0720c385-9b76-11e4-b796-20c9d0478eff
   inputs:
      comp1.x_array[0]: 2.0
      comp1.x_array[1]: 2.0
      comp1.x_array[2]: 2.0
   outputs:
      _pseudo_4.out0[0]: 1.0
      _pseudo_4.out0[1]: 3.0
      _pseudo_4.out0[2]: 5.5
      _pseudo_5.out0.data: 
      _pseudo_5.out0.v1: 1.0
      _pseudo_5.out0.v2: 2.0
      _pseudo_5.out0.vt2.data: 
      _pseudo_5.out0.vt2.vt3.a: 1.0
      _pseudo_5.out0.vt2.vt3.b: 12.0
      _pseudo_5.out0.vt2.vt3.data: 
      _pseudo_5.out0.vt2.x: -1.0
      _pseudo_5.out0.vt2.y: -2.0
      comp1.a_array[0]: 1.0
      comp1.a_array[1]: 3.0
      comp1.a_array[2]: 5.5
      comp1.a_string: Hello',;','
      comp1.derivative_exec_count: 0.0
      comp1.exec_count: 1.0
      comp1.itername: 1-comp1
      comp1.vt.data: 
      comp1.vt.v1: 1.0
      comp1.vt.v2: 2.0
      comp1.vt.vt2.data: 
      comp1.vt.vt2.vt3.a: 1.0
      comp1.vt.vt2.vt3.b: 12.0
      comp1.vt.vt2.vt3.data: 
      comp1.vt.vt2.x: -1.0
      comp1.vt.vt2.y: -2.0
      comp1.z: 0.0
      comp2.derivative_exec_count: 0.0
      comp2.exec_count: 1.0
      comp2.itername: 1-comp2
      comp2.z: 1.0
      driver.workflow.itername: 1

'''

        print sout.getvalue()
        lines = sout.getvalue().split('\n')
        expected_lines = expected.split('\n')
        for index, line in enumerate(lines):
            if line.startswith('Case:'):
                for i in range(len(expected_lines)):
                    if expected_lines[i].startswith('   uuid:'):
                        self.assertTrue(lines[index+i].startswith('   uuid:'))
                    elif expected_lines[i].startswith('   parent_uuid:'):
                        self.assertTrue(lines[index+i].startswith('   parent_uuid:'))
                    elif expected_lines[i].startswith('   timestamp:'):
                        self.assertTrue(lines[index+i].startswith('   timestamp:'))
                    else:
                        self.assertEqual(lines[index+i], expected_lines[i])
                break
        else:
            self.fail("couldn't find the expected Case")