def test_constructor_overlaps_bounds_dict_raises():
    """
    Should raise an error if an overlap is found in constructor.
    """
    bounds_dict = {
        "evening": [
            {
                "hour_lower_bound": 0,
                "hour_upper_bound": 1,
                "day_of_week_upper_bound": 1,
                "day_of_week_lower_bound": 0.5,
            },
            {
                "hour_lower_bound": 0.00001,
                "hour_upper_bound": 1,
                "day_of_week_upper_bound": -0.5,
                "day_of_week_lower_bound": -1,
            },
        ],
        "day": [
            {
                "hour_lower_bound": -1,
                "hour_upper_bound": 0,
                "day_of_week_upper_bound": 0.5,
                "day_of_week_lower_bound": -0.5,
            }
        ],
    }
    with pytest.raises(ValueError):
        LabelEventScore(
            scores=EventScore(start="2016-01-01", stop="2016-01-05"), labels=bounds_dict
        )
def test_whether_passing_reserved_label_fails():
    """
    Test whether passing the reserved label 'unknown' fails.
    """
    es = EventScore(
        start="2016-01-01",
        stop="2016-01-05",
        spatial_unit=make_spatial_unit("versioned-site"),
    )

    with pytest.raises(ValueError):
        ls = LabelEventScore(
            scores=es,
            labels={
                "unknown": {
                    "type": "Polygon",
                    "coordinates": [
                        [
                            [-1.1, -1.1],
                            [-1.0, 1.1],
                            [1.1, 1.1],
                            [1.1, -1.1],
                            [-1.1, -1.1],
                        ]
                    ],
                }
            },
        )
def test_labelled_event_score_column_names(
    exemplar_spatial_unit_param, get_column_names_from_run
):
    es = EventScore(
        start="2016-01-01", stop="2016-01-05", spatial_unit=exemplar_spatial_unit_param
    )
    labelled = LabelEventScore(scores=es, required="evening")
    assert get_column_names_from_run(labelled) == labelled.column_names
Exemple #4
0
def test_labelled_event_score_column_names(
    exemplar_level_param, get_column_names_from_run
):
    if exemplar_level_param["level"] not in JoinToLocation.allowed_levels:
        pytest.skip(f'{exemplar_level_param["level"]} not valid for this test')
    es = EventScore(start="2016-01-01", stop="2016-01-05", **exemplar_level_param)
    labelled = LabelEventScore(scores=es, required="evening")
    assert get_column_names_from_run(labelled) == labelled.column_names
def test_constructor_raises_value_error(bad_bound):
    """
    Constructor should raise valueerror for bad bounds..
    """
    with pytest.raises(ValueError):
        LabelEventScore(
            scores=EventScore(start="2016-01-01", stop="2016-01-05"),
            labels={"DUMMY_LABEL": [bad_bound]},
        )
Exemple #6
0
def test_locations_are_labelled_correctly(get_dataframe):
    """
    Test whether locations are labelled corrected.
    """
    es = EventScore(start="2016-01-01", stop="2016-01-05", level="versioned-site")

    ls = LabelEventScore(
        scores=es,
        labels={
            "daytime": {
                "type": "Polygon",
                "coordinates": [[[-1.1, -1.1], [-1, 1.1], [1.1, 1.1], [1.1, -1.1]]],
            }
        },
    )
    df = get_dataframe(ls)
    assert list(df["label"].unique()) == ["daytime"]
Exemple #7
0
def test_whether_required_label_relabels(get_dataframe):
    """
    Test whether required label relabel the location of subscribers who did not originally have the required label.
    """
    es = EventScore(start="2016-01-01", stop="2016-01-05", level="versioned-site")

    ls = LabelEventScore(
        scores=es,
        labels={
            "daytime": {
                "type": "Polygon",
                "coordinates": [
                    [[-1.1, -1.1], [-1.0, 1.1], [1.1, 1.1], [1.1, -1.1], [-1.1, -1.1]]
                ],
            }
        },
        required="evening",
    )
    df = get_dataframe(ls)
    assert list(df["label"].unique()) == ["evening"]
def test_non_query_scores_obj_raises():
    """
    Passing a scores object which is not a query type should raise an error.
    """
    with pytest.raises(TypeError):
        LabelEventScore(
            scores="NOT_A_QUERY",
            labels={
                "DUMMY_LABEL": {
                    "type": "Polygon",
                    "coordinates": [
                        [
                            [-1.1, -1.1],
                            [-1.0, 1.1],
                            [1.1, 1.1],
                            [1.1, -1.1],
                            [-1.1, -1.1],
                        ]
                    ],
                }
            },
        )
    """
    bounds_dict = {
        "evening": {
            "type": "MultiPolygon",
            "coordinates": [
                [[[0.000_001, 0.5], [0.000_001, 1], [1, 1], [1, 0.5]]],
                [[[0.000_001, -1], [0.000_001, -0.5], [1, -0.5], [1, -1]]],
            ],
        },
        "day": {
            "type": "Polygon",
            "coordinates": [[[-1, -0.5], [-1, 0.5], [0, 0.5], [0, -0.5]]],
        },
    }
    assert LabelEventScore.verify_bounds_dict_has_no_overlaps(
        LabelEventScore._convert_bounds_to_shapely_polygons(bounds_dict)
    )


def test_overlaps_bounds_dict_raises():
    """
    Should raise an error if an overlap is found.
    """
    bounds_dict = {
        "evening": {
            "type": "MultiPolygon",
            "coordinates": [
                [[[0.000_001, 0.5], [0.000_001, 1], [1, 1], [1, 0.5]]],
                [[[0.000_001, -1], [0.000_001, -0.5], [1, -0.5], [1, -1]]],
            ],
        },