|
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
|
1 |
|
from sklearn.manifold import TSNE, MDS |
|
|
|
|
|
|
11
|
1 |
|
from sklearn.decomposition import PCA |
|
|
|
|
|
|
12
|
|
|
|
|
13
|
1 |
|
from matplotlib import pyplot as plt |
|
|
|
|
|
|
14
|
1 |
|
import pandas as pd |
|
|
|
|
|
|
15
|
|
|
|
|
16
|
1 |
|
from pandas.core.base import NoNewAttributesMixin, AccessorProperty |
|
|
|
|
|
|
17
|
1 |
|
from pandas.core.series import Series |
|
|
|
|
|
|
18
|
1 |
|
from pandas.core.index import Index |
|
|
|
|
|
|
19
|
|
|
|
|
20
|
1 |
|
from .. import core |
|
21
|
1 |
|
from .. import descriptors |
|
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): |
|
|
|
|
|
|
38
|
|
|
return self._data.apply(lambda m: m.add_hs(**kwargs)) |
|
|
|
|
|
|
39
|
|
|
|
|
40
|
1 |
|
def remove_hs(self, **kwargs): |
|
|
|
|
|
|
41
|
|
|
return self._data.apply(lambda m: m.remove_hs(**kwargs)) |
|
|
|
|
|
|
42
|
|
|
|
|
43
|
1 |
|
def visualize(self, fper='morgan', dim_red='tsne', dim_red_kw={}, **kwargs): |
|
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
if isinstance(dim_red, str): |
|
46
|
|
|
dim_red = DIM_RED.get(dim_red.lower())(**dim_red_kw) |
|
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
fper = descriptors.get(fper) |
|
49
|
|
|
fper.verbose = False |
|
50
|
|
|
feats = fper.transform(self._data) |
|
51
|
|
|
feats = feats.fillna(feats.mean()) |
|
52
|
|
|
twod = pd.DataFrame(dim_red.fit_transform(feats)) |
|
|
|
|
|
|
53
|
|
|
ax = twod.plot.scatter(x=0, y=1, **kwargs) |
|
|
|
|
|
|
54
|
|
|
ax.set_xticklabels([]) |
|
55
|
|
|
ax.set_xlabel('') |
|
56
|
|
|
ax.set_yticklabels([]) |
|
57
|
|
|
ax.set_ylabel('') |
|
58
|
|
|
|
|
59
|
1 |
|
@property |
|
60
|
|
|
def atoms(self): |
|
|
|
|
|
|
61
|
|
|
return self._data.apply(lambda m: m.atoms) |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
1 |
|
def only_contains_mols(ser): |
|
|
|
|
|
|
65
|
|
|
return ser.apply(lambda s: isinstance(s, core.Mol)).all() |
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
1 |
|
class StructureAccessorMixin(object): |
|
69
|
|
|
|
|
70
|
|
|
""" Mixin to bind chemical methods to objects. """ |
|
71
|
|
|
|
|
72
|
1 |
|
def _make_structure_accessor(self): |
|
73
|
|
|
if isinstance(self, Index): |
|
74
|
|
|
raise AttributeError('Can only use .mol accessor with molecules,' |
|
75
|
|
|
'which use np.object_ in scikit-chem.') |
|
76
|
|
|
if not only_contains_mols(self): |
|
77
|
|
|
raise AttributeError('Can only use .mol accessor with ' |
|
78
|
|
|
'Series that only contain mols.') |
|
79
|
|
|
|
|
80
|
|
|
return StructureMethods(self) |
|
81
|
1 |
|
mol = AccessorProperty(StructureMethods, _make_structure_accessor) |
|
82
|
|
|
|
|
83
|
|
|
Series.__bases__ += StructureAccessorMixin, |
|
84
|
|
|
|
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__.pyfiles in your module folders. Make sure that you place one file in each sub-folder.