def test_1(self): expected = np.array([[0., 1., 0., 0., 0.], [0.5, 0., 0.5, 0., 0.], [0., 0.5, 0., 0.5, 0.], [0., 0., 0.5, 0., 0.5], [0., 0., 0., 1., 0.]]) assert np.all(expected == lu.comp_mat_gen(self.rank, 1))
def test_5(self): expected = np.array([ [0., 0.25, 0.25, 0.25, 0.25], [0.25, 0., 0.25, 0.25, 0.25], [0.25, 0.25, 0., 0.25, 0.25], [0.25, 0.25, 0.25, 0., 0.25], [0.25, 0.25, 0.25, 0.25, 0.]]) assert np.all(expected == lu.comp_mat_gen(self.rank, 5))
def competition(self): """ Generate averages in competition sets """ # Generate competition variables reac_vars = lu.reaction_spec("full")[0] comp_vars = ["OverallRank"] comp_vars = [reac_vars, comp_vars] comp_vars = [el for sublist in comp_vars for el in sublist] comp_vars_comp = [el + "_comp" for el in comp_vars] comp_add = pd.DataFrame(np.zeros((self.data.shape[0], len(comp_vars_comp))), columns=comp_vars_comp) self.data = self.data.join(comp_add) for year in self.data["year"].unique(): for cvar in comp_vars: mask = (1 - np.isnan(self.data[cvar])).astype(bool) mdata = deepcopy(self.data[mask]) comp_mat = lu.comp_mat_gen(mdata.loc[mdata["year"] == year, "OverallRank"]) mdata.loc[mdata["year"] == year, cvar + "_comp"] = np.dot( comp_mat, mdata.loc[mdata["year"] == year, cvar] ) self.data[mask] = mdata