Skip to content

Commit

Permalink
Merge branch 'hs-for-mr-155641308' into rel-5.0.64
Browse files Browse the repository at this point in the history
  • Loading branch information
Crunch.io Jenkins Account committed Mar 27, 2018
2 parents ef3db7d + d418bcd commit 119b0c3
Show file tree
Hide file tree
Showing 4 changed files with 455 additions and 24 deletions.
59 changes: 35 additions & 24 deletions src/cr/cube/crunch_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,39 +127,38 @@ def _fix_shape(cls, array):

def _update_result(self, result, insertions, dimension_index,
valid_indices):
'''Actually insert subtotals into resulting ndarray.'''
'''Insert subtotals into resulting ndarray.'''
for j, (ind_insertion, value) in enumerate(insertions):
if dimension_index == 0:
result = np.insert(
result, ind_insertion + j + 1, value, axis=dimension_index
)
else:
result = np.insert(
result, ind_insertion + j + 1, value, axis=dimension_index
)
result = np.insert(
result, ind_insertion + j + 1, value, axis=dimension_index
)
valid_indices = self._fix_valid_indices(
valid_indices, ind_insertion + j, dimension_index
)
return result, valid_indices

def _transform(self, res, include_transforms_for_dims, valid_indices):
def _transform(self, res, include_transforms_for_dims, valid_indices,
inflate=False):
'''Transform the shape of the resulting ndarray.'''

all_dimensions = self.table.all_dimensions

if not include_transforms_for_dims:
return res[np.ix_(*valid_indices)]

for (i, dim) in enumerate(all_dimensions):
dim_offset = 0
dims = self.table.all_dimensions if self.has_mr else self.dimensions
for (i, dim) in enumerate(dims):
# Check if transformations can/need to be performed
transform = (dim.has_transforms and
i in include_transforms_for_dims)
i - dim_offset in include_transforms_for_dims)
if dim.type == 'multiple_response':
dim_offset += 1
if not transform or dim.type == 'categorical_array':
continue
# Perform transformations
insertions = self._insertions(res, dim, i)
res, valid_indices = self._update_result(res, insertions, i,
valid_indices)
ind = i if inflate else i - dim_offset
res, valid_indices = self._update_result(
res, insertions, ind, valid_indices
)
return res[np.ix_(*valid_indices)]

def _as_array(self, include_missing=False, get_non_selected=False,
Expand Down Expand Up @@ -191,7 +190,9 @@ def _as_array(self, include_missing=False, get_non_selected=False,
valid_indices = self._get_valid_indices(dimensions, include_missing,
get_non_selected)
res = np.array(values).reshape(shape)
res = self._transform(res, include_transforms_for_dims, valid_indices)
res = self._transform(
res, include_transforms_for_dims, valid_indices, inflate=True
)
res = res + adjusted

if prune and not self.has_mr:
Expand Down Expand Up @@ -405,7 +406,7 @@ def _margin(self, axis=None, weighted=True, adjusted=False,

return res

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

if self.is_double_mr:
Expand Down Expand Up @@ -433,7 +434,11 @@ def _mr_proportions(self, axis, weighted):

# The following are normal MR x something (not CA)
if axis == 1:
return self.as_array() / self.margin(axis=1)[:, np.newaxis]
num = self.as_array(
include_transforms_for_dims=include_transforms_for_dims
)
den = self.margin(axis=1)[:, np.newaxis]
return num / den
else:
res = table[:, 0, :] / (table[:, 0, :] + table[:, 1, :])
elif self.mr_dim_ind == 1:
Expand All @@ -445,9 +450,11 @@ def _mr_proportions(self, axis, weighted):
den = num + non_selected
else:
axis = 0 if len(num.shape) < 3 else (1, 2)
# num[np.ix_(*self.valid_indices)] / np.sum(num[np.ix_(*self.valid_indices)] + non_selected[np.ix_(*valid_indices)], (1, 2))
den = np.sum(num + non_selected, axis)
return num / den
res = num / den
return self._transform(
res, include_transforms_for_dims, self.valid_indices
)
elif self.mr_dim_ind == 2:
margin = (
self.margin(axis=axis)[:, np.newaxis]
Expand All @@ -456,7 +463,8 @@ def _mr_proportions(self, axis, weighted):
)
return self.as_array() / margin

return res[np.ix_(*valid_indices)]
return self._transform(res, include_transforms_for_dims, valid_indices)
# return res[np.ix_(*valid_indices)]

@property
def is_univariate_ca(self):
Expand All @@ -467,7 +475,10 @@ def _proportions(self, axis=None, weighted=True, adjusted=False,
include_transforms_for_dims=None, include_missing=False,
prune=False):
if self.has_mr:
return self._mr_proportions(axis, weighted)
return self._mr_proportions(
axis, weighted,
include_transforms_for_dims=include_transforms_for_dims
)

if self.is_univariate_ca and axis != 1:
raise ValueError('CA props only defined for row direction.')
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 @@ -144,3 +144,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')
Loading

0 comments on commit 119b0c3

Please sign in to comment.