コード例 #1
0
def get_distances(data, data_array, max_warping_window):
    a = np.zeros(len(data_array))
    for i in range(0,len(data_array)):
        dist = dtw_distance(data_array[i], data, max_warping_window)
        a[i] = dist
        print str(i) + " - " + str(dist)
    return a
コード例 #2
0
def get_distances(data, data_array, max_warping_window):
    a = np.zeros(len(data_array))
    for i in range(0, len(data_array)):
        dist = dtw_distance(data_array[i], data, max_warping_window)
        a[i] = dist
        print(str(i) + " - " + str(dist))
    return a
コード例 #3
0
ファイル: story.py プロジェクト: fbkarsdorp/storypy
 def distance(self, other, entities='characters', constraint='none',
              window=0, normalized=True, summed=False, sentiments=False):
     if not isinstance(other, Story):
         raise ValueError("Can't compare to %s" % type(other))
     source = self.to_dataframe(sentiments=sentiments, summed=summed).values
     target = other.to_dataframe(
         sentiments=sentiments, summed=summed).values
     return dtw_distance(source, target, constraint=constraint, window=window, step_pattern=2, normalized=normalized)
コード例 #4
0
ファイル: story.py プロジェクト: fbkarsdorp/storypy
    def map(self, other, entities='characters', constraint='none', window=0, threshold=0.6):
        source, target = (self, other) if len(self.characters) > other.characters else (other, self)
        source_df = source.to_dataframe(entities=entities)
        target_df = target.to_dataframe(entities=entities)

        dm = np.zeros((len(source.characters), len(target.characters)))
        for i, character_i in enumerate(source_df.index):
            for j, character_j in enumerate(target_df.index):
                chars_s, chars_t = source_df.ix[i].values, target_df.ix[j].values
                if chars_s.shape[0] < chars_t.shape[0]:
                    chars_s, chars_t = chars_t, chars_s
                d = dtw_distance(chars_s, chars_t, constraint=constraint, window=window, normalized=True)
                dm[i, j] = d
        return pd.DataFrame(dm, index=source_df.index, columns=target_df.index)
コード例 #5
0
    def _distance_matrix(self, x, y):
        count = 0

        x_shape = np.shape(x)
        y_shape = np.shape(y)

        distance_matrix         = np.zeros((x_shape[0], y_shape[0])) 
        distance_matrix_size    = x_shape[0] * y_shape[0]

        for i in xrange(0, x_shape[0]):
            for j in xrange(0, y_shape[0]):
                # Compute DTW
                distance_matrix[i, j] = dtw_distance(x[i], y[j], self.max_warping_window)

                # Update progress
                count += 1
                self._show_progress(distance_matrix_size, count)
    
        print '\r\n'

        return distance_matrix
コード例 #6
0
    def _distance_matrix(self, x, y, show_progress=True):
        count = 0

        x_shape = np.shape(x)
        y_shape = np.shape(y)

        distance_matrix = np.zeros((x_shape[0], y_shape[0]))
        distance_matrix_size = x_shape[0] * y_shape[0]

        for i in range(0, x_shape[0]):
            for j in range(0, y_shape[0]):
                # Compute DTW
                distance_matrix[i, j] = dtw_distance(x[i], y[j],
                                                     self.max_warping_window)

                # Update progress
                count += 1

                if show_progress and count % 50 == 0:
                    self._show_progress(distance_matrix_size, count)

        return distance_matrix
コード例 #7
0
    def _distance_matrix(self, x, y):
        count = 0

        x_shape = np.shape(x)
        y_shape = np.shape(y)

        distance_matrix = np.zeros((x_shape[0], y_shape[0]))
        distance_matrix_size = x_shape[0] * y_shape[0]

        for i in xrange(0, x_shape[0]):
            for j in xrange(0, y_shape[0]):
                # Compute DTW
                distance_matrix[i, j] = dtw_distance(x[i], y[j],
                                                     self.max_warping_window)

                # Update progress
                count += 1
                self._show_progress(distance_matrix_size, count)

        print '\r\n'

        return distance_matrix
コード例 #8
0
ファイル: recognizer.py プロジェクト: TomekZet/PiRD-DTW
def compare(spec1, spec2, radius):
    return dtw.dtw_distance(spec1, spec2, radius)
コード例 #9
0
def compute_labels(data, dataarray, w, window_size, coordinate, should_plot):
    click.echo('--- Compute labels ---')

    desired_csv = load_csvs(data, should_preprocess=False)
    data_csv = load_csvs(dataarray, should_preprocess=False)

    for index, flight_data_number in enumerate(data_csv):
        flight_data_number['data']['flight_number'] = index

    # For Test
    last_second = desired_csv[0]['data']['seconds'].iloc[-1]

    #click.echo('  - data        : %s ' % data)
    click.echo('  - dataarray   : %s ' % dataarray)
    click.echo('  - w           : %d ' % w)

    click.echo('\nRunning...')

    # For plot-pdfs title
    ts = time.time()
    st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d_%H%M%S')
    
    flight_dict = {}
    #flight_full_data = pd.DataFrame()
    flight_dataframe = pd.DataFrame()

    for time_window in range(0, last_second, window_size):
        click.echo('Time window is: %d ' % time_window)

        desired_window = get_windows(desired_csv, time_window, time_window + window_size - 1)
        data_window = get_windows(data_csv, time_window, time_window + window_size - 1)
        #print(data_window)


        # Compute all DTWs

        desired_flight = desired_window[0]['window'][coordinate].tolist()

        dtws = []

        for index, flight_window in enumerate(data_window):
            # Compute DTW of window with desired_window ; window[coordinate].tolist()
            pos_data = flight_window['window'][coordinate].tolist()
            flight_seconds = flight_window['window']['seconds'].tolist()
            
            dtw = dtw_distance(desired_flight, pos_data, w)
            # Add key DTW to window ; window['dtw']
            flight_window['dtw'] = dtw
            # Track all DTWS ; append dtw value
            dtws.append( dtw )
            #click.echo('Done. Plots have been saved.')
            
            print(dtw)

            if should_plot:
                # plotting desired data with each flight for each window
                plt.plot(desired_flight, label='Desired')
                plt.plot(pos_data, label='Unlabelled')
                #plt.xlim(0,15)
                plt.ylim(0,2.1)
                plt.xlabel('Seconds')
                plt.ylabel('Meters')
                plt.title('Desired and unlabelled data. DTW = {}'.format(dtw))
                plt.legend()
                plt.grid(True)
                plt.savefig('plots/z/desired_each_flight/dtwValues_{}_{}_{}.jpeg'.format(st, time_window, index))
                plt.cla()
       
        
        print(dtws)
        if should_plot:
            print(dtws)
            dtws = [550 if x==9.223372036854776e+18 else x for x in dtws]
            # plotting dtw values of all flights for each window
            plt.figure(figsize=(20, 10))
            plt.plot(dtws, color='#006600', alpha=.9) # marker='o', linestyle='--',
            plt.ylim([0, 600])

            flights_x = [i for i in range(0, 53)]
            for flight_x, dtw in zip(flights_x, dtws):
                plt.text(flight_x, dtw, str(dtw))

            plt.grid(True)
            #plt.show()
            plt.savefig('plots/z/dtw_values/dtwValues_{}_{}.pdf'.format(st, time_window))
            plt.cla()

            click.echo('Done. Plots have been saved.')


        # Loop again and update labels
            
        for flight_window in data_window:

            if flight_window['dtw'] < 100:
                flight_window['label'] = 1
            elif flight_window['dtw'] < 299:
                flight_window['label'] = 2
            else: 
                flight_window['label'] = 3

            # Save to flight_dict
            fn = flight_window['window']['flight_number'].iloc[0]
            flight_full_data = flight_window['window']
            flight_full_data.is_copy = False
            flight_full_data['label'] = flight_window['label']
            flight_full_data['dtw'] = flight_window['dtw']

            if fn not in flight_dict.keys():
                flight_dict[fn] = flight_full_data # flight_window['window']
            else:
                flight_dict[fn] = pd.concat([flight_dict[fn], flight_full_data]) # flight_window['window']

    for flight_index in flight_dict:
        flight_dataframe = pd.DataFrame(flight_dict[flight_index])
        flight_dataframe.to_csv('data/labelled/z/f_{}.csv'.format(flight_index), sep=';', encoding='utf-8')
        print('f_{}.csv saved!'.format(flight_index))

   
    click.echo('\nDone.')