tslearn icon indicating copy to clipboard operation
tslearn copied to clipboard

A shallow copy of init_barycenter is used within dtw_barycenter_averaging_subgradient

Open PebetoUofC opened this issue 3 years ago • 4 comments

Describe the bug Setting init_barycenter in dtw_barycenter_averaging_subgradient generates an AttributeError exception. Not specifying init_barycenter, no exception is raised

To Reproduce time_series = [np.array([1, 2, 3, 4]), np.array([1, 2, 4, 5])] dtw_barycenter_averaging_subgradient(time_series, init_barycenter = np.array([0, 0, 0, 0]), max_iter=10, random_state=0, verbose = True)

Expected behavior Similar behavior as running time_series = [np.array([1, 2, 3, 4]), np.array([1, 2, 4, 5])] dtw_barycenter_averaging_subgradient(time_series, max_iter=10, random_state=0, verbose = True)

Environment (please complete the following information):

  • Windows 10
  • Python version 3.10.2
  • tslearn version 0.5.2

Additional context

PebetoUofC avatar Mar 11 '22 18:03 PebetoUofC

Please make sure the time series are in the right format. We provide utility functions for this in tslearn.utils (to_time_series and to_time_series_dataset)

>>> from tslearn.utils import to_time_series, to_time_series_dataset
>>> import numpy as np
>>> from tslearn.barycenters import dtw_barycenter_averaging_subgradient
>>> time_series = to_time_series_dataset([np.array([1, 2, 3, 4]), np.array([1, 2, 4, 5])])
>>> initial = to_time_series(np.array([0,0,0,0]))
>>> dtw_barycenter_averaging_subgradient(time_series, init_barycenter = initial, max_iter=10, random_state=0, verbose = True)
[DBA] epoch 1, cost: 23.000
[DBA] epoch 1, cost: 11.255
[DBA] epoch 2, cost: 8.842
[DBA] epoch 2, cost: 14.619
array([[0.16642495],
       [0.2590444 ],
       [0.4981888 ],
       [0.978247  ]])
>>> initial
array([[0.16642495],
       [0.2590444 ],
       [0.4981888 ],
       [0.978247  ]])

GillesVandewiele avatar Mar 12 '22 08:03 GillesVandewiele

I am actually re-opening this issue as I didn't notice that only a shallow copy of initial is used within the dtw_barycenter_averaging_subgradient which changes the provided initial in-place, while this is probably not wanted behaviour. Instead a deep copy should be taken.

GillesVandewiele avatar Mar 12 '22 09:03 GillesVandewiele

Hello all and thank you for an amazing toolkit! I've been meaning to file an issue about this for a while, as I got snagged by it recently. I'll check the docs about contributing and see if I can get a PR together soon (although likely not before February starts).

fmailhot avatar Jan 26 '24 11:01 fmailhot

Hi @fmailhot , that would be a pleasure to welcome you onboard! :)

rtavenar avatar Jan 26 '24 11:01 rtavenar