Skip to content

Commit

Permalink
Merge 1ef3133 into 3c6c6cb
Browse files Browse the repository at this point in the history
  • Loading branch information
ernestoarbitrio committed Dec 9, 2019
2 parents 3c6c6cb + 1ef3133 commit 6af680f
Show file tree
Hide file tree
Showing 3 changed files with 197 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/cr/cube/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -1573,6 +1573,7 @@ class _MeansVector(_BaseVector):

def __init__(self, element, base_counts, means):
super(_MeansVector, self).__init__(element, base_counts)
self._base_counts = base_counts
self._means = means

@lazyproperty
Expand All @@ -1583,6 +1584,10 @@ def means(self):
def values(self):
return self._means

@lazyproperty
def margin(self):
return np.nan * len(self._base_counts)


class _MeansWithMrVector(_MeansVector):
"""MR vector with means for use in a matrix."""
Expand Down
174 changes: 174 additions & 0 deletions tests/fixtures/age-age-gender.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
{
"result": {
"dimensions": [
{
"type": {
"class": "categorical",
"categories": [
{
"id": 1,
"name": "Mean",
"missing": false
}
]
},
"references": {
"alias": "mean",
"name": "mean"
}
},
{
"references": {
"alias": "age4",
"view": {
"show_counts": false,
"show_numeric_values": false,
"transform": {
"insertions": [
{
"function": "subtotal",
"args": [
2,
1
],
"name": "<44",
"anchor": 2
}
]
},
"include_missing": false,
"column_width": null
},
"description": "Respondent age by category",
"name": "age4"
},
"derived": false,
"type": {
"ordinal": false,
"class": "categorical",
"categories": [
{
"numeric_value": null,
"missing": false,
"id": 1,
"name": "Under 30"
},
{
"numeric_value": null,
"missing": false,
"id": 2,
"name": "30-44"
},
{
"numeric_value": null,
"missing": false,
"id": 3,
"name": "45-64"
},
{
"numeric_value": null,
"missing": false,
"id": 4,
"name": "65+"
},
{
"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"
}
]
}
}
],
"measures": {
"mean": {
"data": [
24.4393575687,
37.3212274591,
55.4857195647,
73.0242765864,
{
"?": -8
},
{
"?": -8
},
{
"?": -8
}
],
"n_missing": 0,
"metadata": {
"derived": true,
"references": {},
"type": {
"integer": false,
"class": "numeric",
"missing_rules": {},
"missing_reasons": {
"No Data": -1,
"NaN": -8
}
}
}
}
},
"n": 1500,
"filter_stats": {
"filtered_complete": {
"unweighted": {
"selected": 1500,
"other": 0,
"missing": 0
},
"weighted": {
"selected": 1500.0,
"other": 0,
"missing": 0
}
},
"filtered": {
"unweighted": {
"selected": 1500,
"other": 0,
"missing": 0
},
"weighted": {
"selected": 1500.0,
"other": 0,
"missing": 0
}
}
},
"unfiltered": {
"unweighted_n": 1500,
"weighted_n": 1500.0
},
"filtered": {
"unweighted_n": 1500,
"weighted_n": 1500.0
},
"counts": [
189,
395,
606,
310,
0,
0,
0
]
}
}
18 changes: 18 additions & 0 deletions tests/integration/test_cubepart.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,24 @@ def it_provides_missing(self):
cube = Cube(CR.CAT_X_CAT)
assert cube.missing == 5

def it_provides_nan_margin_when_has_weighted_mean_without_weighted_counts(self):
slice_ = Cube(CR.AGE_AGE_GENDER).partitions[0]

np.testing.assert_array_almost_equal(
slice_.counts,
np.array(
[[24.43935757, 37.32122746, 61.76058503, 55.48571956, 73.02427659]]
),
)
np.testing.assert_array_almost_equal(
slice_.means,
np.array([[24.43935757, 37.32122746, np.nan, 55.48571956, 73.02427659]]),
)
np.testing.assert_array_almost_equal(slice_.rows_margin, np.array([np.nan]))
np.testing.assert_array_almost_equal(
slice_.columns_margin, np.array([np.nan] * len(slice_.counts[0, :]))
)

def it_calculates_various_measures(self):
transforms = {
"columns_dimension": {"insertions": {}},
Expand Down

0 comments on commit 6af680f

Please sign in to comment.