Exemplo n.º 1
0
 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)
Exemplo n.º 2
0
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'
]
Exemplo n.º 3
0
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 = [
Exemplo n.º 4
0
 def test_create(self):
     sleepset1 = SleepSet(self.patient_list)
     sleepset2 = SleepSet(self.patients)
     assert len(sleepset1) == 2
     assert len(sleepset2) == 2
Exemplo n.º 5
0
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
import features
from sklearn.ensemble import RandomForestClassifier
from scipy.stats import zscore
from sklearn.model_selection import cross_val_score, cross_validate

plt.close('all')
ss = SleepSet(cfg.folder_unisens)
# ss = ss.stratify() # only use matched participants
p = ss[1]

#%% step 1: get features
if __name__ == '__main__':

    train_x = []
    train_y = []

    for p in tqdm(ss, desc='Loading features'):
        feature_names = []
        feats = []
        stages = []
        hypno = p.get_hypno()
Exemplo n.º 6
0
import functions
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
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,
Exemplo n.º 8
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 = {}
Exemplo n.º 9
0
@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