Skip to content

Commit

Permalink
fix some cosmetics
Browse files Browse the repository at this point in the history
  • Loading branch information
kwypchlo committed May 24, 2018
1 parent f3f1869 commit 1593776
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 43 deletions.
2 changes: 0 additions & 2 deletions src/cr/cube/crunch_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,12 +328,10 @@ def prune_indices(self, transforms=None):
if self.ndim >= 3:
# In case of a 3D cube, return list of tuples
# (of row and col pruned indices).
# return self._prune_3d_indices(self.hs_dims)
return self._prune_3d_indices(transforms)

# In case of 1 or 2 D cubes, return a list of
# row indices (or row and col indices)
# return self._prune_indices(self.hs_dims)
return self._prune_indices(transforms)

def _prune_indices(self, transforms):
Expand Down
54 changes: 13 additions & 41 deletions tests/integration/test_crunch_cube.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from unittest import TestCase
from mock import patch
import numpy as np

from cr.cube.crunch_cube import CrunchCube
Expand Down Expand Up @@ -502,7 +501,7 @@ def test_as_array_simple_cat_array_include_missing(self):
np.testing.assert_array_equal(actual, expected)

def test_as_array_cat_x_num_x_datetime(self):
'''Test 3D cube, slicing accross first (numerical) variable.'''
"""Test 3D cube, slicing accross first (numerical) variable."""
cube = CrunchCube(CAT_X_NUM_X_DATETIME)
expected = np.array([
[[1, 1],
Expand Down Expand Up @@ -667,7 +666,7 @@ def test_margin_weighted_gender_x_ideology_axis_1(self):
np.testing.assert_almost_equal(actual, expected)

def test_calculate_standard_error_axis_0(self):
'''Calculate standard error across columns.'''
"""Calculate standard error across columns."""
cube = CrunchCube(ECON_GENDER_X_IDEOLOGY_WEIGHTED)
expected = np.array([
[
Expand Down Expand Up @@ -1129,11 +1128,11 @@ def test_pets_x_fruit_proportions_by_row(self):
np.testing.assert_almost_equal(actual, expected)

def test_cat_x_cat_array_proportions_by_row(self):
'''Get the proportions for each slice of the 3D cube.
"""Get the proportions for each slice of the 3D cube.
The axis is equal to 2, since this is the dimensions across which
we have to calculate the margin.
'''
"""
cube = CrunchCube(FRUIT_X_PETS_ARRAY)
expected = ([
[[0.52, 0.48],
Expand Down Expand Up @@ -1188,7 +1187,7 @@ def test_pets_array_x_pets_cell(self):
cube = CrunchCube(PETS_ARRAY_X_PETS)
expected = np.array([
[0.24992768, 0. , 0.26901938], # noqa
[0.17298235, 0.44258027, 0.21174429],
[0.17298235, 0.44258027, 0.21174429]
])
actual = cube.proportions(axis=None)[0]
np.testing.assert_almost_equal(actual, expected)
Expand All @@ -1200,18 +1199,18 @@ def test_pets_x_pets_array_margin_by_cell(self):
np.testing.assert_almost_equal(actual, expected)

def test_pets_x_pets_array_percentages(self):
'''All directions need to return same percentages.
"""All directions need to return same percentages.
The only direction that makes sense is across categories, and this is
handled automatically by the cube.
'''
"""
cube = CrunchCube(PETS_X_PETS_ARRAY)
expected = np.array([
[0.58823529, 0.41176471], # noqa
[0. , 1. ], # noqa
[0.47058824, 0.52941176], # noqa
])
# TODO: Consult with jon and Mike. The new expectation is closer to what
# TODO Consult with jon and Mike. The new expectation is closer to what
# whaam displays, but diverges from R.
# expected = np.array([
# [0.55555556, 0.19444444],
Expand Down Expand Up @@ -1275,16 +1274,6 @@ def test_cat_x_cat_as_array_prune_cols(self):
np.testing.assert_array_equal(pruned[i], pruned_expected[i])

def test_cat_x_cat_props_by_col_prune_cols(self):
cube = CrunchCube(CAT_X_CAT_WITH_EMPTY_COLS)
expected = np.array([
[1., 0.25, np.nan, 0.25],
[0., 0., np.nan, 0.],
[0., 0.125, np.nan, 0.5],
[0., 0.25, np.nan, 0.],
[0., 0.25, np.nan, 0.25],
[0., 0.125, np.nan, 0.]
])
actual = cube.proportions(axis=0, prune=False)
expected = np.array([
[1., 0.25, 0.25],
[0., 0.125, 0.5],
Expand All @@ -1293,6 +1282,7 @@ def test_cat_x_cat_props_by_col_prune_cols(self):
[0., 0.125, 0.]
])
# Use API method parameter to prune
cube = CrunchCube(CAT_X_CAT_WITH_EMPTY_COLS)
table = cube.proportions(axis=0, prune=True)
actual = table[:, ~table.mask.all(axis=0)][~table.mask.all(axis=1), :]
np.testing.assert_array_equal(actual, expected)
Expand All @@ -1314,15 +1304,6 @@ def test_cat_x_cat_props_by_col_prune_cols(self):

def test_cat_x_cat_props_by_row_prune_cols(self):
cube = CrunchCube(CAT_X_CAT_WITH_EMPTY_COLS)
expected = np.array([
[0.4, 0.4, 0., 0.2],
[np.nan, np.nan, np.nan, np.nan],
[0., 0.33333333, 0., 0.66666667],
[0., 1., 0., 0.],
[0., 0.66666667, 0., 0.33333333],
[0., 1., 0., 0.],
])
actual = cube.proportions(axis=1, prune=False)
expected = np.array([
[0.4, 0.4, 0.2],
[0., 0.33333333, 0.66666667],
Expand Down Expand Up @@ -1352,15 +1333,6 @@ def test_cat_x_cat_props_by_row_prune_cols(self):

def test_cat_x_cat_props_by_cell_prune_cols(self):
cube = CrunchCube(CAT_X_CAT_WITH_EMPTY_COLS)
expected = np.array([
[0.14285714, 0.14285714, 0., 0.07142857],
[0., 0., 0., 0.],
[0., 0.07142857, 0., 0.14285714],
[0., 0.14285714, 0., 0.],
[0., 0.14285714, 0., 0.07142857],
[0., 0.07142857, 0., 0.],
])
actual = cube.proportions(axis=None, prune=False)
expected = np.array([
[0.14285714, 0.14285714, 0.07142857],
[0., 0.07142857, 0.14285714],
Expand Down Expand Up @@ -1492,14 +1464,14 @@ def test_mr_dim_ind_for_cat_cube(self):
self.assertEqual(actual, expected)

def test_total_unweighted_margin_when_has_means(self):
'''Tests that total margin is Unweighted N, when cube has means.'''
"""Tests that total margin is Unweighted N, when cube has means."""
cube = CrunchCube(SINGLE_CAT_MEANS)
expected = 17615
actual = cube.margin(weighted=False)
assert actual == expected

def test_row_unweighted_margin_when_has_means(self):
'''Tests that total margin is Unweighted N, when cube has means.'''
"""Tests that total margin is Unweighted N, when cube has means."""
cube = CrunchCube(SINGLE_CAT_MEANS)
expected = np.array([
806, 14, 14, 28, 780, 42, 1114, 28, 24, 746, 2, 12, 6, 2178, 2026,
Expand Down Expand Up @@ -1602,7 +1574,7 @@ def test_ca_x_single_cat_row_margins(self):
def test_ca_x_single_cat_cell_margins(self):
cube = CrunchCube(CA_X_SINGLE_CAT)
expected = np.array([25, 28, 23])
# Axis equals to (1, 2), because the total is calculated for each slice.
# axis equals to (1, 2), because the total is calculated for each slice
actual = cube.margin(axis=(1, 2))
np.testing.assert_array_equal(actual, expected)

Expand Down Expand Up @@ -1757,7 +1729,7 @@ def test_mr_props_with_hs_by_col(self):
np.testing.assert_almost_equal(actual, expected)

def test_3d_pruning_indices(self):
'''Test pruning indices for a simple XYZ cube.'''
"""Test pruning indices for a simple XYZ cube."""
cube = CrunchCube(XYZ_SIMPLE_ALLTYPES)

# Zeroth slice of the XYZ array:
Expand Down

0 comments on commit 1593776

Please sign in to comment.