Skip to content

Commit

Permalink
Merge pull request #285 from google/window-tests
Browse files Browse the repository at this point in the history
Restructure tests: window ops
  • Loading branch information
ianspektor committed Oct 20, 2023
2 parents c2e2297 + fabc098 commit 48bb7fe
Show file tree
Hide file tree
Showing 13 changed files with 504 additions and 980 deletions.
12 changes: 12 additions & 0 deletions temporian/core/operators/window/test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,15 @@ py_test(
"//temporian/test:utils",
],
)

py_test(
name = "test_moving_count",
srcs = ["test_moving_count.py"],
srcs_version = "PY3",
deps = [
# already_there/absl/testing:absltest
"//temporian/implementation/numpy/data:io",
"//temporian/core/data:duration",
"//temporian/test:utils",
],
)
57 changes: 56 additions & 1 deletion temporian/core/operators/window/test/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
Use it to test expected behavior from the base classes, such as errors or
warnings."""

from math import nan
from unittest.mock import patch

from absl.testing import absltest
Expand All @@ -25,7 +26,8 @@
from temporian.implementation.numpy.operators.window import (
base as base_window_impl,
)
from temporian.test.utils import f32
from temporian.implementation.numpy_cc.operators import operators_cc
from temporian.test.utils import assertOperatorResult, f32, f64


class SimpleMovingAverageTest(absltest.TestCase):
Expand Down Expand Up @@ -57,6 +59,36 @@ def test_variable_winlen_invalid_dtype(self):
):
evset.simple_moving_average(window_length=window_length)

def test_missing_index(self):
"""Tests that if an index of the sampling is missing in the input, the
output has empty values for that index."""
evset = event_set(
timestamps=[0, 1],
features={"a": [1.0, 1.0], "i": ["a", "a"]},
indexes=["i"],
)
sampling_timestamps = [2, 3, 4, 5]
sampling = event_set(
timestamps=sampling_timestamps,
features={"i": ["a", "a", "b", "b"]},
indexes=["i"],
)
result = evset.simple_moving_average(
window_length=5.0, sampling=sampling
)

expected = event_set(
timestamps=sampling_timestamps,
features={
"i": ["a", "a", "b", "b"],
"a": [1.0, 1.0, nan, nan],
},
indexes=["i"],
same_sampling_as=sampling,
)

assertOperatorResult(self, result, expected)

@patch.object(base_window_impl, "logging")
def test_invalid_window_length_warning(self, logging_mock):
"""Tests that warning is shown when receiving non strictly positive
Expand Down Expand Up @@ -89,6 +121,29 @@ def test_sampling_and_variable_winlen(self):
):
evset.moving_sum(window_length=window_length, sampling=sampling)

@patch.object(operators_cc, "moving_sum")
def test_with_variable_winlen_same_sampling_uses_correct_cpp_impl(
self, cpp_moving_sum_mock
):
"""Checks that the no-sampling version of cpp code is called when
passing a variable window_length with same sampling as the input."""
evset = event_set(timestamps=[1], features={"a": [10.0]})

window_length = event_set(
timestamps=[1], features={"a": [1.0]}, same_sampling_as=evset
)

cpp_moving_sum_mock.return_value = f64([10.0])

evset.moving_sum(window_length=window_length)

# sampling_timestamps not passed
cpp_moving_sum_mock.assert_called_once_with(
evset_timestamps=evset.data[()].timestamps,
evset_values=evset.data[()].features[0],
window_length=window_length.data[()].features[0],
)


if __name__ == "__main__":
absltest.main()
94 changes: 94 additions & 0 deletions temporian/core/operators/window/test/test_moving_count.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# 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
from absl.testing.parameterized import TestCase

from temporian.implementation.numpy.data.io import event_set
from temporian.test.utils import assertOperatorResult, i32


class MovingCountTest(TestCase):
def test_basic(self):
timestamps = [1, 2, 3, 5, 20]
evset = event_set(timestamps=timestamps)

result = evset.moving_count(window_length=5.0)

expected = event_set(
timestamps=timestamps,
features={"count": i32([1, 2, 3, 4, 1])},
same_sampling_as=evset,
)

assertOperatorResult(self, result, expected)

def test_w_sampling(self):
timestamps = [0, 1, 2, 3, 5, 20]
evset = event_set(timestamps=timestamps)
sampling_timestamps = [-1, 3, 40]
sampling = event_set(timestamps=sampling_timestamps)

result = evset.moving_count(window_length=3.5, sampling=sampling)

expected = event_set(
timestamps=sampling_timestamps,
features={"count": i32([0, 4, 0])},
same_sampling_as=sampling,
)

assertOperatorResult(self, result, expected)

def test_wo_sampling_w_variable_winlen(self):
timestamps = [1, 2, 3, 5, 20]
evset = event_set(timestamps=timestamps)
winlen = event_set(
timestamps=timestamps,
features={"a": [0, np.inf, 1.001, 5, 0.00001]},
same_sampling_as=evset,
)

result = evset.moving_count(window_length=winlen)

expected = event_set(
timestamps=timestamps,
features={"count": i32([0, 2, 2, 4, 1])},
same_sampling_as=evset,
)

assertOperatorResult(self, result, expected)

def test_w_sampling_w_variable_winlen(self):
timestamps = [1, 2, 3, 5, 20]
evset = event_set(timestamps=timestamps)
sampling_timestamps = [0, 1.5, 3.5, 3.5, 3.5, 20]
winlen = event_set(
timestamps=sampling_timestamps,
features={"a": [1, 1, 1, 3, 0.5, 19.5]},
)

result = evset.moving_count(window_length=winlen)

expected = event_set(
timestamps=sampling_timestamps,
features={"count": i32([0, 1, 1, 3, 0, 5])},
same_sampling_as=winlen,
)

assertOperatorResult(self, result, expected)


if __name__ == "__main__":
absltest.main()
83 changes: 83 additions & 0 deletions temporian/core/operators/window/test/test_moving_max.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,97 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from math import nan

import numpy as np
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, f32


class MovingMaxTest(TestCase):
def test_basic(self):
timestamps = [0, 1, 2, 3, 5, 20]
evset = event_set(
timestamps=timestamps,
features={"a": f32([nan, 10, nan, 12, 13, 14])},
)

result = evset.moving_max(window_length=3.5)

expected = event_set(
timestamps=timestamps,
features={"a": f32([nan, 10, 10, 12, 13, 14])},
same_sampling_as=evset,
)

assertOperatorResult(self, result, expected)

def test_w_sampling(self):
timestamps = [0, 1, 2, 3, 5, 20]
evset = event_set(
timestamps=timestamps,
features={"a": f32([nan, 10, nan, 12, 13, 14])},
)
sampling_timestamps = [-1, 3, 40]
sampling = event_set(timestamps=sampling_timestamps)

result = evset.moving_max(window_length=3.5, sampling=sampling)

expected = event_set(
timestamps=sampling_timestamps,
features={"a": f32([nan, 12, nan])},
same_sampling_as=sampling,
)

assertOperatorResult(self, result, expected)

def test_wo_sampling_w_variable_winlen(self):
timestamps = [0, 1, 2, 3, 5, 20]
evset = event_set(
timestamps=timestamps,
features={"a": [nan, 0, 10, 5, 1, 2]},
)
winlen = event_set(
timestamps=timestamps,
features={"a": [1, 1, 1.5, 0.5, 3.5, 0]},
same_sampling_as=evset,
)

result = evset.moving_max(window_length=winlen)

expected = event_set(
timestamps=timestamps,
features={"a": [nan, 0, 10, 5, 10, nan]},
same_sampling_as=evset,
)

assertOperatorResult(self, result, expected)

def test_w_sampling_w_variable_winlen(self):
timestamps = [0, 1, 2, 3, 5, 20]
evset = event_set(
timestamps=timestamps,
features={"a": [nan, 0, 10, 5, 1, 2]},
)
sampling_timestamps = [-1, 1, 4, 19, 20, 20]
winlen = event_set(
timestamps=sampling_timestamps,
features={"a": [10, 10, 2.5, 19, 0.001, np.inf]},
)

result = evset.moving_max(window_length=winlen)

expected = event_set(
timestamps=sampling_timestamps,
features={"a": [nan, 0, 10, 10, 2, 10]},
same_sampling_as=winlen,
)

assertOperatorResult(self, result, expected)

def test_error_input_bytes(self):
evset = event_set([1, 2], {"f": ["A", "B"]})
with self.assertRaisesRegex(
Expand Down
85 changes: 84 additions & 1 deletion temporian/core/operators/window/test/test_moving_min.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,97 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from math import nan

import numpy as np
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, f32


class MovingMaxTest(TestCase):
class MovingMinTest(TestCase):
def test_basic(self):
timestamps = [0, 1, 2, 3, 5, 20]
evset = event_set(
timestamps=timestamps,
features={"a": f32([nan, 10, nan, 12, 13, 14])},
)

result = evset.moving_min(window_length=3.5)

expected = event_set(
timestamps=timestamps,
features={"a": f32([nan, 10, 10, 10, 12, 14])},
same_sampling_as=evset,
)

assertOperatorResult(self, result, expected)

def test_w_sampling(self):
timestamps = [0, 1, 2, 3, 5, 20]
evset = event_set(
timestamps=timestamps,
features={"a": f32([nan, 10, nan, 12, 13, 14])},
)
sampling_timestamps = [-1, 3, 40]
sampling = event_set(timestamps=sampling_timestamps)

result = evset.moving_min(window_length=3.5, sampling=sampling)

expected = event_set(
timestamps=sampling_timestamps,
features={"a": f32([nan, 10, nan])},
same_sampling_as=sampling,
)

assertOperatorResult(self, result, expected)

def test_wo_sampling_w_variable_winlen(self):
timestamps = [0, 1, 2, 3, 5, 20]
evset = event_set(
timestamps=timestamps,
features={"a": [nan, 0, 10, 5, 1, 2]},
)
winlen = event_set(
timestamps=timestamps,
features={"a": [1, 1, 1.5, 0.5, 3.5, 0]},
same_sampling_as=evset,
)

result = evset.moving_min(window_length=winlen)

expected = event_set(
timestamps=timestamps,
features={"a": [nan, 0, 0, 5, 1, nan]},
same_sampling_as=evset,
)

assertOperatorResult(self, result, expected)

def test_w_sampling_w_variable_winlen(self):
timestamps = [0, 1, 2, 3, 5, 20]
evset = event_set(
timestamps=timestamps,
features={"a": [nan, 0, 10, 5, 1, 2]},
)
sampling_timestamps = [-1, 1, 4, 19, 20, 20]
winlen = event_set(
timestamps=sampling_timestamps,
features={"a": [10, 10, 2.5, 19, 0.001, np.inf]},
)

result = evset.moving_min(window_length=winlen)

expected = event_set(
timestamps=sampling_timestamps,
features={"a": [nan, 0, 5, 0, 2, 0]},
same_sampling_as=winlen,
)

assertOperatorResult(self, result, expected)

def test_error_input_bytes(self):
evset = event_set([1, 2], {"f": ["A", "B"]})
with self.assertRaisesRegex(
Expand Down

0 comments on commit 48bb7fe

Please sign in to comment.