1
|
|
|
#! /usr/bin/env python |
2
|
|
|
# |
3
|
|
|
# Copyright (C) 2015-2016 Rich Lewis <[email protected]> |
4
|
|
|
# License: 3-clause BSD |
5
|
|
|
|
6
|
1 |
|
""" |
7
|
|
|
## skchem.vis.atom |
8
|
|
|
|
9
|
|
|
Module for atom contribution visualization. |
10
|
|
|
""" |
11
|
|
|
|
12
|
1 |
|
from rdkit.Chem.Draw import MolToImage, DrawingOptions |
|
|
|
|
13
|
|
|
|
14
|
1 |
|
import numpy as np |
|
|
|
|
15
|
1 |
|
from matplotlib import pyplot as plt |
|
|
|
|
16
|
|
|
|
17
|
|
|
|
18
|
1 |
|
def plot_weights(mol, weights, quality=1, l=0.4, step=50, levels=20, |
|
|
|
|
19
|
|
|
contour_opacity=0.5, cmap='RdBu', ax=None, **kwargs): |
20
|
|
|
""" Plot weights as a sum of gaussians across a structure image. |
21
|
|
|
|
22
|
|
|
Args: |
23
|
|
|
mol (skchem.Mol): |
24
|
|
|
Molecule to visualize weights for. |
25
|
|
|
weights (iterable<float>): |
26
|
|
|
Array of weights in atom index order. |
27
|
|
|
l (float): |
28
|
|
|
Lengthscale of gaussians to visualize as a multiple of bond length. |
29
|
|
|
step (int): |
30
|
|
|
Size of grid edge to calculate the gaussians. |
31
|
|
|
levels (int): |
32
|
|
|
Number of contours to plot. |
33
|
|
|
contour_opacity (float): |
34
|
|
|
Alpha applied to the contour layer. |
35
|
|
|
ax (plt.axis): |
36
|
|
|
Axis to apply the plot to. Defaults to current axis. |
37
|
|
|
cmap (plt.cm): |
38
|
|
|
Colormap to use for the contour. |
39
|
|
|
**kwargs: |
40
|
|
|
Passed to contourf function. |
41
|
|
|
|
42
|
|
|
Returns: |
43
|
|
|
matplotlib.AxesSubplot: The plot. |
44
|
|
|
""" |
45
|
|
|
|
46
|
|
|
if not ax: |
47
|
|
|
ax = plt.gca() |
48
|
|
|
ax.grid('off') |
49
|
|
|
ax.axis('off') |
50
|
|
|
|
51
|
|
|
opts = DrawingOptions() |
52
|
|
|
opts.dotsPerAngstrom *= quality |
53
|
|
|
opts.atomLabelFontSize *= quality |
54
|
|
|
opts.bondLineWidth *= quality |
55
|
|
|
|
56
|
|
|
size = 300 * quality |
57
|
|
|
|
58
|
|
|
img, canvas, drawer = MolToImage(mol, size=(size, size), options=opts, |
59
|
|
|
returnCanvas=True) |
60
|
|
|
canvas.flush() |
61
|
|
|
coords = [[i / size, 1 - j / size] |
62
|
|
|
for k, (i, j) in list(drawer.atomPs.values())[0].items()] |
|
|
|
|
63
|
|
|
coords = np.array(coords) |
64
|
|
|
|
65
|
|
|
b = mol.bonds[0] |
|
|
|
|
66
|
|
|
begin, end = b.GetBeginAtom().GetIdx(), b.GetEndAtom().GetIdx() |
67
|
|
|
length = np.linalg.norm(coords[end] - coords[begin]) |
68
|
|
|
|
69
|
|
|
x = np.linspace(0, 1, 500) |
|
|
|
|
70
|
|
|
y = np.linspace(0, 1, 500) |
|
|
|
|
71
|
|
|
x, y = np.meshgrid(x, y) |
|
|
|
|
72
|
|
|
|
73
|
|
|
def gaussian(x, y, mu=np.zeros(2), sigma=np.identity(2), size=50): |
|
|
|
|
74
|
|
|
return (1 / (2 * np.pi * sigma[0, 0] * sigma[1, 1]) * |
75
|
|
|
np.exp(-((x - mu[0]) ** 2 / (2 * sigma[0, 0] ** 2) + |
76
|
|
|
(y - mu[1]) ** 2 / (2 * sigma[1, 1] ** 2)))) |
77
|
|
|
|
78
|
|
|
if not np.max(weights) == np.min(weights) == 0: |
79
|
|
|
z = sum([w * gaussian(x, y, mu, sigma=l * length * np.identity(2)) |
|
|
|
|
80
|
|
|
for mu, w in zip(coords, weights)]) |
81
|
|
|
v = np.max((np.abs(z.min()), np.abs(z.max()))) |
|
|
|
|
82
|
|
|
else: |
83
|
|
|
z = np.zeros(x.shape) |
|
|
|
|
84
|
|
|
v = 1 |
|
|
|
|
85
|
|
|
|
86
|
|
|
if z.min() >= 0: |
87
|
|
|
levels = int(levels/2) |
88
|
|
|
ax.contourf(x, y, z, levels, alpha=contour_opacity, |
89
|
|
|
extent=(0, 1, 0, 1), vmin=-v, vmax=v, cmap=cmap, **kwargs) |
90
|
|
|
|
91
|
|
|
ax.imshow(img, extent=(0, 1, 0, 1)) |
92
|
|
|
return ax |
93
|
|
|
|
This can be caused by one of the following:
1. Missing Dependencies
This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.
2. Missing __init__.py files
This error could also result from missing
__init__.py
files in your module folders. Make sure that you place one file in each sub-folder.