Exemple #1
0
    def filter_data(self, low_f=.05, high_f=15, filter_type="bandpass"):

        print("\nFiltering data...")

        # Left ankle
        self.la.x_filt = filter_signal(data=self.la.x,
                                       filter_type=filter_type,
                                       low_f=low_f,
                                       high_f=high_f,
                                       sample_f=self.la.sample_rate)
        self.la.y_filt = filter_signal(data=self.la.y,
                                       filter_type=filter_type,
                                       low_f=low_f,
                                       high_f=high_f,
                                       sample_f=self.la.sample_rate)
        self.la.z_filt = filter_signal(data=self.la.z,
                                       filter_type=filter_type,
                                       low_f=low_f,
                                       high_f=high_f,
                                       sample_f=self.la.sample_rate)

        # Left wrist
        self.lw.x_filt = filter_signal(data=self.lw.x,
                                       filter_type=filter_type,
                                       low_f=low_f,
                                       high_f=high_f,
                                       sample_f=self.lw.sample_rate)
        self.lw.y_filt = filter_signal(data=self.lw.y,
                                       filter_type=filter_type,
                                       low_f=low_f,
                                       high_f=high_f,
                                       sample_f=self.lw.sample_rate)
        self.lw.z_filt = filter_signal(data=self.lw.z,
                                       filter_type=filter_type,
                                       low_f=low_f,
                                       high_f=high_f,
                                       sample_f=self.lw.sample_rate)

        # Right wrist
        self.rw.x_filt = filter_signal(data=self.rw.x,
                                       filter_type=filter_type,
                                       low_f=low_f,
                                       high_f=high_f,
                                       sample_f=self.rw.sample_rate)
        self.rw.y_filt = filter_signal(data=self.rw.y,
                                       filter_type=filter_type,
                                       low_f=low_f,
                                       high_f=high_f,
                                       sample_f=self.rw.sample_rate)
        self.rw.z_filt = filter_signal(data=self.rw.z,
                                       filter_type=filter_type,
                                       low_f=low_f,
                                       high_f=high_f,
                                       sample_f=self.rw.sample_rate)

        print("Complete.")
Exemple #2
0
    def filter_triaxial(x, y, z, fs=75, cutoff=10, epoch_len=1):

        print(
            "\nFiltering data with {}Hz lowpass filtering and epoching it into {}-s epochs..."
            .format(cutoff, epoch_len))

        x_filt = filter_signal(data=x,
                               filter_type='lowpass',
                               sample_f=fs,
                               low_f=cutoff,
                               filter_order=3)
        y_filt = filter_signal(data=y,
                               filter_type='lowpass',
                               sample_f=fs,
                               low_f=cutoff,
                               filter_order=3)
        z_filt = filter_signal(data=z,
                               filter_type='lowpass',
                               sample_f=fs,
                               low_f=cutoff,
                               filter_order=3)

        vm = np.sqrt(
            np.square(np.array([x_filt, y_filt, z_filt])).sum(axis=0)) - 1
        vm[vm < 0] = 0

        avm = [
            np.mean(vm[i:i + int(fs * epoch_len)])
            for i in range(0, len(vm), int(epoch_len * fs))
        ]
        svm = [
            np.sum(vm[i:i + int(fs * epoch_len)])
            for i in range(0, len(vm), int(epoch_len * fs))
        ]

        return vm, avm, svm
    def remove_gravity(self):

        if self.rem_gravity:
            print("-Filtering data to remove gravity...")

            self.lw["x_filt"] = filter_signal(data=self.lw["x"], filter_type="highpass", high_f=0.1, filter_order=2,
                                              sample_f=self.sample_rate)
            self.lw["y_filt"] = filter_signal(data=self.lw["y"], filter_type="highpass", high_f=0.1, filter_order=2,
                                              sample_f=self.sample_rate)
            self.lw["z_filt"] = filter_signal(data=self.lw["z"], filter_type="highpass", high_f=0.1, filter_order=2,
                                              sample_f=self.sample_rate)

            self.la["x_filt"] = filter_signal(data=self.la["x"], filter_type="highpass", high_f=0.1, filter_order=2,
                                              sample_f=self.sample_rate)
            self.la["y_filt"] = filter_signal(data=self.la["y"], filter_type="highpass", high_f=0.1, filter_order=2,
                                              sample_f=self.sample_rate)
            self.la["z_filt"] = filter_signal(data=self.la["z"], filter_type="highpass", high_f=0.1, filter_order=2,
                                              sample_f=self.sample_rate)
    def remove_high_freq(self):

        if self.rem_highf:
            print("\nFiltering data to remove high-frequency noise...")

            self.lw["x_filt"] = filter_signal(data=self.lw["x"], filter_type="lowpass", low_f=5, filter_order=2,
                                              sample_f=self.sample_rate)
            self.lw["y_filt"] = filter_signal(data=self.lw["y"], filter_type="lowpass", low_f=5, filter_order=2,
                                              sample_f=self.sample_rate)
            self.lw["z_filt"] = filter_signal(data=self.lw["z"], filter_type="lowpass", low_f=5, filter_order=2,
                                              sample_f=self.sample_rate)

            self.la["x_filt"] = filter_signal(data=self.la["x"], filter_type="lowpass", low_f=5, filter_order=2,
                                              sample_f=self.sample_rate)
            self.la["y_filt"] = filter_signal(data=self.la["y"], filter_type="lowpass", low_f=5, filter_order=2,
                                              sample_f=self.sample_rate)
            self.la["z_filt"] = filter_signal(data=self.la["z"], filter_type="lowpass", low_f=5, filter_order=2,
                                              sample_f=self.sample_rate)
    def filter_accels(self, low_f=0.05, high_f=15, filter_type="lowpass"):
        print("\nFiltering data...")

        # Left ankle
        if self.df_la is not None:
            print("-Left ankle")
            self.df_la["x_filt"] = filter_signal(data=self.df_la["x"], filter_type=filter_type, filter_order=3,
                                                 low_f=low_f, high_f=high_f, sample_f=self.fs)
            self.df_la["y_filt"] = filter_signal(data=self.df_la["y"], filter_type=filter_type, filter_order=3,
                                                 low_f=low_f, high_f=high_f, sample_f=self.fs)
            self.df_la["z_filt"] = filter_signal(data=self.df_la["z"], filter_type=filter_type, filter_order=3,
                                                 low_f=low_f, high_f=high_f, sample_f=self.fs)

        # Right ankle
        if self.df_ra is not None:
            print("-Right ankle")
            self.df_ra["x_filt"] = filter_signal(data=self.df_ra["x"], filter_type=filter_type, filter_order=3,
                                                 low_f=low_f, high_f=high_f, sample_f=self.fs)
            self.df_ra["y_filt"] = filter_signal(data=self.df_ra["y"], filter_type=filter_type, filter_order=3,
                                                 low_f=low_f, high_f=high_f, sample_f=self.fs)
            self.df_ra["z_filt"] = filter_signal(data=self.df_ra["z"], filter_type=filter_type, filter_order=3,
                                                 low_f=low_f, high_f=high_f, sample_f=self.fs)

        # Left wrist
        if self.df_lw is not None:
            print("-Left wrist")
            self.df_lw["x_filt"] = filter_signal(data=self.df_lw["x"], filter_type=filter_type, filter_order=3,
                                                 low_f=low_f, high_f=high_f, sample_f=self.fs)
            self.df_lw["y_filt"] = filter_signal(data=self.df_lw["y"], filter_type=filter_type, filter_order=3,
                                                 low_f=low_f, high_f=high_f, sample_f=self.fs)
            self.df_lw["z_filt"] = filter_signal(data=self.df_lw["z"], filter_type=filter_type, filter_order=3,
                                                 low_f=low_f, high_f=high_f, sample_f=self.fs)

        # Right wrist 1
        if self.df_rw_d is not None:
            print("-Right wrist #1")
            self.df_rw_d["x_filt"] = filter_signal(data=self.df_rw_d["x"], filter_type=filter_type, filter_order=3,
                                                   low_f=low_f, high_f=high_f, sample_f=self.fs)
            self.df_rw_d["y_filt"] = filter_signal(data=self.df_rw_d["y"], filter_type=filter_type, filter_order=3,
                                                   low_f=low_f, high_f=high_f, sample_f=self.fs)
            self.df_rw_d["z_filt"] = filter_signal(data=self.df_rw_d["z"], filter_type=filter_type, filter_order=3,
                                                   low_f=low_f, high_f=high_f, sample_f=self.fs)

        # Right wrist 2
        if self.df_rw_p is not None:
            print("-Right wrist #2")
            self.df_rw_p["x_filt"] = filter_signal(data=self.df_rw_p["x"], filter_type=filter_type, filter_order=3,
                                                   low_f=low_f, high_f=high_f, sample_f=self.fs)
            self.df_rw_p["y_filt"] = filter_signal(data=self.df_rw_p["y"], filter_type=filter_type, filter_order=3,
                                                   low_f=low_f, high_f=high_f, sample_f=self.fs)
            self.df_rw_p["z_filt"] = filter_signal(data=self.df_rw_p["z"], filter_type=filter_type, filter_order=3,
                                                   low_f=low_f, high_f=high_f, sample_f=self.fs)

        # Bittium Faros
        if self.df_bf is not None:
            print("-Bittium Faros")
            self.df_bf["x_filt"] = filter_signal(data=self.df_bf["x"], filter_type=filter_type, filter_order=3,
                                                 low_f=low_f, high_f=high_f, sample_f=self.bf_fs)
            self.df_bf["y_filt"] = filter_signal(data=self.df_bf["y"], filter_type=filter_type, filter_order=3,
                                                 low_f=low_f, high_f=high_f, sample_f=self.bf_fs)
            self.df_bf["z_filt"] = filter_signal(data=self.df_bf["z"], filter_type=filter_type, filter_order=3,
                                                 low_f=low_f, high_f=high_f, sample_f=self.bf_fs)

        print("Complete.")
def create_plot_gif(wrist_file=None, ankle_file=None, start_time=None, stop_time=None,
                    sample_rate=75, plot_period_ms=100, wrist_obj=None, ankle_obj=None,
                    output_dir=None,
                    slide_window=False, remove_gravity=False, remove_high_f=False, remove_dc=True):

    print("\nImporting data...")

    if wrist_obj is None:
        if "csv" in wrist_file:
            lw = pd.read_csv(wrist_file, skiprows=100)
        if "edf" in wrist_file:
            d = ImportEDF.GENEActiv(filepath=wrist_file, load_raw=True)

            d.sample_rate = 50

            lw = pd.DataFrame(list(zip(d.timestamps, d.x, d.y, d.z, [None for i in range(len(d.timestamps))],
                              [None for i in range(len(d.timestamps))], [None for i in range(len(d.timestamps))])))

        lw.columns = ["Timestamp", "x", "y", "z", "light", 'button', 'temperature']
        lw["Timestamp"] = pd.to_datetime(lw["Timestamp"], format="%Y-%m-%d %H:%M:%S:%f")

    if start_time is not None and stop_time is not None:
        lw = lw.loc[(lw["Timestamp"] >= pd.to_datetime(start_time)) &
                    (lw["Timestamp"] < pd.to_datetime(stop_time))]

    if ankle_obj is None:
        if "csv" in ankle_file:
            la = pd.read_csv(ankle_file, skiprows=100)
        if "edf" in ankle_file:
            d = ImportEDF.GENEActiv(filepath=ankle_file, load_raw=True)

            d.sample_rate = 50

            la = pd.DataFrame(list(zip(d.timestamps, d.x, d.y, d.z, [None for i in range(len(d.timestamps))],
                                       [None for i in range(len(d.timestamps))],
                                       [None for i in range(len(d.timestamps))])))

        la.columns = ["Timestamp", "x", "y", "z", "light", 'button', 'temperature']
        la["Timestamp"] = pd.to_datetime(la["Timestamp"], format="%Y-%m-%d %H:%M:%S:%f")

    if start_time is not None and stop_time is not None:
        la = la.loc[(la["Timestamp"] >= pd.to_datetime(start_time)) &
                    (la["Timestamp"] < pd.to_datetime(stop_time))]

    filenames = []

    # Converts cropped data to list
    time = [i / sample_rate for i in range(lw.shape[0])]
    lw_x = [i for i in lw["x"]]
    lw_y = [i for i in lw["y"]]
    lw_z = [i for i in lw["z"]]
    la_x = [i for i in la["x"]]
    la_y = [i for i in la["y"]]
    la_z = [i for i in la["z"]]

    if remove_gravity:

        print("-Filtering data to remove gravity...")

        lw_x = filter_signal(data=lw_x, filter_type="highpass", high_f=0.1, filter_order=2, sample_f=sample_rate)
        lw_y = filter_signal(data=lw_y, filter_type="highpass", high_f=0.1, filter_order=2, sample_f=sample_rate)
        lw_z = filter_signal(data=lw_z, filter_type="highpass", high_f=0.1, filter_order=2, sample_f=sample_rate)
        la_x = filter_signal(data=la_x, filter_type="highpass", high_f=0.1, filter_order=2, sample_f=sample_rate)
        la_y = filter_signal(data=la_y, filter_type="highpass", high_f=0.1, filter_order=2, sample_f=sample_rate)
        la_z = filter_signal(data=la_z, filter_type="highpass", high_f=0.1, filter_order=2, sample_f=sample_rate)

    if remove_high_f:

        print("-Filtering data to remove high frequency...")

        lw_x = filter_signal(data=lw_x, filter_type="lowpass", low_f=5, filter_order=2, sample_f=sample_rate)
        lw_y = filter_signal(data=lw_y, filter_type="lowpass", low_f=5, filter_order=2, sample_f=sample_rate)
        lw_z = filter_signal(data=lw_z, filter_type="lowpass", low_f=5, filter_order=2, sample_f=sample_rate)
        la_x = filter_signal(data=la_x, filter_type="lowpass", low_f=5, filter_order=2, sample_f=sample_rate)
        la_y = filter_signal(data=la_y, filter_type="lowpass", low_f=5, filter_order=2, sample_f=sample_rate)
        la_z = filter_signal(data=la_z, filter_type="lowpass", low_f=5, filter_order=2, sample_f=sample_rate)

    if remove_dc:
        print("\n-Removing DC component from signal...")

        lw_x = [i - np.mean(lw_x) for i in lw_x]
        lw_y = [i - np.mean(lw_y) for i in lw_y]
        lw_z = [i - np.mean(lw_z) for i in lw_z]
        la_x = [i - np.mean(la_x) for i in la_x]
        la_y = [i - np.mean(la_y) for i in la_y]
        la_z = [i - np.mean(la_z) for i in la_z]

    min_x = min([min(lw_x), min(la_x)])
    min_y = min([min(lw_y), min(la_y)])
    min_z = min([min(lw_z), min(la_z)])

    max_x = max([max(lw_x), max(la_x)])
    max_y = max([max(lw_y), max(la_y)])
    max_z = max([max(lw_z), max(la_z)])

    min_all = min([min_x, min_y, min_z])
    max_all = max([max_x, max_y, max_z])

    plot_rate = int(np.ceil(plot_period_ms / (1000 / sample_rate)))
    if plot_rate == 0:
        plot_rate = 1

    print("\n-Data will be plotted in {}ms increments...\n".format(plot_period_ms))

    for i in range(0, lw.shape[0], plot_rate):

        print("-Generating plot {} of {}...".format(int((i/plot_rate))+1, int(len(range(0, lw.shape[0], plot_rate)))))

        fig, (ax1, ax2) = plt.subplots(2, figsize=(10, 6))
        plt.subplots_adjust(right=.75, left=.07, hspace=.3)

        ax1.plot(time[:i], lw_x[:i], color='black')
        ax1.plot(time[:i], lw_y[:i], color='red')
        ax1.plot(time[:i], lw_z[:i], color='dodgerblue')
        ax1.axvline(time[i], color='limegreen')

        ax2.plot(time[:i], la_x[:i], color='black')
        ax2.plot(time[:i], la_y[:i], color='red')
        ax2.plot(time[:i], la_z[:i], color='dodgerblue')
        ax2.axvline(time[i], color='limegreen')

        ax1.set_ylim(min_all - .5, max_all + .5)
        ax2.set_ylim(min_all - .5, max_all + .5)

        if not slide_window:
            ax1.set_xlim(0, len(lw_x)/sample_rate)
            ax2.set_xlim(0, len(la_x)/sample_rate)

        if slide_window:
            if time[i] <= 12.5:
                ax1.set_xlim(0, 15)
                ax2.set_xlim(0, 15)
            if time[i] > 12.5:
                ax1.set_xlim(time[i]-7.5, time[i]+7.5)
                ax2.set_xlim(time[i]-7.5, time[i]+7.5)

        ax2.set_xlabel("Time (seconds)")
        ax1.set_ylabel("Acceleration")
        ax2.set_ylabel("Acceleration")
        ax1.set_title("Left Wrist")
        ax2.set_title("Left Ankle")
        ax1.set_ylabel("Acceleration")

        # create file name and append it to a list
        filename = f'{i}.png'
        filenames.append(filename)

        plt.savefig(output_dir + filename)
        plt.close()

    # build gif
    print("\nCombining images into gif...")
    with imageio.get_writer(output_dir + "Output.gif", mode='I') as writer:
        for filename in filenames:
            image = imageio.imread(output_dir + filename)
            writer.append_data(image)

    # Remove files
    for filename in set(filenames):
        os.remove(output_dir + filename)

    print("\nComplete.")

    return lw, la