Skip to content

Commit

Permalink
fix scale means insertions when a diff on cat date
Browse files Browse the repository at this point in the history
  • Loading branch information
ernestoarbitrio committed Dec 3, 2024
1 parent 77fa191 commit e487bcd
Show file tree
Hide file tree
Showing 2 changed files with 29 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
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 e487bcd

Please sign in to comment.