Skip to content

Use the logger instead of stderr for warnings in the pbc modules - #2316

Open
william-dawson wants to merge 2 commits into
pyscf:masterfrom
william-dawson:fix_pbc_warnings
Open

william-dawson wants to merge 2 commits into
pyscf:masterfrom
william-dawson:fix_pbc_warnings

Conversation

@william-dawson

Copy link
Copy Markdown

Inside the pbc modules, many times sys.stderr.write is used to write warnings, instead of the warn member function of pyscf.lib.logger. This makes it difficult to suppress warnings. I modified each of those spots to instead call the logger.

In all but one place, I could pass self to warn to ensure the verbosity parameter was communicated. However, in pyscf/pbc/dft/numint.py, the function eval_ao takes instead an integer directly for the verbosity. This is why I use the SimpleNamespace trick there, but if you prefer another approach let me know.

@fishjojo

Copy link
Copy Markdown
Collaborator

I think most of these warnings are important and are not meant to be suppressed.

@william-dawson

Copy link
Copy Markdown
Author

@fishjojo I agree, and I don't want to give the impression that I'm ignoring these warnings for production. With this modification, the warnings are still printed out by default. The change just has them be printed out in the same way as as equivalent warnings in other parts of the code.

Comment thread pyscf/pbc/dft/numint.py
sys.stderr.write('WARN: KNumInt.eval_ao function finds keyword '
'argument "kpt" and converts it to "kpts"\n')
logger.warn(SimpleNamespace(verbose=verbose),
'KNumInt.eval_ao function finds keyword '

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SimpleNamespace(verbose=verbose) fails with NameError: name 'verbose' is not defined . Change this to cell.

@sunqm

sunqm commented Aug 27, 2024

Copy link
Copy Markdown
Collaborator

I think most of these warnings are important and are not meant to be suppressed.

For the critical issues, how about changing them to errors instead of just warnings? Wrapping other warnings should be okay IMO.

@fishjojo

Copy link
Copy Markdown
Collaborator

I think most of these warnings are important and are not meant to be suppressed.

For the critical issues, how about changing them to errors instead of just warnings? Wrapping other warnings should be okay IMO.

okay. I think we can also add an option to always print, since sometimes we don't want the calculation to fail but let the program to choose the correct setting.

`verbose` would be None by default, not an integer or logger type
Comment thread pyscf/pbc/dft/numint.py
if 'kpt' in kwargs:
sys.stderr.write('WARN: KNumInt.nr_rks function finds keyword '
'argument "kpt" and converts it to "kpts"\n')
logger.warn(self, 'KNumInt.nr_rks function finds '

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please raise an error for this incorrect kwarg.

Comment thread pyscf/pbc/dft/numint.py
if 'kpt' in kwargs:
sys.stderr.write('WARN: KNumInt.nr_uks function finds keyword '
'argument "kpt" and converts it to "kpts"\n')
logger.warn(self, 'KNumInt.nr_uks function finds '

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please raise an error for this incorrect kwarg.

@sunqm

sunqm commented Sep 1, 2024

Copy link
Copy Markdown
Collaborator

Other sys.stderr can be replace IMO. @fishjojo please mark any critical warnings that you believe should not be suppressed.

@fishjojo

fishjojo commented Sep 1, 2024

Copy link
Copy Markdown
Collaborator

Other sys.stderr can be replace IMO. @fishjojo please mark any critical warnings that you believe should not be suppressed.

I agree. I don't have more comments.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants