Skip to content

Commit

Permalink
Merge 33e040d into 9c85afa
Browse files Browse the repository at this point in the history
  • Loading branch information
slobodan-ilic committed Aug 10, 2018
2 parents 9c85afa + 33e040d commit ca14e2c
Show file tree
Hide file tree
Showing 7 changed files with 10,701 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/cr/cube/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# encoding: utf-8

'''Shared constants'''

ITEM_DIMENSION_TYPES = ('categorical_array', 'multiple_response')
9 changes: 7 additions & 2 deletions src/cr/cube/crunch_cube.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# encoding: utf-8

'''Home of the CrunchCube class.
This module contains the definition of the CrunchCube class. It represents
the open-source library used for manipulating the crunch cubes (JSON responses
from the Crunch.io platform).
'''

from __future__ import division

import json
Expand All @@ -17,6 +20,7 @@
from .measures.index import Index
from .measures.scale_means import ScaleMeans
from .utils import lazyproperty
from . import ITEM_DIMENSION_TYPES

np.seterr(divide='ignore', invalid='ignore')

Expand Down Expand Up @@ -147,7 +151,7 @@ def _transform(self, res, include_transforms_for_dims,
i - dim_offset in include_transforms_for_dims)
if dim.type == 'multiple_response':
dim_offset += 1
if not transform or dim.type == 'categorical_array':
if not transform or dim.type in ITEM_DIMENSION_TYPES:
continue
# Perform transformations
insertions = self._insertions(res, dim, i)
Expand Down Expand Up @@ -947,6 +951,7 @@ def hs_dims_for_den(hs_dims, axis):

# Calculate nominator from table (include all H&S dimensions).
num = self._transform(table, include_transforms_for_dims, inflate=True)
# num = self._transform(table, hs_dims, inflate=True)

res = self._fix_shape(num / den)

Expand Down Expand Up @@ -1041,7 +1046,7 @@ def index(self, weighted=True, prune=False):

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
has_mr_or_ca = set(dim_types) & set(ITEM_DIMENSION_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):
Expand Down
7 changes: 5 additions & 2 deletions src/cr/cube/dimension.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# encoding: utf-8

'''Contains implementation of the Dimension class, for Crunch Cubes.'''

import numpy as np

from .subtotal import Subtotal
from .utils import lazyproperty, memoize
from . import ITEM_DIMENSION_TYPES


class Dimension(object):
Expand Down Expand Up @@ -115,8 +118,8 @@ def _elements(self):
@property
def inserted_hs_indices(self):
'''Returns inserted H&S indices for the dimension.'''
if (self.type == 'categorical_array' or not self.subtotals):
return [] # For CA subvariables, we don't do H&S insertions
if (self.type in ITEM_DIMENSION_TYPES or not self.subtotals):
return [] # For CA and MR items, we don't do H&S insertions

elements = self.elements()
element_ids = [element['id'] for element in elements]
Expand Down
24 changes: 24 additions & 0 deletions src/cr/cube/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'''Utility functions for crunch cube, as well as other modules.'''
import collections
import functools
import numpy as np

try:
from itertools import ifilterfalse
Expand Down Expand Up @@ -122,3 +123,26 @@ def clear():


memoize = lru_cache(100)


def compress_pruned(table):
"""Compress table based on pruning mask.
Only the rows/cols in which all of the elements are masked need to be
pruned.
"""
if not isinstance(table, np.ma.core.MaskedArray):
return table

if table.ndim == 0:
return table.data

if table.ndim == 1:
return np.ma.compressed(table)

row_inds = ~table.mask.all(axis=1)
col_inds = ~table.mask.all(axis=0)
table = table[row_inds, :][:, col_inds]
if table.dtype == float and table.mask.any():
table[table.mask] = np.nan
return table
1 change: 1 addition & 0 deletions tests/integration/fixtures/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,4 @@ def _load(cube_file):
GENDER_X_WEIGHT = _load('gender-x-weight.json')
CAT_X_CAT_PRUNING_HS = _load('cat-x-cat-pruning-hs.json')
CA_ITEMS_X_CA_CAT_X_CAT = _load('ca-items-x-ca-cat-x-cat.json')
CAT_X_MR_X_CAT = _load('cat-x-mr-x-cat.json')
Loading

0 comments on commit ca14e2c

Please sign in to comment.