Skip to content

Commit

Permalink
Refactor fixtures loading and implement table means
Browse files Browse the repository at this point in the history
  • Loading branch information
slobodan-ilic committed Nov 12, 2017
1 parent 99e8dc3 commit faf9f00
Show file tree
Hide file tree
Showing 6 changed files with 617 additions and 154 deletions.
22 changes: 14 additions & 8 deletions cr/cube/crunch_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,18 @@ def _fix_shape(cls, array):
new_shape = [dim for dim in array.shape if dim != 1]
return array.reshape(new_shape)

def _non_weighted_counts(self):
pass
@property
def _has_means(self):
return self._cube['result']['measures'].get('mean', False)

def _get_values(self, weighted):
if not weighted:
return self._cube['result']['counts']

if self._has_means:
return self._cube['result']['measures']['mean']['data']

return self._cube['result']['measures']['count']['data']

def _as_array(self, include_missing=False, get_non_selected=False,
weighted=True, adjusted=False):
Expand All @@ -167,19 +177,15 @@ def _as_array(self, include_missing=False, get_non_selected=False,
Returns
res (ndarray): Tabular representation of crunch cube
'''
counts = (
self._cube['result']['measures']['count']['data']
if weighted
else self._cube['result']['counts']
)
values = self._get_values(weighted)
all_dimensions = self._get_dimensions(self._cube)
shape = [len(dim.elements) for dim in all_dimensions]
valid_indices = self._get_valid_indices(
all_dimensions,
include_missing,
get_non_selected
)
res = np.array(counts).reshape(shape)[np.ix_(*valid_indices)]
res = np.array(values).reshape(shape)[np.ix_(*valid_indices)]

adjustment = 1 if adjusted else 0
return self._fix_shape(res) + adjustment
Expand Down
10 changes: 10 additions & 0 deletions cr/cube/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'''Utility functions for crunch cube, as well as other modules.'''
import os
import json


def load_fixture(fixtures_directory, filename):
'''Loads fixtures for CrunchCube integration tests.'''
with open(os.path.join(fixtures_directory, filename)) as ctx_file:
fixture = json.load(ctx_file)
return fixture
46 changes: 24 additions & 22 deletions tests/integration/fixtures/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,37 @@
the fixtures is better visibility of the query data, and also keeping the
source files relatively clean.
'''
import os
import json

import os
from cr.cube.utils import load_fixture

def load_fixture(filename):
'''Loads fixtures for CrunchCube integration tests.'''
cubes_directory = os.path.join(os.path.dirname(os.path.abspath(__file__)),
'cubes')
with open(os.path.join(cubes_directory, filename)) as ctx_file:
fixture = json.load(ctx_file)
return fixture
CUBES_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'cubes')


# Bivariate Cubes
FIXT_CAT_X_CAT = load_fixture('cat-x-cat.json')
FIXT_CAT_X_DATETIME = load_fixture('cat-x-datetime.json')
FIXT_CAT_X_CAT = load_fixture(CUBES_DIR, 'cat-x-cat.json')
FIXT_CAT_X_DATETIME = load_fixture(CUBES_DIR, 'cat-x-datetime.json')

# Univariate Cubes
FIXT_UNIVARIATE_CATEGORICAL = load_fixture('univariate-categorical.json')
FIXT_VOTER_REGISTRATION = load_fixture('voter-registration.json')
FIXT_SIMPLE_DATETIME = load_fixture('simple-datetime.json')
FIXT_SIMPLE_TEXT = load_fixture('simple-text.json')
FIXT_SIMPLE_CAT_ARRAY = load_fixture('simple-cat-array.json')
FIXT_UNIVARIATE_CATEGORICAL = load_fixture(CUBES_DIR,
'univariate-categorical.json')
FIXT_VOTER_REGISTRATION = load_fixture(CUBES_DIR, 'voter-registration.json')
FIXT_SIMPLE_DATETIME = load_fixture(CUBES_DIR, 'simple-datetime.json')
FIXT_SIMPLE_TEXT = load_fixture(CUBES_DIR, 'simple-text.json')
FIXT_SIMPLE_CAT_ARRAY = load_fixture(CUBES_DIR, 'simple-cat-array.json')

# Various other Cubes
FIXT_CAT_X_NUM_X_DATETIME = load_fixture('cat-x-num-x-datetime.json')
FIXT_SIMPLE_MR = load_fixture('simple-mr.json')
FIXT_CAT_X_MR = load_fixture('cat-x-mr.json')
FIXT_CAT_X_NUM_X_DATETIME = load_fixture(CUBES_DIR,
'cat-x-num-x-datetime.json')
FIXT_SIMPLE_MR = load_fixture(CUBES_DIR, 'simple-mr.json')
FIXT_CAT_X_MR = load_fixture(CUBES_DIR, 'cat-x-mr.json')
FIXT_ECON_GENDER_X_IDEOLOGY_WEIGHTED = load_fixture(
'econ-gender-x-ideology-weighted.json')
FIXT_CAT_X_CAT_GERMAN_WEIGHTED = load_fixture('cat-x-cat-german-weighted.json')
FIXT_STATS_TEST = load_fixture('stats_test.json')
CUBES_DIR,
'econ-gender-x-ideology-weighted.json'
)
FIXT_CAT_X_CAT_GERMAN_WEIGHTED = load_fixture(CUBES_DIR,
'cat-x-cat-german-weighted.json')
FIXT_STATS_TEST = load_fixture(CUBES_DIR, 'stats_test.json')
FIXT_ECON_MEAN_AGE_BLAME_X_GENDER = load_fixture(
CUBES_DIR, 'econ-mean-age-blame-x-gender.json'
)
Loading

0 comments on commit faf9f00

Please sign in to comment.