libs/data/mass.py
def _read_states(self, path_states_file, signal_length):
        """Loads hypnogram from 'path_states_file'. Only n2 pages are returned.
        First, last and second to last pages of the hypnogram are ignored, since
        there is no enough context."""
        # Total pages not necessarily equal to total_annots
        page_size = self.fs * self.page_duration
        total_pages = int(np.ceil(signal_length / page_size)) - 1

        with pyedflib.EdfReader(path_states_file) as file:
            annotations = file.readAnnotations()

        onsets = np.array(annotations[0])
        durations = np.round(np.array(annotations[1]))
        stages_str = annotations[2]
        # keep only 20s durations
        valid_idx = (durations == self.page_duration)
        onsets = onsets[valid_idx]
        onsets_pages = np.round(onsets / self.page_duration).astype(np.int32)
        stages_str = stages_str[valid_idx]
        stages_char = [single_annot[-1] for single_annot in stages_str]

        # Build complete hypnogram
        total_annots = len(stages_char)

        state_ids = np.array(['1', '2', '3', '4', 'R', 'W', '?'])
        correct_id_dict = {
            '1': 'N1',
            '2': 'N2',
            '3': 'N3',
            '4': 'N3',
            'R': 'R',
            'W': 'W',
            '?': '?'
        }
        unknown_id = '?'  # Character for unknown state in hypnogram

        not_unknown_ids = [
            state_id for state_id in state_ids
            if state_id != unknown_id]
        not_unknown_state_dict = {}
        for state_id in not_unknown_ids:
            state_idx = np.where(
                [stages_char[i] == state_id for i in range(total_annots)])[0]
            not_unknown_state_dict[state_id] = onsets_pages[state_idx]

        hypnogram = []
        for page in range(total_pages):
            state_not_found = True
            for state_id in not_unknown_ids:
                if page in not_unknown_state_dict[state_id] and state_not_found:
                    hypnogram.append(correct_id_dict[state_id])
                    state_not_found = False
            if state_not_found:
                hypnogram.append(unknown_id)
        hypnogram = np.asarray(hypnogram)

        return hypnogram
Loads hypnogram from 'path_states_file'. Only n2 pages are returned.
        First, last and second to last pages of the hypnogram are ignored, since
        there is no enough context.
Loads hypnogram from 'path_states_file'. Only n2 pages are returned.
First, last and second to last pages of the hypnogram are ignored, since
there is no enough context.
Loads hypnogram from ' path_states_file ' . Only n2 pages are returned . First last and second to last pages of the hypnogram are ignored since there is no enough context .
def _read_states(self, path_states_file, signal_length):
        page_size = self.fs * self.page_duration
        total_pages = int(np.ceil(signal_length / page_size)) - 1
        with pyedflib.EdfReader(path_states_file) as file:
            annotations = file.readAnnotations()
        onsets = np.array(annotations[0])
        durations = np.round(np.array(annotations[1]))
        stages_str = annotations[2]
        valid_idx = (durations == self.page_duration)
        onsets = onsets[valid_idx]
        onsets_pages = np.round(onsets / self.page_duration).astype(np.int32)
        stages_str = stages_str[valid_idx]
        stages_char = [single_annot[-1] for single_annot in stages_str]
        total_annots = len(stages_char)
        state_ids = np.array(['1', '2', '3', '4', 'R', 'W', '?'])
        correct_id_dict = {
            '1': 'N1',
            '2': 'N2',
            '3': 'N3',
            '4': 'N3',
            'R': 'R',
            'W': 'W',
            '?': '?'
        }
        unknown_id = '?'  
        not_unknown_ids = [
            state_id for state_id in state_ids
            if state_id != unknown_id]
        not_unknown_state_dict = {}
        for state_id in not_unknown_ids:
            state_idx = np.where(
                [stages_char[i] == state_id for i in range(total_annots)])[0]
            not_unknown_state_dict[state_id] = onsets_pages[state_idx]
        hypnogram = []
        for page in range(total_pages):
            state_not_found = True
            for state_id in not_unknown_ids:
                if page in not_unknown_state_dict[state_id] and state_not_found:
                    hypnogram.append(correct_id_dict[state_id])
                    state_not_found = False
            if state_not_found:
                hypnogram.append(unknown_id)
        hypnogram = np.asarray(hypnogram)
        return hypnogram
def _read_states ( self , path_states_file , signal_length ) : page_size = self . fs * self . page_duration total_pages = int ( np . ceil ( signal_length / page_size ) ) - 1 with pyedflib . EdfReader ( path_states_file ) as file : annotations = file . readAnnotations ( ) onsets = np . array ( annotations [ 0 ] ) durations = np . round ( np . array ( annotations [ 1 ] ) ) stages_str = annotations [ 2 ] valid_idx = ( durations == self . page_duration ) onsets = onsets [ valid_idx ] onsets_pages = np . round ( onsets / self . page_duration ) . astype ( np . int32 ) stages_str = stages_str [ valid_idx ] stages_char = [ single_annot [ - 1 ] for single_annot in stages_str ] total_annots = len ( stages_char ) state_ids = np . array ( [ '1' , '2' , '3' , '4' , 'R' , 'W' , '?' ] ) correct_id_dict = { '1' : 'N1' , '2' : 'N2' , '3' : 'N3' , '4' : 'N3' , 'R' : 'R' , 'W' : 'W' , '?' : '?' } unknown_id = '?' not_unknown_ids = [ state_id for state_id in state_ids if state_id != unknown_id ] not_unknown_state_dict = { } for state_id in not_unknown_ids : state_idx = np . where ( [ stages_char [ i ] == state_id for i in range ( total_annots ) ] ) [ 0 ] not_unknown_state_dict [ state_id ] = onsets_pages [ state_idx ] hypnogram = [ ] for page in range ( total_pages ) : state_not_found = True for state_id in not_unknown_ids : if page in not_unknown_state_dict [ state_id ] and state_not_found : hypnogram . append ( correct_id_dict [ state_id ] ) state_not_found = False if state_not_found : hypnogram . append ( unknown_id ) hypnogram = np . asarray ( hypnogram ) return hypnogram
Loads hypnogram from 'path_states_file'.
Loads hypnogram from ' path_states_file ' .
"""Loads hypnogram from 'path_states_file'. Only n2 pages are returned.
        First, last and second to last pages of the hypnogram are ignored, since
        there is no enough context.""" # Total pages not necessarily equal to total_annots # keep only 20s durations # Build complete hypnogram # Character for unknown state in hypnogram
self
path_states_file
signal_length