From 500018b6aaa9d7b116095a8b4a2e1517d78a50dd Mon Sep 17 00:00:00 2001 From: Skudalen Date: Thu, 1 Jul 2021 13:18:26 +0200 Subject: [PATCH] feat: add function for making sample signals of session_df --- Handle_emg_data.py | 50 +++++++++++++-------- Present_data.py | 22 ++++++--- __pycache__/Handle_emg_data.cpython-38.pyc | Bin 22345 -> 22756 bytes 3 files changed, 47 insertions(+), 25 deletions(-) diff --git a/Handle_emg_data.py b/Handle_emg_data.py index 7181a87..d07356d 100644 --- a/Handle_emg_data.py +++ b/Handle_emg_data.py @@ -495,26 +495,29 @@ class DL_data_handler: def get_emg_list(self, subject_nr, session_nr) -> list: list_of_emgs = [] - for emg_nr in range(8): - df, _ = self.csv_handler.get_data(subject_nr, 'left', session_nr, emg_nr+1) - list_of_emgs.append(df) + df, _ = self.csv_handler.get_data(subject_nr, 'left', session_nr, 1) + list_of_emgs.append(df) + for emg_nr in range(7): + df, _ = self.csv_handler.get_data(subject_nr, 'left', session_nr, emg_nr+2) + list_of_emgs.append(DataFrame(df[get_emg_str(emg_nr+2)])) for emg_nr in range(8): df, _ = self.csv_handler.get_data(subject_nr, 'right', session_nr, emg_nr+1) - list_of_emgs.append(df) - return list_of_emgs + list_of_emgs.append(DataFrame(df[get_emg_str(emg_nr+1)])) + return list_of_emgs # list of emg data where first element also has timestamp column def make_subj_sample(self, list_of_emgs): - starting_point:DataFrame = list_of_emgs[0].rename(columns={'emg1':'emg'}) + # starting_point:DataFrame = list_of_emgs[0].rename(columns={'emg1':'emg'}) tot_session_df_list = [] - for i in range(2, 9): - emg_str = get_emg_str(i) - df = list_of_emgs[i-1].rename(columns={emg_str: 'emg'}) + for i in range(8): + #emg_str = get_emg_str(i) + df = list_of_emgs[i] # .rename(columns={emg_str: 'emg'}) tot_session_df_list.append(df) for i in range(1, 9): - emg_str = get_emg_str(i) - df = list_of_emgs[7+i].rename(columns={emg_str: 'emg'}) + emg_str_old = get_emg_str(i) + emg_str_new = get_emg_str(8+i) + df:DataFrame = list_of_emgs[7+i].rename(columns={emg_str_old: emg_str_new}) tot_session_df_list.append(df) - tot_session_df = starting_point.append(tot_session_df_list, ignore_index=True, sort=True) + tot_session_df = pd.concat(tot_session_df_list, axis=1) return tot_session_df @@ -523,14 +526,22 @@ class DL_data_handler: subj_samples = [] for session_nr in range(4): list_of_emg = self.get_emg_list(subject_nr+1, session_nr+1) - tot_session_df, samplerate = self.make_subj_sample(list_of_emg) - samples = np.array_split(tot_session_df, split_nr) + tot_session_df = self.make_subj_sample(list_of_emg) + samples = np.array_split(tot_session_df.to_numpy(), split_nr) for array in samples: - df = DataFrame(array) - samplerate = get_samplerate(df) - subj_samples.append([df, samplerate]) + df = DataFrame(array).rename(columns={0:'timestamp'}) + df_finished, samplerate = self.reshape_session_df_to_signal(df) + subj_samples.append([df_finished, samplerate]) - self.samples_per_subject[subject_nr] = subj_samples + self.samples_per_subject[subject_nr+1] = subj_samples + + def reshape_session_df_to_signal(self, df:DataFrame): + main_df = df[['timestamp', 1]].rename(columns={1: 'emg'}) + for i in range(2, 17): + adding_df = df[['timestamp', i]].rename(columns={i: 'emg'}) + main_df = pd.concat([main_df, adding_df], ignore_index=True) + samplerate = get_samplerate(main_df) + return main_df, samplerate # HELP FUNCTIONS: ------------------------------------------------------------------------: @@ -541,7 +552,8 @@ def get_emg_str(emg_nr): # Help: gets the min/max of a df def get_min_max_timestamp(df:DataFrame): - min = int(np.floor(df['timestamp'].min())) + #min = int(np.floor(df['timestamp'].min())) + min = df['timestamp'].min() max = df['timestamp'].max() return min, max diff --git a/Present_data.py b/Present_data.py index 2ff5c29..5f23465 100644 --- a/Present_data.py +++ b/Present_data.py @@ -100,6 +100,14 @@ def plot_all_emg_mfcc(data_list:list, label_list:list): plt.show() +def pretty(dict, indent=0): + for key, value in dict.items(): + print('\t' * indent + str(key)) + if isinstance(value, dict): + pretty(value, indent+1) + else: + print('\t' * (indent+1) + str(value)) + # DATA FUNCTIONS: --------------------------------------------------------------: # The CSV_handler takes in data_type, but only for visuals. @@ -213,13 +221,15 @@ def main(): csv_handler = CSV_handler() csv_handler.load_data('soft') - #print(csv_handler.get_data(1, 'left', 1, 1)) dl_data_handler = DL_data_handler(csv_handler) - #dl_data_handler.store_samples(10) - #print(dl_data_handler.samples_per_subject) - emg_list = dl_data_handler.get_emg_list(1, 1) - df = dl_data_handler.make_subj_sample(emg_list) - print(df.head) + dl_data_handler.store_samples(10) + dict = dl_data_handler.samples_per_subject + + test_session_df = dict.get(1)[0] + print(test_session_df) + df, _ = dl_data_handler.reshape_session_df_to_signal(test_session_df) + print(df[:50]) + main() \ No newline at end of file diff --git a/__pycache__/Handle_emg_data.cpython-38.pyc b/__pycache__/Handle_emg_data.cpython-38.pyc index e606f1122dec42abe3ebe3eabcf02da2d4f555f8..033f0e4324f6b8593ae743def950dc1dde53d9fe 100644 GIT binary patch delta 2225 zcmaJ@QEwDg6rOu$c4oF+hOI3v3oERxP=-QNAShG>ff^cUw}oYC*nrE>J6)#i&bD`^ ziexrxQcZk-gm@EQ^r8ERAsW(0|A0iJ?;CwEk{FDMA-)>pId^I;8k}ano^$TmGjqQC z-Lw1XbMp8hv5K2EbtrHR-1*_!qmLe1zb8m(mFiy+GAO<_4qJO*joS!&0mQ@N7BOIY z#1?nWJh4~5H~Xz)L$K0LjXTw);HER_aQ7zP_peYr;QKLT#aZ>Yol~w8I(%4+_D$#` zu*46n-s$^O)8l8Y-phVrC9yC+2q5l_@6=yG?^V$?+AZE2GWDbAz9xP&yH{I7r=FXf z*_e2GH2>Vhp3a~?7xE2dpN@}^0elBr;&h zM0g$GcIOPQ`<0yMFD!+2g1>7+x$tA7wM@Nn-3sZQ5tHb%quG- zB8)_o(Ux|FHnc_}QdelCwFpr{m8r{QOu2fbp>NEMbf)zw8}mag(!xX>UnUu4nc(La z_d$ike=0Qfh(aoTP6>!_DB(vc^laq0iJf5APYkqV>(WYsYwLUxqvrtHy0ft0`izfa zM8!xaZk1cBl!K7lhWKu3SF#`Dq|9HI+@z;p=<9)q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