Skip to content

Commit

Permalink
Make sure returned number of bins does not exceed nbins (#62)
Browse files Browse the repository at this point in the history
Due to floating point accuracy count_cycles might return more bins that what is specified by argument nbins.

Solves #60
  • Loading branch information
iamlikeme authored Dec 28, 2021
1 parent 2381b69 commit 48cc3da
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/rainflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,16 @@ def count_cycles(series, ndigits=None, nbins=None, binsize=None):
if binsize is not None:
nmax = 0
for rng, count in cycles:
n = int(math.ceil(rng / binsize)) # using int for Python 2 compatibility
quotient = rng / binsize
n = int(math.ceil(quotient)) # using int for Python 2 compatibility

if nbins and n > nbins:
# Due to floating point accuracy we may get n > nbins,
# in which case we move rng to the preceeding bin.
if (quotient % 1) > 1e-6:
raise Exception("Unexpected error")
n = n - 1

counts[n * binsize] += count
nmax = max(n, nmax)

Expand Down
15 changes: 15 additions & 0 deletions tests/test_rainflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,18 @@ def test_reversals_yield_value(series, cycles, counts, approx):
)
def test_reversals_small_series(series, reversals):
assert list(rainflow.reversals(series)) == reversals


def test_num_bins():
# This test checks for a bug reported in issue #60 where the
# returned number of bins was different than the nbins argument
# due to floating point accuracy.
series = [
0,
3517.860166127188,
-3093.4966492094213,
0,
]
nbins = 100
result = rainflow.count_cycles(series, nbins=nbins)
assert len(result) == nbins

0 comments on commit 48cc3da

Please sign in to comment.