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

Make view(::AbstractWeights, ...) return an AbstractWeights #723

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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
1 change: 1 addition & 0 deletions docs/src/weights.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ In statistical applications, it is not uncommon to assign weights to samples. To
!!! note
- The weight vector is a light-weight wrapper of the input vector. The input vector is NOT copied during construction.
- The weight vector maintains the sum of weights, which is computed upon construction. If the value of the sum is pre-computed, one can supply it as the second argument to the constructor and save the time of computing the sum again.
- Views of weight vectors are also `AbstractWeights`, but their sum has to be recomputed by each call to `sum` as weights in the parent vector may have been mutated.
nalimilan marked this conversation as resolved.
Show resolved Hide resolved


## Implementations
Expand Down
37 changes: 34 additions & 3 deletions src/weights.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ macro weights(name)
sum::S
end
$(esc(name))(values::AbstractVector{<:Real}) = $(esc(name))(values, sum(values))
$(esc(:weightstype))(::Type{<:$(esc(name))}) = $(esc(name))
bkamins marked this conversation as resolved.
Show resolved Hide resolved
end
end

Expand All @@ -35,7 +36,33 @@ end
W(v, sum(v))
end

Base.getindex(wv::W, ::Colon) where {W <: AbstractWeights} = W(copy(wv.values), sum(wv))
Base.getindex(wv::AbstractWeights, ::Colon) = copy(wv)

Base.copy(wv::W) where {W <: AbstractWeights} =
weightstype(W)(copy(wv.values), sum(wv))

@propagate_inbounds function Base.view(wv::W, inds...) where {S <: Real, W <: AbstractWeights{S}}
@boundscheck checkbounds(wv, inds...)
@inbounds v = invoke(view, Tuple{AbstractArray, Vararg{Any}}, wv, inds...)
nalimilan marked this conversation as resolved.
Show resolved Hide resolved
# Sum is not actually used but compute the right type for clarity
weightstype(W)(v, zero(S))
end

# This method is implemented for backward compatibility
bkamins marked this conversation as resolved.
Show resolved Hide resolved
@propagate_inbounds function Base.view(wv::W, inds::Union{Integer, CartesianIndex}...) where
{S <: Real, W <: AbstractWeights{S}}
@boundscheck checkbounds(wv, inds...)
@inbounds invoke(view, Tuple{AbstractArray, Vararg{Any}}, wv, inds...)
end

# Always recompute the sum for views of AbstractWeights, as we cannot know whether
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe move the definitions of sum and copy into one place to make sure the reader can see both definitions side by side?

# the parent array has been mutated
Base.sum(wv::AbstractWeights{S, T, <:SubArray{T, <:Any, <:AbstractWeights}}) where
nalimilan marked this conversation as resolved.
Show resolved Hide resolved
{S<:Real, T<:Real} = sum(wv.values)

Base.copy(wv::W) where
{S<:Real, T<:Real, W<:AbstractWeights{S, T, <:SubArray{T, <:Any, <:AbstractWeights}}} =
weightstype(W)(copy(view(parent(wv.values).values, parentindices(wv.values)...)), wv.sum)
nalimilan marked this conversation as resolved.
Show resolved Hide resolved

@propagate_inbounds function Base.setindex!(wv::AbstractWeights, v::Real, i::Int)
s = v - wv[i]
Expand Down Expand Up @@ -337,8 +364,12 @@ end

for w in (AnalyticWeights, FrequencyWeights, ProbabilityWeights, Weights)
@eval begin
Base.isequal(x::$w, y::$w) = isequal(x.sum, y.sum) && isequal(x.values, y.values)
Base.:(==)(x::$w, y::$w) = (x.sum == y.sum) && (x.values == y.values)
Base.isequal(x::$w, y::$w) =
(x.values isa SubArray || y.values isa SubArray || isequal(x.values, y.values)) &&
nalimilan marked this conversation as resolved.
Show resolved Hide resolved
isequal(x.values, y.values)
Base.:(==)(x::$w, y::$w) =
(x.values isa SubArray || y.values isa SubArray || x.sum == y.sum) &&
nalimilan marked this conversation as resolved.
Show resolved Hide resolved
(x.values == y.values)
end
end

Expand Down
37 changes: 36 additions & 1 deletion test/weights.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ weight_funcs = (weights, aweights, fweights, pweights)
@test sum(sa, wv) === 7.0
end

@testset "$f, setindex!" for f in weight_funcs
@testset "$f, getindex, view, setindex!" for f in weight_funcs
w = [1., 2., 3.]
wv = f(w)

Expand All @@ -46,6 +46,41 @@ end
@test sum(wv) === 6.
@test wv == w

@test wv[[1, 3]] == w[[1, 3]]
@test typeof(wv[[1, 3]]) === typeof(wv)
@test sum( wv[[1, 3]]) === sum(w[[1, 3]])

# Check copy
@test copy(wv) == wv
@test typeof(copy(wv)) === typeof(wv)

# Check view
@test view(wv, :) == wv
@test typeof(view(wv, :)) <: StatsBase.weightstype(typeof(wv))
@test view(wv, [1, 3]) == view(wv, [true, false, true]) == w[[1, 3]]
@test typeof(view(wv, [1, 3])) === typeof(view(wv, [true, false, true])) <:
StatsBase.weightstype(typeof(wv))
@test sum(view(wv, [1, 3])) === sum(view(wv, [true, false, true])) === sum(w[[1, 3]])
@test_throws BoundsError view(wv, [1, 5])
@test_throws BoundsError view(wv, [true, false, true, true])
v = view(wv, [1, 3])
wv[1] += 1
@test sum(v) == sum(view(wv, [1, 3]))
v[1] -= 1
@test wv[1] == 1
@test copy(v) == v[:] == v
@test typeof(copy(v)) === typeof(v[:]) == typeof(wv)
@test view(wv, [1, 2], 1) == wv[[1, 2]]
@test typeof(view(wv, [1, 2], 1)) <: StatsBase.weightstype(typeof(wv))
@test view(wv, 1) == fill(wv[1])
@test view(wv, 1) isa SubArray
@test view(wv, 1, 1) == fill(wv[1])
@test view(wv, 1, 1) isa SubArray
@test view(wv, CartesianIndex(1, 1)) == fill(wv[CartesianIndex(1, 1)])
@test view(wv, CartesianIndex(1, 1)) isa SubArray
@test view(wv, CartesianIndex(1, 1), 1) == fill(wv[CartesianIndex(1, 1), 1])
@test view(wv, CartesianIndex(1, 1), 1) isa SubArray

# Test setindex! success
@test (wv[1] = 4) === 4 # setindex! returns original val
@test wv[1] === 4. # value correctly converted and set
Expand Down