wannesm / dtaidistance

Time series distances: Dynamic Time Warping (fast DTW implementation in C)
Other
1.08k stars 184 forks source link

error in plotting #191

Open gg4u opened 1 year ago

gg4u commented 1 year ago

I tried:

from dtaidistance import clustering
# Custom Hierarchical clustering
model1 = clustering.Hierarchical(dtw.distance_matrix_fast, {})
cluster_idx = model1.fit(series)
# Augment Hierarchical object to keep track of the full tree
model2 = clustering.HierarchicalTree(model1)
cluster_idx = model2.fit(series)
# SciPy linkage clustering
model3 = clustering.LinkageTree(dtw.distance_matrix_fast, {})
cluster_idx = model3.fit(series)

and

model3.plot()

but error:


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[153], line 1
----> 1 model3.plot()

File /data0/home/h21/luas6629/venv/lib/python3.10/site-packages/dtaidistance/clustering/hierarchical.py:383, in BaseTree.plot(self, filename, axes, ts_height, bottom_margin, top_margin, ts_left_margin, ts_sample_length, tr_label_margin, tr_left_margin, ts_label_margin, show_ts_label, show_tr_label, cmap, ts_color)
    380         cnt_ts = plot_i(child_right, depth + 1, cnt_ts, prev_lcnt + clcntr, ax, False)
    381     return cnt_ts
--> 383 plot_i(self.maxnode, 0, 0, node_props[self.maxnode][2], ax, True)
    385 if filename:
    386     if isinstance(filename, Path):

File /data0/home/h21/luas6629/venv/lib/python3.10/site-packages/dtaidistance/clustering/hierarchical.py:366, in BaseTree.plot.<locals>.plot_i(node, depth, cnt_ts, prev_lcnt, ax, left)
    364 ax[0].add_line(Line2D((px, px), (py, cy), lw=1, color=color, axes=ax[0]))
    365 ax[0].add_line(Line2D((px, cx), (cy, cy), lw=1, color=color, axes=ax[0]))
--> 366 cnt_ts = plot_i(child_left, depth + 1, cnt_ts, prev_lcnt - crcntl, ax, True)
    368 # Right
    369 ccnt, cdepth, clcntr, crcntr, crcdist = node_props[child_right]

File /data0/home/h21/luas6629/venv/lib/python3.10/site-packages/dtaidistance/clustering/hierarchical.py:366, in BaseTree.plot.<locals>.plot_i(node, depth, cnt_ts, prev_lcnt, ax, left)
    364 ax[0].add_line(Line2D((px, px), (py, cy), lw=1, color=color, axes=ax[0]))
    365 ax[0].add_line(Line2D((px, cx), (cy, cy), lw=1, color=color, axes=ax[0]))
--> 366 cnt_ts = plot_i(child_left, depth + 1, cnt_ts, prev_lcnt - crcntl, ax, True)
    368 # Right
    369 ccnt, cdepth, clcntr, crcntr, crcdist = node_props[child_right]

File /data0/home/h21/luas6629/venv/lib/python3.10/site-packages/dtaidistance/clustering/hierarchical.py:366, in BaseTree.plot.<locals>.plot_i(node, depth, cnt_ts, prev_lcnt, ax, left)
    364 ax[0].add_line(Line2D((px, px), (py, cy), lw=1, color=color, axes=ax[0]))
    365 ax[0].add_line(Line2D((px, cx), (cy, cy), lw=1, color=color, axes=ax[0]))
--> 366 cnt_ts = plot_i(child_left, depth + 1, cnt_ts, prev_lcnt - crcntl, ax, True)
    368 # Right
    369 ccnt, cdepth, clcntr, crcntr, crcdist = node_props[child_right]

File /data0/home/h21/luas6629/venv/lib/python3.10/site-packages/dtaidistance/clustering/hierarchical.py:343, in BaseTree.plot.<locals>.plot_i(node, depth, cnt_ts, prev_lcnt, ax, left)
    341     else:
    342         curcolor = None
--> 343     line, = ax[1].plot(ts_left_margin + ts_sample_length * np.arange(len(serie)),
    344                        bottom_margin + ts_height * (cnt_ts + 0.5) + self.ts_height_factor * serie,
    345                        color=curcolor)
    346     cnt_ts += 1
    348 else:

ValueError: too many values to unpack (expected 1)

Can you show example ?

How does the library handle audio files as well, for computing DTW between audio ? I tried to compute DTW and works fine, but plotting seems an issue, also plotting the "mapping" of audio large numpy series reflecting the audio (from librosa).

bramiozo commented 1 year ago

This should fix it: https://github.com/wannesm/dtaidistance/pull/192