def test_join_to_grid(get_dataframe, get_length): """ Test that we can join to a grid square """ ul = subscriber_locations("2016-01-05", "2016-01-07", level="cell") df = get_dataframe(JoinToLocation(ul, level="grid", size=50)) assert len(df) == get_length(ul)
def test_join_to_grid(self): """ Test that we can join to a grid square """ df = JoinToLocation(self.ul, level="grid", size=50).get_dataframe() self.assertEqual(len(df), len(self.ul))
def test_join_with_versioned_cells(get_dataframe, get_length): """ Test that flowmachine.JoinToLocation can fetch the cell version. """ ul = SubscriberLocations( "2016-01-05", "2016-01-07", spatial_unit=make_spatial_unit("cell") ) df = get_dataframe( JoinToLocation(ul, spatial_unit=make_spatial_unit("versioned-cell")) ) # As our database is complete we should not drop any rows assert len(df) == get_length(ul) # These should all be version zero, these are the towers before the changeover date, or those that # have not moved. should_be_version_zero = df[ (df.time <= move_date) | (~df.location_id.isin(moving_sites)) ] # These should all be one, they are the ones after the change over time that have moved. should_be_version_one = df[ (df.time > move_date) & (df.location_id.isin(moving_sites)) ] assert (should_be_version_zero.version == 0).all() assert (should_be_version_one.version == 1).all()
def test_join_to_location_column_names(exemplar_level_param): """ Test that JoinToLocation's column_names property is accurate.""" if "cell" == exemplar_level_param["level"]: pytest.skip("Cell level not valid for JoinToLocation" ) # cell level not valid for JoinToLocation table = subscriber_locations("2016-01-05", "2016-01-07", level="cell") joined = JoinToLocation(table, **exemplar_level_param) assert joined.head(0).columns.tolist() == joined.column_names
def test_join_to_admin(get_dataframe, get_length): """ Test that flowmachine.JoinToLocation can join to a admin region. """ ul = subscriber_locations("2016-01-05", "2016-01-07", level="cell") df = get_dataframe(JoinToLocation(ul, level="admin3")) assert len(df) == get_length(ul) expected_cols = sorted(["subscriber", "time", "location_id", "pcod"]) assert sorted(df.columns) == expected_cols
def test_join_to_location_column_names(exemplar_spatial_unit_param): """ Test that JoinToLocation's column_names property is accurate.""" if not exemplar_spatial_unit_param.has_geography: pytest.skip("JoinToLocation does not accept CellSpatialUnit objects") table = SubscriberLocations("2016-01-05", "2016-01-07", spatial_unit=make_spatial_unit("cell")) joined = JoinToLocation(table, spatial_unit=exemplar_spatial_unit_param) assert joined.head(0).columns.tolist() == joined.column_names
def test_join_to_admin(self): """ Test that flowmachine.JoinToLocation can join to a admin region. """ df = JoinToLocation(self.ul, level="admin3").get_dataframe() self.assertEqual(len(df), len(self.ul)) expected_cols = sorted(["subscriber", "time", "location_id", "name"]) self.assertEqual(sorted(df.columns), expected_cols)
def test_join_to_location_raises_value_error(): """ Test that JoinToLocation raises an InvalidSpatialUnitError if spatial_unit does not have geography information. """ with pytest.raises(InvalidSpatialUnitError): table = SubscriberLocations("2016-01-05", "2016-01-07", spatial_unit=make_spatial_unit("cell")) joined = JoinToLocation(table, spatial_unit=make_spatial_unit("cell"))
def test_join_to_location_no_cache_if_joinee_no_cache(): with pytest.raises(NotImplementedError): table = SubscriberLocations("2016-01-05", "2016-01-07", spatial_unit=make_spatial_unit("admin", level=3)) joined = JoinToLocation(table, spatial_unit=make_spatial_unit("admin", level=3)) joined.fully_qualified_table_name
def test_join_to_grid(get_dataframe, get_length): """ Test that we can join to a grid square """ ul = SubscriberLocations("2016-01-05", "2016-01-07", spatial_unit=make_spatial_unit("cell")) df = get_dataframe( JoinToLocation(ul, spatial_unit=make_spatial_unit("grid", size=50))) assert len(df) == get_length(ul)
def test_join_with_polygon(get_dataframe, get_length): """ Test that flowmachine.JoinToLocation can get the (arbitrary) polygon of each cell. """ ul = subscriber_locations("2016-01-05", "2016-01-07", level="cell") j = JoinToLocation( ul, level="polygon", column_name="admin3pcod", polygon_table="geography.admin3", geom_col="geom", ) df = get_dataframe(j) expected_cols = sorted(["admin3pcod", "location_id", "subscriber", "time"]) assert sorted(df.columns) == expected_cols assert len(df) == get_length(ul)
def test_join_with_versioned_cells(self): """ Test that flowmachine.JoinToLocation can fetch the cell version. """ df = JoinToLocation(self.ul, level="versioned-cell").get_dataframe() # As our database is complete we should not drop any rows self.assertEqual(len(df), len(self.ul)) # These should all be version zero, these are the towers before the changeover date, or those that # have not moved. should_be_version_zero = df[(df.time <= self.move_date) | (~df.location_id.isin(self.moving_sites))] # These should all be one, they are the ones after the change over time that have moved. should_be_version_one = df[(df.time > self.move_date) & (df.location_id.isin(self.moving_sites))] self.assertTrue((should_be_version_zero.version == 0).all()) self.assertTrue((should_be_version_one.version == 1).all())
def test_join_with_lat_lon(get_dataframe): """ Test that flowmachine.JoinToLocation can get the lat-lon values of the cell """ ul = subscriber_locations("2016-01-05", "2016-01-07", level="cell") df = get_dataframe(JoinToLocation(ul, level="lat-lon")) expected_cols = sorted(["subscriber", "time", "location_id", "lat", "lon"]) assert sorted(df.columns) == expected_cols # Pick out one cell that moves location and assert that the # lat-lons are right focal_cell = "dJb0Wd" lat1, long1 = (27.648837800000003, 83.09284486) lat2, long2 = (27.661443318109132, 83.25769074752517) post_move = df[(df.time > move_date) & (df["location_id"] == focal_cell)] pre_move = df[(df.time < move_date) & (df["location_id"] == focal_cell)] # And check them all one-by-one np.isclose(pre_move.lat, lat1).all() np.isclose(pre_move.lon, long1).all() np.isclose(post_move.lat, lat2).all() np.isclose(post_move.lon, long2).all()
def test_join_with_polygon(self): """ Test that flowmachine.JoinToLocation can get the (arbitrary) polygon of each cell. """ try: make_fake_table(self.con) j = JoinToLocation( self.ul, level="polygon", column_name="naame", polygon_table="test_geog.ad3", geom_col="geo", ) df = j.get_dataframe() finally: drop_fake_table(self.con) self.assertIs(type(df), DataFrame) expected_cols = sorted(["subscriber", "time", "location_id", "naame"]) self.assertEqual(sorted(df.columns), expected_cols) self.assertEqual(len(df), len(self.ul))
def test_join_with_lon_lat(get_dataframe): """ Test that flowmachine.JoinToLocation can get the lon-lat values of the cell """ ul = SubscriberLocations( "2016-01-05", "2016-01-07", spatial_unit=make_spatial_unit("cell") ) df = get_dataframe(JoinToLocation(ul, spatial_unit=make_spatial_unit("lon-lat"))) expected_cols = sorted(["subscriber", "time", "location_id", "lon", "lat"]) assert sorted(df.columns) == expected_cols # Pick out one cell that moves location and assert that the # lon-lats are right focal_cell = "dJb0Wd" lon1, lat1 = (83.09284486, 27.648837800000003) lon2, lat2 = (83.25769074752517, 27.661443318109132) post_move = df[(df.time > move_date) & (df["location_id"] == focal_cell)] pre_move = df[(df.time < move_date) & (df["location_id"] == focal_cell)] # And check them all one-by-one np.isclose(pre_move.lon, lon1).all() np.isclose(pre_move.lat, lat1).all() np.isclose(post_move.lon, lon2).all() np.isclose(post_move.lat, lat2).all()
def test_join_with_lat_lon(self): """ Test that flowmachine.JoinToLocation can get the lat-lon values of the cell """ df = JoinToLocation(self.ul, level="lat-lon").get_dataframe() self.assertIs(type(df), DataFrame) expected_cols = sorted( ["subscriber", "time", "location_id", "lat", "lon"]) self.assertEqual(sorted(df.columns), expected_cols) # Pick out one cell that moves location and assert that the # lat-lons are right focal_cell = "dJb0Wd" lat1, long1 = (27.648837800000003, 83.09284486) lat2, long2 = (27.661443318109132, 83.25769074752517) post_move = df[(df.time > self.move_date) & (df["location_id"] == focal_cell)] pre_move = df[(df.time < self.move_date) & (df["location_id"] == focal_cell)] # And check them all one-by-one np.isclose(pre_move.lat, lat1).all() np.isclose(pre_move.lon, long1).all() np.isclose(post_move.lat, lat2).all() np.isclose(post_move.lon, long2).all()