Skip to content

Commit

Permalink
Merge branch 'prune-slice-shape-160788065' into rel-5.2.366
Browse files Browse the repository at this point in the history
  • Loading branch information
Crunch.io Jenkins Account committed Sep 26, 2018
2 parents f4deaa0 + ee4e925 commit cee1adf
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 3 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,8 @@ The detailed description can be found
- Add property `is_double_mr` to `CubeSlice` (which is needed since it differs from the interpretation of the cube. E.g. MR x CA x MR will render slices which are *not* double MRs).
- Add `shape`, `ndim`, and `scale_means` to `CubeSlice`, for accessibility.
- `index` now also operates on slices (no api change).

#### 1.6.8 Deprecate `shape`
- Deprecate the `CubeSlice` `shape` property
- Use `get_shape(prune=False)` instead
- Will be removed in future versions
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from setuptools import setup, find_packages

version = '1.6.7'
version = '1.6.8'


def get_long_desc():
Expand Down
15 changes: 13 additions & 2 deletions src/cr/cube/cube_slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

from functools import partial
import numpy as np
import warnings

from .utils import lazyproperty
from .utils import lazyproperty, compress_pruned


# pylint: disable=too-few-public-methods
Expand Down Expand Up @@ -258,4 +259,14 @@ def ndim(self):

@lazyproperty
def shape(self):
return self.as_array().shape
warnings.warn('Deprecated. Use `get_shape` instead.', DeprecationWarning)
return self.get_shape()

def get_shape(self, prune=False):
if not prune:
return self.as_array().shape

shape = compress_pruned(self.as_array(prune=True)).shape
# Eliminate dimensions that get reduced to 1
# (e.g. single element categoricals)
return tuple(n for n in shape if n > 1)
39 changes: 39 additions & 0 deletions tests/unit/test_cube_slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from unittest import TestCase
from mock import Mock
import numpy as np
import pytest

from cr.cube.cube_slice import CubeSlice

Expand Down Expand Up @@ -375,3 +376,41 @@ def test_ca_dim_ind(self):
cube.ca_dim_ind = 0
cs = CubeSlice(cube, 0)
assert cs.ca_dim_ind == None

def test_shape(self):
cube = Mock()

cube.ndim = 2
cube.as_array.return_value = np.zeros((3, 2))
cs = CubeSlice(cube, 0)
with pytest.warns(DeprecationWarning):
# TODO: Remove once 'shape' is removed
assert cs.shape == (3, 2)

# Test non-pruned
assert cs.get_shape() == (3, 2)

# Test pruned
cube.as_array.return_value = np.ma.masked_array(
np.zeros((3, 2)),
mask=np.array([[True, False], [True, False], [True, False]])
)
assert cs.get_shape(prune=True) == (3,)

cube.as_array.return_value = np.ma.masked_array(
np.zeros((3, 2)),
mask=np.array([[False, False], [True, True], [True, True]])
)
assert cs.get_shape(prune=True) == (2,)

cube.as_array.return_value = np.ma.masked_array(
np.zeros((3, 2)),
mask=np.array([[False, False], [True, True], [False, False]])
)
assert cs.get_shape(prune=True) == (2, 2)

cube.as_array.return_value = np.ma.masked_array(
np.zeros((3, 2)),
mask=np.array([[True, True], [True, True], [True, True]])
)
assert cs.get_shape(prune=True) == ()

0 comments on commit cee1adf

Please sign in to comment.