From 2b9301987499d6dbdcb820db0d237dbb0d5e6b45 Mon Sep 17 00:00:00 2001 From: Oleg Peil Date: Fri, 24 Mar 2023 17:36:35 +0100 Subject: [PATCH] (style) add proper logging for warnings and debug info --- .../converters/plovasp/converter.py | 30 +++++++++++++ .../converters/plovasp/proj_shell.py | 44 +++++++++++-------- .../converters/plovasp/vaspio.py | 18 +++++--- 3 files changed, 67 insertions(+), 25 deletions(-) diff --git a/python/triqs_dft_tools/converters/plovasp/converter.py b/python/triqs_dft_tools/converters/plovasp/converter.py index b2c97548..ad32bf50 100644 --- a/python/triqs_dft_tools/converters/plovasp/converter.py +++ b/python/triqs_dft_tools/converters/plovasp/converter.py @@ -40,6 +40,36 @@ from . import vaspio from .inpconf import ConfigParameters from .elstruct import ElectronicStructure from .plotools import generate_plo, output_as_text +import logging + +class PloFormatter(logging.Formatter): + def format(self, record): + # Save the original format + _style = self._style + + # Customized WARNING format + if record.levelno == logging.WARNING: + self._style = logging.PercentStyle("\n!!! WARNING !!!: %(msg)s\n") + + result = super().format(record) + + # Restore the original format + self._style = _style + + return result + +# Uncomment this to get extra output +#logging.basicConfig(level=logging.DEBUG) + +# Main logger from which all other loggers should be inherited +main_log = logging.getLogger('plovasp') +main_log.propagate = False + +handler = logging.StreamHandler(sys.stdout) +formatter = PloFormatter("[%(levelname)s]:[%(name)s]: %(message)s") +handler.setFormatter(formatter) +main_log.addHandler(handler) + def generate_and_output_as_text(conf_filename, vasp_dir): """ diff --git a/python/triqs_dft_tools/converters/plovasp/proj_shell.py b/python/triqs_dft_tools/converters/plovasp/proj_shell.py index 782e3929..eca79bfc 100644 --- a/python/triqs_dft_tools/converters/plovasp/proj_shell.py +++ b/python/triqs_dft_tools/converters/plovasp/proj_shell.py @@ -29,20 +29,17 @@ r""" Storage and manipulation on projector shells. """ -def issue_warning(message): - """ - Issues a warning. - """ - print() - print(" !!! WARNING !!!: " + message) - print() import itertools as it +import logging import numpy as np + from . import atm np.set_printoptions(suppress=True) +log = logging.getLogger('plovasp.proj_shell') + ################################################################################ ################################################################################ # @@ -60,38 +57,50 @@ class ProjectorShell: Parameters: - sh_pars (dict) : shell parameters from the config-file - - proj_raw (numpy.array) : array of raw projectors - + - proj_raw (numpy.array) : array of raw projectors from LOCPROJ + - proj_params (list[dict]) : parameters of raw projectors from LOCPROJ + - nc_flag (bool) : True if projectors are for non-collinear magnetic state """ def __init__(self, sh_pars, proj_raw, proj_params, kmesh, structure, nc_flag): self.lorb = sh_pars['lshell'] self.ions = sh_pars['ions'] self.user_index = sh_pars['user_index'] + + log.debug(f"-- Shell index: {self.user_index}") + self.corr = sh_pars['corr'] self.ion_sort = [sh_pars['ion_sort']] self.nc_flag = nc_flag -# try: -# self.tmatrix = sh_pars['tmatrix'] -# except KeyError: -# self.tmatrix = None self.lm1 = self.lorb**2 - self.lm2 = (self.lorb+1)**2 + self.lm2 = (self.lorb + 1)**2 self.nion = self.ions['nion'] -# Extract ion list and equivalence classes (ion sorts) + + # Extract ion list and equivalence classes (ion sorts) + # `ion_sort` contains actual indices of ions representing an equivalence class self.ion_list = sorted(it.chain(*self.ions['ion_list'])) + log.debug(f"-- ions: {self.ions}") + log.debug(f"-- ion_list: {self.ion_list}") + if self.ion_sort[0] is None: self.ion_sort = [] + + # Not the most efficient algorithm but ensures that ion indices are properly + # ordered in the resulting `ion_sort` for ion in self.ion_list: for icl, eq_cl in enumerate(self.ions['ion_list']): # Representative ion index of equivalence class `eq_cl` ion_rep = eq_cl[0] if ion in eq_cl: + log.debug(f"-- adding to equivalence class ({icl}, {eq_cl})") + log.debug(f"-- ion = {ion}, ion_rep = {ion_rep}") self.ion_sort.append(ion_rep + 1) # Enumerate classes starting from 1 break + log.debug(f"-- ion_sort: {self.ion_sort}") + self.ndim = self.extract_tmatrices(sh_pars) self.extract_projectors(proj_raw, proj_params, kmesh, structure) @@ -121,14 +130,13 @@ class ProjectorShell: if self.nc_flag == False: nm = self.lm2 - self.lm1 else: - nm = 2*(self.lm2 - self.lm1) + nm = 2 * (self.lm2 - self.lm1) if 'tmatrices' in sh_pars: self.do_transform = True if 'tmatrix' in sh_pars: - mess = "Both TRANSFORM and TRANSFILE are specified, TRANSFORM will be ignored." - issue_warning(mess) + log.warning("Both TRANSFORM and TRANSFILE are specified, TRANSFORM will be ignored") raw_matrices = sh_pars['tmatrices'] nrow, ncol = raw_matrices.shape diff --git a/python/triqs_dft_tools/converters/plovasp/vaspio.py b/python/triqs_dft_tools/converters/plovasp/vaspio.py index dd8ea6f1..de19c0c0 100644 --- a/python/triqs_dft_tools/converters/plovasp/vaspio.py +++ b/python/triqs_dft_tools/converters/plovasp/vaspio.py @@ -37,9 +37,11 @@ r""" - EIGENVAL - DOSCAR """ +import logging import numpy as np import re -#import plocar_io.c_plocar_io as c_plocar_io + +log = logging.getLogger('plovasp.vaspio') def read_lines(filename): r""" @@ -83,12 +85,13 @@ class VaspData: except (IOError, StopIteration): self.eigenval.eigs = None self.eigenval.ferw = None - print("!!! WARNING !!!: Error reading from EIGENVAL, trying LOCPROJ") + log.warning("Error reading from EIGENVAL, trying LOCPROJ...") + try: self.doscar.from_file(vasp_dir) except (IOError, StopIteration): if efermi_required: - print("!!! WARNING !!!: Error reading from Efermi from DOSCAR, trying LOCPROJ") + log.warning("Error reading Efermi from DOSCAR, trying LOCPROJ...") try: self.plocar.efermi self.doscar.efermi = self.plocar.efermi @@ -96,7 +99,7 @@ class VaspData: raise Exception("Efermi cannot be read from DOSCAR or LOCPROJ") else: # TODO: This a hack. Find out a way to determine ncdij without DOSCAR - print("!!! WARNING !!!: Error reading from DOSCAR, taking Efermi from config") + log.warning("Error reading Efermi from DOSCAR, taking from config") self.doscar.ncdij = self.plocar.nspin ################################################################################ @@ -168,14 +171,14 @@ class Plocar: # VASP.6. self.nspin = self.ncdij if self.ncdij < 4 else 1 - print("ISPIN is {}".format(self.nspin)) + log.debug("ISPIN is {}".format(self.nspin)) self.nspin_band = 2 if self.ncdij == 2 else 1 try: self.efermi = float(sline[4]) except: - print("!!! WARNING !!!: Error reading E-Fermi from LOCPROJ, trying DOSCAR") + log.warning("Error reading Efermi from LOCPROJ, trying DOSCAR...") plo = np.zeros((nproj, self.nspin, nk, self.nband), dtype=complex) proj_params = [{} for i in range(nproj)] @@ -189,7 +192,8 @@ class Plocar: self.ncdij = 1 else: self.nc_flag = 0 - print("NC FLAG : {}".format(self.nc_flag)) + + log.debug("NC FLAG : {}".format(self.nc_flag)) # First read the header block with orbital labels line = self.search_for(f, "^ *ISITE")