Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
tianlh
LightGBM-DCU
Commits
3e6156a1
Unverified
Commit
3e6156a1
authored
Jul 04, 2024
by
Nick Miller
Committed by
GitHub
Jul 04, 2024
Browse files
[ci] prevent `lgb.model` and `lgb.pkl` files being left behind after testing (#6518)
parent
497d739a
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
17 additions
and
14 deletions
+17
-14
tests/python_package_test/test_engine.py
tests/python_package_test/test_engine.py
+13
-10
tests/python_package_test/test_sklearn.py
tests/python_package_test/test_sklearn.py
+4
-4
No files found.
tests/python_package_test/test_engine.py
View file @
3e6156a1
...
@@ -1432,7 +1432,7 @@ def test_feature_name():
...
@@ -1432,7 +1432,7 @@ def test_feature_name():
assert
feature_names
==
gbm
.
feature_name
()
assert
feature_names
==
gbm
.
feature_name
()
def
test_feature_name_with_non_ascii
(
rng
):
def
test_feature_name_with_non_ascii
(
rng
,
tmp_path
):
X_train
=
rng
.
normal
(
size
=
(
100
,
4
))
X_train
=
rng
.
normal
(
size
=
(
100
,
4
))
y_train
=
rng
.
normal
(
size
=
(
100
,))
y_train
=
rng
.
normal
(
size
=
(
100
,))
# This has non-ascii strings.
# This has non-ascii strings.
...
@@ -1442,9 +1442,10 @@ def test_feature_name_with_non_ascii(rng):
...
@@ -1442,9 +1442,10 @@ def test_feature_name_with_non_ascii(rng):
gbm
=
lgb
.
train
(
params
,
lgb_train
,
num_boost_round
=
5
)
gbm
=
lgb
.
train
(
params
,
lgb_train
,
num_boost_round
=
5
)
assert
feature_names
==
gbm
.
feature_name
()
assert
feature_names
==
gbm
.
feature_name
()
gbm
.
save_model
(
"lgb.model"
)
model_path_txt
=
str
(
tmp_path
/
"lgb.model"
)
gbm
.
save_model
(
model_path_txt
)
gbm2
=
lgb
.
Booster
(
model_file
=
"lgb.model"
)
gbm2
=
lgb
.
Booster
(
model_file
=
model_path_txt
)
assert
feature_names
==
gbm2
.
feature_name
()
assert
feature_names
==
gbm2
.
feature_name
()
...
@@ -1497,7 +1498,7 @@ def test_parameters_are_loaded_from_model_file(tmp_path, capsys, rng):
...
@@ -1497,7 +1498,7 @@ def test_parameters_are_loaded_from_model_file(tmp_path, capsys, rng):
np
.
testing
.
assert_allclose
(
preds
,
orig_preds
)
np
.
testing
.
assert_allclose
(
preds
,
orig_preds
)
def
test_save_load_copy_pickle
():
def
test_save_load_copy_pickle
(
tmp_path
):
def
train_and_predict
(
init_model
=
None
,
return_model
=
False
):
def
train_and_predict
(
init_model
=
None
,
return_model
=
False
):
X
,
y
=
make_synthetic_regression
()
X
,
y
=
make_synthetic_regression
()
X_train
,
X_test
,
y_train
,
y_test
=
train_test_split
(
X
,
y
,
test_size
=
0.1
,
random_state
=
42
)
X_train
,
X_test
,
y_train
,
y_test
=
train_test_split
(
X
,
y
,
test_size
=
0.1
,
random_state
=
42
)
...
@@ -1509,17 +1510,19 @@ def test_save_load_copy_pickle():
...
@@ -1509,17 +1510,19 @@ def test_save_load_copy_pickle():
gbm
=
train_and_predict
(
return_model
=
True
)
gbm
=
train_and_predict
(
return_model
=
True
)
ret_origin
=
train_and_predict
(
init_model
=
gbm
)
ret_origin
=
train_and_predict
(
init_model
=
gbm
)
other_ret
=
[]
other_ret
=
[]
gbm
.
save_model
(
"lgb.model"
)
model_path_txt
=
str
(
tmp_path
/
"lgb.model"
)
with
open
(
"lgb.model"
)
as
f
:
# check all params are logged into model file correctly
gbm
.
save_model
(
model_path_txt
)
with
open
(
model_path_txt
)
as
f
:
# check all params are logged into model file correctly
assert
f
.
read
().
find
(
"[num_iterations: 10]"
)
!=
-
1
assert
f
.
read
().
find
(
"[num_iterations: 10]"
)
!=
-
1
other_ret
.
append
(
train_and_predict
(
init_model
=
"lgb.model"
))
other_ret
.
append
(
train_and_predict
(
init_model
=
model_path_txt
))
gbm_load
=
lgb
.
Booster
(
model_file
=
"lgb.model"
)
gbm_load
=
lgb
.
Booster
(
model_file
=
model_path_txt
)
other_ret
.
append
(
train_and_predict
(
init_model
=
gbm_load
))
other_ret
.
append
(
train_and_predict
(
init_model
=
gbm_load
))
other_ret
.
append
(
train_and_predict
(
init_model
=
copy
.
copy
(
gbm
)))
other_ret
.
append
(
train_and_predict
(
init_model
=
copy
.
copy
(
gbm
)))
other_ret
.
append
(
train_and_predict
(
init_model
=
copy
.
deepcopy
(
gbm
)))
other_ret
.
append
(
train_and_predict
(
init_model
=
copy
.
deepcopy
(
gbm
)))
with
open
(
"lgb.pkl"
,
"wb"
)
as
f
:
model_path_pkl
=
str
(
tmp_path
/
"lgb.pkl"
)
with
open
(
model_path_pkl
,
"wb"
)
as
f
:
pickle
.
dump
(
gbm
,
f
)
pickle
.
dump
(
gbm
,
f
)
with
open
(
"lgb.
pkl
"
,
"rb"
)
as
f
:
with
open
(
model_path_
pkl
,
"rb"
)
as
f
:
gbm_pickle
=
pickle
.
load
(
f
)
gbm_pickle
=
pickle
.
load
(
f
)
other_ret
.
append
(
train_and_predict
(
init_model
=
gbm_pickle
))
other_ret
.
append
(
train_and_predict
(
init_model
=
gbm_pickle
))
gbm_pickles
=
pickle
.
loads
(
pickle
.
dumps
(
gbm
))
gbm_pickles
=
pickle
.
loads
(
pickle
.
dumps
(
gbm
))
...
...
tests/python_package_test/test_sklearn.py
View file @
3e6156a1
...
@@ -462,7 +462,7 @@ def test_clone_and_property():
...
@@ -462,7 +462,7 @@ def test_clone_and_property():
assert
isinstance
(
clf
.
feature_importances_
,
np
.
ndarray
)
assert
isinstance
(
clf
.
feature_importances_
,
np
.
ndarray
)
def
test_joblib
():
def
test_joblib
(
tmp_path
):
X
,
y
=
make_synthetic_regression
()
X
,
y
=
make_synthetic_regression
()
X_train
,
X_test
,
y_train
,
y_test
=
train_test_split
(
X
,
y
,
test_size
=
0.1
,
random_state
=
42
)
X_train
,
X_test
,
y_train
,
y_test
=
train_test_split
(
X
,
y
,
test_size
=
0.1
,
random_state
=
42
)
gbm
=
lgb
.
LGBMRegressor
(
n_estimators
=
10
,
objective
=
custom_asymmetric_obj
,
verbose
=-
1
,
importance_type
=
"split"
)
gbm
=
lgb
.
LGBMRegressor
(
n_estimators
=
10
,
objective
=
custom_asymmetric_obj
,
verbose
=-
1
,
importance_type
=
"split"
)
...
@@ -473,9 +473,9 @@ def test_joblib():
...
@@ -473,9 +473,9 @@ def test_joblib():
eval_metric
=
mse
,
eval_metric
=
mse
,
callbacks
=
[
lgb
.
early_stopping
(
5
),
lgb
.
reset_parameter
(
learning_rate
=
list
(
np
.
arange
(
1
,
0
,
-
0.1
)))],
callbacks
=
[
lgb
.
early_stopping
(
5
),
lgb
.
reset_parameter
(
learning_rate
=
list
(
np
.
arange
(
1
,
0
,
-
0.1
)))],
)
)
model_path_pkl
=
str
(
tmp_path
/
"lgb.pkl"
)
joblib
.
dump
(
gbm
,
"lgb.
pkl
"
)
# test model with custom functions
joblib
.
dump
(
gbm
,
model_path_
pkl
)
# test model with custom functions
gbm_pickle
=
joblib
.
load
(
"lgb.
pkl
"
)
gbm_pickle
=
joblib
.
load
(
model_path_
pkl
)
assert
isinstance
(
gbm_pickle
.
booster_
,
lgb
.
Booster
)
assert
isinstance
(
gbm_pickle
.
booster_
,
lgb
.
Booster
)
assert
gbm
.
get_params
()
==
gbm_pickle
.
get_params
()
assert
gbm
.
get_params
()
==
gbm_pickle
.
get_params
()
np
.
testing
.
assert_array_equal
(
gbm
.
feature_importances_
,
gbm_pickle
.
feature_importances_
)
np
.
testing
.
assert_array_equal
(
gbm
.
feature_importances_
,
gbm_pickle
.
feature_importances_
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment