Skip to content

Commit a58dcd2

Browse files
authored
test: stop checking ml large tests exact numbers (#690)
* test: stop checking ml large tests exact numbers * clean up
1 parent 9fee857 commit a58dcd2

File tree

9 files changed

+256
-540
lines changed

9 files changed

+256
-540
lines changed

tests/system/large/ml/test_cluster.py

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,11 @@
1313
# limitations under the License.
1414

1515
import pandas as pd
16-
import pytest
1716

1817
from bigframes.ml import cluster
19-
from tests.system.utils import assert_pandas_df_equal
18+
from tests.system import utils
2019

2120

22-
@pytest.mark.flaky(retries=2)
2321
def test_cluster_configure_fit_score_predict(
2422
session, penguins_df_default_index, dataset_id
2523
):
@@ -88,26 +86,18 @@ def test_cluster_configure_fit_score_predict(
8886

8987
# Check score to ensure the model was fitted
9088
score_result = model.score(new_penguins).to_pandas()
91-
score_expected = pd.DataFrame(
92-
{"davies_bouldin_index": [1.502182], "mean_squared_distance": [1.953408]},
93-
dtype="Float64",
94-
)
95-
score_expected = score_expected.reindex(index=score_expected.index.astype("Int64"))
9689

97-
pd.testing.assert_frame_equal(
98-
score_result, score_expected, check_exact=False, rtol=0.1
99-
)
90+
eval_metrics = ["davies_bouldin_index", "mean_squared_distance"]
91+
utils.check_pandas_df_schema_and_index(score_result, columns=eval_metrics, index=1)
10092

10193
predictions = model.predict(new_penguins).to_pandas()
10294
assert predictions.shape == (4, 9)
103-
result = predictions[["CENTROID_ID"]]
104-
expected = pd.DataFrame(
105-
{"CENTROID_ID": [2, 3, 1, 2]},
106-
dtype="Int64",
107-
index=pd.Index(["test1", "test2", "test3", "test4"], dtype="string[pyarrow]"),
95+
utils.check_pandas_df_schema_and_index(
96+
predictions,
97+
columns=["CENTROID_ID"],
98+
index=["test1", "test2", "test3", "test4"],
99+
col_exact=False,
108100
)
109-
expected.index.name = "observation"
110-
assert_pandas_df_equal(result, expected, ignore_order=True)
111101

112102
# save, load, check n_clusters to ensure configuration was kept
113103
reloaded_model = model.to_gbq(

tests/system/large/ml/test_compose.py

Lines changed: 29 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import pandas
16-
1715
from bigframes.ml import compose, preprocessing
16+
from tests.system import utils
1817

1918

2019
def test_columntransformer_standalone_fit_and_transform(
@@ -45,26 +44,18 @@ def test_columntransformer_standalone_fit_and_transform(
4544
)
4645
result = transformer.transform(new_penguins_df).to_pandas()
4746

48-
expected = pandas.DataFrame(
49-
{
50-
"onehotencoded_species": [
51-
[{"index": 1, "value": 1.0}],
52-
[{"index": 1, "value": 1.0}],
53-
[{"index": 2, "value": 1.0}],
54-
],
55-
"standard_scaled_culmen_length_mm": [
56-
-0.811119671289163,
57-
-0.9945520581113803,
58-
-1.104611490204711,
59-
],
60-
"min_max_scaled_culmen_length_mm": [0.269, 0.232, 0.210],
61-
"standard_scaled_flipper_length_mm": [-0.350044, -1.418336, -0.9198],
62-
},
63-
index=pandas.Index([1633, 1672, 1690], dtype="Int64", name="tag_number"),
47+
utils.check_pandas_df_schema_and_index(
48+
result,
49+
columns=[
50+
"onehotencoded_species",
51+
"standard_scaled_culmen_length_mm",
52+
"min_max_scaled_culmen_length_mm",
53+
"standard_scaled_flipper_length_mm",
54+
],
55+
index=[1633, 1672, 1690],
56+
col_exact=False,
6457
)
6558

66-
pandas.testing.assert_frame_equal(result, expected, rtol=0.1, check_dtype=False)
67-
6859

6960
def test_columntransformer_standalone_fit_transform(new_penguins_df):
7061
transformer = compose.ColumnTransformer(
@@ -86,25 +77,17 @@ def test_columntransformer_standalone_fit_transform(new_penguins_df):
8677
new_penguins_df[["species", "culmen_length_mm", "flipper_length_mm"]]
8778
).to_pandas()
8879

89-
expected = pandas.DataFrame(
90-
{
91-
"onehotencoded_species": [
92-
[{"index": 1, "value": 1.0}],
93-
[{"index": 1, "value": 1.0}],
94-
[{"index": 2, "value": 1.0}],
95-
],
96-
"standard_scaled_culmen_length_mm": [
97-
1.313249,
98-
-0.20198,
99-
-1.111118,
100-
],
101-
"standard_scaled_flipper_length_mm": [1.251098, -1.196588, -0.054338],
102-
},
103-
index=pandas.Index([1633, 1672, 1690], dtype="Int64", name="tag_number"),
80+
utils.check_pandas_df_schema_and_index(
81+
result,
82+
columns=[
83+
"onehotencoded_species",
84+
"standard_scaled_culmen_length_mm",
85+
"standard_scaled_flipper_length_mm",
86+
],
87+
index=[1633, 1672, 1690],
88+
col_exact=False,
10489
)
10590

106-
pandas.testing.assert_frame_equal(result, expected, rtol=0.1, check_dtype=False)
107-
10891

10992
def test_columntransformer_save_load(new_penguins_df, dataset_id):
11093
transformer = compose.ColumnTransformer(
@@ -147,23 +130,13 @@ def test_columntransformer_save_load(new_penguins_df, dataset_id):
147130
new_penguins_df[["species", "culmen_length_mm", "flipper_length_mm"]]
148131
).to_pandas()
149132

150-
# TODO(b/340888429): fix type error
151-
expected = pandas.DataFrame( # type: ignore
152-
{
153-
"onehotencoded_species": [
154-
[{"index": 1, "value": 1.0}],
155-
[{"index": 1, "value": 1.0}],
156-
[{"index": 2, "value": 1.0}],
157-
],
158-
"standard_scaled_culmen_length_mm": [
159-
1.313249,
160-
-0.20198,
161-
-1.111118,
162-
],
163-
"standard_scaled_flipper_length_mm": [1.251098, -1.196588, -0.054338],
164-
},
165-
index=pandas.Index([1633, 1672, 1690], dtype="Int64", name="tag_number"),
133+
utils.check_pandas_df_schema_and_index(
134+
result,
135+
columns=[
136+
"onehotencoded_species",
137+
"standard_scaled_culmen_length_mm",
138+
"standard_scaled_flipper_length_mm",
139+
],
140+
index=[1633, 1672, 1690],
141+
col_exact=False,
166142
)
167-
168-
# TODO(b/340888429): fix type error
169-
pandas.testing.assert_frame_equal(result, expected, rtol=0.1, check_dtype=False) # type: ignore

tests/system/large/ml/test_core.py

Lines changed: 41 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,10 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import pandas
16-
import pytest
17-
1815
from bigframes.ml import globals
16+
from tests.system import utils
1917

2018

21-
# TODO(garrettwu): Re-enable or not check exact numbers.
22-
@pytest.mark.skip(reason="bqml regression")
2319
def test_bqml_e2e(session, dataset_id, penguins_df_default_index, new_penguins_df):
2420
df = penguins_df_default_index.dropna()
2521
X_train = df[
@@ -38,41 +34,33 @@ def test_bqml_e2e(session, dataset_id, penguins_df_default_index, new_penguins_d
3834
X_train, y_train, options={"model_type": "linear_reg"}
3935
)
4036

37+
eval_metrics = [
38+
"mean_absolute_error",
39+
"mean_squared_error",
40+
"mean_squared_log_error",
41+
"median_absolute_error",
42+
"r2_score",
43+
"explained_variance",
44+
]
4145
# no data - report evaluation from the automatic data split
4246
evaluate_result = model.evaluate().to_pandas()
43-
evaluate_expected = pandas.DataFrame(
44-
{
45-
"mean_absolute_error": [225.817334],
46-
"mean_squared_error": [80540.705944],
47-
"mean_squared_log_error": [0.004972],
48-
"median_absolute_error": [173.080816],
49-
"r2_score": [0.87529],
50-
"explained_variance": [0.87529],
51-
},
52-
dtype="Float64",
53-
)
54-
evaluate_expected = evaluate_expected.reindex(
55-
index=evaluate_expected.index.astype("Int64")
56-
)
57-
pandas.testing.assert_frame_equal(
58-
evaluate_result, evaluate_expected, check_exact=False, rtol=0.1
47+
utils.check_pandas_df_schema_and_index(
48+
evaluate_result, columns=eval_metrics, index=1
5949
)
6050

6151
# evaluate on all training data
6252
evaluate_result = model.evaluate(df).to_pandas()
63-
pandas.testing.assert_frame_equal(
64-
evaluate_result, evaluate_expected, check_exact=False, rtol=0.1
53+
utils.check_pandas_df_schema_and_index(
54+
evaluate_result, columns=eval_metrics, index=1
6555
)
6656

6757
# predict new labels
6858
predictions = model.predict(new_penguins_df).to_pandas()
69-
expected = pandas.DataFrame(
70-
{"predicted_body_mass_g": [4030.1, 3280.8, 3177.9]},
71-
dtype="Float64",
72-
index=pandas.Index([1633, 1672, 1690], name="tag_number", dtype="Int64"),
73-
)
74-
pandas.testing.assert_frame_equal(
75-
predictions[["predicted_body_mass_g"]], expected, check_exact=False, rtol=0.1
59+
utils.check_pandas_df_schema_and_index(
60+
predictions,
61+
columns=["predicted_body_mass_g"],
62+
index=[1633, 1672, 1690],
63+
col_exact=False,
7664
)
7765

7866
new_name = f"{dataset_id}.my_model"
@@ -108,42 +96,34 @@ def test_bqml_manual_preprocessing_e2e(
10896
X_train, y_train, transforms=transforms, options=options
10997
)
11098

99+
eval_metrics = [
100+
"mean_absolute_error",
101+
"mean_squared_error",
102+
"mean_squared_log_error",
103+
"median_absolute_error",
104+
"r2_score",
105+
"explained_variance",
106+
]
107+
111108
# no data - report evaluation from the automatic data split
112109
evaluate_result = model.evaluate().to_pandas()
113-
evaluate_expected = pandas.DataFrame(
114-
{
115-
"mean_absolute_error": [309.477334],
116-
"mean_squared_error": [152184.227218],
117-
"mean_squared_log_error": [0.009524],
118-
"median_absolute_error": [257.727777],
119-
"r2_score": [0.764356],
120-
"explained_variance": [0.764356],
121-
},
122-
dtype="Float64",
123-
)
124-
evaluate_expected = evaluate_expected.reindex(
125-
index=evaluate_expected.index.astype("Int64")
126-
)
127-
128-
pandas.testing.assert_frame_equal(
129-
evaluate_result, evaluate_expected, check_exact=False, rtol=0.1
110+
utils.check_pandas_df_schema_and_index(
111+
evaluate_result, columns=eval_metrics, index=1
130112
)
131113

132114
# evaluate on all training data
133115
evaluate_result = model.evaluate(df).to_pandas()
134-
pandas.testing.assert_frame_equal(
135-
evaluate_result, evaluate_expected, check_exact=False, rtol=0.1
116+
utils.check_pandas_df_schema_and_index(
117+
evaluate_result, columns=eval_metrics, index=1
136118
)
137119

138120
# predict new labels
139121
predictions = model.predict(new_penguins_df).to_pandas()
140-
expected = pandas.DataFrame(
141-
{"predicted_body_mass_g": [3968.8, 3176.3, 3545.2]},
142-
dtype="Float64",
143-
index=pandas.Index([1633, 1672, 1690], name="tag_number", dtype="Int64"),
144-
)
145-
pandas.testing.assert_frame_equal(
146-
predictions[["predicted_body_mass_g"]], expected, check_exact=False, rtol=0.1
122+
utils.check_pandas_df_schema_and_index(
123+
predictions,
124+
columns=["predicted_body_mass_g"],
125+
index=[1633, 1672, 1690],
126+
col_exact=False,
147127
)
148128

149129
new_name = f"{dataset_id}.my_model"
@@ -168,24 +148,9 @@ def test_bqml_standalone_transform(penguins_df_default_index, new_penguins_df):
168148
)
169149

170150
transformed = model.transform(new_penguins_df).to_pandas()
171-
expected = pandas.DataFrame(
172-
{
173-
"scaled_culmen_length_mm": [-0.8099, -0.9931, -1.103],
174-
"onehotencoded_species": [
175-
[{"index": 1, "value": 1.0}],
176-
[{"index": 1, "value": 1.0}],
177-
[{"index": 2, "value": 1.0}],
178-
],
179-
},
180-
index=pandas.Index([1633, 1672, 1690], name="tag_number", dtype="Int64"),
181-
)
182-
expected["scaled_culmen_length_mm"] = expected["scaled_culmen_length_mm"].astype(
183-
"Float64"
184-
)
185-
pandas.testing.assert_frame_equal(
186-
transformed[["scaled_culmen_length_mm", "onehotencoded_species"]],
187-
expected,
188-
check_exact=False,
189-
rtol=0.1,
190-
check_dtype=False,
151+
utils.check_pandas_df_schema_and_index(
152+
transformed,
153+
columns=["scaled_culmen_length_mm", "onehotencoded_species"],
154+
index=[1633, 1672, 1690],
155+
col_exact=False,
191156
)

0 commit comments

Comments
 (0)