def test_joined_median_aggregate(self): """ Test join with median aggregate. """ mfl = MostFrequentLocation("2016-01-01", "2016-01-04", level="admin3") rog = RadiusOfGyration("2016-01-01", "2016-01-04") joined = mfl.join_aggregate(rog, method="median") rawus_avg = (rog.get_dataframe().set_index("subscriber").join( mfl.get_dataframe().set_index("subscriber")).set_index( "name").ix["Rasuwa"].rog.median()) self.assertAlmostEqual( joined.get_dataframe().set_index("name").ix["Rasuwa"].rog, rawus_avg)
def test_query_can_be_subscriber_set_restricted(self): """Test that some queries can be limited to only a subset of subscribers.""" # Create a temporary table in the DB con = Table.connection.engine sql = "DROP TABLE IF EXISTS subscriber_list" con.execute(sql) sql = """CREATE TABLE subscriber_list (subscriber TEXT)""" con.execute(sql) formatted_subscribers = ",".join("('{}')".format(u) for u in self.subscriber_list) sql = """INSERT INTO subscriber_list (subscriber) VALUES {}""".format( formatted_subscribers) con.execute(sql) rog = RadiusOfGyration("2016-01-01", "2016-01-03", subscriber_subset=Table("subscriber_list")) hl = HomeLocation(*[ daily_location(d, subscriber_subset=Table("subscriber_list")) for d in list_of_dates("2016-01-01", "2016-01-03") ]) rog_df = rog.get_dataframe() hl_df = hl.get_dataframe() sql = "DROP TABLE IF EXISTS subscriber_list" con.execute(sql) # Get the set of subscribers present in the dataframe, we need to handle the logic # of msisdn_from/msisdn_to calculated_subscriber_set = set(rog_df.subscriber) self.assertEqual(calculated_subscriber_set, set(self.subscriber_list)) calculated_subscriber_set = set(hl_df.subscriber) self.assertEqual(calculated_subscriber_set, set(self.subscriber_list))
class TestNumericSubsetting(TestCase): def setUp(self): self.rog = RadiusOfGyration("2016-01-01", "2016-01-02") self.low = 150 self.high = 155 self.rog_df = self.rog.get_dataframe().query( "{low} <= rog <= {high}".format(low=self.low, high=self.high)) self.sub = self.rog.numeric_subset(col="rog", low=self.low, high=self.high) def _query_has_values(self, Q, expected_df): """ Test if the values of a dataframes columns are equal to certain values. """ query_df = Q.get_dataframe() assert_array_equal(expected_df.values, query_df.values) def test_can_numsubset_with_low_and_high(self): """ flowmachine.RadiusOfGyration can be subset within a range """ self._query_has_values(self.sub, self.rog_df) def test_can_numsubset_with_inf(self): """ flowmachine.RadiusOfGyration can be subset between -Inf and Inf """ low = -float("Infinity") high = float("Infinity") sub = self.rog.numeric_subset(col="rog", low=low, high=high) df = self.rog.get_dataframe().query("{low} <= rog <= {high}".format( low=low, high=high)) self._query_has_values(sub, df) def test_call_with_str_raises_error(self): """ Numeric subset can't be called with a string in arguments low and high """ with self.assertRaises(TypeError): self.rog.numeric_subset(col="rog", low="foo", high=self.high) with self.assertRaises(TypeError): self.rog.numeric_subset(col="rog", low=self.low, high="bar") def test_can_be_stored(self): """ Test that flowmachine.NumericSubset can be stored. """ self.sub.store().result() self.assertTrue(self.sub.is_stored) # Test that the store is of the right length sub = self.rog.numeric_subset(col="rog", low=self.low, high=self.high) self.assertEqual(len(sub.get_dataframe()), len(self.rog_df)) def tearDown(self): """ Remove stored table from "can_be_stored" test. """ self.sub.invalidate_db_cache()