def test_merge_asserts_timevar_links(self): """ Test that merge_levels checks input df_links timevar """ df_links_wrong_timevar = self.df_links.copy() df_links_wrong_timevar.index.rename(["wrong_timevar", self.groupvar_l], inplace=True) with self.assertRaises(AssertionError) as _: Crosslevel.merge_levels(self.df_h, self.df_l, df_links_wrong_timevar)
def test_compute_product(self): """ Test compute_product() """ df = Crosslevel.merge_levels(self.df_h, self.df_l, self.df_links) first_col = df.columns[0] second_col = df.columns[1] product_1 = Crosslevel.compute_product(df, first_col, second_col) product_2 = df[first_col] * df[second_col] pd.testing.assert_series_equal(product_1, product_2)
def test_merge_levels_cols(self): """ Test that all cols from low res df are included in merged df""" df = Crosslevel.merge_levels(self.df_h, self.df_l, self.df_links) cols_merged = sorted(list(df.columns)) cols_h = sorted(list(self.df_h.columns)) cols_l = sorted(list(self.df_l.columns)) cols_wanted = cols_h + cols_l + [self.groupvar_l] cols_wanted = sorted(cols_wanted) self.assertEqual(cols_merged, cols_wanted)
def test_setup_state_df(self): this_cl = Crosslevel() this_cl._setup_state(self.df_h, self.df_l, self.df_links, self.timevar, self.groupvar_h, self.groupvar_l) self.assertIsInstance(this_cl.df, pd.DataFrame) df_merged = Crosslevel.merge_levels(self.df_h, self.df_l, self.df_links) pd.testing.assert_frame_equal(this_cl.df, df_merged) with self.assertRaises(AssertionError) as _: pd.testing.assert_frame_equal(this_cl.df, self.df_h)
def test_compute_colaresi(self): """ Test compute_colaresi() """ df = Crosslevel.merge_levels(self.df_h, self.df_l, self.df_links) col_h = self.df_h.columns[0] col_l = self.df_l.columns[0] sum_h_by_l = df.groupby([self.timevar, self.groupvar_l])[col_h].transform('sum') p_h = df[col_h] p_l = df[col_l] joint_1 = p_l * (p_h / sum_h_by_l) joint_2 = Crosslevel.compute_colaresi(df, col_h, col_l, self.timevar, self.groupvar_l) pd.testing.assert_series_equal(joint_1, joint_2)
def test_worker_product(self): """ Test simple product worker chain """ # Compute colaresian product by calling static worker directly df_merged = Crosslevel.merge_levels(self.df_h, self.df_l, self.df_links) job = self.job_product result_1 = Crosslevel.compute_product(df=df_merged, col_a=job['col_h'], col_b=job['col_l']) # Compute by calling the worker chain this_cl = Crosslevel() this_cl._setup_state(self.df_h, self.df_l, self.df_links, self.timevar, self.groupvar_h, self.groupvar_l) result_2 = this_cl.worker(self.local_settings, job) pd.testing.assert_series_equal(result_1, result_2)
def test_merge_levels_len(self): """ Test that merged df has same number of rows as high res df""" df = Crosslevel.merge_levels(self.df_h, self.df_l, self.df_links) self.assertEqual(len(df), len(self.df_h))