Skip to content

Commit

Permalink
[BC] Rename BasicReducer to NativeReducer (#544)
Browse files Browse the repository at this point in the history
  • Loading branch information
durandtibo authored Mar 30, 2024
1 parent 4881c69 commit c0deaa2
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 84 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "coola"
version = "0.5.1"
version = "0.5.2a0"
description = "A library to check if two complex/nested objects are equal or not"
readme = "README.md"
authors = ["Thibaut Durand <[email protected]>"]
Expand Down
4 changes: 2 additions & 2 deletions src/coola/reducers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
__all__ = [
"BaseBasicReducer",
"BaseReducer",
"BasicReducer",
"NativeReducer",
"EmptySequenceError",
"NumpyReducer",
"ReducerRegistry",
Expand All @@ -12,7 +12,7 @@
]

from coola.reducers.base import BaseBasicReducer, BaseReducer, EmptySequenceError
from coola.reducers.basic import BasicReducer
from coola.reducers.native import NativeReducer
from coola.reducers.numpy_ import NumpyReducer
from coola.reducers.registry import ReducerRegistry
from coola.reducers.torch_ import TorchReducer
Expand Down
10 changes: 5 additions & 5 deletions src/coola/reducers/basic.py → src/coola/reducers/native.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from __future__ import annotations

__all__ = ["BasicReducer"]
__all__ = ["NativeReducer"]

from collections.abc import Sequence
from statistics import mean, median, stdev
Expand All @@ -15,14 +15,14 @@
T = TypeVar("T", bound=Sequence[Union[int, float]])


class BasicReducer(BaseBasicReducer[T]):
r"""Implement a basic reducer.
class NativeReducer(BaseBasicReducer[T]):
r"""Implement a native reducer.
Example usage:
```pycon
>>> from coola.reducers import BasicReducer
>>> reducer = BasicReducer()
>>> from coola.reducers import NativeReducer
>>> reducer = NativeReducer()
>>> reducer.max([-2, -1, 0, 1, 2])
2
>>> reducer.median([-2, -1, 0, 1, 2])
Expand Down
12 changes: 6 additions & 6 deletions src/coola/reducers/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from typing import ClassVar

from coola.reducers.base import BaseReducer
from coola.reducers.basic import BasicReducer
from coola.reducers.native import NativeReducer
from coola.utils.format import str_indent, str_mapping


Expand All @@ -16,7 +16,7 @@ class ReducerRegistry:
instances of this class.
"""

registry: ClassVar[dict[str, BaseReducer]] = {"basic": BasicReducer()}
registry: ClassVar[dict[str, BaseReducer]] = {"native": NativeReducer()}

def __repr__(self) -> str:
return f"{self.__class__.__qualname__}(\n {str_indent(str_mapping(self.registry))}\n)"
Expand All @@ -39,8 +39,8 @@ def add_reducer(cls, name: str, reducer: BaseReducer, exist_ok: bool = False) ->
Example usage:
```pycon
>>> from coola.reducers import ReducerRegistry, BasicReducer
>>> ReducerRegistry.add_reducer("basic", BasicReducer(), exist_ok=True)
>>> from coola.reducers import ReducerRegistry, NativeReducer
>>> ReducerRegistry.add_reducer("native", NativeReducer(), exist_ok=True)
```
"""
Expand All @@ -65,7 +65,7 @@ def available_reducers(cls) -> tuple[str, ...]:
```pycon
>>> from coola.reducers import ReducerRegistry
>>> ReducerRegistry.available_reducers()
('basic', 'numpy', 'torch')
('native', 'numpy', 'torch')
```
"""
Expand All @@ -86,7 +86,7 @@ def has_reducer(cls, name: str) -> bool:
```pycon
>>> from coola.reducers import ReducerRegistry
>>> ReducerRegistry.has_reducer("basic")
>>> ReducerRegistry.has_reducer("native")
True
>>> ReducerRegistry.has_reducer("missing")
False
Expand Down
4 changes: 2 additions & 2 deletions src/coola/reducers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

__all__ = ["auto_reducer"]

from coola.reducers import BaseReducer, BasicReducer, NumpyReducer, TorchReducer
from coola.reducers import BaseReducer, NativeReducer, NumpyReducer, TorchReducer
from coola.utils import is_numpy_available, is_torch_available


Expand All @@ -31,4 +31,4 @@ def auto_reducer() -> BaseReducer:
return TorchReducer()
if is_numpy_available():
return NumpyReducer()
return BasicReducer()
return NativeReducer()
4 changes: 2 additions & 2 deletions tests/package_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from coola import objects_are_allclose, objects_are_equal
from coola.random import manual_seed
from coola.reducers import BasicReducer
from coola.reducers import NativeReducer
from coola.utils.imports import (
is_jax_available,
is_numpy_available,
Expand Down Expand Up @@ -293,7 +293,7 @@ def check_random() -> None:

def check_reduction() -> None:
logger.info("Checking reduction...")
reducer = BasicReducer()
reducer = NativeReducer()
assert reducer.max([-2, -1, 0, 1, 2]) == 2
assert reducer.median([-2, -1, 0, 1, 2]) == 0
assert reducer.sort([2, 1, -2, 3, 0]) == [-2, 0, 1, 2, 3]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import pytest

from coola.reducers import BasicReducer, EmptySequenceError, ReducerRegistry
from coola.reducers import EmptySequenceError, NativeReducer, ReducerRegistry

if TYPE_CHECKING:
from collections.abc import Sequence
Expand All @@ -15,119 +15,119 @@


def test_reducer_registry_available_reducers() -> None:
assert isinstance(ReducerRegistry.registry["basic"], BasicReducer)
assert isinstance(ReducerRegistry.registry["native"], NativeReducer)


##################################
# Tests for BasicReducer #
##################################


def test_basic_reducer_str() -> None:
assert str(BasicReducer()).startswith("BasicReducer(")
def test_native_reducer_str() -> None:
assert str(NativeReducer()).startswith("NativeReducer(")


@pytest.mark.parametrize("values", [[-2, -1, 0, 1, 2], (-2, -1, 0, 1, 2), [2], [2, -1, -2, -3]])
def test_basic_reducer_max_int(values: Sequence[int | float]) -> None:
val = BasicReducer().max(values)
def test_native_reducer_max_int(values: Sequence[int | float]) -> None:
val = NativeReducer().max(values)
assert isinstance(val, int)
assert val == 2


@pytest.mark.parametrize(
"values", [[-2.5, -1.5, 0.5, 1.5, 2.5], (-2.5, -1.5, 0.5, 1.5, 2.5), [2.5], [2.5, -1.5, -2, -3]]
)
def test_basic_reducer_max_float(values: Sequence[int | float]) -> None:
val = BasicReducer().max(values)
def test_native_reducer_max_float(values: Sequence[int | float]) -> None:
val = NativeReducer().max(values)
assert isinstance(val, float)
assert val == 2.5


@pytest.mark.parametrize("values", EMPTY_SEQUENCES)
def test_basic_reducer_max_empty(values: Sequence[int | float]) -> None:
def test_native_reducer_max_empty(values: Sequence[int | float]) -> None:
with pytest.raises(
EmptySequenceError, match="Cannot compute the maximum because the summary is empty"
):
BasicReducer().max(values)
NativeReducer().max(values)


@pytest.mark.parametrize("values", [[-2, -1, 0, 1, 2], (-2, -1, 0, 1, 2), [0]])
def test_basic_reducer_mean_int(values: Sequence[int | float]) -> None:
val = BasicReducer().mean(values)
def test_native_reducer_mean_int(values: Sequence[int | float]) -> None:
val = NativeReducer().mean(values)
assert isinstance(val, float)
assert val == 0.0


@pytest.mark.parametrize(
"values", [[-1.5, -0.5, 0.5, 1.5, 2.5], (-1.5, -0.5, 0.5, 1.5, 2.5), [0.5]]
)
def test_basic_reducer_mean_float(values: Sequence[int | float]) -> None:
val = BasicReducer().mean(values)
def test_native_reducer_mean_float(values: Sequence[int | float]) -> None:
val = NativeReducer().mean(values)
assert isinstance(val, float)
assert val == 0.5


@pytest.mark.parametrize("values", EMPTY_SEQUENCES)
def test_basic_reducer_mean_empty(values: Sequence[int | float]) -> None:
def test_native_reducer_mean_empty(values: Sequence[int | float]) -> None:
with pytest.raises(
EmptySequenceError, match="Cannot compute the mean because the summary is empty"
):
BasicReducer().mean(values)
NativeReducer().mean(values)


@pytest.mark.parametrize("values", [[-2, -1, 0, 1, 2], (-2, -1, 0, 1, 2), [0]])
def test_basic_reducer_median_int(values: Sequence[int | float]) -> None:
val = BasicReducer().median(values)
def test_native_reducer_median_int(values: Sequence[int | float]) -> None:
val = NativeReducer().median(values)
assert isinstance(val, int)
assert val == 0


@pytest.mark.parametrize(
"values", [[-1.5, -0.5, 0.5, 1.5, 2.5], (-1.5, -0.5, 0.5, 1.5, 2.5), [0.5]]
)
def test_basic_reducer_median_float(values: Sequence[int | float]) -> None:
val = BasicReducer().median(values)
def test_native_reducer_median_float(values: Sequence[int | float]) -> None:
val = NativeReducer().median(values)
assert isinstance(val, float)
assert val == 0.5


@pytest.mark.parametrize("values", EMPTY_SEQUENCES)
def test_basic_reducer_median_empty(values: Sequence[int | float]) -> None:
def test_native_reducer_median_empty(values: Sequence[int | float]) -> None:
with pytest.raises(
EmptySequenceError, match="Cannot compute the median because the summary is empty"
):
BasicReducer().median(values)
NativeReducer().median(values)


@pytest.mark.parametrize("values", [[-2, -1, 0, 1, 2], (-2, -1, 0, 1, 2), [-2], [-2, 1, 2, 3]])
def test_basic_reducer_min_int(values: Sequence[int | float]) -> None:
val = BasicReducer().min(values)
def test_native_reducer_min_int(values: Sequence[int | float]) -> None:
val = NativeReducer().min(values)
assert isinstance(val, int)
assert val == -2


@pytest.mark.parametrize(
"values", [[-2.5, -1.5, 0.5, 1.5, 2.5], (-2.5, -1.5, 0.5, 1.5, 2.5), [-2.5], [-2.5, 1.5, 2, 3]]
)
def test_basic_reducer_min_float(values: Sequence[int | float]) -> None:
val = BasicReducer().min(values)
def test_native_reducer_min_float(values: Sequence[int | float]) -> None:
val = NativeReducer().min(values)
assert isinstance(val, float)
assert val == -2.5


@pytest.mark.parametrize("values", EMPTY_SEQUENCES)
def test_basic_reducer_min_empty(values: Sequence[int | float]) -> None:
def test_native_reducer_min_empty(values: Sequence[int | float]) -> None:
with pytest.raises(
EmptySequenceError, match="Cannot compute the minimum because the summary is empty"
):
BasicReducer().min(values)
NativeReducer().min(values)


@pytest.mark.parametrize(
"values", [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)]
)
def test_basic_reducer_quantile_int(values: Sequence[int | float]) -> None:
assert BasicReducer().quantile(values, (0.2, 0.5, 0.9)) == [2, 5, 9]
def test_native_reducer_quantile_int(values: Sequence[int | float]) -> None:
assert NativeReducer().quantile(values, (0.2, 0.5, 0.9)) == [2, 5, 9]


@pytest.mark.parametrize(
Expand All @@ -137,57 +137,57 @@ def test_basic_reducer_quantile_int(values: Sequence[int | float]) -> None:
(0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5),
],
)
def test_basic_reducer_quantile_float(values: Sequence[int | float]) -> None:
assert BasicReducer().quantile(values, (0.0, 0.1, 0.4, 0.9)) == [0.5, 1.5, 4.5, 9.5]
def test_native_reducer_quantile_float(values: Sequence[int | float]) -> None:
assert NativeReducer().quantile(values, (0.0, 0.1, 0.4, 0.9)) == [0.5, 1.5, 4.5, 9.5]


@pytest.mark.parametrize("values", EMPTY_SEQUENCES)
def test_basic_reducer_quantile_empty(values: Sequence[int | float]) -> None:
def test_native_reducer_quantile_empty(values: Sequence[int | float]) -> None:
with pytest.raises(
EmptySequenceError, match="Cannot compute the quantiles because the summary is empty"
):
BasicReducer().quantile(values, [0.5])
NativeReducer().quantile(values, [0.5])


@pytest.mark.parametrize("values", [[2, 1, -2, 3, 0], (2, 1, -2, 3, 0)])
def test_basic_reducer_sort_int(values: Sequence[int | float]) -> None:
assert BasicReducer().sort(values) == [-2, 0, 1, 2, 3]
def test_native_reducer_sort_int(values: Sequence[int | float]) -> None:
assert NativeReducer().sort(values) == [-2, 0, 1, 2, 3]


@pytest.mark.parametrize("values", [[2.5, 1.5, -2.5, 3.5, 0.5], (2.5, 1.5, -2.5, 3.5, 0.5)])
def test_basic_reducer_sort_float(values: Sequence[int | float]) -> None:
assert BasicReducer().sort(values) == [-2.5, 0.5, 1.5, 2.5, 3.5]
def test_native_reducer_sort_float(values: Sequence[int | float]) -> None:
assert NativeReducer().sort(values) == [-2.5, 0.5, 1.5, 2.5, 3.5]


@pytest.mark.parametrize("values", [[2, 1, -2, 3, 0], (2, 1, -2, 3, 0)])
def test_basic_reducer_sort_descending(values: Sequence[int | float]) -> None:
assert BasicReducer().sort(values, descending=True) == [3, 2, 1, 0, -2]
def test_native_reducer_sort_descending(values: Sequence[int | float]) -> None:
assert NativeReducer().sort(values, descending=True) == [3, 2, 1, 0, -2]


@pytest.mark.parametrize("values", EMPTY_SEQUENCES)
def test_basic_reducer_sort_empty(values: Sequence[int | float]) -> None:
assert BasicReducer().sort(values) == []
def test_native_reducer_sort_empty(values: Sequence[int | float]) -> None:
assert NativeReducer().sort(values) == []


@pytest.mark.parametrize("values", [[-2, -1, 0, 1, 2], (-2, -1, 0, 1, 2)])
def test_basic_reducer_std_int(values: Sequence[int | float]) -> None:
assert math.isclose(BasicReducer().std(values), 1.5811388492584229, abs_tol=1e-6)
def test_native_reducer_std_int(values: Sequence[int | float]) -> None:
assert math.isclose(NativeReducer().std(values), 1.5811388492584229, abs_tol=1e-6)


@pytest.mark.parametrize("values", [[-1.5, -0.5, 0.5, 1.5, 2.5], (-1.5, -0.5, 0.5, 1.5, 2.5)])
def test_basic_reducer_std_float(values: Sequence[int | float]) -> None:
assert math.isclose(BasicReducer().std(values), 1.5811388492584229, abs_tol=1e-6)
def test_native_reducer_std_float(values: Sequence[int | float]) -> None:
assert math.isclose(NativeReducer().std(values), 1.5811388492584229, abs_tol=1e-6)


@pytest.mark.parametrize("values", [[1], [1.0]])
def test_basic_reducer_std_one(values: Sequence[int | float]) -> None:
assert math.isnan(BasicReducer().std(values))
def test_native_reducer_std_one(values: Sequence[int | float]) -> None:
assert math.isnan(NativeReducer().std(values))


@pytest.mark.parametrize("values", EMPTY_SEQUENCES)
def test_basic_reducer_std_empty(values: Sequence[int | float]) -> None:
def test_native_reducer_std_empty(values: Sequence[int | float]) -> None:
with pytest.raises(
EmptySequenceError,
match="Cannot compute the standard deviation because the summary is empty",
):
BasicReducer().std(values)
NativeReducer().std(values)
Loading

0 comments on commit c0deaa2

Please sign in to comment.