From f76e26c79e0dbd7155e8f85c8ebd99abb99b3675 Mon Sep 17 00:00:00 2001 From: robinzyb <38876805+robinzyb@users.noreply.github.com> Date: Thu, 27 Jun 2024 06:18:52 +0200 Subject: [PATCH] move print to logger --- README.md | 3 ++- cp2kdata/dpdata_plugin.py | 35 ++++++++++++++++++++--------------- cp2kdata/log.py | 13 +++++++++++++ cp2kdata/utils.py | 10 +++++++--- requirements.txt | 1 - 5 files changed, 42 insertions(+), 20 deletions(-) create mode 100644 cp2kdata/log.py delete mode 100644 requirements.txt diff --git a/README.md b/README.md index 13c9826..1822181 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,8 @@ pip install . # Feature Request Any advice is welcome. If you would like to request a new feature, please open an issue in github and upload example input and output files. - +# Developer +set environment variable CP2KDATA_LOG_LEVEL to DEBUG to get the complete information. diff --git a/cp2kdata/dpdata_plugin.py b/cp2kdata/dpdata_plugin.py index 9053371..b59932b 100644 --- a/cp2kdata/dpdata_plugin.py +++ b/cp2kdata/dpdata_plugin.py @@ -1,10 +1,15 @@ +from .log import get_logger +import numpy as np + from dpdata.unit import EnergyConversion, LengthConversion, ForceConversion, PressureConversion from dpdata.format import Format + from . import Cp2kOutput from .block_parser.converge import parse_e_f_converge -import numpy as np -from cp2kdata.block_parser.md_xyz import parse_pos_xyz_from_wannier -import os +from .block_parser.md_xyz import parse_pos_xyz_from_wannier + +logger = get_logger(__name__) + AU_TO_EV = EnergyConversion("hartree", "eV").value() AU_TO_ANG = LengthConversion("bohr", "angstrom").value() @@ -13,9 +18,9 @@ WRAPPER = "--- You are parsing data using package Cp2kData ---" VIRIAL_WRN = ( - "Virial Parsing using cp2kdata as plug in for dpdata\n" - "was not multiplied by volume before cp2kdata v0.6.4\n" - "please check the cp2kdata version and the virial.npy\n" + "Virial Parsing using cp2kdata as plug in for dpdata " + "was not multiplied by volume before cp2kdata v0.6.4 " + "please check the cp2kdata version and the virial.npy" ) @@ -28,7 +33,7 @@ def from_labeled_system(self, file_name, **kwargs): true_symbols = kwargs.get('true_symbols', False) # -- start parsing -- - print(WRAPPER) + logger.debug(WRAPPER) converge_info = parse_e_f_converge(file_name) if not converge_info.converge: data = { @@ -61,11 +66,11 @@ def from_labeled_system(self, file_name, **kwargs): data['forces'] = cp2k_e_f.atomic_forces_list * AU_TO_EV/AU_TO_ANG if cp2k_e_f.has_stress(): # note that virial = stress * volume - print(VIRIAL_WRN) + logger.warning(VIRIAL_WRN) volume = np.linalg.det(data['cells'][0]) data['virials'] = cp2k_e_f.stress_tensor_list*volume/EV_ANG_m3_TO_GPa - print(WRAPPER) + logger.debug(WRAPPER) return data @@ -81,7 +86,7 @@ def from_labeled_system(self, file_name, restart: bool=None, **kwargs): cp2k_output_name = kwargs.get('cp2k_output_name', None) # -- start parsing -- - print(WRAPPER) + logger.debug(WRAPPER) cp2kmd = Cp2kOutput(output_file=cp2k_output_name, run_type="MD", @@ -130,26 +135,26 @@ def from_labeled_system(self, file_name, restart: bool=None, **kwargs): data['forces'] = cp2kmd.atomic_forces_list * AU_TO_EV/AU_TO_ANG if cp2kmd.has_stress(): # note that virial = stress * volume - print(VIRIAL_WRN) + logger.warning(VIRIAL_WRN) # the shape of cells should be (num_frames, 3, 3) # the np.linalg.det() function can handle this and return (num_frames,) volumes = np.linalg.det(data['cells']) volumes = volumes[:, np.newaxis, np.newaxis] data['virials'] = cp2kmd.stress_tensor_list*volumes/EV_ANG_m3_TO_GPa - print(WRAPPER) + logger.debug(WRAPPER) return data def get_chemical_symbols_from_cp2kdata(cp2koutput, true_symbols): if cp2koutput.atomic_kind is None: - print("Missing the atomic kind informations, atom names are true chemical symbols.") + logger.debug("Missing the atomic kind informations, atom names are true chemical symbols.") chemical_symbols = cp2koutput.get_chemical_symbols() elif true_symbols: - print("You have manually true_symbols=True, atom names are true chemical symbols.") + logger.debug("You have manually true_symbols=True, atom names are true chemical symbols.") chemical_symbols = cp2koutput.get_chemical_symbols() else: - print("Atom names are fake chemical symbols as you set in cp2k input.") + logger.debug("Atom names are fake chemical symbols as you set in cp2k input.") chemical_symbols = cp2koutput.get_chemical_symbols_fake() chemical_symbols = np.array(chemical_symbols) return chemical_symbols diff --git a/cp2kdata/log.py b/cp2kdata/log.py new file mode 100644 index 0000000..da313fa --- /dev/null +++ b/cp2kdata/log.py @@ -0,0 +1,13 @@ +import logging +import os + +level_name = os.environ.get('CP2KDATA_LOG_LEVEL', 'INFO') +level = logging._nameToLevel.get(level_name, logging.INFO) + +# format to include timestamp and module +logging.basicConfig(format='%(asctime)s %(levelname)-8s %(name)-40s: %(message)s', level=level) +# suppress transitions logging +# logging.getLogger('transitions.core').setLevel(logging.WARNING) + +def get_logger(name=None): + return logging.getLogger(name) \ No newline at end of file diff --git a/cp2kdata/utils.py b/cp2kdata/utils.py index 720616d..95441bc 100644 --- a/cp2kdata/utils.py +++ b/cp2kdata/utils.py @@ -1,13 +1,17 @@ """ this script put misc function here. """ -from ase.geometry.analysis import Analysis -from ase.io import read, write #from cp2kdata import Cp2kOutput import numpy as np import os import shutil +from ase.geometry.analysis import Analysis +from ase.io import read, write + +from .log import get_logger + +logger = get_logger(__name__) # frequently used unit convertion au2eV = 27.211386245988 au2A = 0.529177210903 @@ -75,7 +79,7 @@ def printtbox(arg): print("--> CP2KDATA: {0}".format(arg)) def format_logger(info, filename): - print(f"Parsing {info:10} from {filename}") + logger.debug(f"Parsing {info:10} from {filename}") def file_content(file, num): # read a specific line of file or return the block diff --git a/requirements.txt b/requirements.txt deleted file mode 100644 index d6e1198..0000000 --- a/requirements.txt +++ /dev/null @@ -1 +0,0 @@ --e .