Completed
Push — master ( 24f8d3...86beb5 )
by Rich
01:25
created

RoughEmbedding   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 3
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A _optimize() 0 2 1
1
#! /usr/bin/env python
2
#
3
# Copyright (C) 2015 Rich Lewis <[email protected]>
4
# License: 3-clause BSD
5
6
"""
7
## skchem.forcefields.base
8
9
Module specifying base class for forcefields.
10
"""
11
import warnings
12
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...
13
import progressbar
0 ignored issues
show
Configuration introduced by
The import progressbar 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...
14
from rdkit.Chem.rdDistGeom import EmbedMolecule
0 ignored issues
show
Configuration introduced by
The import rdkit.Chem.rdDistGeom 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
from .. import core
17
18
class ForceField(object):
0 ignored issues
show
Coding Style introduced by
This class 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...
19
    def __init__(self, embed=True, warn_on_fail=True, error_on_fail=False, drop_failed=True, add_hs=True):
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (106/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
best-practice introduced by
Too many arguments (6/5)
Loading history...
20
21
        self.add_hs = add_hs
22
        self.drop_failed = drop_failed
23
        self.warn_on_fail = warn_on_fail
24
        self.error_on_fail = error_on_fail
25
        self.preembed = embed
26
27
    def embed(self, mol):
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...
28
29
        success = EmbedMolecule(mol)
30
        if success == -1:
31
            msg = 'Failed to Embed Molecule {}'.format(mol.name)
32
            if self.error_on_fail:
33
                raise RuntimeError(msg)
34
            elif self.warn_on_fail:
35
                warnings.warn(msg)
36
                return None
37
            else:
38
                pass
39
40
        if self.add_hs:
41
            mol = mol.add_hs(add_coords=True)
42
43
        return mol
44
45
46
    def optimize(self, mol):
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...
47
48
        # TODO: likely need to handle which conformer here
0 ignored issues
show
Coding Style introduced by
TODO and FIXME comments should generally be avoided.
Loading history...
49
50
        if self.preembed:
51
            mol = self.embed(mol)
52
53
        if mol is None:
54
            return None
55
56
        res = self._optimize(mol)
57
58
        if res == -1:
59
            msg = 'Failed to optimize molecule \'{}\' using {}'.format(mol.name, self.__class__)
60
            if self.error_on_fail:
61
                raise RuntimeError(msg)
62
            elif self.warn_on_fail:
63
                warnings.warn(msg)
64
            return None
65
66
        return mol
67
68
    def _optimize(self, mol):
69
        raise NotImplementedError
70
71
    def transform(self, obj):
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...
72
        if isinstance(obj, core.Mol):
73
            self.optimize(obj)
74
            return obj
75
        elif isinstance(obj, pd.Series):
76
            bar = progressbar.ProgressBar()
0 ignored issues
show
introduced by
Black listed name "bar"
Loading history...
77
            for i, mol in enumerate(bar(obj)):
78
                res = self.optimize(mol)
79
                if res is None and self.drop_failed:
80
                    obj = obj.drop(obj.index[i])
81
            return obj
82
        elif isinstance(obj, pd.DataFrame):
83
            res = self.transform(obj.structure)
84
            return obj.ix[res.index]
85
        elif isinstance(obj, (tuple, list)):
86
            return self.transform(pd.Series(obj, [mol.name for mol in obj]))
87
        else:
88
            raise NotImplementedError
89
90
class RoughEmbedding(ForceField):
0 ignored issues
show
Coding Style introduced by
This class 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...
91
    def _optimize(self, mol):
92
        return mol
93