Skip to content

Commit

Permalink
Merge branch 'axis-out-of-bounds-154195802' into rel-4.1.29
Browse files Browse the repository at this point in the history
  • Loading branch information
Crunch.io Jenkins Account committed Jan 11, 2018
2 parents 39411a4 + 8a00c17 commit 6af19e2
Show file tree
Hide file tree
Showing 6 changed files with 2,024 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/cr/cube/crunch_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,11 @@ def _mr_margin(self, axis, weighted, adjusted):
return np.sum(margin, 0)

if axis == 1:
if len(array.shape) == 1:
# In case of a flattened array (which happens with MR x CAT
# (single element)), restore the flattened dimension.
array = array[:, np.newaxis]

# If MR margin is calculated by rows, we only need the counts
# and that's why we use array and not margin.
return np.sum(array, axis)
Expand All @@ -382,6 +387,14 @@ def _margin(self, axis=None, weighted=True, adjusted=False,
adjusted=adjusted,
include_transforms_for_dims=transform_dims,
)

if axis and axis > 0 and len(array.shape) == 1:
# If any of the dimensions has only one element, it's flattened
# from the resulting array (as a part of the MR pre-processing).
# This can lead to a potential inconsistency between dimensions
# and axes, and we need to restore one dimension in this case.
array = array[:, np.newaxis]

return np.sum(array, axis)

def _mr_proportions(self, axis, weighted):
Expand Down
6 changes: 6 additions & 0 deletions tests/integration/fixtures/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,9 @@
CUBES_DIR,
'fruit-x-pets-array.json'
)
FIXT_IDENTITY_X_PERIOD = load_fixture(
CUBES_DIR,
'econ-identity-x-period.json'
)
FIXT_CA_SINGLE_CAT = load_fixture(CUBES_DIR, 'cat-arr-with-single-cat.json')
FIXT_MR_X_SINGLE_WAVE = load_fixture(CUBES_DIR, 'mr-x-single-wave.json')
334 changes: 334 additions & 0 deletions tests/integration/fixtures/cubes/cat-arr-with-single-cat.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,334 @@
{
"element": "shoji:view",
"self": "https://alpha.crunch.io/api/datasets/f4b3fc3bf4224f0db900a5a6315d3ee3/cube/?filter=%5B%5D&query=%7B%22dimensions%22:%5B%7B%22each%22:%22https:%2F%2Falpha.crunch.io%2Fapi%2Fdatasets%2Ff4b3fc3bf4224f0db900a5a6315d3ee3%2Fvariables%2Fab6abf50fe004ea6a660cf39094570cb%2F%22%7D,%7B%22variable%22:%22https:%2F%2Falpha.crunch.io%2Fapi%2Fdatasets%2Ff4b3fc3bf4224f0db900a5a6315d3ee3%2Fvariables%2Fab6abf50fe004ea6a660cf39094570cb%2F%22%7D%5D,%22measures%22:%7B%22count%22:%7B%22function%22:%22cube_count%22,%22args%22:%5B%5D%7D%7D,%22weight%22:%22https:%2F%2Falpha.crunch.io%2Fapi%2Fdatasets%2Ff4b3fc3bf4224f0db900a5a6315d3ee3%2Fvariables%2F00000d%2F%22%7D",
"value": {
"query": {
"measures": {
"count": {
"function": "cube_count",
"args": []
}
},
"dimensions": [
{
"each": "https://alpha.crunch.io/api/datasets/f4b3fc3bf4224f0db900a5a6315d3ee3/variables/ab6abf50fe004ea6a660cf39094570cb/"
},
{
"variable": "https://alpha.crunch.io/api/datasets/f4b3fc3bf4224f0db900a5a6315d3ee3/variables/ab6abf50fe004ea6a660cf39094570cb/"
}
],
"weight": "https://alpha.crunch.io/api/datasets/f4b3fc3bf4224f0db900a5a6315d3ee3/variables/00000d/"
},
"query_environment": {
"filter": []
},
"result": {
"dimensions": [
{
"derived": true,
"references": {
"description": "",
"format": {
"summary": {
"digits": 0
}
},
"subreferences": [
{
"alias": "a_cat#",
"name": "cat",
"description": "a_cat"
},
{
"alias": "a_dog#",
"name": "dog",
"description": "a_dog"
},
{
"alias": "a_wombat#",
"name": "wombat",
"description": "a_wombat"
},
{
"uniform_basis": null,
"description": null,
"format": null,
"subreferences": null,
"notes": null,
"api_derivation": null,
"dichotomous": false,
"discarded": null,
"alias": "tarantula#",
"view": null,
"name": "tarantula"
}
],
"notes": "",
"discarded": false,
"alias": "pets_array_net",
"view": {
"show_counts": false,
"include_missing": false,
"column_width": null
},
"name": "pets_array (1 category)"
},
"type": {
"subtype": {
"class": "variable"
},
"elements": [
{
"id": 1,
"value": {
"derived": false,
"references": {
"alias": "a_cat#",
"name": "cat",
"description": "a_cat"
},
"id": "0004",
"type": {
"ordinal": false,
"class": "categorical",
"categories": [
{
"numeric_value": null,
"missing": false,
"id": 1,
"name": "exists"
},
{
"numeric_value": null,
"missing": true,
"id": -1,
"name": "No Data"
}
]
}
},
"missing": false
},
{
"id": 2,
"value": {
"derived": false,
"references": {
"alias": "a_dog#",
"name": "dog",
"description": "a_dog"
},
"id": "0005",
"type": {
"ordinal": false,
"class": "categorical",
"categories": [
{
"numeric_value": null,
"missing": false,
"id": 1,
"name": "exists"
},
{
"numeric_value": null,
"missing": true,
"id": -1,
"name": "No Data"
}
]
}
},
"missing": false
},
{
"id": 3,
"value": {
"derived": false,
"references": {
"alias": "a_wombat#",
"name": "wombat",
"description": "a_wombat"
},
"id": "0006",
"type": {
"ordinal": false,
"class": "categorical",
"categories": [
{
"numeric_value": null,
"missing": false,
"id": 1,
"name": "exists"
},
{
"numeric_value": null,
"missing": true,
"id": -1,
"name": "No Data"
}
]
}
},
"missing": false
},
{
"id": 4,
"value": {
"derived": false,
"references": {
"uniform_basis": null,
"description": null,
"format": null,
"subreferences": null,
"notes": null,
"api_derivation": null,
"dichotomous": false,
"discarded": null,
"alias": "tarantula#",
"view": null,
"name": "tarantula"
},
"id": "f624a951fedf4eeb86774ea84142c043",
"type": {
"ordinal": false,
"class": "categorical",
"categories": [
{
"numeric_value": null,
"missing": false,
"id": 1,
"name": "exists"
},
{
"numeric_value": null,
"missing": true,
"id": -1,
"name": "No Data"
}
]
}
},
"missing": false
}
],
"class": "enum"
}
},
{
"references": {
"description": "",
"format": {
"summary": {
"digits": 0
}
},
"subreferences": [
{
"alias": "a_cat#",
"name": "cat",
"description": "a_cat"
},
{
"alias": "a_dog#",
"name": "dog",
"description": "a_dog"
},
{
"alias": "a_wombat#",
"name": "wombat",
"description": "a_wombat"
},
{
"uniform_basis": null,
"description": null,
"name": "tarantula",
"format": null,
"subreferences": null,
"notes": null,
"api_derivation": null,
"dichotomous": false,
"discarded": null,
"alias": "tarantula#",
"view": null
}
],
"notes": "",
"name": "pets_array (1 category)",
"discarded": false,
"alias": "pets_array_net",
"view": {
"show_counts": false,
"include_missing": false,
"column_width": null
}
},
"derived": true,
"type": {
"ordinal": false,
"subvariables": [
"0004",
"0005",
"0006",
"f624a951fedf4eeb86774ea84142c043"
],
"class": "categorical",
"categories": [
{
"numeric_value": null,
"missing": false,
"id": 1,
"name": "exists"
},
{
"numeric_value": null,
"missing": true,
"id": -1,
"name": "No Data"
}
]
}
}
],
"missing": 13,
"measures": {
"count": {
"data": [
199.80858513911403,
57.98181516229974,
206.6250667338822,
51.165333567531576,
181.05633508950658,
76.73406521190718,
0,
257.79040030141374
],
"n_missing": 13,
"metadata": {
"references": {},
"derived": true,
"type": {
"integer": false,
"missing_rules": {},
"missing_reasons": {
"No Data": -1
},
"class": "numeric"
}
}
}
},
"element": "crunch:cube",
"counts": [
79,
23,
80,
22,
70,
32,
0,
102
],
"n": 102
}
}
}
Loading

0 comments on commit 6af19e2

Please sign in to comment.