From ac5ace31c962ae3547b3d317d39e80ea2044d792 Mon Sep 17 00:00:00 2001 From: Slobodan Ilic Date: Sat, 16 Feb 2019 10:33:21 +0100 Subject: [PATCH 1/3] Add pruning to min base size masks --- src/cr/cube/cube_slice.py | 4 ++-- src/cr/cube/min_base_size_mask.py | 30 +++++++++++++++++++++--------- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/cr/cube/cube_slice.py b/src/cr/cube/cube_slice.py index f5fe30ee3..cea0594cf 100644 --- a/src/cr/cube/cube_slice.py +++ b/src/cr/cube/cube_slice.py @@ -278,7 +278,7 @@ def margin( return self._extract_slice_result_from_cube(margin) - def min_base_size_mask(self, size, hs_dims=None): + def min_base_size_mask(self, size, hs_dims=None, prune=False): """Returns MinBaseSizeMask object with correct row, col and table masks. The returned object stores the necessary information about the base size, as @@ -292,7 +292,7 @@ def min_base_size_mask(self, size, hs_dims=None): >>> cube_slice.min_base_size_mask(50).column_mask >>> cube_slice.min_base_size_mask(22).table_mask """ - return MinBaseSizeMask(self, size, hs_dims) + return MinBaseSizeMask(self, size, hs_dims=hs_dims, prune=prune) @lazyproperty def mr_dim_ind(self): diff --git a/src/cr/cube/min_base_size_mask.py b/src/cr/cube/min_base_size_mask.py index f796c5012..969b0a738 100644 --- a/src/cr/cube/min_base_size_mask.py +++ b/src/cr/cube/min_base_size_mask.py @@ -5,7 +5,7 @@ from __future__ import division import numpy as np -from cr.cube.util import lazyproperty +from cr.cube.util import lazyproperty, compress_pruned from cr.cube.enum import DIMENSION_TYPE as DT @@ -18,18 +18,22 @@ class MinBaseSizeMask: marginal values and the shape of the underlying slice. """ - def __init__(self, slice_, size, hs_dims=None): + def __init__(self, slice_, size, hs_dims=None, prune=False): self._slice = slice_ self._size = size self._hs_dims = hs_dims + self._prune = prune @lazyproperty def column_mask(self): """ndarray, True where column margin <= min_base_size, same shape as slice.""" margin = self._slice.margin( - axis=0, weighted=False, include_transforms_for_dims=self._hs_dims + axis=0, + weighted=False, + include_transforms_for_dims=self._hs_dims, + prune=self._prune, ) - mask = margin < self._size + mask = compress_pruned(margin) < self._size if margin.shape == self._shape: # If margin shape is the same as slice's (such as in a col margin for @@ -44,9 +48,12 @@ def column_mask(self): def row_mask(self): """ndarray, True where row margin <= min_base_size, same shape as slice.""" margin = self._slice.margin( - axis=1, weighted=False, include_transforms_for_dims=self._hs_dims + axis=1, + weighted=False, + include_transforms_for_dims=self._hs_dims, + prune=self._prune, ) - mask = margin < self._size + mask = compress_pruned(margin) < self._size if margin.shape == self._shape: # If margin shape is the same as slice's (such as in a row margin for @@ -60,8 +67,13 @@ def row_mask(self): @lazyproperty def table_mask(self): """ndarray, True where table margin <= min_base_size, same shape as slice.""" - margin = self._slice.margin(axis=None, weighted=False) - mask = margin < self._size + margin = self._slice.margin( + axis=None, + weighted=False, + include_transforms_for_dims=self._hs_dims, + prune=self._prune, + ) + mask = compress_pruned(margin) < self._size if margin.shape == self._shape: return mask @@ -74,7 +86,7 @@ def table_mask(self): @lazyproperty def _shape(self): - shape = self._slice.get_shape(hs_dims=self._hs_dims) + shape = self._slice.get_shape(hs_dims=self._hs_dims, prune=self._prune) if len(shape) != self._slice.ndim: # TODO: This is an ugly hack that needs to happen due to the fact that we From 25455c181ca46edcc382abe6ae9e7ada97c9a63d Mon Sep 17 00:00:00 2001 From: Slobodan Ilic Date: Sat, 16 Feb 2019 11:41:23 +0100 Subject: [PATCH 2/3] Fix how pruning is interpreted in min base size --- src/cr/cube/min_base_size_mask.py | 44 ++++++++++++++++++------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/src/cr/cube/min_base_size_mask.py b/src/cr/cube/min_base_size_mask.py index 969b0a738..d27612a88 100644 --- a/src/cr/cube/min_base_size_mask.py +++ b/src/cr/cube/min_base_size_mask.py @@ -27,13 +27,15 @@ def __init__(self, slice_, size, hs_dims=None, prune=False): @lazyproperty def column_mask(self): """ndarray, True where column margin <= min_base_size, same shape as slice.""" - margin = self._slice.margin( - axis=0, - weighted=False, - include_transforms_for_dims=self._hs_dims, - prune=self._prune, + margin = compress_pruned( + self._slice.margin( + axis=0, + weighted=False, + include_transforms_for_dims=self._hs_dims, + prune=self._prune, + ) ) - mask = compress_pruned(margin) < self._size + mask = margin < self._size if margin.shape == self._shape: # If margin shape is the same as slice's (such as in a col margin for @@ -47,13 +49,15 @@ def column_mask(self): @lazyproperty def row_mask(self): """ndarray, True where row margin <= min_base_size, same shape as slice.""" - margin = self._slice.margin( - axis=1, - weighted=False, - include_transforms_for_dims=self._hs_dims, - prune=self._prune, + margin = compress_pruned( + self._slice.margin( + axis=1, + weighted=False, + include_transforms_for_dims=self._hs_dims, + prune=self._prune, + ) ) - mask = compress_pruned(margin) < self._size + mask = margin < self._size if margin.shape == self._shape: # If margin shape is the same as slice's (such as in a row margin for @@ -67,13 +71,15 @@ def row_mask(self): @lazyproperty def table_mask(self): """ndarray, True where table margin <= min_base_size, same shape as slice.""" - margin = self._slice.margin( - axis=None, - weighted=False, - include_transforms_for_dims=self._hs_dims, - prune=self._prune, + margin = compress_pruned( + self._slice.margin( + axis=None, + weighted=False, + include_transforms_for_dims=self._hs_dims, + prune=self._prune, + ) ) - mask = compress_pruned(margin) < self._size + mask = margin < self._size if margin.shape == self._shape: return mask @@ -93,6 +99,6 @@ def _shape(self): # purge dimensions with the count of 1, when getting the slice shape. This # will be addressed in a PR (already on the way) that strives to abandon # the ad-hoc purging of 1-element dimensions altogether. - shape = (shape[0], 1) + shape = (shape[0], 1) if shape else (1,) return shape From e6c056b95b1feaaddd1d2f3bec8be16814280806 Mon Sep 17 00:00:00 2001 From: Slobodan Ilic Date: Mon, 18 Feb 2019 18:33:51 +0100 Subject: [PATCH 3/3] Bump version --- README.md | 3 +++ src/cr/cube/__init__.py | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4c027bb6d..f4cf1527a 100644 --- a/README.md +++ b/README.md @@ -112,6 +112,9 @@ The detailed description can be found ## Changes +#### 1.9.8 +- Enable pruning for min-base-size masks + #### 1.9.7 - Implement Min Base Size suppression masks diff --git a/src/cr/cube/__init__.py b/src/cr/cube/__init__.py index 37aa4c6d3..5f14ff077 100644 --- a/src/cr/cube/__init__.py +++ b/src/cr/cube/__init__.py @@ -2,4 +2,4 @@ """Initialization module for crunch-cube package.""" -__version__ = "1.9.7" +__version__ = "1.9.8"