Skip to content

Commit

Permalink
Merge branch 'major-proportions-refactor-#158185408' into rel-5.2.61
Browse files Browse the repository at this point in the history
  • Loading branch information
Crunch.io Jenkins Account committed Jun 12, 2018
2 parents 2c2f811 + 48abe4d commit b10fea9
Show file tree
Hide file tree
Showing 10 changed files with 680 additions and 462 deletions.
392 changes: 141 additions & 251 deletions src/cr/cube/crunch_cube.py

Large diffs are not rendered by default.

35 changes: 14 additions & 21 deletions src/cr/cube/cube_slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import numpy as np

from .utils import lazyproperty


class CubeSlice(object):
'''Implementation of CubeSlice class.
Expand Down Expand Up @@ -32,18 +34,6 @@ def _update_args(self, kwargs):
kwargs['axis'] += 1
return kwargs

# def _update_args(self, kwargs):
# # if cube is 3D we need to adjust the axis:
# # - if it was "None" we need to pass "(1, 2)"
# # - if it was 0 or 1, we need to increment by one to adjust for tab dim
# if self.ndim == 3 and 'axis' in kwargs:
# if kwargs['axis'] is None:
# kwargs['axis'] = (1, 2)
# elif isinstance(kwargs['axis'], int):
# kwargs['axis'] += 1
#
# return kwargs

def _update_result(self, result):
if self.ndim < 3 or len(result) - 1 < self._index:
return result
Expand All @@ -59,12 +49,12 @@ def _call_cube_method(self, method, *args, **kwargs):

# Properties

@property
@lazyproperty
def ndim(self):
'''Get number of dimensions.'''
return self._cube.ndim

@property
@lazyproperty
def name(self):
'''Get slice name.
Expand All @@ -80,15 +70,15 @@ def name(self):
table_name = self._cube.labels()[0][self._index]
return '%s: %s' % (title, table_name)

@property
@lazyproperty
def rows_title(self):
'''Get title of the rows dimension.
For 3D it's the 1st dimension (0th dimension of the current slice).
'''
return self._cube.dimensions[1].name

@property
@lazyproperty
def inserted_rows_indices(self):
''' Get correct inserted rows indices for the corresponding slice.
Expand All @@ -99,12 +89,12 @@ def inserted_rows_indices(self):
'''
return self._cube.inserted_hs_indices()[self._index][0]

@property
@lazyproperty
def has_means(self):
'''Get has_means from cube.'''
return self._cube.has_means

@property
@lazyproperty
def dimensions(self):
'''Get slice dimensions.
Expand Down Expand Up @@ -141,7 +131,7 @@ def population_counts(self, *args, **kwargs):
'''Get population counts.'''
return self._call_cube_method('population_counts', *args, **kwargs)

@property
@lazyproperty
def standardized_residuals(self):
'''Get cube's standardized residuals.'''
return self._cube.standardized_residuals
Expand All @@ -154,13 +144,16 @@ def zscore(self, *args, **kwargs):
'''Get index.'''
return self._call_cube_method('zscore', *args, **kwargs)

@property
@lazyproperty
def dim_types(self):
'''Get dimension types of the cube slice.'''
return self._cube.dim_types[-2:]

@property
@lazyproperty
def pvals(self):
'''Get pvals of the cube.'''
return self._cube.pvals

def inserted_hs_indices(self, *args, **kwargs):
'''Get inserted H&S indices.'''
return self._cube.inserted_hs_indices(*args, **kwargs)[-2:]
34 changes: 24 additions & 10 deletions src/cr/cube/dimension.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import numpy as np

from cr.cube.subtotal import Subtotal
from .subtotal import Subtotal
from .utils import lazyproperty


class Dimension(object):
Expand All @@ -16,7 +17,7 @@ def __init__(self, dim, selections=None):
self._dim = dim
self._type = self._get_type(dim, selections)

@property
@lazyproperty
def values(self):
values = [
el.get('numeric_value', np.nan)
Expand All @@ -25,7 +26,7 @@ def values(self):
]
return [val if val is not None else np.nan for val in values]

@property
@lazyproperty
def subtotals(self):
view = self._dim.get('references', {}).get('view', {})

Expand Down Expand Up @@ -104,14 +105,16 @@ def _get_name(cls, element):

return None

@property
@lazyproperty
def _elements(self):
if self.type == 'categorical':
return self._dim['type']['categories']
return self._dim['type']['elements']

# This needs to be computed each time. Don't use lazyproperty.
@property
def inserted_hs_indices(self):
'''Returns inserted H&S indices for the dimension.'''
if self.type == 'categorical_array':
return [] # For CA subvariables, we don't do H&S insertions

Expand Down Expand Up @@ -151,7 +154,7 @@ def _transform_anchor(self, subtotal):
# shouldn't be any)
return contiguous_anchors[0]

@property
@lazyproperty
def hs_indices(self):
'''Headers and Subtotals indices.'''
elements = self._elements
Expand All @@ -167,7 +170,7 @@ def hs_indices(self):

return indices

@property
@lazyproperty
def has_transforms(self):
view = self._dim['references'].get('view')
if not view:
Expand All @@ -177,25 +180,25 @@ def has_transforms(self):

# API methods

@property
@lazyproperty
def name(self):
'''Name of a cube's dimension.'''
refs = self._dim['references']
return refs.get('name', refs.get('alias'))

@property
@lazyproperty
def description(self):
'''Description of a cube's dimension.'''
refs = self._dim['references']
return refs.get('description')

@property
@lazyproperty
def alias(self):
'''Alias of a cube's dimension.'''
refs = self._dim['references']
return refs.get('alias')

@property
@lazyproperty
def type(self):
'''Get type of the Crunch Dimension.'''
return self._type
Expand Down Expand Up @@ -293,3 +296,14 @@ def valid_indices(self, include_missing):
else:
return [i for (i, el) in enumerate(self._elements)
if not el.get('missing')]

@lazyproperty
def shape(self):
return len(self._elements)

@lazyproperty
def is_selections(self):
category_ids = [el.get('id') for el in self._elements]
if category_ids == [1, 0, -1]:
return True
return False
Empty file added src/cr/cube/mixins/__init__.py
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ def flat_values(self, weighted, margin=False):
for val in values]
return values

@lazyproperty
def _shape(self):
return tuple([dim.shape for dim in self.all_dimensions])

def data(self, weighted, margin=False):
'''Get the data in non-flattened shape.
Expand All @@ -137,7 +141,4 @@ def data(self, weighted, margin=False):
a ndarray of shape (2, 2).
'''
values = self.flat_values(weighted, margin)
all_dimensions = self.all_dimensions
shape = [len(dim.elements(include_missing=True))
for dim in all_dimensions]
return np.array(values).reshape(shape)
return np.array(values).reshape(self._shape)
12 changes: 7 additions & 5 deletions src/cr/cube/subtotal.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'''Contains implementation of the Subtotal class, for Crunch Cubes.'''

from .utils import lazyproperty


class Subtotal(object):
'''Implementation of the Insertion class for Crunch Cubes.
Expand All @@ -12,7 +14,7 @@ def __init__(self, data, dim):
self._data = data
self._dim = dim

@property
@lazyproperty
def is_valid(self):
'''Test if the subtotal data is valid.'''
if isinstance(self._data, dict):
Expand All @@ -22,7 +24,7 @@ def is_valid(self):
return self._data['function'] == 'subtotal'
return False

@property
@lazyproperty
def anchor(self):
'''Get the anchor of the subtotal (if it's valid).'''
if not self.is_valid:
Expand All @@ -37,18 +39,18 @@ def anchor(self):
except (TypeError, ValueError):
return anchor.lower()

@property
@lazyproperty
def _all_dim_ids(self):
return [el.get('id') for el in self._dim.elements(include_missing=True)]

@property
@lazyproperty
def args(self):
'''Get H&S args.'''
if self.is_valid:
return self._data['args']
return []

@property
@lazyproperty
def data(self):
'''Get data in JSON format.'''
return self._data
Loading

0 comments on commit b10fea9

Please sign in to comment.