Skip to content

Commit

Permalink
Merge branch 'ca-x-single-cat-154381981' into rel-4.2.53
Browse files Browse the repository at this point in the history
  • Loading branch information
Crunch.io Jenkins Account committed Feb 1, 2018
2 parents 5ca3da2 + 3836464 commit 7698c4d
Show file tree
Hide file tree
Showing 6 changed files with 448 additions and 27 deletions.
24 changes: 15 additions & 9 deletions src/cr/cube/crunch_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,8 @@ def _transform(self, res, include_transforms_for_dims, valid_indices):
ind_insertion = indices['anchor_ind'] + ind_offset
if i == 0:
value = sum(res[ind_subtotal_elements])
# res = np.insert(res, ind_insertion + 1, value, axis=i)
else:
value = np.sum(res[:, ind_subtotal_elements], axis=1)
# res = np.insert(res, ind_insertion + 1, value, axis=i)
insertions.append((ind_insertion, value))

for j, (ind_insertion, value) in enumerate(insertions):
Expand Down Expand Up @@ -290,7 +288,7 @@ def _as_array(self, include_missing=False, get_non_selected=False,
get_non_selected)
res = np.array(values).reshape(shape)
res = self._transform(res, include_transforms_for_dims, valid_indices)
res = self._fix_shape(res) + (1 if adjusted else 0)
res = res + adjusted

if prune and not self.has_mr:
return self._prune(res, include_transforms_for_dims)
Expand Down Expand Up @@ -548,7 +546,6 @@ def _inflate_dim(self, array, axis):

def _margin(self, axis=None, weighted=True, adjusted=False,
include_transforms_for_dims=None, prune=False):
'''Calculate cube margin.'''

# MR margins are calculated differently, so they need a separate method
# for them. A good example of this is the rcrunch functionality.
Expand All @@ -566,7 +563,14 @@ def _margin(self, axis=None, weighted=True, adjusted=False,
)
array = self._inflate_dim(array, axis)

res = np.sum(array, axis)
if axis and not isinstance(axis, tuple) and axis > len(array.shape) - 1:
# Handle special case when a dimension is lost due to a
# single element.
res = array
elif axis == (1, 2) and len(array.shape) <= 2:
res = np.sum(array, 1)
else:
res = np.sum(array, axis)

if prune:
# Remove values if 0 or np.nan
Expand Down Expand Up @@ -642,8 +646,9 @@ def _proportions(self, axis=None, weighted=True, adjusted=False,
elif axis == 2:
margin = margin[:, :, np.newaxis]

array = self.as_array(
include_missing=include_missing, weighted=weighted,
array = self._as_array(
include_missing=include_missing,
weighted=weighted,
adjusted=adjusted,
include_transforms_for_dims=include_transforms_for_dims,
prune=prune,
Expand All @@ -656,7 +661,7 @@ def _proportions(self, axis=None, weighted=True, adjusted=False,
len(getattr(margin, 'shape', [])) == 1):
margin = margin[:, np.newaxis, np.newaxis]

return array / margin
return self._fix_shape(array / margin)

# Properties

Expand Down Expand Up @@ -797,13 +802,14 @@ def as_array(self, include_missing=False, weighted=True, adjusted=False,
[0, 0, 0, 0],
])
'''
return self._as_array(
array = self._as_array(
include_missing=include_missing,
weighted=weighted,
adjusted=adjusted,
include_transforms_for_dims=include_transforms_for_dims,
prune=prune,
)
return self._fix_shape(array)

def margin(self, axis=None, weighted=True,
include_transforms_for_dims=None, prune=False):
Expand Down
25 changes: 10 additions & 15 deletions src/cr/cube/dimension.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,21 +99,17 @@ def _elements(self):
@property
def hs_indices(self):
'''Headers and Subtotals indices.'''
elms = self._elements

indices = [{
'anchor_ind': [
i
for (i, el) in enumerate(self._elements)
if el['id'] == subtotal.anchor
][0]
if (subtotal.anchor in [el['id'] for el in self._elements]) else
subtotal.anchor,
# -1,
'inds': [
i
for (i, el) in enumerate(self._elements)
if el['id'] in subtotal.args
],
} for subtotal in self.subtotals]
'anchor_ind': (
[i for (i, el) in enumerate(elms) if el['id'] == st.anchor][0]
if (st.anchor in [el['id'] for el in elms]) else
st.anchor
),
'inds': [i for (i, el) in enumerate(elms) if el['id'] in st.args],
} for st in self.subtotals]

return indices

@property
Expand Down Expand Up @@ -179,7 +175,6 @@ def labels(self, include_missing=False, include_transforms=False,
ind_insert = next((
i for (i, x) in enumerate(labels_with_cat_ids)
if x.get('id') == subtotal.anchor
# ), -1) + 1
), subtotal.anchor)
if ind_insert == 'top':
ind_insert = 0
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 @@ -125,3 +125,4 @@
FIXT_FRUIT_X_PETS_HS_TOP_BOTTOM = load_fixture(
CUBES_DIR, 'fruit-x-pets-hs-top-bottom.json'
)
FIXT_CA_X_SINGLE_CAT = load_fixture(CUBES_DIR, 'ca-x-single-cat.json')
Loading

0 comments on commit 7698c4d

Please sign in to comment.