Replace detection algorithm : basic example

This example illustrates how to replace the default detection algorithm.


After running this script, just go to the Detection panel and run the selected detection by clicking on Apply. The software will automatically use your new detection algorithm. The Python console should confirm that you’re using the new method and not the default method.

Required dataset at :


Load your file and create an instance of Sleep

import os
import numpy as np

from visbrain import Sleep
from import download_file, path_to_visbrain_data

# Get data path and where to save it :
current_path = path_to_visbrain_data()
target_path = os.path.join(current_path, 'sleep_data', 'edf')

# Download the file :
download_file('', unzip=True, to_path=target_path)

# Get data path :
dfile = os.path.join(target_path, 'excerpt2.edf')            # data
hfile = os.path.join(target_path, 'Hypnogram_excerpt2.txt')  # hypnogram
cfile = os.path.join(target_path, 'excerpt2_config.txt')     # GUI config

# Define an instance of Sleep :
sp = Sleep(data=dfile, hypno=hfile, config_file=cfile)

Define new methods

Spindle function

This function does NOT perform a real spindle detection. It’s purpose is to show how to replace the default detection behavior by a custom function. It just highlights samples between [0, 100], [200, 300] and [400, 500].

def fcn_spindle(data, sf, time, hypno):  # noqa
    """New spindle detection function."""
    indices = np.array([[0, 100],
                       [200, 300],
                       [400, 500]])
    return indices

Rapid eye movement function

This function does NOT perform a real REM detection. It illustrates how to replace the default detection behavior by a basic thresholding function. Note that the function returns a boolean array indicating samples that are above a specific threshold.

def fcn_rem(data, sf, time, hypno):  # noqa
    """New REM detection function."""
    mean_data = np.mean(data)
    std_data = np.std(data)
    # Threshold is mean + 3 * STD
    return data > mean_data + 3. * std_data

Replace existing methods

Now we use the visbrain.Sleep.replace_detections method to overwrite existing spindles and REM detections.
# Replace the spindle detection function :
sp.replace_detections('spindle', fcn_spindle)
# Replace the REM detection function :
sp.replace_detections('rem', fcn_rem)

# Finally, open the graphical user interface :

Total running time of the script: ( 0 minutes 0.000 seconds)

Gallery generated by Sphinx-Gallery