Skip to content

Commit 8711705

Browse files
committed
Fix some numpy tests
1 parent bd8b731 commit 8711705

16 files changed

+38
-38
lines changed

computer_vision/haralick_descriptors.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def root_mean_square_error(original: np.ndarray, reference: np.ndarray) -> float
1919
>>> root_mean_square_error(np.array([1, 2, 3]), np.array([6, 4, 2]))
2020
3.1622776601683795
2121
"""
22-
return np.sqrt(((original - reference) ** 2).mean())
22+
return float(np.sqrt(((original - reference) ** 2).mean()))
2323

2424

2525
def normalize_image(
@@ -273,7 +273,7 @@ def haralick_descriptors(matrix: np.ndarray) -> list[float]:
273273
>>> morphological = opening_filter(binary)
274274
>>> mask_1 = binary_mask(gray, morphological)[0]
275275
>>> concurrency = matrix_concurrency(mask_1, (0, 1))
276-
>>> haralick_descriptors(concurrency)
276+
>>> [float(f) for f in haralick_descriptors(concurrency)]
277277
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
278278
"""
279279
# Function np.indices could be used for bigger input types,
@@ -335,7 +335,7 @@ def get_descriptors(
335335
return np.concatenate(descriptors, axis=None)
336336

337337

338-
def euclidean(point_1: np.ndarray, point_2: np.ndarray) -> np.float32:
338+
def euclidean(point_1: np.ndarray, point_2: np.ndarray) -> float:
339339
"""
340340
Simple method for calculating the euclidean distance between two points,
341341
with type np.ndarray.
@@ -346,7 +346,7 @@ def euclidean(point_1: np.ndarray, point_2: np.ndarray) -> np.float32:
346346
>>> euclidean(a, b)
347347
3.3166247903554
348348
"""
349-
return np.sqrt(np.sum(np.square(point_1 - point_2)))
349+
return float(np.sqrt(np.sum(np.square(point_1 - point_2))))
350350

351351

352352
def get_distances(descriptors: np.ndarray, base: int) -> list[tuple[int, float]]:

machine_learning/scoring_functions.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ def mae(predict, actual):
2020
"""
2121
Examples(rounded for precision):
2222
>>> actual = [1,2,3];predict = [1,4,3]
23-
>>> np.around(mae(predict,actual),decimals = 2)
23+
>>> float(np.around(mae(predict,actual),decimals = 2))
2424
0.67
2525
2626
>>> actual = [1,1,1];predict = [1,1,1]
27-
>>> mae(predict,actual)
27+
>>> float(mae(predict,actual))
2828
0.0
2929
"""
3030
predict = np.array(predict)
@@ -41,11 +41,11 @@ def mse(predict, actual):
4141
"""
4242
Examples(rounded for precision):
4343
>>> actual = [1,2,3];predict = [1,4,3]
44-
>>> np.around(mse(predict,actual),decimals = 2)
44+
>>> float(np.around(mse(predict,actual),decimals = 2))
4545
1.33
4646
4747
>>> actual = [1,1,1];predict = [1,1,1]
48-
>>> mse(predict,actual)
48+
>>> float(mse(predict,actual))
4949
0.0
5050
"""
5151
predict = np.array(predict)
@@ -67,7 +67,7 @@ def rmse(predict, actual):
6767
1.15
6868
6969
>>> actual = [1,1,1];predict = [1,1,1]
70-
>>> rmse(predict,actual)
70+
>>> float(rmse(predict,actual))
7171
0.0
7272
"""
7373
predict = np.array(predict)
@@ -84,12 +84,12 @@ def rmse(predict, actual):
8484
def rmsle(predict, actual):
8585
"""
8686
Examples(rounded for precision):
87-
>>> actual = [10,10,30];predict = [10,2,30]
88-
>>> np.around(rmsle(predict,actual),decimals = 2)
87+
>>> actual = [10,10,30];predict = [10,2,30])
88+
>>> float(np.around(rmsle(predict,actual),decimals = 2)
8989
0.75
9090
9191
>>> actual = [1,1,1];predict = [1,1,1]
92-
>>> rmsle(predict,actual)
92+
>>> float(rmsle(predict,actual))
9393
0.0
9494
"""
9595
predict = np.array(predict)

machine_learning/similarity_search.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def cosine_similarity(input_a: np.ndarray, input_b: np.ndarray) -> float:
153153
>>> cosine_similarity(np.array([1, 2]), np.array([6, 32]))
154154
0.9615239476408232
155155
"""
156-
return np.dot(input_a, input_b) / (norm(input_a) * norm(input_b))
156+
return float(np.dot(input_a, input_b) / (norm(input_a) * norm(input_b)))
157157

158158

159159
if __name__ == "__main__":

machine_learning/support_vector_machines.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ def norm_squared(vector: ndarray) -> float:
1414
Returns:
1515
float: squared second norm of vector
1616
17-
>>> norm_squared([1, 2])
17+
>>> int(norm_squared([1, 2]))
1818
5
19-
>>> norm_squared(np.asarray([1, 2]))
19+
>>> int(norm_squared(np.asarray([1, 2])))
2020
5
21-
>>> norm_squared([0, 0])
21+
>>> int(norm_squared([0, 0]))
2222
0
2323
"""
2424
return np.dot(vector, vector)

maths/euclidean_distance.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ def euclidean_distance(vector_1: Vector, vector_2: Vector) -> VectorOut:
1313
"""
1414
Calculate the distance between the two endpoints of two vectors.
1515
A vector is defined as a list, tuple, or numpy 1D array.
16-
>>> euclidean_distance((0, 0), (2, 2))
16+
>>> float(euclidean_distance((0, 0), (2, 2)))
1717
2.8284271247461903
18-
>>> euclidean_distance(np.array([0, 0, 0]), np.array([2, 2, 2]))
18+
>>> float(euclidean_distance(np.array([0, 0, 0]), np.array([2, 2, 2])))
1919
3.4641016151377544
20-
>>> euclidean_distance(np.array([1, 2, 3, 4]), np.array([5, 6, 7, 8]))
20+
>>> float(euclidean_distance(np.array([1, 2, 3, 4]), np.array([5, 6, 7, 8])))
2121
8.0
22-
>>> euclidean_distance([1, 2, 3, 4], [5, 6, 7, 8])
22+
>>> float(euclidean_distance([1, 2, 3, 4], [5, 6, 7, 8]))
2323
8.0
2424
"""
2525
return np.sqrt(np.sum((np.asarray(vector_1) - np.asarray(vector_2)) ** 2))

maths/euler_method.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def explicit_euler(
2626
... return y
2727
>>> y0 = 1
2828
>>> y = explicit_euler(f, y0, 0.0, 0.01, 5)
29-
>>> y[-1]
29+
>>> float(y[-1])
3030
144.77277243257308
3131
"""
3232
n = int(np.ceil((x_end - x0) / step_size))

maths/euler_modified.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ def euler_modified(
2424
>>> def f1(x, y):
2525
... return -2*x*(y**2)
2626
>>> y = euler_modified(f1, 1.0, 0.0, 0.2, 1.0)
27-
>>> y[-1]
27+
>>> float(y[-1])
2828
0.503338255442106
2929
>>> import math
3030
>>> def f2(x, y):
3131
... return -2*y + (x**3)*math.exp(-2*x)
3232
>>> y = euler_modified(f2, 1.0, 0.0, 0.1, 0.3)
33-
>>> y[-1]
33+
>>> float(y[-1])
3434
0.5525976431951775
3535
"""
3636
n = int(np.ceil((x_end - x0) / step_size))

maths/gaussian.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from numpy import exp, pi, sqrt
66

77

8-
def gaussian(x, mu: float = 0.0, sigma: float = 1.0) -> int:
8+
def gaussian(x, mu: float = 0.0, sigma: float = 1.0) -> float:
99
"""
1010
>>> gaussian(1)
1111
0.24197072451914337
@@ -53,7 +53,7 @@ def gaussian(x, mu: float = 0.0, sigma: float = 1.0) -> int:
5353
>>> gaussian(2523, mu=234234, sigma=3425)
5454
0.0
5555
"""
56-
return 1 / sqrt(2 * pi * sigma**2) * exp(-((x - mu) ** 2) / (2 * sigma**2))
56+
return float(1 / sqrt(2 * pi * sigma**2) * exp(-((x - mu) ** 2) / (2 * sigma**2)))
5757

5858

5959
if __name__ == "__main__":

maths/minkowski_distance.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def minkowski_distance(
1919
>>> minkowski_distance([1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0], 2)
2020
8.0
2121
>>> import numpy as np
22-
>>> np.isclose(5.0, minkowski_distance([5.0], [0.0], 3))
22+
>>> bool(np.isclose(5.0, minkowski_distance([5.0], [0.0], 3)))
2323
True
2424
>>> minkowski_distance([1.0], [2.0], -1)
2525
Traceback (most recent call last):

maths/numerical_analysis/adams_bashforth.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def step_3(self) -> np.ndarray:
102102
>>> def f(x, y):
103103
... return x + y
104104
>>> y = AdamsBashforth(f, [0, 0.2, 0.4], [0, 0, 0.04], 0.2, 1).step_3()
105-
>>> y[3]
105+
>>> float(y[3])
106106
0.15533333333333332
107107
108108
>>> AdamsBashforth(f, [0, 0.2], [0, 0], 0.2, 1).step_3()
@@ -140,9 +140,9 @@ def step_4(self) -> np.ndarray:
140140
... return x + y
141141
>>> y = AdamsBashforth(
142142
... f, [0, 0.2, 0.4, 0.6], [0, 0, 0.04, 0.128], 0.2, 1).step_4()
143-
>>> y[4]
143+
>>> float(y[4])
144144
0.30699999999999994
145-
>>> y[5]
145+
>>> float(y[5])
146146
0.5771083333333333
147147
148148
>>> AdamsBashforth(f, [0, 0.2, 0.4], [0, 0, 0.04], 0.2, 1).step_4()
@@ -185,7 +185,7 @@ def step_5(self) -> np.ndarray:
185185
>>> y = AdamsBashforth(
186186
... f, [0, 0.2, 0.4, 0.6, 0.8], [0, 0.02140, 0.02140, 0.22211, 0.42536],
187187
... 0.2, 1).step_5()
188-
>>> y[-1]
188+
>>> float(y[-1])
189189
0.05436839444444452
190190
191191
>>> AdamsBashforth(f, [0, 0.2, 0.4], [0, 0, 0.04], 0.2, 1).step_5()

maths/numerical_analysis/runge_kutta_fehlberg_45.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ def runge_kutta_fehlberg_45(
3434
>>> def f(x, y):
3535
... return 1 + y**2
3636
>>> y = runge_kutta_fehlberg_45(f, 0, 0, 0.2, 1)
37-
>>> y[1]
37+
>>> float(y[1])
3838
0.2027100937470787
3939
>>> def f(x,y):
4040
... return x
4141
>>> y = runge_kutta_fehlberg_45(f, -1, 0, 0.2, 0)
42-
>>> y[1]
42+
>>> float(y[1])
4343
-0.18000000000000002
4444
>>> y = runge_kutta_fehlberg_45(5, 0, 0, 0.1, 1)
4545
Traceback (most recent call last):

maths/numerical_analysis/runge_kutta_gills.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def runge_kutta_gills(
3434
>>> def f(x, y):
3535
... return (x-y)/2
3636
>>> y = runge_kutta_gills(f, 0, 3, 0.2, 5)
37-
>>> y[-1]
37+
>>> float(y[-1])
3838
3.4104259225717537
3939
4040
>>> def f(x,y):

maths/softmax.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def softmax(vector):
2828
2929
The softmax vector adds up to one. We need to ceil to mitigate for
3030
precision
31-
>>> np.ceil(np.sum(softmax([1,2,3,4])))
31+
>>> float(np.ceil(np.sum(softmax([1,2,3,4]))))
3232
1.0
3333
3434
>>> vec = np.array([5,5])

neural_network/two_hidden_layers_neural_network.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def feedforward(self) -> np.ndarray:
6464
>>> nn = TwoHiddenLayerNeuralNetwork(input_val, output_val)
6565
>>> res = nn.feedforward()
6666
>>> array_sum = np.sum(res)
67-
>>> np.isnan(array_sum)
67+
>>> bool(np.isnan(array_sum))
6868
False
6969
"""
7070
# Layer_between_input_and_first_hidden_layer is the layer connecting the
@@ -171,7 +171,7 @@ def train(self, output: np.ndarray, iterations: int, give_loss: bool) -> None:
171171
>>> first_iteration_weights = nn.feedforward()
172172
>>> nn.back_propagation()
173173
>>> updated_weights = nn.second_hidden_layer_and_output_layer_weights
174-
>>> (first_iteration_weights == updated_weights).all()
174+
>>> bool((first_iteration_weights == updated_weights).all())
175175
False
176176
"""
177177
for iteration in range(1, iterations + 1):

other/bankers_algorithm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def __need_index_manager(self) -> dict[int, list[int]]:
9393
{0: [1, 2, 0, 3], 1: [0, 1, 3, 1], 2: [1, 1, 0, 2], 3: [1, 3, 2, 0],
9494
4: [2, 0, 0, 3]}
9595
"""
96-
return {self.__need().index(i): i for i in self.__need()}
96+
return {self.__need().index(i): int(i) for i in self.__need()}
9797

9898
def main(self, **kwargs) -> None:
9999
"""

physics/in_static_equilibrium.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def in_static_equilibrium(
5353
# summation of moments is zero
5454
moments: NDArray[float64] = cross(location, forces)
5555
sum_moments: float = sum(moments)
56-
return abs(sum_moments) < eps
56+
return bool(abs(sum_moments) < eps)
5757

5858

5959
if __name__ == "__main__":

0 commit comments

Comments
 (0)