Skip to content
Snippets Groups Projects
Commit 56a0b294 authored by Raymond Chia's avatar Raymond Chia
Browse files

added docs

parent 1b5a2fc2
No related merge requests found
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
...@@ -77,8 +77,8 @@ def run_fft(data, fs): ...@@ -77,8 +77,8 @@ def run_fft(data, fs):
xf = fftfreq(N,T)[:N//2] xf = fftfreq(N,T)[:N//2]
return xf, yf return xf, yf
def do_pad_fft(sig, fs=IMU_FS): def do_pad_fft(sig, fs=IMU_FS, fr=0.02):
pad_len = npads_frequency_resolution(len(sig), fs=fs) pad_len = npads_frequency_resolution(len(sig), fs=fs, fr=fr)
data_pad = np.pad(sig.squeeze(), (0, pad_len), 'constant', constant_values=0) data_pad = np.pad(sig.squeeze(), (0, pad_len), 'constant', constant_values=0)
data_xf, data_yf = run_fft(data_pad, fs) data_xf, data_yf = run_fft(data_pad, fs)
return data_xf, data_yf return data_xf, data_yf
......
...@@ -367,47 +367,29 @@ def sync_to_ref(df0, df1): ...@@ -367,47 +367,29 @@ def sync_to_ref(df0, df1):
return dsync0.sync_df(df0), dsync1.sync_df(df1) return dsync0.sync_df(df0), dsync1.sync_df(df1)
# Multiprocessing task for windowing dataframe # Task for windowing dataframe
def imu_df_win_task(w_inds, df, i, cols): def df_win_task(w_inds, df, i, cols):
time = df['sec'].values """
if w_inds[-1] == 0: return Performs signal processing on IMU. If BVP values exist in the column
w_df = df.iloc[w_inds] namespace, BVP signal processing is performed. Extract median BR from the
t0, t1 = time[w_inds][0], time[w_inds][-1] summary bioharness file and max frequency of the PSS wave.
diff = time[w_inds[1:]] - time[w_inds[0:-1]] Add index value for each window for tsfresh processing.
mask = np.abs(diff)>20
diff_chk = np.any(mask)
if diff_chk:
return
if cols is None:
cols = IMU_COLS
data = w_df[cols].values
# DSP
sd_data = (data - np.mean(data, axis=0))/np.std(data, axis=0)
# ys = cubic_interp(sd_data, BR_FS, FS_RESAMPLE)
filt_data = imu_signal_processing(sd_data, IMU_FS)
x_out = pd.DataFrame(filt_data,
columns=IMU_COLS)
sm_out = w_df['BR'].values
ps_out = w_df['PSS'].values
x_vec_time = np.median(time[w_inds])
fs = 1/np.mean(diff)
ps_freq = int(get_max_frequency(ps_out, fs=fs))
y_tmp = np.array([x_vec_time, np.nanmedian(sm_out), ps_freq])
x_out['sec'] = x_vec_time
x_out['id'] = i
y_out = pd.DataFrame([y_tmp], columns=['sec', 'br', 'pss'])
return x_out, y_out Attributes
----------
w_inds : numpy.ndarray
specifies the window indexes for the df
df : pandas.DataFrame
DataFrame to extract window from
i : int
window index
cols : list
columns to perform data processing functions across
def df_win_task(w_inds, df, i, cols): Returns
-------
pandas.DataFrame, pandas.DataFrame
"""
time = df['sec'].values time = df['sec'].values
if w_inds[-1] == 0: return if w_inds[-1] == 0: return
w_df = df.iloc[w_inds] w_df = df.iloc[w_inds]
...@@ -417,6 +399,8 @@ def df_win_task(w_inds, df, i, cols): ...@@ -417,6 +399,8 @@ def df_win_task(w_inds, df, i, cols):
fs_est = 1/np.mean(diff) fs_est = 1/np.mean(diff)
if fs_est > 70 and 'acc_x' in cols: fs = IMU_FS if fs_est > 70 and 'acc_x' in cols: fs = IMU_FS
elif fs_est < 70 and 'bvp' in cols: fs = PPG_FS elif fs_est < 70 and 'bvp' in cols: fs = PPG_FS
# Reject window if there is a time difference between rows greater than 20s
mask = np.abs(diff)>20 mask = np.abs(diff)>20
diff_chk = np.any(mask) diff_chk = np.any(mask)
if diff_chk: if diff_chk:
...@@ -445,6 +429,7 @@ def df_win_task(w_inds, df, i, cols): ...@@ -445,6 +429,7 @@ def df_win_task(w_inds, df, i, cols):
x_vec_time = np.median(time[w_inds]) x_vec_time = np.median(time[w_inds])
fs = 1/np.mean(diff) fs = 1/np.mean(diff)
ps_out = pressure_signal_processing(ps_out, fs=fs)
ps_freq = int(get_max_frequency(ps_out, fs=fs)) ps_freq = int(get_max_frequency(ps_out, fs=fs))
y_tmp = np.array([x_vec_time, np.nanmedian(sm_out), ps_freq]) y_tmp = np.array([x_vec_time, np.nanmedian(sm_out), ps_freq])
...@@ -458,18 +443,40 @@ def df_win_task(w_inds, df, i, cols): ...@@ -458,18 +443,40 @@ def df_win_task(w_inds, df, i, cols):
if 'bvp' in cols: if 'bvp' in cols:
xf, yf = do_pad_fft(bvp_filt, fs=fs) xf, yf = do_pad_fft(bvp_filt, fs=fs)
bv_freq = int(xf[yf.argmax()]*60) bv_freq = int(xf[yf.argmax()]*60)
# y_out['bvp_est'] = bv_freq # Uncomment if you wish to extract BVP estimated HR.
# y_out['hr_est'] = bv_freq
return x_out, y_out return x_out, y_out
def get_max_frequency(data, fs=IMU_FS): def get_max_frequency(data, fs=IMU_FS, fr=0.02):
data = pressure_signal_processing(data, fs=fs) """
Returns the maximum frequency after padded fft
Attributes
----------
data : numpy.ndarray
signal to extract max frequency
fs : int
signal sampling frequency (default = IMU_FS)
fr : float
frequency resolution to set pad length (default = 0.02)
xf, yf = do_pad_fft(data, fs=fs) Returns
-------
float
"""
xf, yf = do_pad_fft(data, fs=fs, fr=fr)
max_freq = xf[yf.argmax()]*60 max_freq = xf[yf.argmax()]*60
return max_freq return max_freq
def convert_to_float(df): def convert_to_float(df):
"""
Converts 'sec', 'pss', 'br', and 'subject' columns to float
Attributes
----------
df : pandas.DataFrame
"""
cols = df.columns.values cols = df.columns.values
if 'sec' in cols: if 'sec' in cols:
df['sec'] = df['sec'].astype(float) df['sec'] = df['sec'].astype(float)
...@@ -481,6 +488,22 @@ def convert_to_float(df): ...@@ -481,6 +488,22 @@ def convert_to_float(df):
df['subject'] = df['subject'].astype(float) df['subject'] = df['subject'].astype(float)
def load_and_sync_xsens(subject, sens_list:list=['imu', 'bvp']): def load_and_sync_xsens(subject, sens_list:list=['imu', 'bvp']):
"""
Loads requested sensors from the subject folder and synchronises each to
the beginning and end timestamps. Linearly interpolates the data and
timestamps to match the higher frequency data.
Arguments
---------
subject : str
subject to extract data from (i.e. 'Pilot02', 'S02')
sens_list : list
a list that contains either or both 'imu' and 'bvp'
Returns
-------
pd.DataFrame
"""
assert 'imu' in sens_list or 'bvp' in sens_list, \ assert 'imu' in sens_list or 'bvp' in sens_list, \
f"{sens_list} is not supported, must contain"\ f"{sens_list} is not supported, must contain"\
"'imu', 'bvp' or 'imu, bvp'" "'imu', 'bvp' or 'imu, bvp'"
...@@ -610,10 +633,6 @@ def load_tsfresh(xsens_df, home_dir, ...@@ -610,10 +633,6 @@ def load_tsfresh(xsens_df, home_dir,
""" """
assert data_cols is not None, "invalid selection for data columns" assert data_cols is not None, "invalid selection for data columns"
assert 'acc_x' in xsens_df.columns.tolist() and \
'gyro_x' in xsens_df.columns.tolist() and \
'bvp' in xsens_df.columns.tolist(), \
"Does not include the full required dataset. Must have both IMU and BVP"
# raise NotImplementedError("To be implemented") # raise NotImplementedError("To be implemented")
...@@ -629,6 +648,12 @@ def load_tsfresh(xsens_df, home_dir, ...@@ -629,6 +648,12 @@ def load_tsfresh(xsens_df, home_dir,
if exists(pkl_file) and not overwrite: if exists(pkl_file) and not overwrite:
return pd.read_pickle(pkl_file) return pd.read_pickle(pkl_file)
assert 'acc_x' in xsens_df.columns.tolist() and \
'gyro_x' in xsens_df.columns.tolist() and \
'bvp' in xsens_df.columns.tolist(), \
"First instance must include the full required dataset. Must have both "\
"IMU and BVP"
x_df, y_df = get_df_windows(xsens_df, x_df, y_df = get_df_windows(xsens_df,
df_win_task, df_win_task,
window_size=window_size, window_size=window_size,
...@@ -847,6 +872,8 @@ def dsp_win_func(w_inds, df, i, cols): ...@@ -847,6 +872,8 @@ def dsp_win_func(w_inds, df, i, cols):
x_vec_time = np.median(time[w_inds]) x_vec_time = np.median(time[w_inds])
fs = 1/np.mean(diff) fs = 1/np.mean(diff)
ps_out = pressure_signal_processing(ps_out, fs=fs)
ps_freq = int(get_max_frequency(ps_out, fs=IMU_FS)) ps_freq = int(get_max_frequency(ps_out, fs=IMU_FS))
y_tmp = np.array([x_vec_time, np.nanmedian(sm_out), ps_freq]) y_tmp = np.array([x_vec_time, np.nanmedian(sm_out), ps_freq])
...@@ -1226,8 +1253,8 @@ def sens_rr_model(subject, ...@@ -1226,8 +1253,8 @@ def sens_rr_model(subject,
x_test = make_windows_from_id(x_test_df, data_cols) x_test = make_windows_from_id(x_test_df, data_cols)
y_test = y_test_df[lbl_str].values.reshape(-1, 1) y_test = y_test_df[lbl_str].values.reshape(-1, 1)
# x_train = y_train_df['bvp_est'].values.reshape(-1, 1) # x_train = y_train_df['hr_est'].values.reshape(-1, 1)
# x_test = y_test_df['bvp_est'].values.reshape(-1, 1) # x_test = y_test_df['hr_est'].values.reshape(-1, 1)
print("minirocket transforming...") print("minirocket transforming...")
x_train = np.swapaxes(x_train, 1, 2) x_train = np.swapaxes(x_train, 1, 2)
x_test = np.swapaxes(x_test, 1, 2) x_test = np.swapaxes(x_test, 1, 2)
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment