Skip to content

Commit 557b40c

Browse files
committed
fix ut
1 parent 979398f commit 557b40c

File tree

6 files changed

+45
-40
lines changed

6 files changed

+45
-40
lines changed

_unittests/ut_graph/test_graph_distance.py

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
import os
77
import unittest
88
import copy
9-
from mlstatpy.ext_test_case import get_temp_folder, ExtTestCase
9+
from mlstatpy.ext_test_case import ExtTestCase, ignore_warnings
1010
from mlstatpy.graph.graph_distance import GraphDistance
1111

1212

1313
class TestGraphDistance(ExtTestCase):
14+
@ignore_warnings(FutureWarning)
1415
def test_graph_load(self):
1516
this = os.path.abspath(os.path.dirname(__file__))
1617
graph = os.path.join(this, "data", "graph.gv")
@@ -19,8 +20,6 @@ def test_graph_load(self):
1920
self.assertTrue(len(paths) > 0)
2021

2122
def test_image_video_kohonen(self):
22-
temp = get_temp_folder(__file__, "temp_graph_distance")
23-
2423
graph1 = [
2524
("a", "b"),
2625
("b", "c"),
@@ -69,31 +68,17 @@ def test_image_video_kohonen(self):
6968
if distance is None:
7069
raise AssertionError("expecting something different from None")
7170

72-
outfile1 = os.path.join(temp, "unittest_GraphDistance4_sub1.png")
73-
outfile2 = os.path.join(temp, "unittest_GraphDistance4_sub2.png")
74-
outfilef = os.path.join(temp, "unittest_GraphDistance4_subf.png")
75-
7671
vertices, edges = graph1.draw_vertices_edges()
7772
self.assertNotEmpty(vertices)
7873
self.assertNotEmpty(edges)
79-
try:
80-
draw_graph_graphviz(vertices, edges, outfile1)
81-
except FileNotFoundError as e:
82-
if "No such file or directory: 'dot'" in str(e):
83-
return
84-
raise e
8574

8675
vertices, edges = graph2.draw_vertices_edges()
8776
self.assertNotEmpty(vertices)
8877
self.assertNotEmpty(edges)
89-
draw_graph_graphviz(vertices, edges, outfile2)
90-
self.assertTrue(os.path.exists(outfile2))
9178

9279
vertices, edges = graph.draw_vertices_edges()
9380
self.assertNotEmpty(vertices)
9481
self.assertNotEmpty(edges)
95-
draw_graph_graphviz(vertices, edges, outfilef)
96-
self.assertTrue(os.path.exists(outfilef))
9782

9883
def test_unittest_GraphDistance2(self):
9984
graph1 = [
@@ -174,4 +159,4 @@ def test_unittest_common_paths(self):
174159

175160

176161
if __name__ == "__main__":
177-
unittest.main()
162+
unittest.main(verbosity=2)

_unittests/ut_ml/test_matrices.py

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def test_gram_schmidt(self):
2626
res2, change2 = gram_schmidt(mat, change=True)
2727
self.assertEqual(res, res2)
2828
res3 = change2 @ mat
29-
self.assertEqual(res3, res2)
29+
self.assertEqual(res3, res2, atol=1e-8)
3030

3131
mat1 = numpy.array([[1, 0, 0], [0, 0, 1]], dtype=float)
3232
res = gram_schmidt(mat1)
@@ -58,25 +58,25 @@ def test_linear_regression(self):
5858
y = numpy.array([1, 1.3, 3.9])
5959
b1 = linear_regression(X, y)
6060
b2 = linear_regression(X, y, algo="gram")
61-
self.assertEqualArray(b1, b2)
61+
self.assertEqualArray(b1, b2, atol=1e-8)
6262

6363
def test_linear_regression_qr(self):
6464
X = numpy.array([[1, 0.5, 0], [0, 0.4, 2]], dtype=float).T
6565
y = numpy.array([1, 1.3, 3.9])
6666
b1 = linear_regression(X, y)
6767
b3 = linear_regression(X, y, algo="gram")
6868
b2 = linear_regression(X, y, algo="qr")
69-
self.assertEqualArray(b1, b3)
70-
self.assertEqualArray(b1, b2)
69+
self.assertEqualArray(b1, b3, atol=1e-8)
70+
self.assertEqualArray(b1, b2, atol=1e-8)
7171

7272
def test_linear_regression_qr3(self):
7373
X = numpy.array([[1, 0.5, 0], [0, 0.4, 2], [0, 0.4, 2.1]], dtype=float).T
7474
y = numpy.array([1, 1.3, 3.9])
7575
b1 = linear_regression(X, y)
7676
b3 = linear_regression(X, y, algo="gram")
7777
b2 = linear_regression(X, y, algo="qr")
78-
self.assertEqualArray(b1, b3)
79-
self.assertEqualArray(b1, b2)
78+
self.assertEqualArray(b1, b3, atol=1e-8)
79+
self.assertEqualArray(b1, b2, atol=1e-8)
8080

8181
def test_dim_lin_reg(self):
8282
X = rnd.randn(100, 7)
@@ -86,8 +86,8 @@ def test_dim_lin_reg(self):
8686
b1 = linear_regression(X, y)
8787
b3 = linear_regression(X, y, algo="gram")
8888
b2 = linear_regression(X, y, algo="qr")
89-
self.assertEqualArray(b1.ravel(), b3.ravel())
90-
self.assertEqualArray(b1.ravel(), b2.ravel())
89+
self.assertEqualArray(b1.ravel(), b3.ravel(), atol=1e-8)
90+
self.assertEqualArray(b1.ravel(), b2.ravel(), atol=1e-8)
9191

9292
def test_inner_code(self):
9393
X = numpy.array(
@@ -112,12 +112,12 @@ def test_inner_code(self):
112112
self.assertEqual(Tt.shape, Xt.shape)
113113
self.assertEqual(Pt.shape, (X.shape[1], X.shape[1]))
114114
_Tt = Pt @ Xt
115-
self.assertEqualArray(_Tt, Tt)
115+
self.assertEqualArray(_Tt, Tt, atol=1e-8)
116116
self.assertEqualArray(Tt @ Tt.T, numpy.identity(Tt.shape[0]), atol=1e-10)
117117

118118
beta1 = numpy.linalg.inv(Xt @ X) @ Xt @ y
119119
beta2 = Tt @ y @ Pt
120-
self.assertEqualArray(beta1, beta2)
120+
self.assertEqualArray(beta1, beta2, atol=1e-8)
121121

122122
def test_streaming_gram_schmidt(self):
123123
X0 = numpy.array(
@@ -183,7 +183,7 @@ def test_streaming_linear_regression(self):
183183
for i, bk in enumerate(streaming_linear_regression(X, y)):
184184
algo2.append(bk.copy())
185185
self.assertNotEmpty(bk)
186-
self.assertEqualArray(algo1[i], algo2[i])
186+
self.assertEqualArray(algo1[i], algo2[i], atol=1e-8)
187187
self.assertEqual(len(algo1), len(algo2))
188188

189189
def test_streaming_linear_regression_graph_schmidt(self):
@@ -213,7 +213,7 @@ def test_streaming_linear_regression_graph_schmidt(self):
213213
for i, bk in enumerate(streaming_linear_regression_gram_schmidt(X, y)):
214214
algo2.append(bk.copy())
215215
self.assertNotEmpty(bk)
216-
self.assertEqualArray(algo1[i], algo2[i])
216+
self.assertEqualArray(algo1[i], algo2[i], atol=1e-8)
217217
self.assertEqual(len(algo1), len(algo2))
218218

219219
def test_profile(self):
@@ -223,12 +223,8 @@ def test_profile(self):
223223
y = X.sum(axis=1).reshape((X.shape[0], 1)) + eps
224224
y = y.ravel()
225225
res = self.profile(lambda: list(streaming_linear_regression_gram_schmidt(X, y)))
226-
if __name__ == "__main__":
227-
print("***", res[1])
228226
self.assertIn("streaming", res[1])
229227
res = self.profile(lambda: list(streaming_linear_regression(X, y)))
230-
if __name__ == "__main__":
231-
print("***", res[1])
232228
self.assertIn("streaming", res[1])
233229

234230
def test_norm2(self):
@@ -238,4 +234,4 @@ def test_norm2(self):
238234

239235

240236
if __name__ == "__main__":
241-
unittest.main()
237+
unittest.main(verbosity=2)

_unittests/ut_ml/test_voronoi.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def test_iris(self):
3636
expected_values = numpy.array(
3737
[[3.0, 4.137], [5.044, 0.281], [5.497, 0.184]]
3838
)
39-
self.assertEqualArray(expected_values, points, decimal=2)
39+
self.assertEqualArray(expected_values, points, atol=1e-2)
4040

4141
points = voronoi_estimation_from_lr(
4242
clr.coef_, clr.intercept_, C, D, qr=True, verbose=True
@@ -45,7 +45,7 @@ def test_iris(self):
4545
expected_values = numpy.array(
4646
[[3.0, 4.137], [5.044, 0.281], [5.497, 0.184]]
4747
)
48-
self.assertEqualArray(expected_values, points, decimal=2)
48+
self.assertEqualArray(expected_values, points, atol=1e-2)
4949
std = std.getvalue()
5050
self.assertIn("[voronoi_estimation_from_lr] iter=", std)
5151

@@ -72,7 +72,7 @@ def test_iris_dim4(self):
7272
self.assertEqual(points.shape, (3, 4))
7373
points2 = voronoi_estimation_from_lr(clr.coef_, clr.intercept_, C, D, qr=True)
7474
self.assertEqual(points2.shape, (3, 4))
75-
self.assertEqualArray(points2, points2, decimal=5)
75+
self.assertEqualArray(points2, points2, atol=1e-5)
7676

7777
def test_square(self):
7878
from mlstatpy.ml.voronoi import voronoi_estimation_from_lr

mlstatpy/ext_test_case.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,14 @@ def assertStartsWith(self, prefix: str, full: str):
412412
if not full.startswith(prefix):
413413
raise AssertionError(f"prefix={prefix!r} does not start string {full!r}.")
414414

415+
def assertGreater(self, a, b):
416+
if a < b:
417+
raise AssertionError(f"{a} < {b}")
418+
419+
def assertLesser(self, a, b):
420+
if a > b:
421+
raise AssertionError(f"{a} > {b}")
422+
415423
@classmethod
416424
def tearDownClass(cls):
417425
for name, line, w in cls._warns:
@@ -430,6 +438,22 @@ def capture(self, fct: Callable):
430438
res = fct()
431439
return res, sout.getvalue(), serr.getvalue()
432440

441+
@staticmethod
442+
def profile(fct, sort="cumulative", rootrem=None, return_results=False):
443+
"""
444+
Profiles the execution of a function with function
445+
:func:`profile <pyquickhelper.pycode.profiling.profile>`.
446+
447+
:param fct: function to profile
448+
:param sort: see :meth:`pstats.Stats.sort_stats`
449+
:param rootrem: root to remove in filenames
450+
:param return_results: return the results as well
451+
:return: statistics text dump
452+
"""
453+
from onnx_array_api.profiling import profile
454+
455+
return profile(fct, sort=sort, rootrem=rootrem, return_results=return_results)
456+
433457

434458
def remove_folder(top, remove_also_top=True, raise_exception=True):
435459
"""

mlstatpy/image/detection_segment/detection_segment.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def _calcule_gradient(img, color=None):
116116
dy1 = img[1:-1, :] - img[:-2, :]
117117
dy2 = img[2:, :] - img[1:-1, :]
118118
dy = (dy1 + dy2) / 2
119-
res = numpy.zeros(*img.shape, 2)
119+
res = numpy.zeros((*img.shape, 2))
120120
res[:, 1:-1, 0] = dx
121121
res[1:-1, :, 1] = dy
122122
return res

mlstatpy/ml/matrices.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def linear_regression(X, y, algo=None):
130130
# T = P X
131131
return (y.T @ T.T @ P).ravel()
132132
if algo == "qr":
133-
Q, R = numpy.linalg.qr(X, "full")
133+
Q, R = numpy.linalg.qr(X, "reduced")
134134
Ri = dtrtri(R)[0]
135135
gamma = (y.T @ Q).ravel()
136136
return (gamma @ Ri.T).ravel()

0 commit comments

Comments
 (0)