Skip to content

Commit

Permalink
lint and full test
Browse files Browse the repository at this point in the history
  • Loading branch information
konstantinstadler committed Oct 27, 2024
1 parent ca15a26 commit 1543458
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 25 deletions.
7 changes: 3 additions & 4 deletions doc/source/notebooks/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,11 +389,11 @@
# %% [markdown]
# The same principles as for individual tables can be used for converting full pymrio type Extensions (aka satellite accounts).
# In difference to the single tables, pymrio Extensions consist of several pandas DataFrames which can be converted in one go.
# Almost the same bridge table structure as for single tables can be used. The main additional information needed is in regard to
# Almost the same bridge table structure as for single tables can be used. The main additional information needed is in regard to
# units. Since pymrio Extensions include a unit dataframe, information about the unit names need to be included.

# %% [markdown]
# Extensions can be converted either one at a time, but the main power of the method lies in collecting stressor data across different extensions
# Extensions can be converted either one at a time, but the main power of the method lies in collecting stressor data across different extensions
# and converting them in one go.

# %% [markdown]
Expand All @@ -417,7 +417,7 @@
mrio.emissions.unit

# %% [markdown]
# We now setup a bridge table for converting/characterizing these emission data
# We now setup a bridge table for converting/characterizing these emission data
# to several other accounts.

# %%
Expand Down Expand Up @@ -471,4 +471,3 @@


# CONT: test/explain characterization across different extensions

6 changes: 2 additions & 4 deletions pymrio/core/mriosystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -2079,17 +2079,15 @@ def convert(
unit = pd.DataFrame(columns=["unit"], index=new_extension.get_rows())
bridge_columns = [col for col in df_map.columns if "__" in col]
unique_new_index = (
df_map
.drop_duplicates(subset=bridge_columns)
df_map.drop_duplicates(subset=bridge_columns)
.loc[:, bridge_columns]
.set_index(bridge_columns)
.index
)
unique_new_index.names = [col.split("__")[0] for col in bridge_columns]

unit.unit = (
df_map
.drop_duplicates(subset=bridge_columns)
df_map.drop_duplicates(subset=bridge_columns)
.set_index(bridge_columns)
.loc[unique_new_index]
.loc[:, unit_column_new]
Expand Down
3 changes: 1 addition & 2 deletions pymrio/tools/ioutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -1099,7 +1099,7 @@ def convert(

bridge_columns = [col for col in df_map.columns if "__" in col]

# groupby breaks with NaNs or None, fix it here
# groupby breaks with NaNs or None, fix it here
df_map.loc[:, bridge_columns] = df_map.loc[:, bridge_columns].fillna("")

unique_new_index = (
Expand All @@ -1115,7 +1115,6 @@ def convert(
if isinstance(df_orig, pd.Series):
df_orig = pd.DataFrame(df_orig)


# some consistency checks of arguments and restructuring if everything is ok
if len(bridge_columns) == 0:
raise ValueError("No columns with '__' in the mapping DataFrame")
Expand Down
38 changes: 29 additions & 9 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,8 @@ def test_extension_convert(fix_testmrio):
)

pdt.assert_series_equal(
tt_pre.emissions.D_cba.loc["emission_type1", "air"] * 2 + tt_pre.emissions.D_cba.loc["emission_type2", "water"] * 10,
tt_pre.emissions.D_cba.loc["emission_type1", "air"] * 2
+ tt_pre.emissions.D_cba.loc["emission_type2", "water"] * 10,
tt_pre.pre_calc.D_cba.loc["char_emissions"],
check_names=False,
)
Expand Down Expand Up @@ -635,7 +636,8 @@ def test_extension_convert(fix_testmrio):
)

pdt.assert_series_equal(
tt_post.emissions.D_cba.loc["emission_type1", "air"] * 2 + tt_post.emissions.D_cba.loc["emission_type2", "water"] * 10,
tt_post.emissions.D_cba.loc["emission_type1", "air"] * 2
+ tt_post.emissions.D_cba.loc["emission_type2", "water"] * 10,
tt_post.post_calc.D_cba.loc["char_emissions"],
check_names=False,
)
Expand Down Expand Up @@ -750,7 +752,11 @@ def test_extension_convert_function(fix_testmrio):

df_map_add_across_wrong_name = df_map_add_across.copy()

df_map_add_across_wrong_name.loc[:, "extension"] = df_map_add_across_wrong_name.extension.str.replace("emissions_new_pre_calc", "foo")
df_map_add_across_wrong_name.loc[
:, "extension"
] = df_map_add_across_wrong_name.extension.str.replace(
"emissions_new_pre_calc", "foo"
)

ext_across_correct = pymrio.extension_convert(
tt_pre.emissions,
Expand All @@ -766,9 +772,15 @@ def test_extension_convert_function(fix_testmrio):
new_extension_name="add_across",
)

expected_df_correct_F = tt_pre.emissions.F.loc["emission_type2", :].iloc[0, :] + ext_double.F.loc[("water_emissions", "water")] * 1e-3
expected_df_correct_F = (
tt_pre.emissions.F.loc["emission_type2", :].iloc[0, :]
+ ext_double.F.loc[("water_emissions", "water")] * 1e-3
)
expected_df_wrong_F = tt_pre.emissions.F.loc["emission_type2", :].iloc[0, :]
expected_df_correct_F_Y = tt_pre.emissions.F_Y.loc["emission_type2", :].iloc[0, :] + ext_double.F_Y.loc[("water_emissions", "water")] * 1e-3
expected_df_correct_F_Y = (
tt_pre.emissions.F_Y.loc["emission_type2", :].iloc[0, :]
+ ext_double.F_Y.loc[("water_emissions", "water")] * 1e-3
)
expected_df_wrong_F_Y = tt_pre.emissions.F_Y.loc["emission_type2", :].iloc[0, :]

pdt.assert_series_equal(
Expand Down Expand Up @@ -817,9 +829,18 @@ def test_extension_convert_function(fix_testmrio):
new_extension_name="add_across",
)

expected_df_D_cba = tt_post.emissions.D_cba.loc["emission_type2", :].iloc[0, :] + tt_post.add_across.D_cba.loc[("water_emissions", "water")] * 1e-3
expected_df_S = tt_post.emissions.S.loc["emission_type2", :].iloc[0, :] + tt_post.add_across.S.loc[("water_emissions", "water")] * 1e-3
expected_df_M = tt_post.emissions.M.loc["emission_type2", :].iloc[0, :] + tt_post.add_across.M.loc[("water_emissions", "water")] * 1e-3
expected_df_D_cba = (
tt_post.emissions.D_cba.loc["emission_type2", :].iloc[0, :]
+ tt_post.add_across.D_cba.loc[("water_emissions", "water")] * 1e-3
)
expected_df_S = (
tt_post.emissions.S.loc["emission_type2", :].iloc[0, :]
+ tt_post.add_across.S.loc[("water_emissions", "water")] * 1e-3
)
expected_df_M = (
tt_post.emissions.M.loc["emission_type2", :].iloc[0, :]
+ tt_post.add_across.M.loc[("water_emissions", "water")] * 1e-3
)

pdt.assert_series_equal(
ext_test_all.D_cba.iloc[0],
Expand All @@ -838,7 +859,6 @@ def test_extension_convert_function(fix_testmrio):
)



def test_extension_convert_test_unit_fail(fix_testmrio):
df_fail1 = pd.DataFrame(
columns=[
Expand Down
12 changes: 6 additions & 6 deletions tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,16 +534,18 @@ def test_convert_rename_spread_index():
99,
)


# TEST WITH EMPTY INDEX


rename_bridge_missing_string = pd.DataFrame(
columns=["stressor", "flow__stressor", "class__stressor", "class2__stressor"],
data=[
["em1", "emission1", "to_air", "to_air (unspecified)"],
["em2", "emission2", "to_air", "to_air (specified)"],
["em3", "emission3", "to_water",],
[
"em3",
"emission3",
"to_water",
],
],
)

Expand All @@ -565,7 +567,6 @@ def test_convert_rename_spread_index():
],
)


renamed_missing_string = convert(to_char, rename_bridge_missing_string)
renamed_missing_nan = convert(to_char, rename_bridge_missing_nan)
renamed_missing_none = convert(to_char, rename_bridge_missing_none)
Expand All @@ -582,8 +583,6 @@ def test_convert_rename_spread_index():
rename_bridge_indexed.index.names = ["flow", "class", "class2"]
pdt.assert_index_equal(renamed_simple.index, rename_bridge_indexed.index)



# TEST WITH RENAME IN MUTLIINDEX

to_char_multi = pd.DataFrame(
Expand Down Expand Up @@ -908,6 +907,7 @@ def test_convert_characterize():

# TODO: test case for multindex characterization on one of teh inner levels - does not work in the GLAM example


def test_convert_wrong_inputs():
to_char = pd.DataFrame(
data=5,
Expand Down

0 comments on commit 1543458

Please sign in to comment.