Issues (942)

skchem/pipeline/pipeline.py (5 issues)

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.pipeline.pipeline
8
9
Module implementing pipelines.
10
"""
11
12 1
from ..utils import yaml_dump, json_dump
13 1
from ..io import read_config, read_json, read_yaml
0 ignored issues
show
Unused read_json imported from io
Loading history...
Unused read_yaml imported from io
Loading history...
14
15
16 1
def is_transform_filter(obj):
17
    """ Whether an object is a TransformFilter (by duck typing). """
18
    return hasattr(obj, 'transform_filter')
19
20
21 1
def is_filter(obj):
22
    """ Whether an object is a Filter (by duck typing). """
23
    return hasattr(obj, 'filter')
24
25
26 1
def is_transformer(obj):
27
    """ Whether an object is a Transformer (by duck typing). """
28
    return hasattr(obj, 'transform')
29
30
31 1
class Pipeline(object):
32
33
    """ Pipeline object. Applies filters and transformers in sequence. """
34
35 1
    def __init__(self, objects):
36
        self.objects = objects
37
38 1
    def get_params(self):
0 ignored issues
show
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...
39
        return {'objects': [obj.to_dict() for obj in self.objects]}
40
41 1
    def to_dict(self):
42
        """ Return a dictionary representation of the object."""
43
44
        return {'skchem.pipeline.pipeline.Pipeline': self.get_params()}
45
46 1
    @classmethod
47
    def from_params(cls, params):
48
        """ Create a instance from a params dictionary. """
49
        return cls([read_config(conf) for conf in params['objects']])
50
51 1
    def to_json(self, target=None):
52
53
        """ Serialize the object as JSON.
54
55
        Args:
56
            target (str or file-like):
57
                A file or filepath to serialize the object to.  If `None`,
58
                return the JSON as a string.
59
60
            Returns:
61
                None or str
62
        """
63
64
        return json_dump(self.to_dict(), target)
65
66 1
    def to_yaml(self, target=None):
67
68
        """ Serialize the object as YAML.
69
70
        Args:
71
            target (str or file-like):
72
                A file or filepath to serialize the object to.  If `None`,
73
                return the YAML as a string.
74
75
            Returns:
76
                None or str
77
        """
78
79
        return yaml_dump(self.to_dict(), target)
80
81 1
    def copy(self):
82
        """ Return a copy of this object. """
83
        return self.__class__(self.get_params())
84
85 1
    def transform_filter(self, mols, y=None):
0 ignored issues
show
Coding Style Naming introduced by
The name y does not conform to the argument naming conventions ([a-z_][a-z0-9_]{2,30}$).

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
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...
86
        for obj in self.objects:
87
            if is_transform_filter(obj):
88
                mols = obj.transform_filter(mols)
89
            elif is_filter(obj):
90
                mols = obj.filter(mols)
91
            elif is_transformer(obj):
92
                mols = obj.transform(mols)
93
            else:
94
                raise NotImplementedError('Cannot apply {}.'.format(obj))
95
        return mols if y is None else (mols, y.reindex(mols.index))
96
97