Skip to content

Commit

Permalink
Try to improve unicode string handling for annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
holgern committed May 2, 2017
1 parent 54ae3ea commit ccfc0ad
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
5 changes: 3 additions & 2 deletions pyedflib/edfreader.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,13 @@ def readAnnotations(self):
return np.array([]), np.array([]), np.array([])
ann_time = self._get_float(annot[:, 0])
ann_text = annot[:, 2]
ann_text_out = ["" for x in range(len(annot[:, 1]))]
for i in np.arange(len(annot[:, 1])):
ann_text[i] = self._convert_string(ann_text[i])
ann_text_out[i] = self._convert_string(ann_text[i])
if annot[i, 1] == '':
annot[i, 1] = '-1'
ann_duration = self._get_float(annot[:, 1])
return ann_time/10000000, ann_duration, ann_text
return ann_time/10000000, ann_duration, np.array(ann_text_out)

def _get_float(self, v):
result = np.zeros(np.size(v))
Expand Down
2 changes: 1 addition & 1 deletion pyedflib/edfwriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def du(x):
return x
else:
def u(x):
return x
return x.decode("utf-8", "strict")

def du(x):
if isbytestr(x):
Expand Down
34 changes: 19 additions & 15 deletions pyedflib/tests/test_edfwriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,10 @@ def test_AnnotationWriting(self):
f.writePhysicalSamples(data)
f.writePhysicalSamples(data)
f.writePhysicalSamples(data)
f.writeAnnotation(1.23, 0.2, u"annotation1")
f.writeAnnotation(0.25, -1, u"annotation2")
f.writeAnnotation(1.25, 0, u"annotation3")
f.writeAnnotation(1.23, 0.2, u"annotation1_ä")
f.writeAnnotation(0.25, -1, u"annotation2_ü")
f.writeAnnotation(1.25, 0, u"annotation3_ö")
f.writeAnnotation(1.30, -1, u"annotation4_ß")
f.close()
del f

Expand All @@ -127,13 +128,16 @@ def test_AnnotationWriting(self):
del f
np.testing.assert_almost_equal(ann_time[0], 1.23)
np.testing.assert_almost_equal(ann_duration[0], 0.2)
np.testing.assert_equal(ann_text[0], b"annotation1")
np.testing.assert_equal(ann_text[0], "annotation1_..")
np.testing.assert_almost_equal(ann_time[1], 0.25)
np.testing.assert_almost_equal(ann_duration[1], -1)
np.testing.assert_equal(ann_text[1], b"annotation2")
np.testing.assert_equal(ann_text[1], "annotation2_..")
np.testing.assert_almost_equal(ann_time[2], 1.25)
np.testing.assert_almost_equal(ann_duration[2], 0)
np.testing.assert_equal(ann_text[2], b"annotation3")
np.testing.assert_equal(ann_text[2], "annotation3_..")
np.testing.assert_almost_equal(ann_time[3], 1.30)
np.testing.assert_almost_equal(ann_duration[3], -1)
np.testing.assert_equal(ann_text[3], "annotation4_..")

def test_AnnotationWritingUTF8(self):
channel_info = {'label': 'test_label', 'dimension': 'mV', 'sample_rate': 100,
Expand All @@ -160,13 +164,13 @@ def test_AnnotationWritingUTF8(self):
del f
np.testing.assert_almost_equal(ann_time[0], 1.23)
np.testing.assert_almost_equal(ann_duration[0], 0.2)
np.testing.assert_equal(ann_text[0], b"Z..hne")
np.testing.assert_equal(ann_text[0], "Z..hne")
np.testing.assert_almost_equal(ann_time[1], 0.25)
np.testing.assert_almost_equal(ann_duration[1], -1)
np.testing.assert_equal(ann_text[1], b"Fu..")
np.testing.assert_equal(ann_text[1], "Fu..")
np.testing.assert_almost_equal(ann_time[2], 1.25)
np.testing.assert_almost_equal(ann_duration[2], 0)
np.testing.assert_equal(ann_text[2], b"abc")
np.testing.assert_equal(ann_text[2], "abc")

def test_BytesChars(self):
channel_info = {'label': b'test_label', 'dimension': b'mV', 'sample_rate': 100,
Expand All @@ -181,9 +185,9 @@ def test_BytesChars(self):
f.writePhysicalSamples(data)
f.writePhysicalSamples(data)
f.writePhysicalSamples(data)
f.writeAnnotation(1.23, 0.2, u"Zähne")
f.writeAnnotation(0.25, -1, u"Fuß")
f.writeAnnotation(1.25, 0, u"abc")
f.writeAnnotation(1.23, 0.2, b'Zaehne')
f.writeAnnotation(0.25, -1, b'Fuss')
f.writeAnnotation(1.25, 0, b'abc')
f.close()
del f

Expand All @@ -193,13 +197,13 @@ def test_BytesChars(self):
del f
np.testing.assert_almost_equal(ann_time[0], 1.23)
np.testing.assert_almost_equal(ann_duration[0], 0.2)
np.testing.assert_equal(ann_text[0], b"Z..hne")
np.testing.assert_equal(ann_text[0], "Zaehne")
np.testing.assert_almost_equal(ann_time[1], 0.25)
np.testing.assert_almost_equal(ann_duration[1], -1)
np.testing.assert_equal(ann_text[1], b"Fu..")
np.testing.assert_equal(ann_text[1], "Fuss")
np.testing.assert_almost_equal(ann_time[2], 1.25)
np.testing.assert_almost_equal(ann_duration[2], 0)
np.testing.assert_equal(ann_text[2], b"abc")
np.testing.assert_equal(ann_text[2], "abc")


if __name__ == '__main__':
Expand Down

0 comments on commit ccfc0ad

Please sign in to comment.