Skip to content

Commit

Permalink
Merge pull request #408 from Crunch-io/bug-on-scale-means-diff-188552693
Browse files Browse the repository at this point in the history
Bug on scale means diff 188552693
  • Loading branch information
ernestoarbitrio authored Dec 4, 2024
2 parents 77fa191 + b602c0b commit 441777c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 18 deletions.
17 changes: 12 additions & 5 deletions src/cr/cube/matrix/measure.py
Original file line number Diff line number Diff line change
Expand Up @@ -2906,15 +2906,22 @@ def is_defined(self):
@lazyproperty
def _proportions(self):
"""List of 2 ndarray matrixes of the relevant proportion blocks"""
count_blocks = self._second_order_measures.weighted_counts.blocks
if self.orientation == MO.ROWS:
# --- Use *row* proportions ---
props = self._second_order_measures.row_proportions.blocks
weighted_base_blocks = self._second_order_measures.row_weighted_bases.blocks
# --- Get base values & *row* subtotals
return [props[0][0], props[1][0]]
with np.errstate(divide="ignore", invalid="ignore"):
base_values = count_blocks[0][0] / weighted_base_blocks[0][0]
row_subtotals = count_blocks[1][0] / weighted_base_blocks[1][0]
return [base_values, row_subtotals]
# --- Use *column* proportions ---
props = self._second_order_measures.column_proportions.blocks
# --- Get base values & *column* subtotals
return [props[0][0], props[0][1]]
weighted_base_blocks = self._second_order_measures.column_weighted_bases.blocks
with np.errstate(divide="ignore", invalid="ignore"):
base_values = count_blocks[0][0] / weighted_base_blocks[0][0]
col_subtotals = count_blocks[0][1] / weighted_base_blocks[0][1]
# --- Get base values & *column* subtotals
return [base_values, col_subtotals]

@staticmethod
def _weighted_mean(proportions, values):
Expand Down
10 changes: 10 additions & 0 deletions tests/integration/test_headers_and_subtotals.py
Original file line number Diff line number Diff line change
Expand Up @@ -2980,6 +2980,12 @@ def test_it_computes_diff_for_cat_x_cat_date_with_subdiffs_on_both(self):
nan_ok=True,
rel=1e-4,
)
assert slice_.columns_scale_mean == pytest.approx(
np.array(
[np.nan, 2.26559356, 2.43811395, 2.08453608, 1.9968254, 1.81967213]
),
nan_ok=True,
)

def test_it_computes_diff_for_cat_date_x_cat_with_subdiffs_on_both(self):
slice_ = Cube(
Expand Down Expand Up @@ -3083,6 +3089,10 @@ def test_it_computes_diff_for_cat_date_x_cat_with_subdiffs_on_both(self):
),
nan_ok=True,
)
assert slice_.rows_scale_mean == pytest.approx(
np.array([np.nan, 1.625, 1.4675835, 1.78818737, 1.68138801, 1.46994536]),
nan_ok=True,
)

def test_it_computes_diff_for_cat_date_x_cat_with_subdiffs_on_rows(self):
insertions = [
Expand Down
30 changes: 17 additions & 13 deletions tests/unit/matrix/test_measure.py
Original file line number Diff line number Diff line change
Expand Up @@ -2758,30 +2758,34 @@ def test_it_can_tell_if_it_is_defined(self, request, values, expected):
assert mean.is_defined == expected

def test_it_gets_the_right_proportions_for_rows(self, request):
row_proportions_ = instance_mock(
request, _RowProportions, blocks=[["a", "b"], ["c", "d"]]
)
second_order_measures_ = instance_mock(
request, SecondOrderMeasures, row_proportions=row_proportions_
second_order_measures_ = instance_mock(request, SecondOrderMeasures)
counts_ = instance_mock(request, _WeightedCounts, blocks=[[1, 2], [3, 4]])
bases_ = instance_mock(
request,
_RowWeightedBases,
blocks=[[11, 12], [13, 14]],
)
second_order_measures_.weighted_counts = counts_
second_order_measures_.row_weighted_bases = bases_

mean = _ScaleMean(None, second_order_measures_, None, MO.ROWS)

assert mean._proportions == ["a", "c"]
assert mean._proportions == [0.09090909090909091, 0.23076923076923078]

def test_it_gets_the_right_counts_for_columns(self, request):
column_proportions_ = instance_mock(
request, _ColumnProportions, blocks=[["a", "b"], ["c", "d"]]
)
second_order_measures_ = instance_mock(
second_order_measures_ = instance_mock(request, SecondOrderMeasures)
counts_ = instance_mock(request, _WeightedCounts, blocks=[[1, 2], [3, 4]])
bases_ = instance_mock(
request,
SecondOrderMeasures,
column_proportions=column_proportions_,
_ColumnWeightedBases,
blocks=[[11, 12], [13, 14]],
)
second_order_measures_.weighted_counts = counts_
second_order_measures_.column_weighted_bases = bases_

mean = _ScaleMean(None, second_order_measures_, None, MO.COLUMNS)

assert mean._proportions == ["a", "b"]
assert mean._proportions == [0.09090909090909091, 0.16666666666666666]

@pytest.mark.parametrize(
"proportions, values, expected",
Expand Down

0 comments on commit 441777c

Please sign in to comment.