Skip to content

Commit

Permalink
first draft extension conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
konstantinstadler committed Aug 13, 2024
1 parent 4b13d37 commit 92f709a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 13 deletions.
41 changes: 30 additions & 11 deletions pymrio/core/mriosystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -2040,19 +2040,38 @@ def convert(self, df_map, extension_name,
raise ValueError(
f"Unit in extension does not match the unit in mapping for row {row}"
)

new_extension = Extension(name=extension_name)

if unit_column_new:
ignore_columns.append(unit_column_new)


# for df in self.get_DataFrame:
# CONT:
#
# 1) run with one and check output
# 2) check unit aggregation if unit_column_new is not given
# - unit aggregate with str join of unique entries
# 3) make dict and build new extension
# # run convert of all dataframe and build new extension
#

for df_name, df in zip(self.get_DataFrame(data=False, with_unit=False),
self.get_DataFrame(data=True, with_unit=False)):
setattr(new_extension, df_name, ioutil.convert(
df_orig=df,
df_map=df_map,
agg_func=agg_func,
drop_not_bridged_index=drop_not_bridged_index,
ignore_columns=ignore_columns))

if unit_column_new:
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.loc[:, bridge_columns].drop_duplicates().set_index(bridge_columns).index
)
unique_new_index.names = [col.split("__")[0] for col in bridge_columns]

unit.unit = df_map.set_index(bridge_columns).loc[unique_new_index].loc[:, unit_column_new]
new_extension.unit = unit
else:
new_extension.unit = None

return new_extension


class IOSystem(BaseSystem):
"""Class containing a whole EE MRIO System
Expand Down
1 change: 0 additions & 1 deletion pymrio/tools/ioutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -1136,7 +1136,6 @@ def convert(df_orig,
stacked_columns = []
orig_column_index = df_orig.columns
for col in df_map.columns:
# TODO: other names for unit should be allowed
if col in (["factor"] + ignore_columns):
continue
if col not in df_orig.index.names:
Expand Down
15 changes: 14 additions & 1 deletion tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,20 @@ def test_extension_convert(fix_testmrio):

pass

def test_extension_convert_test_unit(fix_testmrio):

def test_extension_convert_test_conversion(fix_testmrio):
df_map1 = pd.DataFrame(
columns=["stressor", "compartment", "total__stressor", "factor", "unit_orig", "unit_new"],
data=[
["emis.*", "air|water", "total_regex", 1000, "kg", "t"],
],
)

x = fix_testmrio.testmrio.emissions.convert(df_map1, extension_name="emissions_new")
# CONT: write test for conversion, including va, different units, etc.


def test_extension_convert_test_unit_fail(fix_testmrio):

df_fail1 = pd.DataFrame(
columns=["stressor", "compartment", "total__stressor", "factor", "unit_orig", "unit_new"],
Expand Down

0 comments on commit 92f709a

Please sign in to comment.