Skip to content

Commit

Permalink
Added option to return mean for ranges in and
Browse files Browse the repository at this point in the history
  • Loading branch information
Gunnstein committed Dec 15, 2019
1 parent 248faa3 commit a82887a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 10 deletions.
2 changes: 1 addition & 1 deletion fatpack/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
from .endurance import *
from .racetrack import *

__version__ = "0.6.1"
__version__ = "0.6.2"
24 changes: 20 additions & 4 deletions fatpack/rainflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ def find_rainflow_matrix(cycles, rowbins, colbins):
return mat


def find_rainflow_ranges(y, k=64):
def find_rainflow_ranges(y, k=64, return_means=False):
"""Returns the ranges of the complete series (incl. residue)
Returns the ranges by first determining the reversals of the dataseries
Expand All @@ -302,11 +302,15 @@ def find_rainflow_ranges(y, k=64):
k : int
The number of intervals to divide the min-max range of the dataseries
into.
return_means : bool
Return mean for each rainflow range.
Returns
-------
ranges : ndarray
The ranges identified by the rainflow algorithm in the dataseries.
means : Optional[ndarray]
The mean values for each range.
Raises
------
Expand All @@ -328,10 +332,14 @@ def find_rainflow_ranges(y, k=64):
else:
raise ValueError("Could not find any cycles in sequence")
ranges = np.abs(cycles[:, 1] - cycles[:, 0])
return ranges
if return_means:
means = 0.5 * (cycles[:, 0] + cycles[:, 1])
return ranges, means
else:
return ranges


def find_rainflow_ranges_strict(y, k=64):
def find_rainflow_ranges_strict(y, k=64, return_means=False):
"""Returns the ranges of the complete series (incl. residue)
Returns the ranges by first determining the reversals of the dataseries
Expand All @@ -355,11 +363,15 @@ def find_rainflow_ranges_strict(y, k=64):
k : int
The number of intervals to divide the min-max range of the dataseries
into.
return_means : bool
Return mean for each rainflow range.
Returns
-------
ranges : ndarray
The ranges identified by the rainflow algorithm in the dataseries.
means : Optional[ndarray]
The mean values for each range.
Raises
------
Expand All @@ -382,7 +394,11 @@ def find_rainflow_ranges_strict(y, k=64):
else:
raise ValueError("Could not find any cycles in sequence")
ranges = np.abs(cycles[:, 1] - cycles[:, 0])
return ranges
if return_means:
means = 0.5 * (cycles[:, 0] + cycles[:, 1])
return ranges, means
else:
return ranges


def find_range_count(ranges, bins=10, weights=None):
Expand Down
34 changes: 29 additions & 5 deletions fatpack/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,16 @@
[ 4., 3.], [10., 6.], [ 4., 8.], [ 1., 12.],
[ 4., 6.], [ 4., 7.], [ 9., 2.], [ 1., 12.],
]),
ranges_total = np.array([4., 1., 8., 6., 1., 4.,
ranges_total_strict = np.array([4., 1., 8., 6., 1., 4.,
4., 11., 2., 3., 7., 11.]),
ranges_total = np.array([ 0.171875, 3.4375, 1.375, 0.171875,
8.078125, 5.671875, 0.6875, 3.4375,
4.296875, 1.890625, 3.4375, 6.53125,
10.828125, 11.0]),
ranges_total_means = np.array([9.679688, 6.84375, 3.75, 2.289062,
6.242188, 8.304688, 3.921875, 7.703125,
6.070312, 5.210938, 5.640625, 5.296875,
6.585938, 6.50]),
ranges_count = np.array([2, 1, 1, 3, 0, 1, 1, 1, 0, 0, 2, 0]),
classes = np.array([1., 2., 3., 4., 5., 6.,
7., 8., 9., 10., 11., 12.]),
Expand Down Expand Up @@ -65,8 +73,8 @@ class BaseArrayTestCase:
def test_array_equal(self):
np.testing.assert_array_equal(self.result, self.result_true)

def test_allclose(self):
np.testing.assert_allclose(self.result, self.result_true)
# def test_allclose(self):
# np.testing.assert_allclose(self.result, self.result_true)


class TestFindReversalsStrict(BaseArrayTestCase, unittest.TestCase):
Expand Down Expand Up @@ -128,9 +136,25 @@ def setUp(self):
self.result = find_rainflow_matrix(cycles, bins, bins)


class TestFindRainflowRangesStrict(BaseArrayTestCase, unittest.TestCase):
class TestFindRainflowRanges(BaseArrayTestCase, unittest.TestCase):
def setUp(self):
self.result_true = TESTDATA['ranges_total']
self.result = find_rainflow_ranges(TESTDATA['dataseries'], k=64)


class TestFindRainflowRangesMeans(unittest.TestCase):
def setUp(self):
self.result_true = TESTDATA['ranges_total_means']
_, self.result = find_rainflow_ranges(
TESTDATA['dataseries'], k=64, return_means=True)

def test_almost_equal(self):
np.testing.assert_allclose(self.result, self.result_true, rtol=1e-6)


class TestFindRainflowRangesStrict(BaseArrayTestCase, unittest.TestCase):
def setUp(self):
self.result_true = TESTDATA['ranges_total_strict']
self.result = find_rainflow_ranges_strict(
TESTDATA['dataseries'], k=11)

Expand All @@ -140,7 +164,7 @@ def setUp(self):
self.N_true = TESTDATA['ranges_count']
self.S_true = TESTDATA['classes']
self.N, self.S = find_range_count(
TESTDATA['ranges_total'],
TESTDATA['ranges_total_strict'],
bins=TESTDATA['class_boundaries'])

def test_count_allclose(self):
Expand Down

0 comments on commit a82887a

Please sign in to comment.