Created
February 9, 2022 15:35
-
-
Save SherazKhan/950bc19ce48229ebf50edd208a717a18 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib | |
matplotlib.use('TkAgg') | |
import mne | |
import h5py | |
import numpy as np | |
import matplotlib.pyplot as plt | |
import numpy.typing as npt | |
plt.ion() | |
def zero_detector(arr: npt.NDArray[np.uint]) -> list([npt.NDArray[np.uint], | |
npt.NDArray[np.uint]]): | |
"""Return list of time windows where values are zeros. | |
Args: | |
arr: Given 1D array | |
Returns: | |
windows: median absolute deviation | |
""" | |
# Create an array that is 1 where a is 0, and pad each end with an extra 0. | |
iszero = np.concatenate(([0], np.equal(arr, 0).view(np.int8), [0])) | |
absdiff = np.abs(np.diff(iszero)) | |
# Runs start and end where absdiff is 1. | |
windows = np.where(absdiff == 1)[0].reshape(-1, 2) | |
return windows | |
h5_file = 'JoEE_wear_bed_2021-11-01\JoEE_wear_bed_2021-11-01.h5' | |
data = dict() | |
hf = h5py.File(h5_file, "r" ) | |
data['accel'] = np.array(hf.get( "accelerometerAdxlRaw")) | |
data['fs'] = np.int(np.array(hf.get("sampleRate" ))[0][0]) | |
data['temp'] = np.array(hf.get("temperatureAdxlRaw" )) | |
data['wear_nowear'] = np.array(hf.get("wearTruth" )) | |
data['in_bed'] = np.array(hf.get("inBedTruth")) | |
hf.close() | |
data['accel_filt'] = mne.filter.filter_data(data['accel'][1:], data['fs'], 0.05, 1).T | |
time_mins = (data['accel'][0] - data['accel'][0][0])/60 | |
time = np.arange(0, len(data['accel'].T)/data['fs'] , 1/data['fs']) | |
file = h5py.File('test_wear1.h5', 'w') | |
file.create_dataset('acceleration', data=data['accel'][1:, :3200]) | |
file.create_dataset('temperature', data=data['temp'][1:, :3200]) | |
file.create_dataset('wear_truth', data=data['wear_nowear'][1:, :3200]) | |
file.create_dataset('time', data=data['accel'][0, :3200]) | |
file.create_dataset('sfreq', data=data['fs']) | |
file.close() | |
win = int(data['fs'] * 60) # One Minute | |
step = data['fs'] # One seconds | |
len_data = data['temp'].shape[1] | |
win_accel = [] | |
win_temp = [] | |
win_time = [] | |
for index in range(0, len_data - win, step): | |
win_accel.append(np.mean(np.abs(np.std(data['accel_filt'][index:index+win, :], axis = 0)))) | |
win_temp.append(np.mean(data['temp'][1, index:index+win])) | |
win_time.append(np.mean(time_mins[index:index+win])) | |
win_accel = np.array(win_accel) | |
win_temp = np.array(win_temp) | |
win_temp = np.array(win_temp) | |
predict = np.int8(np.bitwise_and(win_temp > 25, win_accel*1000 > 2.1)) | |
non_wear_wins = zero_detector(predict) | |
for non_wear_win in non_wear_wins: | |
if (non_wear_win[1] - non_wear_win[0]) < 800: | |
predict[non_wear_win[0]:non_wear_win[1]] = predict[non_wear_win[0]-1] | |
wear_wins = zero_detector(1 - predict) | |
for wear_win in wear_wins: | |
if (wear_win[1] - wear_win[0]) < 2400: | |
predict[wear_win[0]:wear_win[1]] = predict[wear_win[0]-1] | |
plt.figure() | |
params = {'legend.fontsize': 'x-large', | |
'figure.figsize': (16, 8), | |
'axes.labelsize': 'xx-large', | |
'axes.titlesize':'xx-large', | |
'xtick.labelsize':'xx-large', | |
'ytick.labelsize':'xx-large'} | |
plt.rcParams.update(params) | |
plt.plot(time_mins, data['accel_filt'] + 25, 'r', label='Acceleration (g) + 25') | |
plt.plot(time_mins, data['temp'][1],'g', label='Temperature (C)') | |
plt.plot(time_mins, data['wear_nowear'][1]*2+25, 'k', linewidth=4, label='Truth (25 Non-wear, 27 Wear)') | |
plt.plot(win_time, predict*2+25, 'm', label='Prediction (25 Non-wear, 27 Wear)',linewidth=4) | |
plt.xlabel('Time (min)') | |
plt.ylabel('Temperature (C)') | |
plt.xlim([time_mins[0], time_mins[-1]]) | |
plt.ylim([15, 35]) | |
plt.legend() | |
plt.tight_layout() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment