def setUp(self): """ Method for instantiating test environment. """ self.n_sites = 35 self.c = DistanceMatrix(level="versioned-site") self.df = self.c.get_dataframe()
class DistanceMatrixTestCase(TestCase): """ Tests for the DistanceMatrix() class. """ def setUp(self): """ Method for instantiating test environment. """ self.n_sites = 35 self.c = DistanceMatrix(level="versioned-site") self.df = self.c.get_dataframe() def test_some_results(self): """ DistanceMatrix() returns a dataframe that contains hand-picked results. """ set_df = self.df.set_index("site_id_from") self.assertAlmostEqual( round(set_df.loc["8wPojr"]["distance"].values[0]), 789, 2 ) self.assertAlmostEqual( round(set_df.loc["8wPojr"]["distance"].values[2]), 769, 2 ) self.assertAlmostEqual( round(set_df.loc["8wPojr"]["distance"].values[4]), 758, 2 ) def test_result_has_correct_length(self): """ DistanceMatrix() has the correct length. """ self.assertEqual(len(self.df), self.n_sites ** 2)
def __init__( self, start: str, stop: str, reference_location: BaseLocation, statistic: str = "avg", unit: str = "km", hours: Union[str, Tuple[int, int]] = "all", table: Union[str, List[str]] = "all", subscriber_identifier: str = "msisdn", ignore_nulls: bool = True, return_subscribers_not_seen: bool = False, subscriber_subset: Optional[Query] = None, ): self.return_subscribers_not_seen = return_subscribers_not_seen self.start = start self.stop = stop self.spatial_unit = reference_location.spatial_unit subscriber_locations = SubscriberLocations( self.start, self.stop, spatial_unit=self.spatial_unit, hours=hours, table=table, subscriber_identifier=subscriber_identifier, ignore_nulls=ignore_nulls, subscriber_subset=subscriber_subset, ) self.statistic = statistic.lower() if self.statistic not in valid_stats: raise ValueError( "{} is not a valid statistic. Use one of {}".format( self.statistic, valid_stats)) if not isinstance(reference_location, BaseLocation): raise ValueError( "Argument 'reference_location' should be an instance of BaseLocation class. " f"Got: {type(reference_location)}") else: self.reference_location = reference_location self.joined = reference_location.join( other=subscriber_locations, on_left=["subscriber"], left_append="_from", right_append="_to", ).join( DistanceMatrix(spatial_unit=self.spatial_unit), on_left=[ f"{col}_{direction}" for direction in ("from", "to") for col in self.spatial_unit.location_id_columns ], right_append="_dist", how="left outer", ) self.unit = unit super().__init__()
def __init__( self, *, subscriber_locations: SubscriberLocations, reference_location: Union[BaseLocation, Tuple[float, float]] = (0, 0), statistic: str = "avg", time_bucket: str = "day", ): subscriber_locations.spatial_unit.verify_criterion("has_geography") subscriber_locations.spatial_unit.verify_criterion("has_lon_lat_columns") self.spatial_unit = subscriber_locations.spatial_unit if time_bucket.lower() in valid_time_buckets: self.aggregate_by = time_bucket.lower() else: raise ValueError( f"'{time_bucket}' is not a valid value for time_bucket. Use one of {valid_time_buckets}" ) if statistic.lower() not in valid_stats: raise ValueError( f"'{statistic}' is not a valid statistic. Use one of {valid_stats}" ) self.statistic = statistic.lower() self.start = standardise_date(subscriber_locations.start) self.stop = standardise_date(subscriber_locations.stop) if isinstance(reference_location, tuple): self.reference_location = reference_location self.joined = subscriber_locations elif isinstance(reference_location, BaseLocation): if reference_location.spatial_unit != subscriber_locations.spatial_unit: raise ValueError( "reference_location must have the same spatial unit as subscriber_locations." ) self.reference_location = reference_location self.joined = reference_location.join( other=subscriber_locations, on_left=["subscriber"], left_append="_from", right_append="_to", ).join( DistanceMatrix(spatial_unit=self.spatial_unit), on_left=[ f"{col}_{direction}" for direction in ("from", "to") for col in self.spatial_unit.location_id_columns ], right_append="_dist", how="left outer", ) else: raise ValueError( "Argument 'reference_location' should be an instance of BaseLocation class or a tuple of two floats. " f"Got: {type(reference_location).__name__}" ) super().__init__()
def test_some_results(get_dataframe): """ DistanceMatrix() returns a dataframe that contains hand-picked results. """ c = DistanceMatrix(level="versioned-site") df = get_dataframe(c) set_df = df.set_index("site_id_from") assert round(set_df.loc["8wPojr"]["distance"].values[0]) == 789 assert round(set_df.loc["8wPojr"]["distance"].values[2]) == 769 assert round(set_df.loc["8wPojr"]["distance"].values[4]) == 758
def test_some_results(get_dataframe): """ DistanceMatrix() returns a dataframe that contains hand-picked results. """ c = DistanceMatrix(spatial_unit=make_spatial_unit("versioned-site")) df = get_dataframe(c) set_df = df.set_index( ["site_id_from", "version_from", "site_id_to", "version_to"]) assert set_df.loc[("8wPojr", 1, "GN2k0G", 0)]["value"] == pytest.approx(789.23239740488) assert set_df.loc[("8wPojr", 0, "GN2k0G", 0)]["value"] == pytest.approx(769.20155628077) assert set_df.loc[("8wPojr", 1, "DbWg4K", 0)]["value"] == pytest.approx(757.97771793683)
def test_not_storeable(): with pytest.raises(UnstorableQueryError): DistanceMatrix( spatial_unit=make_spatial_unit("versioned-site")).store()
def test_result_has_correct_length(spatial_unit_type, length, get_length): """ DistanceMatrix has the correct length. """ c = DistanceMatrix(spatial_unit=make_spatial_unit(spatial_unit_type)) assert get_length(c) == length
def test_result_has_correct_length(get_length): """ DistanceMatrix() has the correct length. """ c = DistanceMatrix(level="versioned-site") assert get_length(c) == 35 ** 2