Skip to content

Commit

Permalink
handle edge case in reencode_samples (#88)
Browse files Browse the repository at this point in the history
* make more robust

* restore formatting

* better fix

* Update test/export.jl

Co-authored-by: Dave Kleinschmidt <dave.f.kleinschmidt@gmail.com>

* Update src/export_edf.jl

Co-authored-by: Dave Kleinschmidt <dave.f.kleinschmidt@gmail.com>

* Update test/export.jl

Co-authored-by: Dave Kleinschmidt <dave.f.kleinschmidt@gmail.com>

* Update src/export_edf.jl

---------

Co-authored-by: Dave Kleinschmidt <dave.f.kleinschmidt@gmail.com>
  • Loading branch information
ericphanson and kleinschmidt committed Oct 16, 2023
1 parent 2e27a1f commit e657170
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "OndaEDF"
uuid = "e3ed2cd1-99bf-415e-bb8f-38f4b42a544e"
authors = ["Beacon Biosignals, Inc."]
version = "0.12.2"
version = "0.12.3"

[deps]
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
Expand Down
4 changes: 4 additions & 0 deletions src/export_edf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ function reencode_samples(samples::Samples, sample_type::Type{<:Integer}=Int16)
# maximize the dynamic range of Int16 encoding.
samples = decode(samples)
smin, smax = extrema(samples.data)
# If the input is flat, normalize max to min + 1
if smin == smax
smax = smin + one(smax)
end

This comment has been minimized.

Copy link
@ericphanson

ericphanson Oct 17, 2023

Author Member

Thinking about this a bit more, I wonder if it should be

if smax - smin <= 1
    smin = smin - one(smin) / 2
    smax = smin + one(smax) / 2
end

e.g. if there is a tiny dynamic range, expand it to be at least 1 (and might as well do it symmetrically).

To be a bit more "robust" and avoid potential EDF encoding issues that I could image come with a super small dynamic range.


emin, emax = typemin(sample_type), typemax(sample_type)

Expand Down
17 changes: 17 additions & 0 deletions test/export.jl
Original file line number Diff line number Diff line change
Expand Up @@ -248,4 +248,21 @@
end
end

@testset "`reencode_samples` edge case: constant data" begin
# Weird encoding
info = SamplesInfoV2(; sensor_type="x",
channels=["x"],
sample_unit="microvolt",
sample_resolution_in_unit=0.001,
sample_offset_in_unit=0,
sample_type=Float64,
sample_rate=1)

data = zeros(UInt64, 1, 2) .+ 0x02
samples = Samples(data, info, false)
samples_reenc = OndaEDF.reencode_samples(samples)
@test samples_reenc isa Samples
@test decode(samples_reenc).data == data
end

end

2 comments on commit e657170

@ericphanson
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/93522

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.12.3 -m "<description of version>" e657170fddfefe4cfd8e65be31a52d8b4983324a
git push origin v0.12.3

Please sign in to comment.