def _read_eeg(self, path_eeg_file, channel_name): """Loads signal from 'path_eeg_file', does filtering and resampling.""" proper_name_dict = { KEY_EEG_FRONTAL: 'EEG F3-CLE', # Frontal KEY_EEG_CENTRAL: 'EEG C3-CLE', # Central KEY_EEG_OCCIPITAL: 'EEG O1-CLE', # Occipital KEY_EMG: 'EMG Chin', # EMG KEY_EOG_LEFT: 'EOG Left Horiz', # Left eye KEY_EOG_RIGHT: 'EOG Right Horiz', # Right eye } checks.check_valid_value(channel_name, 'channel_name', list(proper_name_dict.keys())) with pyedflib.EdfReader(path_eeg_file) as file: channel_names = file.getSignalLabels() channel_to_extract = channel_names.index( proper_name_dict[channel_name]) signal = file.readSignal(channel_to_extract) fs_old = file.samplefrequency(channel_to_extract) # Check print('Channel extracted: %s' % file.getLabel(channel_to_extract)) fs_old_round = int(np.round(fs_old)) # Transform the original fs frequency with decimals to rounded version signal = utils.resample_signal_linear(signal, fs_old=fs_old, fs_new=fs_old_round) signal = signal.astype(np.float32) return signal
def _read_eeg(self, path_eeg_file, channel_name): """Loads signal from 'path_eeg_file', does filtering and resampling.""" proper_name_dict = { KEY_EEG_FP1: 'EEG Fp1-CLE', # Frontal KEY_EEG_FP2: 'EEG Fp2-CLE', # Central KEY_ECG: 'ECG I', # ECG } checks.check_valid_value(channel_name, 'channel_name', list(proper_name_dict.keys())) with pyedflib.EdfReader(path_eeg_file) as file: channel_names = file.getSignalLabels() channel_to_extract = channel_names.index( proper_name_dict[channel_name]) signal = file.readSignal(channel_to_extract) fs_old = file.samplefrequency(channel_to_extract) # Check print('Channel extracted: %s' % file.getLabel(channel_to_extract)) fs_old_round = int(np.round(fs_old)) # Transform the original fs frequency with decimals to rounded version signal = utils.resample_signal_linear(signal, fs_old=fs_old, fs_new=fs_old_round) signal = signal.astype(np.float32) return signal
def get_subject_hypnogram(self, subject_id, verbose=False): """Returns the hypogram of this subject.""" checks.check_valid_value(subject_id, 'ID', self.all_ids) ind_dict = self.data[subject_id] hypno = ind_dict[KEY_HYPNOGRAM] if verbose: print('Getting Hypnogram of ID %s' % subject_id) return hypno
def get_subject_signal(self, subject_id, verbose=False): checks.check_valid_value(subject_id, 'ID', self.all_ids) ind_dict = self.data[subject_id] signal = np.stack( [ind_dict[KEY_EEG_FP1], ind_dict[KEY_EEG_FP2], ind_dict[KEY_ECG]], axis=1) if verbose: print('Getting signal of ID %s' % subject_id) return signal
def get_subject_data(self, subject_id, output_fs=100, border_duration=0, ignore_unknown=True, verbose=False): """ Returns segments of the signals for the given id and their corresponding sleep stage. """ checks.check_valid_value(subject_id, 'ID', self.all_ids) if output_fs > self.fs: raise ValueError('output fs cannot be greater than original fs') signal = self.get_subject_signal(subject_id) hypnogram = self.get_subject_hypnogram(subject_id) # Resample if needed if output_fs < self.fs: if verbose: print('Resampling from %d Hz to %d Hz' % (self.fs, output_fs)) resampled_list = [] for chn in range(signal.shape[1]): this_signal = signal[:, chn] this_signal = utils.resample_signal(this_signal, self.fs, output_fs) resampled_list.append(this_signal) signal = np.stack(resampled_list, axis=1) # Extract segments segments_list = [] stages_list = [] page_size = int(self.page_duration * output_fs) border_size = int(border_duration * output_fs) for i, stage in enumerate(hypnogram): if i == 0: if verbose: "Skipping first segment." elif stage == "?" and ignore_unknown: if verbose: "Dropped unknown stage '?'." else: sample_start = i * page_size - border_size sample_end = (i + 1) * page_size + border_size this_segment = signal[sample_start:sample_end, :] segments_list.append(this_segment) stages_list.append(stage) x = np.stack(segments_list, axis=0) y = np.stack(stages_list, axis=0) if verbose: print('S%02d with %d segments' % (subject_id, x.shape[0])) return x, y
def get_subject_signal(self, subject_id, verbose=False): checks.check_valid_value(subject_id, 'ID', self.all_ids) ind_dict = self.data[subject_id] signal = np.stack([ ind_dict[KEY_EEG_FRONTAL], ind_dict[KEY_EEG_CENTRAL], ind_dict[KEY_EEG_OCCIPITAL], ind_dict[KEY_EOG_LEFT], ind_dict[KEY_EOG_RIGHT], ind_dict[KEY_EMG] ], axis=1) if verbose: print('Getting signal of ID %s' % subject_id) return signal