Skip to content

Commit

Permalink
Move veda_cgs calculation to model property
Browse files Browse the repository at this point in the history
  • Loading branch information
olejandro committed Nov 15, 2024
1 parent 1a20d34 commit 66aece7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
15 changes: 15 additions & 0 deletions xl2times/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from collections.abc import Iterable
from dataclasses import dataclass, field
from enum import Enum
from functools import cached_property
from importlib import resources
from itertools import chain

Expand Down Expand Up @@ -205,6 +206,20 @@ def model_years(self) -> set[int]:
"""
return self.past_years | set(self.time_periods["m"].values)

@cached_property
def veda_cgs(self) -> dict[tuple[str, str, str], str]:
"""veda_cgs is a dictionary mapping commodities to their Veda commodity groups."""
cols = ["region", "process", "commodity", "csets"]
# Exclude auxillary flows
index = self.topology["io"].isin({"IN", "OUT"})
veda_cgs = self.topology[cols + ["io"]][index].copy()
veda_cgs.drop_duplicates(subset=cols, keep="last", inplace=True)
veda_cgs["veda_cg"] = veda_cgs["csets"] + veda_cgs["io"].str[:1]
veda_cgs = veda_cgs.set_index(["region", "process", "commodity"])[
"veda_cg"
].to_dict()
return veda_cgs


class Config:
"""Encapsulates all configuration options for a run of the tool, including the
Expand Down
16 changes: 3 additions & 13 deletions xl2times/transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -1255,18 +1255,6 @@ def apply_fixups(
tables: list[EmbeddedXlTable],
model: TimesModel,
) -> list[EmbeddedXlTable]:

# Generate Veda CG info
cols = ["region", "process", "commodity", "csets"]
# Exclude auxillary flows
index = model.topology["io"].isin({"IN", "OUT"})
veda_cgs = model.topology[cols + ["io"]][index].copy()
veda_cgs.drop_duplicates(subset=cols, keep="last", inplace=True)
veda_cgs["veda_cg"] = veda_cgs["csets"] + veda_cgs["io"].str[:1]
veda_cgs = veda_cgs.set_index(["region", "process", "commodity"])[
"veda_cg"
].to_dict()

def apply_fixups_table(table: EmbeddedXlTable):
tag = Tag.fi_t
if not table.tag == tag:
Expand All @@ -1286,7 +1274,9 @@ def apply_fixups_table(table: EmbeddedXlTable):
if any(df["other_indexes"] == "veda_cg"):
i = df["other_indexes"] == "veda_cg"
df.loc[i, "other_indexes"] = df[i].apply(
lambda x: veda_cgs.get((x["region"], x["process"], x["commodity"])),
lambda x: model.veda_cgs.get(
(x["region"], x["process"], x["commodity"])
),
axis=1,
)

Expand Down

0 comments on commit 66aece7

Please sign in to comment.