- 
                Notifications
    You must be signed in to change notification settings 
- 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
          
     Open
      
      
            nalimilan
  wants to merge
  7
  commits into
  master
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
nl/weightsview
  
      
      
   
  
    
  
  
  
 
  
      
    base: master
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            7 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      a29e523
              
                Make `view(::AbstractWeights, ...)` return an `AbstractWeights`
              
              
                nalimilan 04dd238
              
                Make `view` return a `SubArray` of `AbstractWeights`
              
              
                nalimilan 192ccca
              
                Make `copy` preserve weights type
              
              
                nalimilan 91208f4
              
                Revert unrelated changes
              
              
                nalimilan efa81b6
              
                Fixes
              
              
                nalimilan 95b3650
              
                More fixes
              
              
                nalimilan f9db85a
              
                Safer approach, more tests, review fixes
              
              
                nalimilan File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | 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 | ||||||
|  | ||||||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.