def test_bug2():
    with util_numpy.test_uses_numpy() as np:
        s1 = np.array([0, 0, 1, 2, 1, 0, 1, 0, 0], dtype=np.double)
        s2 = np.array([0.0, 1, 2, 0, 0, 0, 0, 0, 0])
        d1a = dtw.distance_fast(s1, s2, window=2)
        d1b = dtw.distance(s1, s2, window=2)

        if directory:
            fn = directory / "warpingpaths.png"
        else:
            file = tempfile.NamedTemporaryFile()
            fn = Path(file.name + "_warpingpaths.png")
        d2, paths = dtw.warping_paths(s1, s2, window=2)
        best_path = dtw.best_path(paths)
        if not dtwvis.test_without_visualization():
            dtwvis.plot_warpingpaths(s1,
                                     s2,
                                     paths,
                                     best_path,
                                     filename=fn,
                                     shownumbers=False)
            print("Figure saved to", fn)

        assert d1a == pytest.approx(d2)
        assert d1b == pytest.approx(d2)
Exemplo n.º 2
0
    def quality_vs_trainsize(X_train, X_test, method, dist='dtw'):
        method = clone(method)

        def fit():
            method.estimator.set_params(**{
                'verbose': 0,
            })
            method.fit(X_train)
            X_pred_codes = method.transform_codes(X_test)
            X_pred = method.codes_to_signal(X_pred_codes)
            rate = BaseBenchmark.compression_rate(X_test, X_pred_codes)
            inv_rate = 1 / rate

            return X_pred, rate, inv_rate

        X_pred, rate, inv_rate = fit()

        # Must truncate test timeseries to prediction timeseries
        a1 = np.array(X_test)
        a2 = np.array(X_pred)
        assert a1.shape[0] >= a2.shape[0]
        a1.resize(a2.shape)

        # Compute distance
        if dist == 'dtw':
            d = dtw.distance_fast(a1, a2)
        elif dist == 'rmsre':
            d = np.sqrt(np.mean(np.square(np.divide(a1 - a2, a1))))
        else:
            raise ValueError(f'Unknown distance {dist}')

        return X_train.shape[0], d, rate, inv_rate
Exemplo n.º 3
0
def test_distance1_numpy():
    with util_numpy.test_uses_numpy() as np:
        s1 = np.array([0., 0, 1, 2, 1, 0, 1, 0, 0])
        s2 = np.array([0., 1, 2, 0, 0, 0, 0, 0, 0])
        d = dtw.distance_fast(s1, s2)
        # print(f'd = {d}')
        assert d == pytest.approx(math.sqrt(2))
Exemplo n.º 4
0
def average_distance_to_templates(sequence, templates, warping_window):
    len_seq = sequence.shape[0]
    val_min = np.inf
    label = 1
    for i, t in enumerate(templates, start=1):
        val = 0.0
        for temp in t:
            len_temp = temp.shape[0]
            if warping_window is not None:
                warping_window = int(
                    np.ceil(warping_window * max(len_seq, len_temp)))
                # If the sequences have different lengths, the warping window cannot be smaller than the difference
                # between the length of the sequences
                warping_window = max(warping_window,
                                     abs(len_seq - len_temp + 1))

            d = (dtw.distance_fast(
                sequence[:, 0], temp[:, 0],
                window=warping_window)) / float(len_seq + len_temp)
            val += d

        val /= len(t)
        if val < val_min:
            val_min = val
            label = i

    return val_min, label
Exemplo n.º 5
0
def test_psi_dtw_1d():
    with util_numpy.test_uses_numpy() as np:
        x = np.arange(0, 20, .5)
        s1 = np.sin(x)
        s2 = np.sin(x - 1)

        random.seed(1)
        for idx in range(len(s2)):
            if random.random() < 0.05:
                s2[idx] += (random.random() - 0.5) / 2

        # print(f's1 = [' + ','.join(f'{vv:.2f}' for vv in s1) + ']')
        # print(f's2 = [' + ','.join(f'{vv:.2f}' for vv in s2) + ']')

        # print('distance_fast')
        d1 = dtw.distance_fast(s1, s2, psi=2)
        # print(f'{d1=}')
        # print('warping_paths')
        d2, paths = dtw.warping_paths(s1, s2, window=25, psi=2)
        # print(f'{d2=}')
        with np.printoptions(threshold=np.inf, linewidth=np.inf):
            print(paths)
        # print('warping_paths fast')
        d3, paths = dtw.warping_paths_fast(s1, s2, window=25, psi=2)
        # print(f'{d3=}')
        # print(paths)
        # print('best_path')
        best_path = dtw.best_path(paths)

        if not dtwvis.test_without_visualization():
            if directory:
                dtwvis.plot_warpingpaths(s1, s2, paths, best_path, filename=directory / "test_psi_dtw_1d.png")

        np.testing.assert_almost_equal(d1, d2)
        np.testing.assert_almost_equal(d1, d3)
Exemplo n.º 6
0
def test_psi_dtw_1c():
    with util_numpy.test_uses_numpy() as np:
        x = np.arange(0, 20, .5)
        s1 = np.sin(x)
        s2 = np.sin(x - 1)
        d = dtw.distance_fast(s1, s2, psi=2)
        np.testing.assert_equal(d, 0.0)
 def _reconizer_run(self, x, y):
     """
     Roda o algotimo com a função de distância escolhida.
     
     Esse método pode ser sobrescrito para utilizar implementações diferentes do DTW.
     Como o AcceleratedDTW.
     """
     return dtw.distance_fast(x, y, use_pruning=True)
Exemplo n.º 8
0
def test_distance2_c():
    with util_numpy.test_uses_numpy() as np:
        dist_opts = {}
        s1 = np.array([0.0, 0.0, 2.0, 1.0, 1.0, 0.0, 0.0])
        s2 = np.array([0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0])
        d1 = dtw.distance(s1, s2, **dist_opts)
        d2 = dtw.distance_fast(s1, s2, **dist_opts)
        assert d1 == d2
        assert d1 == pytest.approx(1.0)
Exemplo n.º 9
0
    def dist_matrix_py_fast(self,
                            x,
                            y,
                            rss=lambda x, y, z: np.sqrt(x**2 + y**2 + z**2)):
        # Compute the distance matrix
        # dm_count = 0
        x_s = np.shape(x)
        y_s = np.shape(y)
        dm = np.zeros((x_s[0], y_s[0]))
        # dm_size = x_s[0] * y_s[0]

        # p = ProgressBar(dm_size)

        for i in range(0, x_s[0]):
            for j in range(0, y_s[0]):
                # x_accelX = array.array('d', [round(n, 2) for n in x[i]["accelX"]])
                # x_accelY = array.array('d', [round(n, 2) for n in x[i]["accelY"]])
                # x_accelZ = array.array('d', [round(n, 2) for n in x[i]["accelZ"]])
                # y_accelX = array.array('d', [round(n, 2) for n in y[j]["accelX"]])
                # y_accelY = array.array('d', [round(n, 2) for n in y[j]["accelY"]])
                # y_accelZ = array.array('d', [round(n, 2) for n in y[j]["accelZ"]])
                x_accelX = array.array('d', x[i]["accelX"])
                x_accelY = array.array('d', x[i]["accelY"])
                x_accelZ = array.array('d', x[i]["accelZ"])
                y_accelX = array.array('d', y[j]["accelX"])
                y_accelY = array.array('d', y[j]["accelY"])
                y_accelZ = array.array('d', y[j]["accelZ"])
                # 3D modification

                # dm[i, j] = rss(fastdtw(x_accelX, y_accelX, dist=euclidean),
                #                fastdtw(x_accelY, y_accelY, dist=euclidean),
                #                fastdtw(x_accelZ, y_accelZ, dist=euclidean))
                dm[i, j] = rss(
                    dtw.distance_fast(x_accelX, y_accelX, use_pruning=True),
                    dtw.distance_fast(x_accelY, y_accelY, use_pruning=True),
                    dtw.distance_fast(x_accelZ, y_accelZ, use_pruning=True))
                # dm[i, j] = self._dtw_distance(x[i, ::self.subsample_step],
                #                               y[j, ::self.subsample_step])
                # Update progress bar
                # dm_count += 1
                # p.animate(dm_count)

        return dm
Exemplo n.º 10
0
def test_distance2_bb():
    with util_numpy.test_uses_numpy() as np:
        dist_opts = {'max_step': 0.1}
        s1 = np.array([0.0, 0.0, 2.0, 1.0, 1.0, 0.0, 0.0])
        s2 = np.array([0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0])
        d1 = dtw.distance(s1, s2, **dist_opts)
        d2 = dtw.distance_fast(s1, s2, **dist_opts)
        print(d1, d2)
        assert d1 == d2
        assert d1 == pytest.approx(np.inf)
Exemplo n.º 11
0
def dta_dtw(signalA, signalB, **dtw_kwargs):
    '''
    The function bundles the path and distance of the dtaidistance package.
    This is the underlying process to be applied in the HDTW process.
     
    :param signalA: The first signal to apply DTW on
    :param signalB: The second signal to apply DTW on
    :param **dtw_kwargs: any key-word arguments to be propogated to the functions.
    '''
    return dtw.distance_fast(signalA, signalB,**dtw_kwargs), \
           dtw.warping_path(signalA, signalB, **dtw_kwargs)
Exemplo n.º 12
0
def test_distance1_a():
    with util_numpy.test_uses_numpy() as np:
        # dist_opts = {'max_dist': 0.201, 'max_step': 0.011, 'max_length_diff': 8, 'window': 3}
        dist_opts = {'window': 3}
        s1 = np.array(
            [0., 0.01, 0., 0.01, 0., 0., 0., 0.01, 0.01, 0.02, 0., 0.])
        s2 = np.array([0., 0.02, 0.02, 0., 0., 0.01, 0.01, 0., 0., 0., 0.])
        d1 = dtw.distance(s1, s2, **dist_opts)
        d2 = dtw.distance_fast(s1, s2, **dist_opts)
        print("X")
        assert d1 == d2
        assert d1 == pytest.approx(0.02)
Exemplo n.º 13
0
def test_distance1_b():
    with util_numpy.test_uses_numpy() as np:
        dist_opts = {}
        s1 = np.array(
            [0., 0.01, 0., 0.01, 0., 0., 0., 0.01, 0.01, 0.02, 0., 0.])
        s2 = np.array([0., 0.02, 0.02, 0., 0., 0.01, 0.01, 0., 0., 0., 0.])
        d1 = dtw.distance(s1, s2, **dist_opts)
        d2 = dtw.distance_fast(s1, s2, **dist_opts)
        d3, wps = dtw.warping_paths(s1, s2, **dist_opts)
        print(np.power(wps, 2))
        assert d1 == d2
        assert d1 == d3
        assert d1 == pytest.approx(0.02)
Exemplo n.º 14
0
    def quality_vs_width(X_train,
                         X_test,
                         method,
                         widths,
                         stride,
                         n_atoms,
                         dist='dtw'):
        method = clone(method)

        def fit(width):
            method.set_params(**{
                'width': width,
                'stride': stride,
            })
            method.estimator.set_params(**{
                'n_components': n_atoms,
                'verbose': 0,
            })
            method.fit(X_train)
            X_pred_codes = method.transform_codes(X_test)
            X_pred = method.codes_to_signal(X_pred_codes)
            rate = BaseBenchmark.compression_rate(X_test, X_pred_codes)
            inv_rate = 1 / rate

            return X_pred, rate, inv_rate

        dists = []
        rates = []
        inv_rates = []
        for width in tqdm(widths, leave=False):
            X_pred, rate, inv_rate = fit(width)

            # Must truncate test timeseries to prediction timeseries
            a1 = np.array(X_test)
            a2 = np.array(X_pred)
            assert a1.shape[0] >= a2.shape[0]
            a1.resize(a2.shape)

            # Compute distance
            if dist == 'dtw':
                d = dtw.distance_fast(a1, a2)
            elif dist == 'rmsre':
                d = np.sqrt(np.mean(np.square(np.divide(a1 - a2, a1))))
            else:
                raise ValueError(f'Unknown distance {dist}')

            dists.append(d)
            rates.append(rate)
            inv_rates.append(inv_rate)

        return dists, rates, inv_rates
Exemplo n.º 15
0
def helper_dtw_distance(sequence, templates, warping_window, index_tuple):
    t = templates[index_tuple[0]][index_tuple[1]]
    len_seq = sequence.shape[0]
    len_temp = t.shape[0]
    if warping_window is not None:
        warping_window = int(np.ceil(warping_window * max(len_seq, len_temp)))
        # If the sequences have different lengths, the warping window cannot be smaller than the difference
        # between the length of the sequences
        warping_window = max(warping_window, abs(len_seq - len_temp + 1))

    d = (dtw.distance_fast(sequence[:, 0], t[:, 0],
                           window=warping_window)) / float(len_seq + len_temp)

    return index_tuple[0], index_tuple[1], d
Exemplo n.º 16
0
def distance(C, subim, S, m, rmin, cmin, factor):
    from dtaidistance import dtw
    """
    
    This function computes the spatial-temporal distance between
    two pixels using the DTW distance.

    Keyword arguments:
        C : numpy.ndarray
            ND-array containing cluster centres information
        subim : numpy.ndarray  
            Cluster under analisis
        S : float  
            Spacing
        m : float 
            Compactness
        rmin : float
            Minimum row
        cmin : float
            Minimum column
        factor : float
            Corrective factor

    Returns
    -------
    D: numpy.ndarray
        ND-Array distance
    
    """
    
    #Initialize submatrix
    dc = numpy.zeros([subim.shape[1],subim.shape[2]])
    ds = numpy.zeros([subim.shape[1],subim.shape[2]])
            
    #get cluster centres
    a2 = C[:subim.shape[0]]                                #Average time series
    ic = (int(numpy.floor(C[subim.shape[0]])) - rmin)         #X-coordinate
    jc = (int(numpy.floor(C[subim.shape[0]+1])) - cmin)       #Y-coordinate
    
    # Critical Loop - need parallel implementation
    for u in range(subim.shape[1]):
        for v in range(subim.shape[2]):
            a1 = subim[:,u,v]                                              # Get pixel time series 
            dc[u,v] = dtw.distance_fast(a1.astype(float),a2.astype(float)) #Compute DTW distance
            ds[u,v] = (((u-ic)**2 + (v-jc)**2)**0.5)                       #Calculate Spatial Distance
    
    
    D =  ( (dc)/m + (ds/S) )**0.5   #Calculate SPatial-temporal distance
          
    return D
 def find_closes_branch_ar(array_query, temp_exemplars):
     closest_nodes = list()
     bsf = 100000
     for i in range(0, len(temp_exemplars)):
         exemplars = np.asarray(temp_exemplars.__getitem__(i))
         if DistanceMeasure.are_equal(exemplars, array_query):
             return i
         dist = dtw.distance_fast(array_query, exemplars)
         if dist < bsf:
             bsf = dist
             closest_nodes.clear()
             closest_nodes.append(i)
         elif bsf == dist:
             closest_nodes.append(i)
     r = random.randint(0, len(closest_nodes) - 1)
     return closest_nodes[r]
Exemplo n.º 18
0
def test_distance4():
    with util_numpy.test_uses_numpy(strict=False) as np:
        try:
            import pandas as pd
        except ImportError:
            # If no pandas, ignore test (not a required dependency)
            return
        s = [[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
             [0.005, 0.01, 0.015, 0.02, 0.01, 0., 0., 0., 0., 0., 0., 0., 0.],
             [0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]]
        p = np.array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
        df = pd.DataFrame(data=s)
        s = df.values
        for i in range(s.shape[0]):
            ss = s[i]  # ss will not be C contiguous memory layout
            d = dtw.distance_fast(ss, p)
Exemplo n.º 19
0
def test_distance3_a():
    with util_numpy.test_uses_numpy() as np:
        dist_opts = {"penalty": 0.005, "max_step": 0.011, "window": 3}
        s = np.array([
            0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.005, 0.01,
            0.015, 0.02, 0.01, 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
            0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
            0.
        ])
        p = np.array([
            0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.005, 0.01, 0.015,
            0.02, 0.01, 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
            0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.
        ])
        d1 = dtw.distance(s, p, **dist_opts)
        d2 = dtw.distance_fast(s, p, **dist_opts)
        assert d1 == pytest.approx(d2)
    def find_closest_nodes(query, temp_exemplars: list):
        array_query = np.asarray(query)
        closest_nodes = list()
        bsf = np.inf
        if len(temp_exemplars) == 0:
            return -1

        for i in range(0, len(temp_exemplars)):
            exemplars = np.asarray(temp_exemplars.__getitem__(i))
            """
            if AppContext.AppContext.elastic_distance == "dtw":
               # dist = dtw.distance_fast(array_query, exemplars, window=AppContext.AppContext.window_length,
               #                          max_dist=bsf)
            elif AppContext.AppContext.elastic_distance == "lcss":
                esilon = LCSS.get_random_epsilon()
                dist = LCSS.distance(array_query, exemplars, window_size=-np.inf, epsilon=esilon)
            elif AppContext.AppContext.elastic_distance == "twe":
                dist = TWE.distance(array_query, exemplars, TWE.get_random_nu(), TWE.get_random_lambda())
            else:
            """
            try:
                dist = dtw.distance_fast(array_query, exemplars, window=2)
            except RecursionError:
                dist = DistanceMeasure._dtw_distance(array_query,
                                                     exemplars,
                                                     d=lambda x, y: abs(x - y))

            if len(closest_nodes) == 0:
                bsf = dist
                closest_nodes.append(i)
            else:
                if dist < bsf:
                    bsf = dist
                    closest_nodes.clear()
                    closest_nodes.append(i)
                elif bsf == dist:
                    closest_nodes.append(i)

        if len(closest_nodes) == 0:
            print("There are no closest Nodes")
            r = 0
        elif len(closest_nodes) == 1:
            return closest_nodes[0]
        else:
            r = random.randint(0, len(closest_nodes) - 1)
        return closest_nodes[r]
Exemplo n.º 21
0
    def bmu(self, x, windowSize, candidateWeights):
        ## function to find winning node
        #: input observatiopn

        # format input for use in this function --- dtw distance
        # x = np.reshape(x[0], (1, 1, len(x[0])))

        ####################################
        # calculate distances (in Euclidean and DTW it is the minimum). Iterate over all nodes to find distance
        #x = np.reshape(x,(1,len(x[0])))
        min_distance = float('inf')
        dtw_Cals = 0
        for i in candidateWeights:

            #get candidate weight
            #this needs to be a deep copy for dtw distance not to throw an error.
            weights = copy.deepcopy(self.weights_Kohonen[i])
            xCopy = copy.deepcopy(x)
            #get dtw distance
            distance = dtw.distance_fast(xCopy,
                                         weights,
                                         window=windowSize + 1,
                                         use_pruning=True)

            dtw_Cals += 1
            #update min distance if new distance is lower
            if distance < min_distance:
                bmuIndex = i
                min_distance = distance
            #update min distance if new distance is equal to min distance
            elif distance == min_distance and np.random.uniform(low=0,
                                                                high=1) < 0.3:
                bmuIndex = i
                min_distance = distance

        return [bmuIndex, dtw_Cals]
Exemplo n.º 22
0
    def createUmatrix(self, windowSize):

        normalizedWeights = self.weights_Kohonen
        self.neighborhoodSize = 1
        self.Umatrix = np.zeros((self.hiddenSize[0], self.hiddenSize[1]))
        # Perform 2D convolution with input data and kernel  to get sum of neighboring nodes
        #for i in range(0,self.hiddenSize[0]):
        #for j in range(0,self.hiddenSize[1]):
        for idx in range(0, len(self.weights_Kohonen)):
            nodeWeights = normalizedWeights[idx]

            [i, j] = np.unravel_index(idx, self.hiddenSize)
            #find all the neighbors for node at i,j
            neighbors2d = self.find_neighbor_indices(i, j)
            #remove self
            neighbors2d.remove((i, j))
            #get weights for node at i,j

            neighbors = []
            for neighbor in neighbors2d:
                neighbors.append(
                    np.ravel_multi_index(neighbor, self.hiddenSize))
            #temp = []
            for neighbor in neighbors:

                #for dtw
                neighborWeights = normalizedWeights[neighbor]
                distance = dtw.distance_fast(neighborWeights,
                                             nodeWeights,
                                             window=windowSize + 1,
                                             use_pruning=True)
                #distance2 = np.sqrt(sum((nodeWeights.flatten()-neighborWeights.flatten())**2))
                #temp.append(distance)
                self.Umatrix[i, j] += distance
            #self.Umatrix[i, j] = min(temp)
        return self.Umatrix
Exemplo n.º 23
0
def distance_slic(C, subim, S, m, rmin, cmin):

    #Initialize submatrix
    dc = np.zeros([subim.shape[1], subim.shape[2]])
    ds = np.zeros([subim.shape[1], subim.shape[2]])

    #get cluster centres
    a2 = C[:subim.shape[0]]  #Average time series
    ic = (int(np.floor(C[subim.shape[0]])) - rmin)  #X-coordinate
    jc = (int(np.floor(C[subim.shape[0] + 1])) - cmin)  #Y-coordinate

    # Critical Loop - need parallel implementation
    for u in range(subim.shape[1]):
        for v in range(subim.shape[2]):
            a1 = subim[:, u, v]  # Get pixel time series
            dc[u,
               v] = dtw.distance_fast(a1.astype(float),
                                      a2.astype(float))  #Compute DTW distance
            ds[u, v] = (
                ((u - ic)**2 + (v - jc)**2)**0.5)  #Calculate Spatial Distance
    D = (dc**2 + (m**2) *
         (ds / S)**2)**0.5  #Calculate SPatial-temporal distance

    return D
Exemplo n.º 24
0
 def d():
     return dtw.distance_fast(s1, s2)
Exemplo n.º 25
0
def test_distance6():
    s1 = np.array([0, 0, 1, 2, 1, 0, 1, 0, 0], dtype=np.double)
    s2 = np.array([0.0, 1, 2, 0, 0, 0, 0, 0, 0])
    d = dtw.distance_fast(s1, s2, window=2)
    print(d)
Exemplo n.º 26
0
 def d():
     return dtw.distance_fast(s1, s2, use_pruning=True)
Exemplo n.º 27
0
def test(amplitude, center, width, noise, target_norm,len_a,window,global_max, ii):
    
    
    source_no_noise = amplitude*norm.pdf(range(0,400),center,width)
    source_no_noise = np.random.normal(source_no_noise, scale=0)
    
    source_max = max(abs(source_no_noise))
    
    source_norm = amplitude*norm.pdf(range(0,400),center,width)
    source_norm = np.random.normal(source_norm, scale=noise)
    
    source_global_norm = source_norm/global_max
    
    total_intensity = (abs(source_norm)).sum()
    
    t,s = target_norm,source_norm
    
    '''
    Choose the normalisation method by commenting/uncommenting below
    '''
    
    target_norm,source_norm = target_norm/max(abs(target_norm)),source_norm/source_max
    
    '''
    target_norm = preprocessing.normalize([target_norm])
    target_norm = target_norm.reshape((400,))
    
    source_norm = preprocessing.normalize([source_norm])
    source_norm = source_norm.reshape((400,))
    '''
    
    
    '''
    '''
    
    
    
    target,source = deriv(target_norm),deriv(source_global_norm)
    
    target.insert(0,0)
    target.insert(len(target),0)
    source.insert(0,0)
    source.insert(len(source),0)
    
    target,source = np.asarray(target),np.asarray(source)
    
    D = dtw.distance_fast(target,source,window,psi=0)
    
    
    
    
    target_deriv,source_deriv = deriv(target_norm),deriv(source_norm)
    
    target_deriv.insert(0,0)
    target_deriv.insert(len(target_deriv),0)
    source_deriv.insert(0,0)
    source_deriv.insert(len(source_deriv),0)
    
    target_deriv,source_deriv = np.asarray(target_deriv),np.asarray(source_deriv)
    
    d, paths = dtw.warping_paths_fast(target_deriv, source_deriv, window, psi=0)
    best_path = dtw.best_path(paths)
    paths = np.array(best_path)
    
    
    '''
    
    euclidean = pow((target_norm[paths[:,0]] - source_norm[paths[:,1]]),2)
    euclidean = np.sqrt(euclidean.sum())
    
    '''
    
    euclidean = abs(t[paths[:,0]] - s[paths[:,1]])
    euclidean = euclidean.sum()
    
    
    plt.figure(3)
    plt.scatter(ii,euclidean)
    plt.title('warping cost')
    
    
    euclidean = np.exp(-euclidean/(total_intensity/D))
    init_euclidean = abs(paths[:,0]-paths[:,1])
    amplitude,center,width,noise = str(amplitude),str(center),str(width),str(noise)
    
    
    plt.figure(1)
    plt.plot(range(0,400),source_norm)
    
    
    plt.figure(2)
    plt.plot(range(0,400),source_deriv)
    plt.plot(range(0,400),target_deriv,'k-')
    
    
    plt.figure(4)
    plt.scatter(ii,total_intensity)
    plt.title('total intensity')
    
    
    plt.figure(5)
    plt.scatter(ii,D)
    plt.title('D')
    
    
    return euclidean, init_euclidean, target_norm
Exemplo n.º 28
0
 def dtw_distance(x, y):
     return dtw.distance_fast(x, y, use_pruning=True)
        img_name = "opencv_frame_{}.png".format(img_counter)
        print("Please wait")
        
        res = cv2.resize(gray, (100,100),interpolation = cv2.INTER_AREA)
        hand = segment(res)

            # check whether hand region is segmented
        if hand is not None:
            thresholded = hand
            thresholded = cv2.erode(thresholded, None, iterations=2)
            thresholded = cv2.dilate(thresholded, None, iterations=2)
            cv2.imshow("Thesholded", thresholded)
            dtw_feat = feat(thresholded)
            final = []
            for xtrain in x_train:
                distance = dtw.distance_fast(dtw_feat,xtrain)
                final.append(distance)
            mini = final.index(min(final))
            pred = y_train[mini]
            print("Predicted:"+pred)
        
            cv2.putText(clone,pred, (200,100), cv2.FONT_HERSHEY_SIMPLEX, 2, 127,3)
            
            cv2.imwrite(img_name, clone)
            print("{} written!".format(img_name))
        img_counter += 1
    
    
        
    cv2.imshow("Video Feed", clone)
    
Exemplo n.º 30
0
def test_distance6():
    with util_numpy.test_uses_numpy() as np:
        s1 = np.array([0, 0, 1, 2, 1, 0, 1, 0, 0], dtype=np.double)
        s2 = np.array([0.0, 1, 2, 0, 0, 0, 0, 0, 0])
        d = dtw.distance_fast(s1, s2, window=2)