Skip to content

Commit 8d62006

Browse files
committed
FIX: tests for TimeMovingCountUnique
1 parent 3700abb commit 8d62006

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

PyFin/tests/Math/Accumulators/testStatefulAccumulators.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -904,7 +904,7 @@ def testTimeMovingCountUniqueRight(self):
904904
window = 60
905905
mat = TimeMovingCountUnique(window, Latest("x"), "right")
906906
values = np.random.randint(0, 10, 2500)
907-
stamps = np.cumsum(np.random.randint(0, 10, 2500))
907+
stamps = np.cumsum(np.random.randint(1, 10, 2500))
908908
for i, (value, stamp) in enumerate(zip(values, stamps)):
909909
mat.push(dict(x=value, stamp=stamp))
910910

@@ -920,7 +920,7 @@ def testTimeMovingCountUniqueBoth(self):
920920
window = 60
921921
mat = TimeMovingCountUnique(window, Latest("x"), "both")
922922
values = np.random.randint(0, 10, 2500)
923-
stamps = np.cumsum(np.random.randint(0, 5, 2500))
923+
stamps = np.cumsum(np.random.randint(1, 5, 2500))
924924
for i, (value, stamp) in enumerate(zip(values, stamps)):
925925
mat.push(dict(x=value, stamp=stamp))
926926

@@ -932,6 +932,38 @@ def testTimeMovingCountUniqueBoth(self):
932932
"expected: {1}\n"
933933
"calculated: {2}".format(i, expected, calculated))
934934

935+
def testTimeMovingCountUniqueLeft(self):
936+
window = 60
937+
mat = TimeMovingCountUnique(window, Latest("x"), "left")
938+
values = np.random.randint(0, 10, 2500)
939+
stamps = np.cumsum(np.random.randint(1, 5, 2500))
940+
for i, (value, stamp) in enumerate(zip(values, stamps)):
941+
mat.push(dict(x=value, stamp=stamp))
942+
943+
calculated = mat.result()
944+
time_diff = (stamp - stamps) <= window
945+
time_diff[i:] = False
946+
expected = len(np.unique(values[time_diff]))
947+
self.assertEqual(calculated, expected, "at index {0}\n"
948+
"expected: {1}\n"
949+
"calculated: {2}".format(i, expected, calculated))
950+
951+
def testTimeMovingCountUniqueNeither(self):
952+
window = 60
953+
mat = TimeMovingCountUnique(window, Latest("x"), "neither")
954+
values = np.random.randint(0, 10, 2500)
955+
stamps = np.cumsum(np.random.randint(1, 5, 2500))
956+
for i, (value, stamp) in enumerate(zip(values, stamps)):
957+
mat.push(dict(x=value, stamp=stamp))
958+
959+
calculated = mat.result()
960+
time_diff = (stamp - stamps) < window
961+
time_diff[i:] = False
962+
expected = len(np.unique(values[time_diff]))
963+
self.assertEqual(calculated, expected, "at index {0}\n"
964+
"expected: {1}\n"
965+
"calculated: {2}".format(i, expected, calculated))
966+
935967
def testMovingAllTrue(self):
936968
window = 3
937969
underlying = Latest('x') > 0.

0 commit comments

Comments
 (0)