skjerns / AutoSleepScorer

An open-source sleep stage classification Python package
GNU Affero General Public License v3.0
103 stars 22 forks source link

Float edf #27

Open gsamyak30 opened 2 years ago

gsamyak30 commented 2 years ago

Hi Simon

I am just getting a error

TypeError: 'float' object does not support item assignment

when I am taking the dataset of ST7022JM-Hypnogram.edf or any other hypnogram.edf from physionet in sleep telemetry while converting into csv file format

How do I resolve it !!

skjerns commented 2 years ago

EDF is an odd format for storing hypnograms, no idea why physionet decided to do that. you need to convert the hypnogram from EDF to CSV.

try this function taken from here: https://github.com/skjerns/AutoSleepScorerDev/blob/master/edfx_database.py

import csv
from pyedflib import highlevel

def convert_hypnograms(hypnogram_edf_file):
    """
    This function is quite a hack to read the edf hypnogram as a byte array. 
    I found no working reader for the hypnogram edfs.
    """

    hypnogram = []
    annot = highlevel.read_edf_header(hypnogram_edf_file)['annotations']
    for bstart, blength, bstage in annot:
        length = int(blength.decode())
        stage = bstage.decode()
        if 'movement' in stage.lower(): stage='M'
        stage = [str(stage[-1])]*(length//30)
        hypnogram.extend(stage)

    csv_file = hypnogram_edf_file.replace('-Hypnogram','')[:-5] + '0-PSG.csv'
    with open(csv_file, "w") as f:
        writer = csv.writer(f, lineterminator='\r')
        writer.writerows(hypnogram)