1
|
|
|
#! /usr/bin/env python |
2
|
|
|
# |
3
|
|
|
# Copyright (C) 2015-2016 Rich Lewis <[email protected]> |
4
|
|
|
# License: 3-clause BSD |
5
|
|
|
|
6
|
1 |
|
""" |
7
|
|
|
skchem.utils.helpers |
8
|
|
|
|
9
|
|
|
Module providing helper functions for scikit-chem |
10
|
|
|
""" |
11
|
|
|
|
12
|
1 |
|
from collections import Iterable |
13
|
1 |
|
from functools import wraps |
14
|
|
|
|
15
|
1 |
|
import numpy as np |
|
|
|
|
16
|
1 |
|
import pandas as pd |
|
|
|
|
17
|
|
|
|
18
|
|
|
|
19
|
1 |
|
def optional_second_method(func): |
|
|
|
|
20
|
1 |
|
@wraps(func) |
21
|
1 |
|
def inner(self, arg, second_arg=None, **kwargs): |
|
|
|
|
22
|
1 |
|
res = func(self, arg, **kwargs) |
23
|
1 |
|
if second_arg is not None: |
24
|
|
|
return res, second_arg |
25
|
|
|
else: |
26
|
1 |
|
return res |
27
|
1 |
|
return inner |
28
|
|
|
|
29
|
|
|
|
30
|
1 |
|
def iterable_to_series(mols): |
|
|
|
|
31
|
1 |
|
assert isinstance(mols, Iterable), 'Object must be iterable.' |
32
|
1 |
|
assert not isinstance(mols, str), 'Object cannot be a string.' |
33
|
1 |
|
if isinstance(mols, dict): |
34
|
1 |
|
return pd.Series(mols, name='structure') |
35
|
|
|
else: |
36
|
1 |
|
return pd.Series(mols, index=[mol.name if mol.name else i |
37
|
|
|
for i, mol in enumerate(mols)], |
38
|
|
|
name='structure') |
39
|
|
|
|
40
|
|
|
|
41
|
1 |
|
def nanarray(shape): |
42
|
|
|
""" Produce an array of NaN in provided shape. |
43
|
|
|
|
44
|
|
|
Args: |
45
|
|
|
shape (tuple): |
46
|
|
|
The shape of the nan array to produce. |
47
|
|
|
|
48
|
|
|
Returns: |
49
|
|
|
np.array |
50
|
|
|
|
51
|
|
|
""" |
52
|
1 |
|
return np.repeat(np.nan, np.prod(shape)).reshape(*shape) |
|
|
|
|
53
|
|
|
|
54
|
|
|
|
55
|
1 |
|
def squeeze(data, axis=None): |
56
|
|
|
|
57
|
|
|
""" Squeeze dimension for length 1 arrays. |
58
|
|
|
|
59
|
|
|
Args: |
60
|
|
|
data (pd.Series or pd.DataFrame or pd.Panel): |
61
|
|
|
The pandas object to squeeze. |
62
|
|
|
axis (int or tuple): |
63
|
|
|
The axes along which to squeeze. |
64
|
|
|
|
65
|
|
|
Returns: |
66
|
|
|
pd.Series or pd.DataFrame |
67
|
|
|
""" |
68
|
|
|
|
69
|
1 |
|
if axis is None: |
70
|
|
|
axis = range(len(data.axes)) |
71
|
1 |
|
elif isinstance(axis, int): |
72
|
1 |
|
axis = (axis,) |
73
|
1 |
|
return data.iloc[tuple([0 if len(a) == 1 and i in axis else slice(None) |
74
|
|
|
for i, a in enumerate(data.axes)])] |
75
|
|
|
|
76
|
|
|
|
77
|
1 |
|
class Defaults(object): |
|
|
|
|
78
|
|
|
|
79
|
1 |
|
def __init__(self, defaults): |
80
|
|
|
|
81
|
1 |
|
self.defaults = defaults |
82
|
|
|
|
83
|
1 |
|
def get(self, val): |
|
|
|
|
84
|
1 |
|
if isinstance(val, str): |
85
|
1 |
|
return self.defaults.get(val) |
86
|
|
|
else: |
87
|
1 |
|
return val |
88
|
|
|
|
89
|
|
|
|
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.