Skip to content

Commit ed2d552

Browse files
committed
chore(opytimizer): Adds annotated typing for few packages.
1 parent c5a214a commit ed2d552

File tree

11 files changed

+322
-242
lines changed

11 files changed

+322
-242
lines changed

opytimizer/math/distribution.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,24 @@
22
"""
33

44
from math import gamma, pi, sin
5+
from typing import Optional
56

67
import numpy as np
78

89
import opytimizer.math.random as r
910

1011

11-
def generate_bernoulli_distribution(prob=0.0, size=1):
12+
def generate_bernoulli_distribution(
13+
prob: Optional[float] = 0.0, size: Optional[int] = 1
14+
) -> np.ndarray:
1215
"""Generates a Bernoulli distribution based on an input probability.
1316
1417
Args:
15-
prob (float): Probability of distribution.
16-
size (int): Size of array.
18+
prob: Probability of distribution.
19+
size: Size of array.
1720
1821
Returns:
19-
Bernoulli distribution n-dimensional array.
22+
(np.ndarray): Bernoulli distribution n-dimensional array.
2023
2124
"""
2225

@@ -32,16 +35,18 @@ def generate_bernoulli_distribution(prob=0.0, size=1):
3235
return bernoulli_array
3336

3437

35-
def generate_choice_distribution(n=1, probs=None, size=1):
38+
def generate_choice_distribution(
39+
n: Optional[int] = 1, probs: Optional[np.ndarray] = None, size: Optional[int] = 1
40+
) -> np.ndarray:
3641
"""Generates a random choice distribution based on probabilities.
3742
3843
Args:
39-
n (int): Amount of values to be picked from.
40-
probs (np.array): Array of probabilities.
41-
size (int): Size of array.
44+
n: Amount of values to be picked from.
45+
probs: Array of probabilities.
46+
size: Size of array.
4247
4348
Returns:
44-
Choice distribution array.
49+
(np.ndarray): Choice distribution array.
4550
4651
"""
4752

@@ -51,19 +56,21 @@ def generate_choice_distribution(n=1, probs=None, size=1):
5156
return choice_array
5257

5358

54-
def generate_levy_distribution(beta=0.1, size=1):
59+
def generate_levy_distribution(
60+
beta: Optional[float] = 0.1, size: Optional[int] = 1
61+
) -> np.ndarray:
5562
"""Generates a n-dimensional array based on a Lévy distribution.
5663
5764
References:
5865
X.-S. Yang and S. Deb. Computers & Operations Research.
5966
Multiobjective Cuckoo Search for Design Optimization (2013).
6067
6168
Args:
62-
beta (float): Skewness parameter.
63-
size (int): Size of array.
69+
beta: Skewness parameter.
70+
size: Size of array.
6471
6572
Returns:
66-
Lévy distribution n-dimensional array.
73+
(np.ndarray): Lévy distribution n-dimensional array.
6774
6875
"""
6976

opytimizer/math/general.py

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,22 @@
22
"""
33

44
from itertools import islice
5+
from typing import Any, Iterable, List, Optional
56

67
import numpy as np
78

89
import opytimizer.math.random as r
910

1011

11-
def euclidean_distance(x, y):
12+
def euclidean_distance(x: np.ndarray, y: np.ndarray) -> float:
1213
"""Calculates the Euclidean distance between two n-dimensional points.
1314
1415
Args:
15-
x (np.array): N-dimensional point.
16-
y (np.array): N-dimensional point.
16+
x: N-dimensional point.
17+
y: N-dimensional point.
1718
1819
Returns:
19-
Euclidean distance between `x` and `y`.
20+
(float): Euclidean distance between `x` and `y`.
2021
2122
"""
2223

@@ -25,17 +26,22 @@ def euclidean_distance(x, y):
2526
return distance
2627

2728

28-
def kmeans(x, n_clusters=1, max_iterations=100, tol=1e-4):
29+
def kmeans(
30+
x: np.ndarray,
31+
n_clusters: Optional[int] = 1,
32+
max_iterations: Optional[int] = 100,
33+
tol: Optional[float] = 1e-4,
34+
) -> np.ndarray:
2935
"""Performs the K-Means clustering over the input data.
3036
3137
Args:
32-
x (np.array): Input array with a shape equal to (n_samples, n_variables, n_dimensions).
33-
n_clusters (int): Number of clusters.
34-
max_iterations (int): Maximum number of clustering iterations.
35-
tol (float): Tolerance value to stop the clustering.
38+
x: Input array with a shape equal to (n_samples, n_variables, n_dimensions).
39+
n_clusters: Number of clusters.
40+
max_iterations: Maximum number of clustering iterations.
41+
tol: Tolerance value to stop the clustering.
3642
3743
Returns:
38-
An array holding the assigned cluster per input sample.
44+
(np.ndarray): An array holding the assigned cluster per input sample.
3945
4046
"""
4147

@@ -79,15 +85,15 @@ def kmeans(x, n_clusters=1, max_iterations=100, tol=1e-4):
7985
return labels
8086

8187

82-
def n_wise(x, size=2):
88+
def n_wise(x: List[Any], size: Optional[int] = 2) -> Iterable:
8389
"""Iterates over an iterator and returns n-wise samples from it.
8490
8591
Args:
8692
x (list): Values to be iterated over.
87-
size (int): Amount of samples per iteration.
93+
size: Amount of samples per iteration.
8894
8995
Returns:
90-
N-wise samples from the iterator.
96+
(Iterable): N-wise samples from the iterator.
9197
9298
"""
9399

@@ -96,16 +102,18 @@ def n_wise(x, size=2):
96102
return iter(lambda: tuple(islice(iterator, size)), ())
97103

98104

99-
def tournament_selection(fitness, n, size=2):
105+
def tournament_selection(
106+
fitness: List[float], n: int, size: Optional[int] = 2
107+
) -> np.array:
100108
"""Selects n-individuals based on a tournament selection.
101109
102110
Args:
103111
fitness (list): List of individuals fitness.
104-
n (int): Number of individuals to be selected.
105-
size (int): Tournament size.
112+
n: Number of individuals to be selected.
113+
size: Tournament size.
106114
107115
Returns:
108-
Indexes of selected individuals.
116+
(np.array): Indexes of selected individuals.
109117
110118
"""
111119

@@ -122,14 +130,14 @@ def tournament_selection(fitness, n, size=2):
122130
return selected
123131

124132

125-
def weighted_wheel_selection(weights):
133+
def weighted_wheel_selection(weights: List[float]) -> int:
126134
"""Selects an individual from a weight-based roulette.
127135
128136
Args:
129-
weights (list): List of individuals weights.
137+
weights: List of individuals weights.
130138
131139
Returns:
132-
Weight-based roulette individual.
140+
(int): Weight-based roulette individual.
133141
134142
"""
135143

opytimizer/math/hyper.py

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@
22
"""
33

44
from functools import wraps
5+
from typing import Any, List, Tuple, Union
56

67
import numpy as np
78

89

9-
def norm(array):
10+
def norm(array: np.ndarray) -> np.ndarray:
1011
"""Calculates the norm over an array. It is used as the first step to map
1112
a hypercomplex number to a real-valued space.
1213
1314
Args:
14-
array (np.array): A 2-dimensional input array.
15+
array: A 2-dimensional input array.
1516
1617
Returns:
17-
Norm calculated over the second axis, such as (2, 4) array shape
18+
(np.ndarray): Norm calculated over the second axis, such as (2, 4) array shape
1819
will result in a norm (2, ) shape.
1920
2021
"""
@@ -24,16 +25,20 @@ def norm(array):
2425
return array_norm
2526

2627

27-
def span(array, lower_bound, upper_bound):
28+
def span(
29+
array: np.ndarray,
30+
lower_bound: Union[List[Any], Tuple[Any, Any], np.ndarray],
31+
upper_bound: Union[List, Tuple, np.ndarray],
32+
) -> np.ndarray:
2833
"""Spans a hypercomplex number between lower and upper bounds.
2934
3035
Args:
31-
array (np.array): A 2-dimensional input array.
32-
lb (list, tuple, np.array): Lower bounds to be spanned.
33-
ub (list, tuple, np.array): Upper bounds to be spanned.
36+
array: A 2-dimensional input array.
37+
lb: Lower bounds to be spanned.
38+
ub: Upper bounds to be spanned.
3439
3540
Returns:
36-
Spanned values that can be used as decision variables.
41+
(np.ndarray): Spanned values that can be used as decision variables.
3742
3843
"""
3944

@@ -47,38 +52,41 @@ def span(array, lower_bound, upper_bound):
4752
return array_span
4853

4954

50-
def span_to_hyper_value(lb, ub):
55+
def span_to_hyper_value(
56+
lb: Union[List[Any], Tuple[Any, Any], np.ndarray],
57+
ub: Union[List[Any], Tuple[Any, Any], np.ndarray],
58+
) -> np.ndarray:
5159
"""Spans a hyper-value between lower and upper bounds.
5260
5361
Args:
54-
lb (list, tuple, np.array): Lower bounds.
55-
ub (list, tuple, np.array): Upper bounds.
62+
lb: Lower bounds.
63+
ub: Upper bounds.
5664
5765
Returns:
58-
The output of the incoming objective function with a spanned input.
66+
(np.ndarray): The output of the incoming objective function with a spanned input.
5967
6068
"""
6169

62-
def _span_to_hyper_value(f):
70+
def _span_to_hyper_value(f: callable) -> callable:
6371
"""Actually decorates the incoming objective function.
6472
6573
Args:
66-
f (callable): Incoming objective function.
74+
f: Incoming objective function.
6775
6876
Returns:
69-
The wrapped objective function.
77+
(callable): The wrapped objective function.
7078
7179
"""
7280

7381
@wraps(f)
74-
def __span_to_hyper_value(x):
82+
def __span_to_hyper_value(x: np.ndarray) -> np.ndarray:
7583
"""Wraps the objective function for calculating its output.
7684
7785
Args:
78-
x (np.array): Array of hyper-values.
86+
x: Array of hyper-values.
7987
8088
Returns:
81-
The objective function itself.
89+
(np.ndarray): The objective function itself.
8290
8391
"""
8492

0 commit comments

Comments
 (0)