diff --git a/src/cr/cube/crunch_cube.py b/src/cr/cube/crunch_cube.py index 3af5dbd4a..5ae01114e 100644 --- a/src/cr/cube/crunch_cube.py +++ b/src/cr/cube/crunch_cube.py @@ -411,18 +411,35 @@ def _mr_proportions(self, axis, weighted): return res[np.ix_(*valid_indices)] if self.dimensions[0].type == 'multiple_response': + if len(table.shape) == 4: + # This is the case of MR x CA, special treatment + # Still haven'f figured out the percentages + if axis == 1: + res = (table[:, 0, :, :] / + np.sum(table[:, 0, :, :], 1)) + else: + res = (table[:, 0, :, :] / + (table[:, 0, :, :] + table[:, 1, :, :])) + + # The following are normal MR x something (not CA) if axis == 1: res = table[:, 0, :] / np.sum(table[:, 0, :], 1)[:, np.newaxis] else: res = table[:, 0, :] / (table[:, 0, :] + table[:, 1, :]) - return res[np.ix_(*valid_indices)] - - if self.dimensions[1].type == 'multiple_response': + elif self.dimensions[1].type == 'multiple_response': if axis == 0: res = table[:, :, 0] / np.sum(table[:, :, 0], 0) else: res = table[:, :, 0] / (table[:, :, 0] + table[:, :, 1]) - return res[np.ix_(*valid_indices)] + elif self.dimensions[2].type == 'multiple_response': + margin = ( + self.margin(axis=axis)[:, np.newaxis] + if axis == 1 else + self.margin(axis=0) + ) + return self.as_array() / margin + + return res[np.ix_(*valid_indices)] def _proportions(self, axis=None, weighted=True, adjusted=False, include_transforms_for_dims=None, include_missing=False): diff --git a/tests/integration/fixtures/__init__.py b/tests/integration/fixtures/__init__.py index 4c4407460..1261e169f 100644 --- a/tests/integration/fixtures/__init__.py +++ b/tests/integration/fixtures/__init__.py @@ -31,18 +31,18 @@ FIXT_CAT_X_MR = load_fixture(CUBES_DIR, 'cat-x-mr.json') FIXT_ECON_GENDER_X_IDEOLOGY_WEIGHTED = load_fixture( CUBES_DIR, - 'econ-gender-x-ideology-weighted.json' + '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' + CUBES_DIR, 'econ-mean-age-blame-x-gender.json', ) FIXT_ECON_MEAN_NO_DIMS = load_fixture(CUBES_DIR, 'econ-mean-no-dims.json') FIXT_MR_X_CAT_PROFILES_STATS_WEIGHTED = load_fixture( CUBES_DIR, - 'mr-x-cat-profiles-stats-weighted.json' + 'mr-x-cat-profiles-stats-weighted.json', ) FIXT_ADMIT_X_DEPT_UNWEIGHTED = load_fixture(CUBES_DIR, 'admit-x-dept-unweighted.json') @@ -55,25 +55,33 @@ FIXT_ECON_BLAME_WITH_HS = load_fixture(CUBES_DIR, 'econ-blame-with-hs.json') FIXT_ECON_BLAME_WITH_HS_MISSING = load_fixture( CUBES_DIR, - 'econ-blame-with-hs-missing.json' + 'econ-blame-with-hs-missing.json', ) FIXT_ECON_BLAME_X_IDEOLOGY_ROW_HS = load_fixture( CUBES_DIR, - 'econ-blame-x-ideology-row-hs.json' + 'econ-blame-x-ideology-row-hs.json', ) FIXT_ECON_BLAME_X_IDEOLOGY_COL_HS = load_fixture( CUBES_DIR, - 'econ-blame-x-ideology-col-hs.json' + 'econ-blame-x-ideology-col-hs.json', ) FIXT_ECON_BLAME_X_IDEOLOGY_ROW_AND_COL_HS = load_fixture( CUBES_DIR, - 'econ-blame-x-ideology-row-and-col-hs.json' + 'econ-blame-x-ideology-row-and-col-hs.json', ) FIXT_SIMPLE_CA_HS = load_fixture(CUBES_DIR, 'simple-ca-hs.json') FIXT_FRUIT_X_PETS = load_fixture(CUBES_DIR, 'fruit-x-pets.json') FIXT_ECON_US_PROBLEM_X_BIGGER_PROBLEM = load_fixture( CUBES_DIR, - 'econ-us-problem-x-bigger-problem.json' + 'econ-us-problem-x-bigger-problem.json', +) +FIXT_PETS_ARRAY_X_PETS = load_fixture( + CUBES_DIR, + 'pets-array-x-pets.json', +) +FIXT_PETS_X_PETS_ARRAY = load_fixture( + CUBES_DIR, + 'pets-x-pets-array.json', ) FIXT_FRUIT_X_PETS_ARRAY = load_fixture( CUBES_DIR, @@ -85,3 +93,12 @@ ) 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') +FIXT_SELECTED_3_WAY_2 = load_fixture( + CUBES_DIR, + 'selected-3way-2-filledmissing.json' +) +FIXT_SELECTED_3_WAY = load_fixture( + CUBES_DIR, + 'selected-3way-filledmissing.json' +) +FIXT_ARRAY_X_MR = load_fixture(CUBES_DIR, 'array-by-mr.json') diff --git a/tests/integration/fixtures/cubes/array-by-mr.json b/tests/integration/fixtures/cubes/array-by-mr.json new file mode 100644 index 000000000..467ce7c06 --- /dev/null +++ b/tests/integration/fixtures/cubes/array-by-mr.json @@ -0,0 +1,477 @@ +{ + "element": "shoji:view", + "self": "/api/datasets/123/cube/?filter=%5B%5D&query=%7B%22dimensions%22:%5B%7B%22each%22:%22/api/datasets/123%2Fvariables%2F00000f%2F%22%7D,%7B%22variable%22:%22/api/datasets/123%2Fvariables%2F00000f%2F%22%7D,%7B%22each%22:%22/api/datasets/123%2Fvariables%2F000010%2F%22%7D,%7B%22function%22:%22as_selected%22,%22args%22:%5B%7B%22variable%22:%22/api/datasets/123%2Fvariables%2F000010%2F%22%7D%5D%7D%5D,%22measures%22:%7B%22count%22:%7B%22function%22:%22cube_count%22,%22args%22:%5B%5D%7D%7D,%22weight%22:%22/api/datasets/123%2Fvariables%2F00000d%2F%22%7D", + "value": { + "query": { + "measures": { + "count": { + "function": "cube_count", + "args": [] + } + }, + "dimensions": [{ + "each": "/api/datasets/123/variables/00000f/" + }, { + "variable": "/api/datasets/123/variables/00000f/" + }, { + "each": "/api/datasets/123/variables/000010/" + }, { + "function": "as_selected", + "args": [{ + "variable": "/api/datasets/123/variables/000010/" + }] + }], + "weight": "/api/datasets/123/variables/00000d/" + }, + "query_environment": { + "filter": [] + }, + "result": { + "dimensions": [{ + "derived": true, + "references": { + "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 + }], + "view": { + "show_counts": false, + "transforms": { + "insertions": [{ + "function": "subtotal", + "args": [2], + "anchor": 2, + "name": "selected, again" + }] + }, + "include_missing": false, + "column_width": null + }, + "name": "pets_array", + "alias": "pets_array" + }, + "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": 1, + "missing": false, + "id": 1, + "name": "not selected" + }, { + "numeric_value": 2, + "missing": false, + "id": 2, + "name": "selected" + }, { + "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": 1, + "missing": false, + "id": 1, + "name": "not selected" + }, { + "numeric_value": 2, + "missing": false, + "id": 2, + "name": "selected" + }, { + "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": 1, + "missing": false, + "id": 1, + "name": "not selected" + }, { + "numeric_value": 2, + "missing": false, + "id": 2, + "name": "selected" + }, { + "numeric_value": null, + "missing": true, + "id": -1, + "name": "No Data" + }] + } + }, + "missing": false + }, { + "id": 4, + "value": { + "derived": false, + "references": { + "uniform_basis": null, + "description": null, + "name": "tarantula", + "format": null, + "subreferences": null, + "notes": null, + "api_derivation": null, + "dichotomous": false, + "discarded": null, + "alias": "tarantula", + "view": null + }, + "id": "f624a951fedf4eeb86774ea84142c043", + "type": { + "ordinal": false, + "class": "categorical", + "categories": [{ + "numeric_value": 1, + "missing": false, + "id": 1, + "name": "not selected" + }, { + "numeric_value": 2, + "missing": false, + "id": 2, + "name": "selected" + }, { + "numeric_value": null, + "missing": true, + "id": -1, + "name": "No Data" + }] + } + }, + "missing": false + }], + "class": "enum" + } + }, { + "references": { + "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" + }], + "alias": "pets_array", + "name": "pets_array", + "view": { + "show_counts": false, + "transforms": { + "insertions": [{ + "function": "subtotal", + "args": [2], + "anchor": 2, + "name": "selected, again" + }] + }, + "include_missing": false, + "column_width": null + } + }, + "derived": false, + "type": { + "ordinal": false, + "subvariables": ["0004", "0005", "0006", "f624a951fedf4eeb86774ea84142c043"], + "class": "categorical", + "categories": [{ + "numeric_value": 1, + "missing": false, + "id": 1, + "name": "not selected" + }, { + "numeric_value": 2, + "missing": false, + "id": 2, + "name": "selected" + }, { + "numeric_value": null, + "missing": true, + "id": -1, + "name": "No Data" + }] + } + }, { + "derived": true, + "references": { + "alias": "languages", + "subreferences": [{ + "alias": "Python", + "name": "Python", + "description": "Python" + }, { + "alias": "R", + "name": "R", + "description": "R" + }, { + "alias": "JavaScript", + "name": "JavaScript", + "description": "JavaScript" + }], + "is_dichotomous": false, + "description": "Languages", + "name": "languages" + }, + "type": { + "subtype": { + "class": "variable" + }, + "elements": [{ + "id": 1, + "value": { + "derived": false, + "references": { + "alias": "Python", + "name": "Python", + "description": "Python" + }, + "id": "0007", + "type": { + "ordinal": false, + "class": "categorical", + "categories": [{ + "numeric_value": 1, + "selected": true, + "id": 1, + "missing": false, + "name": "Selected" + }, { + "numeric_value": 0, + "missing": false, + "id": 0, + "name": "Not Selected" + }, { + "numeric_value": null, + "missing": true, + "id": -1, + "name": "No Data" + }] + } + }, + "missing": false + }, { + "id": 2, + "value": { + "derived": false, + "references": { + "alias": "R", + "name": "R", + "description": "R" + }, + "id": "0008", + "type": { + "ordinal": false, + "class": "categorical", + "categories": [{ + "numeric_value": 1, + "selected": true, + "id": 1, + "missing": false, + "name": "Selected" + }, { + "numeric_value": 0, + "missing": false, + "id": 0, + "name": "Not Selected" + }, { + "numeric_value": null, + "missing": true, + "id": -1, + "name": "No Data" + }] + } + }, + "missing": false + }, { + "id": 3, + "value": { + "derived": false, + "references": { + "alias": "JavaScript", + "name": "JavaScript", + "description": "JavaScript" + }, + "id": "0009", + "type": { + "ordinal": false, + "class": "categorical", + "categories": [{ + "numeric_value": 1, + "selected": true, + "id": 1, + "missing": false, + "name": "Selected" + }, { + "numeric_value": 0, + "missing": false, + "id": 0, + "name": "Not Selected" + }, { + "numeric_value": null, + "missing": true, + "id": -1, + "name": "No Data" + }] + } + }, + "missing": false + }], + "class": "enum" + } + }, { + "references": { + "alias": "languages", + "is_dichotomous": false, + "description": "Languages", + "name": "languages", + "subreferences": [{ + "alias": "Python", + "name": "Python", + "description": "Python" + }, { + "alias": "R", + "name": "R", + "description": "R" + }, { + "alias": "JavaScript", + "name": "JavaScript", + "description": "JavaScript" + }] + }, + "derived": true, + "type": { + "ordinal": false, + "subvariables": ["0007", "0008", "0009"], + "class": "categorical", + "categories": [{ + "numeric_value": 1, + "selected": true, + "id": 1, + "missing": false, + "name": "Selected" + }, { + "numeric_value": 0, + "missing": false, + "id": 0, + "name": "Not Selected" + }, { + "numeric_value": null, + "missing": true, + "id": -1, + "name": "No Data" + }] + } + }], + "missing": 11, + "measures": { + "count": { + "data": [48.30605520841679, 66.9213862845193, 0.0, 4.0, 111.22744149293605, 0.0, 67.20096057862209, 48.02648091431399, 0.0, 45.56222406264413, 43.01891958353383, 0.0, 88.58114364617798, 0.0, 0.0, 46.05868102676006, 42.522462619417894, 0.0, 37.73339333964035, 16.248421822659385, 0.0, 33.11950456297243, 20.862310599327305, 0.0, 22.085620392222374, 31.89619477007737, 0.0, 0.0, 107.34364601126751, 0.0, 44.59908661099255, 62.74455940027492, 0.0, 65.17721810696865, 42.16642790429883, 0.0, 99.28142072261471, 4.0, 0.0, 46.27629201487884, 57.005128707735885, 0.0, 47.398463619902365, 55.882957102712346, 0.0, 32.320251888086545, 14.845081679445034, 0.0, 34.825269583279, 12.340063984252577, 0.0, 22.769580270733503, 24.395753296798073, 0.0, 38.467089744085996, 37.79170703708349, 0.0, 34.91762662660323, 41.34117015456626, 0.0, 0.0, 76.25879678116948, 0.0, 40.219131492305436, 68.57840681603169, 0.0, 43.741061959020406, 65.05647634931671, 0.0, 104.79753830833712, 4.0, 0.0, 52.91545137430984, 19.81861383759734, 0.0, 47.04195962352676, 25.69210558838043, 0.0, 30.5477236892674, 42.186341522639765, 0.0, 0.0, 4.0, 0.0, 4.0, 0.0, 0.0, 0.0, 4.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 131.60167261070126, 122.18872769071254, 0.0, 121.70064820915043, 132.08975209226338, 0.0, 135.3452619976045, 118.44513830380927, 0.0], + "n_missing": 11, + "metadata": { + "references": {}, + "derived": true, + "type": { + "integer": false, + "missing_rules": {}, + "missing_reasons": { + "No Data": -1 + }, + "class": "numeric" + } + } + } + }, + "element": "crunch:cube", + "counts": [21, 26, 0, 2, 45, 0, 24, 23, 0, 18, 16, 0, 34, 0, 0, 18, 16, 0, 12, 9, 0, 13, 8, 0, 9, 12, 0, 0, 40, 0, 18, 22, 0, 21, 19, 0, 40, 2, 0, 19, 23, 0, 21, 21, 0, 11, 9, 0, 12, 8, 0, 9, 11, 0, 16, 16, 0, 13, 19, 0, 0, 32, 0, 18, 22, 0, 18, 22, 0, 38, 2, 0, 17, 13, 0, 18, 12, 0, 13, 17, 0, 0, 2, 0, 2, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 51, 49, 0, 47, 53, 0, 51, 49, 0], + "n": 102 + } + } +} diff --git a/tests/integration/fixtures/cubes/pets-array-x-pets.json b/tests/integration/fixtures/cubes/pets-array-x-pets.json new file mode 100644 index 000000000..3e84384ff --- /dev/null +++ b/tests/integration/fixtures/cubes/pets-array-x-pets.json @@ -0,0 +1,631 @@ +{ + "element": "shoji:view", + "self": "https://alpha.crunch.io/api/datasets/c4b1dc74453d43a28508d18a4aa64221/cube/?filter=%5B%5D&query=%7B%22dimensions%22:%5B%7B%22each%22:%22https:%2F%2Falpha.crunch.io%2Fapi%2Fdatasets%2Fc4b1dc74453d43a28508d18a4aa64221%2Fvariables%2F00000b%2F%22%7D,%7B%22variable%22:%22https:%2F%2Falpha.crunch.io%2Fapi%2Fdatasets%2Fc4b1dc74453d43a28508d18a4aa64221%2Fvariables%2F00000b%2F%22%7D,%7B%22each%22:%22https:%2F%2Falpha.crunch.io%2Fapi%2Fdatasets%2Fc4b1dc74453d43a28508d18a4aa64221%2Fvariables%2F00000a%2F%22%7D,%7B%22function%22:%22as_selected%22,%22args%22:%5B%7B%22variable%22:%22https:%2F%2Falpha.crunch.io%2Fapi%2Fdatasets%2Fc4b1dc74453d43a28508d18a4aa64221%2Fvariables%2F00000a%2F%22%7D%5D%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%2Fc4b1dc74453d43a28508d18a4aa64221%2Fvariables%2Fad87769e360441169923ca6e1864dcd7%2F%22%7D", + "value": { + "query": { + "measures": { + "count": { + "function": "cube_count", + "args": [] + } + }, + "dimensions": [ + { + "each": "https://alpha.crunch.io/api/datasets/c4b1dc74453d43a28508d18a4aa64221/variables/00000b/" + }, + { + "variable": "https://alpha.crunch.io/api/datasets/c4b1dc74453d43a28508d18a4aa64221/variables/00000b/" + }, + { + "each": "https://alpha.crunch.io/api/datasets/c4b1dc74453d43a28508d18a4aa64221/variables/00000a/" + }, + { + "function": "as_selected", + "args": [ + { + "variable": "https://alpha.crunch.io/api/datasets/c4b1dc74453d43a28508d18a4aa64221/variables/00000a/" + } + ] + } + ], + "weight": "https://alpha.crunch.io/api/datasets/c4b1dc74453d43a28508d18a4aa64221/variables/ad87769e360441169923ca6e1864dcd7/" + }, + "query_environment": { + "filter": [] + }, + "result": { + "dimensions": [ + { + "derived": true, + "references": { + "alias": "pets_array", + "is_dichotomous": false, + "description": "Pets Array", + "name": "pets_array", + "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" + } + ] + }, + "type": { + "subtype": { + "class": "variable" + }, + "elements": [ + { + "id": 1, + "value": { + "derived": false, + "references": { + "alias": "a_cat", + "name": "cat", + "description": "a_cat" + }, + "id": "0003", + "type": { + "ordinal": false, + "class": "categorical", + "categories": [ + { + "numeric_value": 1, + "id": 1, + "name": "not selected", + "missing": false + }, + { + "numeric_value": 2, + "id": 2, + "name": "selected", + "missing": false + }, + { + "numeric_value": null, + "id": -1, + "name": "No Data", + "missing": true + } + ] + } + }, + "missing": false + }, + { + "id": 2, + "value": { + "derived": false, + "references": { + "alias": "a_dog", + "name": "dog", + "description": "a_dog" + }, + "id": "0004", + "type": { + "ordinal": false, + "class": "categorical", + "categories": [ + { + "numeric_value": 1, + "id": 1, + "name": "not selected", + "missing": false + }, + { + "numeric_value": 2, + "id": 2, + "name": "selected", + "missing": false + }, + { + "numeric_value": null, + "id": -1, + "name": "No Data", + "missing": true + } + ] + } + }, + "missing": false + }, + { + "id": 3, + "value": { + "derived": false, + "references": { + "alias": "a_wombat", + "name": "wombat", + "description": "a_wombat" + }, + "id": "0005", + "type": { + "ordinal": false, + "class": "categorical", + "categories": [ + { + "numeric_value": 1, + "id": 1, + "name": "not selected", + "missing": false + }, + { + "numeric_value": 2, + "id": 2, + "name": "selected", + "missing": false + }, + { + "numeric_value": null, + "id": -1, + "name": "No Data", + "missing": true + } + ] + } + }, + "missing": false + } + ], + "class": "enum" + } + }, + { + "references": { + "alias": "pets_array", + "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" + } + ], + "is_dichotomous": false, + "description": "Pets Array", + "name": "pets_array" + }, + "derived": false, + "type": { + "ordinal": false, + "subvariables": [ + "0003", + "0004", + "0005" + ], + "class": "categorical", + "categories": [ + { + "numeric_value": 1, + "id": 1, + "name": "not selected", + "missing": false + }, + { + "numeric_value": 2, + "id": 2, + "name": "selected", + "missing": false + }, + { + "numeric_value": null, + "id": -1, + "name": "No Data", + "missing": true + } + ] + } + }, + { + "derived": true, + "references": { + "alias": "pets", + "subreferences": [ + { + "alias": "dog", + "name": "dog", + "description": "dog" + }, + { + "alias": "cat", + "name": "cat", + "description": "cat" + }, + { + "alias": "wombat", + "name": "wombat", + "description": "wombat" + } + ], + "is_dichotomous": true, + "description": null, + "name": "pets" + }, + "type": { + "subtype": { + "class": "variable" + }, + "elements": [ + { + "id": 1, + "value": { + "derived": false, + "references": { + "alias": "dog", + "name": "dog", + "description": "dog" + }, + "id": "0000", + "type": { + "ordinal": false, + "class": "categorical", + "categories": [ + { + "numeric_value": 1, + "selected": true, + "id": 1, + "missing": false, + "name": "Selected" + }, + { + "numeric_value": 0, + "missing": false, + "id": 0, + "name": "Not Selected" + }, + { + "numeric_value": null, + "missing": true, + "id": -1, + "name": "No Data" + } + ] + } + }, + "missing": false + }, + { + "id": 2, + "value": { + "derived": false, + "references": { + "alias": "cat", + "name": "cat", + "description": "cat" + }, + "id": "0001", + "type": { + "ordinal": false, + "class": "categorical", + "categories": [ + { + "numeric_value": 1, + "selected": true, + "id": 1, + "missing": false, + "name": "Selected" + }, + { + "numeric_value": 0, + "missing": false, + "id": 0, + "name": "Not Selected" + }, + { + "numeric_value": null, + "missing": true, + "id": -1, + "name": "No Data" + } + ] + } + }, + "missing": false + }, + { + "id": 3, + "value": { + "derived": false, + "references": { + "alias": "wombat", + "name": "wombat", + "description": "wombat" + }, + "id": "0002", + "type": { + "ordinal": false, + "class": "categorical", + "categories": [ + { + "numeric_value": 1, + "selected": true, + "id": 1, + "missing": false, + "name": "Selected" + }, + { + "numeric_value": 0, + "missing": false, + "id": 0, + "name": "Not Selected" + }, + { + "numeric_value": null, + "missing": true, + "id": -1, + "name": "No Data" + } + ] + } + }, + "missing": false + } + ], + "class": "enum" + } + }, + { + "references": { + "alias": "pets", + "is_dichotomous": true, + "description": null, + "name": "pets", + "subreferences": [ + { + "alias": "dog", + "name": "dog", + "description": "dog" + }, + { + "alias": "cat", + "name": "cat", + "description": "cat" + }, + { + "alias": "wombat", + "name": "wombat", + "description": "wombat" + } + ] + }, + "derived": true, + "type": { + "ordinal": false, + "subvariables": [ + "0000", + "0001", + "0002" + ], + "class": "categorical", + "categories": [ + { + "numeric_value": 1, + "selected": true, + "id": 1, + "missing": false, + "name": "Selected" + }, + { + "numeric_value": 0, + "missing": false, + "id": 0, + "name": "Not Selected" + }, + { + "numeric_value": null, + "missing": true, + "id": -1, + "name": "No Data" + } + ] + } + } + ], + "missing": 11, + "measures": { + "count": { + "data": [ + 19.538670284938934, + 24.038896426956118, + 0, + 0, + 43.57756671189505, + 0, + 21.031207598371772, + 22.546359113523287, + 0, + 13.523292627770239, + 21.07643600180913, + 0, + 34.59972862957937, + 0, + 0, + 16.553595658073267, + 18.0461329715061, + 0, + 6.015377657168702, + 5.291723202170963, + 10.51560379918589, + 0, + 11.307100859339666, + 10.51560379918589, + 0, + 11.307100859339666, + 10.51560379918589, + 0, + 42.152872003618256, + 0, + 15.80732700135685, + 26.34554500226141, + 0, + 19.538670284938938, + 22.614201718679325, + 0, + 39.07734056987787, + 0, + 0, + 13.523292627770239, + 25.554047942107637, + 0, + 18.046132971506104, + 21.031207598371772, + 0, + 0, + 8.254183627317955, + 10.51560379918589, + 5.269109000452284, + 2.9850746268656714, + 10.51560379918589, + 0, + 8.254183627317955, + 10.51560379918589, + 15.01582994120307, + 17.32247851650836, + 0, + 12.777023971053822, + 19.561284486657616, + 0, + 0, + 32.338308457711435, + 0, + 18.046132971506104, + 19.538670284938938, + 0, + 16.553595658073267, + 21.031207598371772, + 0, + 37.584803256445035, + 0, + 0, + 6.015377657168702, + 13.54590682948892, + 10.51560379918589, + 5.269109000452284, + 14.292175486205338, + 10.51560379918589, + 0, + 19.56128448665762, + 10.51560379918589 + ], + "n_missing": 11, + "metadata": { + "references": {}, + "derived": true, + "type": { + "integer": false, + "missing_rules": {}, + "missing_reasons": { + "No Data": -1 + }, + "class": "numeric" + } + } + } + }, + "element": "crunch:cube", + "counts": [ + 20, + 25, + 0, + 0, + 45, + 0, + 22, + 23, + 0, + 14, + 20, + 0, + 34, + 0, + 0, + 16, + 18, + 0, + 6, + 4, + 11, + 0, + 10, + 11, + 0, + 10, + 11, + 0, + 40, + 0, + 15, + 25, + 0, + 20, + 20, + 0, + 40, + 0, + 0, + 14, + 26, + 0, + 18, + 22, + 0, + 0, + 9, + 11, + 5, + 4, + 11, + 0, + 9, + 11, + 16, + 16, + 0, + 13, + 19, + 0, + 0, + 32, + 0, + 18, + 20, + 0, + 16, + 22, + 0, + 38, + 0, + 0, + 6, + 13, + 11, + 5, + 14, + 11, + 0, + 19, + 11 + ], + "n": 100 + } + } +} diff --git a/tests/integration/fixtures/cubes/pets-x-pets-array.json b/tests/integration/fixtures/cubes/pets-x-pets-array.json new file mode 100644 index 000000000..9477f762f --- /dev/null +++ b/tests/integration/fixtures/cubes/pets-x-pets-array.json @@ -0,0 +1,631 @@ +{ + "element": "shoji:view", + "self": "https://alpha.crunch.io/api/datasets/c4b1dc74453d43a28508d18a4aa64221/cube/?filter=%5B%5D&query=%7B%22dimensions%22:%5B%7B%22each%22:%22https:%2F%2Falpha.crunch.io%2Fapi%2Fdatasets%2Fc4b1dc74453d43a28508d18a4aa64221%2Fvariables%2F00000a%2F%22%7D,%7B%22function%22:%22as_selected%22,%22args%22:%5B%7B%22variable%22:%22https:%2F%2Falpha.crunch.io%2Fapi%2Fdatasets%2Fc4b1dc74453d43a28508d18a4aa64221%2Fvariables%2F00000a%2F%22%7D%5D%7D,%7B%22each%22:%22https:%2F%2Falpha.crunch.io%2Fapi%2Fdatasets%2Fc4b1dc74453d43a28508d18a4aa64221%2Fvariables%2F00000b%2F%22%7D,%7B%22variable%22:%22https:%2F%2Falpha.crunch.io%2Fapi%2Fdatasets%2Fc4b1dc74453d43a28508d18a4aa64221%2Fvariables%2F00000b%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": [ + { + "each": "https://alpha.crunch.io/api/datasets/c4b1dc74453d43a28508d18a4aa64221/variables/00000a/" + }, + { + "function": "as_selected", + "args": [ + { + "variable": "https://alpha.crunch.io/api/datasets/c4b1dc74453d43a28508d18a4aa64221/variables/00000a/" + } + ] + }, + { + "each": "https://alpha.crunch.io/api/datasets/c4b1dc74453d43a28508d18a4aa64221/variables/00000b/" + }, + { + "variable": "https://alpha.crunch.io/api/datasets/c4b1dc74453d43a28508d18a4aa64221/variables/00000b/" + } + ], + "weight": null + }, + "query_environment": { + "filter": [] + }, + "result": { + "dimensions": [ + { + "derived": true, + "references": { + "alias": "pets", + "subreferences": [ + { + "alias": "dog", + "name": "dog", + "description": "dog" + }, + { + "alias": "cat", + "name": "cat", + "description": "cat" + }, + { + "alias": "wombat", + "name": "wombat", + "description": "wombat" + } + ], + "is_dichotomous": true, + "description": null, + "name": "pets" + }, + "type": { + "subtype": { + "class": "variable" + }, + "elements": [ + { + "id": 1, + "value": { + "derived": false, + "references": { + "alias": "dog", + "name": "dog", + "description": "dog" + }, + "id": "0000", + "type": { + "ordinal": false, + "class": "categorical", + "categories": [ + { + "numeric_value": 1, + "selected": true, + "id": 1, + "missing": false, + "name": "Selected" + }, + { + "numeric_value": 0, + "missing": false, + "id": 0, + "name": "Not Selected" + }, + { + "numeric_value": null, + "missing": true, + "id": -1, + "name": "No Data" + } + ] + } + }, + "missing": false + }, + { + "id": 2, + "value": { + "derived": false, + "references": { + "alias": "cat", + "name": "cat", + "description": "cat" + }, + "id": "0001", + "type": { + "ordinal": false, + "class": "categorical", + "categories": [ + { + "numeric_value": 1, + "selected": true, + "id": 1, + "missing": false, + "name": "Selected" + }, + { + "numeric_value": 0, + "missing": false, + "id": 0, + "name": "Not Selected" + }, + { + "numeric_value": null, + "missing": true, + "id": -1, + "name": "No Data" + } + ] + } + }, + "missing": false + }, + { + "id": 3, + "value": { + "derived": false, + "references": { + "alias": "wombat", + "name": "wombat", + "description": "wombat" + }, + "id": "0002", + "type": { + "ordinal": false, + "class": "categorical", + "categories": [ + { + "numeric_value": 1, + "selected": true, + "id": 1, + "missing": false, + "name": "Selected" + }, + { + "numeric_value": 0, + "missing": false, + "id": 0, + "name": "Not Selected" + }, + { + "numeric_value": null, + "missing": true, + "id": -1, + "name": "No Data" + } + ] + } + }, + "missing": false + } + ], + "class": "enum" + } + }, + { + "references": { + "alias": "pets", + "is_dichotomous": true, + "description": null, + "name": "pets", + "subreferences": [ + { + "alias": "dog", + "name": "dog", + "description": "dog" + }, + { + "alias": "cat", + "name": "cat", + "description": "cat" + }, + { + "alias": "wombat", + "name": "wombat", + "description": "wombat" + } + ] + }, + "derived": true, + "type": { + "ordinal": false, + "subvariables": [ + "0000", + "0001", + "0002" + ], + "class": "categorical", + "categories": [ + { + "numeric_value": 1, + "selected": true, + "id": 1, + "missing": false, + "name": "Selected" + }, + { + "numeric_value": 0, + "missing": false, + "id": 0, + "name": "Not Selected" + }, + { + "numeric_value": null, + "missing": true, + "id": -1, + "name": "No Data" + } + ] + } + }, + { + "derived": true, + "references": { + "alias": "pets_array", + "is_dichotomous": false, + "description": "Pets Array", + "name": "pets_array", + "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" + } + ] + }, + "type": { + "subtype": { + "class": "variable" + }, + "elements": [ + { + "id": 1, + "value": { + "derived": false, + "references": { + "alias": "a_cat", + "name": "cat", + "description": "a_cat" + }, + "id": "0003", + "type": { + "ordinal": false, + "class": "categorical", + "categories": [ + { + "numeric_value": 1, + "id": 1, + "name": "not selected", + "missing": false + }, + { + "numeric_value": 2, + "id": 2, + "name": "selected", + "missing": false + }, + { + "numeric_value": null, + "id": -1, + "name": "No Data", + "missing": true + } + ] + } + }, + "missing": false + }, + { + "id": 2, + "value": { + "derived": false, + "references": { + "alias": "a_dog", + "name": "dog", + "description": "a_dog" + }, + "id": "0004", + "type": { + "ordinal": false, + "class": "categorical", + "categories": [ + { + "numeric_value": 1, + "id": 1, + "name": "not selected", + "missing": false + }, + { + "numeric_value": 2, + "id": 2, + "name": "selected", + "missing": false + }, + { + "numeric_value": null, + "id": -1, + "name": "No Data", + "missing": true + } + ] + } + }, + "missing": false + }, + { + "id": 3, + "value": { + "derived": false, + "references": { + "alias": "a_wombat", + "name": "wombat", + "description": "a_wombat" + }, + "id": "0005", + "type": { + "ordinal": false, + "class": "categorical", + "categories": [ + { + "numeric_value": 1, + "id": 1, + "name": "not selected", + "missing": false + }, + { + "numeric_value": 2, + "id": 2, + "name": "selected", + "missing": false + }, + { + "numeric_value": null, + "id": -1, + "name": "No Data", + "missing": true + } + ] + } + }, + "missing": false + } + ], + "class": "enum" + } + }, + { + "references": { + "alias": "pets_array", + "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" + } + ], + "is_dichotomous": false, + "description": "Pets Array", + "name": "pets_array" + }, + "derived": false, + "type": { + "ordinal": false, + "subvariables": [ + "0003", + "0004", + "0005" + ], + "class": "categorical", + "categories": [ + { + "numeric_value": 1, + "id": 1, + "name": "not selected", + "missing": false + }, + { + "numeric_value": 2, + "id": 2, + "name": "selected", + "missing": false + }, + { + "numeric_value": null, + "id": -1, + "name": "No Data", + "missing": true + } + ] + } + } + ], + "missing": 11, + "measures": { + "count": { + "data": [ + 20, + 14, + 6, + 0, + 40, + 0, + 16, + 18, + 6, + 25, + 20, + 4, + 40, + 0, + 9, + 16, + 20, + 13, + 0, + 0, + 11, + 0, + 0, + 11, + 0, + 0, + 11, + 0, + 34, + 0, + 15, + 14, + 5, + 13, + 16, + 5, + 45, + 0, + 10, + 25, + 26, + 4, + 19, + 22, + 14, + 0, + 0, + 11, + 0, + 0, + 11, + 0, + 0, + 11, + 22, + 16, + 0, + 20, + 18, + 0, + 0, + 38, + 0, + 23, + 18, + 10, + 20, + 22, + 9, + 32, + 0, + 19, + 0, + 0, + 11, + 0, + 0, + 11, + 0, + 0, + 11 + ], + "n_missing": 11, + "metadata": { + "references": {}, + "derived": true, + "type": { + "integer": true, + "missing_rules": {}, + "missing_reasons": { + "No Data": -1 + }, + "class": "numeric" + } + } + } + }, + "element": "crunch:cube", + "counts": [ + 20, + 14, + 6, + 0, + 40, + 0, + 16, + 18, + 6, + 25, + 20, + 4, + 40, + 0, + 9, + 16, + 20, + 13, + 0, + 0, + 11, + 0, + 0, + 11, + 0, + 0, + 11, + 0, + 34, + 0, + 15, + 14, + 5, + 13, + 16, + 5, + 45, + 0, + 10, + 25, + 26, + 4, + 19, + 22, + 14, + 0, + 0, + 11, + 0, + 0, + 11, + 0, + 0, + 11, + 22, + 16, + 0, + 20, + 18, + 0, + 0, + 38, + 0, + 23, + 18, + 10, + 20, + 22, + 9, + 32, + 0, + 19, + 0, + 0, + 11, + 0, + 0, + 11, + 0, + 0, + 11 + ], + "n": 100 + } + } +} diff --git a/tests/integration/fixtures/cubes/selected-3way-2-filledmissing.json b/tests/integration/fixtures/cubes/selected-3way-2-filledmissing.json new file mode 100644 index 000000000..62f79b28f --- /dev/null +++ b/tests/integration/fixtures/cubes/selected-3way-2-filledmissing.json @@ -0,0 +1,281 @@ +{ + "element": "shoji:view", + "self": "/api/datasets/123/cube/?filter=%5B%5D&query=%7B%22dimensions%22:%5B%7B%22variable%22:%22/api/datasets/123%2Fvariables%2F00000b%2Fsubvariables%2F0003%2F%22%7D,%7B%22each%22:%22/api/datasets/123%2Fvariables%2F00000a%2F%22%7D,%7B%22function%22:%22as_selected%22,%22args%22:%5B%7B%22variable%22:%22/api/datasets/123%2Fvariables%2F00000a%2F%22%7D%5D%7D,%7B%22variable%22:%22/api/datasets/123%2Fvariables%2F000009%2F%22%7D%5D,%22measures%22:%7B%22count%22:%7B%22function%22:%22cube_count%22,%22args%22:%5B%5D%7D%7D,%22weight%22:%22/api/datasets/123%2Fvariables%2Ff7e078235d7b412987ac97848a2877cf%2F%22%7D", + "value": { + "query": { + "measures": { + "count": { + "function": "cube_count", + "args": [] + } + }, + "dimensions": [{ + "variable": "/api/datasets/123/variables/00000b/subvariables/0003/" + }, { + "each": "/api/datasets/123/variables/00000a/" + }, { + "function": "as_selected", + "args": [{ + "variable": "/api/datasets/123/variables/00000a/" + }] + }, { + "variable": "/api/datasets/123/variables/000009/" + }], + "weight": "/api/datasets/123/variables/f7e078235d7b412987ac97848a2877cf/" + }, + "query_environment": { + "filter": [] + }, + "result": { + "dimensions": [{ + "derived": false, + "references": { + "alias": "a_cat", + "name": "cat", + "description": "a_cat" + }, + "type": { + "ordinal": false, + "class": "categorical", + "categories": [{ + "numeric_value": 1, + "id": 1, + "name": "not selected", + "missing": false + }, { + "numeric_value": 2, + "id": 2, + "name": "selected", + "missing": false + }, { + "numeric_value": null, + "id": -1, + "name": "No Data", + "missing": true + }] + } + }, { + "references": { + "uniform_basis": false, + "description": null, + "subreferences": [{ + "alias": "dog", + "name": "dog", + "description": "dog" + }, { + "alias": "cat", + "name": "cat", + "description": "cat" + }, { + "alias": "wombat", + "name": "wombat", + "description": "wombat" + }], + "alias": "pets", + "is_dichotomous": true, + "name": "pets" + }, + "derived": true, + "type": { + "subtype": { + "class": "variable" + }, + "elements": [{ + "id": 1, + "value": { + "references": { + "alias": "dog", + "name": "dog", + "description": "dog" + }, + "derived": false, + "id": "0000", + "type": { + "ordinal": false, + "class": "categorical", + "categories": [{ + "numeric_value": 1, + "selected": true, + "id": 1, + "missing": false, + "name": "Selected" + }, { + "numeric_value": 0, + "missing": false, + "id": 0, + "name": "Not Selected" + }, { + "numeric_value": null, + "missing": true, + "id": -1, + "name": "No Data" + }] + } + }, + "missing": false + }, { + "id": 2, + "value": { + "references": { + "alias": "cat", + "name": "cat", + "description": "cat" + }, + "derived": false, + "id": "0001", + "type": { + "ordinal": false, + "class": "categorical", + "categories": [{ + "numeric_value": 1, + "selected": true, + "id": 1, + "missing": false, + "name": "Selected" + }, { + "numeric_value": 0, + "missing": false, + "id": 0, + "name": "Not Selected" + }, { + "numeric_value": null, + "missing": true, + "id": -1, + "name": "No Data" + }] + } + }, + "missing": false + }, { + "id": 3, + "value": { + "references": { + "alias": "wombat", + "name": "wombat", + "description": "wombat" + }, + "derived": false, + "id": "0002", + "type": { + "ordinal": false, + "class": "categorical", + "categories": [{ + "numeric_value": 1, + "selected": true, + "id": 1, + "missing": false, + "name": "Selected" + }, { + "numeric_value": 0, + "missing": false, + "id": 0, + "name": "Not Selected" + }, { + "numeric_value": null, + "missing": true, + "id": -1, + "name": "No Data" + }] + } + }, + "missing": false + }], + "class": "enum" + } + }, { + "references": { + "uniform_basis": false, + "description": null, + "subreferences": [{ + "alias": "dog", + "name": "dog", + "description": "dog" + }, { + "alias": "cat", + "name": "cat", + "description": "cat" + }, { + "alias": "wombat", + "name": "wombat", + "description": "wombat" + }], + "alias": "pets", + "is_dichotomous": true, + "name": "pets" + }, + "derived": true, + "type": { + "ordinal": false, + "subvariables": ["0000", "0001", "0002"], + "class": "categorical", + "categories": [{ + "numeric_value": 1, + "selected": true, + "id": 1, + "missing": false, + "name": "Selected" + }, { + "numeric_value": 0, + "missing": false, + "id": 0, + "name": "Not Selected" + }, { + "numeric_value": null, + "missing": true, + "id": -1, + "name": "No Data" + }] + } + }, { + "references": { + "alias": "fruit", + "name": "fruit", + "description": "Fruits" + }, + "derived": false, + "type": { + "ordinal": false, + "class": "categorical", + "categories": [{ + "numeric_value": 1, + "id": 1, + "name": "rambutan", + "missing": false + }, { + "numeric_value": 2, + "id": 2, + "name": "satsuma", + "missing": false + }, { + "numeric_value": null, + "id": -1, + "name": "No Data", + "missing": true + }] + } + }], + "missing": 21, + "measures": { + "count": { + "data": [13.936159627626584, 32.96571375940658, 4.0, 9.592286697418047, 54.73328140848486, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 23.52844632504463, 87.69899516789144, 4.0, 0.0, 0.0, 0.0, 11.63056486397291, 53.42591148534379, 0.0, 11.897881461071716, 34.27308368254766, 4.0, 0.0, 0.0, 0.0, 15.187514545521811, 16.59683330383645, 0.0, 24.24241794846634, 32.55437784835336, 0.0, 0.0, 0.0, 0.0, 39.429932493988154, 49.15121115218981, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 19.43314325796529, 20.307918701055126, 0.0, 19.99678923602286, 28.843292451134683, 0.0, 0.0, 0.0, 0.0, 8.511731391434079, 12.083468094789211, 0.0, 4.374720867215336, 0.598811290936972, 0.0, 8.928325175724709, 19.484758342199434, 0.0, 0.0, 0.0, 0.0, 12.886452258649417, 12.682279385726183, 0.0, 8.928325175724709, 19.484758342199434, 0.0, 0.0, 0.0, 0.0, 12.886452258649417, 12.682279385726183, 0.0, 8.928325175724709, 19.484758342199434, 0.0], + "n_missing": 21, + "metadata": { + "references": {}, + "derived": true, + "type": { + "integer": false, + "missing_rules": {}, + "missing_reasons": { + "No Data": -1 + }, + "class": "numeric" + } + } + } + }, + "element": "crunch:cube", + "counts": [6, 14, 2, 7, 18, 0, 0, 0, 0, 0, 0, 0, 13, 32, 2, 0, 0, 0, 6, 16, 0, 7, 16, 2, 0, 0, 0, 4, 10, 0, 8, 12, 0, 0, 0, 0, 12, 22, 0, 0, 0, 0, 0, 0, 0, 6, 10, 0, 6, 12, 0, 0, 0, 0, 2, 4, 0, 3, 1, 0, 3, 8, 0, 0, 0, 0, 5, 5, 0, 3, 8, 0, 0, 0, 0, 5, 5, 0, 3, 8, 0], + "n": 100 + } + } +} diff --git a/tests/integration/fixtures/cubes/selected-3way-filledmissing.json b/tests/integration/fixtures/cubes/selected-3way-filledmissing.json new file mode 100644 index 000000000..3d539eda1 --- /dev/null +++ b/tests/integration/fixtures/cubes/selected-3way-filledmissing.json @@ -0,0 +1,268 @@ +{ + "element": "shoji:view", + "self": "/api/datasets/123/cube/?filter=%5B%5D&query=%7B%22dimensions%22:%5B%7B%22variable%22:%22/api/datasets/123%2Fvariables%2F000009%2F%22%7D,%7B%22each%22:%22/api/datasets/123%2Fvariables%2F3060052429504c5b8c71160a30585485%2F%22%7D,%7B%22function%22:%22as_selected%22,%22args%22:%5B%7B%22variable%22:%22/api/datasets/123%2Fvariables%2F3060052429504c5b8c71160a30585485%2F%22%7D%5D%7D,%7B%22variable%22:%22/api/datasets/123%2Fvariables%2F000009%2F%22%7D%5D,%22measures%22:%7B%22count%22:%7B%22function%22:%22cube_count%22,%22args%22:%5B%5D%7D%7D,%22weight%22:%22/api/datasets/123%2Fvariables%2Ff7e078235d7b412987ac97848a2877cf%2F%22%7D", + "value": { + "query": { + "measures": { + "count": { + "function": "cube_count", + "args": [] + } + }, + "dimensions": [{ + "variable": "/api/datasets/123/variables/000009/" + }, { + "each": "/api/datasets/123/variables/3060052429504c5b8c71160a30585485/" + }, { + "function": "as_selected", + "args": [{ + "variable": "/api/datasets/123/variables/3060052429504c5b8c71160a30585485/" + }] + }, { + "variable": "/api/datasets/123/variables/000009/" + }], + "weight": "/api/datasets/123/variables/f7e078235d7b412987ac97848a2877cf/" + }, + "query_environment": { + "filter": [] + }, + "result": { + "dimensions": [{ + "derived": false, + "references": { + "alias": "fruit", + "name": "fruit", + "description": "Fruits" + }, + "type": { + "ordinal": false, + "class": "categorical", + "categories": [{ + "numeric_value": 1, + "missing": false, + "id": 1, + "name": "rambutan" + }, { + "numeric_value": 2, + "missing": false, + "id": 2, + "name": "satsuma" + }, { + "numeric_value": null, + "missing": true, + "id": -1, + "name": "No Data" + }] + } + }, { + "references": { + "subreferences": [{ + "alias": "alligator", + "name": "alligator" + }, { + "alias": "oryx", + "name": "oryx" + }, { + "alias": "capybara", + "name": "capybara" + }], + "uniform_basis": false, + "name": "zoo", + "alias": "zoo" + }, + "derived": true, + "type": { + "subtype": { + "class": "variable" + }, + "elements": [{ + "id": 1, + "value": { + "references": { + "alias": "alligator", + "name": "alligator" + }, + "derived": false, + "id": "da1129bc216d48e9b8ee5229c1b26c79", + "type": { + "ordinal": false, + "class": "categorical", + "categories": [{ + "numeric_value": 1, + "selected": true, + "id": 1, + "missing": false, + "name": "Selected" + }, { + "numeric_value": 0, + "missing": false, + "id": 0, + "name": "Not Selected" + }, { + "numeric_value": null, + "missing": true, + "id": -1, + "name": "No Data" + }] + } + }, + "missing": false + }, { + "id": 2, + "value": { + "references": { + "alias": "oryx", + "name": "oryx" + }, + "derived": false, + "id": "8e6c724d95994da1a483b96f39fcd661", + "type": { + "ordinal": false, + "class": "categorical", + "categories": [{ + "numeric_value": 1, + "selected": true, + "id": 1, + "missing": false, + "name": "Selected" + }, { + "numeric_value": 0, + "missing": false, + "id": 0, + "name": "Not Selected" + }, { + "numeric_value": null, + "missing": true, + "id": -1, + "name": "No Data" + }] + } + }, + "missing": false + }, { + "id": 3, + "value": { + "references": { + "alias": "capybara", + "name": "capybara" + }, + "derived": false, + "id": "8190dd271bea45f8b2a4204780ce7168", + "type": { + "ordinal": false, + "class": "categorical", + "categories": [{ + "numeric_value": 1, + "selected": true, + "id": 1, + "missing": false, + "name": "Selected" + }, { + "numeric_value": 0, + "missing": false, + "id": 0, + "name": "Not Selected" + }, { + "numeric_value": null, + "missing": true, + "id": -1, + "name": "No Data" + }] + } + }, + "missing": false + }], + "class": "enum" + } + }, { + "references": { + "subreferences": [{ + "alias": "alligator", + "name": "alligator" + }, { + "alias": "oryx", + "name": "oryx" + }, { + "alias": "capybara", + "name": "capybara" + }], + "uniform_basis": false, + "name": "zoo", + "alias": "zoo" + }, + "derived": true, + "type": { + "ordinal": false, + "subvariables": ["da1129bc216d48e9b8ee5229c1b26c79", "8e6c724d95994da1a483b96f39fcd661", "8190dd271bea45f8b2a4204780ce7168"], + "class": "categorical", + "categories": [{ + "numeric_value": 1, + "selected": true, + "id": 1, + "missing": false, + "name": "Selected" + }, { + "numeric_value": 0, + "missing": false, + "id": 0, + "name": "Not Selected" + }, { + "numeric_value": null, + "missing": true, + "id": -1, + "name": "No Data" + }] + } + }, { + "derived": false, + "references": { + "alias": "fruit", + "name": "fruit", + "description": "Fruits" + }, + "type": { + "ordinal": false, + "class": "categorical", + "categories": [{ + "numeric_value": 1, + "missing": false, + "id": 1, + "name": "rambutan" + }, { + "numeric_value": 2, + "missing": false, + "id": 2, + "name": "satsuma" + }, { + "numeric_value": null, + "missing": true, + "id": -1, + "name": "No Data" + }] + } + }], + "missing": 4, + "measures": { + "count": { + "data": [8.460150434597217, 0.0, 0.0, 76.31300581880969, 0.0, 0.0, 0.0, 0.0, 0.0, 17.23265649385687, 0.0, 0.0, 67.54049975955003, 0.0, 0.0, 0.0, 0.0, 0.0, 26.393419799421434, 0.0, 0.0, 58.37973645398548, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 14.507119980255588, 0.0, 0.0, 137.25854296809703, 0.0, 0.0, 17.25158109965425, 0.0, 0.0, 28.54626005104948, 0.0, 0.0, 123.21940289730318, 0.0, 0.0, 17.25158109965425, 0.0, 0.0, 60.5794089920108, 0.0, 0.0, 91.1862539563418, 0.0, 0.0, 17.25158109965425, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 4.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 4.0, 0.0, 0.0, 0.0, 0.0, 0.0, 4.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], + "n_missing": 4, + "metadata": { + "references": {}, + "derived": true, + "type": { + "integer": false, + "missing_rules": {}, + "missing_reasons": { + "No Data": -1 + }, + "class": "numeric" + } + } + } + }, + "element": "crunch:cube", + "counts": [4, 0, 0, 29, 0, 0, 0, 0, 0, 11, 0, 0, 22, 0, 0, 0, 0, 0, 8, 0, 0, 25, 0, 0, 0, 0, 0, 0, 8, 0, 0, 55, 0, 0, 4, 0, 0, 18, 0, 0, 45, 0, 0, 4, 0, 0, 26, 0, 0, 37, 0, 0, 4, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0], + "n": 100 + } + } +} diff --git a/tests/integration/test_crunch_cube.py b/tests/integration/test_crunch_cube.py index e6315e9b3..96f2fc6f9 100644 --- a/tests/integration/test_crunch_cube.py +++ b/tests/integration/test_crunch_cube.py @@ -37,6 +37,11 @@ FIXT_IDENTITY_X_PERIOD, FIXT_CA_SINGLE_CAT, FIXT_MR_X_SINGLE_WAVE, + FIXT_PETS_ARRAY_X_PETS, + FIXT_PETS_X_PETS_ARRAY, + FIXT_SELECTED_3_WAY_2, + FIXT_SELECTED_3_WAY, + FIXT_ARRAY_X_MR, ) from cr.cube.crunch_cube import CrunchCube @@ -245,7 +250,6 @@ def test_proportions_text(self): actual = cube.proportions() np.testing.assert_almost_equal(actual, expected) - def test_proportions_cat_x_cat_axis_none(self): cube = CrunchCube(FIXT_CAT_X_CAT) expected = np.array([ @@ -1957,3 +1961,142 @@ def test_mr_x_single_wave(self): ]) actual = cube.margin(axis=1) np.testing.assert_almost_equal(actual, expected) + + def test_pets_array_x_pets_by_col(self): + cube = CrunchCube(FIXT_PETS_ARRAY_X_PETS) + expected = np.array([ + [0.59097127, 0., 0.55956679], + [0.40902873, 1., 0.44043321], + ]) + # Since cube is 3D, col dim is 1 (instead of 0) + actual = cube.proportions(axis=1)[0] + np.testing.assert_almost_equal(actual, expected) + + def test_pets_array_x_pets_row(self): + cube = CrunchCube(FIXT_PETS_ARRAY_X_PETS) + expected = np.array([ + [0.44836533, 0., 0.48261546], + [0.39084967, 1., 0.47843137], + ]) + # Since cube is 3D, row dim is 2 (instead of 0) + actual = cube.proportions(axis=2)[0] + np.testing.assert_almost_equal(actual, expected) + + def test_pets_array_x_pets_cell(self): + cube = CrunchCube(FIXT_PETS_ARRAY_X_PETS) + expected = np.array([ + [0.44836533, 0., 0.48261546], + [0.39084967, 1., 0.47843137], + ]) + actual = cube.proportions(axis=None)[0] + np.testing.assert_almost_equal(actual, expected) + + def test_pets_x_pets_array_by_col(self): + cube = CrunchCube(FIXT_PETS_X_PETS_ARRAY) + expected = np.array([ + [0.55555556, 0.19444444], + [0., 0.55555556], + [0.44444444, 0.25], + ]) + actual = cube.proportions(axis=1)[0] + # Since cube is 3D, col dim is 1 (instead of 0) + np.testing.assert_almost_equal(actual, expected) + + def test_pets_x_pets_array_by_row(self): + cube = CrunchCube(FIXT_PETS_X_PETS_ARRAY) + expected = np.array([ + [0.44444444, 0.41176471], + [0., 1.], + [0.5, 0.47368421], + ]) + actual = cube.proportions(axis=2)[0] + # Since cube is 3D, col dim is 1 (instead of 0) + np.testing.assert_almost_equal(actual, expected) + + def test_pets_x_pets_array_by_cell(self): + cube = CrunchCube(FIXT_PETS_X_PETS_ARRAY) + expected = np.array([ + [0.44444444, 0.41176471], + [0., 1.], + [0.5, 0.47368421], + ]) + actual = cube.proportions(axis=None)[0] + # Since cube is 3D, col dim is 1 (instead of 0) + np.testing.assert_almost_equal(actual, expected) + + def test_mr_x_cat_x_cat_by_row(self): + cube = CrunchCube(FIXT_SELECTED_3_WAY_2) + # Only compare 0 slice (parity with whaam tests) + expected = np.array([ + [0.5923110874002918, 0.3758961399306439], + [0, 0], + [0.49431928922535223, 0.6091963925363675] + ]) + actual = cube.proportions(axis=2)[0] + # Since cube is 3D, row dim is 2 (instead of 1) + np.testing.assert_almost_equal(actual, expected) + + def test_cat_x_mr_x_cat_by_col(self): + cube = CrunchCube(FIXT_SELECTED_3_WAY) + # Only take first slice (parity with whaam tests). + expected = np.array([[1, 0], [1, 0], [1, 0]]) + # Since MR is 2nd dim, the cube is considered 2 dimensional, + # and the axis 0 represents column direction. + actual = cube.proportions(axis=0)[0] + np.testing.assert_almost_equal(actual, expected) + + def test_cat_x_mr_x_cat_by_row(self): + cube = CrunchCube(FIXT_SELECTED_3_WAY) + # Only take first slice (parity with whaam tests). + expected = np.array([ + [0.0997975162008577, np.nan], + [0.20327963774693497, np.nan], + [0.3113417143573762, np.nan], + ]) + # Since MR is 2nd dim, the cube is considered 2 dimensional, + # and the axis 1 represents row direction. + actual = cube.proportions(axis=1)[0] + np.testing.assert_almost_equal(actual, expected) + + def test_cat_x_mr_x_cat_by_cell(self): + cube = CrunchCube(FIXT_SELECTED_3_WAY) + # Only take first slice (parity with whaam tests). + expected = np.array([ + [0.0997975162008577, np.nan], + [0.20327963774693497, np.nan], + [0.3113417143573762, np.nan], + ]) + # Since MR is 2nd dim, the cube is considered 2 dimensional, + # and the axis None represents cell direction. + actual = cube.proportions(axis=None)[0] + np.testing.assert_almost_equal(actual, expected) + + def test_array_x_mr_by_col(self): + cube = CrunchCube(FIXT_ARRAY_X_MR) + expected = np.array([ + [0.5146153267487166, 0.04320534228100489, 0.5933354514113938], + [0.4853846732512835, 0.9567946577189951, 0.4066645485886063], + ]) + # Only compare the first slice (parity with whaam tests) + actual = cube.proportions(axis=1)[0] + np.testing.assert_almost_equal(actual, expected) + + def test_array_x_mr_by_row(self): + cube = CrunchCube(FIXT_ARRAY_X_MR) + expected = np.array([ + [0.41922353375674093, 0.03471395310157275, 0.5832027484767315], + [0.5143557893611596, 1, 0.5199603338915276], + ]) + # Only compare the first slice (parity with whaam tests) + actual = cube.proportions(axis=2)[0] + np.testing.assert_almost_equal(actual, expected) + + def test_array_x_mr_by_cell(self): + cube = CrunchCube(FIXT_ARRAY_X_MR) + expected = np.array([ + [0.41922353375674093, 0.03471395310157275, 0.5832027484767315], + [0.5143557893611596, 1, 0.5199603338915276], + ]) + # Only compare the first slice (parity with whaam tests) + actual = cube.proportions(axis=None)[0] + np.testing.assert_almost_equal(actual, expected)