From 9b0e234232c7cf9c86843b39db2c9c4d8f843e67 Mon Sep 17 00:00:00 2001 From: Steve Canny Date: Tue, 25 Aug 2020 15:21:57 -0700 Subject: [PATCH] fix: Python3, Black, and numpy fixes --- src/cr/cube/matrix.py | 42 ++++++++++++++++++++++-------- tests/integration/test_cubepart.py | 1 - 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/src/cr/cube/matrix.py b/src/cr/cube/matrix.py index 97a9000be..cdaa7beb4 100644 --- a/src/cr/cube/matrix.py +++ b/src/cr/cube/matrix.py @@ -30,7 +30,7 @@ def __init__(self, unordered_matrix): @classmethod def matrix(cls, cube, dimensions, slice_idx): - """-> TransformedMatrix object constructed from values of `cube`. + """Return TransformedMatrix object constructed from values of `cube`. `cube` is the `cr.cube.Cube` object containing the data for this matrix. Note that not all the data in `cube` will necessarily be used by this matrix. When @@ -280,7 +280,7 @@ def _columns_dimension(self): @lazyproperty def _inserted_columns(self): - """-> tuple of _InsertedColumn objects representing subtotal columns. + """tuple of _InsertedColumn objects representing subtotal columns. The returned vectors are in the order subtotals were specified in the cube result, which is no particular order. All subtotals defined on the column @@ -308,7 +308,7 @@ def _inserted_columns(self): @lazyproperty def _inserted_rows(self): - """-> tuple of _InsertedRow objects representing inserted subtotal rows. + """tuple of _InsertedRow objects representing inserted subtotal rows. The returned vectors are in the order subtotals were specified in the cube result, which is no particular order. @@ -331,7 +331,7 @@ def _inserted_rows(self): @lazyproperty def _row_order(self): - """ -> 1D ndarray of int row idx specifying order of unordered-array rows.""" + """1D ndarray of int row idx specifying order of unordered-array rows.""" dimension = self._rows_dimension collation_method = dimension.collation_method @@ -436,7 +436,7 @@ def table_margin(self): ) # pragma: no cover def _array_type_std_res(self, counts, total, colsum, rowsum): - """-> 2D ndarray of np.float64 std-res value for each cell of MR matrix. + """Return 2D np.float64 ndarray of std-res value for each cell of MR matrix. This is a utility method used by a matrix with one or more MR dimensions. The caller forms the input arrays based on which of its dimensions are MR. @@ -1963,7 +1963,17 @@ def base(self): because each MR_SUBVAR element has a distinct unweighted N. A vector opposing a CAT dimension produces a scalar value. """ - return self._unassembled_vector.base + unassembled_base = self._unassembled_vector.base + + # --- `base` is an ndarray when opposing dimension is MR or CA_SUBVAR. + # --- Neither of these can have subtotals (subvars don't add), so no + # --- interleaving is necessary. + if isinstance(unassembled_base, np.ndarray): + return unassembled_base[self._opposing_order] + + # --- otherwise it's a scalar value and neither interleaving nor ordering + # --- is required + return unassembled_base @lazyproperty def column_index(self): @@ -1973,7 +1983,7 @@ def column_index(self): """ def fsubtot(inserted_vector): - """-> np.nan as unconditional col-index value for `inserted_vector`. + """Return np.nan as unconditional col-index value for `inserted_vector`. Called by ._apply_interleaved() to compute inserted value which it places in the right vector position. @@ -2035,7 +2045,17 @@ def margin(self): dimension produces a scalar value. Values are np.int64 if the cube-result is unweighted. """ - return self._unassembled_vector.margin + unassembled_margin = self._unassembled_vector.margin + + # --- `margin` is an ndarray when opposing dimension is MR or CA_SUBVAR. + # --- Neither of these can have subtotals (subvars don't add), so no + # --- interleaving is necessary. + if isinstance(unassembled_margin, np.ndarray): + return unassembled_margin[self._opposing_order] + + # --- otherwise it's a scalar value and neither interleaving nor ordering + # --- is required + return unassembled_margin @lazyproperty def means(self): @@ -2045,7 +2065,7 @@ def means(self): """ def fsubtot(inserted_vector): - """-> np.nan as unconditional mean value for `inserted_vector`. + """Return np.nan as unconditional mean value for `inserted_vector`. Passed to and called by ._apply_interleaved() to compute inserted value which it places in the right vector position. @@ -2157,7 +2177,7 @@ def zscores(self): """ def fsubtot(inserted_vector): - """-> np.float64 zscore for `inserted_vector`. + """Return np.float64 zscore for `inserted_vector`. Passed to and called by ._apply_interleaved() to compute inserted value which it places in the right vector position. @@ -2187,7 +2207,7 @@ def fsubtot(inserted_vector): return self._apply_interleaved(self._unassembled_vector.zscores, fsubtot) def _apply_interleaved(self, unassembled_values, fsubtot): - """-> 1D array of result of applying fbase or fsubtot to each interleaved item. + """Return 1D array result of applying fbase or fsubtot to each interleaved item. `unassembled_values` is the "unassembled" vector measure values. This vector can be a base vector or an inserted vector. diff --git a/tests/integration/test_cubepart.py b/tests/integration/test_cubepart.py index 3a600c0f3..694313348 100644 --- a/tests/integration/test_cubepart.py +++ b/tests/integration/test_cubepart.py @@ -702,7 +702,6 @@ def it_places_insertions_on_a_reordered_dimension_in_the_right_position(self): ], ) - @pytest.mark.xfail(reason="WIP", raises=AssertionError, strict=True) def it_provides_same_proportions_without_explicit_order(self): transforms = TR.TEST_DASHBOARD_TRANSFORM_SINGLE_EL_VISIBLE slice_ = Cube(CR.TEST_DASHBOARD_FIXTURE, transforms=transforms).partitions[0]