I can't find a way to ignore RDKit's warnings
I'm using RDKit version 2019.03.4; I haven't been able to suppress the warnings that
pop up when trying to convert a SMILES string to a molecule object.
I have to parse a LARGE number of smiles (~46 M strings) and all of them have hydrogen atoms without neighbors. Each time RDKit parses one of the smiles strings I get the warning
[17:58:45] WARNING: not removing hydrogen atom without neighbors
Since the number of strings is so large, this results in a significant slowdown...
My current workaround is to redirect stderr to /dev/null, but there surely is a better solution? I couldn't find it, and it seems that python's warnings module doesn't interact with RDKit's warnings at all.
@IgnacioJPickering: the RDKit warnings are done at the C++ level and, you are correct, they don't integrate well with the normal Python warnings module.
You can, however, switch off the messages using functionality in the RDLogger module:
In [1]: from rdkit import Chem
In [2]: from rdkit import RDLogger
In [3]: Chem.MolFromSmiles('c1cncc1')
[09:26:28] Can't kekulize mol. Unkekulized atoms: 0 1 2 3 4
In [4]: RDLogger.DisableLog('rdApp.*')
In [5]: Chem.MolFromSmiles('c1cncc1')
In [6]:
@IgnacioJPickering: the RDKit warnings are done at the C++ level and, you are correct, they don't integrate well with the normal Python warnings module.
You can, however, switch off the messages using functionality in the
RDLoggermodule:In [1]: from rdkit import Chem In [2]: from rdkit import RDLogger In [3]: Chem.MolFromSmiles('c1cncc1') [09:26:28] Can't kekulize mol. Unkekulized atoms: 0 1 2 3 4 In [4]: RDLogger.DisableLog('rdApp.*') In [5]: Chem.MolFromSmiles('c1cncc1') In [6]:
Thank you so much! After quite a lot googling, finally solved this problem XD
Thanks for this suggestion @greglandrum , this was about 90% of what I needed.
For people coming here through Google who like me have a slightly different use case:
If you only want to see a specific warning for example we wanted to disable
WARNING: not removing hydrogen atom without neighbors but we want to see the logs for Can't kekulize mol. Unkekulized atoms: 0 1 2 3 4
Also, full disclosure, I haven't tried this but I think it should work.
RDLogger.DisableLog('rdApp.info')
Chem.MolFromSmiles('<long_SMILES_STRING_TO_IGNORE>') # this line won't print
RDLogger.EnableLog('rdApp.info')
Chem.MolFromSmiles('c1cncc1') # this line will print
Most helpful comment
@IgnacioJPickering: the RDKit warnings are done at the C++ level and, you are correct, they don't integrate well with the normal Python warnings module.
You can, however, switch off the messages using functionality in the
RDLoggermodule: