Skip to content

Commit

Permalink
Merge branch 'hs-formatting-154780995' into rel-4.2.54
Browse files Browse the repository at this point in the history
  • Loading branch information
Crunch.io Jenkins Account committed Feb 2, 2018
2 parents 1256e4f + 70a1275 commit b9443d4
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 209 deletions.
5 changes: 5 additions & 0 deletions src/cr/cube/crunch_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,11 @@ def is_double_mr(self):
return True
return False

@property
def inserted_hs_indices(self):
'''Get indices of the inserted H&S (for formatting purposes).'''
return [dim.inserted_hs_indices for dim in self.dimensions]

# API Functions

def labels(self, include_missing=False, include_transforms_for_dims=False):
Expand Down
23 changes: 19 additions & 4 deletions src/cr/cube/dimension.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ def __init__(self, dim, selections=None):

@property
def subtotals(self):
insertions_data = self._dim.get('references', {}) \
.get('view', {}) \
.get('transform', {}) \
.get('insertions', [])
view = self._dim.get('references', {}).get('view', {})

if not view:
# View can be both None and {}, thus the edge case.
return []

insertions_data = view.get('transform', {}).get('insertions', [])
subtotals = [Subtotal(data) for data in insertions_data]
return [subtotal for subtotal in subtotals if subtotal.is_valid]

Expand Down Expand Up @@ -96,6 +99,18 @@ def _elements(self):
return self._dim['type']['categories']
return self._dim['type']['elements']

@property
def inserted_hs_indices(self):
tops = [st for st in self.subtotals if st.anchor == 'top']
bottoms = [st for st in self.subtotals if st.anchor == 'bottom']
middles = [st for st in self.subtotals if st.anchor not in ['top', 'bottom']]

top_inds = [i for i, _ in enumerate(tops)]
middle_inds = [i + m.anchor + len(tops) for i, m in enumerate(middles)]
bottom_inds = [i + len(tops) + len(middles) + len(self.elements())
for i, _ in enumerate(bottoms)]
return top_inds + middle_inds + bottom_inds

@property
def hs_indices(self):
'''Headers and Subtotals indices.'''
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
8
],
"name": "Test Heading with Skipped",
"anchor": 8
"anchor": "bottom"
}
]
},
Expand Down
192 changes: 0 additions & 192 deletions tests/integration/test_crunch_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -1278,146 +1278,6 @@ def test_pets_array_margin_by_col(self):
actual = cube.margin(axis=0)
np.testing.assert_array_equal(actual, expected)

def test_headings_econ_blame_one_subtotal(self):
cube = CrunchCube(FIXT_ECON_BLAME_WITH_HS)
expected = [
'President Obama',
'Republicans in Congress',
'Test New Heading (Obama and Republicans)',
'Both',
'Neither',
'Not sure',
]
actual = cube.labels(include_transforms_for_dims=[0])[0]
self.assertEqual(actual, expected)

def test_headings_econ_blame_one_subtotal_do_not_fetch(self):
cube = CrunchCube(FIXT_ECON_BLAME_WITH_HS)
expected = [
'President Obama',
'Republicans in Congress',
'Both',
'Neither',
'Not sure',
]
actual = cube.labels(include_transforms_for_dims=None)[0]
self.assertEqual(actual, expected)

def test_headings_econ_blame_two_subtotal_without_missing(self):
cube = CrunchCube(FIXT_ECON_BLAME_WITH_HS_MISSING)
expected = [
'President Obama',
'Republicans in Congress',
'Test New Heading (Obama and Republicans)',
'Both',
'Neither',
'Not sure',
'Test Heading with Skipped',
]
actual = cube.labels(include_transforms_for_dims=[0])[0]
self.assertEqual(actual, expected)

def test_headings_two_subtotal_without_missing_do_not_fetch(self):
cube = CrunchCube(FIXT_ECON_BLAME_WITH_HS_MISSING)
expected = [
'President Obama',
'Republicans in Congress',
'Both',
'Neither',
'Not sure',
]
actual = cube.labels(include_transforms_for_dims=None)[0]
self.assertEqual(actual, expected)

def test_headings_econ_blame_two_subtotal_with_missing(self):
cube = CrunchCube(FIXT_ECON_BLAME_WITH_HS_MISSING)
expected = [
'President Obama',
'Republicans in Congress',
'Test New Heading (Obama and Republicans)',
'Both',
'Neither',
'Not sure',
'Skipped',
'Test Heading with Skipped',
'Not Asked',
'No Data',
]
actual = cube.labels(include_missing=True, include_transforms_for_dims=[0])[0]
self.assertEqual(actual, expected)

def test_subtotals_as_array_one_transform(self):
cube = CrunchCube(FIXT_ECON_BLAME_WITH_HS)
expected = np.array([285, 396, 681, 242, 6, 68])
actual = cube.as_array(include_transforms_for_dims=[0])
np.testing.assert_array_equal(actual, expected)

def test_subtotals_as_array_one_transform_do_not_fetch(self):
cube = CrunchCube(FIXT_ECON_BLAME_WITH_HS)
expected = np.array([285, 396, 242, 6, 68])
actual = cube.as_array(include_transforms_for_dims=None)
np.testing.assert_array_equal(actual, expected)

def test_subtotals_as_array_two_transforms_missing_excluded(self):
cube = CrunchCube(FIXT_ECON_BLAME_WITH_HS_MISSING)
expected = np.array([285, 396, 681, 242, 6, 68, 77])
actual = cube.as_array(include_transforms_for_dims=[0])
np.testing.assert_array_equal(actual, expected)

def test_subtotals_as_array_two_transforms_missing_included(self):
cube = CrunchCube(FIXT_ECON_BLAME_WITH_HS_MISSING)
expected = np.array([285, 396, 681, 242, 6, 68, 3, 77, 0, 0])
actual = cube.as_array(include_missing=True, include_transforms_for_dims=[0])
np.testing.assert_array_equal(actual, expected)

def test_subtotals_proportions_one_transform(self):
cube = CrunchCube(FIXT_ECON_BLAME_WITH_HS)
expected = np.array([
.2858576, .3971916, .6830491, .2427282, .0060181, .0682046,
])
actual = cube.proportions(include_transforms_for_dims=[0])
np.testing.assert_almost_equal(actual, expected)

def test_subtotals_proportions_one_transform_do_not_fetch(self):
cube = CrunchCube(FIXT_ECON_BLAME_WITH_HS)
expected = np.array([
.2858576, .3971916, .2427282, .0060181, .0682046,
])
actual = cube.proportions(include_transforms_for_dims=None)
np.testing.assert_almost_equal(actual, expected)

def test_subtotals_proportions_two_transforms_missing_excluded(self):
cube = CrunchCube(FIXT_ECON_BLAME_WITH_HS_MISSING)
expected = np.array([
.2858576,
.3971916,
.6830491,
.2427282,
.0060181,
.0682046,
.0772317,
])
actual = cube.proportions(include_transforms_for_dims=[0])
np.testing.assert_almost_equal(actual, expected)

def test_subtotals_proportions_two_transforms_missing_included(self):
cube = CrunchCube(FIXT_ECON_BLAME_WITH_HS_MISSING)
expected = np.array([
.2858576,
.3971916,
.6830491,
.2427282,
.0060181,
.0682046,
.003009,
.0772317,
0,
0,
])
actual = cube.proportions(include_missing=True,
include_transforms_for_dims=[0])
np.testing.assert_almost_equal(actual, expected)

def test_count_unweighted(self):
cube = CrunchCube(FIXT_ADMIT_X_GENDER_WEIGHTED)
expected = 4526
Expand Down Expand Up @@ -1888,48 +1748,6 @@ def test_cat_x_cat_index_by_col_prune_cols(self):
actual = cube.index(axis=0, prune=True)
np.testing.assert_almost_equal(actual, expected)

def test_hs_as_props_by_col_not_affected_by_prune(self):
cube = CrunchCube(FIXT_ECON_US_PROBLEM_X_BIGGER_PROBLEM)
expected = np.array([
[0.93244626, 0.66023166],
[0.63664278, 0.23166023],
[0.29580348, 0.42857143],
[0.04401228, 0.21428571],
[0.00307062, 0.06177606],
[0.02047083, 0.06370656],
])
actual = cube.proportions(axis=0, include_transforms_for_dims=[0, 1],
prune=True)
np.testing.assert_almost_equal(actual, expected)

def test_hs_as_props_by_row_not_affected_by_prune(self):
cube = CrunchCube(FIXT_ECON_US_PROBLEM_X_BIGGER_PROBLEM)
expected = np.array([
[0.72705507, 0.27294493],
[0.83827493, 0.16172507],
[0.56555773, 0.43444227],
[0.27922078, 0.72077922],
[0.08571429, 0.91428571],
[0.37735849, 0.62264151],
])
actual = cube.proportions(axis=1, include_transforms_for_dims=[0, 1],
prune=True)
np.testing.assert_almost_equal(actual, expected)

def test_hs_as_props_by_cell_not_affected_by_prune(self):
cube = CrunchCube(FIXT_ECON_US_PROBLEM_X_BIGGER_PROBLEM)
expected = np.array([
[0.60936455, 0.22876254],
[0.41605351, 0.08026756],
[0.19331104, 0.14849498],
[0.02876254, 0.07424749],
[0.00200669, 0.02140468],
[0.01337793, 0.02207358],
])
actual = cube.proportions(include_transforms_for_dims=[0, 1],
prune=True)
np.testing.assert_almost_equal(actual, expected)

def test_prune_univariate_cat(self):
cube = CrunchCube(FIXT_BINNED)
expected = np.array([
Expand All @@ -1940,16 +1758,6 @@ def test_prune_univariate_cat(self):
actual = cube.as_array(prune=True)
np.testing.assert_almost_equal(actual, expected)

def test_cat_x_mr_with_hs_and_prune_params(self):
'''Test that HS and prune params don't break CAT x MR.'''
cube = CrunchCube(FIXT_CAT_X_MR_SIMPLE)
# Only ensure that the call doesn't break
cube.as_array(
include_transforms_for_dims=[0, 1],
prune=True,
)
assert True

def test_single_col_margin_not_iterable(self):
cube = CrunchCube(FIXT_SINGLE_COL_MARGIN_NOT_ITERABLE)
expected = (1,)
Expand Down
18 changes: 17 additions & 1 deletion tests/integration/test_dimension.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,34 @@ def test_subtotals_indices_single_subtotal(self):
actual = dimension.hs_indices
self.assertEqual(actual, expected)

def test_inserted_hs_indices_single_subtotal(self):
dimension = CrunchCube(FIXT_ECON_BLAME_WITH_HS).dimensions[0]
# It can be verified what the inserted indices are, by comparing
# labels with/without transforms.
expected = [2]
actual = dimension.inserted_hs_indices
assert actual == expected

def test_subtotals_indices_two_subtotals(self):
dimension = CrunchCube(FIXT_ECON_BLAME_WITH_HS_MISSING).dimensions[0]
expected = [{
'anchor_ind': 1,
'inds': [0, 1]
}, {
'anchor_ind': 5,
'anchor_ind': 'bottom',
'inds': [3, 4, 5]
}]
actual = dimension.hs_indices
self.assertEqual(actual, expected)

def test_inserted_hs_indices_two_subtotals(self):
dimension = CrunchCube(FIXT_ECON_BLAME_WITH_HS_MISSING).dimensions[0]
# It can be verified what the inserted indices are, by comparing
# labels with/without transforms.
expected = [2, 6]
actual = dimension.inserted_hs_indices
self.assertEqual(actual, expected)

def test_has_transforms_false(self):
dimension = CrunchCube(
FIXT_ECON_BLAME_X_IDEOLOGY_ROW_HS
Expand Down
29 changes: 18 additions & 11 deletions tests/integration/test_headers_and_subtotals.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ def test_headings_econ_blame_two_subtotal_with_missing(self):
'Neither',
'Not sure',
'Skipped',
'Test Heading with Skipped',
'Not Asked',
'No Data',
'Test Heading with Skipped',
]
actual = cube.labels(include_missing=True, include_transforms_for_dims=[0])[0]
self.assertEqual(actual, expected)
Expand All @@ -105,8 +105,9 @@ def test_subtotals_as_array_two_transforms_missing_excluded(self):

def test_subtotals_as_array_two_transforms_missing_included(self):
cube = CrunchCube(FIXT_ECON_BLAME_WITH_HS_MISSING)
expected = np.array([285, 396, 681, 242, 6, 68, 3, 77, 0, 0])
actual = cube.as_array(include_missing=True, include_transforms_for_dims=[0])
expected = np.array([285, 396, 681, 242, 6, 68, 3, 0, 0, 77])
actual = cube.as_array(include_missing=True,
include_transforms_for_dims=[0])
np.testing.assert_array_equal(actual, expected)

def test_subtotals_proportions_one_transform(self):
Expand Down Expand Up @@ -142,16 +143,16 @@ def test_subtotals_proportions_two_transforms_missing_excluded(self):
def test_subtotals_proportions_two_transforms_missing_included(self):
cube = CrunchCube(FIXT_ECON_BLAME_WITH_HS_MISSING)
expected = np.array([
.2858576,
.3971916,
.6830491,
.2427282,
.0060181,
.0682046,
.003009,
.0772317,
.28585757,
.39719157,
.68304915,
.24272818,
.00601805,
.06820461,
.00300903,
0,
0,
.0772317,
])
actual = cube.proportions(include_missing=True,
include_transforms_for_dims=[0])
Expand Down Expand Up @@ -513,6 +514,12 @@ def test_fruit_hs_top_bottom_labels(self):
actual = cube.labels(include_transforms_for_dims=[0])
assert actual == expected

def test_fruit_hs_top_bottom_inserted_indices(self):
cube = CrunchCube(FIXT_FRUIT_HS_TOP_BOTTOM)
expected = [[0, 2, 4]]
actual = cube.inserted_hs_indices
assert actual == expected

def test_fruit_hs_top_bottom_counts(self):
cube = CrunchCube(FIXT_FRUIT_HS_TOP_BOTTOM)
expected = np.array([100, 33, 100, 67, 100])
Expand Down

0 comments on commit b9443d4

Please sign in to comment.