コード例 #1
0
def diff_sequences(seq_a, seq_b):
    distance, _ = fastdtw.dtw(seq_a, seq_b, dist=my_dist)

    _gauss = gaussian(distance, 0., 100.)
    # print("distance", distance, "\tsimilarity", _gauss)

    return _gauss  # np_clip(_gauss, 0.0001, 1.)
コード例 #2
0
def dtw_segmentize(x_axis, y_axis, z_axis, threshold, window_size):
    total_acc = np.array(x_axis) + np.array(y_axis) + np.array(z_axis)

    segmented_x_axis = []
    segmented_y_axis = []
    segmented_z_axis = []

    prev = total_acc[0:window_size]
    start = 0
    end = window_size

    for i in range(window_size, len(total_acc) - window_size, window_size):
        curr = total_acc[i:i + window_size]
        dist, mappings = dtw(prev, curr)

        if dist / len(mappings) >= threshold:
            segmented_x_axis.append(x_axis[start:end])
            segmented_y_axis.append(y_axis[start:end])
            segmented_z_axis.append(z_axis[start:end])

            start = i
            end = i + window_size
        else:
            end += window_size

        prev = curr

    segmented_x_axis.append(x_axis[start:end])
    segmented_y_axis.append(y_axis[start:end])
    segmented_z_axis.append(z_axis[start:end])

    return segmented_x_axis, segmented_y_axis, segmented_z_axis
コード例 #3
0
ファイル: Metrics.py プロジェクト: cc2020/socialsim
def dtw(ground_truth, simulation):
    """
    Dynamic Time Warping implemenation
    """

    dist = fdtw.dtw(ground_truth, simulation, dist=euclidean)[0]
    return dist
コード例 #4
0
ファイル: MPDist.py プロジェクト: maxinehehe/MPDist
 def distDTW(self, Q, T, L):
     distVec = []
     lenQ = len(Q)
     lenT = len(T)
     for ti in range(0, lenT - lenQ + 1, 165):
         res = fastdtw.dtw(Q, T[ti:ti + L], euclidean)[0]
         print(" idx : ", ti, " curr dist: ", res)
         distVec.append(res)
     return distVec
コード例 #5
0
ファイル: _distance.py プロジェクト: cccxm/deep-learning
def dtw(v1: np.ndarray, v2: np.ndarray) -> float:
    """
    fast_dtw
    :return: 距离
    """
    import fastdtw
    from scipy.spatial.distance import euclidean
    # noinspection PyTypeChecker,PyUnresolvedReferences
    return fastdtw.dtw(v1, v2, dist=euclidean)[0]
コード例 #6
0
 def dtw(self, a: np.ndarray, b: np.ndarray) -> float:
     """
     计算两个向量的dtw距离
     :param a:
     :param b:
     :return:
     """
     distance, _ = fastdtw.dtw(a, b, dist=self._dist)
     return distance
コード例 #7
0
def sign_comparison(_sign_1, _sign_2, _method='dtw_normalized'):
    """
    Compares signs by DTW normalized by path length and trajectory count.
    :param _sign_1: trajectory frames1 X markers
    :param _sign_2: trajectory frames2 X markers
    :param _method: only dtw_normalized available.
    :return: distance
    """
    _normalized = None
    if _method == 'dtw_normalized':
        _distance, _path = fastdtw.dtw(_sign_1, _sign_2)
        _normalized = (_distance / len(_path)) / np.size(_sign_1, 1)
    return _normalized
コード例 #8
0
ファイル: imbalances_learn.py プロジェクト: lanhpham/paytv
 def run(self):
     result = np.empty(shape = [len(self.my_range), len(self.data_Churn)])
     row = 0
     for i in self.my_range:
         x =  self.data_Act.ix[i,:]
         x = np.array(x).reshape(1,-1)
         for j in range(len(self.data_Churn)):
             y =  self.data_Churn.ix[j,:]
             y = np.array(y).reshape(1,-1)
             distance = dtw(x, y,dist= cosine)[0]
             result[row,j] = distance
         row = row + 1
     score = pd.DataFrame(data = result)
     score.to_csv(out_path +str(self.index).zfill(3) + "thread.txt", header = None, index = False)
コード例 #9
0
ファイル: Metrics.py プロジェクト: shj1987/socialsim
def dtw(ground_truth, simulation):
    """
    Dynamic Time Warping implemenation
    """

    try:
        ground_truth = ground_truth['value'].as_matrix()
        simulation = simulation['value'].as_matrix()
    except:
        ground_truth = np.array(ground_truth)
        simulation = np.array(simulation)

    dist = fdtw.dtw(ground_truth.tolist(), simulation, dist=euclidean)[0]
    return dist
コード例 #10
0
def real_time():
    while True:
        try:
            for raw_file, joined_file in collect_files(REAL_TIME_PATH):
                _, sig = wav.read(joined_file)
                test_feats = extract(sig)
                results = dict()
                for model_name, model_feats in models.items():
                    dist, path = dtw(test_feats, model_feats, euclidean)
                    print(dist)
                os.remove(joined_file)
        except Exception as e:
            # print(e.__str__())
            pass
コード例 #11
0
def match_dtw_sift(keypoints_A,
                   keypoints_B,
                   features_A,
                   features_B,
                   test_ratio=0.75):
    """Given two sets of key points and their descriptors, find the best matching pairs."""
    match_A, match_B = [], []

    cost_matrix, path = dtw(features_A, features_B, dist=euclidean)

    for idA, idB in path:
        match_A.append(keypoints_A[idA])
        match_B.append(keypoints_B[idB])

    return np.int32(match_A), np.int32(match_B)
コード例 #12
0
    def dtw(self, ground_truth, simulation, join='outer', fill_value = 0):
        """
        Dynamic Time Warping implemenation
        """
        df = self.join_dfs(ground_truth,simulation,join=join,fill_value=fill_value)

        try:
            ground_truth = df['value_gt'].values
            simulation = df['value_sim'].values
        except:
            ground_truth = np.array(ground_truth)
            simulation = np.array(simulation)

        if len(simulation) > 0:
            dist = fdtw.dtw(ground_truth.tolist(), simulation, dist=euclidean)[0]
        else:
            dist = None

        return dist
コード例 #13
0
def dtwGenerator(X_train, batch_size, sample_distance):
    sample_size = len(X_train)

    while 1:
        idx = []
        idx.append(random.randint(0, sample_size - 1))

        while 1:
            if len(idx) == batch_size: break
            temp = random.randint(0, sample_size - 1)
            flag = 0
            for j in idx:
                d, _ = dtw(X_train[j], X_train[temp], dist=md4dtw)
                if d < sample_distance:
                    flag = 1
                    break
            if flag == 0:
                idx.append(temp)

        yield idx
コード例 #14
0
def align_dtw(
    reference: Trace, *traces: Trace, radius: int = 1, fast: bool = True
) -> List[Trace]:
    """
    Align `traces` to the `reference` trace.

    Use fastdtw (Dynamic Time Warping) as per:

    Stan Salvador, Philip Chan:
    **FastDTW: Toward Accurate Dynamic Time Warping in Linear Time and Space**

    https://cs.fit.edu/~pkc/papers/tdm04.pdf

    :param reference: Trace to align to.
    :param traces: Traces to align.
    :param radius:
    :param fast:
    :return: List of the aligned traces (with the reference).
    """
    result = [deepcopy(reference)]
    reference_samples = reference.samples
    for trace in traces:
        if fast:
            _, path = fastdtw(reference_samples, trace.samples, radius=radius)
        else:
            _, path = dtw(reference_samples, trace.samples)
        result_samples = np.zeros(
            max((len(trace.samples), len(reference_samples))), dtype=trace.samples.dtype
        )
        pairs = np.array(
            np.array(path, dtype=np.dtype("int,int")),
            dtype=np.dtype([("x", "int"), ("y", "int")]),
        )
        result_samples[pairs["x"]] = trace.samples[pairs["y"]]
        del pairs
        del path
        result.append(trace.with_samples(result_samples))
    return result
コード例 #15
0
ファイル: query_op.py プロジェクト: krbrez/BrainEx-UI
def sim_between_array(a1: np.ndarray, a2: np.ndarray, pnorm: int, use_fast=True):
    """
    calculate the similarity between sequence 1 and sequence 2 using DTW

    :param a1:
    :param a2:
    :param pnorm: the distance type that can be: 0, 1, or 2
    :return float: return the Normalized DTW distance between sequence 1 (seq1) and sequence 2 (seq2)
    """
    # dt_pnorm_dict = {'eu': 0,
    #                    'ma': 1,
    #                    'ch': 2,
    #                    'min': 2}

    dist = fastdtw(a1, a2, dist=pnorm)[0] if use_fast else dtw(a1, a2, dist=pnorm)[0]
    if pnorm == 2:
        return np.sqrt(dist / (len(a1) + len(a2)))
    elif pnorm == 1:
        return dist / (len(a1) + len(a2))
    elif pnorm == math.inf:
        return dist
    else:
        raise Exception('Unsupported dist type in array, this should never happen!')
コード例 #16
0
ファイル: match_plots.py プロジェクト: PineBiotechOrg/trkir
def mine_dtw(series_l, series_r):

    print(len(series_l), len(series_r))
    first = series_l[:]
    second = series_r[:]
    # second = ((np.array(series_r[:len(series_l)]) - np.min(series_r[:len(series_l)])) / (
    #             np.max(series_r[:len(series_l)]) - np.min(series_r[:len(series_l)]))) * (
    #                      np.max(series_l) - np.min(series_l)) + np.min(series_l)

    dist, path = dtw(first, second, dist=euclidean)

    res = [0 for i in range(len(series_l))]
    for i in range(len(path)):
        if res[path[i][1]] == 0:
            res[path[i][1]] = (path[i][0], path[i][1])

    new_plt = np.array([first[val[0]] for val in res])
   #  new_plt = ((np.array([first[val[0]] for val in res]) - np.min(series_l)) / (
   #               np.max(series_l) - np.min(series_l))) * (
   #                         np.max(series_r[:len(series_l)]) - np.min(
   #                     series_r[:len(series_l)])) + np.min(series_r[:len(series_l)])

    return new_plt, path, dist
コード例 #17
0
def sequence_distance(seq1, seq2, ignored=None,
                      element_dist=elements_distance_binary):
    """ affinity between two layouts

    :param [] seq1: sequence layout
    :param [] seq2: sequence layout
    :param [] ignored: list of elements to be ignored
    :param element_dist: callable function to measure distance
    :return float:

    >>> seq1 = ['a', 'b', 'a', 'c']
    >>> sequence_distance(seq1, seq1)
    0.0
    >>> seq2 = ['a', 'a', 'b', 'a', 'c']
    >>> sequence_distance(seq1, seq2)
    0.0
    >>> sequence_distance(seq1, seq2[:len(seq1)])
    0.25
    >>> l3 = ['c', None, 'd']
    >>> sequence_distance(seq1, l3)
    0.5
    """
    ignored = norm_ignore_elems(ignored)
    # remove None which crash uniques
    seqs = [b for b in seq1 + seq2 if b is not None]
    blocks = [b for b in np.unique(seqs) if b not in ignored]
    # discrete the lists
    seq1_d = [blocks.index(b) if b not in ignored else -1 for b in seq1]
    seq2_d = [blocks.index(b) if b not in ignored else -1 for b in seq2]

    # TODO: add also reverse time dynamic t -> (t-1)
    # compute the special distance measure
    _wrap_dist = partial(element_dist, ignored=-1)
    dist, match = fastdtw.dtw(seq1_d, seq2_d, _wrap_dist)
    dist = dist / float(max(len(seq1), len(seq2)))

    return dist
コード例 #18
0
def soft2hard_attention(attention):
    '''
    Convert soft attention to hard attention using DTW
    '''
    from fastdtw import dtw
    _, path = dtw(list(range(attention.shape[0])),
                  list(range(attention.shape[1])),
                  dist=lambda x, y: -attention[int(x), int(y)])
    hard_attention = np.zeros_like(attention)
    for i, j in path:
        hard_attention[i, j] = 1
    for frame in range(attention.shape[1]):
        inds = np.argwhere(hard_attention[:, frame] == 1)
        if inds.size <= 1:
            continue
        if attention[inds[0], frame] > attention[inds[-1], frame]:
            if frame < attention.shape[1] - 1 and hard_attention[inds[-1],
                                                                 frame +
                                                                 1] == 1:
                hard_attention[inds[-1], frame] = 0
        else:
            if frame > 0 and hard_attention[inds[0], frame - 1] == 1:
                hard_attention[inds[0], frame] = 0
    return hard_attention
コード例 #19
0
def align_dtw_scale(
    reference: Trace, *traces: Trace, radius: int = 1, fast: bool = True
) -> List[Trace]:
    """
    Align :paramref:`~.align_correlation.traces` to the :paramref:`~.align_correlation.reference` trace.

    Use fastdtw (Dynamic Time Warping) with scaling as per:

    Jasper G. J. van Woudenberg, Marc F. Witteman, Bram Bakker:
    **Improving Differential Power Analysis by Elastic Alignment**

    https://pdfs.semanticscholar.org/aceb/7c307098a414d7c384d6189226e4375cf02d.pdf

    :param reference: Trace to align to.
    :param traces: Traces to align.
    :param radius:
    :param fast:
    :return: List of the aligned traces (with the reference).
    """
    result = [deepcopy(reference)]
    reference_samples = reference.samples
    for trace in traces:
        if fast:
            _, path = fastdtw(reference_samples, trace.samples, radius=radius)
        else:
            _, path = dtw(reference_samples, trace.samples)
        result_samples = np.zeros(len(reference_samples), dtype=trace.samples.dtype)
        scale = np.zeros(len(reference_samples), dtype=trace.samples.dtype)
        for x, y in path:
            result_samples[x] += trace.samples[y]
            scale[x] += 1
        result_samples /= scale
        del path
        del scale
        result.append(trace.with_samples(result_samples))
    return result
コード例 #20
0
def get_dtw_dist(pt1, pt2, window):
    dist_obj = fastdtw.dtw(pt1, pt2, window)
    dist = dist_obj[0]
    # print('time to run get_dtw_dist fct was %f', time.time() - start_time)
    return dist
コード例 #21
0
ファイル: functions.py プロジェクト: xiaozhah/tools-function
def Compute_US_Acoustic_Distortion(us_dir,
                                   testwav_ref,
                                   dim,
                                   corpus_dfmcep,
                                   corpus_f0,
                                   ref_file=None,
                                   dtw_mode='fastdtw'):
    if ref_file == None:
        ref_list = map(FileBaseName, sorted(glob(os.path.join(us_dir,
                                                              '*.stt'))))
    else:
        ref_list = np.loadtxt(ref_file, 'str')
    tmpDir = 'tmp_' + uuid.uuid4().hex
    SaveMkdir(tmpDir)
    ref_cep_dir = tmpDir + os.sep + 'speFea_ref'
    ref_old_f0_dir = tmpDir + os.sep + 'old_f0_ref'
    ref_f0_dir = tmpDir + os.sep + 'f0_ref'

    align_ref_cep_dir = tmpDir + os.sep + 'align_speFea_ref'
    align_ref_f0_dir = tmpDir + os.sep + 'align_f0_ref'
    align_candidates_cep_dir = tmpDir + os.sep + 'gen_cep'
    align_candidates_f0_dir = tmpDir + os.sep + 'gen_f0'
    SaveMkdir(ref_cep_dir)
    SaveMkdir(ref_old_f0_dir)
    SaveMkdir(ref_f0_dir)
    SaveMkdir(align_ref_cep_dir)
    SaveMkdir(align_ref_f0_dir)
    SaveMkdir(align_candidates_cep_dir)
    SaveMkdir(align_candidates_f0_dir)

    corpus_files = map(FileBaseName,
                       sorted(glob(os.path.join(corpus_dfmcep, '*.cep'))))
    test_files = map(FileBaseName, sorted(glob(os.path.join(us_dir, '*.stt'))))
    cmds = []
    for name in test_files:
        if name in ref_list:
            # 从testwav_ref分析其声学数据
            wavfile = os.path.join(testwav_ref, name + '.wav')
            old_f0file = os.path.join(ref_old_f0_dir, name + '.f0')
            f0file = os.path.join(ref_f0_dir, name + '.f0')
            cepfile = os.path.join(ref_cep_dir, name + '.cep')
            commandline = "%s -f 16000 -shift 5 -lf0 50 -uf0 600 -src %s %s" % (
                straight, wavfile, old_f0file)
            cmds.append(commandline)
    ParallelRun(cmds, threads=32)

    commandline = "%s -i %s -o %s -w %s -l %s" % (
        F0PostProcess, ref_old_f0_dir, ref_f0_dir, testwav_ref, ref_file)
    os.system(commandline)

    cmds = []
    for name in test_files:
        if name in ref_list:
            wavfile = os.path.join(testwav_ref, name + '.wav')
            f0file = os.path.join(ref_f0_dir, name + '.f0')
            cepfile = os.path.join(ref_cep_dir, name + '.cep')
            commandline = "%s -shift 5 -mcep -pow -order %d -f0file %s -ana %s %s" % (
                straight, dim - 1, f0file, wavfile, cepfile)
            cmds.append(commandline)
    ParallelRun(cmds, threads=32)

    for name in tqdm(test_files):
        if name in ref_list:
            #读取备选单元
            stt_file = os.path.join(us_dir, name + '.stt')
            stt_text = open(stt_file, 'rt').readlines()
            stt_synNo = map(lambda i: int(i.split()[2]), stt_text[1:])
            stt_dur = np.array(map(lambda i: i.split()[3:5], stt_text)[1:],
                               dtype=np.int)

            # 从备选单元得到所挑选单元的声学数据
            cep_data_stt = []
            f0_data_stt = []
            for index, SynNo in enumerate(stt_synNo):
                frames_s_e = stt_dur[index]
                cep_data_stt += ReadFloatRawMat(
                    os.path.join(corpus_dfmcep, corpus_files[SynNo] + '.cep'),
                    dim)[frames_s_e[0]:frames_s_e[1]].tolist()
                f0_stt = open(
                    os.path.join(corpus_f0, corpus_files[SynNo] + '.f0'),
                    'rt').readlines()
                f0_data_stt += map(lambda i: float(i.split()[0]),
                                   f0_stt[1:][frames_s_e[0]:frames_s_e[1]])

            cep_data_stt = np.array(cep_data_stt)
            f0_data_stt = np.array(f0_data_stt)

            f0file = os.path.join(ref_f0_dir, name + '.f0')
            cepfile = os.path.join(ref_cep_dir, name + '.cep')
            cep_data_ref = ReadFloatRawMat(cepfile, dim)
            f0_data_ref = open(f0file, 'rt').readlines()
            f0_data_ref = np.array(
                map(lambda i: float(i.split()[0]), f0_data_ref[1:]))
            min_dim = min(f0_data_ref.shape[0], cep_data_ref.shape[0])
            cep_data_ref = cep_data_ref[:min_dim]
            f0_data_ref = f0_data_ref[:min_dim]

            # 归一化
            cep_data_stt_meanData = np.concatenate(
                (np.mean(cep_data_stt, axis=0).reshape(
                    1, -1), np.std(cep_data_stt, axis=0).reshape(1, -1)),
                axis=0)
            cep_data_stt = Normalization_MeanStd(cep_data_stt, range(dim),
                                                 cep_data_stt_meanData)
            cep_data_ref_meanData = np.concatenate(
                (np.mean(cep_data_ref, axis=0).reshape(
                    1, -1), np.std(cep_data_ref, axis=0).reshape(1, -1)),
                axis=0)
            cep_data_ref = Normalization_MeanStd(cep_data_ref, range(dim),
                                                 cep_data_ref_meanData)

            if dtw_mode == 'fastdtw':
                distance, path = fastdtw(cep_data_stt,
                                         cep_data_ref,
                                         dist=euclidean)
            elif dtw_mode == 'dtw':
                distance, path = dtw(cep_data_stt,
                                     cep_data_ref,
                                     dist=euclidean)
            else:
                print 'dtw_mode must be dtw or fastdtw'
                exit()

            cep_data_stt = cep_data_stt[map(lambda i: i[0], path)]
            f0_data_stt = f0_data_stt[map(lambda i: i[0], path)]
            cep_data_ref = cep_data_ref[map(lambda i: i[1], path)]
            f0_data_ref = f0_data_ref[map(lambda i: i[1], path)]
            #print data_stt.shape #(Q, 14)
            #print data_ref.shape #(Q, 14)
            assert (len(cep_data_stt) == len(cep_data_ref))
            # 反归一化
            cep_data_stt = DeNormalization_MeanStd(cep_data_stt, range(dim),
                                                   cep_data_stt_meanData)
            cep_data_ref = DeNormalization_MeanStd(cep_data_ref, range(dim),
                                                   cep_data_ref_meanData)

            with open(os.path.join(align_candidates_f0_dir, name + '.f0'),
                      'wt') as f:
                for i in range(len(f0_data_stt)):
                    f.write('%f\n' % (f0_data_stt[i]))
            with open(os.path.join(align_ref_f0_dir, name + '.f0'), 'wt') as f:
                for i in range(len(f0_data_ref)):
                    f.write('%f\n' % (f0_data_ref[i]))

            WriteArrayFloat(
                os.path.join(align_candidates_cep_dir, name + '.mcep'),
                cep_data_stt)
            WriteArrayFloat(os.path.join(align_ref_cep_dir, name + '.cep'),
                            cep_data_ref)

    # 参考声学
    reference_cep_list = prepare_file_path_list(ref_list, align_ref_cep_dir,
                                                '.cep')
    reference_f0_list = prepare_file_path_list(ref_list, align_ref_f0_dir,
                                               '.f0')

    # 预测声学
    generation_cep_list = prepare_file_path_list(ref_list,
                                                 align_candidates_cep_dir,
                                                 '.mcep')
    generation_f0_list = prepare_file_path_list(ref_list,
                                                align_candidates_f0_dir, '.f0')

    MCD_value = MCD(reference_cep_list, generation_cep_list, dim)
    f0_rmse, f0_corr, vuv_error = F0_VUV_distortion(reference_f0_list,
                                                    generation_f0_list)
    tqdm.write(us_dir)
    tqdm.write('MCD: %.4f dB; F0:- RMSE: %.4f Hz; CORR: %.4f; VUV: %.4f%%' %
               (MCD_value, f0_rmse, f0_corr, vuv_error * 100.))
    delDir(tmpDir)
コード例 #22
0
 def dtw(self, x, y):
     return fastdtw.dtw(x, y)[0]
コード例 #23
0
ファイル: UnitTests.py プロジェクト: sali45/DTW
def test_dtw_path(s1, s2):
    assert (dtw(s1, s2, None, None)[1] == fastdtw.dtw(s1, s2)[1])
    print "Test path passed"
コード例 #24
0
ファイル: UnitTests.py プロジェクト: sali45/DTW
def test_dtw_distance(s1, s2):
    assert (dtw(s1, s2, None, None)[0] == fastdtw.dtw(s1, s2)[0])
    print "Test distance passed"
コード例 #25
0
ファイル: test.py プロジェクト: SemanticBeeng/fastdtw
 def test_1d_dtw(self):
     self.assertEqual(dtw(self.x_1d, self.y_1d)[0], 2)
コード例 #26
0
def apply_dtw(sen_data0, sen_data1, radius, dist, sensors):
    if sensors == 'all' or sensors == 'nomag':
        if radius < 0:
            dtw, path = fastdtw.dtw(np.transpose(np.squeeze(sen_data0)),
                                    np.transpose(np.squeeze(sen_data1)),
                                    dist=dist)
        else:
            dtw, path = fastdtw.fastdtw(np.transpose(np.squeeze(sen_data0)),
                                        np.transpose(np.squeeze(sen_data1)),
                                        radius=radius,
                                        dist=dist)
    elif sensors == 'separate':
        dtw = 0.0
        path = []
        for i_sensor in range(sen_data0.shape[0]):
            if radius < 0:
                curr_dist, curr_path = fastdtw.dtw(
                    np.transpose(np.squeeze(sen_data0[i_sensor, :])),
                    np.transpose(np.squeeze(sen_data1[i_sensor, :])),
                    dist=dist)
            else:
                curr_dist, curr_path = fastdtw.fastdtw(
                    np.transpose(np.squeeze(sen_data0[i_sensor, :])),
                    np.transpose(np.squeeze(sen_data1[i_sensor, :])),
                    radius=radius,
                    dist=dist)
            curr_path = np.array(curr_path)
            dtw += curr_dist
            path.append(curr_path)
    elif sensors == 'three':
        dtw = 0.0
        path = []
        for i_sensor in range(0, sen_data0.shape[0], 3):
            if radius < 0:
                curr_dist, curr_path = fastdtw.dtw(
                    np.transpose(
                        np.squeeze(sen_data0[i_sensor:(i_sensor + 3), :])),
                    np.transpose(
                        np.squeeze(sen_data1[i_sensor:(i_sensor + 3), :])),
                    dist=dist)
            else:
                curr_dist, curr_path = fastdtw.fastdtw(
                    np.transpose(
                        np.squeeze(sen_data0[i_sensor:(i_sensor + 3), :])),
                    np.transpose(
                        np.squeeze(sen_data1[i_sensor:(i_sensor + 3), :])),
                    radius=radius,
                    dist=dist)
            curr_path = np.array(curr_path)
            dtw += curr_dist
            path.append(curr_path)
    else:
        if radius < 0:
            dtw, path = fastdtw.dtw(
                np.transpose(np.squeeze(sen_data0[2::3, :])),
                np.transpose(np.squeeze(sen_data1[2::3, :])),
                dist=dist)
        else:
            dtw, path = fastdtw.fastdtw(
                np.transpose(np.squeeze(sen_data0[2::3, :])),
                np.transpose(np.squeeze(sen_data1[2::3, :])),
                radius=radius,
                dist=dist)
    return dtw, path
コード例 #27
0
def build_Adtw(data, wells, nwells, npts_in_well, cum_pts, cols):
    formations = data['FormationClass'].unique()
    max_num_pairs = int(nwells * (nwells - 1) / 2 * np.max(npts_in_well))
    max_nz_in_row = int(np.max(npts_in_well) * 2)
    max_num_rows = max_num_pairs
    max_num_nonzero = max_num_rows * max_nz_in_row
    dist = np.zeros([len(wells), len(wells)])
    distformation = np.zeros([len(wells), len(wells), len(formations)])
    indices = np.zeros(max_num_nonzero, dtype=int)
    indptr = np.zeros(max_num_rows + 1, dtype=int)
    Adata = np.zeros(max_num_nonzero)
    b = np.zeros(max_num_rows)
    bounds = np.ones(len(data))
    nz_rows = 0
    nz_indices = 0

    def add_shift_sum(Adata, indices, nz_indices, i, path, cum_pts, wellidx,
                      idx):
        col0 = cum_pts[wellidx]
        col1 = cum_pts[wellidx] + path[i][idx]
        num_added_indices = col1 - col0 + 1
        indices[nz_indices:nz_indices + num_added_indices] = np.arange(
            col0, col1 + 1)
        #1-2*idx so when idx=0 put +1 in Adata, when idx=1 put -1 in Adata
        Adata[nz_indices:nz_indices +
              num_added_indices] = np.ones(num_added_indices) * (1 - 2 * idx)
        return num_added_indices

    def add_row(Adata, indices, indptr, b, nz_rows, nz_indices, i, path,
                cum_pts, wellidxs):
        num_added_indices = 0
        indptr[nz_rows] = nz_indices
        for idx in [0, 1]:
            num_added_indices = add_shift_sum(Adata, indices, nz_indices, i,
                                              path, cum_pts, wellidxs[idx],
                                              idx)
            nz_indices = nz_indices + num_added_indices
        b[nz_rows] = 0.0
        return nz_indices

    weightsum = 0.0
    for well1idx in range(nwells - 1):
        for well2idx in range(well1idx + 1, nwells):
            w1df = data.loc[data['Well Name'] == wells[well1idx],
                            cols + ['FormationClass']]
            w2df = data.loc[data['Well Name'] == wells[well2idx],
                            cols + ['FormationClass']]
            w1formations = w1df['FormationClass'].unique()
            w2formations = w2df['FormationClass'].unique()
            nzcols = []
            path = []
            for col in cols:
                if (np.all(np.isfinite(w1df[col]))
                        & np.all(np.isfinite(w2df[col]))):
                    nzcols.append(col)
            for formation in formations:
                if (formation in w1formations) & (formation in w2formations):
                    w1f = w1df.loc[w1df['FormationClass'] == formation, nzcols]
                    w2f = w2df.loc[w2df['FormationClass'] == formation, nzcols]
                    w1 = np.array(w1f.values)
                    w2 = np.array(w2f.values)
                    dist_tmp, path_tmp = fastdtw.dtw(w1, w2, 2)
                    dist[well1idx, well2idx] += dist_tmp
                    distformation[
                        well1idx, well2idx,
                        formations.tolist().index(formation)] = dist_tmp
                    for pair in path_tmp:
                        idx1 = w1f.index[pair[0]] - w1df.index[0]
                        idx2 = w2f.index[pair[1]] - w2df.index[0]
                        path.append((idx1, idx2))
            bounds[cum_pts[well1idx]] = np.max(
                [bounds[cum_pts[well1idx]], path[0][1]])
            bounds[cum_pts[well2idx]] = np.max(
                [bounds[cum_pts[well2idx]], path[0][0]])
            #NOTE delete
            #np.save('path_%d_%d_fce.npy' % (well1idx, well2idx), path, allow_pickle = False)
            pre_nz_rows = nz_rows
            pre_nz_indices = nz_indices
            added_1 = -1
            added_2 = -1
            for i in range(len(path)):
                if ((path[i][0] != added_1) & (path[i][1] != added_2)):
                    if ((i > 0) & (i < len(path) - 1)):
                        if (((path[i][0] != path[i - 1][0]) &
                             (path[i][1] != path[i + 1][1])) |
                            ((path[i][0] != path[i + 1][0]) &
                             (path[i][1] != path[i - 1][1]))):
                            nz_indices = add_row(Adata, indices, indptr, b,
                                                 nz_rows, nz_indices, i, path,
                                                 cum_pts, [well1idx, well2idx])
                            nz_rows = nz_rows + 1
                            added_1 = path[i][0]
                            added_2 = path[i][1]
                    elif (i > 0):
                        if ((path[i][0] != path[i - 1][0]) &
                            (path[i][1] != path[i - 1][1])):
                            nz_indices = add_row(Adata, indices, indptr, b,
                                                 nz_rows, nz_indices, i, path,
                                                 cum_pts, [well1idx, well2idx])
                            nz_rows = nz_rows + 1
                            added_1 = path[i][0]
                            added_2 = path[i][1]
                    else:
                        if ((path[i][0] != path[i + 1][0]) &
                            (path[i][1] != path[i + 1][1])):
                            nz_indices = add_row(Adata, indices, indptr, b,
                                                 nz_rows, nz_indices, i, path,
                                                 cum_pts, [well1idx, well2idx])
                            nz_rows = nz_rows + 1
                            added_1 = path[i][0]
                            added_2 = path[i][1]
            num_matched_pairs = nz_rows - pre_nz_rows + 1
            p = 2.0
            weight = num_matched_pairs * (num_matched_pairs /
                                          dist[well1idx, well2idx])**(2.0 / p)
            weightsum = weightsum + weight
            Adata[pre_nz_indices:
                  nz_indices] = Adata[pre_nz_indices:nz_indices] * weight

    Adata[:nz_indices] = Adata[:nz_indices] / weightsum
    indptr[nz_rows] = nz_indices
    indptr = indptr[:nz_rows + 1]
    np.save('dtw_dist_fce.npy', dist)
    np.save('dtw_distformation_fce.npy', distformation)
    return Adata, indices, indptr, b, bounds, nz_rows, nz_indices
コード例 #28
0
ファイル: test.py プロジェクト: caiostringari/fastdtw
 def test_1d_dtw(self):
     distance = dtw(self.x_1d, self.y_1d)[0]
     self.assertEqual(distance, 2)
コード例 #29
0
#!/usr/bin/env python
"""
    test.py
"""

from __future__ import print_function

import sys
import fastdtw
import numpy as np
from tqdm import tqdm
from awarp import awarp, to_dense

for _ in tqdm(range(100)):
    s = np.sort(np.random.choice(1000, 100))
    t = np.sort(np.random.choice(1000, 100))

    new = awarp(s, t, return_matrix=False)

    ds = to_dense(s)
    dt = to_dense(t)
    base = fastdtw.dtw(ds, dt, dist=None)[0]

    assert new == base

s = np.array([1, -1, 1, -1, 1])
t = np.array([1, -1, 1, -5, 1])
awarp(s, t, preencode=False, return_matrix=True)
awarp(s, t, w=4, preencode=False, return_matrix=True)
コード例 #30
0
 def test_1d_dtw(self):
     self.assertEqual(dtw(self.x_1d, self.y_1d ,
                          dist=euclidean_distances)[0], 2)
コード例 #31
0
ファイル: UnitTests.py プロジェクト: sali45/DTW
from DTW import dtw
import fastdtw
import numpy as np

x = np.array([1, 2, 3, 4, 5], dtype='float')
y = np.array([1, 2, 3, 4, 5], dtype='float')
window = []
print fastdtw.dtw(x, y)
print dtw(x, y, None, None)


def test_dtw_distance(s1, s2):
    assert (dtw(s1, s2, None, None)[0] == fastdtw.dtw(s1, s2)[0])
    print "Test distance passed"


def test_dtw_path(s1, s2):
    assert (dtw(s1, s2, None, None)[1] == fastdtw.dtw(s1, s2)[1])
    print "Test path passed"


test_dtw_distance(x, y)
test_dtw_path(x, y)

x = np.array([1, 2, 3, 4, 5], dtype='float')
y = np.array([2, 3, 4], dtype='float')

test_dtw_distance(x, y)
test_dtw_path(x, y)

x = np.array([[1, 2, 3, 4, 5]], dtype='float')
コード例 #32
0
def main(frame_size=0.0231999, frame_stride=0.010):
    #take command line argument as the file name
    filename1 = input('Give a voice print for enrollment:')
    print('reading audio file')
    time.sleep(1)
    record_audio(filename1)
    sampling_rate, signal = read_audio(filename1)
    sound = pm.Sound(filename1)
    # print(type(signal))
    # print(signal)
    print('Feature extraction started')
    time.sleep(1)
    print('Calculating amplitude...')
    time.sleep(1)
    amp1, e1, fft1 = find_features(signal, sampling_rate, frame_size,
                                   frame_stride, 'hamming')
    print('calculating energy...')
    time.sleep(1)
    print('calculating FFT...')
    time.sleep(1)
    print("MFCC______________________________")
    time.sleep(1)
    print('Feature extraction completed')
    time.sleep(3)

    #print("MFCC______________________________")
    mfcc1 = find_mfcc(sound)
    #print(find_mfcc(sound))
    # plt.figure(figsize=(10,5))
    # plt.subplot(3,1,1)
    # plt.title('amplitude')
    # plt.plot(amp1)
    # plt.subplot(3,1,2)
    # plt.title('Short term energy')
    # plt.plot(e1)

    # plt.subplot(3,1,3)
    # plt.title('FFT')
    # plt.plot(fft1)u

    # plt.show()
    print('\n' '\n' '\n' '\n' '\n')
    filename2 = input('Give a voice authentication password:'******'Feature extraction started')
    time.sleep(1)
    print('Calculating amplitude...')
    time.sleep(1)
    # amp1, e1, fft1=find_features(signal,sampling_rate,frame_size,frame_stride,'hamming')
    print('calculating energy...')
    time.sleep(1)
    print('calculating FFT...')
    time.sleep(1)

    print("MFCC______________________________")

    time.sleep(1)
    print('Feature extraction completed')
    time.sleep(3)
    mfcc2 = find_mfcc(sound)
    #print(find_mfcc(sound))
    amp2, e2, fft2 = find_features(signal, sampling_rate, frame_size,
                                   frame_stride, 'hamming')
    amp1 = amp1.flatten()
    amp2 = amp2.flatten()
    fft1 = fft1.flatten()
    fft2 = fft2.flatten()
    mfcc1 = mfcc1.flatten()
    mfcc2 = mfcc2.flatten()
    e1 = [sum(x) for x in e1]
    e2 = [sum(x) for x in e2]
    # fft1=np.array(fft1)
    # fft2=np.array(fft2)
    # e1=e1.flatten()
    # print('correaltio',np.corrcoef(amp1[:20],amp2[:20]))
    # print('correaltio',np.corrcoef(e1[:20],e2[:20]))
    # plt.plot(np.corrcoef(e1[:20],e2[:20]))
    # plt.show()
    ecorr = np.corrcoef(e1, e1)
    # print(amp1.shape)
    # print(amp2.shape)
    amp1, amp2 = padding(amp1, amp2)
    e1, e2 = padding(e1, e2)
    fft1, fft2 = padding(fft1, fft2)
    mfcc1, mfcc2 = padding(mfcc1, mfcc2)
    # print('after')
    # print(amp1.shape)
    # print(amp2.shape)
    #ampcorr=np.corrcoef(amp1,amp2)
    # print('FFT')
    # print(fft1.shape)
    # print(fft2.shape)
    #fftcorr=np.correlate(fft1,fft2)
    # print(ampcorr)
    # print(fftcorr)
    # print(ecorr,"\n\n\n\n",ampcorr,'\n\n\n',fftcorr)
    d1, path = dtw(amp1, amp2, dist=euclidean)
    d2, path = dtw(e1, e2, dist=euclidean)
    d4, path = dtw(mfcc1, mfcc2, dist=euclidean)
    # print(d1)
    # print(d2)
    # print(d4)
    mfcc_corr = np.correlate(mfcc1, mfcc2)
    coor = mfcc_corr.flatten()
    print(coor[0] / 10000000)
    #print(coor[1])
    if coor[0] / 10000000 > 180:
        print('positive corelation')
    else:
        print('negative corelation')
    time.sleep(3)
    # print(coor[1])

    plt.plot(amp1)
    plt.plot(amp2)
    plt.show()

    plt.plot(e1)
    plt.plot(e2)
    plt.show()

    # plt.plot(fft1)
    # plt.plot(fft2)
    # plt.show()

    plt.plot(mfcc1)
    plt.plot(mfcc2)
    plt.show()

    #Dynamic Time Warping
    # manhattan_norm = lambda x, y: np.abs(x - y)
    # #d1, cost_matrix_amp1, acc_cost_matrix_amp2, path1 = dtw(np.asarray(amp1), np.asarray(amp2), dist=manhattan_norm)
    # d2, cost_matrix_e1, acc_cost_matrix_e2, path2 = dtw(np.asarray(e1), np.asarray(e2), dist=manhattan_norm)
    # #d3, cost_matrix_fft1, acc_cost_matrix_fft2, path3 = dtw(np.asarray(fft1), np.asarray(fft2), dist=manhattan_norm)
    # d4, cost_matrix_mfcc1, acc_cost_matrix_mfcc2, path4 = dtw(np.asarray(mfcc1), np.asarray(mfcc2), dist=manhattan_norm)
    # #print(d1)
    # print(d2)
    # #print(d3)
    # print(d4)
    d1, path = dtw(amp1, amp2, dist=euclidean)
    d2, path = dtw(e1, e2, dist=euclidean)
    d4, path = dtw(mfcc1, mfcc2, dist=euclidean)
    print(d1)
    print(d2)
    print(d4)
コード例 #33
0
#         dist, mapping = dtw(y_axes[i], y_axes[j])
#         print(dist)

# print('z_axes dtw values')
# for i in range(len(z_axes)-1):
#     for j in range(i, len(z_axes)):
#         dist, mapping = dtw(z_axes[i], z_axes[j])
#         print(dist)

# print('combined_axes dtw values')
# for i in range(len(combined_axes)-1):
#     for j in range(i, len(combined_axes)):

#         min_len = min(len(combined_axes[i]), len(combined_axes[j]))
#         dist, mapping = dtw(combined_axes[i][:min_len], combined_axes[j][:min_len])
#         print(dist)

# print('avg_axes dtw values')
# for i in range(len(avg_axes)-1):
#     for j in range(i, len(avg_axes)):

#         min_len = min(len(combined_axes[i]), len(combined_axes[j]))
#         dist, mapping = dtw(avg_axes[i][100:min_len], avg_axes[j][100:min_len])
#         print(dist)

window_size = 32
for i in range(0, len(avg_axes[0]) - window_size, window_size):
    dist, mappings = dtw(avg_axes[0][i:i + window_size],
                         avg_axes[0][i + window_size:i + window_size * 2])
    print(dist / len(mappings))
コード例 #34
0
        days_DIP[i] = days_DIP[i].astype(float).tolist()

        for j in range(period, len(days_DIP[i]) - period):
            mean_days_DIP[i].append(np.mean(days_DIP[i][j - period: j + period]))

    mean_PV1_temp = [np.mean([mean_days_PV1[0][i], mean_days_PV1[1][i]]) for i in range(len(mean_days_PV1[0]))]
    mean_DIP_temp = mean_days_DIP[0]#[np.mean([mean_days[0][i], mean_days[1][i], mean_days[2][i]]) for i in range(len(mean_days[0]))]

    # 5 STEP: moved average temp for healthy, PV1, PV1 + DIP

    #first = [(i, mean_healthy_temp[i]) for i in range(len(mean_healthy_temp))]
    first = mean_healthy_temp[:]
    #second = [(i, mean_PV1_temp[i]) for i in range(len(mean_healthy_temp))]
    second = ((np.array(mean_PV1_temp[:len(mean_healthy_temp)]) - np.min(mean_PV1_temp[:len(mean_healthy_temp)])) / (np.max(mean_PV1_temp[:len(mean_healthy_temp)]) - np.min(mean_PV1_temp[:len(mean_healthy_temp)]))) * (np.max(mean_healthy_temp) - np.min(mean_healthy_temp)) + np.min(mean_healthy_temp)

    dist, path = dtw(first, second, dist=euclidean)
    print(path)

    res = [0 for i in range(len(mean_healthy_temp))]
    for i in range(len(path)):
        if res[path[i][1]] == 0:
            res[path[i][1]] = (path[i][0], path[i][1])

    new_plt = ((np.array([first[val[0]] for val in res]) - np.min(mean_healthy_temp)) / (np.max(mean_healthy_temp) - np.min(mean_healthy_temp))) * (np.max(mean_PV1_temp[:len(mean_healthy_temp)]) - np.min(mean_PV1_temp[:len(mean_healthy_temp)])) + np.min(mean_PV1_temp[:len(mean_healthy_temp)])

    plt.figure(figsize=(14, 8))
    plt.plot(first, 'C0')
    plt.plot(mean_PV1_temp[:len(mean_healthy_temp)], 'C1')
    plt.plot(new_plt, 'C2')
    plt.show()