1 2
from typing import Any, Callable, List, Mapping, Optional
2

3 2
from snorkel.labeling.lf import LabelingFunction
4 2
from snorkel.preprocess import BasePreprocessor
5

6

7 2
class SlicingFunction(LabelingFunction):
8
    """Base class for slicing functions.
9

10
    See ``snorkel.labeling.lf.LabelingFunction`` for details.
11
    """
12

13 2
    pass
14

15

16 2
class slicing_function:
17
    """Decorator to define a SlicingFunction object from a function.
18

19
    Parameters
20
    ----------
21
    name
22
        Name of the SF
23
    resources
24
        Slicing resources passed in to ``f`` via ``kwargs``
25
    preprocessors
26
        Preprocessors to run on data points before SF execution
27

28
    Examples
29
    --------
30
    >>> @slicing_function()
31
    ... def f(x):
32
    ...     return x.a > 42
33
    >>> f
34
    SlicingFunction f, Preprocessors: []
35
    >>> from types import SimpleNamespace
36
    >>> x = SimpleNamespace(a=90, b=12)
37
    >>> f(x)
38
    True
39

40
    >>> @slicing_function(name="my_sf")
41
    ... def g(x):
42
    ...     return 0 if x.a > 42 else -1
43
    >>> g
44
    SlicingFunction my_sf, Preprocessors: []
45
    """
46

47 2
    def __init__(
48
        self,
49
        name: Optional[str] = None,
50
        resources: Optional[Mapping[str, Any]] = None,
51
        pre: Optional[List[BasePreprocessor]] = None,
52
    ) -> None:
53 2
        if callable(name):
54 2
            raise ValueError("Looks like this decorator is missing parentheses!")
55 2
        self.name = name
56 2
        self.resources = resources
57 2
        self.pre = pre
58

59 2
    def __call__(self, f: Callable[..., int]) -> SlicingFunction:
60
        """Wrap a function to create a ``SlicingFunction``.
61

62
        Parameters
63
        ----------
64
        f
65
            Function that implements the core LF logic
66

67
        Returns
68
        -------
69
        SlicingFunction
70
            New ``SlicingFunction`` executing logic in wrapped function
71
        """
72 2
        name = self.name or f.__name__
73 2
        return SlicingFunction(name=name, f=f, resources=self.resources, pre=self.pre)

Read our documentation on viewing source code .

Loading