From 4bd9e547c62cb207f6d9e421807811bc076bb527 Mon Sep 17 00:00:00 2001 From: Holger Nahrstaedt Date: Wed, 10 May 2017 10:32:48 +0200 Subject: [PATCH] EDF and BDF can now be written issue #14 fixed --- pyedflib/_extensions/c/edflib.c | 302 ++++++++++++++++++------------- pyedflib/_extensions/c/edflib.h | 1 + pyedflib/edfreader.py | 62 +++++-- pyedflib/edfwriter.py | 4 +- pyedflib/tests/test_edfwriter.py | 79 +++++++- 5 files changed, 291 insertions(+), 157 deletions(-) diff --git a/pyedflib/_extensions/c/edflib.c b/pyedflib/_extensions/c/edflib.c index e28e71bb..76a23917 100644 --- a/pyedflib/_extensions/c/edflib.c +++ b/pyedflib/_extensions/c/edflib.c @@ -1929,7 +1929,7 @@ static struct edfhdrblock * edflib_check_edf_file(FILE *inputfile, int *edf_erro } edfhdr->edfparam[i].phys_max = edflib_atof_nonlocalized(scratchpad); - if(edfhdr->edfparam[i].phys_max==edfhdr->edfparam[i].phys_min) + if(edfhdr->edfparam[i].phys_max==edfhdr->edfparam[i].phys_min && (edfhdr->bdfplus || edfhdr->edfplus)) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); @@ -2101,7 +2101,7 @@ static struct edfhdrblock * edflib_check_edf_file(FILE *inputfile, int *edf_erro } } edfhdr->edfparam[i].dig_max = n; - if(edfhdr->edfparam[i].dig_max<(edfhdr->edfparam[i].dig_min + 1)) + if(edfhdr->edfparam[i].dig_max<(edfhdr->edfparam[i].dig_min + 1) && (edfhdr->bdfplus || edfhdr->edfplus)) { *edf_error = EDFLIB_FILE_CONTAINS_FORMAT_ERRORS; free(edf_hdr); @@ -2614,9 +2614,16 @@ static struct edfhdrblock * edflib_check_edf_file(FILE *inputfile, int *edf_erro edfhdr->edfparam[i].buf_offset = n; if(edfhdr->bdf) n += edfhdr->edfparam[i].smp_per_record * 3; else n += edfhdr->edfparam[i].smp_per_record * 2; - - edfhdr->edfparam[i].bitvalue = (edfhdr->edfparam[i].phys_max - edfhdr->edfparam[i].phys_min) / (edfhdr->edfparam[i].dig_max - edfhdr->edfparam[i].dig_min); - edfhdr->edfparam[i].offset = edfhdr->edfparam[i].phys_max / edfhdr->edfparam[i].bitvalue - edfhdr->edfparam[i].dig_max; + if ((edfhdr->edfparam[i].dig_max == edfhdr->edfparam[i].dig_min) || (edfhdr->edfparam[i].phys_max == edfhdr->edfparam[i].phys_min)) + { + edfhdr->edfparam[i].bitvalue = 1; + edfhdr->edfparam[i].offset = 0; + } + else + { + edfhdr->edfparam[i].bitvalue = (edfhdr->edfparam[i].phys_max - edfhdr->edfparam[i].phys_min) / (edfhdr->edfparam[i].dig_max - edfhdr->edfparam[i].dig_min); + edfhdr->edfparam[i].offset = edfhdr->edfparam[i].phys_max / edfhdr->edfparam[i].bitvalue - edfhdr->edfparam[i].dig_max; + } } edfhdr->file_hdl = inputfile; @@ -3632,11 +3639,12 @@ int edfopen_file_writeonly(const char *path, int filetype, int number_of_signals struct edfhdrblock *hdr; - + /* if((filetype!=EDFLIB_FILETYPE_EDFPLUS)&&(filetype!=EDFLIB_FILETYPE_BDFPLUS)) { return(EDFLIB_FILETYPE_ERROR); } + */ if(edf_files_open>=EDFLIB_MAXFILES) { @@ -3731,19 +3739,35 @@ int edfopen_file_writeonly(const char *path, int filetype, int number_of_signals { hdr->edf = 1; hdr->edfplus = 1; + hdr->nr_annot_chns = 1; + } + + if(filetype==EDFLIB_FILETYPE_EDF) + { + hdr->edf = 1; + hdr->edfplus = 0; + hdr->nr_annot_chns = 0; } if(filetype==EDFLIB_FILETYPE_BDFPLUS) { hdr->bdf = 1; hdr->bdfplus = 1; + hdr->nr_annot_chns = 1; } + if(filetype==EDFLIB_FILETYPE_BDF) + { + hdr->bdf = 1; + hdr->bdfplus = 0; + hdr->nr_annot_chns = 0; + } + hdr->long_data_record_duration = EDFLIB_TIME_DIMENSION; hdr->data_record_duration = 1.0; - hdr->nr_annot_chns = 1; + return(handle); } @@ -3991,22 +4015,23 @@ int edfwrite_digital_short_samples(int handle, short *buf) if(hdr->signal_write_sequence_pos == hdr->edfsignals) { hdr->signal_write_sequence_pos = 0; - - p = edflib_fprint_ll_number_nonlocalized(file, (hdr->datarecords * hdr->long_data_record_duration) / EDFLIB_TIME_DIMENSION, 0, 1); - if(hdr->long_data_record_duration % EDFLIB_TIME_DIMENSION) - { - fputc('.', file); - p++; - p += edflib_fprint_ll_number_nonlocalized(file, (hdr->datarecords * hdr->long_data_record_duration) % EDFLIB_TIME_DIMENSION, 7, 0); - } - fputc(20, file); - fputc(20, file); - p += 2; - for(; ptotal_annot_bytes; p++) + if (hdr->bdfplus || hdr->edfplus) { - fputc(0, file); + p = edflib_fprint_ll_number_nonlocalized(file, (hdr->datarecords * hdr->long_data_record_duration) / EDFLIB_TIME_DIMENSION, 0, 1); + if(hdr->long_data_record_duration % EDFLIB_TIME_DIMENSION) + { + fputc('.', file); + p++; + p += edflib_fprint_ll_number_nonlocalized(file, (hdr->datarecords * hdr->long_data_record_duration) % EDFLIB_TIME_DIMENSION, 7, 0); + } + fputc(20, file); + fputc(20, file); + p += 2; + for(; ptotal_annot_bytes; p++) + { + fputc(0, file); + } } - hdr->datarecords++; fflush(file); @@ -4113,22 +4138,23 @@ int edfwrite_digital_samples(int handle, int *buf) if(hdr->signal_write_sequence_pos == hdr->edfsignals) { hdr->signal_write_sequence_pos = 0; - - p = edflib_fprint_ll_number_nonlocalized(file, (hdr->datarecords * hdr->long_data_record_duration) / EDFLIB_TIME_DIMENSION, 0, 1); - if(hdr->long_data_record_duration % EDFLIB_TIME_DIMENSION) + if (hdr->bdfplus || hdr->edfplus) { - fputc('.', file); - p++; - p += edflib_fprint_ll_number_nonlocalized(file, (hdr->datarecords * hdr->long_data_record_duration) % EDFLIB_TIME_DIMENSION, 7, 0); - } - fputc(20, file); - fputc(20, file); - p += 2; - for(; ptotal_annot_bytes; p++) - { - fputc(0, file); + p = edflib_fprint_ll_number_nonlocalized(file, (hdr->datarecords * hdr->long_data_record_duration) / EDFLIB_TIME_DIMENSION, 0, 1); + if(hdr->long_data_record_duration % EDFLIB_TIME_DIMENSION) + { + fputc('.', file); + p++; + p += edflib_fprint_ll_number_nonlocalized(file, (hdr->datarecords * hdr->long_data_record_duration) % EDFLIB_TIME_DIMENSION, 7, 0); + } + fputc(20, file); + fputc(20, file); + p += 2; + for(; ptotal_annot_bytes; p++) + { + fputc(0, file); + } } - hdr->datarecords++; fflush(file); @@ -4239,22 +4265,23 @@ int edf_blockwrite_digital_samples(int handle, int *buf) buf_offset += sf; } - - p = edflib_fprint_ll_number_nonlocalized(file, (hdr->datarecords * hdr->long_data_record_duration) / EDFLIB_TIME_DIMENSION, 0, 1); - if(hdr->long_data_record_duration % EDFLIB_TIME_DIMENSION) - { - fputc('.', file); - p++; - p += edflib_fprint_ll_number_nonlocalized(file, (hdr->datarecords * hdr->long_data_record_duration) % EDFLIB_TIME_DIMENSION, 7, 0); - } - fputc(20, file); - fputc(20, file); - p += 2; - for(; ptotal_annot_bytes; p++) + if (hdr->bdfplus || hdr->edfplus) { - fputc(0, file); + p = edflib_fprint_ll_number_nonlocalized(file, (hdr->datarecords * hdr->long_data_record_duration) / EDFLIB_TIME_DIMENSION, 0, 1); + if(hdr->long_data_record_duration % EDFLIB_TIME_DIMENSION) + { + fputc('.', file); + p++; + p += edflib_fprint_ll_number_nonlocalized(file, (hdr->datarecords * hdr->long_data_record_duration) % EDFLIB_TIME_DIMENSION, 7, 0); + } + fputc(20, file); + fputc(20, file); + p += 2; + for(; ptotal_annot_bytes; p++) + { + fputc(0, file); + } } - hdr->datarecords++; fflush(file); @@ -4369,22 +4396,23 @@ int edf_blockwrite_digital_short_samples(int handle, short *buf) buf_offset += sf; } - - p = edflib_fprint_ll_number_nonlocalized(file, (hdr->datarecords * hdr->long_data_record_duration) / EDFLIB_TIME_DIMENSION, 0, 1); - if(hdr->long_data_record_duration % EDFLIB_TIME_DIMENSION) + if (hdr->bdfplus || hdr->edfplus) { - fputc('.', file); - p++; - p += edflib_fprint_ll_number_nonlocalized(file, (hdr->datarecords * hdr->long_data_record_duration) % EDFLIB_TIME_DIMENSION, 7, 0); - } - fputc(20, file); - fputc(20, file); - p += 2; - for(; ptotal_annot_bytes; p++) - { - fputc(0, file); + p = edflib_fprint_ll_number_nonlocalized(file, (hdr->datarecords * hdr->long_data_record_duration) / EDFLIB_TIME_DIMENSION, 0, 1); + if(hdr->long_data_record_duration % EDFLIB_TIME_DIMENSION) + { + fputc('.', file); + p++; + p += edflib_fprint_ll_number_nonlocalized(file, (hdr->datarecords * hdr->long_data_record_duration) % EDFLIB_TIME_DIMENSION, 7, 0); + } + fputc(20, file); + fputc(20, file); + p += 2; + for(; ptotal_annot_bytes; p++) + { + fputc(0, file); + } } - hdr->datarecords++; fflush(file); @@ -4465,22 +4493,23 @@ int edf_blockwrite_digital_3byte_samples(int handle, void *buf) { return(-1); } - - p = edflib_fprint_ll_number_nonlocalized(file, (hdr->datarecords * hdr->long_data_record_duration) / EDFLIB_TIME_DIMENSION, 0, 1); - if(hdr->long_data_record_duration % EDFLIB_TIME_DIMENSION) - { - fputc('.', file); - p++; - p += edflib_fprint_ll_number_nonlocalized(file, (hdr->datarecords * hdr->long_data_record_duration) % EDFLIB_TIME_DIMENSION, 7, 0); - } - fputc(20, file); - fputc(20, file); - p += 2; - for(; ptotal_annot_bytes; p++) + if (hdr->bdfplus || hdr->edfplus) { - fputc(0, file); + p = edflib_fprint_ll_number_nonlocalized(file, (hdr->datarecords * hdr->long_data_record_duration) / EDFLIB_TIME_DIMENSION, 0, 1); + if(hdr->long_data_record_duration % EDFLIB_TIME_DIMENSION) + { + fputc('.', file); + p++; + p += edflib_fprint_ll_number_nonlocalized(file, (hdr->datarecords * hdr->long_data_record_duration) % EDFLIB_TIME_DIMENSION, 7, 0); + } + fputc(20, file); + fputc(20, file); + p += 2; + for(; ptotal_annot_bytes; p++) + { + fputc(0, file); + } } - hdr->datarecords++; fflush(file); @@ -4614,22 +4643,23 @@ int edfwrite_physical_samples(int handle, double *buf) if(hdr->signal_write_sequence_pos == hdr->edfsignals) { hdr->signal_write_sequence_pos = 0; - - p = edflib_fprint_ll_number_nonlocalized(file, (hdr->datarecords * hdr->long_data_record_duration) / EDFLIB_TIME_DIMENSION, 0, 1); - if(hdr->long_data_record_duration % EDFLIB_TIME_DIMENSION) + if (hdr->bdfplus || hdr->edfplus) { - fputc('.', file); - p++; - p += edflib_fprint_ll_number_nonlocalized(file, (hdr->datarecords * hdr->long_data_record_duration) % EDFLIB_TIME_DIMENSION, 7, 0); - } - fputc(20, file); - fputc(20, file); - p += 2; - for(; ptotal_annot_bytes; p++) - { - fputc(0, file); + p = edflib_fprint_ll_number_nonlocalized(file, (hdr->datarecords * hdr->long_data_record_duration) / EDFLIB_TIME_DIMENSION, 0, 1); + if(hdr->long_data_record_duration % EDFLIB_TIME_DIMENSION) + { + fputc('.', file); + p++; + p += edflib_fprint_ll_number_nonlocalized(file, (hdr->datarecords * hdr->long_data_record_duration) % EDFLIB_TIME_DIMENSION, 7, 0); + } + fputc(20, file); + fputc(20, file); + p += 2; + for(; ptotal_annot_bytes; p++) + { + fputc(0, file); + } } - hdr->datarecords++; fflush(file); @@ -4769,22 +4799,23 @@ int edf_blockwrite_physical_samples(int handle, double *buf) buf_offset += sf; } - - p = edflib_fprint_ll_number_nonlocalized(file, (hdr->datarecords * hdr->long_data_record_duration) / EDFLIB_TIME_DIMENSION, 0, 1); - if(hdr->long_data_record_duration % EDFLIB_TIME_DIMENSION) + if (hdr->bdfplus || hdr->edfplus) { - fputc('.', file); - p++; - p += edflib_fprint_ll_number_nonlocalized(file, (hdr->datarecords * hdr->long_data_record_duration) % EDFLIB_TIME_DIMENSION, 7, 0); - } - fputc(20, file); - fputc(20, file); - p += 2; - for(; ptotal_annot_bytes; p++) - { - fputc(0, file); + p = edflib_fprint_ll_number_nonlocalized(file, (hdr->datarecords * hdr->long_data_record_duration) / EDFLIB_TIME_DIMENSION, 0, 1); + if(hdr->long_data_record_duration % EDFLIB_TIME_DIMENSION) + { + fputc('.', file); + p++; + p += edflib_fprint_ll_number_nonlocalized(file, (hdr->datarecords * hdr->long_data_record_duration) % EDFLIB_TIME_DIMENSION, 7, 0); + } + fputc(20, file); + fputc(20, file); + p += 2; + for(; ptotal_annot_bytes; p++) + { + fputc(0, file); + } } - hdr->datarecords++; fflush(file); @@ -4825,28 +4856,31 @@ static int edflib_write_edf_header(struct edfhdrblock *hdr) hdr->eq_sf = 1; + for(i=0; iedfparam[i].smp_per_record<1) - { - return(-22); - } - - if(hdr->edfparam[i].dig_max==hdr->edfparam[i].dig_min) - { - return(-23); - } - - if(hdr->edfparam[i].dig_maxedfparam[i].dig_min) - { - return(-24); - } - - if(hdr->edfparam[i].phys_max==hdr->edfparam[i].phys_min) - { - return(-25); + if (hdr->edfplus || hdr->bdfplus) + { + if(hdr->edfparam[i].smp_per_record<1) + { + return(-22); + } + + if(hdr->edfparam[i].dig_max==hdr->edfparam[i].dig_min) + { + return(-23); + } + + if(hdr->edfparam[i].dig_maxedfparam[i].dig_min) + { + return(-24); + } + + if(hdr->edfparam[i].phys_max==hdr->edfparam[i].phys_min) + { + return(-25); + } } - if(i > 0) { if(hdr->edfparam[i].smp_per_record != hdr->edfparam[i-1].smp_per_record) @@ -4858,8 +4892,16 @@ static int edflib_write_edf_header(struct edfhdrblock *hdr) for(i=0; iedfparam[i].bitvalue = (hdr->edfparam[i].phys_max - hdr->edfparam[i].phys_min) / (hdr->edfparam[i].dig_max - hdr->edfparam[i].dig_min); - hdr->edfparam[i].offset = hdr->edfparam[i].phys_max / hdr->edfparam[i].bitvalue - hdr->edfparam[i].dig_max; + if ((hdr->edfparam[i].phys_max == hdr->edfparam[i].phys_min) || (hdr->edfparam[i].dig_max == hdr->edfparam[i].dig_min)) + { + hdr->edfparam[i].bitvalue = 1; + hdr->edfparam[i].offset = 0; + } + else + { + hdr->edfparam[i].bitvalue = (hdr->edfparam[i].phys_max - hdr->edfparam[i].phys_min) / (hdr->edfparam[i].dig_max - hdr->edfparam[i].dig_min); + hdr->edfparam[i].offset = hdr->edfparam[i].phys_max / hdr->edfparam[i].bitvalue - hdr->edfparam[i].dig_max; + } } rewind(file); @@ -5171,14 +5213,18 @@ static int edflib_write_edf_header(struct edfhdrblock *hdr) { fputc(' ', file); } - if(hdr->edf) + if(hdr->edfplus) { fprintf(file, "EDF+C"); } - else + else if(hdr->bdfplus) { fprintf(file, "BDF+C"); } + else + { + fprintf(file, " "); + } for(i=0; i<39; i++) { fputc(' ', file); diff --git a/pyedflib/_extensions/c/edflib.h b/pyedflib/_extensions/c/edflib.h index eadd6e2a..93737f9b 100644 --- a/pyedflib/_extensions/c/edflib.h +++ b/pyedflib/_extensions/c/edflib.h @@ -85,6 +85,7 @@ #define EDFLIB_REPAIR_FILE_SIZE_IF_WRONG 2 /* the following defines are possible errors returned by edfopen_file_writeonly() */ +/* Errors only for EDF+/BDF+*/ #define EDFLIB_NO_SIGNALS -20 #define EDFLIB_TOO_MANY_SIGNALS -21 #define EDFLIB_NO_SAMPLES_IN_RECORD -22 diff --git a/pyedflib/edfreader.py b/pyedflib/edfreader.py index 2e28551c..2d40e1ac 100644 --- a/pyedflib/edfreader.py +++ b/pyedflib/edfreader.py @@ -441,7 +441,7 @@ def getLabel(self,chn): if 0 <= chn < self.signals_in_file: return self._convert_string(self.signal_label(chn).rstrip()) else: - return self._convert_string(b'') + return self._convert_string('') def getPrefilter(self,chn): """ @@ -465,9 +465,9 @@ def getPrefilter(self,chn): if 0 <= chn < self.signals_in_file: return self._convert_string(self.prefilter(chn).rstrip()) else: - return self._convert_string(b'') + return self._convert_string('') - def getPhysicalMaximum(self,chn): + def getPhysicalMaximum(self,chn=None): """ Returns the maximum physical value of signal edfsignal. @@ -486,12 +486,18 @@ def getPhysicalMaximum(self,chn): >>> del f """ - if 0 <= chn < self.signals_in_file: - return self.physical_max(chn) + if chn is not None: + if 0 <= chn < self.signals_in_file: + return self.physical_max(chn) + else: + return 0 else: - return 0 + physMax = np.zeros(self.signals_in_file) + for i in np.arange(self.signals_in_file): + physMax[i] = self.physical_max(i) + return physMax - def getPhysicalMinimum(self,chn): + def getPhysicalMinimum(self,chn=None): """ Returns the minimum physical value of signal edfsignal. @@ -510,12 +516,18 @@ def getPhysicalMinimum(self,chn): >>> del f """ - if 0 <= chn < self.signals_in_file: - return self.physical_min(chn) + if chn is not None: + if 0 <= chn < self.signals_in_file: + return self.physical_min(chn) + else: + return 0 else: - return 0 + physMin = np.zeros(self.signals_in_file) + for i in np.arange(self.signals_in_file): + physMin[i] = self.physical_min(i) + return physMin - def getDigitalMaximum(self, chn): + def getDigitalMaximum(self, chn=None): """ Returns the maximum digital value of signal edfsignal. @@ -534,12 +546,18 @@ def getDigitalMaximum(self, chn): >>> del f """ - if 0 <= chn < self.signals_in_file: - return self.digital_max(chn) + if chn is not None: + if 0 <= chn < self.signals_in_file: + return self.digital_max(chn) + else: + return 0 else: - return 0 + digMax = np.zeros(self.signals_in_file) + for i in np.arange(self.signals_in_file): + digMax[i] = self.digital_max(i) + return digMax - def getDigitalMinimum(self, chn): + def getDigitalMinimum(self, chn=None): """ Returns the minimum digital value of signal edfsignal. @@ -558,10 +576,16 @@ def getDigitalMinimum(self, chn): >>> del f """ - if 0 <= chn < self.signals_in_file: - return self.digital_min(chn) + if chn is not None: + if 0 <= chn < self.signals_in_file: + return self.digital_min(chn) + else: + return 0 else: - return 0 + digMin = np.zeros(self.signals_in_file) + for i in np.arange(self.signals_in_file): + digMin[i] = self.digital_min(i) + return digMin def getTransducer(self, chn): """ @@ -609,7 +633,7 @@ def getPhysicalDimension(self, chn): if 0 <= chn < self.signals_in_file: return self._convert_string(self.physical_dimension(chn).rstrip()) else: - return self._convert_string(b'') + return self._convert_string('') def readSignal(self, chn): diff --git a/pyedflib/edfwriter.py b/pyedflib/edfwriter.py index 14a52f53..931f7e18 100644 --- a/pyedflib/edfwriter.py +++ b/pyedflib/edfwriter.py @@ -111,12 +111,12 @@ def __init__(self, file_name, n_channels, self.channels = [] self.sample_buffer = [] for i in np.arange(self.n_channels): - if self.file_type == FILETYPE_BDFPLUS: + if self.file_type == FILETYPE_BDFPLUS or self.file_type == FILETYPE_BDF: self.channels.append({'label': 'test_label', 'dimension': 'mV', 'sample_rate': 100, 'physical_max': 1.0, 'physical_min': -1.0, 'digital_max': 8388607,'digital_min': -8388608, 'prefilter': 'pre1', 'transducer': 'trans1'}) - elif (self.file_type == FILETYPE_EDFPLUS): + elif self.file_type == FILETYPE_EDFPLUS or self.file_type == FILETYPE_EDF: self.channels.append({'label': 'test_label', 'dimension': 'mV', 'sample_rate': 100, 'physical_max': 1.0, 'physical_min': -1.0, 'digital_max': 32767, 'digital_min': -32768, diff --git a/pyedflib/tests/test_edfwriter.py b/pyedflib/tests/test_edfwriter.py index ec4cc909..71afb510 100644 --- a/pyedflib/tests/test_edfwriter.py +++ b/pyedflib/tests/test_edfwriter.py @@ -13,15 +13,17 @@ class TestEdfWriter(unittest.TestCase): def setUp(self): data_dir = os.path.join(os.path.dirname(__file__), 'data') + self.bdfplus_data_file = os.path.join(data_dir, 'tmp_test_file_plus.bdf') + self.edfplus_data_file = os.path.join(data_dir, 'tmp_test_file_plus.edf') self.bdf_data_file = os.path.join(data_dir, 'tmp_test_file.bdf') self.edf_data_file = os.path.join(data_dir, 'tmp_test_file.edf') - def test_EdfWriter_EDF(self): + def test_EdfWriter_BDFplus(self): channel_info = {'label': 'test_label', 'dimension': 'mV', 'sample_rate': 100, 'physical_max': 1.0, 'physical_min': -1.0, 'digital_max': 8388607, 'digital_min': -8388608, 'prefilter': 'pre1', 'transducer': 'trans1'} - f = pyedflib.EdfWriter(self.bdf_data_file, 1, + f = pyedflib.EdfWriter(self.bdfplus_data_file, 1, file_type=pyedflib.FILETYPE_BDFPLUS) f.setSignalHeader(0,channel_info) f.setTechnician('tec1') @@ -31,7 +33,7 @@ def test_EdfWriter_EDF(self): f.close() del f - f = pyedflib.EdfReader(self.bdf_data_file) + f = pyedflib.EdfReader(self.bdfplus_data_file) np.testing.assert_equal(f.getTechnician(), 'tec1') np.testing.assert_equal(f.getLabel(0), 'test_label') @@ -42,12 +44,44 @@ def test_EdfWriter_EDF(self): f._close() del f - def test_EdfWriter_EDF(self): + def test_EdfWriter_BDF(self): + channel_info1 = {'label': 'test_label', 'dimension': 'mV', 'sample_rate': 100, + 'physical_max': 1.0, 'physical_min': -1.0, + 'digital_max': 8388607, 'digital_min': -8388608, + 'prefilter': 'pre1', 'transducer': 'trans1'} + channel_info2 = {'label': 'test_label', 'dimension': 'mV', 'sample_rate': 100, + 'physical_max': 1.0, 'physical_min': -1.0, + 'digital_max': 8388607, 'digital_min': -8388608, + 'prefilter': 'pre1', 'transducer': 'trans1'} + f = pyedflib.EdfWriter(self.bdf_data_file, 2, + file_type=pyedflib.FILETYPE_BDF) + f.setSignalHeader(0,channel_info1) + f.setSignalHeader(1,channel_info2) + + data = np.ones(100) * 0.1 + f.writePhysicalSamples(data) + f.writePhysicalSamples(data) + f.writePhysicalSamples(data) + f.writePhysicalSamples(data) + f.close() + del f + + f = pyedflib.EdfReader(self.bdf_data_file) + + np.testing.assert_equal(f.getLabel(0), 'test_label') + np.testing.assert_equal(f.getPhysicalDimension(0), 'mV') + np.testing.assert_equal(f.getPrefilter(0), 'pre1') + np.testing.assert_equal(f.getTransducer(0), 'trans1') + np.testing.assert_equal(f.getSampleFrequency(0), 100) + f._close() + del f + + def test_EdfWriter_EDFplus(self): channel_info = {'label': 'test_label', 'dimension': 'mV', 'sample_rate': 100, 'physical_max': 1.0, 'physical_min': -1.0, 'digital_max': 32767, 'digital_min': -32768, 'prefilter': 'pre1', 'transducer': 'trans1'} - f = pyedflib.EdfWriter(self.edf_data_file, 1, + f = pyedflib.EdfWriter(self.edfplus_data_file, 1, file_type=pyedflib.FILETYPE_EDFPLUS) f.setSignalHeader(0,channel_info) f.setTechnician('tec1') @@ -57,7 +91,7 @@ def test_EdfWriter_EDF(self): f.close() del f - f = pyedflib.EdfReader(self.edf_data_file) + f = pyedflib.EdfReader(self.edfplus_data_file) np.testing.assert_equal(f.getTechnician(), 'tec1') np.testing.assert_equal(f.getLabel(0), 'test_label') @@ -68,6 +102,35 @@ def test_EdfWriter_EDF(self): f._close() del f + def test_EdfWriter_EDF(self): + channel_info1 = {'label': 'test_label', 'dimension': 'mV', 'sample_rate': 100, + 'physical_max': 1.0, 'physical_min': -1.0, + 'digital_max': 32767, 'digital_min': -32768, + 'prefilter': 'pre1', 'transducer': 'trans1'} + channel_info2 = {'label': 'test_label', 'dimension': 'mV', 'sample_rate': 100, + 'physical_max': 1.0, 'physical_min': -1.0, + 'digital_max': 32767, 'digital_min': -32768, + 'prefilter': 'pre1', 'transducer': 'trans1'} + f = pyedflib.EdfWriter(self.edf_data_file, 2, + file_type=pyedflib.FILETYPE_EDF) + f.setSignalHeader(0,channel_info1) + f.setSignalHeader(1,channel_info2) + data = np.ones(100) * 0.1 + f.writePhysicalSamples(data) + f.writePhysicalSamples(data) + f.close() + del f + + f = pyedflib.EdfReader(self.edf_data_file) + + np.testing.assert_equal(f.getLabel(0), 'test_label') + np.testing.assert_equal(f.getPhysicalDimension(0), 'mV') + np.testing.assert_equal(f.getPrefilter(0), 'pre1') + np.testing.assert_equal(f.getTransducer(0), 'trans1') + np.testing.assert_equal(f.getSampleFrequency(0), 100) + f._close() + del f + def test_SampleWriting(self): channel_info1 = {'label':'test_label1', 'dimension':'mV', 'sample_rate':100, 'physical_max':1.0,'physical_min':-1.0, @@ -77,7 +140,7 @@ def test_SampleWriting(self): 'physical_max':1.0,'physical_min':-1.0, 'digital_max':8388607,'digital_min':-8388608, 'prefilter':'pre2','transducer':'trans2'} - f = pyedflib.EdfWriter(self.bdf_data_file, 2, + f = pyedflib.EdfWriter(self.bdfplus_data_file, 2, file_type=pyedflib.FILETYPE_BDFPLUS) f.setSignalHeader(0,channel_info1) f.setSignalHeader(1,channel_info2) @@ -92,7 +155,7 @@ def test_SampleWriting(self): f.close() del f - f = pyedflib.EdfReader(self.bdf_data_file) + f = pyedflib.EdfReader(self.bdfplus_data_file) data1_read = f.readSignal(0) data2_read = f.readSignal(1) f._close()