fix: change to right naming conventions

This commit is contained in:
Skudalen 2021-07-02 10:26:05 +02:00
parent c36ddf1609
commit 99277bc101
5 changed files with 65 additions and 61 deletions

View File

@ -487,14 +487,14 @@ class CSV_handler:
return data_frame, samplerate
'''
'''
def get_keyboard_data(self, filename:str, pres_or_release:str='pressed'):
filepath = self.working_dir + str(filename)
df = pd.read_csv(filepath)
if pres_or_release == 'pressed':
df = df[(df['event'] == 'KeyPressed') and (df['event'] == 'KeyPressed')]
else
'''
'''
class DL_data_handler:
@ -513,7 +513,7 @@ class DL_data_handler:
4: [],
5: []
}
def get_samples_dict(self):
return self.samples_per_subject
@ -628,48 +628,52 @@ class DL_data_handler:
with open(json_path, "w") as fp:
json.dump(data, fp, indent=4)
'''
# HELP FUNCTIONS: ------------------------------------------------------------------------:
# Help: gets the str from emg nr
def get_emg_str(emg_nr):
return 'emg' + str(emg_nr)
# HELP FUNCTIONS: ------------------------------------------------------------------------:
# Help: gets the min/max of a df
def get_min_max_timestamp(df:DataFrame):
#min = int(np.floor(df['timestamp'].min()))
min = df['timestamp'].min()
max = df['timestamp'].max()
return min, max
# Help: gets the str from emg nr
def get_emg_str(emg_nr):
return 'emg' + str(emg_nr)
# Help: returns df_time_emg
def make_df_from_xandy(x, y, emg_nr):
dict = {'timestamp': x, get_emg_str(emg_nr): y}
df = DataFrame(dict)
#print(df)
return df
# Help: gets the min/max of a df
def get_min_max_timestamp(df:DataFrame):
#min = int(np.floor(df['timestamp'].min()))
min = df['timestamp'].min()
max = df['timestamp'].max()
return min, max
# Help: returns the samplerate of a df
def get_samplerate(df:DataFrame):
min, max = get_min_max_timestamp(df)
if max > 60:
seconds = max - 60 - min
else:
seconds = max - min
samples = len(df.index)
samplerate = samples / seconds
return int(samplerate)
# Help: returns df_time_emg
def make_df_from_xandy(x, y, emg_nr):
dict = {'timestamp': x, get_emg_str(emg_nr): y}
df = DataFrame(dict)
#print(df)
return df
# Takes in a df and outputs np arrays for x and y values
def get_xory_from_df(x_or_y, df:DataFrame):
swither = {
'x': df.iloc[:,0].to_numpy(),
'y': df.iloc[:,1].to_numpy()
}
return swither.get(x_or_y, 0)
# Slightly modified mfcc with inputs like below.
# Returns N (x_values from original df) and mfcc_y_values
def mfcc_custom(df:DataFrame, samplesize, windowsize, stepsize, nr_coefficients, nr_mel_filters):
# Help: returns the samplerate of a df
def get_samplerate(df:DataFrame):
min, max = get_min_max_timestamp(df)
if max > 60:
seconds = max - 60 - min
else:
seconds = max - min
samples = len(df.index)
samplerate = samples / seconds
return int(samplerate)
# Takes in a df and outputs np arrays for x and y values
def get_xory_from_df(x_or_y, df:DataFrame):
swither = {
'x': df.iloc[:,0].to_numpy(),
'y': df.iloc[:,1].to_numpy()
}
return swither.get(x_or_y, 0)
# Slightly modified mfcc with inputs like below.
# Returns N (x_values from original df) and mfcc_y_values
def mfcc_custom(df:DataFrame, samplesize, windowsize=MFCC_WINDOWSIZE,
stepsize=MFCC_STEPSIZE,
nr_coefficients=NR_COEFFICIENTS,
nr_mel_filters=NR_MEL_BINS):
N = get_xory_from_df('x', df)
y = get_xory_from_df('y', df)
return N, base.mfcc(y, samplesize, windowsize, stepsize, nr_coefficients, nr_mel_filters)

View File

@ -42,7 +42,7 @@ def plot_mfcc(mfcc_data, data_label:str):
fig, ax = plt.subplots()
mfcc_data= np.swapaxes(mfcc_data, 0 ,1)
ticks_x = ticker.FuncFormatter(lambda x, pos: '{0:g}'.format(x * mfcc_stepsize))
ticks_x = ticker.FuncFormatter(lambda x, pos: '{0:g}'.format(x * MFCC_STEPSIZE))
ax.xaxis.set_major_formatter(ticks_x)
ax.imshow(mfcc_data, interpolation='nearest', cmap=cm.coolwarm, origin='lower')
@ -55,7 +55,7 @@ def plot_3_mfcc(mfcc_data1, data_label1:str, mfcc_data2, data_label2:str, mfcc_d
fig, axes = plt.subplots(nrows=3)
plt.subplots_adjust(hspace=1.4, wspace=0.4)
ticks_x = ticker.FuncFormatter(lambda x, pos: '{0:g}'.format(x * mfcc_stepsize))
ticks_x = ticker.FuncFormatter(lambda x, pos: '{0:g}'.format(x * MFCC_STEPSIZE))
data_list = [mfcc_data1, mfcc_data2, mfcc_data3]
label_list = [data_label1, data_label2, data_label3]
@ -74,7 +74,7 @@ def plot_3_mfcc(mfcc_data1, data_label1:str, mfcc_data2, data_label2:str, mfcc_d
def plot_all_emg_mfcc(data_list:list, label_list:list):
fig, axes = plt.subplots(nrows=4, ncols=2)
plt.subplots_adjust(hspace=1.4, wspace=0.4)
ticks_x = ticker.FuncFormatter(lambda x, pos: '{0:g}'.format(x * mfcc_stepsize))
ticks_x = ticker.FuncFormatter(lambda x, pos: '{0:g}'.format(x * MFCC_STEPSIZE))
plt.autoscale()
d_list = np.array([ [data_list[0], data_list[4]],
@ -115,7 +115,7 @@ def pretty(dict):
# Takes in handler and detailes to denoise.
# Returns arrays and df
def denoice_dataset(handler:Handler.CSV_handler, subject_nr, which_arm, round, emg_nr):
def denoice_dataset(handler:CSV_handler, subject_nr, which_arm, round, emg_nr):
df = handler.get_df_from_data_dict(subject_nr, which_arm, round, emg_nr)
N = get_xory_from_df('x', df)
@ -123,7 +123,7 @@ def denoice_dataset(handler:Handler.CSV_handler, subject_nr, which_arm, round, e
cA_filt, cD_filt = soft_threshold_filter(cA, cD)
y_values = inverse_wavelet(df, cA_filt, cD_filt)
df_new = Handler.make_df_from_xandy(N, y_values, emg_nr)
df_new = make_df_from_xandy(N, y_values, emg_nr)
return df_new
@ -157,9 +157,9 @@ def mfcc_3_plots_1_1_2(csv_handler:CSV_handler):
#print(df1.head, samplerate1)
#print(df2.head, samplerate2)
#print(df3.head, samplerate3)
N1, mfcc_feat1 = mfcc_custom(df1, samplerate1, mfcc_windowsize, mfcc_stepsize)
N2, mfcc_feat2 = mfcc_custom(df2, samplerate2, mfcc_windowsize, mfcc_stepsize)
N3, mfcc_feat3 = mfcc_custom(df3, samplerate3, mfcc_windowsize, mfcc_stepsize)
N1, mfcc_feat1 = mfcc_custom(df1, samplerate1, MFCC_WINDOWSIZE, MFCC_STEPSIZE)
N2, mfcc_feat2 = mfcc_custom(df2, samplerate2, MFCC_WINDOWSIZE, MFCC_STEPSIZE)
N3, mfcc_feat3 = mfcc_custom(df3, samplerate3, MFCC_WINDOWSIZE, MFCC_STEPSIZE)
label_1 = 'Subject 1, session 1, left arm, emg nr. 1'
label_2 = 'Subject 1, session 2, left arm, emg nr. 1'
label_3 = 'Subject 2, session 1, left arm, emg nr. 1'
@ -176,9 +176,9 @@ def mfcc_3_plots_3_3_4(csv_handler:CSV_handler):
#print(df1.head, samplerate1)
#print(df2.head, samplerate2)
#print(df3.head, samplerate3)
N1, mfcc_feat1 = mfcc_custom(df1, samplerate1, mfcc_windowsize, mfcc_stepsize)
N2, mfcc_feat2 = mfcc_custom(df2, samplerate2, mfcc_windowsize, mfcc_stepsize)
N3, mfcc_feat3 = mfcc_custom(df3, samplerate3, mfcc_windowsize, mfcc_stepsize)
N1, mfcc_feat1 = mfcc_custom(df1, samplerate1, MFCC_WINDOWSIZE, MFCC_STEPSIZE)
N2, mfcc_feat2 = mfcc_custom(df2, samplerate2, MFCC_WINDOWSIZE, MFCC_STEPSIZE)
N3, mfcc_feat3 = mfcc_custom(df3, samplerate3, MFCC_WINDOWSIZE, MFCC_STEPSIZE)
label_1 = 'Subject 3, session 1, left arm, emg nr. 1'
label_2 = 'Subject 3, session 2, left arm, emg nr. 1'
label_3 = 'Subject 4, session 1, left arm, emg nr. 1'
@ -194,14 +194,14 @@ def mfcc_all_emg_plots(csv_handler:CSV_handler):
df6, samplerate6 = csv_handler.get_data( 1, 'left', 1, 6)
df7, samplerate7 = csv_handler.get_data( 1, 'left', 1, 7)
df8, samplerate8 = csv_handler.get_data( 1, 'left', 1, 8)
N1, mfcc_feat1 = csv_handler.mfcc_custom(df1, samplerate1, MFCC_WINDOWSIZE, MFCC_STEPSIZE)
N2, mfcc_feat2 = csv_handler.mfcc_custom(df2, samplerate2, MFCC_WINDOWSIZE, MFCC_STEPSIZE)
N3, mfcc_feat3 = csv_handler.mfcc_custom(df3, samplerate3, MFCC_WINDOWSIZE, MFCC_STEPSIZE)
N4, mfcc_feat4 = csv_handler.mfcc_custom(df4, samplerate4, MFCC_WINDOWSIZE, MFCC_STEPSIZE)
N5, mfcc_feat5 = csv_handler.mfcc_custom(df5, samplerate5, MFCC_WINDOWSIZE, MFCC_STEPSIZE)
N6, mfcc_feat6 = csv_handler.mfcc_custom(df6, samplerate6, MFCC_WINDOWSIZE, MFCC_STEPSIZE)
N7, mfcc_feat7 = csv_handler.mfcc_custom(df7, samplerate7, MFCC_WINDOWSIZE, MFCC_STEPSIZE)
N8, mfcc_feat8 = csv_handler.mfcc_custom(df8, samplerate8, MFCC_WINDOWSIZE, MFCC_STEPSIZE)
N1, mfcc_feat1 = mfcc_custom(df1, samplerate1, MFCC_WINDOWSIZE, MFCC_STEPSIZE)
N2, mfcc_feat2 = mfcc_custom(df2, samplerate2, MFCC_WINDOWSIZE, MFCC_STEPSIZE)
N3, mfcc_feat3 = mfcc_custom(df3, samplerate3, MFCC_WINDOWSIZE, MFCC_STEPSIZE)
N4, mfcc_feat4 = mfcc_custom(df4, samplerate4, MFCC_WINDOWSIZE, MFCC_STEPSIZE)
N5, mfcc_feat5 = mfcc_custom(df5, samplerate5, MFCC_WINDOWSIZE, MFCC_STEPSIZE)
N6, mfcc_feat6 = mfcc_custom(df6, samplerate6, MFCC_WINDOWSIZE, MFCC_STEPSIZE)
N7, mfcc_feat7 = mfcc_custom(df7, samplerate7, MFCC_WINDOWSIZE, MFCC_STEPSIZE)
N8, mfcc_feat8 = mfcc_custom(df8, samplerate8, MFCC_WINDOWSIZE, MFCC_STEPSIZE)
feat_list = [mfcc_feat1, mfcc_feat2, mfcc_feat3, mfcc_feat4, mfcc_feat5, mfcc_feat6, mfcc_feat7, mfcc_feat8]
label_1 = 'Subject 1, session 1, left arm, emg nr. 1'
label_2 = 'Subject 1, session 1, left arm, emg nr. 2'

View File

@ -3,7 +3,7 @@ from pandas.core.frame import DataFrame
from scipy.fft import fft, fftfreq
import pywt
import sys
import Handle_emg_data as Handler
from Handle_emg_data import *
@ -17,7 +17,7 @@ def fft_of_df(df:DataFrame):
y_values = get_xory_from_df('y', df)
N = y_values.size
norm = normalize_wave(y_values)
N_trans = fftfreq(N, 1 / Handler.get_samplerate(df))
N_trans = fftfreq(N, 1 / get_samplerate(df))
y_f = fft(norm)
return N_trans, y_f