Skip to content

Commit

Permalink
Merge branch 'sig-test-3d-tables-157314298' into rel-5.2.84
Browse files Browse the repository at this point in the history
  • Loading branch information
Crunch.io Jenkins Account committed Jun 19, 2018
2 parents 25bc1b7 + 69f7f1d commit 32eec4f
Show file tree
Hide file tree
Showing 8 changed files with 4,419 additions and 22 deletions.
54 changes: 35 additions & 19 deletions src/cr/cube/crunch_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from scipy.stats.contingency import expected_freq

from .mixins.data_table import DataTable
from .cube_slice import CubeSlice
from .measures.index import Index
from .measures.scale_means import ScaleMeans
from .utils import lazyproperty
Expand Down Expand Up @@ -59,16 +60,9 @@ def __init__(self, response):
So we need to check its type, and convert it to a dictionary if
it's JSON, if possible.
'''

# If the provided response is dict, create cube immediately
if isinstance(response, dict):
super(CrunchCube, self).__init__(response.get('value', response))
# self._table = DataTable(response.get('value', response))
return

try:
response = json.loads(response)
# self._table = DataTable(response.get('value', response))
if not isinstance(response, dict):
response = json.loads(response)
super(CrunchCube, self).__init__(response.get('value', response))
except TypeError:
# If an unexpected type is provided raise descriptive exception.
Expand All @@ -78,6 +72,8 @@ def __init__(self, response):
'A `cube` must be JSON or `dict`.'
).format(type(response)))

self.slices = self._get_slices()

def _fix_shape(self, array):
'''Fixes shape of MR variables.
For MR variables, where 'selections' dims are dropped, the ndarray
Expand Down Expand Up @@ -1135,16 +1131,12 @@ def index(self, weighted=True, prune=False):
'''Get cube index measurement.'''
return Index(self, weighted, prune).data

def zscore(self, weighted=True, prune=False, hs_dims=None):
'''Get cube zscore measurement.'''
counts = self.as_array(weighted=weighted, prune=prune)

total = self.margin(weighted=weighted, prune=prune)
colsum = self.margin(axis=0, weighted=weighted, prune=prune)
rowsum = self.margin(axis=1, weighted=weighted, prune=prune)

if self.has_mr or self.ca_dim_ind is not None:
if not self.is_double_mr and self.mr_dim_ind == 0:
def _calculate_std_res(self, counts, total, colsum, rowsum, slice_):
dim_types = slice_.dim_types
has_mr_or_ca = 'categorical_array' in dim_types or 'multiple_response' in dim_types
# if self.has_mr or self.ca_dim_ind is not None:
if has_mr_or_ca:
if not self.is_double_mr and (self.mr_dim_ind == 0 or self.mr_dim_ind == 1 and self.ndim == 3):
total = total[:, np.newaxis]
rowsum = rowsum[:, np.newaxis]

Expand All @@ -1161,6 +1153,24 @@ def zscore(self, weighted=True, prune=False, hs_dims=None):
np.outer(total - rowsum, total - colsum) / total**3
)
res = residuals / np.sqrt(variance)
return res

def zscore(self, weighted=True, prune=False, hs_dims=None):
'''Get cube zscore measurement.'''

res = []
for slice_ in self.slices:
counts = slice_.as_array(weighted=weighted, prune=prune)
total = slice_.margin(weighted=weighted, prune=prune)
colsum = slice_.margin(axis=0, weighted=weighted, prune=prune)
rowsum = slice_.margin(axis=1, weighted=weighted, prune=prune)
std_res = self._calculate_std_res(counts, total, colsum, rowsum, slice_)
res.append(std_res)

if len(res) == 1:
res = res[0]
else:
res = np.array(res)

if hs_dims:
res = self._intersperse_hs_in_std_res(hs_dims, res)
Expand Down Expand Up @@ -1198,3 +1208,9 @@ def pvals(self, weighted=True, prune=False, hs_dims=None):
def scale_means(self):
'''Get cube means.'''
return ScaleMeans(self).data

def _get_slices(self):
if self.ndim < 3:
return [CubeSlice(self, 0)]

return [CubeSlice(self, i) for i, _ in enumerate(self.labels()[0])]
5 changes: 2 additions & 3 deletions src/cr/cube/cube_slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,9 @@ def dim_types(self):
'''Get dimension types of the cube slice.'''
return self._cube.dim_types[-2:]

@lazyproperty
def pvals(self):
def pvals(self, *args, **kwargs):
'''Get pvals of the cube.'''
return self._cube.pvals
return self._call_cube_method('pvals', *args, **kwargs)

def inserted_hs_indices(self, *args, **kwargs):
'''Get inserted H&S indices.'''
Expand Down
7 changes: 7 additions & 0 deletions tests/integration/fixtures/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,10 @@ def _load(cube_file):
CA_X_MR_HS = _load('ca-x-mr-hs.json')
MR_X_CAT_HS = _load('mr-x-cat-hs.json')
CAT_X_MR_HS = _load('cat-x-mr-hs.json')
CA_X_MR_SIG_TESTING_SUBTOTALS = _load('ca-x-mr-sig-testing-subtotals.json')
STARTTIME_X_NORDIC_COUNTRIES_X_FOOD_GROOPS = _load(
'starttime-x-nordic-countries-x-food-groops.json',
)
FOOD_GROOPS_X_STARTTIME_X_NORDIC_COUNTRIES = _load(
'food-groops-x-starttime-x-nordic-countries.json',
)

0 comments on commit 32eec4f

Please sign in to comment.