plot_weights()   C
last analyzed

Complexity

Conditions 7

Size

Total Lines 75

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 51.5414

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 75
ccs 1
cts 32
cp 0.0313
rs 5.3953
cc 7
crap 51.5414

1 Method

Rating   Name   Duplication   Size   Complexity  
A gaussian() 0 4 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
0 ignored issues
show
Configuration introduced by
The import rdkit.Chem.Draw could not be resolved.

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.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

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.

Loading history...
13
14 1
import numpy as np
0 ignored issues
show
Configuration introduced by
The import numpy could not be resolved.

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.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

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.

Loading history...
15 1
from matplotlib import pyplot as plt
0 ignored issues
show
Configuration introduced by
The import matplotlib could not be resolved.

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.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

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.

Loading history...
16
17
18 1
def plot_weights(mol, weights, quality=1, l=0.4, step=50, levels=20,
0 ignored issues
show
Coding Style Naming introduced by
The name l does not conform to the argument naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style Naming introduced by
The name ax does not conform to the argument naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
best-practice introduced by
Too many arguments (9/5)
Loading history...
Comprehensibility introduced by
This function exceeds the maximum number of variables (30/15).
Loading history...
Unused Code introduced by
The argument step seems to be unused.
Loading history...
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()]
0 ignored issues
show
Unused Code introduced by
The variable k seems to be unused.
Loading history...
63
    coords = np.array(coords)
64
65
    b = mol.bonds[0]
0 ignored issues
show
Coding Style Naming introduced by
The name b does not conform to the variable naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
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)
0 ignored issues
show
Coding Style Naming introduced by
The name x does not conform to the variable naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
70
    y = np.linspace(0, 1, 500)
0 ignored issues
show
Coding Style Naming introduced by
The name y does not conform to the variable naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
71
    x, y = np.meshgrid(x, y)
0 ignored issues
show
Coding Style Naming introduced by
The name x does not conform to the variable naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style Naming introduced by
The name y does not conform to the variable naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
72
73
    def gaussian(x, y, mu=np.zeros(2), sigma=np.identity(2), size=50):
0 ignored issues
show
Coding Style Naming introduced by
The name x does not conform to the argument naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style Naming introduced by
The name y does not conform to the argument naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style Naming introduced by
The name mu does not conform to the argument naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
Unused Code introduced by
The argument size seems to be unused.
Loading history...
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))
0 ignored issues
show
Coding Style Naming introduced by
The name z does not conform to the variable naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
80
                 for mu, w in zip(coords, weights)])
81
        v = np.max((np.abs(z.min()), np.abs(z.max())))
0 ignored issues
show
Coding Style Naming introduced by
The name v does not conform to the variable naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
82
    else:
83
        z = np.zeros(x.shape)
0 ignored issues
show
Coding Style Naming introduced by
The name z does not conform to the variable naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
84
        v = 1
0 ignored issues
show
Coding Style Naming introduced by
The name v does not conform to the variable naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
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