1
|
|
|
#! /usr/bin/env python |
2
|
|
|
# |
3
|
|
|
# Copyright (C) 2016 Rich Lewis <[email protected]> |
4
|
|
|
# License: 3-clause BSD |
5
|
|
|
|
6
|
1 |
|
""" |
7
|
|
|
# skchem.features.descriptors.decorators |
8
|
|
|
|
9
|
|
|
Decorators for descriptors in scikit-chem. |
10
|
|
|
""" |
11
|
1 |
|
import inspect |
12
|
1 |
|
from functools import wraps |
13
|
1 |
|
from collections import OrderedDict, defaultdict |
14
|
|
|
|
15
|
1 |
|
import pandas as pd |
|
|
|
|
16
|
1 |
|
|
17
|
|
|
|
18
|
|
|
def requires_h_depleted(func): |
19
|
1 |
|
|
20
|
|
|
""" Decorate a function that requires an h-depleted graph. |
21
|
|
|
|
22
|
|
|
This will check if the molecule argument is h-depleted, and will |
23
|
|
|
memoize a depleted version if it is not, and pass it to the func. |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
""" |
27
|
|
|
|
28
|
|
|
@wraps(func) |
29
|
|
|
def inner(mol, *args, **kwargs): |
|
|
|
|
30
|
|
|
|
31
|
|
|
# if is already h depleted |
32
|
|
|
if (mol.atoms.atomic_number == 1).sum() == 0: |
33
|
|
|
return func(mol, *args, **kwargs) |
34
|
|
|
|
35
|
|
|
if not hasattr(mol, '_h_depleted'): |
36
|
|
|
mol._h_depleted = mol.remove_hs() |
|
|
|
|
37
|
|
|
|
38
|
|
|
return func(mol._h_depleted, *args, **kwargs) |
|
|
|
|
39
|
|
|
|
40
|
|
|
return inner |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
def requires_h_filled(func): |
44
|
1 |
|
|
45
|
|
|
""" Decorate a function that requires a h-filled graph. |
46
|
|
|
|
47
|
|
|
This will check if the molecule argument is h-filled, and will |
48
|
|
|
memoize a filled version if it is not, and pass it to the func. |
49
|
|
|
|
50
|
|
|
Note: |
51
|
|
|
This decorator should be used first if in combination with dMat etc. |
52
|
|
|
|
53
|
|
|
""" |
54
|
|
|
|
55
|
|
|
@wraps(func) |
56
|
|
|
def inner(mol, *args, **kwargs): |
|
|
|
|
57
|
|
|
# if is already h enriched |
58
|
|
|
if mol.atoms.n_total_hs.sum() == 0: |
59
|
|
|
return func(mol, *args, **kwargs) |
60
|
|
|
|
61
|
|
|
# if not, memoize the enriched one and pass it |
62
|
|
|
if not hasattr(mol, '_h_enriched'): |
63
|
|
|
mol._h_enriched = mol.add_hs() |
|
|
|
|
64
|
|
|
|
65
|
|
|
return func(mol._h_enriched, *args, **kwargs) |
|
|
|
|
66
|
|
|
|
67
|
|
|
return inner |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
class Cache(object): |
71
|
1 |
|
|
72
|
|
|
""" Function cache.""" |
73
|
|
|
|
74
|
|
|
def __init__(self): |
75
|
1 |
|
self.cached = {} |
76
|
1 |
|
|
77
|
|
|
def extract_kwargs(self, func): |
|
|
|
|
78
|
1 |
|
|
79
|
|
|
# py2 compat |
80
|
|
|
if pd.compat.PY2: |
81
|
|
|
spec = inspect.getargspec(func) |
82
|
|
|
kwds = spec.args[len(spec.defaults) - len(spec.args):] |
83
|
|
|
kwds = OrderedDict(zip(kwds, spec.defaults)) |
84
|
|
|
|
85
|
|
|
else: |
86
|
|
|
|
87
|
1 |
|
kwds = OrderedDict((k, v.default) for k, v in |
88
|
|
|
inspect.signature(func).parameters.items() |
|
|
|
|
89
|
|
|
if v.default != inspect._empty) |
|
|
|
|
90
|
1 |
|
|
91
|
|
|
return kwds |
92
|
|
|
|
93
|
|
|
def __call__(self, func): |
94
|
1 |
|
|
95
|
1 |
|
""" Create a decorator to identify a function as returning a cached |
96
|
|
|
value. |
97
|
1 |
|
|
98
|
|
|
This can be used for objects that are nontrivial to generate, but |
99
|
|
|
are used by many functions. |
100
|
1 |
|
""" |
101
|
1 |
|
|
102
|
|
|
name = func.__name__ |
103
|
|
|
|
104
|
|
|
# get the key word arguments and the default values of the function |
105
|
1 |
|
|
106
|
|
|
|
107
|
|
|
@wraps(func) |
108
|
1 |
|
def inner(mol, *args, **kwargs): |
|
|
|
|
109
|
|
|
|
110
|
|
|
# py2 compat |
111
|
1 |
|
force = kwargs.pop('force', False) |
112
|
|
|
|
113
|
|
|
self.setup_cache(mol) |
114
|
1 |
|
|
115
|
|
|
# get the full set of keywords to use, including defaults |
116
|
1 |
|
kwds.update(kwargs) |
|
|
|
|
117
|
|
|
kw_to_save = tuple(sorted(kwds.items())) |
|
|
|
|
118
|
1 |
|
|
119
|
|
|
# call function if it hasn't already been called |
120
|
1 |
|
# with required arguments, or if told to. |
121
|
|
|
if force or name not in self.cached.keys() or \ |
122
|
|
|
kw_to_save not in mol.cache.get(name, {}).keys(): |
123
|
|
|
|
124
|
|
|
res = func(mol, *args, **kwargs) |
125
|
|
|
|
126
|
|
|
# cache the value with the args used. |
127
|
|
|
mol.cache[name].update({kw_to_save: res}) |
128
|
|
|
|
129
|
1 |
|
# return the cached value |
130
|
|
|
return mol.cache[name][kw_to_save] |
131
|
|
|
|
132
|
1 |
|
self.cached[name] = inner, tuple(kwds.keys()) |
|
|
|
|
133
|
|
|
|
134
|
|
|
return inner |
135
|
|
|
|
136
|
1 |
|
def inject(self, *args_to_inject): |
137
|
|
|
|
138
|
|
|
""" Create a decorator that will inject cached values as arguments. |
139
|
|
|
|
140
|
1 |
|
Args: |
141
|
|
|
args (list<str>): |
142
|
1 |
|
A list of cachable requirements for this function. |
143
|
|
|
""" |
144
|
|
|
|
145
|
|
|
def outer(func): |
|
|
|
|
146
|
|
|
|
147
|
1 |
|
# extract the defaults for the func |
148
|
|
|
kwds = OrderedDict((k, v.default) for k, v in |
149
|
1 |
|
inspect.signature(func).parameters.items() |
|
|
|
|
150
|
|
|
if v.default != inspect._empty) |
|
|
|
|
151
|
1 |
|
|
152
|
|
|
@wraps(func) |
153
|
|
|
def inner(mol, *args, **kwargs): |
|
|
|
|
154
|
1 |
|
|
155
|
|
|
# augment with the keywords from the function |
156
|
|
|
kwds.update(kwargs) |
157
|
|
|
|
158
|
1 |
|
self.setup_cache(mol) |
159
|
|
|
|
160
|
|
|
# look up cached values, or produce them if not. |
161
|
1 |
|
# inject the cached values |
162
|
|
|
|
163
|
|
|
args_supp = () |
164
|
1 |
|
|
165
|
1 |
|
for arg in args_to_inject: |
166
|
|
|
# lookup function to inject |
167
|
|
|
inj_func, params = self.cached[arg.__name__] |
168
|
1 |
|
|
169
|
|
|
# get the kwargs required |
170
|
|
|
inj_kwargs = {param: kwds[param] for param in params |
171
|
1 |
|
if param in kwds.keys()} |
172
|
|
|
|
173
|
1 |
|
# get a hashable representation of the kwargs |
174
|
|
|
immut = tuple(sorted(inj_kwargs.items())) |
175
|
1 |
|
|
176
|
|
|
# retrieve the cached result (or None if not yet cached) |
177
|
1 |
|
res = mol.cache.get(arg.__name__, {}).get(immut, None) |
178
|
|
|
|
179
|
1 |
|
# calculate and cache result |
180
|
|
|
if res is None: |
181
|
|
|
res = inj_func(mol, **inj_kwargs) |
|
|
|
|
182
|
|
|
|
183
|
|
|
# add to injected args |
184
|
1 |
|
args_supp += (res,) |
185
|
1 |
|
|
186
|
|
|
# put injected args at start of arg list |
187
|
1 |
|
args = args_supp + args |
188
|
|
|
|
189
|
|
|
return func(mol, *args, **kwargs) |
190
|
|
|
|
191
|
|
|
return inner |
192
|
|
|
|
193
|
|
|
return outer |
194
|
|
|
|
195
|
1 |
|
@staticmethod |
196
|
|
|
def setup_cache(mol): |
197
|
|
|
|
198
|
|
|
""" Set up a cache on e.g. a `Mol`. """ |
199
|
|
|
|
200
|
|
|
if not hasattr(mol, 'cache'): |
201
|
|
|
mol.cache = defaultdict(dict) |
202
|
|
|
|
203
|
|
|
@staticmethod |
204
|
|
|
def teardown_cache(mol): |
205
|
|
|
|
206
|
|
|
""" Tear down a cache on e.g. a `Mol`. """ |
207
|
|
|
|
208
|
|
|
if hasattr(mol, 'cache'): |
209
|
|
|
del mol.cache |
210
|
|
|
|
211
|
|
|
cache = Cache() |
|
|
|
|
212
|
|
|
|
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.