toggle()   A
last analyzed

Complexity

Conditions 4

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 4
c 1
b 0
f 1
dl 0
loc 9
ccs 0
cts 7
cp 0
crap 20
rs 9.2
1
#! /usr/bin/env python
2
#
3
# Copyright (C) 2016 Rich Lewis <[email protected]>
4
# License: 3-clause BSD
5
6 1
"""
7
## skchem.vis.mol
8
9
Module for drawing molecules.
10
"""
11
12 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...
13 1
from rdkit.Chem.Draw import DrawingOptions, MolToImage
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...
14 1
from matplotlib.widgets import CheckButtons
0 ignored issues
show
Configuration introduced by
The import matplotlib.widgets 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 mpl_toolkits.mplot3d import Axes3D  # needed to get 3D
0 ignored issues
show
Configuration introduced by
The import mpl_toolkits.mplot3d 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...
Unused Code introduced by
Unused Axes3D imported from mpl_toolkits.mplot3d
Loading history...
16 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...
17
18 1
def draw(mol, quality=1, ax=None):
0 ignored issues
show
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...
19
20
    """Draw a molecule on a matplotlib axis.
21
22
    Args:
23
        mol (skchem.Mol):
24
            The molecule to be drawn.
25
        quality (int):
26
            The level of quality.  Higher quality takes more time, but will
27
            look better (so long as matplotlib's savefig.dpi is high enough).
28
        ax (plt.Axes or None):
29
            An existing axis on which to draw the molecule.
30
31
    Returns:
32
        plt.AxesImage:
33
            A matplotlib AxesImage object with the molecule drawn.
34
    """
35
36
    if not ax:
37
        ax = plt.gca()
38
    ax.grid('off')
39
    ax.axis('off')
40
41
    opts = DrawingOptions()
42
    opts.dotsPerAngstrom *= quality
43
    opts.atomLabelFontSize *= quality
44
    opts.bondLineWidth *= quality
45
46
    size = 300 * quality
47
48
    img, canvas, drawer = MolToImage(mol, size=(size, size), options=opts,
0 ignored issues
show
Unused Code introduced by
The variable drawer seems to be unused.
Loading history...
49
                                     returnCanvas=True)
50
    canvas.flush()
51
    return ax.imshow(img, extent=(0, 1, 0, 1))
52
53
54 1
def _plot_sphere(pos=(0, 0, 0), radius=1., ax=None, **kwargs):
0 ignored issues
show
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...
55
56
    """ Plot a sphere.
57
58
    Args:
59
        pos tuple[float]:
60
            the centre of the sphere.
61
        radius (bool):
62
            The radius of the sphere.
63
        ax (plt.axes):
64
            The axes to draw the sphere on (defaults to current axis).
65
66
    Note:
67
        Keyword args are passed to Axes3D.plot_surface.
68
    """
69
70
    if ax is None:
71
        ax = plt.gca()
72
73
    u, v = np.mgrid[0:2 * np.pi:100j], np.mgrid[0:np.pi:100j]
0 ignored issues
show
Coding Style Naming introduced by
The name u 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 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...
introduced by
Slice index is not an int, None, or instance with __index__
Loading history...
74
75
    x = 2 * radius * np.outer(np.cos(u), np.sin(v)) + pos[0]
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...
76
    y = 2 * radius * np.outer(np.sin(u), np.sin(v)) + pos[1]
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...
77
    z = 2 * radius * np.outer(np.ones(np.size(u)), np.cos(v)) + pos[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...
78
79
    return ax.plot_surface(x, y, z, **kwargs)
80
81
82 1
_skchem_mpl3d_check = None
0 ignored issues
show
Coding Style Naming introduced by
The name _skchem_mpl3d_check does not conform to the constant naming conventions ((([A-Z_][A-Z0-9_]*)|(__.*__))$).

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...
83
84
85 1
def draw_3d(m, conformer_id=-1, label_atoms=None):
0 ignored issues
show
Coding Style Naming introduced by
The name m 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...
Comprehensibility introduced by
This function exceeds the maximum number of variables (19/15).
Loading history...
Unused Code introduced by
The argument label_atoms seems to be unused.
Loading history...
86
87
    """ Draw a molecule in three dimensions.
88
89
    Args:
90
        conformer_id (int):
91
            The id of the conformer to draw.
92
        label_atoms (bool):
93
            Whether to label the atoms (this can be toggled in interactive
94
            mode):
95
96
    Returns:
97
        plt.figure
98
99
    Note:
100
        This works great in the notebook with `%matplotlib notebook`.
101
    """
102
103
    # required to stop widgets getting garbage collected
104
    global _skchem_mpl3d_check
0 ignored issues
show
Coding Style introduced by
Usage of the global statement should be avoided.

Usage of global can make code hard to read and test, its usage is generally not recommended unless you are dealing with legacy code.

Loading history...
Coding Style Naming introduced by
The name _skchem_mpl3d_check does not conform to the constant naming conventions ((([A-Z_][A-Z0-9_]*)|(__.*__))$).

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...
105
106
    fig = plt.figure()
107
    ax = fig.add_subplot(111, projection='3d')
0 ignored issues
show
Coding Style Naming introduced by
The name ax 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...
108
109
    pos = m.conformers[conformer_id].positions
110
111
    for i, j in m.bonds.atom_idxs:
112
        ax.plot(*[(pos[i, d], pos[j, d]) for d in range(3)], c='black',
113
                zorder=1)
114
115
    for atom, p in zip(m.atoms, pos):
0 ignored issues
show
Coding Style Naming introduced by
The name p 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...
116
       _plot_sphere(p, radius=0.25 * atom.covalent_radius, ax=ax,
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 8 spaces were expected, but 7 were found.
Loading history...
117
                    color=atom.hexcode, linewidth=0, zorder=10)
118
    x, y, z = p + 0.2
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...
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...
Bug introduced by
The loop variable p might not be defined here.
Loading history...
119
    labels = [ax.text(x, y, z, i, visible=False) for i, p in enumerate(pos)]
120
121
    # set limits
122
    lo, hi = min(pos.flatten()) - 1, max(
0 ignored issues
show
Coding Style Naming introduced by
The name lo 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 hi 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...
123
        pos.flatten()) + 1.5  # 1.5 angstrom outside bording box
124
    ax.set_xlim(lo, hi)
125
    ax.set_ylim(lo, hi)
126
    ax.set_zlim(lo, hi)
127
    ax.set_aspect('equal')
128
129
    ax.set_xlabel('x')
130
    ax.set_ylabel('y')
131
    ax.set_zlabel('z')
132
133
    rax = plt.axes([0.15, 0.1, 0.1, 0.15])
134
135
    _skchem_mpl3d_check = CheckButtons(rax, ('show labels', 'show axes'),
136
                                       (False, True))
137
138
    def toggle(label):
0 ignored issues
show
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...
139
        if label == 'show labels':
140
            for l in labels:
0 ignored issues
show
Coding Style Naming introduced by
The name l 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...
141
                l.set_visible(not l.get_visible())
142
143
        if label == 'show axes':
144
            ax._axis3don = not ax._axis3don  # easiest way
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _axis3don was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
145
146
        plt.draw()
147
148
    _skchem_mpl3d_check.on_clicked(toggle)
149
150
    return ax
151