Skip to content

Commit

Permalink
[#157391555]: Fix MR x CA margin calculation (as in whaam)
Browse files Browse the repository at this point in the history
  • Loading branch information
slobodan-ilic committed May 9, 2018
1 parent e90cf3f commit d84b703
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/cr/cube/crunch_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,28 +536,35 @@ def _transform_table(self, table, include_transforms_for_dims):
)
return table

def _mr_margin(self, axis, weighted, adjusted,
include_transforms_for_dims=None, prune=False):
def _mr_margin(self, axis, weighted, adjusted, hs_dims=None, prune=False):
'''Margin for cube that contains MR.'''
if self.is_double_mr:
return self._double_mr_margin(axis, weighted)
elif self.ndim == 1:
return self._1d_mr_margin(axis, weighted)
elif self._calculate_along_non_mr(axis):
return self._mr_margin_along_non_mr_dim(axis, weighted,
include_transforms_for_dims)
return self._mr_margin_along_non_mr_dim(axis, weighted, hs_dims)

is_ca_row_margin = (
self.ndim == 3 and
self.dimensions[1].type == 'categorical_array' and
axis is not None
)
if is_ca_row_margin:
# For MR x CA always return row margin (only one that makes sense)
return np.sum(self.as_array(), axis=2)

table = self.table.data(weighted, margin=True)
if include_transforms_for_dims:
if hs_dims:
# In case of H&S the entire table needs to be
# transformed (with selections).
table = self._transform_table(table, include_transforms_for_dims)
table = self._transform_table(table, hs_dims)

# For cases when the margin is calculated for the MR dimension, we need
# the sum of selected and non-selected slices (if axis is None), or the
# sublimated version (another sum along the axis), if axis is defined.
margin = table[self.ind_selected] + table[self.ind_non_selected]
if not include_transforms_for_dims:
if not hs_dims:
# If entire table was transformed, we already have it with all the
# valid indices. If not, we need to apply valid indices.
margin = margin[np.ix_(*self.valid_indices)]
Expand Down Expand Up @@ -654,7 +661,7 @@ def _mr_props_as_0th(self, axis, table, hs_dims, prune):
prune=prune,
)
if self.dimensions[1].type == 'categorical_array':
den = np.sum(self.as_array(), axis=2)[:, :, None]
den = self.margin(axis=2)[:, :, None]
else:
den = self.margin(axis=1)[:, None, :]
return num / den
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 @@ -770,3 +770,13 @@ def test_mr_x_ca_props_by_row_with_hs(self):
])
actual = cube.proportions(include_transforms_for_dims=[0, 1, 2])
np.testing.assert_almost_equal(actual, expected)

def test_mr_x_ca_rows_margin(self):
cube = CrunchCube(MR_X_CA_HS)
actual = cube.margin(axis=2)
expected = np.array([
[3, 3, 3],
[4, 4, 4],
[0, 0, 0],
])
np.testing.assert_array_equal(actual, expected)

0 comments on commit d84b703

Please sign in to comment.