Skip to content

Commit

Permalink
Added unary inversion operator and tests for logical operations. Closes
Browse files Browse the repository at this point in the history
  • Loading branch information
stringertheory committed Feb 5, 2024
1 parent ffbdf57 commit 9ac2097
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 3 deletions.
20 changes: 20 additions & 0 deletions tests/test_methods_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,23 @@ def test_moving_average_pandas_compatibility():
assert all(pd_ts.index[i] == i for i in range(1, 9))
assert np.isnan(pd_ts.values[0])
assert all(pd_ts.values[i] == ts.mean(i - 1, i + 1) for i in range(2, 9))


def test_moving_average_pandas_flag():
ts = traces.TimeSeries([[1, 2], [2, 3], [6, 1], [8, 4]], default=0)
ma = ts.moving_average(1, 2, -1, 11, pandas=True)
assert list(zip(ma.index, ma)) == [
(-1, 0.0),
(0, 0.0),
(1, 1.0),
(2, 2.5),
(3, 3.0),
(4, 3.0),
(5, 3.0),
(6, 2.0),
(7, 1.0),
(8, 2.5),
(9, 4.0),
(10, 4.0),
(11, 4.0),
]
30 changes: 30 additions & 0 deletions tests/test_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,33 @@ def test_to_bool_default():
assert TimeSeries(data=data, default=None).to_bool(default=10).default == 10
assert TimeSeries(data=data, default=0).to_bool(default=10).default == 10
assert TimeSeries(data=data, default=5).to_bool(default=10).default == 10


def test_truthiness():
data = [(0, 0), (2, 5), (3, 0), (4, 7), (10, 3)]
assert bool(TimeSeries()) is False
assert bool(TimeSeries(data=data)) is True

if TimeSeries():
assert "should not be here"


def test_logical_operations():
ts1 = TimeSeries([(0, 0), (2, 5), (3, 0), (4, 7), (10, 3)])
ts2 = TimeSeries([(0, 0), (1, 1), (2, 3), (4, 0), (5, 5), (10, 1), (11, 0)])

a_or = [(0, 0), (1, 1), (2, 5), (3, 3), (4, 7), (5, 7), (10, 3), (11, 3)]
assert list(ts1.logical_or(ts2).items()) == a_or
assert list((ts1 | ts2).items()) == a_or

a_and = [(0, 0), (1, 0), (2, 3), (3, 0), (4, 0), (5, 5), (10, 1), (11, 0)]
assert list(ts1.logical_and(ts2).items()) == a_and
assert list((ts1 & ts2).items()) == a_and

a_xor = [(0, 0), (1, 1), (2, 0), (3, 1), (4, 1), (5, 0), (10, 0), (11, 1)]
assert list(ts1.logical_xor(ts2).items()) == a_xor
assert list((ts1 ^ ts2).items()) == a_xor

a_not = [(0, True), (2, False), (3, True), (4, False), (10, False)]
assert list(ts1.to_bool(invert=True).items()) == a_not
assert list((~ts1).items()) == a_not
12 changes: 12 additions & 0 deletions tests/test_traces.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,15 @@ def test_set_same_interval_twice():

tr[17:42] = 0
assert list(tr.items()) == [(0, 10), (17, 0), (42, 10), (100, 10)]


def test_convenience_access_methods():
ts = TimeSeries([(1, 2), (2, 3), (6, 1), (8, 4)])
assert ts.first_key() == 1
assert ts.first_value() == 2
assert ts.first_item() == (1, 2)
assert ts.last_key() == 8
assert ts.last_value() == 4
assert ts.last_item() == (8, 4)
assert ts.get_item_by_index(0) == (1, 2)
assert ts.get_item_by_index(-1) == (8, 4)
9 changes: 6 additions & 3 deletions traces/timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,10 @@ def _get_previous(self, time):
return left_value
elif right_index == 0:
return self.default
else:
else: # pragma: no cover
msg = (
f"self._d.bisect_right({time}) returned a negative value. "
"""This "can't" happen: please file an issue at """
"https://github.com/datascopeanalytics/traces/issues"
"""This "can't" happen: please help by filing an issue."""
)
raise ValueError(msg)

Expand Down Expand Up @@ -1008,6 +1007,10 @@ def __xor__(self, other):
"""Allow a ^ b syntax"""
return self.logical_xor(other)

def __invert__(self):
"""Allow ~a syntax"""
return self.to_bool(invert=True)

def __eq__(self, other):
return self.items() == other.items()

Expand Down

0 comments on commit 9ac2097

Please sign in to comment.