Skip to content

Commit

Permalink
feat(aggregation-apis): rename aggregation cols (#2250)
Browse files Browse the repository at this point in the history
  • Loading branch information
mabw-rte authored Dec 13, 2024
1 parent 88e3486 commit d51e88c
Show file tree
Hide file tree
Showing 8 changed files with 17,372 additions and 17,333 deletions.
46 changes: 37 additions & 9 deletions antarest/study/business/aggregator_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,20 @@
"""Index in path parts starting from the Monte Carlo year to determine the Monte Carlo year."""
AREA_OR_LINK_INDEX__IND, AREA_OR_LINK_INDEX__ALL = 2, 1
"""Indexes in path parts starting from the output root `economy//mc-(ind/all)` to determine the area/link name."""
PRODUCTION_COLUMN_NAME = "production"
PRODUCTION_COLUMN_NAME_GENERAL = "production"
PRODUCTION_COLUMN_NAME_ST = "levels"
PRODUCTION_COLUMN_REGEX = "mwh"
RENAME_MAPPING = {
"NP Cost - Euro": "NP Cost",
"NODU": "NODU",
"P-injection - MW": "P.injection",
"P-withdrawal - MW": "P.withdrawal",
"CashFlow - Euro": "CashFlow",
}
"""
Dictionary to rename columns in a table.
keys are the regexes to fetch for columns to rename, values are the new column names.
"""
CLUSTER_ID_COMPONENT = 0
ACTUAL_COLUMN_COMPONENT = 1
DUMMY_COMPONENT = 2
Expand Down Expand Up @@ -104,8 +116,9 @@ def _columns_ordering(df_cols: t.List[str], column_name: str, is_details: bool,
return new_column_order


def _infer_production_column(cols: t.Sequence[str]) -> t.Optional[str]:
return next((c for c in cols if PRODUCTION_COLUMN_REGEX in c.lower().strip()), None)
def _infer_column_from_regex(cols: t.Sequence[str], col_regex: str) -> t.Optional[str]:
stripped_lower_col_regex = col_regex.lower().strip()
return next((c for c in cols if stripped_lower_col_regex in c.lower().strip()), None)


def _infer_time_id(df: pd.DataFrame, is_details: bool) -> t.List[int]:
Expand Down Expand Up @@ -309,16 +322,31 @@ def _process_df(self, file_path: Path, is_details: bool) -> pd.DataFrame:
new_obj[CLUSTER_ID_COL] += [cluster_id for _ in range(df_len)]
new_obj[TIME_ID_COL] += list(range(1, df_len + 1))

# rename the columns
renamed_cols = []
# check if there is a production column to rename it to `PRODUCTION_COLUMN_NAME`
prod_col = _infer_production_column(actual_cols)
if prod_col is not None:
new_obj[PRODUCTION_COLUMN_NAME] = new_obj.pop(prod_col)
prod_col = _infer_column_from_regex(actual_cols, PRODUCTION_COLUMN_REGEX)
if prod_col:
prod_col_name = (
PRODUCTION_COLUMN_NAME_ST
if (
self.query_file == MCIndAreasQueryFile.DETAILS_ST_STORAGE
or self.query_file == MCAllAreasQueryFile.DETAILS_ST_STORAGE
)
else PRODUCTION_COLUMN_NAME_GENERAL
)
new_obj[prod_col_name] = new_obj.pop(prod_col)
actual_cols.remove(prod_col)
renamed_cols.append(prod_col_name)
for col_regex, new_col_name in RENAME_MAPPING.items():
col_name = _infer_column_from_regex(actual_cols, col_regex)
if col_name:
new_obj[new_col_name] = new_obj.pop(col_name)
actual_cols.remove(col_name)
renamed_cols.append(new_col_name)

# reorganize the data frame
# first the production column if it exists
add_prod = [PRODUCTION_COLUMN_NAME] if prod_col is not None else []
columns_order = [CLUSTER_ID_COL, TIME_ID_COL] + add_prod + list(actual_cols)
columns_order = [CLUSTER_ID_COL, TIME_ID_COL] + renamed_cols + list(actual_cols)
df = pd.DataFrame(new_obj).reindex(columns=columns_order).sort_values(by=[TIME_ID_COL, CLUSTER_ID_COL])
df.index = pd.Index(list(range(1, len(df) + 1)))

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
area_1 area sts annual
VARIABLES BEGIN END
4 1 1

area_1 annual storage_1 storage_1 storage_1 storage_1
P-injection - MW P-withdrawal - MW Levels - MWh CashFlow - Euro
EXP EXP EXP EXP
Annual 0 0 0 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
area,cluster,timeId,levels,P.injection,P.withdrawal,CashFlow
de,storage_1,1,0.000000,0.000000,0.000000,0.000000
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
area cluster timeId production NODU NP Cost - Euro
de 01_solar 1 315000.0 167.0 0.0
de 02_wind_on 1 275000.0 147.0 0.0
de 03_wind_off 1 235000.0 127.0 0.0
de 04_res 1 195000.0 107.0 0.0
de 05_nuclear 1 155000.0 87.0 0.0
de 06_coal 1 115000.0 67.0 0.0
de 07_gas 1 75000.0 47.0 0.0
de 08_non-res 1 35000.0 27.0 0.0
de 09_hydro_pump 1 2800.0 7.0 0.0
area cluster timeId production NP Cost NODU
de 01_solar 1 315000.0 0.0 167.0
de 02_wind_on 1 275000.0 0.0 147.0
de 03_wind_off 1 235000.0 0.0 127.0
de 04_res 1 195000.0 0.0 107.0
de 05_nuclear 1 155000.0 0.0 87.0
de 06_coal 1 115000.0 0.0 67.0
de 07_gas 1 75000.0 0.0 47.0
de 08_non-res 1 35000.0 0.0 27.0
de 09_hydro_pump 1 2800.0 0.0 7.0
Loading

0 comments on commit d51e88c

Please sign in to comment.