Skip to content

Commit 464e579

Browse files
authored
Merge pull request #253 from iiasa/fix/material-data-lfs
Convert aluminum_trade.csv from Git LFS pointer
2 parents 84469b0 + 4655ae4 commit 464e579

File tree

8 files changed

+1233
-61
lines changed

8 files changed

+1233
-61
lines changed

doc/whatsnew.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Next release
2323
- Reconfigure use and implementation of technology variants/modules to be more agnostic (:pull:`221`).
2424
- Change cost decay to reach reduction percentage specified on the year 2100 (:pull:`227`).
2525
- Add `cooling` technology variant/module (:pull:`222`).
26-
- Improve and extend :doc:`/material/index` (:pull:`218`).
26+
- Improve and extend :doc:`/material/index` (:pull:`218`, :pull:`253`).
2727

2828
- Release of MESSAGEix-Materials 1.1.0 (:doc:`/material/v1.1.0`).
2929

message_ix_models/data/material/aluminum/aluminum_trade.csv

+1,186-3
Large diffs are not rendered by default.

message_ix_models/model/material/data_aluminum.py

+14-17
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
def read_data_aluminum(
2222
scenario: message_ix.Scenario,
23-
) -> (pd.DataFrame, pd.DataFrame, pd.DataFrame):
23+
) -> tuple[pd.DataFrame, pd.DataFrame, pd.DataFrame]:
2424
"""Read and clean data from :file:`aluminum_techno_economic.xlsx`.
2525
2626
Parameters
@@ -42,10 +42,7 @@ def read_data_aluminum(
4242

4343
fname = "aluminum_techno_economic.xlsx"
4444

45-
if "R12_CHN" in s_info.N:
46-
sheet_n = "data_R12"
47-
else:
48-
sheet_n = "data_R11"
45+
sheet_n = "data_R12" if "R12_CHN" in s_info.N else "data_R11"
4946

5047
# Read the file
5148
data_alu = pd.read_excel(
@@ -564,7 +561,9 @@ def gen_data_alu_trade(scenario: message_ix.Scenario) -> dict[str, pd.DataFrame]
564561
data_trade.loc[(data_trade["Region"] == "Europe"), "Value"] *= 0.7
565562
data_trade.loc[(data_trade["Region"] == "Europe"), "Region"] = "West Europe"
566563

567-
data_trade_eeu = data_trade[data_trade["Region"] == "West Europe"]
564+
data_trade_eeu = data_trade.loc[data_trade["Region"] == "West Europe"].copy(
565+
deep=True
566+
)
568567
data_trade_eeu["Value"] *= 0.3 / 0.7
569568
data_trade_eeu["Region"] = "East Europe"
570569

@@ -592,7 +591,7 @@ def gen_data_alu_trade(scenario: message_ix.Scenario) -> dict[str, pd.DataFrame]
592591
data_trade.loc[(data_trade["Region"] == "Other Asia"), "Value"] *= 0.5
593592
data_trade.loc[(data_trade["Region"] == "Other Asia"), "Region"] = "South Asia"
594593

595-
data_trade_pas = data_trade[data_trade["Region"] == "South Asia"]
594+
data_trade_pas = data_trade[data_trade["Region"] == "South Asia"].copy(deep=True)
596595
data_trade_pas["Region"] = "Other Pacific Asia"
597596

598597
data_trade = pd.concat([data_trade, data_trade_pas])
@@ -602,7 +601,7 @@ def gen_data_alu_trade(scenario: message_ix.Scenario) -> dict[str, pd.DataFrame]
602601
data_trade.loc[(data_trade["Region"] == "Other Producers"), "Value"] *= 0.5
603602
data_trade.loc[(data_trade["Region"] == "Other Producers"), "Region"] = "Africa"
604603

605-
data_trade_fsu = data_trade[data_trade["Region"] == "Africa"]
604+
data_trade_fsu = data_trade[data_trade["Region"] == "Africa"].copy(deep=True)
606605
data_trade_fsu["Region"] = "Former Soviet Union"
607606

608607
data_trade = pd.concat([data_trade, data_trade_fsu])
@@ -614,12 +613,8 @@ def gen_data_alu_trade(scenario: message_ix.Scenario) -> dict[str, pd.DataFrame]
614613

615614
s_info = ScenarioInfo(scenario)
616615

617-
if "R12_CHN" in s_info.N:
618-
region_tag = "R12_"
619-
china_mapping = "R12_CHN"
620-
else:
621-
region_tag = "R11_"
622-
china_mapping = "R11_CPA"
616+
region_tag = "R12_" if "R12_CHN" in s_info.N else "R11_"
617+
china_mapping = "R12_CHN" if "R12_CHN" in s_info.N else "R11_CPA"
623618

624619
region_mapping = {
625620
"China": china_mapping,
@@ -647,7 +642,7 @@ def gen_data_alu_trade(scenario: message_ix.Scenario) -> dict[str, pd.DataFrame]
647642
# For imports this corresponds to: USE|Inputs|Imports
648643

649644
data_import = data_trade[data_trade["Variable"] == "USE|Inputs|Imports"]
650-
data_import_hist = data_import[data_import["year_act"] <= 2015]
645+
data_import_hist = data_import[data_import["year_act"] <= 2015].copy(deep=True)
651646
data_import_hist["technology"] = "import_aluminum"
652647
data_import_hist["mode"] = "M1"
653648
data_import_hist["time"] = "year"
@@ -658,7 +653,7 @@ def gen_data_alu_trade(scenario: message_ix.Scenario) -> dict[str, pd.DataFrame]
658653
# For exports this corresponds to: MANUFACTURING|Outputs|Exports
659654

660655
data_export = data_trade[data_trade["Variable"] == "MANUFACTURING|Outputs|Exports"]
661-
data_export_hist = data_export[data_export["year_act"] <= 2015]
656+
data_export_hist = data_export[data_export["year_act"] <= 2015].copy(deep=True)
662657
data_export_hist["technology"] = "export_aluminum"
663658
data_export_hist["mode"] = "M1"
664659
data_export_hist["time"] = "year"
@@ -714,7 +709,9 @@ def gen_data_alu_trade(scenario: message_ix.Scenario) -> dict[str, pd.DataFrame]
714709
merged_df["value"] = merged_df["value_export"] - merged_df["value_import"]
715710

716711
# Select relevant columns for the final DataFrame
717-
bound_act_net_export_chn = merged_df[["node_loc", "year_act", "value"]]
712+
bound_act_net_export_chn = merged_df[["node_loc", "year_act", "value"]].copy(
713+
deep=True
714+
)
718715

719716
bound_act_net_export_chn["technology"] = "export_aluminum"
720717
bound_act_net_export_chn["mode"] = "M1"

message_ix_models/model/material/data_ammonia_new.py

+11-12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Any, cast
2+
13
import message_ix
24
import numpy as np
35
import pandas as pd
@@ -160,10 +162,7 @@ def __missing__(self, key):
160162
conv_cost_df = pd.DataFrame()
161163
df = par_dict[p]
162164
for tec in tec_list:
163-
if p == "inv_cost":
164-
year_col = "year_vtg"
165-
else:
166-
year_col = "year_act"
165+
year_col = "year_vtg" if p == "inv_cost" else "year_act"
167166

168167
df_tecs = df[df["technology"] == tec]
169168
df_tecs = df_tecs.merge(cost_conv, left_on=year_col, right_index=True)
@@ -213,11 +212,11 @@ def gen_data_rel(scenario, dry_run=False, add_ccs: bool = True):
213212
df = par_dict[par_name]
214213
# remove "default" node name to broadcast with all scenario regions later
215214
df["node_rel"] = df["node_rel"].apply(lambda x: None if x == "all" else x)
216-
df = df.to_dict()
217-
df = make_df(par_name, **df)
215+
df_dict = cast(dict[str, Any], df.to_dict())
216+
df = make_df(par_name, **df_dict)
218217
# split df into df with default values and df with regionalized values
219218
df_all_regs = df[df["node_rel"].isna()]
220-
df_single_regs = df[~df["node_rel"].isna()]
219+
df_single_regs = df.copy(deep=True).loc[~df["node_rel"].isna()]
221220

222221
# broadcast regions to default parameter values
223222
df_all_regs = df_all_regs.pipe(broadcast, node_rel=nodes)
@@ -234,7 +233,9 @@ def same_node_if_nan(df):
234233
df_single_regs["node_loc"] = df_single_regs["node_loc"].apply(
235234
lambda x: None if x == "all" else x
236235
)
237-
df_new_reg_all_regs = df_single_regs[df_single_regs["node_loc"].isna()]
236+
df_new_reg_all_regs = df_single_regs.copy(deep=True).loc[
237+
df_single_regs["node_loc"].isna()
238+
]
238239
df_new_reg_all_regs = df_new_reg_all_regs.pipe(broadcast, node_loc=nodes)
239240
df_single_regs = pd.concat(
240241
[
@@ -466,13 +467,11 @@ def read_demand() -> dict[str, pd.DataFrame]:
466467
# Process the regional historical activities
467468

468469
fs_GLO = feedshare_GLO.copy()
469-
fs_GLO.insert(1, "bio_pct", 0)
470-
fs_GLO.insert(2, "elec_pct", 0)
470+
fs_GLO.insert(1, "bio_pct", 0.0)
471+
fs_GLO.insert(2, "elec_pct", 0.0)
471472
# 17/14 NH3:N ratio, to get NH3 activity based on N demand
472473
# => No NH3 loss assumed during production
473474

474-
# FIXME: Name: elec_pct, dtype: float64 ' has dtype incompatible with int64,
475-
# please explicitly cast to a compatible dtype first.
476475
fs_GLO.iloc[:, 1:6] = input_fuel[5] * fs_GLO.iloc[:, 1:6]
477476
fs_GLO.insert(6, "NH3_to_N", 1)
478477

message_ix_models/model/material/data_power_sector.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ def read_material_intensities(
184184
for t in data_lca["technology"].unique():
185185
for c in data_lca["commodity"].unique():
186186
for p in data_lca["phase"].unique():
187-
temp = data_lca.loc[
187+
temp = data_lca.copy(deep=True).loc[
188188
(
189189
(data_lca["node"] == n)
190190
& (data_lca["technology"] == t)
@@ -501,6 +501,4 @@ def gen_data_power_sector(
501501
maybe_init_pars(scenario)
502502

503503
# Concatenate to one data frame per parameter
504-
results = {par_name: pd.concat(dfs) for par_name, dfs in results.items()}
505-
506-
return results
504+
return {par_name: pd.concat(dfs) for par_name, dfs in results.items()}

message_ix_models/model/material/data_util.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ def modify_demand_and_hist_activity(scen: message_ix.Scenario) -> None:
204204
columns=["REGION", "SECTOR", "FUEL", "RYEAR", "UNIT_OUT", "RESULT"]
205205
)
206206
for r in df_spec["REGION"].unique():
207-
df_spec_temp = df_spec.loc[df_spec["REGION"] == r]
207+
df_spec_temp = df_spec[df_spec["REGION"] == r].copy(deep=True)
208208
df_spec_total_temp = df_spec_total.loc[df_spec_total["REGION"] == r]
209209
df_spec_temp.loc[:, "i_spec"] = (
210210
df_spec_temp.loc[:, "RESULT"]
@@ -269,7 +269,7 @@ def modify_demand_and_hist_activity(scen: message_ix.Scenario) -> None:
269269
)
270270

271271
for r in df_therm["REGION"].unique():
272-
df_therm_temp = df_therm.loc[df_therm["REGION"] == r]
272+
df_therm_temp = df_therm.loc[df_therm["REGION"] == r].copy(deep=True)
273273
df_therm_total_temp = df_therm_total.loc[df_therm_total["REGION"] == r]
274274
df_therm_temp.loc[:, "i_therm"] = (
275275
df_therm_temp.loc[:, "RESULT"]

message_ix_models/model/material/material_demand/material_demand_calc.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -134,27 +134,27 @@ def read_timer_gdp(
134134

135135

136136
def project_demand(df: pd.DataFrame, phi: float, mu: float):
137-
df_demand = df.groupby("region", group_keys=False).apply(
137+
df_demand = df.groupby("region", group_keys=False)[df.columns].apply(
138138
lambda group: group.assign(
139139
demand_pcap_base=group["demand.tot.base"].iloc[0]
140140
* giga
141141
/ group["pop.mil"].iloc[0]
142142
/ mega
143143
)
144144
)
145-
df_demand = df_demand.groupby("region", group_keys=False).apply(
145+
df_demand = df_demand.groupby("region", group_keys=False)[df_demand.columns].apply(
146146
lambda group: group.assign(
147147
gap_base=group["demand_pcap_base"].iloc[0] - group["demand_pcap0"].iloc[0]
148148
)
149149
)
150-
df_demand = df_demand.groupby("region", group_keys=False).apply(
150+
df_demand = df_demand.groupby("region", group_keys=False)[df_demand.columns].apply(
151151
lambda group: group.assign(
152152
demand_pcap=group["demand_pcap0"]
153153
+ group["gap_base"] * gompertz(phi, mu, y=group["year"])
154154
)
155155
)
156156
df_demand = (
157-
df_demand.groupby("region", group_keys=False)
157+
df_demand.groupby("region", group_keys=False)[df_demand.columns]
158158
.apply(
159159
lambda group: group.assign(
160160
demand_tot=group["demand_pcap"] * group["pop.mil"] * mega / giga

message_ix_models/model/material/util.py

+13-18
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,8 @@ def prepare_xlsx_for_explorer(filepath: str) -> None:
6464
"""
6565
df = pd.read_excel(filepath)
6666

67-
def add_R12(str):
68-
if len(str) < 5:
69-
return "R12_" + str
70-
else:
71-
return str
67+
def add_R12(str: str) -> str:
68+
return "R12_" + str if len(str) < 5 else str
7269

7370
df = df[~df["Region"].isna()]
7471
df["Region"] = df["Region"].map(add_R12)
@@ -97,7 +94,7 @@ def combine_df_dictionaries(*args: dict[str, pd.DataFrame]) -> dict:
9794
return comb_dict
9895

9996

100-
def read_yaml_file(file_path: str or Path) -> dict or None:
97+
def read_yaml_file(file_path: Union[str, Path]) -> Union[dict, None]:
10198
"""
10299
Tries to read yaml file into a dict
103100
@@ -119,7 +116,8 @@ def read_yaml_file(file_path: str or Path) -> dict or None:
119116
return None
120117

121118

122-
def invert_dictionary(original_dict: dict[str, list]) -> dict:
119+
# NOTE guessing the type hint here, but this seems unused anyway
120+
def invert_dictionary(original_dict: dict[str, list[str]]) -> dict[str, list[str]]:
123121
"""
124122
Create inverted dictionary from existing dictionary, where values turn
125123
into keys and vice versa
@@ -134,7 +132,7 @@ def invert_dictionary(original_dict: dict[str, list]) -> dict:
134132
dict
135133
136134
"""
137-
inverted_dict = {}
135+
inverted_dict: dict[str, list[str]] = {}
138136
for key, value in original_dict.items():
139137
for array_element in value:
140138
if array_element not in inverted_dict:
@@ -196,7 +194,7 @@ def remove_from_list_if_exists(element: Any, _list: list) -> None:
196194
_list.remove(element)
197195

198196

199-
def exponential(x: float or list[float], b: float, m: float) -> float:
197+
def exponential(x: Union[float, list[float]], b: float, m: float) -> float:
200198
"""
201199
Mathematical function used in Excels GROWTH function
202200
@@ -300,7 +298,7 @@ def update_macro_calib_file(scenario: message_ix.Scenario, fname: str) -> None:
300298
df = scenario.var("COST_NODAL_NET", filters={"year": years_cost})
301299
df["node"] = pd.Categorical(df["node"], nodes)
302300
df = df[df["year"].isin(years_cost)].groupby(["node"]).apply(cost_fit)
303-
ws = wb.get_sheet_by_name("cost_ref")
301+
ws = wb["cost_ref"]
304302
# write derived values to sheet. Cell B7 (MEA region) is skipped.
305303
for i in range(2, 7):
306304
ws[f"B{i}"].value = df.values[i - 2]
@@ -316,28 +314,25 @@ def update_macro_calib_file(scenario: message_ix.Scenario, fname: str) -> None:
316314
df["node"] = pd.Categorical(df["node"], nodes)
317315
df["commodity"] = pd.Categorical(df["commodity"], comms)
318316
df = df.groupby(["node", "commodity"]).apply(price_fit)
319-
ws = wb.get_sheet_by_name("price_ref")
317+
ws = wb["price_ref"]
320318
for i in range(2, 62):
321319
ws[f"C{i}"].value = df.values[i - 2]
322320
wb.save(path)
323321

324322

325323
def get_ssp_from_context(context: Context) -> str:
326-
"""
327-
Get selected SSP from context
324+
"""Get selected SSP from context
325+
328326
Parameters
329327
----------
330328
context: Context
329+
331330
Returns
332331
-------
333332
str
334333
SSP label
335334
"""
336-
if "ssp" not in context:
337-
ssp = "SSP2"
338-
else:
339-
ssp = context["ssp"]
340-
return ssp
335+
return "SSP2" if "ssp" not in context else context["ssp"]
341336

342337

343338
def maybe_remove_water_tec(scenario: message_ix.Scenario, results: dict) -> None:

0 commit comments

Comments
 (0)