def test_unique_subscriber_counts_column_names(exemplar_level_param): """ Test that column_names property of UniqueSubscriberCounts matches head(0) """ usc = UniqueSubscriberCounts("2016-01-01", "2016-01-04", **exemplar_level_param) assert usc.head(0).columns.tolist() == usc.column_names
def test_correct_counts(get_dataframe): """ UniqueLocationCounts returns correct counts. """ usc = UniqueSubscriberCounts("2016-01-01", "2016-01-02", level="cell", hours=(5, 17)) df = get_dataframe(usc) dful = get_dataframe( subscriber_locations("2016-01-01", "2016-01-02", level="cell", hours=(5, 17))) assert [ df["unique_subscriber_counts"][0], df["unique_subscriber_counts"][1], df["unique_subscriber_counts"][2], ] == [ len(dful[dful["location_id"] == df["location_id"][0]] ["subscriber"].unique()), len(dful[dful["location_id"] == df["location_id"][1]] ["subscriber"].unique()), len(dful[dful["location_id"] == df["location_id"][2]] ["subscriber"].unique()), ]
def test_redacted_unique_visitor_counts(get_dataframe): """ Values test for redacted unique visitor counts. """ activity = RedactedUniqueVisitorCounts( unique_visitor_counts=UniqueVisitorCounts( ActiveAtReferenceLocationCounts( ActiveAtReferenceLocation( subscriber_locations=UniqueLocations( SubscriberLocations( "2016-01-01", "2016-01-02", spatial_unit=make_spatial_unit("admin", level=3), )), reference_locations=daily_location("2016-01-03"), )), UniqueSubscriberCounts( "2016-01-01", "2016-01-02", spatial_unit=make_spatial_unit("admin", level=3), ), )) df = get_dataframe(activity).set_index("pcod") assert len(df) == 2 assert df.loc["524 3 08 44"].value == 170
def test_correct_counts(get_dataframe): """ UniqueLocationCounts returns correct counts. """ usc = UniqueSubscriberCounts( "2016-01-01", "2016-01-02", spatial_unit=make_spatial_unit("cell"), hours=(5, 17), ) df = get_dataframe(usc) dful = get_dataframe( SubscriberLocations( "2016-01-01", "2016-01-02", spatial_unit=make_spatial_unit("cell"), hours=(5, 17), )) assert df.value[:3].tolist() == [ len(dful[dful["location_id"] == df["location_id"][0]] ["subscriber"].unique()), len(dful[dful["location_id"] == df["location_id"][1]] ["subscriber"].unique()), len(dful[dful["location_id"] == df["location_id"][2]] ["subscriber"].unique()), ]
def _flowmachine_query_obj(self): """ Return the underlying flowmachine UniqueSubscriberCounts object. Returns ------- Query """ return UniqueSubscriberCounts( start=self.start_date, stop=self.end_date, level=self.aggregation_unit )
def test_all_above_threshold(get_dataframe): """ Test that all values in the redacted query are above the redaction threshold. """ us = UniqueSubscriberCounts("2016-01-01", "2016-01-02", table=["events.calls"]) rus_df = get_dataframe( RedactedUniqueSubscriberCounts(unique_subscriber_counts=us)) us_df = get_dataframe(us) assert all(rus_df.value > 15) assert set(us_df.location_id).issuperset(set(rus_df.location_id)) assert set(us_df.location_id) != set(rus_df.location_id)
def _flowmachine_query_obj(self): """ Return the underlying flowmachine UniqueSubscriberCounts object. Returns ------- Query """ return RedactedUniqueSubscriberCounts( unique_subscriber_counts=UniqueSubscriberCounts( start=self.start_date, stop=self.end_date, spatial_unit=get_spatial_unit_obj(self.aggregation_unit), ) )
def _flowmachine_query_obj(self): """ Return the underlying flowmachine UniqueSubscriberCounts object. Returns ------- Query """ return RedactedUniqueSubscriberCounts( unique_subscriber_counts=UniqueSubscriberCounts( start=self.start_date, stop=self.end_date, spatial_unit=self.aggregation_unit, table=self.event_types, subscriber_subset=self.subscriber_subset, hours=self.hours, ))
def test_unique_visitor_counts_column_names(get_column_names_from_run): assert (get_column_names_from_run( UniqueVisitorCounts( ActiveAtReferenceLocationCounts( ActiveAtReferenceLocation( subscriber_locations=UniqueLocations( SubscriberLocations( "2016-01-01", "2016-01-02", spatial_unit=make_spatial_unit("admin", level=3), )), reference_locations=daily_location("2016-01-03"), )), UniqueSubscriberCounts( "2016-01-01", "2016-01-02", spatial_unit=make_spatial_unit("admin", level=3), ), )) == ["pcod", "value"])
def test_all_above_threshold(get_dataframe): """ TotalLocationEvents() can get activity on a daily level but only above threshold. """ te = RedactedTotalEvents(total_events=TotalLocationEvents( "2016-01-01", "2016-01-02", spatial_unit=make_spatial_unit("cell"), interval="day", table=["events.calls"], )) us = get_dataframe( RedactedUniqueSubscriberCounts( unique_subscriber_counts=UniqueSubscriberCounts( "2016-01-01", "2016-01-02", table=["events.calls"]))) te_df = get_dataframe(te) assert all(te_df.value > 15) assert set(us.location_id) == set(te_df.location_id)
def test_returns_errors(): """ Test level exists """ with pytest.raises(BadLevelError): UniqueSubscriberCounts("2016-01-01", "2016-01-02", level="BAD_LEVEL")
def test_returns_errors(self): """ Test level exists """ with self.assertRaises(BadLevelError): UniqueSubscriberCounts("2016-01-01", "2016-01-02", level="foo")
def setUp(self): self.usc = UniqueSubscriberCounts("2016-01-01", "2016-01-02", level="cell", hours=(5, 17))
class test_unique_subscriber_counts(TestCase): """ Tests associated with the unique_location_counts class """ def setUp(self): self.usc = UniqueSubscriberCounts("2016-01-01", "2016-01-02", level="cell", hours=(5, 17)) def test_returns_df(self): """ UniqueLocationCounts() object can return a dataframe """ self.assertIs(type(self.usc.get_dataframe()), pd.DataFrame) def test_returns_errors(self): """ Test level exists """ with self.assertRaises(BadLevelError): UniqueSubscriberCounts("2016-01-01", "2016-01-02", level="foo") def test_handles_levels(self): """ Location classes can be called with any level and return a dataframe with the right columns. """ expected_cols = [c + ["dl_count"] for c in subscriber_plus_levels] sweep_levels_assert_columns( UniqueSubscriberCounts, { "start": "2016-01-01", "stop": "2016-01-02" }, expected_cols=[ i + ["unique_subscriber_counts"] for i in levels_only ], ) def test_correct_counts(self): """ UniqueLocationCounts returns correct counts. """ df = self.usc.get_dataframe() dful = subscriber_locations("2016-01-01", "2016-01-02", level="cell", hours=(5, 17)).get_dataframe() self.assertEqual( [ df["unique_subscriber_counts"][0], df["unique_subscriber_counts"][1], df["unique_subscriber_counts"][2], ], [ len(dful[dful["location_id"] == df["location_id"][0]] ["subscriber"].unique()), len(dful[dful["location_id"] == df["location_id"][1]] ["subscriber"].unique()), len(dful[dful["location_id"] == df["location_id"][2]] ["subscriber"].unique()), ], )