def test_louvain_takes_reduction_parameters():
    """
    Louvain().run() takes reduction parameters.
    """
    test_graph = ContactBalance("2016-01-01", "2016-01-07")
    l = Louvain(test_graph.to_networkx(directed_graph=False))
    result = l.run(
        resolution=0.7, min_members=10, weight_property="events"
    ).get_dataframe()
    set_df = result.set_index("subscriber")
    assert (
        set_df.loc["nMvpK39bowVXYN9G"]["community"]
        != set_df.loc["qPAbaED3vDYkjZ0n"]["community"]
    )

    assert (
        set_df.loc["qRlQo7ly3Zg9GVN2"]["community"]
        != set_df.loc["YeqxBmVgL1EnONv8"]["community"]
    )
    assert (
        set_df.loc["E0LZAa7AyNd34Djq"]["community"]
        == set_df.loc["8dpPLR15XwR7jQyN"]["community"]
    )
    assert (
        set_df.loc["APj9roe8jKOwEDZl"]["community"]
        == set_df.loc["4dqenN2oQZExwEK2"]["community"]
    )
def test_returns_correct_results():
    """
    Louvain().run() doesn't put everybody in the same community.
    """
    test_graph = ContactBalance("2016-01-01", "2016-01-07")
    l = Louvain(test_graph.to_networkx(directed_graph=False))
    set_df = l.run().get_dataframe().set_index("subscriber")
    assert len(set(set_df.community)) != 1
Esempio n. 3
0
def test_contact_reference_location_stats_false_statistic_raises():
    """ Test ValueError is raised for non-compliant statistics parameter. """
    cb = ContactBalance("2016-01-01", "2016-01-03")
    ml = ModalLocation(*[
        daily_location(
            d,
            spatial_unit=make_spatial_unit("versioned-cell"),
            subscriber_subset=cb.counterparts_subset(include_subscribers=True),
        ) for d in list_of_dates("2016-01-01", "2016-01-03")
    ])
    with pytest.raises(ValueError):
        query = ContactReferenceLocationStats(cb, ml, statistic="error")
Esempio n. 4
0
def test_contact_reference_location_false_level_raises():
    """ Test ValueError is raised for contact_location with non-compliant level. """
    cb = ContactBalance("2016-01-01", "2016-01-03")
    ml = ModalLocation(*[
        daily_location(
            d,
            level="admin3",
            subscriber_subset=cb.counterparts_subset(include_subscribers=True),
        ) for d in list_of_dates("2016-01-01", "2016-01-03")
    ])
    with pytest.raises(ValueError):
        query = ContactReferenceLocationStats(cb, ml)
Esempio n. 5
0
def test_contact_reference_location_no_spatial_unit_raises():
    """ Test ValueError is raised for contact_location without spatial_unit attribute. """
    cb = ContactBalance("2016-01-01", "2016-01-03")
    # by encapsulating ModalLocations in a CustomQuery we remove the spatial_unit
    # attribute from it which should raise an error
    ml = ModalLocation(*[
        daily_location(
            d,
            spatial_unit=make_spatial_unit("versioned-cell"),
            subscriber_subset=cb.counterparts_subset(include_subscribers=True),
        ) for d in list_of_dates("2016-01-01", "2016-01-03")
    ])
    ml = CustomQuery(ml.get_query(), ml.column_names)
    with pytest.raises(ValueError):
        query = ContactReferenceLocationStats(cb, ml)
Esempio n. 6
0
def test_contact_reference_location_bad_spatial_unit_raises():
    """
    Test InvalidSpatialUnitError is raised for contact_location with
    non-compliant spatial unit.
    """
    cb = ContactBalance("2016-01-01", "2016-01-03")
    ml = ModalLocation(*[
        daily_location(
            d,
            spatial_unit=make_spatial_unit("admin", level=3),
            subscriber_subset=cb.counterparts_subset(include_subscribers=True),
        ) for d in list_of_dates("2016-01-01", "2016-01-03")
    ])
    with pytest.raises(InvalidSpatialUnitError):
        query = ContactReferenceLocationStats(cb, ml)
Esempio n. 7
0
def test_contact_reference_location_stats(get_dataframe, statistic, msisdn,
                                          spatial_unit_type, want):
    """ Test a few hand-picked ContactReferenceLocationStats. """
    cb = ContactBalance("2016-01-01", "2016-01-03")
    ml = ModalLocation(*[
        daily_location(
            d,
            spatial_unit=make_spatial_unit(spatial_unit_type),
            subscriber_subset=cb.counterparts_subset(include_subscribers=True),
        ) for d in list_of_dates("2016-01-01", "2016-01-03")
    ])
    cb.store()
    ml.store()
    query = ContactReferenceLocationStats(cb, ml, statistic=statistic)
    df = get_dataframe(query).set_index("subscriber")
    assert df.value[msisdn] == pytest.approx(want)
def test_per_contact_event_stats_errors(kwarg):
    """ Test ValueError is raised for non-compliant kwarg in PerContactEventStats. """

    with pytest.raises(ValueError):
        query = PerContactEventStats(
            ContactBalance("2016-01-02", "2016-01-06"), **{kwarg: "error"}
        )
Esempio n. 9
0
def test_pareto_nepal(get_dataframe):
    """Test flowmachine's method against the nepal code."""
    cb = ContactBalance("2016-01-01", "2016-01-07", exclude_self_calls=False)
    p = ParetoInteractions("2016-01-01", "2016-01-07")
    df = get_dataframe(p)
    df2 = paretos(get_dataframe(cb))
    assert all(
        df.set_index("subscriber").sort_index() == df2.set_index(
            "subscriber").sort_index())
Esempio n. 10
0
def test_contact_reference_location_stats_custom_geometry(get_dataframe):
    """ Test ContactReferenceLocationStats with custom geometry column. """
    cb = ContactBalance("2016-01-01", "2016-01-03")
    ml = ModalLocation(*[
        daily_location(
            d,
            spatial_unit=make_spatial_unit("versioned-cell"),
            subscriber_subset=cb.counterparts_subset(include_subscribers=True),
        ) for d in list_of_dates("2016-01-01", "2016-01-03")
    ])
    cb.store()
    ml.store()
    ml = CustomQuery(
        f"SELECT subscriber, ST_POINT(lon, lat) AS loc FROM ({ml.get_query()}) _",
        ["subscriber", "loc"],
    )
    query = ContactReferenceLocationStats(cb,
                                          ml,
                                          statistic="avg",
                                          geom_column="loc")
    df = get_dataframe(query).set_index("subscriber")
    assert df.value["gwAynWXp4eWvxGP7"] == pytest.approx(298.7215)
Esempio n. 11
0
def test_contact_reference_location_no_subscriber_raises():
    """ Test ValueError is raised for contact_location without subscriber. """
    cb = ContactBalance("2016-01-01", "2016-01-03")
    cl = CustomQuery("SELECT 1 AS foo", ["foo"])
    with pytest.raises(ValueError):
        query = ContactReferenceLocationStats(cb, cl)
def test_per_contact_event_stats(get_dataframe, statistic, msisdn, want):
    """ Test hand-picked PerContactEventStats. """
    query = PerContactEventStats(ContactBalance("2016-01-02", "2016-01-06"), statistic)
    df = get_dataframe(query).set_index("subscriber")
    assert df.value[msisdn] == pytest.approx(want)