Skip to content

Commit

Permalink
Correct expectations and fix H&S in scale means
Browse files Browse the repository at this point in the history
  • Loading branch information
slobodan-ilic committed Aug 8, 2018
1 parent 8b269f8 commit 0152017
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
18 changes: 12 additions & 6 deletions src/cr/cube/crunch_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -1128,14 +1128,20 @@ def scale_means(self, hs_dims=None, prune=False):
slices_means = [ScaleMeans(slice_).data for slice_ in self.slices]

if hs_dims and self.ndim > 1:
# Intersperse scale means with nans if H&S specified
inserted_indices = self.inserted_hs_indices()
# Intersperse scale means with nans if H&S specified, and 2D. No
# need to modify 1D, as only one mean will ever be inserted.
inserted_indices = self.inserted_hs_indices()[-2:]
for scale_means in slices_means:
if scale_means[0] is not None and 0 in hs_dims and inserted_indices[0]:
for i in inserted_indices[0]:
scale_means[0] = np.insert(scale_means[0], i, np.nan)
if scale_means[1] is not None and 1 in hs_dims and inserted_indices[1]:
# Scale means 0 corresonds to the column dimension (is
# calculated by using its values). The result of it, however,
# is a row. That's why we need to check the insertions on the
# row dim (inserted columns).
if scale_means[0] is not None and 1 in hs_dims and inserted_indices[1]:
for i in inserted_indices[1]:
scale_means[0] = np.insert(scale_means[0], i, np.nan)
# Scale means 1 is a column, so we need to check for row insertions.
if scale_means[1] is not None and 0 in hs_dims and inserted_indices[0]:
for i in inserted_indices[0]:
scale_means[1] = np.insert(scale_means[1], i, np.nan)

if prune:
Expand Down
8 changes: 4 additions & 4 deletions tests/integration/test_scale_means.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ def test_cat_x_cat_with_hs():

# Test with H&S
expected = [[
np.array([2.19444444, 2.19230769, np.nan, 2.26666667, 1.88990826, 1.76363636, 3.85]),
np.array([3.87368421, 2.51767677, 3.38429752, 3.66666667, 4.13235294]),
np.array([2.19444444, 2.19230769, 2.26666667, 1.88990826, 1.76363636, 3.85]),
np.array([3.87368421, 2.51767677, np.nan, 3.38429752, 3.66666667, 4.13235294]),
]]
actual = cube.scale_means(hs_dims=[0, 1])
assert_scale_means_equal(actual, expected)
Expand Down Expand Up @@ -180,8 +180,8 @@ def test_cat_x_cat_with_hs_on_both_dims():

# Test with H&S
expected =[[
np.array([2.19444444, 2.19230769, np.nan, 2.26666667, 1.88990826, 1.76363636, 3.85]),
np.array([3.87368421, 2.51767677, 3.38429752, np.nan, 3.66666667, 4.13235294]),
np.array([2.19444444, 2.19230769, 2.26666667, np.nan, 1.88990826, 1.76363636, 3.85]),
np.array([3.87368421, 2.51767677, np.nan, 3.38429752, 3.66666667, 4.13235294]),
]]
actual = cube.scale_means(hs_dims=[0, 1])
assert_scale_means_equal(actual, expected)
Expand Down

0 comments on commit 0152017

Please sign in to comment.