forked from aimclub/BAMT
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* serialization fix * bug fix * mixtures were excluded from list of serizalization & bug with extensions fixed * log mod API * making selbst.ini removed * log API & docs * net plot for jupiter * Docs update Fromal style Co-authored-by: Jerzy Kamiński <[email protected]> * Code imprvm Getting rid of nested loops Co-authored-by: Jerzy Kamiński <[email protected]> * log update --------- Co-authored-by: Jerzy Kamiński <[email protected]>
- Loading branch information
1 parent
28685d0
commit 77ffcf6
Showing
8 changed files
with
199 additions
and
102 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,67 @@ | ||
import logging | ||
import logging.config | ||
import os | ||
import warnings | ||
|
||
from bamt.config import config | ||
|
||
log_file_path = config.get( | ||
"LOG", "log_conf_loc", fallback="log_conf_path is not defined" | ||
) | ||
|
||
if not os.path.isdir(os.path.join(os.path.expanduser("~"), "BAMT")): | ||
os.mkdir(os.path.join(os.path.expanduser("~"), "BAMT")) | ||
|
||
try: | ||
logging.config.fileConfig(log_file_path) | ||
except BaseException: | ||
log_file_path = os.path.join( | ||
os.path.dirname(os.path.abspath(__file__)), "logging.conf" | ||
) | ||
logging.config.fileConfig(log_file_path) | ||
warnings.warn( | ||
"Reading log path location from config file failed. Default location will be used instead." | ||
) | ||
|
||
logger_builder = logging.getLogger("builder") | ||
logger_network = logging.getLogger("network") | ||
logger_preprocessor = logging.getLogger("preprocessor") | ||
logger_nodes = logging.getLogger("nodes") | ||
logger_display = logging.getLogger("display") | ||
|
||
logging.captureWarnings(True) | ||
logger_warnings = logging.getLogger("py.warnings") | ||
|
||
|
||
class BamtLogger: | ||
def __init__(self): | ||
self.file_handler = False | ||
self.enabled = True | ||
self.base = os.path.join(os.path.dirname(os.path.abspath(__file__))) | ||
self.log_file_path = os.path.join(self.base, "logging.conf") | ||
logging.config.fileConfig(self.log_file_path) | ||
|
||
self.loggers = dict( | ||
logger_builder=logging.getLogger("builder"), | ||
logger_network=logging.getLogger("network"), | ||
logger_preprocessor=logging.getLogger("preprocessor"), | ||
logger_nodes=logging.getLogger("nodes"), | ||
logger_display=logging.getLogger("display"), | ||
) | ||
|
||
def has_handler(self, logger, handler_type): | ||
"""Check if a logger has a handler of a specific type.""" | ||
return any(isinstance(handler, handler_type) for handler in logger.handlers) | ||
|
||
def remove_handler_type(self, logger, handler_type): | ||
"""Remove all handlers of a specific type from a logger.""" | ||
for handler in logger.handlers[:]: | ||
if isinstance(handler, handler_type): | ||
logger.removeHandler(handler) | ||
|
||
def switch_console_out(self, value: bool): | ||
""" | ||
Turn off console output from logger. | ||
""" | ||
assert isinstance(value, bool) | ||
handler_class = logging.StreamHandler if not value else logging.NullHandler | ||
|
||
for logger in self.loggers.values(): | ||
if self.has_handler(logger, handler_class): | ||
self.remove_handler_type(logger, handler_class) | ||
logger.addHandler(logging.NullHandler() if not value else logging.root.handlers[0]) | ||
|
||
def switch_file_out(self, value: bool, log_file: str): | ||
""" | ||
Send all messages in file | ||
""" | ||
assert isinstance(value, bool) | ||
|
||
for logger in self.loggers.values(): | ||
if value: | ||
file_handler = logging.FileHandler(log_file, mode="a") | ||
file_handler.setFormatter(logging.root.handlers[0].formatter) | ||
logger.addHandler(file_handler) | ||
else: | ||
self.remove_handler_type(logger, logging.FileHandler) | ||
|
||
|
||
bamt_logger = BamtLogger() | ||
|
||
( | ||
logger_builder, | ||
logger_network, | ||
logger_preprocessor, | ||
logger_nodes, | ||
logger_display, | ||
) = list(bamt_logger.loggers.values()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
Setting up loggers in BAMT | ||
=============================== | ||
|
||
Used imports: | ||
|
||
.. code-block:: python | ||
import pandas as pd | ||
from sklearn import preprocessing as pp | ||
import bamt.preprocessors as preprocessors | ||
from bamt.networks import ContinuousBN | ||
from bamt.log import bamt_logger | ||
There are 2 methods to use: ``switch_console_out`` and ``switch_file_out`` of ``bamt_logger``. | ||
|
||
By default, bamt will print out messages in console and will not use any log files. | ||
|
||
How to turn off/on console output? | ||
_______________________________ | ||
|
||
Let's consider this example: | ||
|
||
.. code-block:: python | ||
def learn_bn(): | ||
hack_data = pd.read_csv("data/real data/hack_processed_with_rf.csv")[ | ||
[ | ||
"Tectonic regime", | ||
"Period", | ||
"Lithology", | ||
"Structural setting", | ||
"Gross", | ||
"Netpay", | ||
"Porosity", | ||
"Permeability", | ||
"Depth", | ||
] | ||
].dropna() | ||
encoder = pp.LabelEncoder() | ||
discretizer = pp.KBinsDiscretizer(n_bins=5, encode="ordinal", strategy="quantile") | ||
p = preprocessors.Preprocessor([("encoder", encoder), ("discretizer", discretizer)]) | ||
discretized_data, est = p.apply(hack_data) | ||
bn = ContinuousBN() | ||
info = p.info | ||
bn.add_nodes(info) # here you will get an error | ||
learn_bn() | ||
# The error: | ||
# 2023-12-14 16:20:05,010 | ERROR | base.py-add_nodes-0090 | Continuous BN does not support discrete data | ||
Remove output: | ||
|
||
.. code-block:: python | ||
bamt_logger.switch_console_out(False) | ||
learn_bn() # only KeyError from Python | ||
After this you will no longer receive messages from all loggers of BAMT. | ||
|
||
To revert changes just use: | ||
|
||
.. code-block:: python | ||
bamt_logger.switch_console_out(True) | ||
learn_bn() | ||
# return | ||
# 2023-12-14 16:20:05,010 | ERROR | base.py-add_nodes-0090 | Continuous BN does not support discrete data | ||
How to turn on/off log files for BAMT? | ||
______________________________________ | ||
|
||
In order to redirect errors to log file: | ||
|
||
.. code-block:: python | ||
bamt_logger.switch_file_out(True, | ||
log_file="<absolute/path/to/my_log.log>") # only absolute path | ||
learn_bn() | ||
# log file | ||
# 2023-12-14 16:34:23,414 | ERROR | base.py-add_nodes-0090 | Continuous BN does not support discrete data | ||
To revert this (it will not delete log created before): | ||
|
||
.. code-block:: python | ||
bamt_logger.switch_file_out(False) # only absolute path | ||
learn_bn() | ||
# log file: no new messages. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters