-
Notifications
You must be signed in to change notification settings - Fork 5
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
avoid decoding sample data as Ints #99
Comments
In what situation does this arise? if you want the decoded representation to be It's generally good style in Julia to avoid avoidable promotion/widening, and instead respect the callers' choice of input types as much as possible. To do otherwise is to leak an implementation detail and can be far more annoying to workaround in practice (e.g. the classic CuArrays <-> Float32 autoconversion choice that resulted in lots of developer pain). |
This can arise because of https://github.com/beacon-biosignals/Onda.jl/blob/master/src/samples.jl#L352 Maybe a good fix is to make that line convert decoded data to |
Ah. Yeah, I think it'd be okay to have the result in that case be promoted
ehhhh it should converted to whatever the promotion should be based on the arguments, not to (and thus the aliasing optimization is still preserved in the case where the types already match) |
Ok, I was wondering which of using BenchmarkTools
x = rand(Int16, 200 * 3600 * 8) # 6-channel 8 hour recording at 200Hz
@btime $x .* 1.0 .+ 0.0
@btime convert.(Float64, sample_data) both give ~180ms and 2 allocations over 263.67Mb. Same times and allocations for x of eltype Float32. So maybe best is to just special-case |
yeah the patch I'd apply is this: diff --git a/src/samples.jl b/src/samples.jl
index 4c806eb..a91cf0f 100644
--- a/src/samples.jl
+++ b/src/samples.jl
@@ -343,13 +343,17 @@ If:
sample_data isa AbstractArray &&
sample_resolution_in_unit == 1 &&
- sample_offset_in_unit == 0
+ sample_offset_in_unit == 0 &&
+ promote_type(typeof(sample_resolution_in_unit), typeof(sample_offset_in_unit)) == eltype(sample_data)
then this function is the identity and will return `sample_data` directly without copying.
"""
function decode(sample_resolution_in_unit, sample_offset_in_unit, sample_data)
- if sample_data isa AbstractArray
- sample_resolution_in_unit == 1 && sample_offset_in_unit == 0 && return sample_data
+ if (sample_data isa AbstractArray &&
+ sample_resolution_in_unit == 1 &&
+ sample_offset_in_unit == 0 &&
+ promote_type(typeof(sample_resolution_in_unit), typeof(sample_offset_in_unit)) == eltype(sample_data))
+ return sample_data
end
return sample_resolution_in_unit .* sample_data .+ sample_offset_in_unit
end |
closed by #133 ; |
If the encoding offset and resolution are
0.0 and 1.0 respectively, the decodedInt
ssamples.data
can have eltypeInt
, which is not friendly for downstream uses of the data includingDSP
.This could be avoided by either converting decoded data to have
Float64
eltype, or made slightly less awkward to handle by implementingconvert(Samples{D}, samples)
.The text was updated successfully, but these errors were encountered: