def test_column_names_meaningful_locations_aggregate(
        exemplar_spatial_unit_param, get_column_names_from_run):
    """ Test that column_names property matches head(0) for aggregated meaningful locations"""
    if not exemplar_spatial_unit_param.is_polygon:
        pytest.xfail(
            f"The spatial unit {exemplar_spatial_unit_param} is not supported as an aggregation unit for MeaningfulLocations."
        )
    mfl_agg = MeaningfulLocationsAggregate(
        meaningful_locations=MeaningfulLocations(
            clusters=HartiganCluster(
                calldays=CallDays(subscriber_locations=SubscriberLocations(
                    start="2016-01-01",
                    stop="2016-01-02",
                    spatial_unit=make_spatial_unit("versioned-site"),
                )),
                radius=1,
            ),
            scores=EventScore(
                start="2016-01-01",
                stop="2016-01-02",
                spatial_unit=make_spatial_unit("versioned-site"),
            ),
            labels=labels,
            label="evening",
        ),
        spatial_unit=exemplar_spatial_unit_param,
    )

    assert get_column_names_from_run(mfl_agg) == mfl_agg.column_names
def test_meaningful_locations_aggregation_results(exemplar_spatial_unit_param,
                                                  get_dataframe):
    """
    Test that aggregating MeaningfulLocations returns expected results and redacts values below 15.
    """
    if not exemplar_spatial_unit_param.is_polygon:
        pytest.xfail(
            f"The spatial unit {exemplar_spatial_unit_param} is not supported as an aggregation unit for MeaningfulLocations."
        )
    mfl = MeaningfulLocations(
        clusters=HartiganCluster(
            calldays=CallDays(subscriber_locations=SubscriberLocations(
                start="2016-01-01",
                stop="2016-01-02",
                spatial_unit=make_spatial_unit("versioned-site"),
            )),
            radius=1,
        ),
        scores=EventScore(
            start="2016-01-01",
            stop="2016-01-02",
            spatial_unit=make_spatial_unit("versioned-site"),
        ),
        labels=labels,
        label="evening",
    )
    mfl_agg = MeaningfulLocationsAggregate(
        meaningful_locations=mfl, spatial_unit=exemplar_spatial_unit_param)
    mfl_df = get_dataframe(mfl)
    mfl_agg_df = get_dataframe(mfl_agg)
    # Aggregate should not include any counts below 15
    assert all(mfl_agg_df.total > 15)
    # Sum of aggregate should be less than the number of unique subscribers
    assert mfl_agg_df.total.sum() < mfl_df.subscriber.nunique()
def test_column_names_meaningful_locations_aggregate(
        exemplar_level_param, get_column_names_from_run):
    """ Test that column_names property matches head(0) for aggregated meaningful locations"""
    if exemplar_level_param[
            "level"] not in MeaningfulLocationsAggregate.allowed_levels:
        pytest.xfail(
            f'The level "{exemplar_level_param["level"]}" is not supported as an aggregation unit for MeaningfulLocations.'
        )
    mfl_agg = MeaningfulLocationsAggregate(
        meaningful_locations=MeaningfulLocations(
            clusters=HartiganCluster(
                calldays=CallDays(subscriber_locations=subscriber_locations(
                    start="2016-01-01",
                    stop="2016-01-02",
                    level="versioned-site")),
                radius=1,
            ),
            scores=EventScore(start="2016-01-01",
                              stop="2016-01-02",
                              level="versioned-site"),
            labels=labels,
            label="evening",
        ),
        **exemplar_level_param,
    )

    assert get_column_names_from_run(mfl_agg) == mfl_agg.column_names
def test_meaningful_locations_aggregation_results(exemplar_level_param,
                                                  get_dataframe):
    """
    Test that aggregating MeaningfulLocations returns expected results and redacts values below 15.
    """
    if exemplar_level_param[
            "level"] not in MeaningfulLocationsAggregate.allowed_levels:
        pytest.xfail(
            f'The level "{exemplar_level_param["level"]}" is not supported as an aggregation unit for MeaningfulLocations.'
        )
    mfl = MeaningfulLocations(
        clusters=HartiganCluster(
            calldays=CallDays(subscriber_locations=subscriber_locations(
                start="2016-01-01", stop="2016-01-02",
                level="versioned-site")),
            radius=1,
        ),
        scores=EventScore(start="2016-01-01",
                          stop="2016-01-02",
                          level="versioned-site"),
        labels=labels,
        label="evening",
    )
    mfl_agg = MeaningfulLocationsAggregate(meaningful_locations=mfl,
                                           **exemplar_level_param)
    mfl_df = get_dataframe(mfl)
    mfl_agg_df = get_dataframe(mfl_agg)
    # Aggregate should not include any counts below 15
    assert all(mfl_agg_df.total > 15)
    # Sum of aggregate should be less than the number of unique subscribers
    assert mfl_agg_df.total.sum() < mfl_df.subscriber.nunique()
def test_meaningful_locations_aggregate_disallowed_level_raises():
    """ Test that a bad level raises a BadLevelError"""

    with pytest.raises(BadLevelError):
        mfl_agg = MeaningfulLocationsAggregate(
            meaningful_locations=MeaningfulLocations(
                clusters=HartiganCluster(
                    calldays=CallDays(
                        subscriber_locations=subscriber_locations(
                            start="2016-01-01",
                            stop="2016-01-02",
                            level="versioned-site",
                        )),
                    radius=1,
                ),
                scores=EventScore(start="2016-01-01",
                                  stop="2016-01-02",
                                  level="versioned-site"),
                labels=labels,
                label="evening",
            ),
            level="NOT_A_LEVEL",
        )
def test_meaningful_locations_aggregate_disallowed_spatial_unit_raises():
    """ Test that a bad spatial unit raises an InvalidSpatialUnitError"""

    with pytest.raises(InvalidSpatialUnitError):
        mfl_agg = MeaningfulLocationsAggregate(
            meaningful_locations=MeaningfulLocations(
                clusters=HartiganCluster(
                    calldays=CallDays(subscriber_locations=SubscriberLocations(
                        start="2016-01-01",
                        stop="2016-01-02",
                        spatial_unit=make_spatial_unit("versioned-site"),
                    )),
                    radius=1,
                ),
                scores=EventScore(
                    start="2016-01-01",
                    stop="2016-01-02",
                    spatial_unit=make_spatial_unit("versioned-site"),
                ),
                labels=labels,
                label="evening",
            ),
            spatial_unit=make_spatial_unit("lon-lat"),
        )