diff --git a/src/cr/cube/dimension.py b/src/cr/cube/dimension.py index 6ccac595b..f6aabed30 100644 --- a/src/cr/cube/dimension.py +++ b/src/cr/cube/dimension.py @@ -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 @@ -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. diff --git a/tests/integration/test_headers_and_subtotals.py b/tests/integration/test_headers_and_subtotals.py index 06bd7e26e..9cfc6c6e3 100644 --- a/tests/integration/test_headers_and_subtotals.py +++ b/tests/integration/test_headers_and_subtotals.py @@ -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 @@ -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 diff --git a/tests/integration/test_pruning.py b/tests/integration/test_pruning.py deleted file mode 100644 index 5d9c87829..000000000 --- a/tests/integration/test_pruning.py +++ /dev/null @@ -1,25 +0,0 @@ -from unittest import TestCase -from mock import patch -import numpy as np - -from cr.cube.crunch_cube import CrunchCube - -from .fixtures import MISSING_CAT_HS - - -class TestCrunchCube(TestCase): - 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