Skip to content

Commit

Permalink
Merge branch 'scale-mean-marginal-160719473' into rel-5.2.366
Browse files Browse the repository at this point in the history
  • Loading branch information
Crunch.io Jenkins Account committed Sep 26, 2018
2 parents f4deaa0 + a2d01b9 commit e7946e1
Show file tree
Hide file tree
Showing 18 changed files with 1,782 additions and 1,025 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,6 @@ The detailed description can be found
- Add property `is_double_mr` to `CubeSlice` (which is needed since it differs from the interpretation of the cube. E.g. MR x CA x MR will render slices which are *not* double MRs).
- Add `shape`, `ndim`, and `scale_means` to `CubeSlice`, for accessibility.
- `index` now also operates on slices (no api change).

#### 1.6.8 Scale Means Marginal
- Add capability to calculate the scale means marginal. This is used when analysing a 2D cube, and obtaining a sort of a "scale mean _total_" for each of the variables constituting a cube.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from setuptools import setup, find_packages

version = '1.6.7'
version = '1.6.8'


def get_long_desc():
Expand Down
5 changes: 5 additions & 0 deletions src/cr/cube/cube_slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from functools import partial
import numpy as np

from cr.cube.measures.scale_means import ScaleMeans

from .utils import lazyproperty


Expand Down Expand Up @@ -259,3 +261,6 @@ def ndim(self):
@lazyproperty
def shape(self):
return self.as_array().shape

def scale_means_margin(self, axis):
return ScaleMeans(self).margin(axis)
18 changes: 18 additions & 0 deletions src/cr/cube/measures/scale_means.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@ def data(self):
means.append(mean)
return means

def margin(self, axis):
if self._slice.ndim < 2:
msg = (
'Scale Means marginal cannot be calculated on 1D cubes, as'
'the scale means already get reduced to a scalar value.'
)
raise ValueError(msg)

dimension_index = 1 - axis
margin = self._slice.margin(axis)
total = np.sum(margin)
values = self.values[dimension_index]

if values is None:
return None

return np.sum(values * margin) / total

@lazyproperty
def values(self):
'''Get num values for means calculation.'''
Expand Down
1 change: 1 addition & 0 deletions tests/integration/fixtures/cubes/scale_means/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ def _load(cube_file):
CAT_X_MR = _load('cat-x-mr.json')
MR_X_CAT = _load('mr-x-cat.json')
UNIVARIATE_CAT = _load('univariate-cat.json')
CAT_X_CAT_SM_MARGIN = _load('cat-x-cat-sm-margin.json')
328 changes: 328 additions & 0 deletions tests/integration/fixtures/cubes/scale_means/cat-x-cat-sm-margin.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,328 @@
{
"query": {
"dimensions": [
{
"variable": "000026"
},
{
"variable": "000067"
}
],
"measures": {
"count": {
"args": [],
"function": "cube_count"
}
},
"weight": null
},
"query_environment": {
"filter": []
},
"result": {
"counts": [
32,
0,
8,
3,
0,
12,
0,
33,
0,
45,
27,
0,
21,
0,
158,
3,
218,
132,
0,
105,
0,
66,
0,
102,
60,
0,
82,
0,
93,
1,
70,
47,
0,
190,
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
],
"dimensions": [
{
"derived": false,
"references": {
"alias": "pasta",
"description": "The geometry of pasta",
"name": "Shapes of pasta",
"notes": "A categorical variable",
"view": {
"column_width": null,
"include_missing": false,
"show_counts": false,
"show_numeric_values": false,
"transform": {
"insertions": []
}
}
},
"type": {
"categories": [
{
"id": 1,
"missing": false,
"name": "Bucatini",
"numeric_value": 1
},
{
"id": 2,
"missing": false,
"name": "Chitarra",
"numeric_value": 2
},
{
"id": 0,
"missing": false,
"name": "Boccoli",
"numeric_value": 0
},
{
"id": 4,
"missing": false,
"name": "Orecchiette",
"numeric_value": 4
},
{
"id": 5,
"missing": false,
"name": "Quadrefiore",
"numeric_value": 5
},
{
"id": 6,
"missing": false,
"name": "Fileja",
"numeric_value": 6
},
{
"id": 32766,
"missing": true,
"name": "Skipped",
"numeric_value": 32766
},
{
"id": 32767,
"missing": true,
"name": "Not asked",
"numeric_value": 32767
},
{
"id": -1,
"missing": true,
"name": "No Data",
"numeric_value": null
}
],
"class": "categorical",
"ordinal": false
}
},
{
"derived": false,
"references": {
"alias": "food_groups",
"description": "Four of the five USDA food groups",
"name": "Food groups",
"notes": "A categorical variable where the missing categories are interspersed throughout the non-missing categories",
"view": {
"column_width": null,
"include_missing": false,
"show_counts": false,
"show_numeric_values": false,
"transform": {
"insertions": []
}
}
},
"type": {
"categories": [
{
"id": 0,
"missing": false,
"name": "Vegetables",
"numeric_value": 0
},
{
"id": 32766,
"missing": true,
"name": "Don't know",
"numeric_value": 32766
},
{
"id": 2,
"missing": false,
"name": "Fruit",
"numeric_value": 2
},
{
"id": 5,
"missing": false,
"name": "Grain",
"numeric_value": 5
},
{
"id": -1,
"missing": true,
"name": "No Data",
"numeric_value": null
},
{
"id": 4,
"missing": false,
"name": "Meat",
"numeric_value": 4
},
{
"id": 32767,
"missing": true,
"name": "Not asked",
"numeric_value": 32767
}
],
"class": "categorical",
"ordinal": false
}
}
],
"element": "crunch:cube",
"filtered": {
"unweighted_n": 1662,
"weighted_n": 1662
},
"measures": {
"count": {
"data": [
32,
0,
8,
3,
0,
12,
0,
33,
0,
45,
27,
0,
21,
0,
158,
3,
218,
132,
0,
105,
0,
66,
0,
102,
60,
0,
82,
0,
93,
1,
70,
47,
0,
190,
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
],
"metadata": {
"derived": true,
"references": {},
"type": {
"class": "numeric",
"integer": true,
"missing_reasons": {
"No Data": -1
},
"missing_rules": {}
}
},
"n_missing": 10
}
},
"missing": 10,
"n": 1662,
"unfiltered": {
"unweighted_n": 1662,
"weighted_n": 1662
}
}
}
Loading

0 comments on commit e7946e1

Please sign in to comment.