Skip to content

Commit

Permalink
update cyclecount getbins routine to handle equal mx and mn values
Browse files Browse the repository at this point in the history
  • Loading branch information
twmacro committed Jul 23, 2023
1 parent 12cb90a commit fdfb74a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
12 changes: 9 additions & 3 deletions pyyeti/cyclecount.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,11 @@ def getbins(bins, mx, mn, right=True, check_bounds=False):
Notes
-----
If `mx` and `mn` are equal, they are reset::
mx = mx + 0.5
mn = mn - 0.5
If `bins` is a scalar, this routine tries to mimic the behavior of
the :func:`pandas.cut` routine::
Expand All @@ -400,8 +405,6 @@ def getbins(bins, mx, mn, right=True, check_bounds=False):
Raises
------
ValueError
If `mx` and `mn` are equal
ValueError
If `bins` is a vector but is not monotonically increasing
Expand All @@ -419,7 +422,10 @@ def getbins(bins, mx, mn, right=True, check_bounds=False):
if mx < mn:
mx, mn = mn, mx
elif mx == mn:
raise ValueError("`mx` and `mn` must not be equal")
mx = mx + 0.5
mn = mn - 0.5
# raise ValueError("`mx` and `mn` must not be equal")

if bins.size == 1:
bins = int(bins)
bb = np.linspace(mn, mx, bins + 1)
Expand Down
27 changes: 25 additions & 2 deletions tests/test_cyclecount.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,6 @@ def test_getbins_1():
assert np.allclose(bb, [3.992, 6.0, 8.0, 10.0, 12.0])
bb = cyclecount.getbins(4, 4, 12, right=False)
assert np.allclose(bb, [4.0, 6.0, 8.0, 10.0, 12.008])
with pytest.raises(ValueError):
cyclecount.getbins(4, 3.0, 3.0)


def test_getbins_2():
Expand Down Expand Up @@ -210,3 +208,28 @@ def test_sigcount_2():
assert np.allclose(table.values, [[12.0, 12.5], [12.5, 12.5]])
assert np.allclose(ampb, [0.500, 49.500, 98.598])
assert np.allclose(aveb, [-0.500, 0.000, 0.501])


def test_sigcount_3():
df = cyclecount.sigcount(np.array([10.0, -10.0, 10.0, -10.0]), ampbins=4)
assert np.allclose(df.values, [[0.0, 1.5, 0.0, 0.0]])
assert list(df.columns) == [
"(9.499, 9.750]",
"(9.750, 10.000]",
"(10.000, 10.250]",
"(10.250, 10.500]",
]
assert list(df.index) == ["(-0.501, 0.500]"]

df = cyclecount.sigcount(
np.array([10.0, -10.0, 10.0, -10.0]), ampbins=4, right=False
)

assert np.allclose(df.values, [[0.0, 0.0, 1.5, 0.0]])
assert list(df.columns) == [
"[9.500, 9.750)",
"[9.750, 10.000)",
"[10.000, 10.250)",
"[10.250, 10.501)",
]
assert list(df.index) == ["[-0.500, 0.501)"]

0 comments on commit fdfb74a

Please sign in to comment.