Skip to content

Commit a82887a

Browse files
committed
Added option to return mean for ranges in and
1 parent 248faa3 commit a82887a

File tree

3 files changed

+50
-10
lines changed

3 files changed

+50
-10
lines changed

fatpack/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
from .endurance import *
66
from .racetrack import *
77

8-
__version__ = "0.6.1"
8+
__version__ = "0.6.2"

fatpack/rainflow.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ def find_rainflow_matrix(cycles, rowbins, colbins):
286286
return mat
287287

288288

289-
def find_rainflow_ranges(y, k=64):
289+
def find_rainflow_ranges(y, k=64, return_means=False):
290290
"""Returns the ranges of the complete series (incl. residue)
291291
292292
Returns the ranges by first determining the reversals of the dataseries
@@ -302,11 +302,15 @@ def find_rainflow_ranges(y, k=64):
302302
k : int
303303
The number of intervals to divide the min-max range of the dataseries
304304
into.
305+
return_means : bool
306+
Return mean for each rainflow range.
305307
306308
Returns
307309
-------
308310
ranges : ndarray
309311
The ranges identified by the rainflow algorithm in the dataseries.
312+
means : Optional[ndarray]
313+
The mean values for each range.
310314
311315
Raises
312316
------
@@ -328,10 +332,14 @@ def find_rainflow_ranges(y, k=64):
328332
else:
329333
raise ValueError("Could not find any cycles in sequence")
330334
ranges = np.abs(cycles[:, 1] - cycles[:, 0])
331-
return ranges
335+
if return_means:
336+
means = 0.5 * (cycles[:, 0] + cycles[:, 1])
337+
return ranges, means
338+
else:
339+
return ranges
332340

333341

334-
def find_rainflow_ranges_strict(y, k=64):
342+
def find_rainflow_ranges_strict(y, k=64, return_means=False):
335343
"""Returns the ranges of the complete series (incl. residue)
336344
337345
Returns the ranges by first determining the reversals of the dataseries
@@ -355,11 +363,15 @@ def find_rainflow_ranges_strict(y, k=64):
355363
k : int
356364
The number of intervals to divide the min-max range of the dataseries
357365
into.
366+
return_means : bool
367+
Return mean for each rainflow range.
358368
359369
Returns
360370
-------
361371
ranges : ndarray
362372
The ranges identified by the rainflow algorithm in the dataseries.
373+
means : Optional[ndarray]
374+
The mean values for each range.
363375
364376
Raises
365377
------
@@ -382,7 +394,11 @@ def find_rainflow_ranges_strict(y, k=64):
382394
else:
383395
raise ValueError("Could not find any cycles in sequence")
384396
ranges = np.abs(cycles[:, 1] - cycles[:, 0])
385-
return ranges
397+
if return_means:
398+
means = 0.5 * (cycles[:, 0] + cycles[:, 1])
399+
return ranges, means
400+
else:
401+
return ranges
386402

387403

388404
def find_range_count(ranges, bins=10, weights=None):

fatpack/test.py

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,16 @@
3636
[ 4., 3.], [10., 6.], [ 4., 8.], [ 1., 12.],
3737
[ 4., 6.], [ 4., 7.], [ 9., 2.], [ 1., 12.],
3838
]),
39-
ranges_total = np.array([4., 1., 8., 6., 1., 4.,
39+
ranges_total_strict = np.array([4., 1., 8., 6., 1., 4.,
4040
4., 11., 2., 3., 7., 11.]),
41+
ranges_total = np.array([ 0.171875, 3.4375, 1.375, 0.171875,
42+
8.078125, 5.671875, 0.6875, 3.4375,
43+
4.296875, 1.890625, 3.4375, 6.53125,
44+
10.828125, 11.0]),
45+
ranges_total_means = np.array([9.679688, 6.84375, 3.75, 2.289062,
46+
6.242188, 8.304688, 3.921875, 7.703125,
47+
6.070312, 5.210938, 5.640625, 5.296875,
48+
6.585938, 6.50]),
4149
ranges_count = np.array([2, 1, 1, 3, 0, 1, 1, 1, 0, 0, 2, 0]),
4250
classes = np.array([1., 2., 3., 4., 5., 6.,
4351
7., 8., 9., 10., 11., 12.]),
@@ -65,8 +73,8 @@ class BaseArrayTestCase:
6573
def test_array_equal(self):
6674
np.testing.assert_array_equal(self.result, self.result_true)
6775

68-
def test_allclose(self):
69-
np.testing.assert_allclose(self.result, self.result_true)
76+
# def test_allclose(self):
77+
# np.testing.assert_allclose(self.result, self.result_true)
7078

7179

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

130138

131-
class TestFindRainflowRangesStrict(BaseArrayTestCase, unittest.TestCase):
139+
class TestFindRainflowRanges(BaseArrayTestCase, unittest.TestCase):
132140
def setUp(self):
133141
self.result_true = TESTDATA['ranges_total']
142+
self.result = find_rainflow_ranges(TESTDATA['dataseries'], k=64)
143+
144+
145+
class TestFindRainflowRangesMeans(unittest.TestCase):
146+
def setUp(self):
147+
self.result_true = TESTDATA['ranges_total_means']
148+
_, self.result = find_rainflow_ranges(
149+
TESTDATA['dataseries'], k=64, return_means=True)
150+
151+
def test_almost_equal(self):
152+
np.testing.assert_allclose(self.result, self.result_true, rtol=1e-6)
153+
154+
155+
class TestFindRainflowRangesStrict(BaseArrayTestCase, unittest.TestCase):
156+
def setUp(self):
157+
self.result_true = TESTDATA['ranges_total_strict']
134158
self.result = find_rainflow_ranges_strict(
135159
TESTDATA['dataseries'], k=11)
136160

@@ -140,7 +164,7 @@ def setUp(self):
140164
self.N_true = TESTDATA['ranges_count']
141165
self.S_true = TESTDATA['classes']
142166
self.N, self.S = find_range_count(
143-
TESTDATA['ranges_total'],
167+
TESTDATA['ranges_total_strict'],
144168
bins=TESTDATA['class_boundaries'])
145169

146170
def test_count_allclose(self):

0 commit comments

Comments
 (0)