Skip to content

Commit

Permalink
Allow converting encoded samples if the element type matches (#171)
Browse files Browse the repository at this point in the history
At present, we error on `convert(::Type{Samples{A}}, ::Samples{B})` for
all `A` and `B` if the samples are encoded. This is reasonable since we
don't want to affect how the samples get decoded. However, decoding
should be based on the type of the encoded samples themselves rather
than of their container; a decoder shouldn't care whether the incoming
data is e.g. a full matrix or a view of a matrix. Thus we can allow
conversion for encoded samples provided that only the container type is
affected, not the element type.
  • Loading branch information
ararslan authored Sep 13, 2024
1 parent 3537cd8 commit 4153b50
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Onda"
uuid = "e853f5be-6863-11e9-128d-476edb89bfb5"
authors = ["Beacon Biosignals, Inc."]
version = "0.15.8"
version = "0.15.9"

[deps]
Arrow = "69666777-d1a9-59fb-9406-91d4454c9d45"
Expand Down
6 changes: 5 additions & 1 deletion src/samples.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ function Base.hash(a::Samples, h::UInt)
end

function Base.convert(::Type{Samples{T}}, samples::Samples) where {T}
samples.encoded && throw(ArgumentError("can't `convert` encoded samples; use `decode` first"))
# For encoded samples, allow converting between container types so long as the element
# type remains the same
if samples.encoded && eltype(T) != eltype(samples.data)
throw(ArgumentError("can't `convert` encoded samples to a different element type; use `decode` first"))
end
return Samples(convert(T, samples.data), samples.info, samples.encoded)
end

Expand Down
16 changes: 13 additions & 3 deletions test/samples.jl
Original file line number Diff line number Diff line change
Expand Up @@ -232,19 +232,29 @@ end

# In particular, this fixes an arrow deserialization issue
# (https://github.com/beacon-biosignals/Onda.jl/issues/156)
table = [(; col = samples), (; col = samples)]
table = [(; col=samples), (; col=samples)]
# Here, materializing this table in this way threw an error before `convert` was defined
rt_table = DataFrame(Arrow.Table(Arrow.tobuffer(table)); copycols=true)
rt = rt_table[1, "col"]
@test rt.data isa AbstractMatrix{Float32}
@test rt == samples

# For encoded samples, in generally we cannot `convert`.
# For encoded samples, in general we cannot `convert` between element types, as this
# could affect downstream decoding.
# We choose to not update encoding parameter in `convert`, since `convert` can be implied
# implicitly, and changing the encoding parameters seems like too big of a change.
samples = Samples(rand(sample_type(info), 3, 100), info, true)
err = ArgumentError("can't `convert` encoded samples; use `decode` first")
err = ArgumentError("can't `convert` encoded samples to a different element type; use `decode` first")
@test_throws err convert(Samples{Matrix{Int32}}, samples)

# If the element type remains the same, we can `convert` between container types, as
# this is unlikely to affect decoding (unless a truly deranged method has been defined)
T = sample_type(info)
samples = Samples(view(rand(T, 3, 100), :, :), info, true)
@test samples.data isa SubArray{T}
s2 = convert(Samples{Matrix{T}}, samples)
@test s2.data isa Matrix{T}
@test samples == s2
end

@testset "Base.copy" begin
Expand Down

2 comments on commit 4153b50

@ararslan
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/115105

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

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.15.9 -m "<description of version>" 4153b509427cf607bbc6c82efc09bb0ac2f5bbf5
git push origin v0.15.9

Please sign in to comment.