Skip to content

Commit a7ff8a0

Browse files
Merge pull request #415 from Crunch-io/fix-3d-cube-with-odd-order
fix _replaced_order_element_ids to avoid None returns
2 parents da0d3a4 + d321700 commit a7ff8a0

File tree

4 files changed

+38
-9
lines changed

4 files changed

+38
-9
lines changed

src/cr/cube/dimension.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class Dimensions(tuple):
6363
"""Collection containing every dimension defined in cube response."""
6464

6565
def __repr__(self) -> str:
66-
"str representation of this dimension sequence." ""
66+
"""str representation of this dimension sequence."""
6767
dim_lines = "\n".join(str(d) for d in self)
6868
return f"Dimensions:\n{dim_lines}"
6969

@@ -875,7 +875,11 @@ def _replaced_order_element_ids(self, element_ids) -> List[Optional[str]]:
875875
The explicit order transform includes a list of ids that can be specified in
876876
many different ways, this translate them to the subvariable aliases.
877877
"""
878-
return [self.translate_element_id(_id) for _id in element_ids]
878+
return [
879+
el
880+
for _id in element_ids
881+
if (el := self.translate_element_id(_id)) is not None
882+
]
879883

880884
@lazyproperty
881885
def _subvar_aliases(self) -> Tuple[str, ...]:

src/cr/cube/matrix/measure.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1621,7 +1621,7 @@ def _base_values(self):
16211621
in this example (2,3).
16221622
"""
16231623
# --- Use "body" reference values for base values
1624-
(ref_props, ref_bases) = self._reference_values(0)
1624+
ref_props, ref_bases = self._reference_values(0)
16251625
return self._calculate_t_stats(
16261626
self._proportions[0][0], self._column_bases[0][0], ref_props, ref_bases
16271627
)
@@ -1709,9 +1709,9 @@ def _calculate_t_stats(self, props, bases, ref_props, ref_bases):
17091709

17101710
@lazyproperty
17111711
def _intersections(self):
1712-
"2D ndarray np.float64 of the t-stats for the intersections (4th block)" ""
1712+
"""2D ndarray np.float64 of the t-stats for the intersections (4th block)"""
17131713
# --- Use "inserted" reference values for intersections
1714-
(ref_props, ref_variance) = self._reference_values(1)
1714+
ref_props, ref_variance = self._reference_values(1)
17151715
return self._calculate_t_stats(
17161716
self._proportions[1][1], self._column_bases[1][1], ref_props, ref_variance
17171717
)
@@ -1725,7 +1725,7 @@ def _proportions(self):
17251725
def _subtotal_columns(self):
17261726
"""2D ndarray np.float64 of the values for the subtotal columns (2nd block)"""
17271727
# --- Use "body" reference values for inserted columns
1728-
(ref_props, ref_variance) = self._reference_values(0)
1728+
ref_props, ref_variance = self._reference_values(0)
17291729
return self._calculate_t_stats(
17301730
self._proportions[0][1], self._column_bases[0][1], ref_props, ref_variance
17311731
)
@@ -1734,7 +1734,7 @@ def _subtotal_columns(self):
17341734
def _subtotal_rows(self):
17351735
"""2D ndarray np.float64 of the t-stats for the subtotal rows (3rd block)"""
17361736
# --- Use "inserted" reference values for inserted rows
1737-
(ref_props, ref_variance) = self._reference_values(1)
1737+
ref_props, ref_variance = self._reference_values(1)
17381738
return self._calculate_t_stats(
17391739
self._proportions[1][0], self._column_bases[1][0], ref_props, ref_variance
17401740
)

tests/integration/test_cubepart.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,31 @@
2222
class Test_Slice:
2323
"""Integration-test suite for _Slice object."""
2424

25+
def test_it_provides_values_for_cat_x_mr_x_mr_with_odd_order(self):
26+
transforms = {
27+
"columns_dimension": {
28+
"elements": {
29+
"2": {"hide": True},
30+
"3": {"hide": True},
31+
"4": {"hide": True},
32+
"foo": {"hide": True},
33+
"bar": {"hide": True},
34+
},
35+
"order": {
36+
"element_ids": [1, 2, 3, 4, 6, "foo", "bar"],
37+
"type": "explicit",
38+
},
39+
},
40+
}
41+
parts = Cube(CR.CAT_X_MR_X_MR, transforms=transforms).partitions
42+
assert len(parts) == 2
43+
assert parts[0].column_proportions == pytest.approx(
44+
np.array([[0.60553814], [0.10292581], [0.10031348]])
45+
)
46+
assert parts[1].column_proportions == pytest.approx(
47+
np.array([[0.08141321], [0.60522273], [0.5847414]])
48+
)
49+
2550
def test_it_provides_values_for_cat_x_cat(self):
2651
slice_ = Cube(CR.CAT_X_CAT, population=9001).partitions[0]
2752

tests/integration/test_scale_means.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ def test_cat_x_cat_pruning_and_hs():
239239
np.testing.assert_almost_equal(
240240
slice_.columns_scale_mean,
241241
[1.57933884, 1.8308135, 2.10618401, 2.30460074, np.nan, 2.34680135],
242-
),
242+
)
243243
np.testing.assert_almost_equal(
244244
slice_.rows_scale_mean,
245245
[1.74213625, 2.2364515, 1.97, 2.45356177, 2.11838791, np.nan, 2.0],
@@ -268,7 +268,7 @@ def test_cat_x_cat_pruning_and_hs():
268268
slice_ = Cube(CR.CAT_HS_MT_X_CAT_HS_MT, transforms=transforms).partitions[0]
269269
np.testing.assert_almost_equal(
270270
slice_.columns_scale_mean, [1.57933884, 2.106184, 2.3046007, 2.34680135]
271-
),
271+
)
272272
np.testing.assert_almost_equal(
273273
slice_.rows_scale_mean, [1.74213625, 1.97, 2.45356177, 2.11838791, 2.0]
274274
)

0 commit comments

Comments
 (0)