def test_init_df_with_extra_col(test_pd_df): tdf = test_pd_df.copy() extra_col = "climate model" extra_value = "scm_model" tdf[extra_col] = extra_value df = IamDataFrame(tdf) assert df.extra_cols == [extra_col] pd.testing.assert_frame_equal(df.timeseries().reset_index(), tdf, check_like=True)
def test_relationship_negative_specific(self, unequal_df, test_downscale_df, match_sign, caplog): # Test that match_sign results in the correct multipliers when negative values # are added to the positive ones. follow = "Emissions|HFC|C5F12" lead = ["Emissions|HFC|C2F6"] equal_df = unequal_df.filter(model="model_a") invert_sign = equal_df.filter(variable=lead) invert_sign.data["value"] = -1 * invert_sign.data["value"] invert_sign.append(equal_df.filter(variable=follow), inplace=True) invert_sign.data["model"] = "negative_model" equal_df.append(invert_sign, inplace=True) equal_df = IamDataFrame(equal_df.data) tcruncher = self.tclass(equal_df) # Invert the sign of the infillee too, as we haven't tested the formula for # negative values test_downscale_df.data["value"] = -1 * test_downscale_df.data["value"] test_downscale_df = self._adjust_time_style_to_match( test_downscale_df, equal_df).filter(year=[2010, 2015]) filler = tcruncher.derive_relationship(variable_follower=follow, variable_leaders=lead, same_sign=match_sign) res = filler(test_downscale_df) # if we have match sign on, this is identical to the above except for -ve. if match_sign: lead_iamdf = test_downscale_df.filter( variable="Emissions|HFC|C2F6") exp = lead_iamdf.timeseries() # The follower values are 1 and 9 (average 5), the leader values # are all -1, hence we expect the input * -5 as output. exp[exp.columns[0]] = exp[exp.columns[0]] * -5 exp[exp.columns[1]] = exp[exp.columns[1]] * -1 exp = exp.reset_index() exp["variable"] = "Emissions|HFC|C5F12" exp["unit"] = "kt C5F12/yr" exp = IamDataFrame(exp) # Test that our constructed value is the same as the result pd.testing.assert_frame_equal(res.timeseries(), exp.timeseries(), check_like=True) # comes back on input timepoints np.testing.assert_array_equal( res.timeseries().columns.values.squeeze(), test_downscale_df.timeseries().columns.values.squeeze(), ) else: # If we combine all signs together, there is no net lead but a net follow, # so we have have a multiplier of infinity. assert all(res.data["value"] == -np.inf)
def test_init_df_with_extra_col(test_pd_df): tdf = test_pd_df.copy() extra_col = "climate model" extra_value = "scm_model" tdf[extra_col] = extra_value df = IamDataFrame(tdf) # check that timeseries data is as expected obs = df.timeseries().reset_index() exp = tdf[obs.columns] # get the columns into the right order pd.testing.assert_frame_equal(obs, exp)
def test_relationship_usage_interpolation(self, test_db, test_downscale_df, interpolate): tcruncher = self.tclass(test_db) filler = tcruncher.derive_relationship("Emissions|HFC|C5F12", ["Emissions|HFC|C2F6"]) test_downscale_df = self._adjust_time_style_to_match( test_downscale_df.filter(year=2015, keep=False), test_db) required_timepoint = test_db.filter( year=2015).data[test_db.time_col].iloc[0] if not interpolate: if isinstance(required_timepoint, pd.Timestamp): required_timepoint = required_timepoint.to_pydatetime() error_msg = re.escape( "Required downscaling timepoint ({}) is not in the data for the " "lead gas (Emissions|HFC|C2F6)".format(required_timepoint)) with pytest.raises(ValueError, match=error_msg): filler(test_downscale_df, interpolate=interpolate) return res = filler(test_downscale_df, interpolate=interpolate) lead_iamdf = test_downscale_df.filter(variable="Emissions|HFC|C2F6", region="World", unit="kt C2F6/yr") exp = lead_iamdf.timeseries() # will have to make this more intelligent for time handling lead_df = lead_iamdf.timeseries() lead_df[required_timepoint] = np.nan lead_df = lead_df.reindex(sorted(lead_df.columns), axis=1) lead_df = lead_df.interpolate(method="index", axis=1) lead_val_2015 = lead_df[required_timepoint] exp = (exp.T * 3.14 / lead_val_2015).T.reset_index() exp["variable"] = "Emissions|HFC|C5F12" exp["unit"] = "kt C5F12/yr" exp = IamDataFrame(exp) pd.testing.assert_frame_equal(res.timeseries(), exp.timeseries(), check_like=True) # comes back on input timepoints np.testing.assert_array_equal( res.timeseries().columns.values.squeeze(), test_downscale_df.timeseries().columns.values.squeeze(), )
def test_relationship_usage(self, test_downscale_df, add_col): units = "new units" tcruncher = self.tclass() test_downscale_df = test_downscale_df.filter(year=[2010, 2015]) if add_col: # what should happen if there's more than one value in the `add_col`? add_col_val = "blah" test_downscale_df[add_col] = add_col_val test_downscale_df = IamDataFrame(test_downscale_df.data) assert test_downscale_df.extra_cols[0] == add_col lead = ["Emissions|HFC|C2F6"] follow = "Emissions|HFC|C5F12" filler = tcruncher.derive_relationship(follow, lead, ratio=2, units=units) res = filler(test_downscale_df) exp = test_downscale_df.filter(variable=lead) exp.data["variable"] = follow exp.data["value"] = exp.data["value"] * 2 exp.data["unit"] = units pd.testing.assert_frame_equal(res.timeseries(), exp.timeseries(), check_like=True) # comes back on input timepoints np.testing.assert_array_equal( res.timeseries().columns.values.squeeze(), test_downscale_df.timeseries().columns.values.squeeze(), ) # Test we can append the results correctly append_df = test_downscale_df.append(res) assert append_df.filter(variable=follow).equals(res) if add_col: assert all(append_df.filter(variable=lead)[add_col] == add_col_val)
def test_init_df_from_timeseries(test_df): df = IamDataFrame(test_df.timeseries()) pd.testing.assert_frame_equal(df.timeseries(), test_df.timeseries())
def test_init_df_with_index(test_pd_df): df = IamDataFrame(test_pd_df.set_index(META_IDX)) pd.testing.assert_frame_equal(df.timeseries().reset_index(), test_pd_df)