Skip to content

Commit

Permalink
[#154065274]: Added means tests, fix CA cases
Browse files Browse the repository at this point in the history
  • Loading branch information
slobodan-ilic committed Aug 2, 2018
1 parent 73aedff commit dccc95c
Show file tree
Hide file tree
Showing 12 changed files with 5,086 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/cr/cube/measures/scale_means.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@ def data(self):

if self._cube.has_mr and not self._cube.is_double_mr:
# axis = 1 - self._cube.mr_dim_ind
# axis = self._cube.dim_types[-2:].index('categorical')
axis = self._cube.dim_types.index('categorical')
return np.sum(contents, axis) / np.sum(table, axis)

if self.valid_inds.all():
return np.sum(contents, self.axis) / self._cube.margin(self.axis)
else:
num = np.sum(contents[self.contents_inds], self.axis)
den = np.sum(table[self.contents_inds], self.axis)
return num / den

num = np.sum(contents[self.contents_inds], self.axis)
den = np.sum(table[self.contents_inds], self.axis)
return num / den

@lazyproperty
def axis(self):
Expand All @@ -44,11 +45,9 @@ def axis(self):
def values(self):
'''Get num values for means calculation.'''
return np.array([
dim.values for dim in self._cube.dimensions
dim.values for dim in self._cube.dimensions[-2:]
if dim.values and any(~np.isnan(dim.values))
][:1][-1])
# TODO: Refactor this indexing hack
# ][int(len(self._cube.dimensions) > 2)])
][0])

@lazyproperty
def valid_inds(self):
Expand All @@ -67,7 +66,8 @@ def _inner_prod(self, contents, values):
inflate_values = (
self._cube.ca_dim_ind == 0 and len(contents.shape) == 3 or
self._cube.mr_dim_ind == 1 or
self._cube.ca_dim_ind == 1 and len(contents.shape) < 3
self._cube.ca_dim_ind == 1 and len(contents.shape) < 3 or
self._cube.ca_dim_ind == 2 and len(contents.shape) == 3
)
if inflate_values:
values = values[:, np.newaxis]
Expand Down
Empty file.
21 changes: 21 additions & 0 deletions tests/integration/fixtures/cubes/scale_means/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import os
from functools import partial

from cr.cube.utils import load_fixture

CUBES_DIR = os.path.dirname(os.path.abspath(__file__))


def _load(cube_file):
load = partial(load_fixture, CUBES_DIR)
return load(cube_file)


CA_CAT_X_ITEMS = _load('ca-cat-x-items.json')
CA_ITEMS_X_CAT = _load('ca-items-x-cat.json')
CA_X_MR = _load('ca-x-mr.json')
CAT_X_CA_CAT_X_ITEMS = _load('cat-x-ca-cat-x-items.json')
CAT_X_CAT = _load('cat-x-cat.json')
CAT_X_MR = _load('cat-x-mr.json')
MR_X_CAT = _load('mr-x-cat.json')
UNIVARIATE_CAT = _load('univariate-cat.json')
513 changes: 513 additions & 0 deletions tests/integration/fixtures/cubes/scale_means/ca-cat-x-items.json

Large diffs are not rendered by default.

513 changes: 513 additions & 0 deletions tests/integration/fixtures/cubes/scale_means/ca-items-x-cat.json

Large diffs are not rendered by default.

1,390 changes: 1,390 additions & 0 deletions tests/integration/fixtures/cubes/scale_means/ca-x-mr.json

Large diffs are not rendered by default.

1,033 changes: 1,033 additions & 0 deletions tests/integration/fixtures/cubes/scale_means/cat-x-ca-cat-x-items.json

Large diffs are not rendered by default.

308 changes: 308 additions & 0 deletions tests/integration/fixtures/cubes/scale_means/cat-x-cat.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,308 @@
{
"element": "shoji:view",
"self": "https://app.crunch.io/api/datasets/ddc1b8a25c454689911d3d7a59c97aee/cube/?filter=%7B%22filter%22:%22https:%2F%2Fapp.crunch.io%2Fapi%2Fdatasets%2Fddc1b8a25c454689911d3d7a59c97aee%2Ffilters%2F06b4deb17519405ebe4581dd8dbe9eba%2F%22%7D&query=%7B%22dimensions%22:%5B%7B%22variable%22:%22https:%2F%2Fapp.crunch.io%2Fapi%2Fdatasets%2Fddc1b8a25c454689911d3d7a59c97aee%2Fvariables%2F000026%2F%22%7D,%7B%22variable%22:%22https:%2F%2Fapp.crunch.io%2Fapi%2Fdatasets%2Fddc1b8a25c454689911d3d7a59c97aee%2Fvariables%2F000067%2F%22%7D%5D,%22measures%22:%7B%22count%22:%7B%22function%22:%22cube_count%22,%22args%22:%5B%5D%7D%7D,%22weight%22:null%7D",
"value": {
"query": {
"measures": {
"count": {
"function": "cube_count",
"args": []
}
},
"dimensions": [
{
"variable": "https://app.crunch.io/api/datasets/ddc1b8a25c454689911d3d7a59c97aee/variables/000026/"
},
{
"variable": "https://app.crunch.io/api/datasets/ddc1b8a25c454689911d3d7a59c97aee/variables/000067/"
}
],
"weight": null
},
"query_environment": {
"filter": [
"https://app.crunch.io/api/datasets/ddc1b8a25c454689911d3d7a59c97aee/filters/06b4deb17519405ebe4581dd8dbe9eba/"
]
},
"result": {
"dimensions": [
{
"references": {
"alias": "pasta",
"notes": "A categorical variable",
"name": "Shapes of pasta",
"description": "The geometry of pasta"
},
"derived": false,
"type": {
"ordinal": false,
"class": "categorical",
"categories": [
{
"numeric_value": 1,
"missing": false,
"id": 1,
"name": "Bucatini"
},
{
"numeric_value": 2,
"missing": false,
"id": 2,
"name": "Chitarra"
},
{
"numeric_value": 0,
"missing": false,
"id": 0,
"name": "Boccoli"
},
{
"numeric_value": 4,
"missing": false,
"id": 4,
"name": "Orecchiette"
},
{
"numeric_value": 5,
"missing": false,
"id": 5,
"name": "Quadrefiore"
},
{
"numeric_value": 6,
"missing": false,
"id": 6,
"name": "Fileja"
},
{
"numeric_value": 32766,
"missing": true,
"id": 32766,
"name": "Skipped"
},
{
"numeric_value": 32767,
"missing": true,
"id": 32767,
"name": "Not asked"
},
{
"numeric_value": null,
"missing": true,
"id": -1,
"name": "No Data"
}
]
}
},
{
"references": {
"alias": "food_groups",
"notes": "A categorical variable where the missing categories are interspersed throughout the non-missing categories",
"name": "Food groups",
"description": "Four of the five USDA food groups"
},
"derived": false,
"type": {
"ordinal": false,
"class": "categorical",
"categories": [
{
"numeric_value": 0,
"missing": false,
"id": 0,
"name": "Vegetables"
},
{
"numeric_value": 32766,
"missing": true,
"id": 32766,
"name": "Don't know"
},
{
"numeric_value": 2,
"missing": false,
"id": 2,
"name": "Fruit"
},
{
"numeric_value": 5,
"missing": false,
"id": 5,
"name": "Grain"
},
{
"numeric_value": null,
"missing": true,
"id": -1,
"name": "No Data"
},
{
"numeric_value": 4,
"missing": false,
"id": 4,
"name": "Meat"
},
{
"numeric_value": 32767,
"missing": true,
"id": 32767,
"name": "Not asked"
}
]
}
}
],
"missing": 10,
"measures": {
"count": {
"data": [
31,
0,
8,
3,
0,
12,
0,
31,
0,
45,
27,
0,
21,
0,
156,
3,
217,
129,
0,
105,
0,
66,
0,
101,
59,
0,
80,
0,
92,
1,
68,
47,
0,
188,
0,
49,
2,
51,
25,
0,
23,
0,
3,
0,
1,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0
],
"n_missing": 10,
"metadata": {
"references": {},
"derived": true,
"type": {
"integer": true,
"missing_rules": {},
"missing_reasons": {
"No Data": -1
},
"class": "numeric"
}
}
}
},
"element": "crunch:cube",
"counts": [
31,
0,
8,
3,
0,
12,
0,
31,
0,
45,
27,
0,
21,
0,
156,
3,
217,
129,
0,
105,
0,
66,
0,
101,
59,
0,
80,
0,
92,
1,
68,
47,
0,
188,
0,
49,
2,
51,
25,
0,
23,
0,
3,
0,
1,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0
],
"n": 1644
}
}
}
Loading

0 comments on commit dccc95c

Please sign in to comment.