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.model_selection import KFold, StratifiedKFold 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' ]
import scipy import scipy.signal as signal from sleep import SleepSet import seaborn as sns import matplotlib.pyplot as plt from tqdm import tqdm import plotting import ospath from itertools import permutations plt.close('all') stimer.start('All calculations') files = ospath.list_folders(cfg.folder_unisens) stimer.start() ss = SleepSet(files, readonly=True) stimer.stop() # ss = ss.filter(lambda x: 'body' in x) # only take patients with body position sensors # ss = ss.filter(lambda x: x.drug_hrv==0 and x.drug_sleep==0) ss = ss.stratify() # only use matched participants p = ss[2] # apply this type of pvalue correction per table, # see from statsmodels.stats.multitest.multipletests correction = 'holm' # only analyse the first 3 REM cycles max_epochs = int(2 * 60 * 4.5) #%% Population description descriptors = [
def test_create(self): sleepset1 = SleepSet(self.patient_list) sleepset2 = SleepSet(self.patients) assert len(sleepset1) == 2 assert len(sleepset2) == 2
Created on Mon May 25 10:54:50 2020 @author: Simon """ import numpy as np from sleep import SleepSet import config as cfg import features import pandas as pd import dateparser from tqdm import tqdm from datetime import datetime from scipy.ndimage.morphology import binary_dilation if __name__ == '__main__': ss = SleepSet(cfg.folder_unisens).stratify() header = [ 'Code', 'Epochs', '% artefact 30s', '% artefact 300s', ] table = pd.DataFrame(columns=header) for p in tqdm(ss, 'caluclating'): p.reset() art_30 = p.get_artefacts(only_sleeptime=True, wsize=30) art_300 = p.get_artefacts(only_sleeptime=True, wsize=300) row = { 'Code': p.code, 'Epochs': p.epochs_hypno,
@author: Simon Kern """ import os import numpy as np import matplotlib.pyplot as plt from tqdm import tqdm from sklearn.metrics import f1_score, roc_auc_score, roc_curve, cohen_kappa_score from sleep import SleepSet import seaborn as sns import pandas as pd data_dir = 'Z:/NT1-HRV-unisens' pred_dir = 'Z:/stanford' ss_predictions = SleepSet(pred_dir) ss_data = SleepSet(data_dir) f1s = [] accs = [] cohens = [] nt1 = [] nt1_pred = [] for p_pred in tqdm(ss_predictions, desc='evaluating'): code = os.path.basename(os.path.dirname(p_pred._folder + '/')) p = ss_data[code] nt1_pred.append(p_pred.stanford_prediction) hypno_pred = list(zip(*p_pred.hypnogram_stanford_csv.get_data()))[1] hypno_pred = np.array(hypno_pred) hypno_pred[hypno_pred == 5] = 4