Skip to content

Commit

Permalink
Merge pull request #1336 from mrvisscher/its_the_final_logger
Browse files Browse the repository at this point in the history
Logging changes
  • Loading branch information
mrvisscher authored Aug 9, 2024
2 parents 8e1d44c + 1f6a3f8 commit f28bdbd
Show file tree
Hide file tree
Showing 51 changed files with 230 additions and 184 deletions.
6 changes: 5 additions & 1 deletion activity_browser/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# -*- coding: utf-8 -*-
import sys
from logging import getLogger


from .logger import exception_hook, log, log_file_location
from .logger import log_file_location, setup_ab_logging
from .mod import bw2data
from .application import application
from .signals import signals
Expand All @@ -12,6 +13,8 @@
from .plugin import Plugin
from .controllers import *

log = getLogger(__name__)


def load_settings() -> None:
if ab_settings.settings:
Expand All @@ -22,6 +25,7 @@ def load_settings() -> None:


def run_activity_browser():
setup_ab_logging()
log.info(f"Activity Browser version: {version}")
if log_file_location:
log.info(f"The log file can be found at {log_file_location}")
Expand Down
6 changes: 4 additions & 2 deletions activity_browser/actions/calculation_setup/cs_delete.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import traceback
from logging import getLogger

from PySide2 import QtWidgets

from activity_browser import application, log, signals
from activity_browser import application, signals
from activity_browser.actions.base import ABAction, exception_dialogs
from activity_browser.mod import bw2data as bd
from activity_browser.ui.icons import qicons

log = getLogger(__name__)


class CSDelete(ABAction):
"""
Expand Down
8 changes: 5 additions & 3 deletions activity_browser/actions/calculation_setup/cs_duplicate.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from typing import Callable, Union
from logging import getLogger

from PySide2 import QtCore, QtWidgets
from PySide2 import QtWidgets

from activity_browser import application, log, signals
from activity_browser import application, signals
from activity_browser.actions.base import ABAction, exception_dialogs
from activity_browser.mod import bw2data as bd
from activity_browser.ui.icons import qicons

log = getLogger(__name__)


class CSDuplicate(ABAction):
"""
Expand Down
6 changes: 5 additions & 1 deletion activity_browser/actions/calculation_setup/cs_new.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
from logging import getLogger

from PySide2 import QtWidgets

from activity_browser import application, log, signals
from activity_browser import application, signals
from activity_browser.actions.base import ABAction, exception_dialogs
from activity_browser.mod import bw2data as bd
from activity_browser.ui.icons import qicons

log = getLogger(__name__)


class CSNew(ABAction):
"""
Expand Down
6 changes: 5 additions & 1 deletion activity_browser/actions/calculation_setup/cs_rename.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
from logging import getLogger

from PySide2 import QtWidgets

from activity_browser import application, log, signals
from activity_browser import application, signals
from activity_browser.actions.base import ABAction, exception_dialogs
from activity_browser.mod import bw2data as bd
from activity_browser.ui.icons import qicons

log = getLogger(__name__)


class CSRename(ABAction):
"""
Expand Down
5 changes: 4 additions & 1 deletion activity_browser/actions/method/method_delete.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
from typing import List
from logging import getLogger

from PySide2 import QtWidgets

from activity_browser import application, log
from activity_browser import application
from activity_browser.actions.base import ABAction, exception_dialogs
from activity_browser.mod import bw2data as bd
from activity_browser.ui.icons import qicons

log = getLogger(__name__)


class MethodDelete(ABAction):
"""
Expand Down
5 changes: 4 additions & 1 deletion activity_browser/actions/method/method_duplicate.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from typing import List
from logging import getLogger

from activity_browser import application, log
from activity_browser import application
from activity_browser.actions.base import ABAction, exception_dialogs
from activity_browser.mod import bw2data as bd
from activity_browser.ui.icons import qicons
from activity_browser.ui.widgets import TupleNameDialog

log = getLogger(__name__)


class MethodDuplicate(ABAction):
"""
Expand Down
5 changes: 4 additions & 1 deletion activity_browser/actions/project/project_export.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import os
import json
import tarfile
from logging import getLogger

from PySide2 import QtWidgets, QtCore

from activity_browser import application, log
from activity_browser import application
from activity_browser.mod import bw2data as bd
from activity_browser.actions.base import ABAction, exception_dialogs
from activity_browser.ui.threading import ABThread

log = getLogger(__name__)


class ProjectExport(ABAction):
"""
Expand Down
6 changes: 4 additions & 2 deletions activity_browser/actions/project/project_import.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import os
import codecs
import json
import tarfile
from logging import getLogger

from PySide2 import QtWidgets, QtCore
from bw2io import backup

from activity_browser import application, log
from activity_browser import application
from activity_browser.mod import bw2data as bd
from activity_browser.actions.base import ABAction, exception_dialogs
from activity_browser.ui.icons import qicons
from activity_browser.ui.threading import ABThread

log = getLogger(__name__)


class ProjectImport(ABAction):
"""
Expand Down
5 changes: 4 additions & 1 deletion activity_browser/actions/project/project_switch.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from activity_browser import log
from logging import getLogger

from activity_browser.actions.base import ABAction, exception_dialogs
from activity_browser.mod import bw2data as bd

log = getLogger(__name__)


class ProjectSwitch(ABAction):
"""
Expand Down
3 changes: 2 additions & 1 deletion activity_browser/application.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# -*- coding: utf-8 -*-
import os
from logging import getLogger

from PySide2.QtCore import QCoreApplication, QObject, QSysInfo, Qt
from PySide2.QtWidgets import QApplication

from activity_browser import log
log = getLogger(__name__)


class ABApplication(QApplication):
Expand Down
8 changes: 5 additions & 3 deletions activity_browser/bwutils/calculations.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# -*- coding: utf-8 -*-
from logging import getLogger

from bw2calc.errors import BW2CalcError
from PySide2.QtWidgets import QApplication

from activity_browser import log

from ..bwutils import (MLCA, Contributions, MonteCarloLCA,
SuperstructureContributions, SuperstructureMLCA)
from .errors import CriticalCalculationError, ScenarioExchangeNotFoundError

log = getLogger(__name__)


def do_LCA_calculations(data: dict):
"""Perform the MLCA calculation."""
Expand Down Expand Up @@ -49,7 +51,7 @@ def do_LCA_calculations(data: dict):
QApplication.restoreOverrideCursor()
raise CriticalCalculationError
else:
log.error("Calculation type must be: simple or scenario. Given:", cs_name)
log.error(f"Calculation type must be: simple or scenario. Given: {cs_name}")
raise ValueError

mlca.calculate()
Expand Down
6 changes: 4 additions & 2 deletions activity_browser/bwutils/commontasks.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import hashlib
import textwrap
from logging import getLogger

import arrow

from activity_browser import log
from activity_browser.mod import bw2data as bd

from .metadata import AB_metadata

log = getLogger(__name__)

"""
bwutils is a collection of methods that build upon brightway2 and are generic enough to provide here so that we avoid
re-typing the same code in different parts of the Activity Browser.
Expand Down Expand Up @@ -94,7 +96,7 @@ def cleanup_deleted_bw_projects() -> None:
NOTE: This cannot be done from within the AB.
"""
n_dir = bd.projects.purge_deleted_directories()
log.info("Deleted {} unused project directories!".format(n_dir))
log.info(f"Deleted {n_dir} unused project directories!")


# Database
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import os
from zipfile import ZipFile
from logging import getLogger

from bw2io.importers import Ecospold2BiosphereImporter
from bw2io.importers.ecospold2_biosphere import EMISSIONS_CATEGORIES
from lxml import objectify

from activity_browser import log
from activity_browser.mod import bw2data as bd

from ...info import __ei_versions__
from ...utils import sort_semantic_versions

log = getLogger(__name__)


def create_default_biosphere3(version) -> None:
"""Reimplementation of bw.create_default_biosphere3 to allow import from older biosphere versions."""
Expand Down
17 changes: 9 additions & 8 deletions activity_browser/bwutils/metadata.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
# -*- coding: utf-8 -*-
from logging import getLogger

import numpy as np
import pandas as pd
from bw2data.errors import UnknownObject

import activity_browser.bwutils.commontasks as bc
from activity_browser import log
from activity_browser.mod import bw2data as bd
from activity_browser.mod.bw2data.backends import ActivityDataset

# todo: extend store over several projects

log = getLogger(__name__)


def list_to_tuple(x) -> tuple:
return tuple(x) if isinstance(x, list) else x
Expand Down Expand Up @@ -74,15 +77,13 @@ def add_metadata(self, db_names_list: list) -> None:
dfs = list()
dfs.append(self.dataframe)
log.debug(
"Current shape and databases in the MetaDataStore:",
self.dataframe.shape,
self.databases,
f"Current shape and databases in the MetaDataStore: {self.dataframe.shape} {self.databases}"
)
for db_name in new:
if db_name not in bd.databases:
raise ValueError("This database does not exist:", db_name)

log.debug("Adding:", db_name)
log.debug(f"Adding: {db_name}")
self.databases.add(db_name)

# make a temporary DataFrame and index it by ('database', 'code') (like all brightway activities)
Expand Down Expand Up @@ -127,7 +128,7 @@ def update_metadata(self, key: tuple) -> None:
) # if this does not work, it has been deleted (see except:).
except (UnknownObject, ActivityDataset.DoesNotExist):
# Situation 1: activity has been deleted (metadata needs to be deleted)
log.debug("Deleting activity from metadata:", key)
log.debug(f"Deleting activity from metadata: {key}")
self.dataframe.drop(key, inplace=True, errors="ignore")
# print('Dimensions of the Metadata:', self.dataframe.shape)
return
Expand All @@ -140,7 +141,7 @@ def update_metadata(self, key: tuple) -> None:
if (
key in self.dataframe.index
): # Situation 2: activity has been modified (metadata needs to be updated)
log.debug("Updating activity in metadata: ", act, key)
log.debug(f"Updating activity in metadata: {key}")
for col in self.dataframe.columns:
if col in self.CLASSIFICATION_SYSTEMS:
# update classification data
Expand All @@ -153,7 +154,7 @@ def update_metadata(self, key: tuple) -> None:
self.dataframe.at[key, 'key'] = act.key

else: # Situation 3: Activity has been added to database (metadata needs to be generated)
log.debug('Adding activity to metadata:', act, key)
log.debug(f'Adding activity to metadata: {key}')
df_new = pd.DataFrame([act.as_dict()], index=pd.MultiIndex.from_tuples([act.key]))
df_new['key'] = [act.key]
if act.get('classifications', False): # add classification data if present
Expand Down
19 changes: 8 additions & 11 deletions activity_browser/bwutils/montecarlo.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
from collections import defaultdict
from time import time
from typing import Optional, Union
from logging import getLogger

import bw2calc as bc
import numpy as np
import pandas as pd
from stats_arrays import MCRandomNumberGenerator

from activity_browser import log
from activity_browser.mod import bw2data as bd

from .manager import MonteCarloParameterManager

log = getLogger(__name__)


class MonteCarloLCA(object):
"""A Monte Carlo LCA for multiple reference flows and methods loaded from a calculation setup."""
Expand Down Expand Up @@ -243,12 +245,8 @@ def calculate(self, iterations=10, seed: int = None, **kwargs):
self.results[iteration, row, col] = self.lca.score

log.info(
"Monte Carlo LCA: finished {} iterations for {} reference flows and {} methods in {} seconds.".format(
iterations,
len(self.func_units),
len(self.methods),
np.round(time() - start, 2),
)
f"Monte Carlo LCA: finished {iterations} iterations for {len(self.func_units)} reference flows and "
f"{len(self.methods)} methods in {np.round(time() - start, 2)} seconds."
)

@property
Expand All @@ -270,10 +268,10 @@ def get_results_by(self, act_key=None, method=None):

if act_key:
act_index = self.activity_index.get(act_key)
log.info("Activity key provided:", act_key, act_index)
log.info(f"Activity key provided: {act_key} {act_index}")
if method:
method_index = self.method_index.get(method)
log.info("Method provided", method, method_index)
log.info(f"Method provided: {method} {method_index}")

if not act_key and not method:
return self.results
Expand All @@ -282,7 +280,6 @@ def get_results_by(self, act_key=None, method=None):
elif method and not act_key:
return np.squeeze(self.results[:, :, method_index])
elif method and act_key:
log.info(act_index, method_index)
return np.squeeze(self.results[:, act_index, method_index])

def get_results_dataframe(self, act_key=None, method=None, labelled=True):
Expand Down Expand Up @@ -333,7 +330,7 @@ def get_labels(
def perform_MonteCarlo_LCA(project="default", cs_name=None, iterations=10):
"""Performs Monte Carlo LCA based on a calculation setup and returns the
Monte Carlo LCA object."""
log.info("-- Monte Carlo LCA --\n Project:", project, "CS:", cs_name)
log.info(f"-- Monte Carlo LCA --\n Project: {project} CS: {cs_name}")
bd.projects.set_current(project)

# perform Monte Carlo simulation
Expand Down
Loading

0 comments on commit f28bdbd

Please sign in to comment.