-
Couldn't load subscription status.
- Fork 195
Make view(::AbstractWeights, ...) return an AbstractWeights
#723
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
base: master
Are you sure you want to change the base?
Changes from all commits
a29e523
04dd238
192ccca
91208f4
efa81b6
95b3650
f9db85a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -11,14 +11,19 @@ macro weights(name) | |||||
| return quote | ||||||
| mutable struct $name{S<:Real, T<:Real, V<:AbstractVector{T}} <: AbstractWeights{S, T, V} | ||||||
| values::V | ||||||
| sum::S | ||||||
| """ | ||||||
| Pre-computed sum. Private field, to be accessed only via `sum(wv)`. | ||||||
| Set to `missing` by `view` as we cannot know when the parent is mutated. | ||||||
| """ | ||||||
| sum::Union{S, Missing} | ||||||
| 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 | ||||||
|
|
||||||
| length(wv::AbstractWeights) = length(wv.values) | ||||||
| sum(wv::AbstractWeights) = wv.sum | ||||||
| sum(wv::AbstractWeights{S}) where {S<:Real} = wv.sum::S | ||||||
| isempty(wv::AbstractWeights) = isempty(wv.values) | ||||||
| size(wv::AbstractWeights) = size(wv.values) | ||||||
|
|
||||||
|
|
@@ -35,7 +40,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
|
||||||
| weightstype(W){S, eltype(wv), typeof(v)}(v, missing) | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| 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 | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe move the definitions of |
||||||
| # the parent array has been mutated | ||||||
| Base.sum(wv::AbstractWeights{S, T, <:SubArray}) where {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)...)), sum(wv)) | ||||||
|
|
||||||
| @propagate_inbounds function Base.setindex!(wv::AbstractWeights, v::Real, i::Int) | ||||||
| s = v - wv[i] | ||||||
|
|
@@ -85,7 +116,7 @@ if `corrected=true`. | |||||
| @inline function varcorrection(w::Weights, corrected::Bool=false) | ||||||
| corrected && throw(ArgumentError("Weights type does not support bias correction: " * | ||||||
| "use FrequencyWeights, AnalyticWeights or ProbabilityWeights if applicable.")) | ||||||
| 1 / w.sum | ||||||
| 1 / sum(w) | ||||||
| end | ||||||
|
|
||||||
| @weights AnalyticWeights | ||||||
|
|
@@ -118,7 +149,7 @@ aweights(vs::RealArray) = AnalyticWeights(vec(vs)) | |||||
| * `corrected=false`: ``\\frac{1}{\\sum w}`` | ||||||
| """ | ||||||
| @inline function varcorrection(w::AnalyticWeights, corrected::Bool=false) | ||||||
| s = w.sum | ||||||
| s = sum(w) | ||||||
|
|
||||||
| if corrected | ||||||
| sum_sn = sum(x -> (x / s) ^ 2, w) | ||||||
|
|
@@ -156,7 +187,7 @@ fweights(vs::RealArray) = FrequencyWeights(vec(vs)) | |||||
| * `corrected=false`: ``\\frac{1}{\\sum w}`` | ||||||
| """ | ||||||
| @inline function varcorrection(w::FrequencyWeights, corrected::Bool=false) | ||||||
| s = w.sum | ||||||
| s = sum(w) | ||||||
|
|
||||||
| if corrected | ||||||
| 1 / (s - 1) | ||||||
|
|
@@ -194,7 +225,7 @@ pweights(vs::RealArray) = ProbabilityWeights(vec(vs)) | |||||
| * `corrected=false`: ``\\frac{1}{\\sum w}`` | ||||||
| """ | ||||||
| @inline function varcorrection(w::ProbabilityWeights, corrected::Bool=false) | ||||||
| s = w.sum | ||||||
| s = sum(w) | ||||||
|
|
||||||
| if corrected | ||||||
| n = count(!iszero, w) | ||||||
|
|
@@ -337,8 +368,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.sum, y.sum)) && | ||||||
| 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 | ||||||
|
|
||||||
|
|
@@ -669,7 +704,7 @@ function quantile(v::RealVector{V}, w::AbstractWeights{W}, p::RealVector) where | |||||
| isempty(p) && throw(ArgumentError("empty quantile array")) | ||||||
| all(x -> 0 <= x <= 1, p) || throw(ArgumentError("input probability out of [0,1] range")) | ||||||
|
|
||||||
| w.sum == 0 && throw(ArgumentError("weight vector cannot sum to zero")) | ||||||
| sum(w) == 0 && throw(ArgumentError("weight vector cannot sum to zero")) | ||||||
| length(v) == length(w) || throw(ArgumentError("data and weight vectors must be the same size," * | ||||||
| "got $(length(v)) and $(length(w))")) | ||||||
| for x in w.values | ||||||
|
|
||||||
Uh oh!
There was an error while loading. Please reload this page.