Skip to content

Commit 3692813

Browse files
committed
Implements equality methods for impf and impfset
1 parent 7b1a6bb commit 3692813

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

climada/entity/impact_funcs/base.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,20 @@ def __init__(
9797
self.mdd = mdd if mdd is not None else np.array([])
9898
self.paa = paa if paa is not None else np.array([])
9999

100+
def __eq__(self, value: object, /) -> bool:
101+
if isinstance(value, ImpactFunc):
102+
return (
103+
self.haz_type == value.haz_type
104+
and self.id == value.id
105+
and self.name == value.name
106+
and self.intensity_unit == value.intensity_unit
107+
and np.array_equal(self.intensity, value.intensity)
108+
and np.array_equal(self.mdd, value.mdd)
109+
and np.array_equal(self.paa, value.paa)
110+
)
111+
else:
112+
return False
113+
100114
def calc_mdr(self, inten: Union[float, np.ndarray]) -> np.ndarray:
101115
"""Interpolate impact function to a given intensity.
102116
@@ -177,7 +191,7 @@ def from_step_impf(
177191
mdd: tuple[float, float] = (0, 1),
178192
paa: tuple[float, float] = (1, 1),
179193
impf_id: int = 1,
180-
**kwargs
194+
**kwargs,
181195
):
182196
"""Step function type impact function.
183197
@@ -218,7 +232,7 @@ def from_step_impf(
218232
intensity=intensity,
219233
mdd=mdd,
220234
paa=paa,
221-
**kwargs
235+
**kwargs,
222236
)
223237

224238
def set_step_impf(self, *args, **kwargs):
@@ -238,7 +252,7 @@ def from_sigmoid_impf(
238252
x0: float,
239253
haz_type: str,
240254
impf_id: int = 1,
241-
**kwargs
255+
**kwargs,
242256
):
243257
r"""Sigmoid type impact function hinging on three parameter.
244258
@@ -287,7 +301,7 @@ def from_sigmoid_impf(
287301
intensity=intensity,
288302
paa=paa,
289303
mdd=mdd,
290-
**kwargs
304+
**kwargs,
291305
)
292306

293307
def set_sigmoid_impf(self, *args, **kwargs):
@@ -308,7 +322,7 @@ def from_poly_s_shape(
308322
exponent: float,
309323
haz_type: str,
310324
impf_id: int = 1,
311-
**kwargs
325+
**kwargs,
312326
):
313327
r"""S-shape polynomial impact function hinging on four parameter.
314328

climada/entity/impact_funcs/impact_func_set.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,23 @@ def __init__(self, impact_funcs: Optional[Iterable[ImpactFunc]] = None):
109109
for impf in impact_funcs:
110110
self.append(impf)
111111

112+
def __eq__(self, value: object, /) -> bool:
113+
if not isinstance(value, ImpactFuncSet):
114+
return False
115+
116+
if self._data.keys() != value._data.keys():
117+
return False
118+
119+
for haz_type1, id_map1 in self._data.items():
120+
id_map2 = value._data[haz_type1]
121+
if id_map1.keys() != id_map2.keys():
122+
return False
123+
for fid, func1 in id_map1.items():
124+
if not func1 == id_map2[fid]:
125+
return False
126+
127+
return True
128+
112129
def clear(self):
113130
"""Reinitialize attributes."""
114131
self._data = dict() # {hazard_type : {id:ImpactFunc}}

0 commit comments

Comments
 (0)