Skip to content

Commit

Permalink
fix empty TAL handling (#37)
Browse files Browse the repository at this point in the history
* fix empty TAL handling

* bump Project.toml
  • Loading branch information
jrevels authored Aug 28, 2020
1 parent f315c72 commit bca8a2d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
3 changes: 2 additions & 1 deletion src/read.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 8 additions & 3 deletions src/write.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

2 comments on commit bca8a2d

@jrevels
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/20425

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.5.1 -m "<description of version>" bca8a2db6040ef1ecb0f1a9a16266dfc4dfe6ec5
git push origin v0.5.1

Please sign in to comment.