|
1
|
|
|
#! /usr/bin/env python |
|
|
|
|
|
|
2
|
|
|
# |
|
3
|
|
|
# Copyright (C) 2016 Rich Lewis <[email protected]> |
|
4
|
|
|
# License: 3-clause BSD |
|
5
|
|
|
|
|
6
|
1 |
|
import matplotlib.pyplot as plt |
|
|
|
|
|
|
7
|
|
|
|
|
8
|
1 |
|
from .. import features |
|
9
|
1 |
|
from .. import core |
|
10
|
1 |
|
from .. import vis |
|
11
|
1 |
|
from ipywidgets import Dropdown, Text, VBox, HBox, Valid, HTML |
|
|
|
|
|
|
12
|
1 |
|
from IPython import get_ipython |
|
|
|
|
|
|
13
|
1 |
|
from IPython.display import clear_output, display |
|
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
1 |
|
class Visualizer(object): |
|
|
|
|
|
|
17
|
1 |
|
def __init__(self, fper='morgan', smiles='c1ccccc1O', dpi=200): |
|
18
|
|
|
|
|
19
|
|
|
self.initialize_ipython() |
|
20
|
|
|
|
|
21
|
|
|
if isinstance(fper, str): |
|
22
|
|
|
self.fper = features.get(fper) |
|
23
|
|
|
else: |
|
24
|
|
|
self.fper = fper |
|
25
|
|
|
|
|
26
|
|
|
self.smiles_input = Text(smiles, description='smiles') |
|
27
|
|
|
self.smiles_input.on_submit(self.update_smiles) |
|
28
|
|
|
self.smiles_input.observe(self.typing) |
|
29
|
|
|
|
|
30
|
|
|
self.valid = Valid(True) |
|
31
|
|
|
|
|
32
|
|
|
self.dropdown = Dropdown(options=[], description='bit') |
|
33
|
|
|
self.dropdown.observe(self.plot) |
|
34
|
|
|
|
|
35
|
|
|
self.dpi_input = Text(str(dpi), description='dpi') |
|
36
|
|
|
self.dpi_input.on_submit(self.plot) |
|
37
|
|
|
|
|
38
|
|
|
self.ui = VBox([ |
|
|
|
|
|
|
39
|
|
|
HTML('<h2>Visualizer</h2>'), |
|
40
|
|
|
HBox([self.smiles_input, self.valid]), |
|
41
|
|
|
self.dropdown, |
|
42
|
|
|
self.dpi_input]) |
|
43
|
|
|
|
|
44
|
|
|
self.update_smiles(None) |
|
45
|
|
|
self.display() |
|
46
|
|
|
|
|
47
|
1 |
|
@staticmethod |
|
48
|
|
|
def initialize_ipython(): |
|
|
|
|
|
|
49
|
|
|
ipython = get_ipython() |
|
50
|
|
|
try: |
|
51
|
|
|
ipython.magic('matplotlib inline') |
|
52
|
|
|
except: |
|
|
|
|
|
|
53
|
|
|
pass |
|
54
|
|
|
|
|
55
|
1 |
|
def typing(self, _): |
|
|
|
|
|
|
56
|
|
|
self.valid.visible = False |
|
57
|
|
|
|
|
58
|
1 |
|
@property |
|
59
|
|
|
def dpi(self): |
|
|
|
|
|
|
60
|
|
|
try: |
|
61
|
|
|
return int(self.dpi_input.value) |
|
62
|
|
|
except: |
|
|
|
|
|
|
63
|
|
|
return 50 |
|
64
|
|
|
|
|
65
|
1 |
|
@dpi.setter |
|
66
|
|
|
def dpi(self, value): |
|
|
|
|
|
|
67
|
|
|
self.dpi_input.value = str(value) |
|
68
|
|
|
|
|
69
|
1 |
|
def display(self): |
|
|
|
|
|
|
70
|
|
|
display(self.ui) |
|
71
|
|
|
|
|
72
|
1 |
|
def update_smiles(self, _): |
|
|
|
|
|
|
73
|
|
|
try: |
|
74
|
|
|
self._mol = core.Mol.from_smiles(self.smiles_input.value) |
|
|
|
|
|
|
75
|
|
|
self.valid.value = True |
|
76
|
|
|
except ValueError: |
|
77
|
|
|
self.valid.value = False |
|
78
|
|
|
return |
|
79
|
|
|
finally: |
|
80
|
|
|
self.valid.visible = True |
|
81
|
|
|
return self.calculate() |
|
82
|
|
|
|
|
83
|
1 |
|
def calculate(self): |
|
|
|
|
|
|
84
|
|
|
fp = self.fper.transform(self.mol) |
|
|
|
|
|
|
85
|
|
|
self.fp = fp[fp == 1].index |
|
|
|
|
|
|
86
|
|
|
self.fpg = self.fper.grad(self.mol).ix[self.fp] |
|
|
|
|
|
|
87
|
|
|
return self.update_dropdown() |
|
88
|
|
|
|
|
89
|
1 |
|
def update_dropdown(self): |
|
|
|
|
|
|
90
|
|
|
self.dropdown.options.append(self.fp[0]) |
|
91
|
|
|
self.dropdown.value = self.fp[0] |
|
92
|
|
|
self.dropdown.options = self.fp.tolist() |
|
93
|
|
|
return self.plot(self.dropdown.value) |
|
94
|
|
|
|
|
95
|
1 |
|
@property |
|
96
|
|
|
def mol(self): |
|
|
|
|
|
|
97
|
|
|
return self._mol |
|
98
|
|
|
|
|
99
|
1 |
|
@mol.setter |
|
100
|
|
|
def mol(self, mol): |
|
|
|
|
|
|
101
|
|
|
self._mol = mol |
|
|
|
|
|
|
102
|
|
|
self.smiles_input.value = mol.to_smiles() |
|
103
|
|
|
self.calculate() |
|
104
|
|
|
|
|
105
|
1 |
|
@property |
|
106
|
|
|
def current_smiles(self): |
|
|
|
|
|
|
107
|
|
|
return self.smiles_input.value |
|
108
|
|
|
|
|
109
|
1 |
|
@property |
|
110
|
|
|
def current_bit(self): |
|
|
|
|
|
|
111
|
|
|
return self.dropdown.value |
|
112
|
|
|
|
|
113
|
1 |
|
def plot(self, _): |
|
|
|
|
|
|
114
|
|
|
clear_output() |
|
115
|
|
|
plt.clf() |
|
116
|
|
|
plt.rcParams['savefig.dpi'] = self.dpi |
|
117
|
|
|
vis.plot_weights(self.mol, self.fpg.ix[self.current_bit], |
|
118
|
|
|
quality=4, ax=plt.gca()) |
|
119
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.