diff --git a/src/cr/cube/matrix/measure.py b/src/cr/cube/matrix/measure.py index 9268534b6..0ea1d8b64 100644 --- a/src/cr/cube/matrix/measure.py +++ b/src/cr/cube/matrix/measure.py @@ -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): diff --git a/tests/integration/test_headers_and_subtotals.py b/tests/integration/test_headers_and_subtotals.py index 2bd4892ef..47a75a1c3 100644 --- a/tests/integration/test_headers_and_subtotals.py +++ b/tests/integration/test_headers_and_subtotals.py @@ -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( @@ -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 = [ diff --git a/tests/unit/matrix/test_measure.py b/tests/unit/matrix/test_measure.py index 232887482..c8f80a5a9 100644 --- a/tests/unit/matrix/test_measure.py +++ b/tests/unit/matrix/test_measure.py @@ -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",