tslearn-team / tslearn

The machine learning toolkit for time series analysis in Python
https://tslearn.readthedocs.io
BSD 2-Clause "Simplified" License
2.83k stars 331 forks source link

Fix error in function from_hdf5 #480

Closed YannCabanes closed 9 months ago

YannCabanes commented 9 months ago

A test is failing for Python 3.9 and Python 3.10: FAILED tslearn/tests/test_serialize_models.py::test_serialize_kshape The error is the following:

____________________________ test_serialize_kshape _____________________________

    def test_serialize_kshape():
        n, sz, d = 15, 10, 3
        rng = numpy.random.RandomState(0)
        time_series = rng.randn(n, sz, d)
        X = TimeSeriesScalerMeanVariance().fit_transform(time_series)

        ks = KShape(n_clusters=3, verbose=True)

        _check_not_fitted(ks)

        ks.fit(X)

        _check_params_predict(ks, X, ['predict'])

        seed_ixs = [numpy.random.randint(0, X.shape[0] - 1) for i in range(3)]
        seeds = numpy.array([X[i] for i in seed_ixs])

        ks_seeded = KShape(n_clusters=3, verbose=True, init=seeds)

        _check_not_fitted(ks_seeded)

        ks_seeded.fit(X)

>       _check_params_predict(ks_seeded, X, ['predict'])

tslearn/tests/test_serialize_models.py:185: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
tslearn/tests/test_serialize_models.py:86: in _check_params_predict
    sm = getattr(model, "from_{}".format(fmt))(
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

cls = <class 'tslearn.clustering.kshape.KShape'>
path = '/home/vsts/work/1/s/tslearn/tests/tmp/KShape.hdf5'

    @classmethod
    def from_hdf5(cls, path):
        """
        Load model from a HDF5 file.
        Requires ``h5py`` http://docs.h5py.org/

        for k in model['hyper_params'].keys():
>           if model['hyper_params'][k] == 'None':
E           ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

tslearn/bases/bases.py:207: ValueError
codecov-commenter commented 9 months ago

Codecov Report

All modified lines are covered by tests :white_check_mark:

Comparison is base (cac29f5) 92.79% compared to head (2476236) 92.80%.

:exclamation: Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #480 +/- ## ======================================= Coverage 92.79% 92.80% ======================================= Files 67 67 Lines 5583 5584 +1 ======================================= + Hits 5181 5182 +1 Misses 402 402 ``` | [Files](https://app.codecov.io/gh/tslearn-team/tslearn/pull/480?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=tslearn-team) | Coverage Δ | | |---|---|---| | [tslearn/bases/bases.py](https://app.codecov.io/gh/tslearn-team/tslearn/pull/480?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=tslearn-team#diff-dHNsZWFybi9iYXNlcy9iYXNlcy5weQ==) | `92.37% <100.00%> (+0.06%)` | :arrow_up: |

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.