Ejemplo n.º 1
1
    def __init__(self, db_name):

        super(DbView, self).__init__()
        self.setGeometry(50, 50, 800, 600)
        self.setWindowTitle("Database " + db_name)
        self.setWindowIcon(QIcon(':python-logo.png'))
        self.statusBar()

        # get first table
        self.conn = di.get_sqlite_connection(db_name)
        self.chunk_size = 10
        db_tables = dv.get_table_names(self.conn)
        data = dv.get_table(self.conn,
                            db_tables[0],
                            chunk_size=self.chunk_size)
        self.data_iterator = iter(data)
        self.df = next(self.data_iterator)

        # add a combobox to a toolbar
        combo_box = QComboBox()
        combo_box.insertItems(1, db_tables)
        combo_box.activated[str].connect(self.table_changed)

        # add a more button
        more_button = QPushButton("More")
        more_button.clicked.connect(self.more_clicked)

        # add a toolbar
        self.toolBar = self.addToolBar("table")
        self.toolBar.addWidget(QLabel("Table: "))
        self.toolBar.addWidget(combo_box)
        self.toolBar.addWidget(more_button)

        # add a TableView
        self.table_view = QTableView(self)
        model = md.PandasModel(self.df)
        self.table_view.setModel(model)
        self.setCentralWidget(self.table_view)

        self.show()
Ejemplo n.º 2
0
class Output(TestCase):
    # solve test problem
    config = mb.get_config('test_model_config.json')
    instance = mb.build_instance(config)
    conn = di.get_sqlite_connection()
    lookup = dv.LookupTables(conn)
    opt = pe.SolverFactory("glpk")
    instance.Cost.deactivate()
    instance.Environmental_Cost_Impact.deactivate()
    opt.solve(instance)

    def test_get_sets_frame(self):
        sets_df = mo.get_sets_frame(self.instance)
        self.assertGreater(len(sets_df), 0)

    def test_get_entity(self):
        # output the variables with and without units
        for v in self.instance.component_objects(pe.Var):
            v_dfr = mo.get_entity(v)
            self.assertIsInstance(v_dfr, pd.DataFrame)
            if len(v_dfr) > 0:
                self.assertGreater(len(v_dfr), 0)
            u_dfr = mo.get_entity(v, self.lookup, units=True)
            self.assertIsInstance(u_dfr, pd.DataFrame)
            if len(u_dfr) > 0:
                self.assertGreater(len(u_dfr), 0)

        # name the unit set for lookup
        flow_dfr = mo.get_entity(self.instance.Flow, self.lookup, units=['P_m'])
        self.assertGreater(len(flow_dfr), 0)

    def test_objectives(self):
        # do the optimisation for each objective
        for i, o in enumerate(self.instance.component_objects(pe.Objective)):
            for j, oo in enumerate(self.instance.component_objects(pe.Objective)):
                if i == j:
                    oo.activate()
                else:
                    oo.deactivate()
            opt = pe.SolverFactory("glpk")
            opt.solve(self.instance)
            o_dfr = mo.get_entity(o)
            self.assertGreater(len(o_dfr), 0)
            ou_dfr = mo.get_entity(o, self.lookup, units=True)
            self.assertGreater(len(ou_dfr), 0)

    def test_get_onset_names(self):
        l = list()
        for o in chain(self.instance.component_objects(pe.Var), self.instance.component_objects(pe.Param)):
            sets = mo.get_onset_names(o)
            l.append(sets)
        self.assertGreater(len(sets), 0)
Ejemplo n.º 3
0
class TestLookupTables(TestCase):
    conn = di.get_sqlite_connection()
    lookup = dv.LookupTables(conn)

    def test_get(self):
        pm = self.lookup.get('P_m')
        self.assertEqual(pm.shape[1], 2)
        kpi = self.lookup.get('KPI')
        self.assertEqual(kpi.shape[1], 3)

    def test_get_single_column(self):
        pm = self.lookup.get_single_column('P_m')
        self.assertEqual(pm.shape[1], 1)
Ejemplo n.º 4
0
class TestWidgets(TestCase):
    conn = di.get_sqlite_connection()
    lookup = dv.get_lookup_tables(conn)

    def test_get_set(self):
        dfr = TestWidgets.lookup['flows']
        set_qgw = mw.get_set(dfr)
        self.assertEqual(len(set_qgw.df), 0)

    def test_get_sets(self):
        lookups = {
            'F_m': self.lookup['flows'],
            'P_m': self.lookup['processes'],
            'KPI': self.lookup['KPI']
        }
        with open('test_model_config.json') as fp:
            config = json.load(fp)
        sets_file = NamedTemporaryFile(suffix='.json', delete=False)
        with open(sets_file.name, 'w') as fp:
            json.dump(config['sets'], fp)
        spec = ms.GeneralSpecification()
        vbox, tab = mw.get_sets(spec, lookups, sets_file.name)
        sets_file.close()
        self.assertEqual(len(vbox.children), 2)
Ejemplo n.º 5
0
    def test_model_solve(self):

        setting = mqu.system_settings(testing=True)
        config_path = setting['config_path'].joinpath('Lemon_Toy_Model.json')
        config = mb.get_config(config_path)
        instance = mb.build_instance(config)

        # get lookups from db
        conn = di.get_sqlite_connection()
        lookup = dv.LookupTables(conn)

        # setup a model run
        model_solve = ms.ModelSolve(lookup)
        model_solve.concrete_model = instance
        model_solve.resize(800, 600)
        model_solve.show()

        # run the model
        QTest.mouseClick(model_solve.run_button, Qt.LeftButton)

        if 'IGNORE_EXEC' not in os.environ:
            app.exec()

        self.assertIsInstance(model_solve, ms.ModelSolve)
Ejemplo n.º 6
0
setting = mqu.system_settings(testing=True)
config_path = setting['config_path'].joinpath('Lemon_Toy_Model.json')
config = mb.get_config(config_path)
spec = mb.create_specification(config['specification'])
instance = mb.build_instance(config)
for i, obj in enumerate(instance.component_objects(pe.Objective)):
    # activate first objective
    if i == 0:
        obj.activate()
    else:
        obj.deactivate()
opt = pe.SolverFactory("glpk")
opt.solve(instance)

# get lookups from db
conn = di.get_sqlite_connection()
lookup = dv.LookupTables(conn)


class ModelViewManagerTest(TestCase):
    def test_init(self):

        # setup a model viewer
        model_view_manager = mv.ModelViewManager(lookup, spec)
        model_view_manager.concrete_model = instance

        model_view_manager.resize(800, 600)
        model_view_manager.show()

        if 'IGNORE_EXEC' not in os.environ:
            app.exec()
Ejemplo n.º 7
0
class TestSQLGenerator(TestCase):
    conn = di.get_sqlite_connection()

    # conn1 = di.get_sqlite_connection('C:/data/openlca/sqlite/system/CSV_juice_ecoinvent_36_apos_lci_20200206_20201029-102818.sqlite')

    def test_build_process_elementary_flow(self):
        p_m = ['f22f5f6e-1bdc-3cb5-8f48-8a04d8f9b768']
        p_t = ['44ad59ca-4fe0-394c-a6d9-5dea68783c23']
        pe_sql = sq.build_process_elementary_flow(process_ref_ids=p_m + p_t)
        z = pd.read_sql(pe_sql, self.conn)
        self.assertGreater(len(z), 0)

    def test_build_location(self):
        p_m = ['64867712-23c4-3be5-a50e-3631e74571a6']
        loc_sql = sq.build_location(process_ref_ids=p_m)
        z = pd.read_sql(loc_sql, self.conn)
        self.assertGreater(len(z), 0)

    # def test_build_product_flow(self):
    #     p_m = ['f22f5f6e-1bdc-3cb5-8f48-8a04d8f9b768']
    #     bp_sql = sq.build_product_flow(process_ref_ids=p_m)
    #     z = pd.read_sql(bp_sql, self.conn)
    #     self.assertGreater(len(z), 0)

    def test_build_product_flow_cost(self):
        p_m = ['f22f5f6e-1bdc-3cb5-8f48-8a04d8f9b768']
        pfc_sql = sq.build_product_flow_cost(process_ref_ids=p_m, time=['t0'])
        z = pd.read_sql(pfc_sql, self.conn)
        self.assertGreater(len(z), 0)

    def test_build_impact_category_elementary_flow(self):
        # ReCiPe Midpoint (H) V1.13 / climate change - GWP100 from 3.6 methods
        kpi = ['42b1e910-3bd2-3741-85ce-a3966798440b']
        ice_sql = sq.build_impact_category_elementary_flow(ref_ids=kpi)
        z = pd.read_sql(ice_sql, self.conn)
        self.assertGreater(len(z), 0)
        # no environmental kpi, but costs
        kpi = []
        ice_sql = sq.build_impact_category_elementary_flow(ref_ids=kpi)
        z = pd.read_sql(ice_sql, self.conn)
        self.assertEqual(len(z), 0)

    # def test_build_flow_reference_unit(self):
    #     f = ['e6aad2de-0b1b-49c3-a0c4-797ba34d87e5', '774bc814-70cf-4389-8bf8-e6435174c72e']
    #     fc_sql = sq.build_flow_reference_unit(flow_ref_ids=f)
    #     x = pd.read_sql(fc_sql, self.conn)
    #     fc_sql = sq.build_flow_reference_unit()
    #     y = pd.read_sql(fc_sql, self.conn)
    #     unique_units = y.UNITS_NAME.unique()
    #     self.assertEqual(len(unique_units), 15)

    # def test_build_product_flow_unit(self):
    #     f = ['e6aad2de-0b1b-49c3-a0c4-797ba34d87e5', '774bc814-70cf-4389-8bf8-e6435174c72e']
    #     fc_sql = sq.build_product_flow_unit(flow_ref_ids=f)
    #     x = pd.read_sql(fc_sql, self.conn)
    #     fc_sql = sq.build_product_flow_unit()
    #     y = pd.read_sql(fc_sql, self.conn)
    #     unique_units = y.UNITS_NAME.unique()
    #     self.assertEqual(len(unique_units), 19)

    def test_build_product_flow_units(self):
        p = ['f22f5f6e-1bdc-3cb5-8f48-8a04d8f9b768']
        pfu_sql = sq.build_product_flow_units(process_ref_ids=p)
        z = pd.read_sql(pfu_sql, self.conn)
        self.assertEqual(len(z), 1)
Ejemplo n.º 8
0
class DataView(TestCase):
    # testing from full default db
    conn = di.get_sqlite_connection()

    def test_get_ids(self):
        dfr = dv.get_ids(DataView.conn,
                         ref_ids=['64867712-23c4-3be5-a50e-3631e74571a6'],
                         table_name='TBL_PROCESSES')
        self.assertGreater(len(dfr), 0)

    def test_get_ref_ids(self):
        dfr = dv.get_ref_ids(DataView.conn,
                             ids=[4578980],
                             table_name='TBL_PROCESSES')
        self.assertGreater(len(dfr), 0)

    def test_get_elementary_flows(self):
        elementary_flows_dfr = dv.get_elementary_flows(DataView.conn)
        self.assertGreater(len(elementary_flows_dfr), 0)

    def test_get_impact_categories(self):
        impact_categories_dfr = dv.get_impact_categories(
            DataView.conn, category_name=['Climate change - GWP100%'])
        self.assertGreater(len(impact_categories_dfr), 0)

    def test_get_table(self):
        table_dfr = dv.get_table(DataView.conn, 'TBL_IMPACT_METHODS')
        self.assertGreater(len(table_dfr), 0)

    def test_get_impact_category_elementary_flow(self):
        impact_element_dfr = dv.get_impact_category_elementary_flow(
            DataView.conn, ref_ids=['42b1e910-3bd2-3741-85ce-a3966798440b'])
        self.assertGreater(len(impact_element_dfr), 0)

    def test_get_process_elementary_flow(self):
        process_element_dfr = dv.get_process_elementary_flow(
            DataView.conn, ref_ids=['64867712-23c4-3be5-a50e-3631e74571a6'])
        self.assertGreater(len(process_element_dfr), 0)

    def test_get_processes(self):
        process_dfr = dv.get_processes(DataView.conn,
                                       name=['lemon production%'],
                                       location=["Spain"])
        self.assertGreater(len(process_dfr), 0)

    def test_get_process_locations(self):
        process_dfr = dv.get_process_locations(
            DataView.conn, ref_ids=['64867712-23c4-3be5-a50e-3631e74571a6'])
        self.assertGreater(len(process_dfr), 0)

    def test_get_process_product_flow(self):
        process_flow1_dfr = dv.get_process_product_flow(
            DataView.conn,
            process_ref_ids='64867712-23c4-3be5-a50e-3631e74571a6')
        self.assertEqual(process_flow1_dfr.shape, (1, 5))
        process_flow2_dfr = dv.get_process_product_flow(
            DataView.conn,
            process_ref_ids=['64867712-23c4-3be5-a50e-3631e74571a6'])
        self.assertEqual(process_flow1_dfr.shape, process_flow2_dfr.shape)

    def test_get_ref_id_dicts(self):
        d = dv.get_ref_id_dicts(DataView.conn, {
            'flows': 'TBL_FLOWS',
            'processes': 'TBL_PROCESSES'
        })
        self.assertGreater(len(d), 0)

    def test_get_lookup_tables(self):
        lookup = dv.get_lookup_tables(DataView.conn)
        self.assertGreater(len(lookup), 0)

    def test_get_process_product_flow_costs(self):
        ref_id = ['cd177b7d-e908-3e69-b40c-4827b4abaa4d']
        costs = dv.get_process_product_flow_costs(DataView.conn,
                                                  process_ref_ids=ref_id)
        self.assertGreater(len(costs), 0)

    def test_get_process_product_flow_units(self):
        ref_id = ['cd177b7d-e908-3e69-b40c-4827b4abaa4d']
        units = dv.get_process_product_flow_units(DataView.conn,
                                                  process_ref_ids=ref_id)
        self.assertGreater(len(units), 0)