StructureAccessorMixin._make_structure_accessor()   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 8.2077

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 9
ccs 1
cts 6
cp 0.1666
rs 9.6666
cc 3
crap 8.2077
1
#! /usr/bin/env python
2
#
3
# Copyright (C) 2015-2016 Rich Lewis <[email protected]>
4
# License: 3-clause BSD
5
6 1
""" # skchem.pandas.structure_methods
7
8
 Tools for adding a default attribute to pandas objects."""
9
10
11 1
from sklearn.manifold import TSNE, MDS
0 ignored issues
show
Configuration introduced by
The import sklearn.manifold 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...
12 1
from sklearn.decomposition import PCA
0 ignored issues
show
Configuration introduced by
The import sklearn.decomposition 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 pandas as pd
0 ignored issues
show
Configuration introduced by
The import pandas 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
16 1
from pandas.core.base import NoNewAttributesMixin, AccessorProperty
0 ignored issues
show
Configuration introduced by
The import pandas.core.base 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 1
from pandas.core.series import Series
0 ignored issues
show
Configuration introduced by
The import pandas.core.series 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...
18 1
from pandas.core.index import Index
0 ignored issues
show
Configuration introduced by
The import pandas.core.index 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...
19
20 1
from .. import core
21 1
from .. import features
22
23 1
DIM_RED = {
24
    'tsne': TSNE,
25
    'pca': PCA,
26
    'mds': MDS
27
}
28
29
30 1
class StructureMethods(NoNewAttributesMixin):
31
32
    """ Accessor for calling chemical methods on series of molecules. """
33
34 1
    def __init__(self, data):
35
        self._data = data
36
37 1
    def add_hs(self, **kwargs):
0 ignored issues
show
Coding Style introduced by
This method 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...
38
        return self._data.apply(lambda m: m.add_hs(**kwargs))
0 ignored issues
show
Coding Style introduced by
Usage of * or ** arguments should usually be done with care.

Generally, there is nothing wrong with usage of * or ** arguments. For readability of the code base, we suggest to not over-use these language constructs though.

For more information, we can recommend this blog post from Ned Batchelder including its comments which also touches this aspect.

Loading history...
39
40 1
    def remove_hs(self, **kwargs):
0 ignored issues
show
Coding Style introduced by
This method 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...
41
        return self._data.apply(lambda m: m.remove_hs(**kwargs))
0 ignored issues
show
Coding Style introduced by
Usage of * or ** arguments should usually be done with care.

Generally, there is nothing wrong with usage of * or ** arguments. For readability of the code base, we suggest to not over-use these language constructs though.

For more information, we can recommend this blog post from Ned Batchelder including its comments which also touches this aspect.

Loading history...
42
43 1
    def visualize(self, fper='morgan', dim_red='tsne', dim_red_kw=None,
0 ignored issues
show
Coding Style introduced by
This method 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...
44
                  **kwargs):
45
46
        if dim_red_kw is None:
47
            dim_red_kw = {}
48
49
        if isinstance(dim_red, str):
50
            dim_red = DIM_RED.get(dim_red.lower())(**dim_red_kw)
0 ignored issues
show
Coding Style introduced by
Usage of * or ** arguments should usually be done with care.

Generally, there is nothing wrong with usage of * or ** arguments. For readability of the code base, we suggest to not over-use these language constructs though.

For more information, we can recommend this blog post from Ned Batchelder including its comments which also touches this aspect.

Loading history...
51
52
        fper = features.get(fper)
53
        fper.verbose = False
54
        feats = fper.transform(self._data)
55
        feats = feats.fillna(feats.mean())
56
        twod = pd.DataFrame(dim_red.fit_transform(feats))
0 ignored issues
show
Bug introduced by
The Instance of str does not seem to have a member named fit_transform.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
57
        ax = twod.plot.scatter(x=0, y=1, **kwargs)
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...
58
        ax.set_xticklabels([])
59
        ax.set_xlabel('')
60
        ax.set_yticklabels([])
61
        ax.set_ylabel('')
62
63 1
    @property
64
    def atoms(self):
0 ignored issues
show
Coding Style introduced by
This method 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...
65
        return self._data.apply(lambda m: m.atoms)
66
67
68 1
def only_contains_mols(ser):
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...
69
    return ser.apply(lambda s: isinstance(s, core.Mol)).all()
70
71
72 1
class StructureAccessorMixin(object):
73
74
    """ Mixin to bind chemical methods to objects. """
75
76 1
    def _make_structure_accessor(self):
77
        if isinstance(self, Index):
78
            raise AttributeError('Can only use .mol accessor with molecules,'
79
                                 'which use np.object_ in scikit-chem.')
80
        if not only_contains_mols(self):
81
            raise AttributeError('Can only use .mol accessor with '
82
                                 'Series that only contain mols.')
83
84
        return StructureMethods(self)
85 1
    mol = AccessorProperty(StructureMethods, _make_structure_accessor)
86
87
Series.__bases__ += StructureAccessorMixin,
88