Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move print to logger #61

Merged
merged 1 commit into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.



Expand Down
35 changes: 20 additions & 15 deletions cp2kdata/dpdata_plugin.py
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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"
)


Expand All @@ -28,7 +33,7 @@
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 = {
Expand Down Expand Up @@ -61,11 +66,11 @@
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


Expand All @@ -81,7 +86,7 @@
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",
Expand Down Expand Up @@ -130,26 +135,26 @@
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.")

Check warning on line 151 in cp2kdata/dpdata_plugin.py

View check run for this annotation

Codecov / codecov/patch

cp2kdata/dpdata_plugin.py#L151

Added line #L151 was not covered by tests
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.")

Check warning on line 154 in cp2kdata/dpdata_plugin.py

View check run for this annotation

Codecov / codecov/patch

cp2kdata/dpdata_plugin.py#L154

Added line #L154 was not covered by tests
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
Expand Down
13 changes: 13 additions & 0 deletions cp2kdata/log.py
Original file line number Diff line number Diff line change
@@ -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)
10 changes: 7 additions & 3 deletions cp2kdata/utils.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion requirements.txt

This file was deleted.

Loading