Package: deepsleep
DeepSleepThis is a simple framework to preprocess and classify sleep stages based on
recorded EEG/EMG/EOG data. It replicates and extends on top of theSpindle
method.InstallationFirst make a new python virtual environment and then install the package using pip:pip install deepsleepSpindle sleep scoringYou could use both preprocessing routines and CNN network for the classification
demonstrated in thepaperSpindle preprocessing routineBelow is an example of how to use the preprocessing routine:importnumpyasnpimportpyedflibfromdeepsleep.dataimportSpindlePreprocfromdeepsleep.utilsimportsetup_logger_to_std# Set the logger to print to stdout. You could skip this and ignore logs.setup_logger_to_std()# Adapt this for your own .edf dataeeg_file='/path/to/eeg.edf'# preprocessing parameters. Below are defaults used as in the paperSPINDLE_PREPROCESSING_PARAMS={'name':'SpindlePreproc','target_srate':128,'spectrogram-stride':16,'time_interval':4,'num_neighbors':4,'EEG-filtering':{'lfreq':0.5,'hfreq':12},'EMG-filtering':{'lfreq':2,'hfreq':30}}print("Loading EEG/EMG...")all_signals,signal_header,header=pyedflib.highlevel.read_edf(eeg_file)print("Preprocessing data...")preprocessing=SpindlePreproc(SPINDLE_PREPROCESSING_PARAMS)data=preprocessing(all_signals,signal_header,np.array([0,1]),np.array([]),np.array([2]))Spindle CNN networkYou could import the pytorch CNN network for training:fromdeepsleep.modelsimportSpindleGraphmyGraph=SpindleGraph(input_dim=(3,24,160),nb_class=3,dropout_rate=0.5)Making prediction with SpindleThere are two ways of making predictions with our pretrained model, either by
importing the model, preprocessing routine etc. from the library, or simply
using the terminal and commandline:Predictions using command lineIn the terminal you could simply provide the path to your.edffile, a path
where you would like to save predictions.csvs, and the path to the directory
where the pre-trained weights are stored as demonstrated below:predict.py/path/to/my/AWESOME_EEG.edf--predictions/path/to/csv_folder/--weight_dir/path/to/weights/Two.csvfiles/path/to/csv_folder/AWESOME_EEG_ad_predictions_prob.csvand/path/to/csv_folder/AWESOME_EEG_predictions_without_artifacts.csvwill be
stored as a result.The directory/path/to/weights/should contain two provided weights namedcheckpoint_replicatePaperAD_1563189723.278988epoch3.pthandcheckpoint_replicatePaperSS_1563188754.9532504epoch10.pthPredictions by importing the libraryVery similar topredict.py, you could follow this example: (Make sure you set all the paths)frompathlibimportPathfromdeepsleep.dataimportSpindlePreprocfromdeepsleep.dataimportProdDatafromdeepsleep.modelsimportSpindlePredictModelfromdeepsleep.utilsimportsetup_logger_to_stdfromdeepsleep.utilsimportset_up_paths# Set parameters for preprocessingSPINDLE_PREPROCESSING_PARAMS={'name':'SpindlePreproc','target_srate':128,'spectrogram-stride':16,'time_interval':4,'num_neighbors':4,'EEG-filtering':{'lfreq':0.5,'hfreq':12},'EMG-filtering':{'lfreq':2,'hfreq':30}}# Set parameters for pytorch data loaderDATALOADER_PARAMS={'num_workers':4,'batch_size':100,'do_shuffle':False,'batch_prefetch':10,'hdf5':'','folds':['fold1']}# Set parameters to set up the Spindle modelMODEL_PARAMS={'name':'SpindlePredictModel','artefact_threshold':0.5,# The probability threshold more than which the sample is considered as a noise sample'weights':['checkpoint_replicatePaperAD_1563189723.278988epoch3.pth','checkpoint_replicatePaperSS_1563188754.9532504epoch10.pth']}# Set the logger to print to stdoutsetup_logger_to_std()# Set the required pathsroot_path=Path('/path/to/my/results_folder')data_path=Path('/path/to/eeg.pdf')# Set this to your input .edf fileweights_path=Path('/path/to/weights/')# Set this to the folder in which weights are locatedset_up_paths(root_path=root_path,data_path=data_path,weights_path=weights_path)# Load data and preprocesspreprocessing=SpindlePreproc(SPINDLE_PREPROCESSING_PARAMS)pred_handlers=ProdData(DATALOADER_PARAMS,preprocessing)# Make modelparams=dict({'model':MODEL_PARAMS})params['model']['predictions_path']=Path('/path/to/csv_folder/preds_')#  Set this to the folder in which .csv prediction files will be savedmodel=SpindlePredictModel(params)# Set inputs and predictmodel.set_inputs(prediction=[pred_handlers])model.prediction()