Skip to content

Commit 26bb85d

Browse files
committed
adds list comprehensions, fixes typo
1 parent 3f0c5f9 commit 26bb85d

File tree

2 files changed

+30
-46
lines changed

2 files changed

+30
-46
lines changed

climada/trajectories/interpolated_trajectory.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ def pairwise(container: list):
173173
next(b, None)
174174
return zip(a, b)
175175

176-
# impfset = self._merge_impfset(snapshots)
177176
return [
178177
CalcRiskMetricsPeriod(
179178
start_snapshot,
@@ -210,11 +209,11 @@ def _generic_metrics(
210209
return getattr(self, attr_name)
211210

212211
LOGGER.debug(f"Computing {attr_name}")
213-
tmp = []
214-
for calc_period in self._risk_metrics_calculators:
215-
# Call the specified method on the calc_period object
216-
with log_level(level="WARNING", name_prefix="climada"):
217-
tmp.append(getattr(calc_period, metric_meth)(**kwargs))
212+
with log_level(level="WARNING", name_prefix="climada"):
213+
tmp = [
214+
getattr(calc_period, metric_meth)(**kwargs)
215+
for calc_period in self._risk_metrics_calculators
216+
]
218217

219218
# Notably for per_group_aai being None:
220219
try:
@@ -337,7 +336,7 @@ def risk_contributions_metrics(self, **kwargs) -> pd.DataFrame:
337336
)
338337

339338
# If there is more than one Snapshot, we need to update the
340-
# contributions from previous periods for for continuity
339+
# contributions from previous periods for continuity
341340
# and to set the base risk from the first period
342341
if len(self._snapshots) > 2:
343342
tmp.set_index(["group", "date", "measure", "metric"], inplace=True)

climada/trajectories/static_trajectory.py

Lines changed: 24 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -105,49 +105,34 @@ def _generic_metrics(
105105
# Construct the attribute name for storing the metric results
106106
attr_name = f"_{metric_name}_metrics"
107107

108-
tmp = []
109108
with log_level(level="WARNING", name_prefix="climada"):
110-
tmp.append(getattr(self._risk_metrics_calculators, metric_meth)(**kwargs))
111-
112-
# Notably for per_group_aai being None:
113-
try:
114-
tmp = pd.concat(tmp)
115-
if len(tmp) == 0:
116-
return pd.DataFrame()
117-
except ValueError as e:
118-
if str(e) == "All objects passed were None":
119-
return pd.DataFrame()
120-
else:
121-
raise e
122-
123-
else:
124-
tmp = tmp.set_index(["date", "group", "measure", "metric"])
125-
if "coord_id" in tmp.columns:
126-
tmp = tmp.set_index(["coord_id"], append=True)
127-
128-
# When more than 2 snapshots, there are duplicated rows, we need to remove them.
129-
tmp = tmp[~tmp.index.duplicated(keep="first")]
130-
tmp = tmp.reset_index()
131-
tmp["group"] = tmp["group"].cat.add_categories([self._all_groups_name])
132-
tmp["group"] = tmp["group"].fillna(self._all_groups_name)
133-
columns_to_front = ["group", "date", "measure", "metric"]
134-
tmp = tmp[
135-
columns_to_front
136-
+ [
137-
col
138-
for col in tmp.columns
139-
if col not in columns_to_front + ["group", "risk", "rp"]
140-
]
141-
+ ["risk"]
109+
tmp = getattr(self._risk_metrics_calculators, metric_meth)(**kwargs)
110+
111+
tmp = tmp.set_index(["date", "group", "measure", "metric"])
112+
if "coord_id" in tmp.columns:
113+
tmp = tmp.set_index(["coord_id"], append=True)
114+
115+
# When more than 2 snapshots, there are duplicated rows, we need to remove them.
116+
tmp = tmp[~tmp.index.duplicated(keep="first")]
117+
tmp = tmp.reset_index()
118+
tmp["group"] = tmp["group"].cat.add_categories([self._all_groups_name])
119+
tmp["group"] = tmp["group"].fillna(self._all_groups_name)
120+
columns_to_front = ["group", "date", "measure", "metric"]
121+
tmp = tmp[
122+
columns_to_front
123+
+ [
124+
col
125+
for col in tmp.columns
126+
if col not in columns_to_front + ["group", "risk", "rp"]
142127
]
143-
setattr(self, attr_name, tmp)
128+
+ ["risk"]
129+
]
130+
setattr(self, attr_name, tmp)
144131

145-
if self._risk_disc_rates:
146-
return self.npv_transform(
147-
getattr(self, attr_name), self._risk_disc_rates
148-
)
132+
if self._risk_disc_rates:
133+
return self.npv_transform(getattr(self, attr_name), self._risk_disc_rates)
149134

150-
return getattr(self, attr_name)
135+
return getattr(self, attr_name)
151136

152137
def eai_metrics(self, **kwargs) -> pd.DataFrame:
153138
"""Return the estimated annual impacts at each exposure point for each date.

0 commit comments

Comments
 (0)