Remove support for deprecated format in Python. (#10490)
This commit is contained in:
parent
2d88d17008
commit
824fba783e
@ -2636,7 +2636,7 @@ class Booster:
|
|||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
raw_format :
|
raw_format :
|
||||||
Format of output buffer. Can be `json`, `ubj` or `deprecated`.
|
Format of output buffer. Can be `json` or `ubj`.
|
||||||
|
|
||||||
Returns
|
Returns
|
||||||
-------
|
-------
|
||||||
|
|||||||
@ -75,26 +75,10 @@ class TestBoosterIO:
|
|||||||
from_ubjraw = xgb.Booster()
|
from_ubjraw = xgb.Booster()
|
||||||
from_ubjraw.load_model(ubj_raw)
|
from_ubjraw.load_model(ubj_raw)
|
||||||
|
|
||||||
if parameters.get("multi_strategy", None) != "multi_output_tree":
|
|
||||||
# Old binary model is not supported for vector leaf.
|
|
||||||
with pytest.warns(Warning, match="Model format is default to UBJSON"):
|
|
||||||
old_from_json = from_jraw.save_raw(raw_format="deprecated")
|
|
||||||
old_from_ubj = from_ubjraw.save_raw(raw_format="deprecated")
|
|
||||||
|
|
||||||
assert old_from_json == old_from_ubj
|
|
||||||
|
|
||||||
raw_json = bst.save_raw(raw_format="json")
|
raw_json = bst.save_raw(raw_format="json")
|
||||||
pretty = json.dumps(json.loads(raw_json), indent=2) + "\n\n"
|
pretty = json.dumps(json.loads(raw_json), indent=2) + "\n\n"
|
||||||
bst.load_model(bytearray(pretty, encoding="ascii"))
|
bst.load_model(bytearray(pretty, encoding="ascii"))
|
||||||
|
|
||||||
if parameters.get("multi_strategy", None) != "multi_output_tree":
|
|
||||||
# old binary model is not supported.
|
|
||||||
with pytest.warns(Warning, match="Model format is default to UBJSON"):
|
|
||||||
old_from_json = from_jraw.save_raw(raw_format="deprecated")
|
|
||||||
old_from_ubj = from_ubjraw.save_raw(raw_format="deprecated")
|
|
||||||
|
|
||||||
assert old_from_json == old_from_ubj
|
|
||||||
|
|
||||||
rng = np.random.default_rng()
|
rng = np.random.default_rng()
|
||||||
X = rng.random(size=from_jraw.num_features() * 10).reshape(
|
X = rng.random(size=from_jraw.num_features() * 10).reshape(
|
||||||
(10, from_jraw.num_features())
|
(10, from_jraw.num_features())
|
||||||
@ -126,11 +110,6 @@ class TestBoosterIO:
|
|||||||
predt_0 = booster.predict(Xy)
|
predt_0 = booster.predict(Xy)
|
||||||
|
|
||||||
with tempfile.TemporaryDirectory() as tempdir:
|
with tempfile.TemporaryDirectory() as tempdir:
|
||||||
path = os.path.join(tempdir, "model.deprecated")
|
|
||||||
with pytest.raises(ValueError, match=r".*JSON/UBJSON.*"):
|
|
||||||
with pytest.warns(Warning, match="Model format is default to UBJSON"):
|
|
||||||
booster.save_model(path)
|
|
||||||
|
|
||||||
path = os.path.join(tempdir, "model.json")
|
path = os.path.join(tempdir, "model.json")
|
||||||
booster.save_model(path)
|
booster.save_model(path)
|
||||||
booster = xgb.Booster(model_file=path)
|
booster = xgb.Booster(model_file=path)
|
||||||
@ -181,34 +160,6 @@ class TestBoosterIO:
|
|||||||
objectives_from_schema.add(j_obj["properties"]["name"]["const"])
|
objectives_from_schema.add(j_obj["properties"]["name"]["const"])
|
||||||
assert set(objectives) == objectives_from_schema
|
assert set(objectives) == objectives_from_schema
|
||||||
|
|
||||||
def test_model_binary_io(self) -> None:
|
|
||||||
model_path = "test_model_binary_io.deprecated"
|
|
||||||
parameters = {
|
|
||||||
"tree_method": "hist",
|
|
||||||
"booster": "gbtree",
|
|
||||||
"scale_pos_weight": "0.5",
|
|
||||||
}
|
|
||||||
X = np.random.random((10, 3))
|
|
||||||
y = np.random.random((10,))
|
|
||||||
dtrain = xgb.DMatrix(X, y)
|
|
||||||
bst = xgb.train(parameters, dtrain, num_boost_round=2)
|
|
||||||
with pytest.warns(Warning, match="Model format is default to UBJSON"):
|
|
||||||
bst.save_model(model_path)
|
|
||||||
bst = xgb.Booster(model_file=model_path)
|
|
||||||
os.remove(model_path)
|
|
||||||
config = json.loads(bst.save_config())
|
|
||||||
assert (
|
|
||||||
float(config["learner"]["objective"]["reg_loss_param"]["scale_pos_weight"])
|
|
||||||
== 0.5
|
|
||||||
)
|
|
||||||
|
|
||||||
buf = bst.save_raw()
|
|
||||||
from_raw = xgb.Booster()
|
|
||||||
from_raw.load_model(buf)
|
|
||||||
|
|
||||||
buf_from_raw = from_raw.save_raw()
|
|
||||||
assert buf == buf_from_raw
|
|
||||||
|
|
||||||
def test_with_pathlib(self) -> None:
|
def test_with_pathlib(self) -> None:
|
||||||
"""Saving and loading model files from paths."""
|
"""Saving and loading model files from paths."""
|
||||||
save_path = Path("model.ubj")
|
save_path = Path("model.ubj")
|
||||||
@ -269,41 +220,19 @@ class TestBoosterIO:
|
|||||||
os.rename(src, dst)
|
os.rename(src, dst)
|
||||||
|
|
||||||
with tempfile.TemporaryDirectory() as tmpdir:
|
with tempfile.TemporaryDirectory() as tmpdir:
|
||||||
path_dep = os.path.join(tmpdir, "model.deprecated")
|
|
||||||
# save into deprecated format
|
|
||||||
with pytest.warns(UserWarning, match="UBJSON"):
|
|
||||||
booster.save_model(path_dep)
|
|
||||||
|
|
||||||
path_ubj = os.path.join(tmpdir, "model.ubj")
|
path_ubj = os.path.join(tmpdir, "model.ubj")
|
||||||
rename(path_dep, path_ubj)
|
|
||||||
|
|
||||||
with pytest.raises(ValueError, match="{"):
|
|
||||||
xgb.Booster(model_file=path_ubj)
|
|
||||||
|
|
||||||
path_json = os.path.join(tmpdir, "model.json")
|
path_json = os.path.join(tmpdir, "model.json")
|
||||||
|
|
||||||
|
booster.save_model(path_ubj)
|
||||||
rename(path_ubj, path_json)
|
rename(path_ubj, path_json)
|
||||||
|
|
||||||
with pytest.raises(ValueError, match="{"):
|
with pytest.raises(ValueError, match="{"):
|
||||||
xgb.Booster(model_file=path_json)
|
xgb.Booster(model_file=path_json)
|
||||||
|
|
||||||
# save into ubj format
|
|
||||||
booster.save_model(path_ubj)
|
|
||||||
rename(path_ubj, path_dep)
|
|
||||||
# deprecated is not a recognized format internally, XGBoost can guess the
|
|
||||||
# right format
|
|
||||||
xgb.Booster(model_file=path_dep)
|
|
||||||
rename(path_dep, path_json)
|
|
||||||
with pytest.raises(ValueError, match="Expecting"):
|
|
||||||
xgb.Booster(model_file=path_json)
|
|
||||||
|
|
||||||
# save into JSON format
|
|
||||||
booster.save_model(path_json)
|
booster.save_model(path_json)
|
||||||
rename(path_json, path_dep)
|
rename(path_json, path_ubj)
|
||||||
# deprecated is not a recognized format internally, XGBoost can guess the
|
|
||||||
# right format
|
with pytest.raises(ValueError, match="{"):
|
||||||
xgb.Booster(model_file=path_dep)
|
|
||||||
rename(path_dep, path_ubj)
|
|
||||||
with pytest.raises(ValueError, match="Expecting"):
|
|
||||||
xgb.Booster(model_file=path_ubj)
|
xgb.Booster(model_file=path_ubj)
|
||||||
|
|
||||||
# save model without file extension
|
# save model without file extension
|
||||||
@ -382,11 +311,6 @@ def test_sklearn_model() -> None:
|
|||||||
from sklearn.datasets import load_digits
|
from sklearn.datasets import load_digits
|
||||||
from sklearn.model_selection import train_test_split
|
from sklearn.model_selection import train_test_split
|
||||||
|
|
||||||
with tempfile.TemporaryDirectory() as tempdir:
|
|
||||||
model_path = os.path.join(tempdir, "digits.deprecated")
|
|
||||||
with pytest.warns(Warning, match="Model format is default to UBJSON"):
|
|
||||||
save_load_model(model_path)
|
|
||||||
|
|
||||||
with tempfile.TemporaryDirectory() as tempdir:
|
with tempfile.TemporaryDirectory() as tempdir:
|
||||||
model_path = os.path.join(tempdir, "digits.model.json")
|
model_path = os.path.join(tempdir, "digits.model.json")
|
||||||
save_load_model(model_path)
|
save_load_model(model_path)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user