- 
                Notifications
    
You must be signed in to change notification settings  - Fork 25
 
Adds collector accumulator #378
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
      
      
            HDembinski
  wants to merge
  4
  commits into
  develop
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
add_collector
  
      
      
   
  
    
  
  
  
 
  
      
    base: develop
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.
          
          
  
     Draft
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            4 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    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
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| // Copyright 2020 Hans Dembinski | ||
| // | ||
| // Distributed under the Boost Software License, version 1.0. | ||
| // (See accompanying file LICENSE_1_0.txt | ||
| // or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
| 
     | 
||
| #pragma once | ||
| 
     | 
||
| #include <algorithm> | ||
| #include <array> | ||
| #include <boost/core/nvp.hpp> | ||
| #include <boost/histogram/weight.hpp> | ||
| #include <vector> | ||
| 
     | 
||
| namespace accumulators { | ||
| 
     | 
||
| /** Keeps track of all weights in each bin. | ||
| 
     | 
||
| Can be used to compute bootstrap estimates of the uncertainies. | ||
| */ | ||
| template <class ValueType> | ||
| struct weight_collector { | ||
| using value_type = ValueType; | ||
| using const_reference = const value_type&; | ||
| using data_type = std::vector<value_type>; | ||
| 
     | 
||
| weight_collector() = default; | ||
| 
     | 
||
| void operator+=(const boost::histogram::weight_type<value_type>& w) noexcept { | ||
| data.push_back(w.value); | ||
| } | ||
| 
     | 
||
| weight_collector& operator+=(const weight_collector& rhs) noexcept { | ||
| data.reserve(data.size() + rhs.data.size()); | ||
| for(auto&& x : rhs.data) | ||
| data.push_back(x); | ||
| return *this; | ||
| } | ||
| 
     | 
||
| weight_collector& operator*=(const value_type& s) noexcept { | ||
| for(auto&& x : data) | ||
| x *= s; | ||
| return *this; | ||
| } | ||
| 
     | 
||
| bool operator==(const weight_collector& rhs) const noexcept { | ||
| return std::equal(data.begin(), data.end(), rhs.data.begin(), rhs.data.end()); | ||
| } | ||
| 
     | 
||
| bool operator!=(const weight_collector& rhs) const noexcept { | ||
| return !operator==(rhs); | ||
| } | ||
| 
     | 
||
| template <class Archive> | ||
| void serialize(Archive& ar, unsigned) { | ||
| ar& boost::make_nvp("data", data); | ||
| } | ||
| 
     | 
||
| data_type data{}; | ||
| }; | ||
| 
     | 
||
| /** Keeps track of all samples in each bin. | ||
| 
     | 
||
| Can be used to compute bootstrap estimates of the uncertainies. | ||
| */ | ||
| template <class ValueType> | ||
| struct sample_collector { | ||
| using value_type = ValueType; | ||
| using const_reference = const value_type&; | ||
| using item_type = std::array<value_type, 2>; | ||
| using data_type = std::vector<item_type>; | ||
| 
     | 
||
| sample_collector() = default; | ||
| 
     | 
||
| void operator()(const value_type& x) noexcept { data.emplace_back(1, x); } | ||
| 
     | 
||
| void operator()(const boost::histogram::weight_type<value_type>& w, | ||
| const value_type& x) noexcept { | ||
| data.emplace_back(w.value, x); | ||
| } | ||
| 
     | 
||
| sample_collector& operator+=(const sample_collector& rhs) noexcept { | ||
| data.reserve(data.size() + rhs.data.size()); | ||
| for(auto&& x : rhs) | ||
| data.push_back(x); | ||
| return *this; | ||
| } | ||
| 
     | 
||
| sample_collector& operator*=(const value_type& s) noexcept { | ||
| for(auto&& x : data) | ||
| x.second *= s; | ||
| return *this; | ||
| } | ||
| 
     | 
||
| bool operator==(const sample_collector& rhs) const noexcept { | ||
| return std::equal(data.begin(), data.end(), rhs.begin(), rhs.end()); | ||
| } | ||
| 
     | 
||
| bool operator!=(const sample_collector& rhs) const noexcept { | ||
| return !operator==(rhs); | ||
| } | ||
| 
     | 
||
| template <class Archive> | ||
| void serialize(Archive& ar, unsigned) { | ||
| ar& boost::make_nvp("data", data); | ||
| } | ||
| 
     | 
||
| data_type data{}; | ||
| }; | ||
| 
     | 
||
| } // namespace accumulators | 
  
    
      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
    
  
  
    
              
  
    
      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 | 
|---|---|---|
| @@ -1,16 +1,26 @@ | ||
| # -*- coding: utf-8 -*- | ||
| from __future__ import absolute_import, division, print_function | ||
| 
     | 
||
| from ..accumulators import ( | ||
| Sum as sum, | ||
| Mean as mean, | ||
| WeightedSum as weighted_sum, | ||
| WeightedMean as weighted_mean, | ||
| ) | ||
| 
     | 
||
| del absolute_import, division, print_function | ||
| def _load(): | ||
| from .. import accumulators as acc | ||
| import string | ||
| 
     | 
||
| tr = {ord(k): "_" + k.lower() for k in string.ascii_uppercase} | ||
| 
     | 
||
| # from CamelCase to snake_case | ||
| r = {} | ||
| for key in dir(acc): | ||
| if key.startswith("_"): | ||
| continue | ||
| nkey = key[0].lower() + key[1:].translate(tr) | ||
| r[nkey] = getattr(acc, key) | ||
| return r | ||
| 
     | 
||
| __all__ = ("sum", "mean", "weighted_sum", "weighted_mean") | ||
| 
     | 
||
| locals().update(_load()) | ||
| 
     | 
||
| del absolute_import, division, print_function | ||
| del _load | ||
| 
     | 
||
| # These will have the original module locations and original names. | 
  
    
      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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is taking a very simple static list, and doing run time manipulations with a function that has more lines than the code it replaces, breaking static analysis. We are also losing any ability to not follow the specific naming scheme in the future if something different is added.
If we add unit tests for a new type here, that will immediately break if a developer forgets to update this static list.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, PyBind11 is anything but DRY...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remember,
_coreis monkey-patched for documentation, so everything in it should be explicitly imported. It is also ignored for static analysis, so there again, everything should be explicitly imported. Explicit is better than implicit.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This (unrelated to list accumulators) change is also what is breaking Python 2!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know what pybind11 has to do with it, and on the contrary, it is a good example for being dry. It is even stated in their docs, that they strongly prefer minimal code to do the work. Minimal code equates avoiding redundancy.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your counter arguments make no sense to me. The code is explicit, explicit in the forwarding and transformation rules. I don't have a problem with not being able to do static analysis here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you have another solution that allows me to easily add an accumulator without changing the code in several places, then go ahead. For now this is better than it was before.