Skip to content

Commit 4bf4803

Browse files
committed
fixes impfset change helper
1 parent d63451c commit 4bf4803

2 files changed

Lines changed: 38 additions & 25 deletions

File tree

climada/entity/measures/helper.py

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -175,25 +175,40 @@ def impfset_change(impfset: ImpactFuncSet, **_kwargs) -> ImpactFuncSet:
175175
if impfset_modifier.new_impfset_path is not None
176176
else impfset
177177
)
178-
funcs = changed_impfset.get_func(
179-
impfset_modifier.haz_type, impfset_modifier.impf_id
180-
)
178+
if impfset_modifier.impf_ids is None or impfset_modifier.impf_ids == "all":
179+
ids_to_change = impfset.get_ids(haz_type=impfset_modifier.haz_type)
180+
elif isinstance(impfset_modifier.impf_ids, list):
181+
ids_to_change = impfset_modifier.impf_ids
182+
elif isinstance(impfset_modifier.impf_ids, (str, int)):
183+
ids_to_change = [impfset_modifier.impf_ids]
184+
else:
185+
raise ValueError(
186+
f"Impact function ids to changes are invalid: {impfset_modifier.impf_ids}"
187+
)
188+
189+
funcs = changed_impfset.get_func(haz_type=impfset_modifier.haz_type)
181190
funcs = [funcs] if isinstance(funcs, ImpactFunc) else funcs
191+
182192
for impf in funcs:
183193
# Apply Intensity Mod
184-
if impf.id in impfset_modifier.impf_intensity_modifier:
185-
mult, shift = impfset_modifier.impf_intensity_modifier[impf.id]
186-
impf.intensity = np.maximum(impf.intensity * mult - shift, 0.0)
187-
188-
# Apply MDD Mod
189-
if impf.id in impfset_modifier.impf_mdd_modifier:
190-
mult, shift = impfset_modifier.impf_mdd_modifier[impf.id]
191-
impf.mdd = np.maximum(impf.mdd * mult + shift, 0.0)
192-
193-
# Apply PAA Mod
194-
if impf.id in impfset_modifier.impf_paa_modifier:
195-
mult, shift = impfset_modifier.impf_paa_modifier[impf.id]
196-
impf.paa = np.maximum(impf.paa * mult + shift, 0.0)
194+
if impf.id in ids_to_change:
195+
mult, add = (
196+
impfset_modifier.impf_int_mult,
197+
impfset_modifier.impf_int_add,
198+
)
199+
impf.intensity = impf.intensity * mult + add
200+
201+
mult, add = (
202+
impfset_modifier.impf_mdd_mult,
203+
impfset_modifier.impf_mdd_add,
204+
)
205+
impf.mdd = impf.mdd * mult + add
206+
207+
mult, add = (
208+
impfset_modifier.impf_paa_mult,
209+
impfset_modifier.impf_paa_add,
210+
)
211+
impf.paa = impf.paa * mult + add
197212

198213
return changed_impfset
199214

climada/entity/measures/measure_config.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,13 @@ class ImpfsetModifierConfig(_ModifierConfig):
116116
"""Configuration for impact function modifiers."""
117117

118118
haz_type: str
119-
impf_id: Optional[Union[int, str]] = 1
120-
impf_mdd_mult: Optional[float] = 1.0
121-
impf_mdd_add: Optional[float] = 0.0
122-
impf_paa_mult: Optional[float] = 1.0
123-
impf_paa_add: Optional[float] = 0.0
124-
impf_int_mult: Optional[float] = 1.0
125-
impf_int_add: Optional[float] = 0.0
119+
impf_ids: Optional[Union[int, str, list[Union[int, str]]]] = None
120+
impf_mdd_mult: float = 1.0
121+
impf_mdd_add: float = 0.0
122+
impf_paa_mult: float = 1.0
123+
impf_paa_add: float = 0.0
124+
impf_int_mult: float = 1.0
125+
impf_int_add: float = 0.0
126126
new_impfset_path: Optional[str] = None
127127
"""Excel filepath for new impfset."""
128128

@@ -195,7 +195,6 @@ class CostIncomeConfig(_ModifierConfig):
195195
income_yearly_growth_rate: float = 0.0
196196
freq: str = "Y"
197197
custom_cash_flows: Optional[list[dict]] = None
198-
# ^ serialized as list of {"date": "2024-01-01", "cost": -100.0, "income": 50.0}
199198

200199
def to_cost_income(self) -> CostIncome:
201200
df = None
@@ -303,7 +302,6 @@ def from_row(
303302
) -> "MeasureConfig":
304303
"""Build a MeasureConfig from a legacy Excel row."""
305304
row_dict = row.to_dict()
306-
impf_id = row_dict.get("impf_id", 1)
307305
return cls.from_dict(row_dict)
308306

309307

0 commit comments

Comments
 (0)