def test_error_when_not_modal_location(): """ Test that error is raised if modal_locations is not a ModalLocation. """ dl = daily_location("2016-01-01", spatial_unit=make_spatial_unit("lon-lat")) with pytest.raises(ValueError): Displacement("2016-01-01", "2016-01-02", modal_locations=dl, statistic="avg")
def test_min_displacement_zero(get_dataframe): """ When time period for diplacement and home location are the same min displacement should be zero for all subscribers """ df = get_dataframe(Displacement("2016-01-01", "2016-01-07", statistic="min")) assert df.statistic.sum() == 0
def test_returns_expected_values(stat, sub_a_expected, sub_b_expected, get_dataframe): """ Test that we get expected return values for the various statistics """ sub_a_id, sub_b_id = "j6QYNbMJgAwlVORP", "NG1km5NzBg5JD8nj" df = get_dataframe(Displacement("2016-01-01", "2016-01-07", statistic=stat)).set_index("subscriber") assert df.loc[sub_a_id].statistic, pytest.approx(sub_a_expected) assert df.loc[sub_b_id].statistic, pytest.approx(sub_b_expected)
def test_returns_expected_result_for_unit_m(get_dataframe): """ Test that we get expected results when unit='m'. """ sub_a_id, sub_b_id = "j6QYNbMJgAwlVORP", "NG1km5NzBg5JD8nj" df = get_dataframe( Displacement("2016-01-01", "2016-01-07", statistic="max", unit="m") ).set_index("subscriber") assert df.loc[sub_a_id].statistic == pytest.approx(500809.349) assert df.loc[sub_b_id].statistic == pytest.approx(387024.628)
def test_invalid_statistic_raises_error(): """ Test that passing an invalid statistic raises an error. """ dl = daily_location("2016-01-01", spatial_unit=make_spatial_unit("lon-lat")) with pytest.raises(ValueError): Displacement("2016-01-01", "2016-01-07", reference_location=dl, statistic="BAD_STATISTIC")
def test_error_when_modal_location_not_lon_lat(): """ Test that error is raised if home location passed to class is not using lon-lat spatial unit """ ml = ModalLocation( *[daily_location(d) for d in list_of_dates("2016-01-01", "2016-01-02")] ) with pytest.raises(ValueError): Displacement("2016-01-01", "2016-01-02", modal_locations=ml, statistic="avg")
def test_error_when_home_location_not_latlong(): """ Test that error is raised if home location passed to class is not using level lat-lon """ hl = HomeLocation( *[daily_location(d) for d in list_of_dates("2016-01-01", "2016-01-02")] ) with pytest.raises(ValueError): Displacement("2016-01-01", "2016-01-02", home_locations=hl, statistic="avg")
def test_error_when_reference_location_is_not_a_base_location(): """ Test that error is raised if reference_locations is not an object of type `BaseLocation`. """ not_an_instance_of_base_location = Mock() with pytest.raises(ValueError): Displacement( "2016-01-01", "2016-01-02", reference_location=not_an_instance_of_base_location, statistic="avg", )
def test_error_when_reference_location_not_lon_lat(): """ Test that error is raised if home location passed to class is not using lon-lat spatial unit """ rl = daily_location("2016-01-01") with pytest.raises(ValueError): Displacement("2016-01-01", "2016-01-02", reference_location=rl, statistic="avg")
def test_min_displacement_zero(get_dataframe): """ When time period for diplacement and home location are the same min displacement should be zero for all subscribers """ rl = daily_location("2016-01-01", spatial_unit=make_spatial_unit("lon-lat")) df = get_dataframe( Displacement("2016-01-01", "2016-01-07", reference_location=rl, statistic="min")) assert df.value.sum() == 0
def _unsampled_query_obj(self): """ Return the underlying flowmachine displacement object. Returns ------- Query """ return Displacement( start=self.start, stop=self.stop, statistic=self.statistic, reference_location=self.reference_location._flowmachine_query_obj, subscriber_subset=self.subscriber_subset, )
def test_subscriber_with_home_loc_but_no_calls_is_ommitted(get_dataframe): """ Test that a subscriber who has no activity between start and stop but has a home location is omitted by default. """ p2 = ("2016-01-01 12:01:00", "2016-01-01 15:20:00") subscriber = "OdM7np8LYEp1mkvP" rl = daily_location("2016-01-01", spatial_unit=make_spatial_unit("lon-lat")) d = Displacement(*p2, reference_location=rl) df = get_dataframe(d).set_index("subscriber") assert subscriber not in df
def test_returns_expected_values(stat, unit, sub_a_expected, sub_b_expected, get_dataframe): """ Test that we get expected return values for the various statistics """ sub_a_id, sub_b_id = "j6QYNbMJgAwlVORP", "NG1km5NzBg5JD8nj" rl = daily_location("2016-01-01", spatial_unit=make_spatial_unit("lon-lat")) df = get_dataframe( Displacement("2016-01-01", "2016-01-07", reference_location=rl, statistic=stat, unit=unit)).set_index("subscriber") assert df.loc[sub_a_id].value == pytest.approx(sub_a_expected) assert df.loc[sub_b_id].value == pytest.approx(sub_b_expected)
def test_pass_reference_location(get_dataframe): """ Test that we can pass a home location object to the class """ rl = daily_location("2016-01-01", spatial_unit=make_spatial_unit("lon-lat")) df = get_dataframe( Displacement("2016-01-01", "2016-01-07", reference_location=rl, statistic="avg")) df = df.set_index("subscriber") val = df.loc["j6QYNbMJgAwlVORP"].value assert val == pytest.approx(370.301009034846)
def test_get_all_users_in_modal_location(get_dataframe): """ This tests that diplacement values are returned for all subscribers in the home location object. """ p1 = ("2016-01-02 10:00:00", "2016-01-02 12:00:00") p2 = ("2016-01-01 12:01:00", "2016-01-01 15:20:00") hl = ModalLocation(*[ daily_location(d, level="lat-lon", hours=(12, 13)) for d in list_of_dates(p1[0], p1[1]) ]) d = Displacement(p2[0], p2[1], modal_locations=hl) hl_subscribers = set(get_dataframe(hl).subscriber) d_subscribers = set(get_dataframe(d).subscriber) assert not (hl_subscribers - d_subscribers)
def test_pass_modal_location(get_dataframe): """ Test that we can pass a home location object to the class """ hl = ModalLocation(*[ daily_location(d, level="lat-lon") for d in list_of_dates("2016-01-01", "2016-01-06") ]) df = get_dataframe( Displacement("2016-01-01", "2016-01-07", modal_locations=hl, statistic="avg")) df = df.set_index("subscriber") val = df.loc["j6QYNbMJgAwlVORP"].statistic assert val == pytest.approx(169.926194)
def test_subscriber_with_home_loc_but_no_calls_is_nan(get_dataframe): """ Test that a subscriber who has no activity between start and stop but has a home location returns a nan value """ p2 = ("2016-01-01 12:01:00", "2016-01-01 15:20:00") subscriber = "OdM7np8LYEp1mkvP" rl = daily_location("2016-01-01", spatial_unit=make_spatial_unit("lon-lat")) d = Displacement(p2[0], p2[1], reference_location=rl, return_subscribers_not_seen=True) df = get_dataframe(d).set_index("subscriber") assert isnan(df.loc[subscriber].value)
def test_subscriber_with_home_loc_but_no_calls_is_nan(get_dataframe): """ Test that a subscriber who has no activity between start and stop but has a home location returns a nan value """ p1 = ("2016-01-02 10:00:00", "2016-01-02 12:00:00") p2 = ("2016-01-01 12:01:00", "2016-01-01 15:20:00") subscriber = "OdM7np8LYEp1mkvP" hl = ModalLocation(*[ daily_location(d, level="lat-lon", hours=(12, 13)) for d in list_of_dates(p1[0], p1[1]) ]) d = Displacement(p2[0], p2[1], modal_locations=hl) df = get_dataframe(d).set_index("subscriber") assert isnan(df.loc[subscriber].statistic)
def test_pass_modal_location(get_dataframe): """ Test that we can pass a home location object to the class """ ml = ModalLocation( *[ daily_location(d, spatial_unit=make_spatial_unit("lon-lat")) for d in list_of_dates("2016-01-01", "2016-01-06") ] ) df = get_dataframe( Displacement("2016-01-01", "2016-01-07", modal_locations=ml, statistic="avg") ) df = df.set_index("subscriber") val = df.loc["j6QYNbMJgAwlVORP"].statistic assert val == pytest.approx(176.903620)
def test_get_all_users_in_reference_location(get_dataframe): """ This tests that displacement values are returned for all subscribers in the home location object. """ p2 = ("2016-01-01 12:01:00", "2016-01-01 15:20:00") rl = daily_location("2016-01-01", spatial_unit=make_spatial_unit("lon-lat")) d = Displacement(p2[0], p2[1], reference_location=rl, return_subscribers_not_seen=True) ml_subscribers = set(get_dataframe(rl).subscriber) d_subscribers = set(get_dataframe(d).subscriber) assert not (ml_subscribers - d_subscribers)
def test_invalid_statistic_raises_error(): """ Test that passing an invalid statistic raises an error. """ with pytest.raises(ValueError): Displacement("2016-01-01", "2016-01-07", statistic="BAD_STATISTIC")