Exemple #1
0
    def it_can_assemble_a_vector_to_help(self, _row_order_prop_):
        base_values = np.array([1, 2, 3, 4])
        subtotal_values = (3, 5, 7)
        blocks = (base_values, subtotal_values)
        _row_order_prop_.return_value = np.array([-3, 1, 0, -2, 3, 2, -1])
        assembler = StripeAssembler(None, None, None, None)

        assert assembler._assemble_vector(blocks).tolist() == [
            3, 2, 1, 5, 4, 3, 7
        ]
Exemple #2
0
    def it_knows_the_scale_stderr(self, _measures_prop_, measures_,
                                  scaled_counts_):
        scaled_counts_.scale_stderr = 6
        measures_.scaled_counts = scaled_counts_
        _measures_prop_.return_value = measures_
        assembler = StripeAssembler(None, None, None, None)

        assert assembler.scale_stderr == 6
Exemple #3
0
    def it_knows_the_table_margin_range(self, request, _measures_prop_,
                                        measures_):
        measures_.weighted_bases = instance_mock(request,
                                                 _WeightedBases,
                                                 table_margin_range=np.array(
                                                     [50.5, 100.1]))
        _measures_prop_.return_value = measures_
        assembler = StripeAssembler(None, None, None, None)

        assert assembler.table_margin_range.tolist() == [50.5, 100.1]
Exemple #4
0
    def it_knows_the_row_order_to_help(self, request, rows_dimension_,
                                       _measures_prop_, measures_):
        _measures_prop_.return_value = measures_
        _BaseOrderHelper_ = class_mock(
            request, "cr.cube.stripe.assembler._BaseOrderHelper")
        _BaseOrderHelper_.display_order.return_value = (-1, 1, -2, 2, -3, 3)
        assembler = StripeAssembler(None, rows_dimension_, None, None)

        row_order = assembler._row_order

        _BaseOrderHelper_.display_order.assert_called_once_with(
            rows_dimension_, measures_)
        assert row_order.tolist() == [-1, 1, -2, 2, -3, 3]
Exemple #5
0
    def it_knows_the_row_labels(self, request, rows_dimension_,
                                _row_order_prop_):
        rows_dimension_.valid_elements = tuple(
            instance_mock(request, _Element, label=label)
            for label in ("baz", "foo", "bar"))
        rows_dimension_.subtotals = tuple(
            instance_mock(request, _Subtotal, label=label)
            for label in ("bing", "bada"))
        _row_order_prop_.return_value = np.array([1, 2, 0, -1, -2])
        assembler = StripeAssembler(None, rows_dimension_, None, None)

        assert assembler.row_labels.tolist() == [
            "foo", "bar", "baz", "bada", "bing"
        ]
Exemple #6
0
    def it_knows_the_rows_dimension_fills(self, request, rows_dimension_,
                                          _row_order_prop_):
        rows_dimension_.valid_elements = tuple(
            instance_mock(request, _Element, fill=fill)
            for fill in ("cdef01", "6789ab", "012345"))
        _row_order_prop_.return_value = np.array([2, -2, 1, -1, 0])
        assembler = StripeAssembler(None, rows_dimension_, None, None)

        print(assembler.rows_dimension_fills)
        assert assembler.rows_dimension_fills == (
            "012345",
            None,
            "6789ab",
            None,
            "cdef01",
        )
Exemple #7
0
    def it_constructs_its_measures_collaborator_object_to_help(
            self, request, cube_, rows_dimension_, measures_):
        StripeMeasures_ = class_mock(
            request,
            "cr.cube.stripe.assembler.StripeMeasures",
            return_value=measures_,
        )
        assembler = StripeAssembler(cube_,
                                    rows_dimension_,
                                    ca_as_0th=False,
                                    slice_idx=7)

        measures = assembler._measures

        StripeMeasures_.assert_called_once_with(cube_, rows_dimension_, False,
                                                7)
        assert measures is measures_
Exemple #8
0
    def it_assembles_various_measures(
        self,
        request,
        _measures_prop_,
        measures_,
        _assemble_vector_,
        measure_prop_name,
        MeasureCls,
    ):
        _measures_prop_.return_value = measures_
        setattr(
            measures_,
            measure_prop_name,
            instance_mock(request, MeasureCls, blocks=("A", "B")),
        )
        _assemble_vector_.return_value = np.array([1, 2, 3, 4, 5])
        assembler = StripeAssembler(None, None, None, None)

        value = getattr(assembler, measure_prop_name)

        _assemble_vector_.assert_called_once_with(assembler, ("A", "B"))
        assert value.tolist() == [1, 2, 3, 4, 5]
Exemple #9
0
    def it_provides_values_for_univariate_cat_means(self):
        cube = Cube(CR.CAT_MEANS_HS)
        assembler = StripeAssembler(cube, cube.dimensions[0], False, 0)

        assert assembler.inserted_row_idxs == (3, 4)
        assert assembler.means == pytest.approx(
            [19.85556, 13.85417, 52.7894736842, np.nan, np.nan], nan_ok=True)
        assert assembler.row_count == 5
        assert assembler.row_labels.tolist() == [
            "Yes",
            "No",
            "I'm not sur",
            "Seen the Ad",
            "Not Seen th",
        ]
        assert assembler.rows_dimension_fills == (None, None, None, None, None)
        assert assembler.scale_mean is None
        assert assembler.scale_median is None
        assert assembler.scale_stddev is None
        assert assembler.scale_stderr is None
        assert assembler.table_base_range == pytest.approx([661, 661])
        # for a cube with numeric measure like mean, table margin and table base are the
        # same because they are both calculated on the (unweighted) valid-counts.
        assert assembler.table_margin_range == pytest.approx([661, 661])
Exemple #10
0
    def it_provides_values_for_univariate_cat(self):
        cube = Cube(CR.UNIVARIATE_CATEGORICAL)
        assembler = StripeAssembler(cube, cube.dimensions[0], False, 0)

        assert assembler.inserted_row_idxs == ()
        assert assembler.row_count == 2
        assert assembler.row_labels.tolist() == ["C", "E"]
        assert assembler.rows_dimension_fills == (None, None)
        assert assembler.scale_mean == pytest.approx(1.666667)
        assert assembler.scale_median == pytest.approx(1.0)
        assert assembler.scale_stddev == pytest.approx(0.9428090)
        assert assembler.scale_stderr == pytest.approx(0.2434322)
        assert assembler.table_base_range == pytest.approx([15, 15])
        assert assembler.table_margin_range == pytest.approx([15, 15])
        assert assembler.table_proportion_stddevs == pytest.approx(
            [0.4714045, 0.4714045])
        assert assembler.table_proportion_stderrs == pytest.approx(
            [0.1217161, 0.1217161])
        assert assembler.table_proportions == pytest.approx(
            [0.6666667, 0.3333333])
        assert assembler.unweighted_bases.tolist() == [15, 15]
        assert assembler.unweighted_counts.tolist() == [10, 5]
        assert assembler.weighted_bases.tolist() == [15, 15]
        assert assembler.weighted_counts.tolist() == [10, 5]
Exemple #11
0
    def it_knows_the_row_count(self, _row_order_prop_):
        _row_order_prop_.return_value = np.array([1, 2, 3, 4, 5])
        assembler = StripeAssembler(None, None, None, None)

        assert assembler.row_count == 5
Exemple #12
0
    def it_knows_the_inserted_row_idxs(self, _row_order_prop_):
        _row_order_prop_.return_value = np.array([-1, 0, 3, -2, 4, 1])
        assembler = StripeAssembler(None, None, None, None)

        assert assembler.inserted_row_idxs == (0, 3)
Exemple #13
0
    def it_provides_values_for_univariate_mr(self):
        cube = Cube(CR.MR_WGTD)
        assembler = StripeAssembler(cube, cube.dimensions[0], False, 0)

        assert assembler.inserted_row_idxs == ()
        assert assembler.row_count == 9
        assert assembler.row_labels.tolist() == [
            "liver",
            "thalmus",
            "heart",
            "tripe",
            "kidney",
            "lungs",
            "other",
            "Don't know",
            "None of the",
        ]
        assert assembler.rows_dimension_fills == (
            None,
            None,
            None,
            None,
            None,
            None,
            None,
            None,
            None,
        )
        assert assembler.scale_mean is None
        assert assembler.scale_median is None
        assert assembler.scale_stddev is None
        assert assembler.scale_stderr is None
        assert assembler.table_base_range == pytest.approx([43504, 43504])
        assert assembler.table_margin_range == pytest.approx(
            [43009.56, 43009.56])
        assert assembler.table_proportion_stddevs == pytest.approx([
            0.4986677,
            0.4036694,
            0.4486895,
            0.3885031,
            0.4918164,
            0.3241297,
            0.1177077,
            0.1979895,
            0.2601507,
        ])
        assert assembler.table_proportion_stderrs == pytest.approx([
            0.0024045217,
            0.0019464504,
            0.0021635322,
            0.0018733202,
            0.0023714856,
            0.0015629185,
            0.0005675739,
            0.0009546841,
            0.0012544185,
        ])
        assert assembler.table_proportions == pytest.approx([
            0.4635233,
            0.2049559,
            0.2793696,
            0.1852536,
            0.5900924,
            0.1192902,
            0.01405258,
            0.04087024,
            0.07300864,
        ])
        assert assembler.unweighted_bases.tolist() == [
            43504,
            43504,
            43504,
            43504,
            43504,
            43504,
            43504,
            43504,
            43504,
        ]
        assert assembler.unweighted_counts.tolist() == [
            21545,
            9256,
            13412,
            8562,
            27380,
            5041,
            676,
            1281,
            3112,
        ]
        assert assembler.weighted_bases == pytest.approx([
            43009.56,
            43009.56,
            43009.56,
            43009.56,
            43009.56,
            43009.56,
            43009.56,
            43009.56,
            43009.56,
        ])
        assert assembler.weighted_counts == pytest.approx([
            19935.93,
            8815.065,
            12015.56,
            7967.675,
            25379.62,
            5130.620,
            604.3953,
            1757.811,
            3140.070,
        ])
Exemple #14
0
    def it_provides_values_for_univariate_mr_means(self):
        cube = Cube(CR.MR_MEAN_FILT_WGTD)
        assembler = StripeAssembler(cube, cube.dimensions[0], False, 0)

        assert assembler.means == pytest.approx(
            [3.72405146, 2.57842929, 2.21859327, 1.86533494])