|
1 | | -""" |
2 | | - Auxiliary handler methods for data summary extraction |
3 | | -""" |
4 | | -from typing import Any, Callable, Dict, List, Sequence |
5 | | - |
6 | | -import networkx as nx |
7 | | -from visions import VisionsTypeset |
8 | | - |
9 | | - |
10 | | -def compose(functions: Sequence[Callable]) -> Callable: |
11 | | - """ |
12 | | - Compose a sequence of functions. |
13 | | -
|
14 | | - :param functions: sequence of functions |
15 | | - :return: combined function applying all functions in order. |
16 | | - """ |
17 | | - |
18 | | - def composed_function(*args) -> List[Any]: |
19 | | - result = args # Start with the input arguments |
20 | | - for func in functions: |
21 | | - result = func(*result) if isinstance(result, tuple) else func(result) |
22 | | - return result # type: ignore |
23 | | - |
24 | | - return composed_function # type: ignore |
25 | | - |
26 | | - |
27 | | -class Handler: |
28 | | - """A generic handler |
29 | | -
|
30 | | - Allows any custom mapping between data types and functions |
31 | | - """ |
32 | | - |
33 | | - def __init__( |
34 | | - self, |
35 | | - mapping: Dict[str, List[Callable]], |
36 | | - typeset: VisionsTypeset, |
37 | | - *args, |
38 | | - **kwargs |
39 | | - ): |
40 | | - self.mapping = mapping |
41 | | - self.typeset = typeset |
42 | | - self._complete_dag() |
43 | | - |
44 | | - def _complete_dag(self) -> None: |
45 | | - for from_type, to_type in nx.topological_sort( |
46 | | - nx.line_graph(self.typeset.base_graph) |
47 | | - ): |
48 | | - self.mapping[str(to_type)] = ( |
49 | | - self.mapping[str(from_type)] + self.mapping[str(to_type)] |
50 | | - ) |
51 | | - |
52 | | - def handle(self, dtype: str, *args, **kwargs) -> dict: |
53 | | - """ |
54 | | - Returns: |
55 | | - object: a tuple containing the config, the dataset series and the summary extracted |
56 | | - """ |
57 | | - funcs = self.mapping.get(dtype, []) |
58 | | - op = compose(funcs) |
59 | | - summary = op(*args)[-1] |
60 | | - return summary |
| 1 | +""" |
| 2 | + Auxiliary handler methods for data summary extraction |
| 3 | +""" |
| 4 | +from typing import Any, Callable, Dict, List, Sequence |
| 5 | + |
| 6 | +import networkx as nx |
| 7 | +from visions import VisionsTypeset |
| 8 | + |
| 9 | + |
| 10 | +def compose(functions: Sequence[Callable]) -> Callable: |
| 11 | + """ |
| 12 | + Compose a sequence of functions. |
| 13 | +
|
| 14 | + :param functions: sequence of functions |
| 15 | + :return: combined function applying all functions in order. |
| 16 | + """ |
| 17 | + |
| 18 | + def composed_function(*args) -> List[Any]: |
| 19 | + result = args # Start with the input arguments |
| 20 | + for func in functions: |
| 21 | + result = func(*result) if isinstance(result, tuple) else func(result) |
| 22 | + return result # type: ignore |
| 23 | + |
| 24 | + return composed_function # type: ignore |
| 25 | + |
| 26 | + |
| 27 | +class Handler: |
| 28 | + """A generic handler |
| 29 | +
|
| 30 | + Allows any custom mapping between data types and functions |
| 31 | + """ |
| 32 | + |
| 33 | + def __init__( |
| 34 | + self, |
| 35 | + mapping: Dict[str, List[Callable]], |
| 36 | + typeset: VisionsTypeset, |
| 37 | + *args, |
| 38 | + **kwargs |
| 39 | + ): |
| 40 | + self.mapping = mapping |
| 41 | + self.typeset = typeset |
| 42 | + self._complete_dag() |
| 43 | + |
| 44 | + def _complete_dag(self) -> None: |
| 45 | + for from_type, to_type in nx.topological_sort( |
| 46 | + nx.line_graph(self.typeset.base_graph) |
| 47 | + ): |
| 48 | + self.mapping[str(to_type)] = ( |
| 49 | + self.mapping[str(from_type)] + self.mapping[str(to_type)] |
| 50 | + ) |
| 51 | + |
| 52 | + def handle(self, dtype: str, *args, **kwargs) -> dict: |
| 53 | + """ |
| 54 | + Returns: |
| 55 | + object: a tuple containing the config, the dataset series and the summary extracted |
| 56 | + """ |
| 57 | + funcs = self.mapping.get(dtype, []) |
| 58 | + op = compose(funcs) |
| 59 | + summary = op(*args)[-1] |
| 60 | + return summary |
| 61 | + |
| 62 | + |
| 63 | + |
0 commit comments