def test_filter(self): s = SleepSet(self.patients) f = lambda x: 'u2' in x._folder a = s.filter(f) self.assertTrue(len(a) == 1) self.assertTrue(isinstance(a, SleepSet)) self.assertTrue('u2' in a[0]._folder)
from sklearn.metrics import f1_score, classification_report from scipy.signal import resample from sklearn.utils import shuffle from scipy.stats import zscore from functions import interpolate_nans from tqdm import tqdm import misc np.random.seed(0) tf.random.set_seed(0) os.environ['PYTHONHASHSEED'] = '0' rn.seed(0) #%% ss = SleepSet(cfg.folder_unisens) ss = ss.filter(lambda x: x.duration < 60 * 60 * 11) # only less than 14 hours ss = ss.filter( lambda x: x.group in ['control', 'nt1']) # only less than 14 hours ss = ss.filter(lambda x: np.mean(x.get_artefacts(only_sleeptime=True)) < 0.25 ) #only take patients with artefact percentage <25% #%% Load data length = 2 * 60 * 3 # first 3 hours downsample = 2 # downsample factor feat_names = [ 'mean_HR', 'rrHRV', 'SDNN', 'RMSSD', 'RR_range', 'SDSD', 'LF_power', 'HF_power', 'LF_HF', 'SD2_SD1', 'SD1', 'SD2', 'SampEn', 'pNN50', 'PetrosianFract', 'SDSD' ] feats = []
descriptors = [ 'gender', 'age', 'Number of epochs (only sleeptime)', 'Artefact ratio' ] table_general = {name: {'nt1': {}, 'control': {}} for name in descriptors} total_number_of_epochs = sum( [len(p.get_hypno(only_sleeptime=True)) for p in ss]) total_number_discarded = sum( [sum(p.get_artefacts(only_sleeptime=True)) for p in ss]) print( f'{total_number_of_epochs} are available, of which {total_number_discarded} are discarded ({total_number_discarded/total_number_of_epochs*100:.1f}%)' ) for group in ['nt1', 'control']: subset = ss.filter(lambda x: x.group == group) # gender table_general['gender'][group]['female'] = np.sum( [x.gender == 'female' for x in subset]) table_general['gender'][group]['male'] = np.sum( [x.gender == 'male' for x in subset]) table_general['gender'][group]['values'] = [ x.gender == 'male' for x in subset ] table_general['gender'][group]['mean'] = 5 table_general['gender'][group]['std'] = None table_general['gender'][group]['p'] = 2 # number of epochs values = list([len(p.get_hypno(only_sleeptime=True)) for p in subset])
import config as cfg import numpy as np import pandas as pd import seaborn as sns from tqdm import tqdm from sleep import SleepSet from datetime import timedelta import matplotlib.pyplot as plt from joblib.parallel import Parallel, delayed #%% settings length = int(2 * 60 * 3.50) # first four hours ss = SleepSet(cfg.folder_unisens) ss = ss.filter(lambda x: x.duration < 60 * 60 * 11) # only less than 14 hours ss = ss.filter(lambda x: x.group in ['control', 'nt1']) # no hypersomnikers ss = ss.filter( lambda x: np.mean(x.get_artefacts(only_sleeptime=True)[:length] ) < 0.25) # only low artefacts count <25% p = ss[1] data_y = np.array([p.group == 'nt1' for p in ss], dtype=bool) stop #%% average hypnograms to see importances hypnos = np.array([p.get_hypno(only_sleeptime=True)[:length + 1] for p in ss]) cmap = matplotlib.cm.get_cmap("magma", 6) hypno_nt1 = hypnos[data_y] hypno_cnt = hypnos[~data_y] hypno_nt1[hypno_nt1 == 5] = -1 # replace artefact with 0 hypno_cnt[hypno_cnt == 5] = -1 # replace artefact with 0
y_tree = KDTree(np.vstack([r_true, np.zeros_like(r_true)]).T, p=1) dist, idxs = y_tree.query(np.vstack([r_pred, np.zeros_like(r_pred)]).T, k = 1, return_distance=True) def compare(r_true, r_pred): """ R positions are annotated in seconds """ y_tree = KDTree(np.vstack([r_true, np.zeros_like(r_true)]).T, p=1) dist, idxs = y_tree.query(np.vstack([r_pred, np.zeros_like(r_pred)]).T, k = 1, return_distance=True) return dist, idxs ss = SleepSet(cfg.folder_unisens) ss = ss.filter(lambda x:x.ecg.sampleRate==256) p = ss[0] r_true = p.get_RR(offset=False)[0] ecg = p.ecg.get_data('ecg').squeeze() fs = p.ecg.sampleRate detectors = Detectors(fs) detector_names = ['engzee_detector', 'hamilton_detector', 'christov_detector', 'swt_detector', 'pan_tompkins_detector', 'two_average_detector'] res = {} for name in detector_names: