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 |
|
|
|
|
12
|
1 |
|
from sklearn.decomposition import PCA |
|
|
|
|
13
|
|
|
|
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 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): |
|
|
|
|
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=None, |
|
|
|
|
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) |
|
|
|
|
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)) |
|
|
|
|
57
|
|
|
ax = twod.plot.scatter(x=0, y=1, **kwargs) |
|
|
|
|
58
|
|
|
ax.set_xticklabels([]) |
59
|
|
|
ax.set_xlabel('') |
60
|
|
|
ax.set_yticklabels([]) |
61
|
|
|
ax.set_ylabel('') |
62
|
|
|
|
63
|
1 |
|
@property |
64
|
|
|
def atoms(self): |
|
|
|
|
65
|
|
|
return self._data.apply(lambda m: m.atoms) |
66
|
|
|
|
67
|
|
|
|
68
|
1 |
|
def only_contains_mols(ser): |
|
|
|
|
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
|
|
|
|
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.