Completed
Push — master ( 01edc4...2b1c29 )
by Rich
01:29
created

ForceField.optimize()   C

Complexity

Conditions 7

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 22
rs 5.7894
cc 7
1
#! /usr/bin/env python
2
#
3
# Copyright (C) 2016 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
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...
14
15
from .. import core
16
from ..utils import NamedProgressBar, Suppressor
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
38
        if self.add_hs:
39
            return mol.add_hs(add_coords=True)
40
        else:
41
            return mol
42
43
44
    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...
45
46
        # 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...
47
48
        with Suppressor():
49
            if self.preembed:
50
                mol = self.embed(mol)
51
52
            if mol is None: # embedding failed
53
                return None
54
55
            res = self._optimize(mol)
56
57
        if res == -1:
58
            msg = 'Failed to optimize molecule \'{}\' using {}'.format(mol.name, self.__class__)
59
            if self.error_on_fail:
60
                raise RuntimeError(msg)
61
            elif self.warn_on_fail:
62
                warnings.warn(msg)
63
            return None
64
65
        return mol
66
67
    def _optimize(self, mol):
68
        raise NotImplementedError
69
70
    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...
71
        if isinstance(obj, core.Mol):
72
            return self.optimize(obj)
73
        elif isinstance(obj, pd.Series):
74
            bar = NamedProgressBar(name=self.__class__.__name__)
0 ignored issues
show
introduced by
Black listed name "bar"
Loading history...
75
            res = pd.Series([self.optimize(mol) for mol in bar(obj)], index=obj.index, name=obj.name)
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (101/100).

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

Loading history...
Bug introduced by
bar does not seem to be callable.
Loading history...
76
            if self.drop_failed:
77
                res = res[res.notnull()]
78
            return res
79
        elif isinstance(obj, pd.DataFrame):
80
            res = self.transform(obj.structure)
81
            res = res.to_frame().join(obj.drop('structure', axis=1))
82
            return res
83
        elif isinstance(obj, (tuple, list)):
84
            return self.transform(pd.Series(obj, [mol.name for mol in obj]))
85
        else:
86
            raise NotImplementedError
87
88
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...
89
    def _optimize(self, mol):
90
        return mol
91