Skip to content

Commit

Permalink
Merge branch 'fix-mr-labels-pruning-156237403' into rel-5.1.24
Browse files Browse the repository at this point in the history
  • Loading branch information
Crunch.io Jenkins Account committed Apr 5, 2018
2 parents 9151dae + 6983ff2 commit 531a912
Show file tree
Hide file tree
Showing 4 changed files with 2,853 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/cr/cube/crunch_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,18 +195,29 @@ def _as_array(self, include_missing=False, get_non_selected=False,
)
res = res + adjusted

if prune and not self.has_mr:
if prune:
return self._prune(res, include_transforms_for_dims)

return res

def _mr_prune(self, res):
if len(res.shape) > 2:
# Only perform pruning for 1-D MR cubes.
return res
margin = self.margin(axis=0)
ind_prune = margin == 0
return res[~ind_prune]

def _prune(self, res, transforms=None):
'''Prune the result based on margins content.
Pruning is the removal of rows or columns, whose corresponding
marginal elements are either 0 or not defined (np.nan).
'''

if self.has_mr:
return self._mr_prune(res)

# Note: If the cube contains transforms (H&S), and they happen to
# have the marginal value 0 (or NaN), they're NOT pruned. This is
# done to achieve parity with how the front end client works (whaam).
Expand Down Expand Up @@ -412,7 +423,8 @@ def _margin(self, axis=None, weighted=True, adjusted=False,

return res

def _mr_proportions(self, axis, weighted, include_transforms_for_dims=None):
def _mr_proportions(self, axis, weighted, prune,
include_transforms_for_dims=None):
'''Calculate MR proportions.'''

if self.is_double_mr:
Expand All @@ -425,7 +437,10 @@ def _mr_proportions(self, axis, weighted, include_transforms_for_dims=None):

if len(self.dimensions) == 1:
res = table[:, 0] / (table[:, 0] + table[:, 1])
return res[np.ix_(*valid_indices)]
res = res[np.ix_(*valid_indices)]
if prune:
res = res[res != 0]
return res

if self.mr_dim_ind == 0:
if len(table.shape) == 4:
Expand Down Expand Up @@ -491,7 +506,7 @@ def _proportions(self, axis=None, weighted=True, adjusted=False,
prune=False):
if self.has_mr:
return self._mr_proportions(
axis, weighted,
axis, weighted, prune,
include_transforms_for_dims=include_transforms_for_dims
)

Expand Down
1 change: 1 addition & 0 deletions tests/integration/fixtures/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,4 @@
FIXT_SCALE_WITH_NULL_VALUES = load_fixture(CUBES_DIR,
'scale-with-null-values.json')
PETS_X_FRUIT_HS = load_fixture(CUBES_DIR, 'pets-x-fruit-hs.json')
PROMPTED_AWARENESS = load_fixture(CUBES_DIR, 'prompted-awareness.json')
Loading

0 comments on commit 531a912

Please sign in to comment.