Skip to content

Commit

Permalink
Merge branch 'support-ca-as-0th-cube-#158497249' into rel-5.2.103
Browse files Browse the repository at this point in the history
  • Loading branch information
Crunch.io Jenkins Account committed Jun 26, 2018
2 parents e4e1b85 + 0779141 commit 1facf14
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
8 changes: 4 additions & 4 deletions src/cr/cube/crunch_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def __init__(self, response):
'A `cube` must be JSON or `dict`.'
).format(type(response)))

self.slices = self._get_slices()
self.slices = self.get_slices()

def _fix_shape(self, array):
'''Fixes shape of MR variables.
Expand Down Expand Up @@ -1165,8 +1165,8 @@ def scale_means(self):
'''Get cube means.'''
return ScaleMeans(self).data

def _get_slices(self):
if self.ndim < 3:
def get_slices(self, ca_as_0th=False):
if self.ndim < 3 and not ca_as_0th:
return [CubeSlice(self, 0)]

return [CubeSlice(self, i) for i, _ in enumerate(self.labels()[0])]
return [CubeSlice(self, i, ca_as_0th) for i, _ in enumerate(self.labels()[0])]
34 changes: 28 additions & 6 deletions src/cr/cube/cube_slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,23 @@ class CubeSlice(object):
'''

row_dim_ind = 0
col_dim_ind = 1

def __init__(self, cube, index):
def __init__(self, cube, index, ca_as_0th=False):

if ca_as_0th and cube.dim_types[0] != 'categorical_array':
msg = (
'Cannot set CA as 0th for cube that '
'does not have CA items as the 0th dimension.'
)
raise ValueError(msg)

self._cube = cube
self._index = index
self.ca_as_0th = ca_as_0th

@property
def col_dim_ind(self):
return 1 if not self.ca_as_0th else 0

def __getattr__(self, attr):
cube_attr = getattr(self._cube, attr)
Expand All @@ -45,6 +57,12 @@ def _update_args(self, kwargs):
# If cube is 2D it doesn't actually have slices (itself is a slice).
# In this case we don't need to convert any arguments, but just
# pass them to the underlying cube (which is the slice).
if self.ca_as_0th:
axis = kwargs.get('axis', False)
if axis is None:
# TODO: Write detailed explanation here in comments.
# Special case for CA slices (in multitables).
kwargs['axis'] = 1
return kwargs

# Handling API methods that include 'axis' parameter
Expand Down Expand Up @@ -88,7 +106,7 @@ def _update_args(self, kwargs):
return kwargs

def _update_result(self, result):
if self.ndim < 3 or len(result) - 1 < self._index:
if (self.ndim < 3 and not self.ca_as_0th) or len(result) - 1 < self._index:
return result
result = result[self._index]
if isinstance(result, tuple):
Expand All @@ -100,7 +118,7 @@ def _update_result(self, result):
def _call_cube_method(self, method, *args, **kwargs):
kwargs = self._update_args(kwargs)
result = getattr(self._cube, method)(*args, **kwargs)
if method in ('labels', 'inserted_hs_indices'):
if method in ('labels', 'inserted_hs_indices') and not self.ca_as_0th:
return result[-2:]
return self._update_result(result)

Expand All @@ -112,7 +130,7 @@ def table_name(self):
of the cube name with the label of the corresponding slice
(nth label of the 0th dimension).
'''
if self.ndim < 3:
if self.ndim < 3 and not self.ca_as_0th:
return None

title = self._cube.name
Expand Down Expand Up @@ -153,7 +171,11 @@ def ca_main_axis(self):

def labels(self, hs_dims=None, prune=False):
'''Get labels for the cube slice, and perform pruning by slice.'''
labels = self._cube.labels(include_transforms_for_dims=hs_dims)[-2:]
if self.ca_as_0th:
labels = self._cube.labels(include_transforms_for_dims=hs_dims)[1:]
else:
labels = self._cube.labels(include_transforms_for_dims=hs_dims)[-2:]

if not prune:
return labels

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_crunch_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


# pylint: disable=invalid-name, no-self-use, protected-access
@patch('cr.cube.crunch_cube.CrunchCube._get_slices', lambda x: None)
@patch('cr.cube.crunch_cube.CrunchCube.get_slices', lambda x: None)
class TestCrunchCube(TestCase):
'''Test class for the CrunchCube unit tests.
Expand Down

0 comments on commit 1facf14

Please sign in to comment.