Replies: 2 comments 1 reply
-
Hi Rafal,
this has come up previously on the mailing list; please find after my
signature a working, if a bit cumbersome, solution:
$ python wraplogs.py
RX [C:1]>>[C:2]
product formed from [C:1]>>[C:2]
RX [C:1][C:1]>>[C:2][C:2]
runReactants failed rx: [C:1][C:1]>>[C:2][C:2]
BEGIN of warning:
[19:16:31] reactant atom-mapping number 1 found multiple times.
[19:16:31] product atom-mapping number 2 not found in reactants.
[19:16:31] product atom-mapping number 2 found multiple times.
[19:16:31]
****
Invariant Violation
missing atom
Violation occurred on line 173 in file
/scratch/toscopa1/src/rdkit/Code/GraphMol/ChemReactions/Reaction.cpp
Failed Expression: rAtom
****
END
RX [C:1][C:1]>>[C:2][C:1]
runReactants failed rx: [C:1][C:1]>>[C:2][C:1]
BEGIN of warning:
[19:16:31] reactant atom-mapping number 1 found multiple times.
[19:16:31] product atom-mapping number 2 not found in reactants.
[19:16:31] initialization failed
END
END OF ALL REACTIONS
Cheers,
p.
import os
import sys
import datetime
import ctypes
import io
import tempfile
from contextlib import contextmanager
from rdkit import Chem
from rdkit.Chem import AllChem
# Adapted from
#
https://eli.thegreenplace.net/2015/redirecting-all-kinds-of-stdout-in-python/
if (sys.platform == "win32"):
kernel32 = ctypes.WinDLL("kernel32")
# https://docs.microsoft.com/en-us/windows/console/getstdhandle
C_STD_ERROR_HANDLE = -12
c_stderr = kernel32.GetStdHandle(C_STD_ERROR_HANDLE)
c_flush = kernel32.FlushFileBuffers
else:
libc = ctypes.CDLL(None)
c_stderr = ctypes.c_void_p.in_dll(libc, "stderr")
c_flush = libc.fflush
@contextmanager
def stderr_redirector(stream):
# The original fd stderr points to.
original_stderr_fd = sys.stderr.fileno()
def _redirect_stderr(to_fd):
"""Redirect stderr to the given file descriptor."""
# Flush the C-level buffer stderr
c_flush(c_stderr)
# Flush and close sys.stderr - also closes the file descriptor (fd)
sys.stderr.close()
# Make original_stderr_fd point to the same file as to_fd
os.dup2(to_fd, original_stderr_fd)
# Create a new sys.stderr that points to the redirected fd
sys.stderr = io.TextIOWrapper(os.fdopen(original_stderr_fd, 'wb'))
# Save a copy of the original stderr fd in saved_stderr_fd
saved_stderr_fd = os.dup(original_stderr_fd)
try:
# Create a temporary file and redirect stderr to it
tfile = tempfile.TemporaryFile(mode='w+b')
_redirect_stderr(tfile.fileno())
# Yield to caller, then redirect stderr back to the saved fd
yield
_redirect_stderr(saved_stderr_fd)
# Copy contents of temporary file to the given stream
tfile.flush()
tfile.seek(0, io.SEEK_SET)
stream.write(tfile.read())
finally:
tfile.close()
os.close(saved_stderr_fd)
mols = [ Chem.MolFromSmiles(s) for s in 'CCC'.split('.') ]
for rx in ['[C:1]>>[C:2]', '[C:1][C:1]>>[C:2][C:2]',
'[C:1][C:1]>>[C:2][C:1]']:
print("RX", rx)
f = io.BytesIO()
rxn = None
with stderr_redirector(f):
try:
rxn = AllChem.ReactionFromSmarts(rx)
except:
print("failed on ", rx)
if (rxn is None):
grabbed_stderr = f.getvalue().decode('utf-8')
print(f"BEGIN of warning:\n{grabbed_stderr}\nEND")
continue
prod = None
with stderr_redirector(f):
try:
prod = rxn.RunReactants(mols)
except:
print("runReactants failed rx:", rx)
if (prod is None):
grabbed_stderr = f.getvalue().decode('utf-8')
print(f"BEGIN of warning:\n{grabbed_stderr}\nEND")
continue
if prod:
print('product formed from', rx )
else:
print("no product from", rx)
print("END OF ALL REACTIONS")
…On Fri, Dec 11, 2020 at 2:38 PM Rafal Roszak ***@***.***> wrote:
I follow instruction from
http://rdkit.blogspot.com/2016/03/capturing-error-information.html and
try to do sth similar for catching logs from reactions, minimal example:
from rdkit import Chem
from rdkit.Chem import AllChem
from io import StringIO
import sys
Chem.WrapLogs()
mols = [ Chem.MolFromSmiles(s) for s in 'CCC'.split('.') ]
for rx in ['[C:1]>>[C:2]', '[C:1][C:1]>>[C:2][C:2]', '[C:1][C:1]>>[C:2][C:1]']:
#print("RX", rx)
sio = sys.stderr = StringIO()
try:
rxn = AllChem.ReactionFromSmarts(rx)
except:
print("failed on ", rx)
continue
txt = sio.getvalue()
print("BEGIN of warning:", txt, "END")
try:
prod = rxn.RunReactants(mols)
except:
print("runReactants failed rx:", rx)
continue
if prod:
print('product form from', rx )
else:
print("no product from", rx)
print("END OF ALL REACTIONS")
results is
BEGIN of warning: END
product form from [C:1]>>[C:2]
BEGIN of warning: END
runReactants failed rx: [C:1][C:1]>>[C:2][C:2]
BEGIN of warning: END
runReactants failed rx: [C:1][C:1]>>[C:2][C:1]
END OF ALL REACTIONS
[14:35:53] product atom-mapping number 2 not found in reactants.
[14:35:53] mapped atoms in the reactants were not mapped in the products.
unmapped numbers are: 1
[14:35:53] product atom-mapping number 2 not found in reactants.
[14:35:53] product atom-mapping number 2 found multiple times.
[14:35:53] product atom-mapping number 2 not found in reactants.
[14:35:53] reactant atom-mapping number 1 found multiple times.
[14:35:53]
****
Invariant Violation
missing atom
Violation occurred on line 173 in file /opt/conda/conda-bld/rdkit_1591929907781/work/Code/GraphMol/ChemReactions/Reaction.cpp
Failed Expression: rAtom
****
[14:35:53] reactant atom-mapping number 1 found multiple times.
[14:35:53] initialization failed
Nothing is captured and all information is printed to stderr after loop
finished.
What should I do to catch this message?
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#3633>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABIALYNQECG2HVGOAN5UBC3SUIOD5ANCNFSM4UWUUMEQ>
.
|
Beta Was this translation helpful? Give feedback.
1 reply
-
Chem.WrapLogs() appears to be broken. If should direct logs to sys.stderr
and then you *should* be able to capture them with
```
sys.stderr = StringIO()
```
Like you were doing, but this behavior isn't working anymore. We had
changed the invariant exceptions to be better handled in python, I wonder
if this had a side effect for the logging.
On Fri, Dec 11, 2020 at 1:19 PM Paolo Tosco <notifications@github.com>
wrote:
… Hi Rafal,
this has come up previously on the mailing list; please find after my
signature a working, if a bit cumbersome, solution:
$ python wraplogs.py
RX [C:1]>>[C:2]
product formed from [C:1]>>[C:2]
RX [C:1][C:1]>>[C:2][C:2]
runReactants failed rx: [C:1][C:1]>>[C:2][C:2]
BEGIN of warning:
[19:16:31] reactant atom-mapping number 1 found multiple times.
[19:16:31] product atom-mapping number 2 not found in reactants.
[19:16:31] product atom-mapping number 2 found multiple times.
[19:16:31]
****
Invariant Violation
missing atom
Violation occurred on line 173 in file
/scratch/toscopa1/src/rdkit/Code/GraphMol/ChemReactions/Reaction.cpp
Failed Expression: rAtom
****
END
RX [C:1][C:1]>>[C:2][C:1]
runReactants failed rx: [C:1][C:1]>>[C:2][C:1]
BEGIN of warning:
[19:16:31] reactant atom-mapping number 1 found multiple times.
[19:16:31] product atom-mapping number 2 not found in reactants.
[19:16:31] initialization failed
END
END OF ALL REACTIONS
Cheers,
p.
import os
import sys
import datetime
import ctypes
import io
import tempfile
from contextlib import contextmanager
from rdkit import Chem
from rdkit.Chem import AllChem
# Adapted from
#
https://eli.thegreenplace.net/2015/redirecting-all-kinds-of-stdout-in-python/
if (sys.platform == "win32"):
kernel32 = ctypes.WinDLL("kernel32")
# https://docs.microsoft.com/en-us/windows/console/getstdhandle
C_STD_ERROR_HANDLE = -12
c_stderr = kernel32.GetStdHandle(C_STD_ERROR_HANDLE)
c_flush = kernel32.FlushFileBuffers
else:
libc = ctypes.CDLL(None)
c_stderr = ctypes.c_void_p.in_dll(libc, "stderr")
c_flush = libc.fflush
@contextmanager
def stderr_redirector(stream):
# The original fd stderr points to.
original_stderr_fd = sys.stderr.fileno()
def _redirect_stderr(to_fd):
"""Redirect stderr to the given file descriptor."""
# Flush the C-level buffer stderr
c_flush(c_stderr)
# Flush and close sys.stderr - also closes the file descriptor (fd)
sys.stderr.close()
# Make original_stderr_fd point to the same file as to_fd
os.dup2(to_fd, original_stderr_fd)
# Create a new sys.stderr that points to the redirected fd
sys.stderr = io.TextIOWrapper(os.fdopen(original_stderr_fd, 'wb'))
# Save a copy of the original stderr fd in saved_stderr_fd
saved_stderr_fd = os.dup(original_stderr_fd)
try:
# Create a temporary file and redirect stderr to it
tfile = tempfile.TemporaryFile(mode='w+b')
_redirect_stderr(tfile.fileno())
# Yield to caller, then redirect stderr back to the saved fd
yield
_redirect_stderr(saved_stderr_fd)
# Copy contents of temporary file to the given stream
tfile.flush()
tfile.seek(0, io.SEEK_SET)
stream.write(tfile.read())
finally:
tfile.close()
os.close(saved_stderr_fd)
mols = [ Chem.MolFromSmiles(s) for s in 'CCC'.split('.') ]
for rx in ['[C:1]>>[C:2]', '[C:1][C:1]>>[C:2][C:2]',
'[C:1][C:1]>>[C:2][C:1]']:
print("RX", rx)
f = io.BytesIO()
rxn = None
with stderr_redirector(f):
try:
rxn = AllChem.ReactionFromSmarts(rx)
except:
print("failed on ", rx)
if (rxn is None):
grabbed_stderr = f.getvalue().decode('utf-8')
print(f"BEGIN of warning:\n{grabbed_stderr}\nEND")
continue
prod = None
with stderr_redirector(f):
try:
prod = rxn.RunReactants(mols)
except:
print("runReactants failed rx:", rx)
if (prod is None):
grabbed_stderr = f.getvalue().decode('utf-8')
print(f"BEGIN of warning:\n{grabbed_stderr}\nEND")
continue
if prod:
print('product formed from', rx )
else:
print("no product from", rx)
print("END OF ALL REACTIONS")
On Fri, Dec 11, 2020 at 2:38 PM Rafal Roszak ***@***.***>
wrote:
> I follow instruction from
> http://rdkit.blogspot.com/2016/03/capturing-error-information.html and
> try to do sth similar for catching logs from reactions, minimal example:
>
> from rdkit import Chem
> from rdkit.Chem import AllChem
> from io import StringIO
> import sys
> Chem.WrapLogs()
>
>
> mols = [ Chem.MolFromSmiles(s) for s in 'CCC'.split('.') ]
> for rx in ['[C:1]>>[C:2]', '[C:1][C:1]>>[C:2][C:2]',
'[C:1][C:1]>>[C:2][C:1]']:
> #print("RX", rx)
> sio = sys.stderr = StringIO()
> try:
> rxn = AllChem.ReactionFromSmarts(rx)
> except:
> print("failed on ", rx)
> continue
> txt = sio.getvalue()
> print("BEGIN of warning:", txt, "END")
> try:
> prod = rxn.RunReactants(mols)
> except:
> print("runReactants failed rx:", rx)
> continue
> if prod:
> print('product form from', rx )
> else:
> print("no product from", rx)
> print("END OF ALL REACTIONS")
>
> results is
>
> BEGIN of warning: END
> product form from [C:1]>>[C:2]
> BEGIN of warning: END
> runReactants failed rx: [C:1][C:1]>>[C:2][C:2]
> BEGIN of warning: END
> runReactants failed rx: [C:1][C:1]>>[C:2][C:1]
> END OF ALL REACTIONS
> [14:35:53] product atom-mapping number 2 not found in reactants.
> [14:35:53] mapped atoms in the reactants were not mapped in the products.
> unmapped numbers are: 1
> [14:35:53] product atom-mapping number 2 not found in reactants.
> [14:35:53] product atom-mapping number 2 found multiple times.
> [14:35:53] product atom-mapping number 2 not found in reactants.
> [14:35:53] reactant atom-mapping number 1 found multiple times.
> [14:35:53]
>
> ****
> Invariant Violation
> missing atom
> Violation occurred on line 173 in file
/opt/conda/conda-bld/rdkit_1591929907781/work/Code/GraphMol/ChemReactions/Reaction.cpp
> Failed Expression: rAtom
> ****
>
> [14:35:53] reactant atom-mapping number 1 found multiple times.
> [14:35:53] initialization failed
>
> Nothing is captured and all information is printed to stderr after loop
> finished.
> What should I do to catch this message?
>
> —
> You are receiving this because you are subscribed to this thread.
> Reply to this email directly, view it on GitHub
> <#3633>, or unsubscribe
> <
https://github.com/notifications/unsubscribe-auth/ABIALYNQECG2HVGOAN5UBC3SUIOD5ANCNFSM4UWUUMEQ
>
> .
>
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#3633 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ACLOFIBAQTQPCOSESDIITMTSUJPDDANCNFSM4UWUUMEQ>
.
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I follow instruction from http://rdkit.blogspot.com/2016/03/capturing-error-information.html and try to do sth similar for catching logs from reactions, minimal example:
results is
Nothing is captured and all information is printed to stderr after loop finished.
What should I do to catch this message?
Beta Was this translation helpful? Give feedback.
All reactions