Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add xidx and yidx #86

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

GuiyinLi
Copy link

@GuiyinLi GuiyinLi commented Feb 1, 2024

I was using dtw-python's dtwPlotThreeWay and dtwPlotTwoWay to plot my desired angle sequence and the robotic arm's actual angle sequence prior to dtw, and found that I couldn't change the horizontal coordinates to my time series (each moment corresponds to the current desired angle and the robotic arm's actual angle), which would lead to the result in the image below:
error

Due to the control delay, the lengths of the actual angle sequence (black line) and the desired angle sequence (blue line) of the robotic arm differ by about ten times, so the final plotted graph cannot fully demonstrate the point-to-point matching relationship between the two angle sequences. Therefore I would like to align these two angle sequences by my own recorded time series, the result of this timeseries branch is as follows:
correct

The plotting results before and after the dtwPlotThreeWay change are shown below:
threeway_error

threeway_correct

In the timeseries branch, I added xidx and yidx to replace d.index1 and d.index2 as the horizontal coordinates, and still use the original d.index when plotting the matching points. At the same time, xidx and yidx are defaulted to None, which is the same as in the previous matser branch. Meanwhile, xidx and yidx are defaulted to None, and then, as in the previous matser branch, when xidx and yidx are the actual time series of xts and yts, respectively, they will be plotted instead of the original horizontal coordinates.
In short, when calculating the dtw between two signal sequences of different lengths, the plots of dtwPlotThreeWay and dtwPlotTwoWay do not show all the information, and this branch can unify the reference to the horizontal coordinates when adding the corresponding time series of each of the two signal sequences, and thus show more information.
I hope that this suggestion can be added to a future version, thus compensating for the shortcomings when plotting two DTW graphs with large differences in length.

@GuiyinLi
Copy link
Author

GuiyinLi commented Feb 1, 2024

My test code:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from dtw import *

blue = '#1f77b4'

dpi_config = 500


def get_column(file: str, col: str):
    df = pd.read_csv(file)
    df.columns = df.columns.str.strip()
    return df[col].to_numpy()


pose_ts = get_column('myo.csv', 'time')
cmd_ts = get_column('control.csv', 'time')


def plt_two(dtw: DTW, query: str, ref: str):
    """
    蓝色参考值control, 黑色查询值bot
    """
    dtw.plot(type="twoway", offset=-10, xlab='index', ylab=query)
    plt.ylabel(ref, color=blue)
    plt.gca().tick_params(axis='y', colors=blue)
    plt.savefig(query + 'TO' + ref + '_visual', dpi=dpi_config,
                bbox_inches='tight', transparent=False)
    plt.close()


def plt_three(dtw: DTW, query: str, ref: str):
    """
    横坐标查询值bot, 纵坐标参考值myo
    """
    dtw.plot(type="threeway", xlab=query, ylab=ref)
    plt.savefig(query + 'TO' + ref, dpi=dpi_config,
                bbox_inches='tight', transparent=False)
    plt.close()


def get_dis_Gesture(source: str, target: str, col: str):
    con = get_column('control.csv', col)
    bot = get_column('dobot_pose.csv', col)
    dtw_con_bot = dtw(bot, con, xidx=pose_ts, yidx=cmd_ts,
                      dist_method='seuclidean', keep_internals=True, open_end=True)

    dis_con_bot = np.around(dtw_con_bot.distance, 2)

    print(source + ' <-> ' + target + ':', dis_con_bot)
    plt_two(dtw_con_bot, target, source)
    plt_three(dtw_con_bot, target, source)
    return dis_con_bot


conJ2_botJ2 = get_dis_Gesture('con_j2', 'bot_j2', 'j2')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant