Пример #1
0
    def delete_parameter(self, proxy) -> None:
        """ Override the base method to include additional logic.

        If there are multiple `ActivityParameters` for a single activity, only
        delete the selected instance, otherwise use `bw.parameters.remove_from_group`
        to clear out the `ParameterizedExchanges` as well.
        """
        key = self.get_key(proxy)
        query = (ActivityParameter.select()
                 .where(ActivityParameter.database == key[0],
                        ActivityParameter.code == key[1]))

        if query.count() > 1:
            super().delete_parameter(proxy)
        else:
            act = bw.get_activity(key)
            group = self.get_current_group(proxy)
            bw.parameters.remove_from_group(group, act)
            # Also clear the group if there are no more parameters in it
            exists = (ActivityParameter.select()
                      .where(ActivityParameter.group == group).exists())
            if not exists:
                with bw.parameters.db.atomic():
                    Group.delete().where(Group.name == group).execute()

        bw.parameters.recalculate()
        signals.parameters_changed.emit()
Пример #2
0
def resetParams(db_name):
    """Reset project and activity parameters"""
    _param_registry().clear()
    ProjectParameter.delete().execute()
    ActivityParameter.delete().execute()
    DatabaseParameter.delete().execute()
    Group.delete().execute()
Пример #3
0
    def delete_parameter(self, parameter: ParameterBase) -> None:
        """ Remove the given parameter from the project.

        If there are multiple `ActivityParameters` for a single activity, only
        delete the selected instance, otherwise use `bw.parameters.remove_from_group`
        to clear out the `ParameterizedExchanges` as well.
        """
        if isinstance(parameter, ActivityParameter):
            db = parameter.database
            code = parameter.code
            amount = (ActivityParameter.select()
                      .where((ActivityParameter.database == db) &
                             (ActivityParameter.code == code))
                      .count())

            if amount > 1:
                with bw.parameters.db.atomic():
                    parameter.delete_instance()
            else:
                group = parameter.group
                act = bw.get_activity((db, code))
                bw.parameters.remove_from_group(group, act)
                # Also clear the group if there are no more parameters in it
                exists = (ActivityParameter.select()
                          .where(ActivityParameter.group == group).exists())
                if not exists:
                    with bw.parameters.db.atomic():
                        Group.delete().where(Group.name == group).execute()
        else:
            with bw.parameters.db.atomic():
                parameter.delete_instance()
        # After deleting things, recalculate and signal changes
        bw.parameters.recalculate()
        signals.parameters_changed.emit()
Пример #4
0
def add_example_database(overwrite=True):
    from ..importers.excel import (
        assign_only_product_as_production,
        convert_activity_parameters_to_list,
        convert_uncertainty_types_to_integers,
        csv_add_missing_exchanges_section,
        csv_drop_unknown,
        csv_numerize,
        csv_restore_booleans,
        csv_restore_tuples,
        drop_falsey_uncertainty_fields_but_keep_zeros,
        ExcelImporter,
        set_code_by_activity_hash,
        strip_biosphere_exc_locations,
    )

    if "Mobility example" in databases:
        if not overwrite:
            print("Example already imported, use `overwrite=True` to delete")
            return
        else:
            del databases["Mobility example"]
            if ("IPCC", "simple") in methods:
                del methods[("IPCC", "simple")]

    importer = ExcelImporter(
        os.path.join(dirpath, "examples",
                     "sample_parameterized_database.xlsx"))
    importer.strategies = [
        csv_restore_tuples,
        csv_restore_booleans,
        csv_numerize,
        csv_drop_unknown,
        csv_add_missing_exchanges_section,
        strip_biosphere_exc_locations,
        set_code_by_activity_hash,
        assign_only_product_as_production,
        drop_falsey_uncertainty_fields_but_keep_zeros,
        convert_uncertainty_types_to_integers,
        convert_activity_parameters_to_list,
    ]
    importer.apply_strategies()
    importer.match_database(fields=['name'])
    importer.write_database(activate_parameters=True)

    group = "Mobility exchanges"
    Group.delete().where(Group.name == group).execute()
    group = Group.create(name=group)

    for ds in Database("Mobility example"):
        parameters.add_exchanges_to_group(group, ds)

    parameters.recalculate()

    ipcc = Method(("IPCC", "simple"))
    ipcc.register()
    ipcc.write([(("Mobility example", "CO2"), 1)])
Пример #5
0
 def delete_database(self, name: str) -> None:
     ok = QtWidgets.QMessageBox.question(self.window, "Delete database?", (
         "Are you sure you want to delete database '{}'? It has {} activity datasets"
     ).format(name, bc.count_database_records(name)))
     if ok == QtWidgets.QMessageBox.Yes:
         project_settings.remove_db(name)
         del bw.databases[name]
         Group.delete().where(Group.name == name).execute()
         ProjectController.change_project(bw.projects.current, reload=True)
Пример #6
0
def resetParams(db_name=None):
    """Clear parameters in live memory (registry) and on disk.
    Clear either all params (project and all db params) or db params from a single database (if db_name provided)"""
    _param_registry().clear(db_name)

    if db_name is None:
        ProjectParameter.delete().execute()
        ActivityParameter.delete().execute()
        DatabaseParameter.delete().execute()
    else:
        ActivityParameter.delete().where(
            ActivityParameter.database == db_name).execute()
        DatabaseParameter.delete().where(
            DatabaseParameter.database == db_name).execute()
        Group.delete().execute()
Пример #7
0
 def clear_broken_activity_parameter(database: str, code: str, group: str) -> None:
     """Take the given information and attempt to remove all of the
     downstream parameter information.
     """
     with bw.parameters.db.atomic() as txn:
         bw.parameters.remove_exchanges_from_group(group, None, False)
         ActivityParameter.delete().where(
             ActivityParameter.database == database,
             ActivityParameter.code == code
         ).execute()
         # Do commit to ensure .exists() call does not include deleted params
         txn.commit()
         exists = (ActivityParameter.select()
                   .where(ActivityParameter.group == group)
                   .exists())
         if not exists:
             # Also clear Group if it is not in use anymore
             Group.delete().where(Group.name == group).execute()
Пример #8
0
 def delete_activity_parameter(key: tuple) -> None:
     """Remove all activity parameters and underlying exchange parameters
     for the given activity key.
     """
     query = (ActivityParameter
              .select(ActivityParameter.group)
              .where((ActivityParameter.database == key[0]) &
                     (ActivityParameter.code == key[1]))
              .tuples())
     if not query.exists():
         return
     groups = set(p[0] for p in query)
     for group in groups:
         bw.parameters.remove_from_group(group, key)
         exists = (ActivityParameter.select()
                   .where(ActivityParameter.group == group)
                   .exists())
         if not exists:
             Group.delete().where(Group.name == group).execute()
     bw.parameters.recalculate()
     signals.parameters_changed.emit()