Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initial work on supporting discontiguous data #78

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/EDF.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ using BitIntegers, Dates, Printf

include("types.jl")
include("read.jl")
include("discontiguous.jl")
include("write.jl")

end # module
86 changes: 86 additions & 0 deletions src/discontiguous.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
function read_discontiguous!(file::File)
file.header.is_contiguous && throw(ArgumentError("File is continguous "))
isopen(file.io) && !eof(file.io) && read_discontiguous_signals!(file)
return file

Check warning on line 4 in src/discontiguous.jl

View check run for this annotation

Codecov / codecov/patch

src/discontiguous.jl#L1-L4

Added lines #L1 - L4 were not covered by tests
end

"""
EDF.read_discontiguous(io::IO)

Return `EDF.read_discontiguous(EDF.File(io))`.

See also: [`EDF.File`](@ref), [`EDF.read_discontiguous`](@ref)
"""
read_discontiguous(io::IO) = read_discontiguous!(File(io))

Check warning on line 14 in src/discontiguous.jl

View check run for this annotation

Codecov / codecov/patch

src/discontiguous.jl#L14

Added line #L14 was not covered by tests

"""
EDF.read_discontiguous(path)

Return `open(EDF.read_discontiguous, path)`.
"""
read_discontiguous(path) = open(read_discontiguous, path)

Check warning on line 21 in src/discontiguous.jl

View check run for this annotation

Codecov / codecov/patch

src/discontiguous.jl#L21

Added line #L21 was not covered by tests

function read_discontiguous_signals!(file::File)

Check warning on line 23 in src/discontiguous.jl

View check run for this annotation

Codecov / codecov/patch

src/discontiguous.jl#L23

Added line #L23 was not covered by tests
# XXX need to have this be slightly more constrained
time_idx = findfirst(x -> isa(x, EDF.AnnotationsSignal), file.signals)
time_anns = file.signals[time_idx]

Check warning on line 26 in src/discontiguous.jl

View check run for this annotation

Codecov / codecov/patch

src/discontiguous.jl#L25-L26

Added lines #L25 - L26 were not covered by tests
# annotations have their own timestamps
signals = filter(x -> isa(x, Signal), file.signals)

Check warning on line 28 in src/discontiguous.jl

View check run for this annotation

Codecov / codecov/patch

src/discontiguous.jl#L28

Added line #L28 was not covered by tests
# @info "" time_anns
# record_stop = 1
# for record_index in 1:(file.header.record_count), (signal_idx, signal) in enumerate(file.signals)
# if signal_idx != time_idx
# record_stop += record_index * signal.header.samples_per_record - 1
# continue
# end
# seek(file.io, record_stop)
# read_signal_record!(file, signal, record_index)
# break
# end
# # not quite right because of headers
# seekstart(file.io)
EDF.read!(file)

Check warning on line 42 in src/discontiguous.jl

View check run for this annotation

Codecov / codecov/patch

src/discontiguous.jl#L42

Added line #L42 was not covered by tests
# gonna read and then resize in memory and then copy around
# not the most efficient route, but c'est la vie
# specific logic for discontinuous signals
final_record_start = only(last(time_anns.records)).onset_in_seconds
final_record_start = Int(final_record_start)
for signal in signals
sr = sampling_rate(file, signal)
total_length = final_record_start * sr + signal.header.samples_per_record
prev_length = length(signal.samples)
resize!(signal.samples, total_length)

Check warning on line 52 in src/discontiguous.jl

View check run for this annotation

Codecov / codecov/patch

src/discontiguous.jl#L46-L52

Added lines #L46 - L52 were not covered by tests
# fill all the new stuff with 0s
samples = signal.samples
fill!(@view(samples[prev_length:end]), 0)
end

Check warning on line 56 in src/discontiguous.jl

View check run for this annotation

Codecov / codecov/patch

src/discontiguous.jl#L54-L56

Added lines #L54 - L56 were not covered by tests


palday marked this conversation as resolved.
Show resolved Hide resolved
spr = file.header.seconds_per_record
count = 0

Check warning on line 60 in src/discontiguous.jl

View check run for this annotation

Codecov / codecov/patch

src/discontiguous.jl#L59-L60

Added lines #L59 - L60 were not covered by tests

for signal in signals
prev_start = only(first(time_anns.records)).onset_in_seconds
for tal in @view(time_anns.records[2:end])
start = only(tal).onset_in_seconds
if start - spr == prev_start
prev_start = start
continue

Check warning on line 68 in src/discontiguous.jl

View check run for this annotation

Codecov / codecov/patch

src/discontiguous.jl#L62-L68

Added lines #L62 - L68 were not covered by tests
else
prev_start = start

Check warning on line 70 in src/discontiguous.jl

View check run for this annotation

Codecov / codecov/patch

src/discontiguous.jl#L70

Added line #L70 was not covered by tests
# TODO copy around and fill source areas with 0s
palday marked this conversation as resolved.
Show resolved Hide resolved

count += 1

Check warning on line 73 in src/discontiguous.jl

View check run for this annotation

Codecov / codecov/patch

src/discontiguous.jl#L73

Added line #L73 was not covered by tests
end
end
end

Check warning on line 76 in src/discontiguous.jl

View check run for this annotation

Codecov / codecov/patch

src/discontiguous.jl#L75-L76

Added lines #L75 - L76 were not covered by tests

count % length(signals) == 0 ||

Check warning on line 78 in src/discontiguous.jl

View check run for this annotation

Codecov / codecov/patch

src/discontiguous.jl#L78

Added line #L78 was not covered by tests
error("Found an unexpected number of discontinuities")

return file

Check warning on line 81 in src/discontiguous.jl

View check run for this annotation

Codecov / codecov/patch

src/discontiguous.jl#L81

Added line #L81 was not covered by tests
end

function sampling_rate(file::File, signal::Signal)
return Int(signal.header.samples_per_record / file.header.seconds_per_record)

Check warning on line 85 in src/discontiguous.jl

View check run for this annotation

Codecov / codecov/patch

src/discontiguous.jl#L84-L85

Added lines #L84 - L85 were not covered by tests
end
7 changes: 7 additions & 0 deletions test/data/generate_discontinuous.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using Luna_jll

luna() do exec
run(pipeline(`$exec test.edf -s WRITE edf-tag=vanilla force-edf`))
run(pipeline(`$exec test-vanilla.edf starttime=10.19.44 -s 'SET-HEADERS start-time=10.19.44 & WRITE edf-tag=2'`))
return run(pipeline(`$exec --merge test-vanilla.edf test-vanilla-2.edf edf=test_merged.edf `))
end
3,735 changes: 3,735 additions & 0 deletions test/data/test_merged.edf

Large diffs are not rendered by default.

Loading