Skip to content

Commit

Permalink
dev update
Browse files Browse the repository at this point in the history
  • Loading branch information
klausweinbauer committed Sep 4, 2024
1 parent 5fd8945 commit 33e1eca
Show file tree
Hide file tree
Showing 21 changed files with 381 additions and 105 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
*.pdf
*.json
.jukit/

# Byte-compiled / optimized / DLL files
Expand Down
21 changes: 21 additions & 0 deletions create_common_report.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from fgutils.proxy_collection.common import common_groups
from fgutils.proxy import MolProxy

from fgutils.vis import PdfWriter, GraphVisualizer

top_level_groups = ["alkyl", "aryl", "allyl", "alkene", "amine", "ester", "ether", "carbonyl"]

vis = GraphVisualizer()
def plot(value, ax, length=0, group="", index=0):
vis.plot(value, ax, title="{} {}/{}".format(group, index + 1, length))

pdf_writer = PdfWriter("common_groups.pdf", plot_fn=plot, cols=3)

data = common_groups
for tlg in top_level_groups:
proxy = MolProxy("{{{}}}".format(tlg), common_groups)
data = list(proxy)
print("Group '{}' has {} molecules.".format(tlg, len(data)))
pdf_writer.plot(data, group=tlg, length=len(data))

pdf_writer.close()
50 changes: 20 additions & 30 deletions demo.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,36 @@
import numpy as np
from fgutils.proxy_collection.diels_alder_proxy import DielsAlderProxy
from fgutils.vis import GraphVisualizer, plot_reaction, plot_its
from fgutils.vis import GraphVisualizer, plot_reaction, plot_its, PdfWriter
from fgutils.chem.its import get_its

import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages


def plot(data, ax, index=0, **kwargs):
g, h = data
its = get_its(g, h)
plot_reaction(g, h, ax[0])
ax[0].set_title("Reaction | Index: {}".format(index))
plot_its(its, ax[1])
ax[1].set_title("ITS | Index: {}".format(index))


neg_sample = True

if neg_sample:
file_name = "DA_reactions_neg.pdf"
else:
file_name = "DA_reactions.pdf"

proxy = DielsAlderProxy(neg_sample=neg_sample)
vis = GraphVisualizer()
pdf_writer = PdfWriter(
file_name, plot, max_pages=50, plot_per_row=True, width_ratios=[2, 1]
)

data = list(proxy)
print("Generated {} reactions.".format(len(data)))

data = [r for r in proxy]
print("Created {} reactions.".format(len(data)))

pages = 10
rows, cols = 7, 2
n_print = pages * rows
step = np.max([len(data) / n_print, 1])
pp = PdfPages(file_name)
done = False
for p in range(pages):
print("Pring page {} of {}".format(p + 1, pages))
fig, ax = plt.subplots(rows, cols, figsize=(21, 29.7), width_ratios=[2, 1])
for r in range(rows):
_idx = int((p * rows + r) * step)
if _idx >= len(data):
done = True
break
g, h = data[_idx]
its = get_its(g, h)
plot_reaction(g, h, ax[r, 0])
ax[r, 0].set_title("Reaction | Index: {}".format(_idx))
plot_its(its, ax[r, 1])
ax[r, 1].set_title("ITS | Index: {}".format(_idx))
plt.tight_layout()
pp.savefig(fig, bbox_inches="tight", pad_inches=1)
plt.close()
if done:
break
pp.close()
pdf_writer.plot(data)
pdf_writer.close()
14 changes: 9 additions & 5 deletions doc/figure_scripts/proxy_collection_DAReactionProxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from fgutils.parse import Parser
from fgutils.proxy_collection.diels_alder_proxy import DielsAlderProxy

plot = False

def get_group(groups, name):
for g in groups:
Expand All @@ -31,7 +32,8 @@ def plot_core_graphs():
vis.plot(parser(g.pattern), ax[i], title=g.name)
plt.tight_layout()
plt.savefig("doc/figures/proxy_collection_DAReactionProxy.png", bbox_inches="tight")
plt.show()
if plot:
plt.show()


def plot_graphs(graphs, name, cols=4, show_aam=False):
Expand All @@ -58,7 +60,8 @@ def plot_graphs(graphs, name, cols=4, show_aam=False):
"doc/figures/proxy_collection_DAReactionProxy_{}.png".format(name),
bbox_inches="tight",
)
plt.show()
if plot:
plt.show()


def plot_group(group, name, cols=4):
Expand Down Expand Up @@ -114,9 +117,9 @@ def plot_samples(neg_sample=False):
data = [r for r in proxy]
print("Created {} reactions.".format(len(data)))

rows, cols = 7, 2
rows, cols = 10, 2
step = np.max([len(data) / rows, 1])
fig, ax = plt.subplots(rows, cols, figsize=(21, 29.7), width_ratios=[2, 1])
fig, ax = plt.subplots(rows, cols, figsize=(21, 4 * rows), width_ratios=[2, 1])
for r in range(rows):
_idx = int(r * step)
if _idx >= len(data):
Expand All @@ -132,7 +135,8 @@ def plot_samples(neg_sample=False):
"doc/figures/proxy_collection_DAReactionProxy_{}.png".format(file_name),
bbox_inches="tight",
)
plt.show()
if plot:
plt.show()


def plot_s_trans_diene():
Expand Down
Binary file modified doc/figures/proxy_collection_DAReactionProxy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/figures/proxy_collection_DAReactionProxy_DA_reactions.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/figures/proxy_collection_DAReactionProxy_EDG.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/figures/proxy_collection_DAReactionProxy_EWG.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/figures/proxy_collection_DAReactionProxy_diene.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/figures/proxy_collection_DAReactionProxy_dienophile.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified doc/figures/proxy_collection_DAReactionProxy_s-trans_diene.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 3 additions & 2 deletions doc/proxy_collection.rst
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ parent, are surrounded by ``[]``.
.. image:: figures/proxy_collection_DAReactionProxy_EWG.png
:width: 1000

**A few of the 2873 Diels-Alder reactions generated by the proxy:**
**A few of the 10470 Diels-Alder reactions generated by the proxy:**

.. image:: figures/proxy_collection_DAReactionProxy_DA_reactions.png
:width: 1000
Expand All @@ -89,7 +89,8 @@ example in a fixed s-trans conformation.
.. image:: figures/proxy_collection_DAReactionProxy_s-trans_diene.png
:width: 1000

**A few of the 6253 INVALID Diels-Alder reactions generated by the proxy (negative data):**
**A few of the 12875 INVALID Diels-Alder reactions generated by the proxy
(negative data):**

.. image:: figures/proxy_collection_DAReactionProxy_DA_reactions_neg.png
:width: 1000
22 changes: 22 additions & 0 deletions export_DA.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import json

from fgutils.proxy_collection import DielsAlderProxy
from fgutils.rdkit import graph_to_smiles

def export(file_name, neg_sample):
idx_fmt = "DA_{}"
if neg_sample:
idx_fmt = "DA_neg_{}"

dataset = []
proxy = DielsAlderProxy(neg_sample=neg_sample)
for i, r in enumerate(proxy):
g, h = r
smiles = "{}>>{}".format(graph_to_smiles(g), graph_to_smiles(h))
dataset.append({"index": idx_fmt.format(i), "reaction": smiles})

with open(file_name, "w") as f:
json.dump(dataset, f, indent=4)

export("Diels-Alder_synthetic_data.json", neg_sample=False)
export("Diels-Alder_synthetic_negative_data.json", neg_sample=True)
10 changes: 1 addition & 9 deletions fgutils/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


token_specification = [
("ATOM", r"H|Br|Cl|Se|Sn|Si|C|N|O|P|S|F|B|I|b|c|n|o|p|s"),
("ATOM", r"H|Br|Cl|Se|Sn|Si|Mg|Li|C|N|O|P|S|F|B|I|b|c|n|o|p|s"),
("BOND", r"\.|-|=|#|$|:|/|\\"),
("BRANCH_START", r"\("),
("BRANCH_END", r"\)"),
Expand Down Expand Up @@ -116,14 +116,6 @@ def __process_token_ring(self, value):
if value in self.rings.keys():
anchor_sym = self.graph.nodes[self.anchor][SYMBOL_KEY]
ring_anchor = self.rings[value]
ring_anchor_sym = self.graph.nodes[ring_anchor][SYMBOL_KEY]
if anchor_sym.islower() != ring_anchor_sym.islower():
raise SyntaxError(
(
"Ring {} must be of same aromaticity type. "
+ "Started with {} and ended with {}."
).format(value, ring_anchor_sym, anchor_sym)
)
if anchor_sym.islower():
self.bond_order = 1.5
if self.bond_order is not None:
Expand Down
6 changes: 5 additions & 1 deletion fgutils/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,8 +414,12 @@ def groups(self, value):
elif isinstance(value, list):
for group in value:
self.__groups[group.name] = group
else:
elif isinstance(value, dict) and isinstance(
list(value.values())[0], ProxyGroup
):
self.__groups = value
else:
raise TypeError("Invalid group type.")

def __str__(self):
s = "ReactionProxy | Core: {} Enable AAM: {}\n".format(
Expand Down
Loading

0 comments on commit 33e1eca

Please sign in to comment.