Skip to content

Commit

Permalink
Implement correct handling of missing labels
Browse files Browse the repository at this point in the history
  • Loading branch information
slobodan-ilic committed Apr 12, 2018
1 parent 926cc8a commit b2dc7bc
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 34 deletions.
32 changes: 23 additions & 9 deletions src/cr/cube/dimension.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,19 @@ def labels(self, include_missing=False, include_transforms=False,
'id': el['id'],
'name': self._get_name(el),
} for (i, el) in enumerate(self._elements)]
labels_with_cat_ids = self._update_with_subtotals(labels_with_cat_ids)

return [
(
label['name']
if not include_cat_ids else
(label['name'], label.get('id', -1))
)
for label in labels_with_cat_ids
if self._include_in_labels(label, valid_indices)
]

def _update_with_subtotals(self, labels_with_cat_ids):
for subtotal in self.subtotals:
# If anchor is 0, the default value of 'next' (which is -1) will
# add with 1, and amount to the total of 0, which will be the
Expand All @@ -230,15 +243,16 @@ def labels(self, include_missing=False, include_transforms=False,
ind_insert += 1
labels_with_cat_ids.insert(ind_insert, subtotal.data)

return [
(
label['name']
if not include_cat_ids else
(label['name'], label.get('id', -1))
)
for label in labels_with_cat_ids
if not label.get('ind') or label['ind'] in valid_indices
]
return labels_with_cat_ids

@staticmethod
def _include_in_labels(label_with_ind, valid_indices):
if label_with_ind.get('ind') is None:
# In this case, it's a transformation and not an element of the
# cube. Thus, needs to be included in resulting labels.
return True

return label_with_ind['ind'] in valid_indices

def elements(self, include_missing=False):
'''Get elements of the crunch Dimension.
Expand Down
17 changes: 17 additions & 0 deletions tests/integration/test_headers_and_subtotals.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from .fixtures import CAT_X_DATE_HS_PRUNE
from .fixtures import CAT_X_NUM_HS_PRUNE
from .fixtures import PETS_X_FRUIT_HS
from .fixtures import MISSING_CAT_HS

from cr.cube.crunch_cube import CrunchCube

Expand Down Expand Up @@ -607,3 +608,19 @@ def test_mr_x_cat_hs_props_by_col(self):
axis=1, include_transforms_for_dims=[0, 1]
).shape
np.testing.assert_array_equal(actual, expected)

def test_missing_cat_hs_labels(self):
cube = CrunchCube(MISSING_CAT_HS)

# Don't expect the missing category "Non voters"
expected = [[
'Whites',
'White college women voters',
'White non-college women voters',
'White college men voters',
'White non-college men voters',
'Black voters',
'Latino and other voters',
]]
actual = cube.labels(include_transforms_for_dims=[0])
assert actual == expected
25 changes: 0 additions & 25 deletions tests/integration/test_pruning.py

This file was deleted.

0 comments on commit b2dc7bc

Please sign in to comment.