Rounds iterables (arrays, lists, sets, etc) while maintaining the sum of the initial array.
Likely abandoned. No release or commit for years (last release over 4 years ago).
pip install iteroundREADME as published on PyPI with version 1.0.4.
Iteround: Sum-safe Rounding for Iterables
.. image:: https://travis-ci.org/cgdeboer/iteround.svg?branch=master :target: https://travis-ci.org/cgdeboer/iteround
.. image:: https://img.shields.io/pypi/v/iteround.svg :target: https://pypi.org/project/iteround/
.. image:: https://img.shields.io/conda/vn/conda-forge/iteround.svg :target: https://anaconda.org/conda-forge/iteround
Iteround is an organic (standard library) sum-safe rounding library for Python iterables (lists, tuples, dicts).
.. image:: https://raw.githubusercontent.com/cgdeboer/iteround/master/docs/iteround.png
Example Code:
.. code-block:: python
>>> import iteround
>>> data = {'foo': 60.19012964572332,
'bar': 15.428802458406679,
'baz': 24.381067895870007}
>>> sum(data.values())
100.0
>>> rounded = iteround.saferound(data, 0)
>>> rounded
{'foo': 60.0,
'bar': 16.0,
'baz': 24.0}
>>> sum(rounded.values())
100.0
How It Works
Iteround provides a single method, called :code:saferound that takes the
following inputs:
iterable (list, dict, set, numpy.array, generator): list(y) of numbers If a dict is passed in, the values must be all floats.
places (int): Places for rounding. Number of places each item in the set should be rounded to.
topline (float, optional): Topline to match Useful in places where we want the total sum to match a different topline than the sum of iterable. This can useful in cases where original values are altered before passing into the saferound method, but the original sum needs to be maintained.
strategy (str, optional): The strategy used to clean up rounding errors 'difference', 'largest', 'smallest'. Defaults to 'difference'
'difference' seeks to minimize the sum of the array of the
differences between the original value and the rounded value of
each item in the iterable. It will adjust the items with the
largest difference to preserve the sum. This is the default.
'largest' for any post rounding adjustments, sort the array by
the largest values to smallest and adjust those first.
'smallest' for any post rounding adjustments, sort the array by
the smallest values to largest, adjust the smaller ones first.
Strategy strings are available as:
:code:`iteround.DIFFERENCE`
:code:`iteround.LARGEST`
:code:`iteround.SMALLEST`