Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug fix len vs any #407

Merged
merged 1 commit into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/cr/cube/matrix/subtotals.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,8 +459,9 @@ def _subtotal_columns(self):

def _subtotal_column(self, subtotal, default):
"""Return (n_rows,) ndarray of values for `subtotal` column."""
if self._dimensions[1].dimension_type == DT.CAT_DATE and any(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any([0]) returns False and this is a bug cause the [0] means we have a subtrahend in the first position.

subtotal.subtrahend_idxs
if (
self._dimensions[1].dimension_type == DT.CAT_DATE
and len(subtotal.subtrahend_idxs) > 0
):
if self._multiple_subtrahends_or_addends(subtotal):
return self._nan_subtotals(axis=0)
Expand All @@ -480,9 +481,9 @@ def _subtotal_column(self, subtotal, default):

def _subtotal_row(self, subtotal, default):
"""Return (n_cols,) ndarray of values for `subtotal` row."""

if self._dimensions[0].dimension_type == DT.CAT_DATE and any(
subtotal.subtrahend_idxs
if (
self._dimensions[0].dimension_type == DT.CAT_DATE
and len(subtotal.subtrahend_idxs) > 0
):
if self._multiple_subtrahends_or_addends(subtotal):
return self._nan_subtotals(axis=1)
Expand Down
98 changes: 98 additions & 0 deletions tests/unit/matrix/test_subtotals.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
import pytest

from cr.cube.dimension import Dimension, _Subtotal
from cr.cube.enums import DIMENSION_TYPE as DT
from cr.cube.matrix.subtotals import (
_BaseSubtotals,
NanSubtotals,
NegativeTermSubtotals,
PositiveTermSubtotals,
SumSubtotals,
WaveDiffSubtotal,
)

from ...unitutil import ANY, initializer_mock, instance_mock, method_mock, property_mock
Expand Down Expand Up @@ -355,6 +357,102 @@ def subtotal_(self, request):
return instance_mock(request, _Subtotal)


class TestWaveDiffSubtotals:
"""Unit test suite for `cr.cube.matrix.WaveDiffSubtotal` object."""

def test_it_provides_a_subtotal_columns_interface_method(
self, request, dimensions_, _init_
):
base_values = [[0, 4], [7, 9]]
counts = [[10, 20], [30, 40]]
default_insertions = [[0.1, 0.2], [0.3, 0.4]]
property_mock(
request,
WaveDiffSubtotal,
"_subtotal_columns",
return_value=np.array([[1, 2], [3, 4]]),
)

subtotal_columns = WaveDiffSubtotal.subtotal_columns(
base_values, counts, default_insertions, dimensions_
)

_init_.assert_called_once_with(
ANY, base_values, counts, default_insertions, dimensions_
)
assert subtotal_columns.tolist() == [[1, 2], [3, 4]]

def test_it_provides_a_subtotal_rows_interface_method(
self, request, dimensions_, _init_
):
base_values = [[4, 1], [3, 5]]
counts = [[10, 20], [30, 40]]
default_insertions = [[0.1, 0.2], [0.3, 0.4]]
property_mock(
request,
WaveDiffSubtotal,
"_subtotal_rows",
return_value=np.array([[4, 3], [2, 1]]),
)

subtotal_rows = WaveDiffSubtotal.subtotal_rows(
base_values, counts, default_insertions, dimensions_
)

_init_.assert_called_once_with(
ANY, base_values, counts, default_insertions, dimensions_
)
assert subtotal_rows.tolist() == [[4, 3], [2, 1]]

def test_renders_row_wave_diff_under_specific_conditions(
self, dimensions_, subtotal_
):
dimensions_[1].dimension_type = DT.CAT
dimensions_[0].dimension_type = DT.CAT_DATE
subtotal_.subtrahend_idxs = [0]
subtotal_.addend_idxs = [1]
base_values = np.array([[4, 1], [3, 5]])
counts = np.array([[10, 20], [30, 40]])
default_insertion = [0.1, 0.2]

subtotal = WaveDiffSubtotal(base_values, counts, default_insertion, dimensions_)

assert subtotal._subtotal_row(subtotal_, default_insertion) == pytest.approx(
np.array([7.5, -12.0])
)

def test_renders_col_wave_diff_under_specific_conditions(
self, dimensions_, subtotal_
):
dimensions_[1].dimension_type = DT.CAT_DATE
dimensions_[0].dimension_type = DT.CAT
subtotal_.subtrahend_idxs = [0]
subtotal_.addend_idxs = [1]
base_values = np.array([[4, 1], [3, 5]])
counts = np.array([[10, 20], [30, 40]])
default_insertion = [0.1, 0.2]

subtotal = WaveDiffSubtotal(base_values, counts, default_insertion, dimensions_)

assert subtotal._subtotal_column(subtotal_, default_insertion) == pytest.approx(
np.array([17.5, -2.0])
)

# --- fixture components -----------------------------------------

@pytest.fixture
def dimensions_(self, request):
return (instance_mock(request, Dimension), instance_mock(request, Dimension))

@pytest.fixture
def _init_(self, request):
return initializer_mock(request, WaveDiffSubtotal)

@pytest.fixture
def subtotal_(self, request):
return instance_mock(request, _Subtotal)


class TestSumSubtotals:
"""Unit test suite for `cr.cube.matrix.SumSubtotals` object."""

Expand Down