def three_event_channel(): stim_amp, stim_grad, stim_env = np.zeros((1, raw.n_times)), np.zeros( (1, raw.n_times)), np.zeros((1, raw.n_times)) for evt in spikes_df.values: if evt[1] == 'amp': stim_amp[0][evt[4]] = 1 elif evt[1] == 'grad': stim_grad[0][evt[4]] = 1 else: stim_env[0][evt[4]] = 1 raw.pick_channels([selected_channel]) info_amp = mne.create_info(['AMP'], raw.info['sfreq'], ['stim']) info_grad = mne.create_info(['GRAD'], raw.info['sfreq'], ['stim']) info_env = mne.create_info(['ENV'], raw.info['sfreq'], ['stim']) amp_raw = mne.io.RawArray(stim_amp, info_amp) grad_raw = mne.io.RawArray(stim_grad, info_grad) env_raw = mne.io.RawArray(stim_env, info_env) raw.load_data() raw.add_channels([amp_raw, grad_raw, env_raw], force_update_info=True) raw.reorder_channels([selected_channel, 'AMP', 'GRAD', 'ENV']) if original_sf > 1000: raw.resample(500) sp = Sleep(data=raw._data, sf=raw.info['sfreq'], channels=raw.info['ch_names'], downsample=None) sp.replace_detections('peak', peak_index) sp.replace_detections('spindle', spikes_index) sp.show() print('finish')
############################################################################### # 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 :class:`visbrain.gui.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 : sp.show()
# Define the function to replace : def fcn_slowwave(data, sf, time, hypno): # noqa """New slowwave detection function. See : https://wonambi-python.github.io/api/wonambi.detect.slowwave.html for an exhaustive list of implemented detections inside wonambi. """ out = detect_Massimini2004(data, sf, time, opts_sw) indices = np.zeros((len(out), 2)) for i, k in enumerate(out): indices[i, 0] = k['start'] indices[i, 1] = k['end'] indices *= sf return indices.astype(int) ############################################################################### # Replace existing methods ############################################################################### # Now we use the :class:`visbrain.gui.Sleep.replace_detections` method to # overwrite existing spindles and slow-waves detections. # Replace the spindle detection function : sp.replace_detections('spindle', fcn_spindle) # Replace the slow-wave detection function : sp.replace_detections('sw', fcn_slowwave) # Finally, open the graphical user interface : sp.show()
# Define spindles function def fcn_spindle(data, sf, time, hypno): """Replace Visbrain built-in spindles detection by YASA algorithm. See http://visbrain.org/sleep.html#use-your-own-detections-in-sleep """ # Apply on the full recording # sp = spindles_detect(data, sf) # NREM sleep only sp = spindles_detect(data, sf, hypno=hypno) return (sp[['Start', 'End']].values * sf).astype(int) # Define slow-waves function def fcn_sw(data, sf, time, hypno): """Replace Visbrain built-in slow-wave detection by YASA algorithm. """ # On N2 / N3 sleep only # Note that if you want to apply the detection on N3 sleep only, you should # use sw_detect(..., include=(3)) sw = sw_detect(data, sf, hypno=hypno) return (sw[['Start', 'End']].values * sf).astype(int) # Replace the native Visbrain detections sl.replace_detections('spindle', fcn_spindle) sl.replace_detections('sw', fcn_sw) # Launch the Graphical User Interface sl.show()
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~ ############################################################################### # 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 :class:`visbrain.gui.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 : sp.show()
raw.pick_channels(['LAH1']) info_amp = mne.create_info(['AMP'], raw.info['sfreq'], ['stim']) info_grad = mne.create_info(['GRAD'], raw.info['sfreq'], ['stim']) info_env = mne.create_info(['ENV'], raw.info['sfreq'], ['stim']) amp_raw = mne.io.RawArray(stim_amp, info_amp) grad_raw = mne.io.RawArray(stim_grad, info_grad) env_raw = mne.io.RawArray(stim_env, info_env) raw.load_data() raw.add_channels([amp_raw, grad_raw, env_raw], force_update_info=True) # raw.reorder_channels([selected_channel, 'AMP', 'GRAD', 'ENV']) # if original_sf > 1000: # raw.resample(500) sp = Sleep(data=raw._data, sf=raw.info['sfreq'], channels=raw.info['ch_names'], downsample=None) sp.replace_detections('peak', peak_index) # sp.replace_detections('spindle', spikes_index) sp.show() print('finish') # edf = 'C:\\Lilach\\402_for_tag.edf' # # raw = mne.io.read_raw_edf(edf) # # data, sf, chan = raw.get_data(), raw.info['sfreq'], raw.info['ch_names'] # # Sleep(data=data, sf=sf, channels=chan, annotations=raw.annotations).show() # # print(1)