From bca8a2db6040ef1ecb0f1a9a16266dfc4dfe6ec5 Mon Sep 17 00:00:00 2001 From: Jarrett Revels Date: Thu, 27 Aug 2020 23:51:58 -0400 Subject: [PATCH] fix empty TAL handling (#37) * fix empty TAL handling * bump Project.toml --- Project.toml | 2 +- src/read.jl | 3 ++- src/write.jl | 11 ++++++++--- test/runtests.jl | 12 ++++++------ 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/Project.toml b/Project.toml index 09340e9..697fc32 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "EDF" uuid = "ccffbfc1-f56e-50fb-a33b-53d1781b2825" authors = ["Beacon Biosignals, Inc."] -version = "0.5.0" +version = "0.5.1" [deps] Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" diff --git a/src/read.jl b/src/read.jl index 1ebe7fa..3183798 100644 --- a/src/read.jl +++ b/src/read.jl @@ -155,7 +155,8 @@ function read_tal(io::IO) timestamp = split(String(bytes), '\x15'; keepempty=false) onset_in_seconds = flipsign(parse(Float64, timestamp[1]), sign) duration_in_seconds = length(timestamp) == 2 ? parse(Float64, timestamp[2]) : nothing - annotations = convert(Vector{String}, split(String(readuntil(io, 0x00)), '\x14'; keepempty=false)) + annotations = convert(Vector{String}, split(String(readuntil(io, 0x00)), '\x14'; keepempty=true)) + isempty(last(annotations)) && pop!(annotations) return TimestampedAnnotationList(onset_in_seconds, duration_in_seconds, annotations) end diff --git a/src/write.jl b/src/write.jl index 197cc24..469c54c 100644 --- a/src/write.jl +++ b/src/write.jl @@ -128,10 +128,15 @@ function write_tal(io::IO, tal::TimestampedAnnotationList) bytes_written += Base.write(io, 0x15) bytes_written += Base.write(io, _edf_repr(tal.duration_in_seconds)) end - bytes_written += Base.write(io, 0x14) - for annotation in tal.annotations - bytes_written += Base.write(io, annotation) + if isempty(tal.annotations) bytes_written += Base.write(io, 0x14) + bytes_written += Base.write(io, 0x14) + else + for annotation in tal.annotations + bytes_written += Base.write(io, 0x14) + bytes_written += Base.write(io, annotation) + bytes_written += Base.write(io, 0x14) + end end bytes_written += Base.write(io, 0x00) return bytes_written diff --git a/test/runtests.jl b/test/runtests.jl index 23f13a8..c6b2608 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -60,12 +60,12 @@ const DATADIR = joinpath(@__DIR__, "data") # according to the EDF+ specification, onsets should be relative to the start time of # the entire file, but it seems like whoever wrote these onsets might have used values # that were relative to the start of the surrounding data record - expected = [[TimestampedAnnotationList(0.0, nothing, String[]), TimestampedAnnotationList(0.0, nothing, ["start"])], - [TimestampedAnnotationList(1.0, nothing, String[]), TimestampedAnnotationList(0.1344, 0.256, ["type A"])], - [TimestampedAnnotationList(2.0, nothing, String[]), TimestampedAnnotationList(0.3904, 1.0, ["type A"])], - [TimestampedAnnotationList(3.0, nothing, String[]), TimestampedAnnotationList(2.0, nothing, ["type B"])], - [TimestampedAnnotationList(4.0, nothing, String[]), TimestampedAnnotationList(2.5, 2.5, ["type A"])], - [TimestampedAnnotationList(5.0, nothing, String[])]] + expected = [[TimestampedAnnotationList(0.0, nothing, String[""]), TimestampedAnnotationList(0.0, nothing, ["start"])], + [TimestampedAnnotationList(1.0, nothing, String[""]), TimestampedAnnotationList(0.1344, 0.256, ["type A"])], + [TimestampedAnnotationList(2.0, nothing, String[""]), TimestampedAnnotationList(0.3904, 1.0, ["type A"])], + [TimestampedAnnotationList(3.0, nothing, String[""]), TimestampedAnnotationList(2.0, nothing, ["type B"])], + [TimestampedAnnotationList(4.0, nothing, String[""]), TimestampedAnnotationList(2.5, 2.5, ["type A"])], + [TimestampedAnnotationList(5.0, nothing, String[""])]] @test all(signal.records .== expected) @test AnnotationsSignal(signal.records).samples_per_record == 16 end