Skip to content

Add setindex! #51

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

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 16 additions & 2 deletions src/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,20 @@
copyto!(dst, adapt(Array, src))
end

@inline function Base.setindex!(x::OneHotArray{<:Any, N}, v, i::Integer, I::Vararg{Integer, N}) where N
@boundscheck checkbounds(x, i, I...)
if Bool(v)
@inbounds x.indices[I...] = i
elseif x.indices[I...] == i
# writing 0, at position of the 1 => move the 1 down if possible
i == x.nlabels && throw(ArgumentError("`setindex!` here would leave the `OneHotArray` without a hot one (in this column)"))
@inbounds x.indices[I...] = i+1
else

Check warning on line 99 in src/array.jl

View check run for this annotation

Codecov / codecov/patch

src/array.jl#L99

Added line #L99 was not covered by tests
# writing 0, where it's already 0 => do nothing
end
x
end

function Base.showarg(io::IO, x::OneHotArray, toplevel)
print(io, ndims(x) == 1 ? "OneHotVector(" : ndims(x) == 2 ? "OneHotMatrix(" : "OneHotArray(")
Base.showarg(io, x.indices, false)
Expand All @@ -104,9 +118,9 @@
# copy CuArray versions back before trying to print them:
for fun in (:show, :print_array) # print_array is used by 3-arg show
@eval begin
Base.$fun(io::IO, X::OneHotLike{T, N, var"N+1", <:AbstractGPUArray}) where {T, N, var"N+1"} =
Base.$fun(io::IO, X::OneHotLike{T, N, var"N+1", <:AbstractGPUArray}) where {T, N, var"N+1"} =
Base.$fun(io, adapt(Array, X))
Base.$fun(io::IO, X::LinearAlgebra.AdjOrTrans{Bool, <:OneHotLike{T, N, <:Any, <:AbstractGPUArray}}) where {T, N} =
Base.$fun(io::IO, X::LinearAlgebra.AdjOrTrans{Bool, <:OneHotLike{T, N, <:Any, <:AbstractGPUArray}}) where {T, N} =

Check warning on line 123 in src/array.jl

View check run for this annotation

Codecov / codecov/patch

src/array.jl#L123

Added line #L123 was not covered by tests
Base.$fun(io, adapt(Array, X))
end
end
Expand Down
25 changes: 25 additions & 0 deletions test/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,31 @@ end
@test_throws BoundsError oa[:, :]
end

@testset "Writing" begin
x = onehotbatch([1,2,3], 0:4)
x[1] = 1
@test x[:, 1] == [1, 0, 0, 0, 0]
x[2, 1] = 1.0
@test x[:, 1] == [0, 1, 0, 0, 0]
x[2] = 0
@test x[:, 1] == [0, 0, 1, 0, 0] # writing 0 pushes 1 down

y = onehotbatch([4,0,1], 0:4)
@test copyto!(x, y) == y
@test x[:, 1] == [0, 0, 0, 0, 1]
x .= 1
@test x[end, :] == [1, 1, 1]
x .= y
@test x[:, 1] == [0, 0, 0, 0, 1]

@test_throws ArgumentError y[5,1] = 0 # can't push 1 off the end
@test sum(y) == 3 # has not been corrupted before error

@test_throws BoundsError x[6,1] = 0
@test_throws BoundsError x[16] = 0
@test_throws InexactError x[2] = 1.5
end

@testset "Concatenating" begin
# vector cat
@test hcat(ov, ov) == OneHotMatrix(vcat(ov.indices, ov.indices), 10)
Expand Down
Loading