Skip to content

Commit cdb6195

Browse files
Fix indentation errors in tests (#153)
moves the asserts for error message outside of the assertRaises context the tests for the error messages were ignored because they were within the assertRaises context
1 parent 1ccb509 commit cdb6195

13 files changed

+217
-220
lines changed

skcosmo/preprocessing/_data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ def fit(self, Knm, Kmm, y=None, sample_weight=None):
504504

505505
if Knm.shape[1] != Kmm.shape[0]:
506506
raise ValueError(
507-
"The reference kernel is not commensurate shape with the"
507+
"The reference kernel is not commensurate shape with the "
508508
"active kernel."
509509
)
510510

skcosmo/sample_selection/_voronoi_fps.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,13 +183,13 @@ def _init_greedy_search(self, X, y, n_to_select):
183183
if isinstance(self.full_fraction, numbers.Real):
184184
if not 0 < self.full_fraction <= 1:
185185
raise ValueError(
186-
"Switching point should be real and more than 0 and less than 1",
187-
f"received {self.full_fraction}",
186+
"Switching point should be real and more than 0 and less than 1. "
187+
f"Received {self.full_fraction}"
188188
)
189189
else:
190190
raise ValueError(
191-
"Switching point should be real and more than 0 and less than 1",
192-
f"received {self.full_fraction}",
191+
"Switching point should be real and more than 0 and less than 1. "
192+
f"Received {self.full_fraction}"
193193
)
194194

195195
super()._init_greedy_search(X, y, n_to_select)

tests/test_feature_pcov_fps.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,10 @@ def test_no_mixing_1(self):
3131

3232
with self.assertRaises(ValueError) as cm:
3333
_ = PCovFPS(n_to_select=1, mixing=1.0)
34-
self.assertEquals(
35-
str(cm.message),
36-
"Mixing = 1.0 corresponds to traditional FPS."
37-
"Please use the FPS class.",
38-
)
34+
self.assertEqual(
35+
str(cm.exception),
36+
"Mixing = 1.0 corresponds to traditional FPS." "Please use the FPS class.",
37+
)
3938

4039

4140
if __name__ == "__main__":

tests/test_feature_simple_fps.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ def test_initialize(self):
4545
with self.assertRaises(ValueError) as cm:
4646
selector = FPS(n_to_select=1, initialize="bad")
4747
selector.fit(self.X)
48-
self.assertEquals(
49-
str(cm.message), "Invalid value of the initialize parameter"
50-
)
48+
self.assertEqual(str(cm.exception), "Invalid value of the initialize parameter")
5149

5250
def test_get_distances(self):
5351
"""

tests/test_greedy_selector.py

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,19 @@ def test_score_threshold(self):
4444
def test_score_threshold_and_full(self):
4545
with self.assertRaises(ValueError) as cm:
4646
_ = GreedyTester(score_threshold=20, full=True, n_to_select=12).fit(self.X)
47-
self.assertEqual(
48-
str(cm.message),
49-
"You cannot specify both `score_threshold` and `full=True`.",
50-
)
47+
self.assertEqual(
48+
str(cm.exception),
49+
"You cannot specify both `score_threshold` and `full=True`.",
50+
)
5151

5252
def test_bad_warm_start(self):
5353
selector = GreedyTester()
5454
with self.assertRaises(ValueError) as cm:
5555
selector.fit(self.X, warm_start=True)
56-
self.assertTrue(
57-
str(cm.message),
58-
"Cannot fit with warm_start=True without having been previously initialized",
59-
)
56+
self.assertTrue(
57+
str(cm.exception),
58+
"Cannot fit with warm_start=True without having been previously initialized",
59+
)
6060

6161
def test_bad_y(self):
6262
self.X, self.Y = get_dataset(return_X_y=True)
@@ -66,11 +66,12 @@ def test_bad_y(self):
6666

6767
def test_bad_transform(self):
6868
selector = GreedyTester(n_to_select=2)
69+
selector.fit(self.X)
6970
with self.assertRaises(ValueError) as cm:
7071
_ = selector.transform(self.X[:, :3])
71-
self.assertEqual(
72-
str(cm.message), "X has a different shape than during fitting."
73-
)
72+
self.assertEqual(
73+
str(cm.exception), "X has a different shape than during fitting."
74+
)
7475

7576
def test_no_nfeatures(self):
7677
selector = GreedyTester()
@@ -88,17 +89,16 @@ def test_bad_nfeatures(self):
8889
selector = GreedyTester(n_to_select=nf)
8990
with self.assertRaises(ValueError) as cm:
9091
selector.fit(self.X)
91-
self.assertEqual(
92-
str(cm.message),
93-
(
94-
"n_to_select must be either None, an "
95-
"integer in [1, n_features - 1] "
96-
"representing the absolute "
97-
"number of features, or a float in (0, 1] "
98-
"representing a percentage of features to "
99-
f"select. Got {nf}"
100-
),
101-
)
92+
self.assertEqual(
93+
str(cm.exception),
94+
(
95+
"n_to_select must be either None, an integer in "
96+
"[1, n_features] representing the absolute number "
97+
"of features, or a float in (0, 1] representing a "
98+
f"percentage of features to select. Got {nf} "
99+
f"features and an input with {self.X.shape[1]} feature."
100+
),
101+
)
102102

103103
def test_not_fitted(self):
104104
with self.assertRaises(NotFittedError):
@@ -120,18 +120,18 @@ def test_size_input(self):
120120
selector_sample.fit(X)
121121
with self.assertRaises(ValueError) as cm:
122122
selector_feature.fit(X)
123-
self.assertEqual(
124-
str(cm.message),
125-
"Found array with 1 feature(s) (shape=(5, 1)) while a minimum of 2 is required.",
126-
)
123+
self.assertEqual(
124+
str(cm.exception),
125+
"Found array with 1 feature(s) (shape=(5, 1)) while a minimum of 2 is required.",
126+
)
127127
X = X.reshape(1, -1)
128128
selector_feature.fit(X)
129129
with self.assertRaises(ValueError) as cm:
130130
selector_sample.fit(X)
131-
self.assertEqual(
132-
str(cm.message),
133-
"Found array with 1 sample(s) (shape=(1, 5)) while a minimum of 2 is required.",
134-
)
131+
self.assertEqual(
132+
str(cm.exception),
133+
"Found array with 1 sample(s) (shape=(1, 5)) while a minimum of 2 is required.",
134+
)
135135

136136

137137
if __name__ == "__main__":

tests/test_kernel_pcovr.py

Lines changed: 65 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -232,13 +232,13 @@ def test_regressor_modifications(self):
232232
# kernel parameters now inconsistent
233233
with self.assertRaises(ValueError) as cm:
234234
kpcovr.fit(self.X, self.Y)
235-
self.assertTrue(
236-
str(cm.message),
237-
"Kernel parameter mismatch: the regressor has kernel parameters "
238-
"{kernel: linear, gamma: 0.2, degree: 3, coef0: 1, kernel_params: None}"
239-
" and KernelPCovR was initialized with kernel parameters "
240-
"{kernel: linear, gamma: 0.1, degree: 3, coef0: 1, kernel_params: None}",
241-
)
235+
self.assertTrue(
236+
str(cm.exception),
237+
"Kernel parameter mismatch: the regressor has kernel parameters "
238+
"{kernel: linear, gamma: 0.2, degree: 3, coef0: 1, kernel_params: None}"
239+
" and KernelPCovR was initialized with kernel parameters "
240+
"{kernel: linear, gamma: 0.1, degree: 3, coef0: 1, kernel_params: None}",
241+
)
242242

243243
def test_incompatible_regressor(self):
244244
regressor = Ridge(alpha=1e-8)
@@ -247,10 +247,10 @@ def test_incompatible_regressor(self):
247247

248248
with self.assertRaises(ValueError) as cm:
249249
kpcovr.fit(self.X, self.Y)
250-
self.assertTrue(
251-
str(cm.message),
252-
"Regressor must be an instance of `KernelRidge`",
253-
)
250+
self.assertTrue(
251+
str(cm.exception),
252+
"Regressor must be an instance of `KernelRidge`",
253+
)
254254

255255
def test_none_regressor(self):
256256
kpcovr = KernelPCovR(mixing=0.5, regressor=None)
@@ -269,24 +269,24 @@ def test_incompatible_coef_shape(self):
269269
# Dimension mismatch
270270
with self.assertRaises(ValueError) as cm:
271271
kpcovr.fit(self.X, self.Y[:, 0])
272-
self.assertTrue(
273-
str(cm.message),
274-
"The regressor coefficients have a dimension incompatible "
275-
"with the supplied target space. "
276-
"The coefficients have dimension %d and the targets "
277-
"have dimension %d" % (regressor.dual_coef_.ndim, self.Y[:, 0].ndim),
278-
)
272+
self.assertTrue(
273+
str(cm.exception),
274+
"The regressor coefficients have a dimension incompatible "
275+
"with the supplied target space. "
276+
"The coefficients have dimension %d and the targets "
277+
"have dimension %d" % (regressor.dual_coef_.ndim, self.Y[:, 0].ndim),
278+
)
279279

280280
# Shape mismatch (number of targets)
281281
with self.assertRaises(ValueError) as cm:
282282
kpcovr.fit(self.X, self.Y)
283-
self.assertTrue(
284-
str(cm.message),
285-
"The regressor coefficients have a shape incompatible "
286-
"with the supplied target space. "
287-
"The coefficients have shape %r and the targets "
288-
"have shape %r" % (regressor.dual_coef_.shape, self.Y.shape),
289-
)
283+
self.assertTrue(
284+
str(cm.exception),
285+
"The regressor coefficients have a shape incompatible "
286+
"with the supplied target space. "
287+
"The coefficients have shape %r and the targets "
288+
"have shape %r" % (regressor.dual_coef_.shape, self.Y.shape),
289+
)
290290

291291
def test_precomputed_regression(self):
292292
regressor = KernelRidge(alpha=1e-8, kernel="rbf", gamma=0.1)
@@ -422,7 +422,7 @@ def test_bad_solver(self):
422422
kpcovr = self.model(svd_solver="bad")
423423
kpcovr.fit(self.X, self.Y)
424424

425-
self.assertTrue(str(cm.message), "Unrecognized svd_solver='bad'" "")
425+
self.assertTrue(str(cm.exception), "Unrecognized svd_solver='bad'" "")
426426

427427
def test_good_n_components(self):
428428
"""
@@ -454,60 +454,60 @@ def test_bad_n_components(self):
454454
kpcovr = self.model(n_components=-1, svd_solver="auto")
455455
kpcovr.fit(self.X, self.Y)
456456

457-
self.assertTrue(
458-
str(cm.message),
459-
"self.n_components=%r must be between 0 and "
460-
"min(n_samples, n_features)=%r with "
461-
"svd_solver='%s'"
462-
% (
463-
kpcovr.n_components,
464-
self.X.shape[0],
465-
kpcovr.svd_solver,
466-
),
467-
)
457+
self.assertTrue(
458+
str(cm.exception),
459+
"self.n_components=%r must be between 0 and "
460+
"min(n_samples, n_features)=%r with "
461+
"svd_solver='%s'"
462+
% (
463+
kpcovr.n_components,
464+
self.X.shape[0],
465+
kpcovr.svd_solver,
466+
),
467+
)
468468
with self.subTest(type="0_ncomponents"):
469469
with self.assertRaises(ValueError) as cm:
470470
kpcovr = self.model(n_components=0, svd_solver="randomized")
471471
kpcovr.fit(self.X, self.Y)
472472

473-
self.assertTrue(
474-
str(cm.message),
475-
"self.n_components=%r must be between 1 and "
476-
"min(n_samples, n_features)=%r with "
477-
"svd_solver='%s'"
478-
% (
479-
kpcovr.n_components,
480-
self.X.shape[0],
481-
kpcovr.svd_solver,
482-
),
483-
)
473+
self.assertTrue(
474+
str(cm.exception),
475+
"self.n_components=%r must be between 1 and "
476+
"min(n_samples, n_features)=%r with "
477+
"svd_solver='%s'"
478+
% (
479+
kpcovr.n_components,
480+
self.X.shape[0],
481+
kpcovr.svd_solver,
482+
),
483+
)
484484
with self.subTest(type="arpack_X_ncomponents"):
485485
with self.assertRaises(ValueError) as cm:
486486
kpcovr = self.model(n_components=self.X.shape[0], svd_solver="arpack")
487487
kpcovr.fit(self.X, self.Y)
488-
self.assertTrue(
489-
str(cm.message),
490-
"self.n_components=%r must be strictly less than "
491-
"min(n_samples, n_features)=%r with "
492-
"svd_solver='%s'"
493-
% (
494-
kpcovr.n_components,
495-
self.X.shape[0],
496-
kpcovr.svd_solver,
497-
),
498-
)
488+
self.assertTrue(
489+
str(cm.exception),
490+
"self.n_components=%r must be strictly less than "
491+
"min(n_samples, n_features)=%r with "
492+
"svd_solver='%s'"
493+
% (
494+
kpcovr.n_components,
495+
self.X.shape[0],
496+
kpcovr.svd_solver,
497+
),
498+
)
499499

500500
for svd_solver in ["auto", "full"]:
501501
with self.subTest(type="pi_ncomponents"):
502502
with self.assertRaises(ValueError) as cm:
503503
kpcovr = self.model(n_components=np.pi, svd_solver=svd_solver)
504504
kpcovr.fit(self.X, self.Y)
505-
self.assertTrue(
506-
str(cm.message),
507-
"self.n_components=%r must be of type int "
508-
"when greater than or equal to 1, was of type=%r"
509-
% (kpcovr.n_components, type(kpcovr.n_components)),
510-
)
505+
self.assertTrue(
506+
str(cm.exception),
507+
"self.n_components=%r must be of type int "
508+
"when greater than or equal to 1, was of type=%r"
509+
% (kpcovr.n_components, type(kpcovr.n_components)),
510+
)
511511

512512

513513
if __name__ == "__main__":

tests/test_orthogonalizers.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,11 @@ def test_multicolumn(self):
130130
-3, 3, size=(self.n_samples + 4, self.n_features)
131131
),
132132
)
133-
self.assertEqual(
134-
str(cm.message),
135-
"You can only orthogonalize a matrix using a vector with the same number of rows."
136-
f"Matrix X has {self.n_samples} rows, whereas the orthogonalizing matrix has {self.n_samples+4} rows.",
137-
)
133+
self.assertEqual(
134+
str(cm.exception),
135+
"You can only orthogonalize a matrix using a vector with the same number of rows."
136+
f"Matrix X has {self.n_samples} rows, whereas the orthogonalizing matrix has {self.n_samples+4} rows.",
137+
)
138138

139139
def test_warning(self):
140140
# checks that a warning is raised when trying to orthogonalize by

0 commit comments

Comments
 (0)