From 130a0e00990f13e53b6073126f7781c5e8a5b8f9 Mon Sep 17 00:00:00 2001 From: Ian Spektor Date: Tue, 31 Oct 2023 17:24:20 +0100 Subject: [PATCH 1/7] restructure since_last tests --- temporian/core/operators/test/BUILD | 11 ++ .../core/operators/test/test_since_last.py | 102 +++++++++++ .../implementation/numpy/operators/test/BUILD | 19 -- .../numpy/operators/test/since_last_test.py | 163 ------------------ 4 files changed, 113 insertions(+), 182 deletions(-) create mode 100644 temporian/core/operators/test/test_since_last.py delete mode 100644 temporian/implementation/numpy/operators/test/since_last_test.py diff --git a/temporian/core/operators/test/BUILD b/temporian/core/operators/test/BUILD index e856364fb..f68936ec1 100644 --- a/temporian/core/operators/test/BUILD +++ b/temporian/core/operators/test/BUILD @@ -162,6 +162,17 @@ py_test( ], ) +py_test( + name = "test_since_last", + srcs = ["test_since_last.py"], + srcs_version = "PY3", + deps = [ + # already_there/absl/testing:absltest + "//temporian/implementation/numpy/data:io", + "//temporian/test:utils", + ], +) + py_test( name = "test_until_next", srcs = ["test_until_next.py"], diff --git a/temporian/core/operators/test/test_since_last.py b/temporian/core/operators/test/test_since_last.py new file mode 100644 index 000000000..389db844e --- /dev/null +++ b/temporian/core/operators/test/test_since_last.py @@ -0,0 +1,102 @@ +# Copyright 2021 Google LLC. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from math import nan + +from absl.testing import absltest +from absl.testing.parameterized import TestCase + +from temporian.implementation.numpy.data.io import event_set +from temporian.test.utils import assertOperatorResult + + +class SinceLastTest(TestCase): + def test_no_sampling(self): + ts = [1, 5, 8, 9, 1, 1, 2] + x = [1, 1, 1, 1, 2, 2, 2] + evset = event_set( + timestamps=ts, + features={"x": x}, + indexes=["x"], + ) + + result = evset.since_last() + + expected = event_set( + timestamps=ts, + features={"x": x, "since_last": [nan, 4, 3, 1, nan, 0, 1]}, + indexes=["x"], + same_sampling_as=evset, + ) + + assertOperatorResult(self, result, expected) + + def test_no_sampling_2steps(self): + ts = [1, 5, 8, 9, 1, 1, 2, 2, 2] + x = [1, 1, 1, 1, 2, 2, 2, 2, 2] + evset = event_set( + timestamps=ts, + features={"x": x}, + indexes=["x"], + ) + + result = evset.since_last(steps=2) + + expected = event_set( + timestamps=ts, + features={ + "x": x, + "since_last": [nan, nan, 7, 4, nan, nan, 1, 1, 0], + }, + indexes=["x"], + same_sampling_as=evset, + ) + + assertOperatorResult(self, result, expected) + + def test_with_sampling(self): + sampling_ts = [-1, 1, 1.5, 2, 2.1, 4, 5] + + evset = event_set(timestamps=[1, 2, 2, 4]) + sampling = event_set(timestamps=sampling_ts) + + result = evset.since_last(sampling=sampling) + + expected = event_set( + timestamps=sampling_ts, + features={"since_last": [nan, 0, 0.5, 0, 0.1, 0, 1]}, + same_sampling_as=sampling, + ) + + assertOperatorResult(self, result, expected) + + def test_with_sampling_2steps(self): + sampling_ts = [-1, 1, 1.5, 2, 2, 2.1, 4, 5] + + evset = event_set(timestamps=[1, 2, 2, 4]) + sampling = event_set(timestamps=sampling_ts) + + result = evset.since_last(sampling=sampling, steps=2) + + expected = event_set( + timestamps=sampling_ts, + features={"since_last": [nan, nan, nan, 0, 0, 0.1, 2, 3]}, + same_sampling_as=sampling, + ) + + assertOperatorResult(self, result, expected) + + +if __name__ == "__main__": + absltest.main() diff --git a/temporian/implementation/numpy/operators/test/BUILD b/temporian/implementation/numpy/operators/test/BUILD index 75ed9b169..de5ddb7ff 100644 --- a/temporian/implementation/numpy/operators/test/BUILD +++ b/temporian/implementation/numpy/operators/test/BUILD @@ -149,25 +149,6 @@ py_test( ], ) -py_test( - name = "since_last_test", - srcs = ["since_last_test.py"], - srcs_version = "PY3", - deps = [ - # already_there/absl/testing:absltest - # already_there/pandas - # already_there/numpy - "//temporian/io:pandas", - ":utils", - "//temporian/core/data:dtype", - "//temporian/core/data:node", - "//temporian/core/data:schema", - "//temporian/core/operators:since_last", - "//temporian/implementation/numpy/data:io", - "//temporian/implementation/numpy/operators:since_last", - ], -) - py_test( name = "begin_test", srcs = ["begin_test.py"], diff --git a/temporian/implementation/numpy/operators/test/since_last_test.py b/temporian/implementation/numpy/operators/test/since_last_test.py deleted file mode 100644 index 2a0b69ef7..000000000 --- a/temporian/implementation/numpy/operators/test/since_last_test.py +++ /dev/null @@ -1,163 +0,0 @@ -# Copyright 2021 Google LLC. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import math -from absl.testing import absltest - -import pandas as pd - -from temporian.core.operators.since_last import SinceLast -from temporian.implementation.numpy.operators.since_last import ( - SinceLastNumpyImplementation, -) -from temporian.io.pandas import from_pandas - -nan = math.nan - - -class SinceLastOperatorTest(absltest.TestCase): - def setUp(self): - pass - - def test_no_sampling(self): - event_data = from_pandas( - pd.DataFrame( - { - "timestamp": [1, 5, 8, 9, 1, 1, 2], - "x": [1, 1, 1, 1, 2, 2, 2], - } - ), - indexes=["x"], - ) - event = event_data.node() - - expected_output = from_pandas( - pd.DataFrame( - { - "timestamp": [1, 5, 8, 9, 1, 1, 2], - "x": [1, 1, 1, 1, 2, 2, 2], - "since_last": [nan, 4, 3, 1, nan, 0, 1], - } - ), - indexes=["x"], - ) - - # Run op - op = SinceLast(input=event, steps=1) - op.outputs["output"].check_same_sampling(event) - - instance = SinceLastNumpyImplementation(op) - output = instance.call(input=event_data)["output"] - - self.assertEqual(output, expected_output) - - def test_no_sampling_2steps(self): - steps = 2 - event_data = from_pandas( - pd.DataFrame( - { - "timestamp": [1, 5, 8, 9, 1, 1, 2, 2, 2], - "x": [1, 1, 1, 1, 2, 2, 2, 2, 2], - } - ), - indexes=["x"], - ) - event = event_data.node() - - expected_output = from_pandas( - pd.DataFrame( - { - "timestamp": [1, 5, 8, 9, 1, 1, 2, 2, 2], - "x": [1, 1, 1, 1, 2, 2, 2, 2, 2], - "since_last": [nan, nan, 7, 4, nan, nan, 1, 1, 0], - } - ), - indexes=["x"], - ) - - # Run op - op = SinceLast(input=event, steps=steps) - op.outputs["output"].check_same_sampling(event) - - instance = SinceLastNumpyImplementation(op) - output = instance.call(input=event_data)["output"] - - self.assertEqual(output, expected_output) - - def test_with_sampling(self): - event_data = from_pandas( - pd.DataFrame({"timestamp": [1, 2, 2, 4]}), - ) - event = event_data.node() - - sampling_data = from_pandas( - pd.DataFrame({"timestamp": [-1, 1, 1.5, 2, 2.1, 4, 5]}) - ) - sampling = sampling_data.node() - - expected_output = from_pandas( - pd.DataFrame( - { - "timestamp": [-1, 1, 1.5, 2, 2.1, 4, 5], - "since_last": [nan, 0, 0.5, 0, 0.1, 0, 1], - } - ) - ) - - # Run op - op = SinceLast(input=event, steps=1, sampling=sampling) - op.outputs["output"].check_same_sampling(sampling) - - instance = SinceLastNumpyImplementation(op) - output = instance.call(input=event_data, sampling=sampling_data)[ - "output" - ] - - self.assertEqual(output, expected_output) - - def test_with_sampling_2steps(self): - steps = 2 - event_data = from_pandas( - pd.DataFrame({"timestamp": [1, 2, 2, 4]}), - ) - event = event_data.node() - - sampling_data = from_pandas( - pd.DataFrame({"timestamp": [-1, 1, 1.5, 2, 2, 2.1, 4, 5]}) - ) - sampling = sampling_data.node() - - expected_output = from_pandas( - pd.DataFrame( - { - "timestamp": [-1, 1, 1.5, 2, 2, 2.1, 4, 5], - "since_last": [nan, nan, nan, 0, 0, 0.1, 2, 3], - } - ) - ) - - # Run op - op = SinceLast(input=event, steps=steps, sampling=sampling) - op.outputs["output"].check_same_sampling(sampling) - - instance = SinceLastNumpyImplementation(op) - output = instance.call(input=event_data, sampling=sampling_data)[ - "output" - ] - - self.assertEqual(output, expected_output) - - -if __name__ == "__main__": - absltest.main() From 6b2b18eccbe8a15cf537d0c26f4f9827e4634960 Mon Sep 17 00:00:00 2001 From: Ian Spektor Date: Tue, 31 Oct 2023 17:27:58 +0100 Subject: [PATCH 2/7] restructure unique_timestamps tests --- temporian/core/operators/test/BUILD | 11 ++++ .../operators/test/test_unique_timestamps.py | 45 ++++++++++++++ temporian/core/test/event_set_ops_test.py | 8 --- .../implementation/numpy/operators/test/BUILD | 15 ----- .../operators/test/unique_timestamps_test.py | 62 ------------------- 5 files changed, 56 insertions(+), 85 deletions(-) create mode 100644 temporian/core/operators/test/test_unique_timestamps.py delete mode 100644 temporian/implementation/numpy/operators/test/unique_timestamps_test.py diff --git a/temporian/core/operators/test/BUILD b/temporian/core/operators/test/BUILD index f68936ec1..a351e49a9 100644 --- a/temporian/core/operators/test/BUILD +++ b/temporian/core/operators/test/BUILD @@ -173,6 +173,17 @@ py_test( ], ) +py_test( + name = "test_unique_timestamps", + srcs = ["test_unique_timestamps.py"], + srcs_version = "PY3", + deps = [ + # already_there/absl/testing:absltest + "//temporian/implementation/numpy/data:io", + "//temporian/test:utils", + ], +) + py_test( name = "test_until_next", srcs = ["test_until_next.py"], diff --git a/temporian/core/operators/test/test_unique_timestamps.py b/temporian/core/operators/test/test_unique_timestamps.py new file mode 100644 index 000000000..0b92d1ad9 --- /dev/null +++ b/temporian/core/operators/test/test_unique_timestamps.py @@ -0,0 +1,45 @@ +# Copyright 2021 Google LLC. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from absl.testing import absltest +from absl.testing.parameterized import TestCase + +from temporian.implementation.numpy.data.io import event_set +from temporian.test.utils import assertOperatorResult + + +class UniqueTimestampsTest(TestCase): + def test_basic(self): + evset = event_set( + timestamps=[1, 2, 2, 2, 3, 3, 3, 4], + features={ + "a": [1, 2, 3, 4, 5, 6, 7, 8], + "c": [1, 1, 1, 1, 1, 2, 2, 2], + }, + indexes=["c"], + ) + + result = evset.unique_timestamps() + + expected = event_set( + timestamps=[1, 2, 3, 3, 4], + features={"c": [1, 1, 1, 2, 2]}, + indexes=["c"], + ) + + assertOperatorResult(self, result, expected, check_sampling=False) + + +if __name__ == "__main__": + absltest.main() diff --git a/temporian/core/test/event_set_ops_test.py b/temporian/core/test/event_set_ops_test.py index bb1593f73..87c6871dc 100644 --- a/temporian/core/test/event_set_ops_test.py +++ b/temporian/core/test/event_set_ops_test.py @@ -97,10 +97,6 @@ def test_set_index(self): self.assertTrue(isinstance(self.evset.set_index("a"), EventSet)) self.assertTrue(isinstance(self.node.set_index("a"), EventSetNode)) - def test_since_last(self): - self.assertTrue(isinstance(self.evset.since_last(), EventSet)) - self.assertTrue(isinstance(self.node.since_last(), EventSetNode)) - def test_tick(self): self.assertTrue(isinstance(self.evset.tick(1), EventSet)) self.assertTrue(isinstance(self.node.tick(1), EventSetNode)) @@ -109,10 +105,6 @@ def test_timestamps(self): self.assertTrue(isinstance(self.evset.timestamps(), EventSet)) self.assertTrue(isinstance(self.node.timestamps(), EventSetNode)) - def test_unique_timestamps(self): - self.assertTrue(isinstance(self.evset.unique_timestamps(), EventSet)) - self.assertTrue(isinstance(self.node.unique_timestamps(), EventSetNode)) - def test_filter_moving_count(self): self.assertTrue(isinstance(self.evset.filter_moving_count(5), EventSet)) self.assertTrue( diff --git a/temporian/implementation/numpy/operators/test/BUILD b/temporian/implementation/numpy/operators/test/BUILD index de5ddb7ff..c61e97d36 100644 --- a/temporian/implementation/numpy/operators/test/BUILD +++ b/temporian/implementation/numpy/operators/test/BUILD @@ -134,21 +134,6 @@ py_test( ], ) -py_test( - name = "unique_timestamps_test", - srcs = ["unique_timestamps_test.py"], - srcs_version = "PY3", - deps = [ - # already_there/absl/testing:absltest - # already_there/pandas - # already_there/numpy - "//temporian/io:pandas", - "//temporian/core/operators:unique_timestamps", - "//temporian/implementation/numpy/data:io", - "//temporian/implementation/numpy/operators:unique_timestamps", - ], -) - py_test( name = "begin_test", srcs = ["begin_test.py"], diff --git a/temporian/implementation/numpy/operators/test/unique_timestamps_test.py b/temporian/implementation/numpy/operators/test/unique_timestamps_test.py deleted file mode 100644 index 9b5e257e1..000000000 --- a/temporian/implementation/numpy/operators/test/unique_timestamps_test.py +++ /dev/null @@ -1,62 +0,0 @@ -# Copyright 2021 Google LLC. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - - -from absl.testing import absltest - -import pandas as pd -from temporian.core.operators.unique_timestamps import UniqueTimestamps -from temporian.implementation.numpy.operators.unique_timestamps import ( - UniqueTimestampsNumpyImplementation, -) -from temporian.io.pandas import from_pandas - - -class UniqueTimestampsOperatorTest(absltest.TestCase): - def setUp(self): - pass - - def test_base(self): - evset = from_pandas( - pd.DataFrame( - { - "timestamp": [1, 2, 2, 2, 3, 3, 3, 4], - "a": [1, 2, 3, 4, 5, 6, 7, 8], - "c": [1, 1, 1, 1, 1, 2, 2, 2], - } - ), - indexes=["c"], - ) - node = evset.node() - - expected_output = from_pandas( - pd.DataFrame( - { - "timestamp": [1, 2, 3, 3, 4], - "c": [1, 1, 1, 2, 2], - } - ), - indexes=["c"], - ) - - # Run op - op = UniqueTimestamps(input=node) - instance = UniqueTimestampsNumpyImplementation(op) - output = instance.call(input=evset)["output"] - - self.assertEqual(output, expected_output) - - -if __name__ == "__main__": - absltest.main() From d0f3bf38217909e4ad18a7d7bf9a5b7edaf671e3 Mon Sep 17 00:00:00 2001 From: Ian Spektor Date: Tue, 31 Oct 2023 17:32:25 +0100 Subject: [PATCH 3/7] restructure tick tests --- temporian/core/operators/test/BUILD | 11 ++ temporian/core/operators/test/test_tick.py | 77 +++++++++++++ .../implementation/numpy/operators/test/BUILD | 16 --- .../numpy/operators/test/tick_test.py | 102 ------------------ 4 files changed, 88 insertions(+), 118 deletions(-) create mode 100644 temporian/core/operators/test/test_tick.py delete mode 100644 temporian/implementation/numpy/operators/test/tick_test.py diff --git a/temporian/core/operators/test/BUILD b/temporian/core/operators/test/BUILD index a351e49a9..8db0f142a 100644 --- a/temporian/core/operators/test/BUILD +++ b/temporian/core/operators/test/BUILD @@ -173,6 +173,17 @@ py_test( ], ) +py_test( + name = "test_tick", + srcs = ["test_tick.py"], + srcs_version = "PY3", + deps = [ + # already_there/absl/testing:absltest + "//temporian/implementation/numpy/data:io", + "//temporian/test:utils", + ], +) + py_test( name = "test_unique_timestamps", srcs = ["test_unique_timestamps.py"], diff --git a/temporian/core/operators/test/test_tick.py b/temporian/core/operators/test/test_tick.py new file mode 100644 index 000000000..3e0fd8904 --- /dev/null +++ b/temporian/core/operators/test/test_tick.py @@ -0,0 +1,77 @@ +# Copyright 2021 Google LLC. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from absl.testing import absltest +from absl.testing.parameterized import TestCase + +from temporian.implementation.numpy.data.io import event_set +from temporian.test.utils import assertOperatorResult + + +class TickTest(TestCase): + def test_no_align(self): + evset = event_set([1, 5.5]) + result = evset.tick(4.0, align=False) + expected = event_set([1, 5]) + + assertOperatorResult(self, result, expected, check_sampling=False) + + def test_on_the_spot(self): + evset = event_set([0, 4, 8]) + result = evset.tick(4.0, align=False) + expected_output = event_set([0, 4, 8]) + + assertOperatorResult( + self, result, expected_output, check_sampling=False + ) + + def test_align(self): + evset = event_set([1, 5.5, 8.1]) + expected_output = event_set([4, 8]) + result = evset.tick(4.0, align=True) + + assertOperatorResult( + self, result, expected_output, check_sampling=False + ) + + def test_empty(self): + evset = event_set([]) + expected_output = event_set([]) + result = evset.tick(4.0, align=False) + + assertOperatorResult( + self, result, expected_output, check_sampling=False + ) + + def test_no_align_single(self): + evset = event_set([1]) + expected_output = event_set([1]) + result = evset.tick(4.0, align=False) + + assertOperatorResult( + self, result, expected_output, check_sampling=False + ) + + def test_align_single(self): + evset = event_set([1]) + expected_output = event_set([]) + result = evset.tick(4.0, align=True) + + assertOperatorResult( + self, result, expected_output, check_sampling=False + ) + + +if __name__ == "__main__": + absltest.main() diff --git a/temporian/implementation/numpy/operators/test/BUILD b/temporian/implementation/numpy/operators/test/BUILD index c61e97d36..1b051bc84 100644 --- a/temporian/implementation/numpy/operators/test/BUILD +++ b/temporian/implementation/numpy/operators/test/BUILD @@ -166,22 +166,6 @@ py_test( ], ) -py_test( - name = "tick_test", - srcs = ["tick_test.py"], - srcs_version = "PY3", - deps = [ - # already_there/absl/testing:absltest - ":utils", - "//temporian/core/data:dtype", - "//temporian/core/data:node", - "//temporian/core/data:schema", - "//temporian/implementation/numpy/data:io", - "//temporian/core/operators:tick", - "//temporian/implementation/numpy/operators:tick", - ], -) - py_test( name = "join_test", srcs = ["join_test.py"], diff --git a/temporian/implementation/numpy/operators/test/tick_test.py b/temporian/implementation/numpy/operators/test/tick_test.py deleted file mode 100644 index 4662015b9..000000000 --- a/temporian/implementation/numpy/operators/test/tick_test.py +++ /dev/null @@ -1,102 +0,0 @@ -# Copyright 2021 Google LLC. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - - -from absl.testing import absltest - -import numpy as np -from temporian.core.operators.tick import Tick -from temporian.implementation.numpy.data.io import event_set -from temporian.implementation.numpy.operators.tick import ( - TickNumpyImplementation, -) -from temporian.implementation.numpy.operators.test.utils import ( - assertEqualEventSet, - testOperatorAndImp, -) - - -class TickOperatorTest(absltest.TestCase): - def setUp(self): - pass - - def test_no_align(self): - evset = event_set([1, 5.5]) - expected_output = event_set([1, 5]) - - op = Tick(input=evset.node(), interval=4.0, align=False) - instance = TickNumpyImplementation(op) - testOperatorAndImp(self, op, instance) - output = instance.call(input=evset)["output"] - - assertEqualEventSet(self, output, expected_output) - - def test_on_the_spot(self): - evset = event_set([0, 4, 8]) - expected_output = event_set([0, 4, 8]) - - op = Tick(input=evset.node(), interval=4.0, align=False) - instance = TickNumpyImplementation(op) - testOperatorAndImp(self, op, instance) - output = instance.call(input=evset)["output"] - - assertEqualEventSet(self, output, expected_output) - - def test_align(self): - evset = event_set([1, 5.5, 8.1]) - expected_output = event_set([4, 8]) - - op = Tick(input=evset.node(), interval=4.0, align=True) - instance = TickNumpyImplementation(op) - testOperatorAndImp(self, op, instance) - output = instance.call(input=evset)["output"] - - assertEqualEventSet(self, output, expected_output) - - def test_empty(self): - evset = event_set([]) - expected_output = event_set([]) - - op = Tick(input=evset.node(), interval=4.0, align=False) - instance = TickNumpyImplementation(op) - testOperatorAndImp(self, op, instance) - output = instance.call(input=evset)["output"] - - assertEqualEventSet(self, output, expected_output) - - def test_no_align_single(self): - evset = event_set([1]) - expected_output = event_set([1]) - - op = Tick(input=evset.node(), interval=4.0, align=False) - instance = TickNumpyImplementation(op) - testOperatorAndImp(self, op, instance) - output = instance.call(input=evset)["output"] - - assertEqualEventSet(self, output, expected_output) - - def test_align_single(self): - evset = event_set([1]) - expected_output = event_set([]) - - op = Tick(input=evset.node(), interval=4.0, align=True) - instance = TickNumpyImplementation(op) - testOperatorAndImp(self, op, instance) - output = instance.call(input=evset)["output"] - - assertEqualEventSet(self, output, expected_output) - - -if __name__ == "__main__": - absltest.main() From 31aafadaddfbf5e7f5962f8f5d0fee9d8f4fb8de Mon Sep 17 00:00:00 2001 From: Ian Spektor Date: Tue, 31 Oct 2023 17:45:21 +0100 Subject: [PATCH 4/7] restructure join tests --- temporian/core/operators/test/BUILD | 23 ++-- temporian/core/operators/test/join_test.py | 72 ----------- temporian/core/operators/test/test_join.py | 120 ++++++++++++++++++ temporian/core/test/event_set_ops_test.py | 10 -- .../implementation/numpy/operators/test/BUILD | 16 --- .../numpy/operators/test/join_test.py | 95 -------------- 6 files changed, 131 insertions(+), 205 deletions(-) delete mode 100644 temporian/core/operators/test/join_test.py create mode 100644 temporian/core/operators/test/test_join.py delete mode 100644 temporian/implementation/numpy/operators/test/join_test.py diff --git a/temporian/core/operators/test/BUILD b/temporian/core/operators/test/BUILD index 8db0f142a..6297dd94d 100644 --- a/temporian/core/operators/test/BUILD +++ b/temporian/core/operators/test/BUILD @@ -6,18 +6,6 @@ package( # Tests # ===== -py_test( - name = "join_test", - srcs = ["join_test.py"], - srcs_version = "PY3", - deps = [ - # already_there/absl/testing:absltest - "//temporian/core/data:dtype", - "//temporian/core/data:node", - "//temporian/core/operators:join", - ], -) - py_test( name = "fft_test", srcs = ["fft_test.py"], @@ -63,6 +51,17 @@ py_test( ], ) +py_test( + name = "test_join", + srcs = ["test_join.py"], + srcs_version = "PY3", + deps = [ + # already_there/absl/testing:absltest + "//temporian/implementation/numpy/data:io", + "//temporian/test:utils", + ], +) + py_test( name = "test_lag", srcs = ["test_lag.py"], diff --git a/temporian/core/operators/test/join_test.py b/temporian/core/operators/test/join_test.py deleted file mode 100644 index 335d395c9..000000000 --- a/temporian/core/operators/test/join_test.py +++ /dev/null @@ -1,72 +0,0 @@ -# Copyright 2021 Google LLC. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -from absl.testing import absltest - -from temporian.core.data.node import input_node -from temporian.core.data.dtype import DType -from temporian.core.operators.join import join - - -class JoinOperatorTest(absltest.TestCase): - def setUp(self): - pass - - def test_left(self): - input_1 = input_node([("a", DType.FLOAT64)]) - input_2 = input_node([("b", DType.FLOAT64)]) - _ = join(input_1, input_2) - - def test_left_on(self): - input_1 = input_node([("a", DType.FLOAT64), ("c", DType.INT64)]) - input_2 = input_node([("b", DType.FLOAT64), ("c", DType.INT64)]) - _ = join(input_1, input_2, on="c") - - def test_duplicated_feature(self): - input_1 = input_node([("a", DType.FLOAT64)]) - input_2 = input_node([("a", DType.FLOAT64)]) - with self.assertRaisesRegex(ValueError, "is defined in both inputs"): - _ = join(input_1, input_2) - - def test_wrong_index(self): - input_1 = input_node([("a", DType.FLOAT64)]) - input_2 = input_node( - [("b", DType.FLOAT64)], indexes=[("x", DType.STRING)] - ) - with self.assertRaisesRegex( - ValueError, "Arguments don't have the same index" - ): - _ = join(input_1, input_2) - - def test_wrong_join(self): - input_1 = input_node([("a", DType.FLOAT64)]) - input_2 = input_node([("b", DType.FLOAT64)]) - with self.assertRaisesRegex(ValueError, "Non supported join type"): - _ = join(input_1, input_2, how="non existing join") - - def test_missing_on(self): - input_1 = input_node([("a", DType.FLOAT64)]) - input_2 = input_node([("b", DType.FLOAT64)]) - with self.assertRaisesRegex(ValueError, "does not exist in left"): - _ = join(input_1, input_2, on="c") - - def test_wrong_on_type(self): - input_1 = input_node([("a", DType.FLOAT64), ("c", DType.FLOAT64)]) - input_2 = input_node([("b", DType.FLOAT64)]) - with self.assertRaisesRegex(ValueError, "Got float64 instead for left"): - _ = join(input_1, input_2, on="c") - - -if __name__ == "__main__": - absltest.main() diff --git a/temporian/core/operators/test/test_join.py b/temporian/core/operators/test/test_join.py new file mode 100644 index 000000000..2493e228c --- /dev/null +++ b/temporian/core/operators/test/test_join.py @@ -0,0 +1,120 @@ +# Copyright 2021 Google LLC. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import math + +from absl.testing import absltest +from absl.testing.parameterized import TestCase + +from temporian.implementation.numpy.data.io import event_set +from temporian.test.utils import assertOperatorResult, f64 + + +class JoinTest(TestCase): + def test_base(self): + evset_left = event_set( + timestamps=[1, 2, 3, 5, 5, 6, 6], + features={"a": [11, 12, 13, 14, 15, 16, 17]}, + ) + evset_right = event_set( + timestamps=[1, 2, 4, 5, 5], + features={"b": [21.0, 22.0, 23.0, 24.0, 25.0]}, + ) + + result = evset_left.join(evset_right) + + expected = event_set( + timestamps=[1, 2, 3, 5, 5, 6, 6], + features={ + "a": [11, 12, 13, 14, 15, 16, 17], + "b": [21.0, 22.0, math.nan, 24.0, 24.0, math.nan, math.nan], + }, + ) + + assertOperatorResult(self, result, expected, check_sampling=False) + + def test_base_on(self): + evset_left = event_set( + timestamps=[1, 2, 2, 3, 4, 5], + features={ + "a": [11, 12, 13, 14, 15, 16], + "c": [0, 1, 2, 3, 4, 5], + }, + ) + evset_right = event_set( + timestamps=[1, 2, 2, 3, 4], + features={ + "c": [0, 2, 1, 3, 5], + "b": [11.0, 12.0, 13.0, 14.0, 15.0], + }, + ) + + result = evset_left.join(evset_right, on="c") + + expected = event_set( + timestamps=[1, 2, 2, 3, 4, 5], + features={ + "a": [11, 12, 13, 14, 15, 16], + "c": [0, 1, 2, 3, 4, 5], + "b": [11.0, 13.0, 12.0, 14.0, math.nan, math.nan], + }, + ) + + assertOperatorResult(self, result, expected, check_sampling=False) + + def test_left(self): + evset_1 = event_set([0], features={"a": [0]}) + evset_2 = event_set([0], features={"b": [0]}) + evset_1.join(evset_2) + + def test_left_on(self): + evset_1 = event_set([0], {"a": [0], "x": [0]}) + evset_2 = event_set([0], {"b": [0], "x": [0]}) + evset_1.join(evset_2, on="x") + + def test_duplicated_feature(self): + evset_1 = event_set([0], features={"a": [0]}) + evset_2 = event_set([0], features={"a": [0]}) + with self.assertRaisesRegex(ValueError, "is defined in both inputs"): + evset_1.join(evset_2) + + def test_wrong_index(self): + evset_1 = event_set([0], features={"a": [0]}) + evset_2 = event_set([0], features={"b": [0], "x": ["x"]}, indexes=["x"]) + with self.assertRaisesRegex( + ValueError, "Arguments don't have the same index" + ): + evset_1.join(evset_2) + + def test_wrong_join(self): + evset_1 = event_set([0], features={"a": [0]}) + evset_2 = event_set([0], features={"b": [0]}) + with self.assertRaisesRegex(ValueError, "Non supported join type"): + evset_1.join(evset_2, how="non existing join") + + def test_missing_on(self): + evset_1 = event_set([0], features={"a": [0]}) + evset_2 = event_set([0], features={"b": [0]}) + with self.assertRaisesRegex(ValueError, "does not exist in left"): + evset_1.join(evset_2, on="c") + + def test_wrong_on_type(self): + evset_1 = event_set([0], features={"a": [0], "c": f64([0])}) + evset_2 = event_set([0], features={"b": [0]}) + with self.assertRaisesRegex(ValueError, "Got float64 instead for left"): + evset_1.join(evset_2, on="c") + + +if __name__ == "__main__": + absltest.main() diff --git a/temporian/core/test/event_set_ops_test.py b/temporian/core/test/event_set_ops_test.py index 87c6871dc..5173e0f90 100644 --- a/temporian/core/test/event_set_ops_test.py +++ b/temporian/core/test/event_set_ops_test.py @@ -83,12 +83,6 @@ def test_fast_fourier_transform(self): ) ) - def test_join(self): - self.assertTrue(isinstance(self.evset.join(self.other_evset), EventSet)) - self.assertTrue( - isinstance(self.node.join(self.other_node), EventSetNode) - ) - def test_select(self): self.assertTrue(isinstance(self.evset.select("a"), EventSet)) self.assertTrue(isinstance(self.node.select("a"), EventSetNode)) @@ -97,10 +91,6 @@ def test_set_index(self): self.assertTrue(isinstance(self.evset.set_index("a"), EventSet)) self.assertTrue(isinstance(self.node.set_index("a"), EventSetNode)) - def test_tick(self): - self.assertTrue(isinstance(self.evset.tick(1), EventSet)) - self.assertTrue(isinstance(self.node.tick(1), EventSetNode)) - def test_timestamps(self): self.assertTrue(isinstance(self.evset.timestamps(), EventSet)) self.assertTrue(isinstance(self.node.timestamps(), EventSetNode)) diff --git a/temporian/implementation/numpy/operators/test/BUILD b/temporian/implementation/numpy/operators/test/BUILD index 1b051bc84..8d5b99f15 100644 --- a/temporian/implementation/numpy/operators/test/BUILD +++ b/temporian/implementation/numpy/operators/test/BUILD @@ -166,22 +166,6 @@ py_test( ], ) -py_test( - name = "join_test", - srcs = ["join_test.py"], - srcs_version = "PY3", - deps = [ - # already_there/absl/testing:absltest - ":utils", - "//temporian/core/data:dtype", - "//temporian/core/data:node", - "//temporian/core/data:schema", - "//temporian/implementation/numpy/data:io", - "//temporian/core/operators:join", - "//temporian/implementation/numpy/operators:join", - ], -) - py_test( name = "timestamps_test", srcs = ["timestamps_test.py"], diff --git a/temporian/implementation/numpy/operators/test/join_test.py b/temporian/implementation/numpy/operators/test/join_test.py deleted file mode 100644 index 55e52f870..000000000 --- a/temporian/implementation/numpy/operators/test/join_test.py +++ /dev/null @@ -1,95 +0,0 @@ -# Copyright 2021 Google LLC. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import math - -from absl.testing import absltest - -from temporian.core.operators.join import Join -from temporian.implementation.numpy.data.io import event_set -from temporian.implementation.numpy.operators.join import ( - JoinNumpyImplementation, -) -from temporian.implementation.numpy.operators.test.utils import ( - assertEqualEventSet, - testOperatorAndImp, -) - - -class JoinOperatorTest(absltest.TestCase): - def setUp(self): - pass - - def test_base(self): - evset_left = event_set( - timestamps=[1, 2, 3, 5, 5, 6, 6], - features={"a": [11, 12, 13, 14, 15, 16, 17]}, - ) - evset_right = event_set( - timestamps=[1, 2, 4, 5, 5], - features={"b": [21.0, 22.0, 23.0, 24.0, 25.0]}, - ) - - expected_output = event_set( - timestamps=[1, 2, 3, 5, 5, 6, 6], - features={ - "a": [11, 12, 13, 14, 15, 16, 17], - "b": [21.0, 22.0, math.nan, 24.0, 24.0, math.nan, math.nan], - }, - ) - - # Run op - op = Join(left=evset_left.node(), right=evset_right.node()) - instance = JoinNumpyImplementation(op) - testOperatorAndImp(self, op, instance) - output = instance.call(left=evset_left, right=evset_right)["output"] - - assertEqualEventSet(self, output, expected_output) - - def test_base_on(self): - evset_left = event_set( - timestamps=[1, 2, 2, 3, 4, 5], - features={ - "a": [11, 12, 13, 14, 15, 16], - "c": [0, 1, 2, 3, 4, 5], - }, - ) - evset_right = event_set( - timestamps=[1, 2, 2, 3, 4], - features={ - "c": [0, 2, 1, 3, 5], - "b": [11.0, 12.0, 13.0, 14.0, 15.0], - }, - ) - - expected_output = event_set( - timestamps=[1, 2, 2, 3, 4, 5], - features={ - "a": [11, 12, 13, 14, 15, 16], - "c": [0, 1, 2, 3, 4, 5], - "b": [11.0, 13.0, 12.0, 14.0, math.nan, math.nan], - }, - ) - - # Run op - op = Join(left=evset_left.node(), right=evset_right.node(), on="c") - instance = JoinNumpyImplementation(op) - testOperatorAndImp(self, op, instance) - output = instance.call(left=evset_left, right=evset_right)["output"] - - assertEqualEventSet(self, output, expected_output) - - -if __name__ == "__main__": - absltest.main() From fea4276a1b07567079f15ede09817de6c72b79ef Mon Sep 17 00:00:00 2001 From: Ian Spektor Date: Tue, 31 Oct 2023 17:58:39 +0100 Subject: [PATCH 5/7] restructure fft tests --- temporian/core/operators/test/BUILD | 24 ++-- temporian/core/operators/test/fft_test.py | 63 ---------- .../test/test_fast_fourier_transform.py | 111 ++++++++++++++++++ temporian/core/test/event_set_ops_test.py | 44 +++---- .../implementation/numpy/operators/test/BUILD | 17 --- .../numpy/operators/test/fft_test.py | 85 -------------- 6 files changed, 142 insertions(+), 202 deletions(-) delete mode 100644 temporian/core/operators/test/fft_test.py create mode 100644 temporian/core/operators/test/test_fast_fourier_transform.py delete mode 100644 temporian/implementation/numpy/operators/test/fft_test.py diff --git a/temporian/core/operators/test/BUILD b/temporian/core/operators/test/BUILD index 6297dd94d..78f6a6eed 100644 --- a/temporian/core/operators/test/BUILD +++ b/temporian/core/operators/test/BUILD @@ -7,20 +7,30 @@ package( # ===== py_test( - name = "fft_test", - srcs = ["fft_test.py"], + name = "test_add_index", + srcs = ["test_add_index.py"], srcs_version = "PY3", deps = [ # already_there/absl/testing:absltest - "//temporian/core/data:dtype", - "//temporian/core/data:node", - "//temporian/core/operators:fast_fourier_transform", + "//temporian/implementation/numpy/data:io", + "//temporian/test:utils", ], ) py_test( - name = "test_add_index", - srcs = ["test_add_index.py"], + name = "test_drop_index", + srcs = ["test_drop_index.py"], + srcs_version = "PY3", + deps = [ + # already_there/absl/testing:absltest + "//temporian/implementation/numpy/data:io", + "//temporian/test:utils", + ], +) + +py_test( + name = "test_fast_fourier_transform", + srcs = ["test_fast_fourier_transform.py"], srcs_version = "PY3", deps = [ # already_there/absl/testing:absltest diff --git a/temporian/core/operators/test/fft_test.py b/temporian/core/operators/test/fft_test.py deleted file mode 100644 index 964deb2f6..000000000 --- a/temporian/core/operators/test/fft_test.py +++ /dev/null @@ -1,63 +0,0 @@ -# Copyright 2021 Google LLC. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - - -from absl.testing import absltest - -from temporian.core.data.node import input_node -from temporian.core.data.dtype import DType -from temporian.core.operators.fast_fourier_transform import ( - fast_fourier_transform, -) - - -class FFTOperatorTest(absltest.TestCase): - def test_good(self): - input = input_node([("a", DType.FLOAT32)]) - fast_fourier_transform(input, num_events=20) - fast_fourier_transform(input, num_events=20, window="hamming") - fast_fourier_transform( - input, num_events=20, window="hamming", num_spectral_lines=10 - ) - - def test_wrong_dtype(self): - input = input_node([("a", DType.INT32)]) - with self.assertRaisesRegex(ValueError, "should be tp.float32"): - fast_fourier_transform(input, num_events=20) - - def test_wrong_features(self): - input = input_node([("a", DType.FLOAT32), ("b", DType.FLOAT32)]) - with self.assertRaisesRegex(ValueError, "to be a single feature"): - fast_fourier_transform(input, num_events=20) - - def test_wrong_num_events(self): - input = input_node([("a", DType.FLOAT32)]) - with self.assertRaisesRegex(ValueError, "should be strictly positive"): - fast_fourier_transform(input, num_events=0) - - def test_wrong_window(self): - input = input_node([("a", DType.FLOAT32)]) - with self.assertRaisesRegex(ValueError, "window should be None or"): - fast_fourier_transform(input, num_events=20, window="AAA") - - def test_wrong_num_spectral_lines(self): - input = input_node([("a", DType.FLOAT32)]) - with self.assertRaisesRegex( - ValueError, "num_spectral_lines should be less or equal" - ): - fast_fourier_transform(input, num_events=20, num_spectral_lines=15) - - -if __name__ == "__main__": - absltest.main() diff --git a/temporian/core/operators/test/test_fast_fourier_transform.py b/temporian/core/operators/test/test_fast_fourier_transform.py new file mode 100644 index 000000000..926fc7775 --- /dev/null +++ b/temporian/core/operators/test/test_fast_fourier_transform.py @@ -0,0 +1,111 @@ +# Copyright 2021 Google LLC. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import numpy as np +from absl.testing import absltest, parameterized +from absl.testing.parameterized import TestCase + +from temporian.implementation.numpy.data.io import event_set +from temporian.test.utils import assertOperatorResult, f32, i32 + + +class FastFourierTransformTest(TestCase): + @parameterized.parameters( + {"window": None, "num_spectral_lines": None}, + {"window": "hamming", "num_spectral_lines": None}, + {"window": None, "num_spectral_lines": 1}, + ) + def test_base(self, window, num_spectral_lines): + x = [0, 1, 2, 3, 4, 5] + y = f32([0, 1, 5, -1, 3, 1]) + + def expected(data, frequency_idx): + if window == "hamming": + data = np.hamming(len(data)) * data + else: + assert window is None + return np.abs(np.fft.fft(data)).astype(np.float32)[frequency_idx] + + evset = event_set(timestamps=x, features={"a": y}) + + if num_spectral_lines is None: + num_spectral_lines = 2 + + expected_features = {} + for i in range(num_spectral_lines): + expected_features[f"a{i}"] = [ + expected([0, 1, 5, -1], i), + expected([1, 5, -1, 3], i), + expected([5, -1, 3, 1], i), + ] + + expected_output = event_set( + timestamps=x[3:], features=expected_features + ) + + result = evset.experimental_fast_fourier_transform( + num_events=4, + hop_size=1, + window=window, + num_spectral_lines=num_spectral_lines, + ) + + assertOperatorResult( + self, result, expected_output, check_sampling=False + ) + + def test_good(self): + evset = event_set([0, 0, 0, 0], features={"a": f32([0, 0, 0, 0])}) + evset.experimental_fast_fourier_transform(num_events=4) + evset.experimental_fast_fourier_transform( + num_events=4, window="hamming" + ) + evset.experimental_fast_fourier_transform( + num_events=4, window="hamming", num_spectral_lines=2 + ) + + def test_wrong_dtype(self): + evset = event_set([], {"a": i32([])}) + with self.assertRaisesRegex(ValueError, "should be tp.float32"): + evset.experimental_fast_fourier_transform(num_events=20) + + def test_wrong_features(self): + evset = event_set([], {"a": f32([]), "b": f32([])}) + with self.assertRaisesRegex(ValueError, "to be a single feature"): + evset.experimental_fast_fourier_transform(num_events=20) + + def test_wrong_num_events(self): + evset = event_set([], {"a": f32([])}) + with self.assertRaisesRegex(ValueError, "should be strictly positive"): + evset.experimental_fast_fourier_transform(num_events=0) + + def test_wrong_window(self): + evset = event_set([], {"a": f32([])}) + with self.assertRaisesRegex(ValueError, "window should be None or"): + evset.experimental_fast_fourier_transform( + num_events=20, window="AAA" + ) + + def test_wrong_num_spectral_lines(self): + evset = event_set([], {"a": f32([])}) + with self.assertRaisesRegex( + ValueError, "num_spectral_lines should be less or equal" + ): + evset.experimental_fast_fourier_transform( + num_events=20, num_spectral_lines=15 + ) + + +if __name__ == "__main__": + absltest.main() diff --git a/temporian/core/test/event_set_ops_test.py b/temporian/core/test/event_set_ops_test.py index 5173e0f90..abe48f804 100644 --- a/temporian/core/test/event_set_ops_test.py +++ b/temporian/core/test/event_set_ops_test.py @@ -1,3 +1,17 @@ +# Copyright 2021 Google LLC. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + from absl.testing import absltest from temporian.core.data.node import EventSetNode from temporian.implementation.numpy.data.event_set import EventSet @@ -37,10 +51,6 @@ def setUp(self): self.node = self.evset.node() self.other_node = self.other_evset.node() - def test_add_index(self): - self.assertTrue(isinstance(self.evset.add_index("a"), EventSet)) - self.assertTrue(isinstance(self.node.add_index("a"), EventSetNode)) - def test_begin(self): self.assertTrue(isinstance(self.evset.begin(), EventSet)) self.assertTrue(isinstance(self.node.begin(), EventSetNode)) @@ -53,10 +63,6 @@ def test_cumsum(self): self.assertTrue(isinstance(self.evset.cumsum(), EventSet)) self.assertTrue(isinstance(self.node.cumsum(), EventSetNode)) - def test_drop_index(self): - self.assertTrue(isinstance(self.evset.drop_index("x"), EventSet)) - self.assertTrue(isinstance(self.node.drop_index("x"), EventSetNode)) - def test_end(self): self.assertTrue(isinstance(self.evset.end(), EventSet)) self.assertTrue(isinstance(self.node.end(), EventSetNode)) @@ -65,32 +71,10 @@ def test_enumerate(self): self.assertTrue(isinstance(self.evset.enumerate(), EventSet)) self.assertTrue(isinstance(self.node.enumerate(), EventSetNode)) - def test_fast_fourier_transform(self): - self.assertTrue( - isinstance( - self.evset["a"].experimental_fast_fourier_transform( - num_events=2 - ), - EventSet, - ) - ) - self.assertTrue( - isinstance( - self.node["a"].experimental_fast_fourier_transform( - num_events=2 - ), - EventSetNode, - ) - ) - def test_select(self): self.assertTrue(isinstance(self.evset.select("a"), EventSet)) self.assertTrue(isinstance(self.node.select("a"), EventSetNode)) - def test_set_index(self): - self.assertTrue(isinstance(self.evset.set_index("a"), EventSet)) - self.assertTrue(isinstance(self.node.set_index("a"), EventSetNode)) - def test_timestamps(self): self.assertTrue(isinstance(self.evset.timestamps(), EventSet)) self.assertTrue(isinstance(self.node.timestamps(), EventSetNode)) diff --git a/temporian/implementation/numpy/operators/test/BUILD b/temporian/implementation/numpy/operators/test/BUILD index 8d5b99f15..356cb197a 100644 --- a/temporian/implementation/numpy/operators/test/BUILD +++ b/temporian/implementation/numpy/operators/test/BUILD @@ -214,23 +214,6 @@ py_test( ], ) -py_test( - name = "fft_test", - srcs = ["fft_test.py"], - srcs_version = "PY3", - deps = [ - # already_there/absl/testing:absltest - # already_there/absl/testing:parameterized - ":utils", - "//temporian/core/data:dtype", - "//temporian/core/data:node", - "//temporian/core/data:schema", - "//temporian/implementation/numpy/data:io", - "//temporian/core/operators:fast_fourier_transform", - "//temporian/implementation/numpy/operators:fast_fourier_transform", - ], -) - py_test( name = "filter_moving_count_test", srcs = ["filter_moving_count_test.py"], diff --git a/temporian/implementation/numpy/operators/test/fft_test.py b/temporian/implementation/numpy/operators/test/fft_test.py deleted file mode 100644 index 9464a721a..000000000 --- a/temporian/implementation/numpy/operators/test/fft_test.py +++ /dev/null @@ -1,85 +0,0 @@ -# Copyright 2021 Google LLC. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - - -from absl.testing import parameterized, absltest - -import numpy as np -from temporian.core.operators.fast_fourier_transform import FastFourierTransform -from temporian.implementation.numpy.data.io import event_set -from temporian.implementation.numpy.operators.fast_fourier_transform import ( - FastFourierTransformNumpyImplementation, -) -from temporian.implementation.numpy.operators.test.utils import ( - assertEqualEventSet, - testOperatorAndImp, -) - - -class FastFourierTransformOperatorTest(parameterized.TestCase): - def setUp(self): - pass - - @parameterized.parameters( - {"window": None, "num_spectral_lines": None}, - {"window": "hamming", "num_spectral_lines": None}, - {"window": None, "num_spectral_lines": 1}, - ) - def test_base(self, window, num_spectral_lines): - x = [0, 1, 2, 3, 4, 5] - y = np.array([0, 1, 5, -1, 3, 1], dtype=np.float32) - - def expected(data, frequency_idx): - if window == "hamming": - data = np.hamming(len(data)) * data - else: - assert window is None - return np.abs(np.fft.fft(data)).astype(np.float32)[frequency_idx] - - evset = event_set(timestamps=x, features={"a": y}) - node = evset.node() - - if num_spectral_lines is None: - num_spectral_lines = 2 - - expected_features = {} - for i in range(num_spectral_lines): - expected_features[f"a{i}"] = [ - expected([0, 1, 5, -1], i), - expected([1, 5, -1, 3], i), - expected([5, -1, 3, 1], i), - ] - - expected_output = event_set( - timestamps=x[3:], - features=expected_features, - ) - - # Run op - op = FastFourierTransform( - input=node, - num_events=4, - hop_size=1, - window=window, - num_spectral_lines=num_spectral_lines, - ) - instance = FastFourierTransformNumpyImplementation(op) - testOperatorAndImp(self, op, instance) - output = instance.call(input=evset)["output"] - - assertEqualEventSet(self, output, expected_output) - - -if __name__ == "__main__": - absltest.main() From 064d3211b467e54492cd57d612fcd4edb9ff6856 Mon Sep 17 00:00:00 2001 From: Ian Spektor Date: Tue, 31 Oct 2023 18:00:43 +0100 Subject: [PATCH 6/7] restructure begin tests --- temporian/core/operators/test/BUILD | 11 ++++++ .../operators/test/test_begin.py} | 35 ++++++------------- temporian/core/test/event_set_ops_test.py | 8 ----- .../implementation/numpy/operators/test/BUILD | 16 --------- 4 files changed, 21 insertions(+), 49 deletions(-) rename temporian/{implementation/numpy/operators/test/begin_test.py => core/operators/test/test_begin.py} (53%) diff --git a/temporian/core/operators/test/BUILD b/temporian/core/operators/test/BUILD index 78f6a6eed..33b759752 100644 --- a/temporian/core/operators/test/BUILD +++ b/temporian/core/operators/test/BUILD @@ -17,6 +17,17 @@ py_test( ], ) +py_test( + name = "test_begin", + srcs = ["test_begin.py"], + srcs_version = "PY3", + deps = [ + # already_there/absl/testing:absltest + "//temporian/implementation/numpy/data:io", + "//temporian/test:utils", + ], +) + py_test( name = "test_drop_index", srcs = ["test_drop_index.py"], diff --git a/temporian/implementation/numpy/operators/test/begin_test.py b/temporian/core/operators/test/test_begin.py similarity index 53% rename from temporian/implementation/numpy/operators/test/begin_test.py rename to temporian/core/operators/test/test_begin.py index 58642ab38..3532d5fb2 100644 --- a/temporian/implementation/numpy/operators/test/begin_test.py +++ b/temporian/core/operators/test/test_begin.py @@ -12,43 +12,28 @@ # See the License for the specific language governing permissions and # limitations under the License. - from absl.testing import absltest +from absl.testing.parameterized import TestCase -from temporian.core.operators.begin import BeginOperator from temporian.implementation.numpy.data.io import event_set -from temporian.implementation.numpy.operators.begin import ( - BeginNumpyImplementation, -) - +from temporian.test.utils import assertOperatorResult -class BeginOperatorTest(absltest.TestCase): - def setUp(self): - pass - def test_base(self): +class BeginTest(TestCase): + def test_basic(self): evset = event_set( timestamps=[1, 2, 3, 4], - features={ - "a": [5, 6, 7, 8], - "b": ["A", "A", "B", "B"], - }, + features={"a": [5, 6, 7, 8], "b": ["A", "A", "B", "B"]}, indexes=["b"], ) - node = evset.node() - expected_output = event_set( - timestamps=[1, 3], - features={"b": ["A", "B"]}, - indexes=["b"], - ) + result = evset.begin() - # Run op - op = BeginOperator(input=node) - instance = BeginNumpyImplementation(op) - output = instance.call(input=evset)["output"] + expected = event_set( + timestamps=[1, 3], features={"b": ["A", "B"]}, indexes=["b"] + ) - self.assertEqual(output, expected_output) + assertOperatorResult(self, result, expected, check_sampling=False) if __name__ == "__main__": diff --git a/temporian/core/test/event_set_ops_test.py b/temporian/core/test/event_set_ops_test.py index abe48f804..e5f0519a0 100644 --- a/temporian/core/test/event_set_ops_test.py +++ b/temporian/core/test/event_set_ops_test.py @@ -51,10 +51,6 @@ def setUp(self): self.node = self.evset.node() self.other_node = self.other_evset.node() - def test_begin(self): - self.assertTrue(isinstance(self.evset.begin(), EventSet)) - self.assertTrue(isinstance(self.node.begin(), EventSetNode)) - def test_cast(self): self.assertTrue(isinstance(self.evset.cast({"a": float}), EventSet)) self.assertTrue(isinstance(self.node.cast({"a": float}), EventSetNode)) @@ -63,10 +59,6 @@ def test_cumsum(self): self.assertTrue(isinstance(self.evset.cumsum(), EventSet)) self.assertTrue(isinstance(self.node.cumsum(), EventSetNode)) - def test_end(self): - self.assertTrue(isinstance(self.evset.end(), EventSet)) - self.assertTrue(isinstance(self.node.end(), EventSetNode)) - def test_enumerate(self): self.assertTrue(isinstance(self.evset.enumerate(), EventSet)) self.assertTrue(isinstance(self.node.enumerate(), EventSetNode)) diff --git a/temporian/implementation/numpy/operators/test/BUILD b/temporian/implementation/numpy/operators/test/BUILD index 356cb197a..8bd03a5db 100644 --- a/temporian/implementation/numpy/operators/test/BUILD +++ b/temporian/implementation/numpy/operators/test/BUILD @@ -134,22 +134,6 @@ py_test( ], ) -py_test( - name = "begin_test", - srcs = ["begin_test.py"], - srcs_version = "PY3", - deps = [ - # already_there/absl/testing:absltest - ":utils", - "//temporian/implementation/numpy/data:io", - "//temporian/core/data:dtype", - "//temporian/core/data:node", - "//temporian/core/data:schema", - "//temporian/core/operators:begin", - "//temporian/implementation/numpy/operators:begin", - ], -) - py_test( name = "end_test", srcs = ["end_test.py"], From f7f7875beae25f2d0b8fbc1588e2b0ab1e3f18d2 Mon Sep 17 00:00:00 2001 From: Ian Spektor Date: Tue, 31 Oct 2023 18:02:28 +0100 Subject: [PATCH 7/7] restructure end op tests --- temporian/core/operators/test/BUILD | 11 ++++++ .../operators/test/test_end.py} | 35 ++++++------------- .../implementation/numpy/operators/test/BUILD | 16 --------- 3 files changed, 21 insertions(+), 41 deletions(-) rename temporian/{implementation/numpy/operators/test/end_test.py => core/operators/test/test_end.py} (53%) diff --git a/temporian/core/operators/test/BUILD b/temporian/core/operators/test/BUILD index 33b759752..3969cf518 100644 --- a/temporian/core/operators/test/BUILD +++ b/temporian/core/operators/test/BUILD @@ -39,6 +39,17 @@ py_test( ], ) +py_test( + name = "test_end", + srcs = ["test_end.py"], + srcs_version = "PY3", + deps = [ + # already_there/absl/testing:absltest + "//temporian/implementation/numpy/data:io", + "//temporian/test:utils", + ], +) + py_test( name = "test_fast_fourier_transform", srcs = ["test_fast_fourier_transform.py"], diff --git a/temporian/implementation/numpy/operators/test/end_test.py b/temporian/core/operators/test/test_end.py similarity index 53% rename from temporian/implementation/numpy/operators/test/end_test.py rename to temporian/core/operators/test/test_end.py index 73b3545ee..97c652ed1 100644 --- a/temporian/implementation/numpy/operators/test/end_test.py +++ b/temporian/core/operators/test/test_end.py @@ -12,43 +12,28 @@ # See the License for the specific language governing permissions and # limitations under the License. - from absl.testing import absltest +from absl.testing.parameterized import TestCase -from temporian.core.operators.end import EndOperator from temporian.implementation.numpy.data.io import event_set -from temporian.implementation.numpy.operators.end import ( - EndNumpyImplementation, -) - +from temporian.test.utils import assertOperatorResult -class EndOperatorTest(absltest.TestCase): - def setUp(self): - pass - def test_base(self): +class EndTest(TestCase): + def test_basic(self): evset = event_set( timestamps=[1, 2, 3, 4], - features={ - "a": [5, 6, 7, 8], - "b": ["A", "A", "B", "B"], - }, + features={"a": [5, 6, 7, 8], "b": ["A", "A", "B", "B"]}, indexes=["b"], ) - node = evset.node() - expected_output = event_set( - timestamps=[2, 4], - features={"b": ["A", "B"]}, - indexes=["b"], - ) + result = evset.end() - # Run op - op = EndOperator(input=node) - instance = EndNumpyImplementation(op) - output = instance.call(input=evset)["output"] + expected = event_set( + timestamps=[2, 4], features={"b": ["A", "B"]}, indexes=["b"] + ) - self.assertEqual(output, expected_output) + assertOperatorResult(self, result, expected, check_sampling=False) if __name__ == "__main__": diff --git a/temporian/implementation/numpy/operators/test/BUILD b/temporian/implementation/numpy/operators/test/BUILD index 8bd03a5db..aaff47fc7 100644 --- a/temporian/implementation/numpy/operators/test/BUILD +++ b/temporian/implementation/numpy/operators/test/BUILD @@ -134,22 +134,6 @@ py_test( ], ) -py_test( - name = "end_test", - srcs = ["end_test.py"], - srcs_version = "PY3", - deps = [ - # already_there/absl/testing:absltest - ":utils", - "//temporian/implementation/numpy/data:io", - "//temporian/core/data:dtype", - "//temporian/core/data:node", - "//temporian/core/data:schema", - "//temporian/core/operators:end", - "//temporian/implementation/numpy/operators:end", - ], -) - py_test( name = "timestamps_test", srcs = ["timestamps_test.py"],