Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ jobs:
pip install black=="22.6.0" isort=="5.10.1" mypy=="1.0.0"
pip uninstall --yes scikit-learn
if [ ${{ matrix.sklearn_version }} == "nightly" ];
then pip install --pre --extra-index https://pypi.anaconda.org/scipy-wheels-nightly/simple scikit-learn;
then pip install --pre --extra-index https://pypi.anaconda.org/scientific-python-nightly-wheels/simple scikit-learn;
else pip install "scikit-learn~=${{ matrix.sklearn_version }}";
fi
if [ ${{ matrix.os }} == "ubuntu-latest" ];
Expand Down
15 changes: 15 additions & 0 deletions skops/io/tests/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,25 @@ def _assert_vals_equal(val1, val2):
_assert_generic_objects_equal(val1, val2)


def _clean_params(params):
# this function deals with cleaning special parameters that for one reason
# or another should be removed or modified.
params = params.copy()

# see #375
keys_to_remove = ["_has_canonical_format", "_has_sorted_indices"]
for key in keys_to_remove:
params.pop(key, None)

return params


def assert_params_equal(params1, params2):
# helper function to compare estimator dictionaries of parameters
if params1 is None and params2 is None:
return

params1, params2 = _clean_params(params1), _clean_params(params2)
assert len(params1) == len(params2)
assert set(params1.keys()) == set(params2.keys())
for key in params1:
Expand Down
18 changes: 18 additions & 0 deletions skops/io/tests/test_persist.py
Original file line number Diff line number Diff line change
Expand Up @@ -1047,3 +1047,21 @@ def test_compression_level():
dumped_compressed = dumps(model, compression=ZIP_DEFLATED, compresslevel=9)
# This reduces the size substantially
assert len(dumped_raw) > 5 * len(dumped_compressed)


@pytest.mark.parametrize("call_has_canonical_format", [False, True])
def test_sparse_matrix(call_has_canonical_format):
# see https://github.com/skops-dev/skops/pull/375

# note: this behavior is already implicitly tested by sklearn estimators
# that use sparse matrices under the hood (tfidf) but it is better to check
# the behavior explicitly
x = sparse.csr_matrix((3, 4))
if call_has_canonical_format:
x.has_canonical_format

dumped = dumps(x)
untrusted_types = get_untrusted_types(data=dumped)
y = loads(dumped, trusted=untrusted_types)

assert_params_equal(x.__dict__, y.__dict__)