Skip to content

Commit 12bb10b

Browse files
docs: refactor utils.combination module
1 parent b25d779 commit 12bb10b

1 file changed

Lines changed: 40 additions & 62 deletions

File tree

modAL/utils/combination.py

Lines changed: 40 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,28 @@
1+
from typing import Callable, Optional, Collection, Tuple
2+
13
import numpy as np
2-
from itertools import chain
4+
from sklearn.base import BaseEstimator
35

6+
from modAL.utils.data import modALinput
47

5-
def make_linear_combination(*functions, weights=None):
6-
"""
7-
Takes the given functions and makes a function which returns the linear combination
8-
of the output of original functions. It works well with functions returning numpy
9-
arrays of the same shape.
108

11-
:param functions:
12-
Base functions for the linear combination.The functions shall have the same
13-
argument and if they return numpy arrays, the returned arrays shall have the same shape.
14-
:type functions:
15-
function
9+
def make_linear_combination(*functions: Callable, weights: Optional[Collection] = None) -> Callable:
10+
"""
11+
Takes the given functions and makes a function which returns the linear combination of the output of original
12+
functions. It works well with functions returning numpy arrays of the same shape.
1613
17-
:param weights:
18-
Coefficients of the functions in the linear combination. The i-th given function
19-
will be multiplied with with weights[i].
20-
:type weights:
21-
array-like, length shall match the number of functions given
14+
Args:
15+
*functions: Base functions for the linear combination.The functions shall have the same argument and if they
16+
return numpy arrays, the returned arrays shall have the same shape.
17+
weights: Coefficients of the functions in the linear combination. The i-th given function will be multiplied
18+
with weights[i].
2219
20+
Todo:
21+
Doesn't it better to accept functions as a Collection explicitly?
2322
24-
:returns:
25-
- **linear_combination** *(function)* --
23+
Returns:
2624
A function which returns the linear combination of the given functions output.
2725
"""
28-
2926
if weights is None:
3027
weights = np.ones(shape=(len(functions)))
3128
else:
@@ -38,29 +35,20 @@ def linear_combination(*args, **kwargs):
3835
return linear_combination
3936

4037

41-
def make_product(*functions, exponents=None):
38+
def make_product(*functions: Callable, exponents: Optional[Collection] = None) -> Callable:
4239
"""
43-
Takes the given functions and makes a function which returns the product of the output
44-
of original functions. It works well with functions returning numpy arrays of the same
45-
shape.
46-
47-
:param functions:
48-
Base functions for the product. The functions shall have the same argument and if
49-
they return numpy arrays, the returned arrays shall have the same shape.
50-
:type functions:
51-
function
52-
53-
:param exponents:
54-
Exponents of the functions in the product. The i-th given function in the product
55-
will be raised to the power of exponents[i].
56-
:type exponents:
57-
array-like, length shall match the number of functions given
58-
59-
:returns:
60-
- **product_function** *(function)* --
40+
Takes the given functions and makes a function which returns the product of the output of original functions. It
41+
works well with functions returning numpy arrays of the same shape.
42+
43+
Args:
44+
*functions: Base functions for the product. The functions shall have the same argument and if they return numpy
45+
arrays, the returned arrays shall have the same shape.
46+
exponents: Exponents of the functions in the product. The i-th given function in the product will be raised to
47+
the power of exponents[i].
48+
49+
Returns:
6150
A function which returns the product function of the given functions output.
6251
"""
63-
6452
if exponents is None:
6553
exponents = np.ones(shape=(len(functions)))
6654
else:
@@ -74,31 +62,21 @@ def product_function(*args, **kwargs):
7462
return product_function
7563

7664

77-
def make_query_strategy(utility_measure, selector):
78-
"""
79-
Takes the given utility measure and selector functions and makes a query strategy
80-
by combining them.
81-
82-
:param utility_measure:
83-
Utility measure, for instance modAL.disagreement.vote_entropy, but it can be
84-
a custom function as well. Should take a classifier and the unlabelled data
85-
and should return an array containing the utility scores.
86-
:type utility_measure:
87-
function
88-
89-
:param selector:
90-
Function selecting instances for query. Should take an array of utility scores
91-
and should return an array containing the queried items.
92-
:type selector:
93-
function
94-
95-
:returns:
96-
- **query_strategy** *(function)* --
97-
A function which returns queried instances given a classifier and an unlabelled
98-
pool.
65+
def make_query_strategy(utility_measure: Callable, selector: Callable) -> Callable:
9966
"""
67+
Takes the given utility measure and selector functions and makes a query strategy by combining them.
10068
101-
def query_strategy(classifier, X):
69+
Args:
70+
utility_measure: Utility measure, for instance :func:`~modAL.disagreement.vote_entropy`, but it can be a custom
71+
function as well. Should take a classifier and the unlabelled data and should return an array containing the
72+
utility scores.
73+
selector: Function selecting instances for query. Should take an array of utility scores and should return an
74+
array containing the queried items.
75+
76+
Returns:
77+
A function which returns queried instances given a classifier and an unlabelled pool.
78+
"""
79+
def query_strategy(classifier: BaseEstimator, X: modALinput) -> Tuple:
10280
utility = utility_measure(classifier, X)
10381
query_idx = selector(utility)
10482
return query_idx, X[query_idx]

0 commit comments

Comments
 (0)