How to use dtaidistance - 10 common examples

To help you get started, we’ve selected a few dtaidistance examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github wannesm / dtaidistance / tests / test_dtw_weighted.py View on Github external
def plot_margins(serie, weights, clfs, importances=None):
    global directory
    if directory is None:
        directory = prepare_directory()
    from sklearn import tree
    feature_names = ["f{} ({}, {})".format(i // 2, i, '-' if (i % 2) == 0 else '+') for i in range(2 * len(serie))]
    out_str = io.StringIO()
    for clf in clfs:
        tree.export_graphviz(clf, out_file=out_str, feature_names=feature_names)
        print("\n\n", file=out_str)
    with open(str(directory / "tree.dot"), "w") as ofile:
        print(out_str.getvalue(), file=ofile)
    dtww.plot_margins(serie, weights, filename=str(directory / "margins.png"), importances=importances)
github wannesm / dtaidistance / tests / test_dtw_weighted.py View on Github external
def plot_margins(serie, weights, clfs, importances=None):
    global directory
    if directory is None:
        directory = prepare_directory()
    from sklearn import tree
    feature_names = ["f{} ({}, {})".format(i // 2, i, '-' if (i % 2) == 0 else '+') for i in range(2 * len(serie))]
    out_str = io.StringIO()
    for clf in clfs:
        tree.export_graphviz(clf, out_file=out_str, feature_names=feature_names)
        print("\n\n", file=out_str)
    with open(str(directory / "tree.dot"), "w") as ofile:
        print(out_str.getvalue(), file=ofile)
    dtww.plot_margins(serie, weights, filename=str(directory / "margins.png"), importances=importances)
github wannesm / dtaidistance / tests / test_benchmark.py View on Github external
def d():
        c = clustering.LinkageTree(dtw.distance_matrix_fast, {})
        return c.fit(s)
github wannesm / dtaidistance / tests / test_clustering.py View on Github external
def test_clustering():
    s = np.array([
         [0., 0, 1, 2, 1, 0, 1, 0, 0],
         [0., 1, 2, 0, 0, 0, 0, 0, 0],
         [1., 2, 0, 0, 0, 0, 0, 1, 1],
         [0., 0, 1, 2, 1, 0, 1, 0, 0],
         [0., 1, 2, 0, 0, 0, 0, 0, 0],
         [1., 2, 0, 0, 0, 0, 0, 1, 1]])

    def test_hook(from_idx, to_idx, distance):
        assert (from_idx, to_idx) in [(3, 0), (4, 1), (5, 2), (1, 0)]
    model = clustering.Hierarchical(dtw.distance_matrix_fast, {}, 2, merge_hook=test_hook,
                                    show_progress=False)
    cluster_idx = model.fit(s)
    assert cluster_idx[0] == {0, 1, 3, 4}
    assert cluster_idx[2] == {2, 5}
github wannesm / dtaidistance / tests / test_clustering.py View on Github external
def test_plotbug1():
    s1 = np.array([0., 0, 1, 2, 1, 0, 1, 0, 0, 2, 1, 0, 0])
    s2 = np.array([0., 1, 2, 3, 1, 0, 0, 0, 2, 1, 0, 0])

    series = s1, s2

    m = clustering.LinkageTree(dtw.distance_matrix, {})
    m.fit(series)

    if directory:
        hierarchy_fn = os.path.join(directory, "clustering.png")
    else:
        file = tempfile.NamedTemporaryFile()
        hierarchy_fn = file.name + "_clustering.png"
    m.plot(hierarchy_fn)
    print("Figure save to", hierarchy_fn)
github wannesm / dtaidistance / tests / test_bugsvis.py View on Github external
def test_bug3():
    series = np.array([
        np.array([1, 2, 1]),
        np.array([0., 1, 2, 0, 0, 0, 0, 0, 0]),
        np.array([1., 2, 0, 0, 0, 0, 0, 1, 1, 3, 4, 5]),
        np.array([0., 0, 1, 2, 1, 0, 1]),
        np.array([0., 1, 2, 0, 0, 0, 0, 0]),
        np.array([1., 2, 0, 0, 0, 0, 0, 1, 1])])
    ds = dtw.distance_matrix(series)
    print(ds)

    model = clustering.LinkageTree(dtw.distance_matrix, {})
    cluster_idx = model.fit(series)
    print(cluster_idx)

    if directory:
        fn = directory / "bug3.png"
    else:
        file = tempfile.NamedTemporaryFile()
        fn = Path(file.name + "_bug3.png")

    model.plot(fn, show_ts_label=True)
github wannesm / dtaidistance / tests / test_clustering.py View on Github external
def test_controlchart():
    import matplotlib.pyplot as plt
    from matplotlib.colors import ListedColormap
    series = np.zeros((600, 60))
    rsrc_fn = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'rsrc', 'synthetic_control.data')
    with open(rsrc_fn, 'r') as ifile:
        for idx, line in enumerate(ifile.readlines()):
            series[idx, :] = line.split()
    s = []
    for idx in range(0, 600, 20):
        s.append(series[idx, :])

    model = clustering.LinkageTree(dtw.distance_matrix_fast, {'parallel': True})
    cluster_idx = model.fit(s)

    if directory:
        hierarchy_fn = os.path.join(directory, "hierarchy.png")
    else:
        file = tempfile.NamedTemporaryFile()
        hierarchy_fn = file.name + "_hierarchy.png"
    fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(10, 10))
    show_ts_label = lambda idx: "ts-" + str(idx)
    # show_ts_label = list(range(len(s)))

    def curcmap(idx):
        if idx % 2 == 0:
            return 'r'
        return 'g'
github wannesm / dtaidistance / tests / test_benchmark.py View on Github external
def d():
        c = clustering.Hierarchical(dtw.distance_matrix_fast, {})
        return c.fit(s)
github wannesm / dtaidistance / tests / test_bugsvis.py View on Github external
def test_bug1():
    series = np.array([
        [0., 0, 1, 2, 1, 0, 1, 0, 0],
        [0., 1, 2, 0, 0, 0, 0, 0, 0],
        [1., 2, 0, 0, 0, 0, 0, 1, 1],
        [0., 0, 1, 2, 1, 0, 1, 0, 0],
        [0., 1, 2, 0, 0, 0, 0, 0, 0],
        [1., 2, 0, 0, 0, 0, 0, 1, 1]])
    model = clustering.LinkageTree(dtw.distance_matrix_fast, {})
    cluster_idx = model.fit(series)

    if directory:
        hierarchy_fn = directory / "hierarchy.png"
    else:
        file = tempfile.NamedTemporaryFile()
        hierarchy_fn = Path(file.name + "_hierarchy.png")
    model.plot(hierarchy_fn)
    print("Figure saved to", hierarchy_fn)
github wannesm / dtaidistance / tests / test_bugsvis.py View on Github external
def test_bug3():
    series = np.array([
        np.array([1, 2, 1]),
        np.array([0., 1, 2, 0, 0, 0, 0, 0, 0]),
        np.array([1., 2, 0, 0, 0, 0, 0, 1, 1, 3, 4, 5]),
        np.array([0., 0, 1, 2, 1, 0, 1]),
        np.array([0., 1, 2, 0, 0, 0, 0, 0]),
        np.array([1., 2, 0, 0, 0, 0, 0, 1, 1])])
    ds = dtw.distance_matrix(series)
    print(ds)

    model = clustering.LinkageTree(dtw.distance_matrix, {})
    cluster_idx = model.fit(series)
    print(cluster_idx)

    if directory:
        fn = directory / "bug3.png"
    else:
        file = tempfile.NamedTemporaryFile()
        fn = Path(file.name + "_bug3.png")

    model.plot(fn, show_ts_label=True)