Skip to content

Commit bb4fbba

Browse files
committed
Polish (unused imports, lsp/pylint errors)
1 parent a032cc5 commit bb4fbba

6 files changed

Lines changed: 53 additions & 83 deletions

File tree

climada/test/test_trajectories.py

Lines changed: 37 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -19,64 +19,35 @@
1919
2020
"""
2121

22-
import copy
23-
from itertools import groupby
24-
from unittest import TestCase
25-
26-
import geopandas as gpd
2722
import numpy as np
2823
import pandas as pd
2924
import pytest
3025

3126
from climada.engine.impact_calc import ImpactCalc
3227
from climada.entity.disc_rates.base import DiscRates
33-
from climada.entity.exposures.base import Exposures
34-
from climada.entity.impact_funcs.base import ImpactFunc
35-
from climada.entity.impact_funcs.impact_func_set import ImpactFuncSet
36-
from climada.hazard.base import Hazard
37-
from climada.test.conftest import (
38-
CATEGORIES,
39-
DATES,
40-
EVENT_IDS,
41-
EVENT_NAMES,
42-
EXPOSURE_REF_YEAR,
43-
FREQUENCY,
44-
FREQUENCY_UNIT,
45-
HAZARD_MAX_INTENSITY,
46-
HAZARD_TYPE,
47-
HAZARD_UNIT,
48-
IMPF_ID,
49-
IMPF_NAME,
50-
)
28+
from climada.test.conftest import CATEGORIES, EXPOSURE_REF_YEAR
5129
from climada.trajectories import InterpolatedRiskTrajectory, StaticRiskTrajectory
5230
from climada.trajectories.constants import (
5331
AAI_METRIC_NAME,
54-
AAI_PER_GROUP_METRIC_NAME,
5532
CONTRIBUTION_BASE_RISK_NAME,
5633
CONTRIBUTION_EXPOSURE_NAME,
5734
CONTRIBUTION_HAZARD_NAME,
5835
CONTRIBUTION_INTERACTION_TERM_NAME,
5936
CONTRIBUTION_VULNERABILITY_NAME,
60-
COORD_ID_COL_NAME,
6137
DATE_COL_NAME,
62-
EAI_METRIC_NAME,
6338
GROUP_COL_NAME,
6439
MEASURE_COL_NAME,
6540
METRIC_COL_NAME,
6641
NO_MEASURE_VALUE,
6742
PERIOD_COL_NAME,
68-
RETURN_PERIOD_METRIC_NAME,
6943
RISK_COL_NAME,
70-
RP_VALUE_PREFIX,
7144
UNIT_COL_NAME,
7245
)
7346
from climada.trajectories.snapshot import Snapshot
7447
from climada.trajectories.trajectory import DEFAULT_RP
7548

7649
EXPOSURE_FUTURE_YEAR = 2040
7750

78-
from climada.trajectories.snapshot import Snapshot
79-
8051

8152
@pytest.fixture(scope="session")
8253
def snapshot_factory(
@@ -509,21 +480,21 @@ def expected_interp_metrics():
509480
return pd.DataFrame.from_dict(
510481
{'index': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
511482
'columns': [DATE_COL_NAME, GROUP_COL_NAME, MEASURE_COL_NAME, METRIC_COL_NAME, UNIT_COL_NAME, RISK_COL_NAME],
512-
'data': [[ pd.Period(2020), 'All',NO_MEASURE_VALUE, 'aai', 'USD', 18.0],
513-
[ pd.Period(2021), 'All',NO_MEASURE_VALUE, 'aai', 'USD', 94.5],
483+
'data': [[ pd.Period("2020"), 'All',NO_MEASURE_VALUE, 'aai', 'USD', 18.0],
484+
[ pd.Period("2021"), 'All',NO_MEASURE_VALUE, 'aai', 'USD', 94.5],
514485
# Above should indeed not be 216+18 / 2 and slightly
515486
# because as we interpolate each contributor separately,
516487
# the interaction term grows slower.
517-
[ pd.Period(2022), 'All',NO_MEASURE_VALUE, 'aai', 'USD', 216.0],
518-
[ pd.Period(2020), 'All',NO_MEASURE_VALUE, 'rp_50', 'USD', 0.0],
519-
[ pd.Period(2021), 'All',NO_MEASURE_VALUE, 'rp_50', 'USD', 0.0],
520-
[ pd.Period(2022), 'All',NO_MEASURE_VALUE, 'rp_50', 'USD', 0.0],
521-
[ pd.Period(2020), 'All',NO_MEASURE_VALUE, 'rp_100', 'USD', 500.0],
522-
[ pd.Period(2021), 'All',NO_MEASURE_VALUE, 'rp_100', 'USD', 2625.0],
523-
[ pd.Period(2022), 'All',NO_MEASURE_VALUE, 'rp_100', 'USD', 6000.0],
524-
[ pd.Period(2020), 'All',NO_MEASURE_VALUE, 'rp_250', 'USD', 3750.0],
525-
[ pd.Period(2021), 'All',NO_MEASURE_VALUE, 'rp_250', 'USD', 19687.5],
526-
[ pd.Period(2022), 'All',NO_MEASURE_VALUE, 'rp_250', 'USD', 45000.0]],
488+
[ pd.Period("2022"), 'All',NO_MEASURE_VALUE, 'aai', 'USD', 216.0],
489+
[ pd.Period("2020"), 'All',NO_MEASURE_VALUE, 'rp_50', 'USD', 0.0],
490+
[ pd.Period("2021"), 'All',NO_MEASURE_VALUE, 'rp_50', 'USD', 0.0],
491+
[ pd.Period("2022"), 'All',NO_MEASURE_VALUE, 'rp_50', 'USD', 0.0],
492+
[ pd.Period("2020"), 'All',NO_MEASURE_VALUE, 'rp_100', 'USD', 500.0],
493+
[ pd.Period("2021"), 'All',NO_MEASURE_VALUE, 'rp_100', 'USD', 2625.0],
494+
[ pd.Period("2022"), 'All',NO_MEASURE_VALUE, 'rp_100', 'USD', 6000.0],
495+
[ pd.Period("2020"), 'All',NO_MEASURE_VALUE, 'rp_250', 'USD', 3750.0],
496+
[ pd.Period("2021"), 'All',NO_MEASURE_VALUE, 'rp_250', 'USD', 19687.5],
497+
[ pd.Period("2022"), 'All',NO_MEASURE_VALUE, 'rp_250', 'USD', 45000.0]],
527498
'index_names': [None],
528499
'column_names': [None]
529500
},
@@ -612,21 +583,21 @@ def expected_interp_metrics_rpchange():
612583
return pd.DataFrame.from_dict(
613584
{'index': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
614585
'columns': [DATE_COL_NAME, GROUP_COL_NAME, MEASURE_COL_NAME, METRIC_COL_NAME, UNIT_COL_NAME, RISK_COL_NAME],
615-
'data': [[ pd.Period(2020), 'All',NO_MEASURE_VALUE, 'aai', 'USD', 18.0],
616-
[ pd.Period(2021), 'All',NO_MEASURE_VALUE, 'aai', 'USD', 94.5],
586+
'data': [[ pd.Period("2020"), 'All',NO_MEASURE_VALUE, 'aai', 'USD', 18.0],
587+
[ pd.Period("2021"), 'All',NO_MEASURE_VALUE, 'aai', 'USD', 94.5],
617588
# Above should indeed not be 216+18 / 2 and slightly
618589
# because as we interpolate each contributor separately,
619590
# the interaction term grows slower.
620-
[ pd.Period(2022), 'All',NO_MEASURE_VALUE, 'aai', 'USD', 216.0],
621-
[ pd.Period(2020), 'All',NO_MEASURE_VALUE, 'rp_20', 'USD', 0.0],
622-
[ pd.Period(2021), 'All',NO_MEASURE_VALUE, 'rp_20', 'USD', 0.0],
623-
[ pd.Period(2022), 'All',NO_MEASURE_VALUE, 'rp_20', 'USD', 0.0],
624-
[ pd.Period(2020), 'All',NO_MEASURE_VALUE, 'rp_50', 'USD', 0.0],
625-
[ pd.Period(2021), 'All',NO_MEASURE_VALUE, 'rp_50', 'USD', 0.0],
626-
[ pd.Period(2022), 'All',NO_MEASURE_VALUE, 'rp_50', 'USD', 0.0],
627-
[ pd.Period(2020), 'All',NO_MEASURE_VALUE, 'rp_500', 'USD', 3750.0],
628-
[ pd.Period(2021), 'All',NO_MEASURE_VALUE, 'rp_500', 'USD', 19687.5],
629-
[ pd.Period(2022), 'All',NO_MEASURE_VALUE, 'rp_500', 'USD', 45000.0]],
591+
[ pd.Period("2022"), 'All',NO_MEASURE_VALUE, 'aai', 'USD', 216.0],
592+
[ pd.Period("2020"), 'All',NO_MEASURE_VALUE, 'rp_20', 'USD', 0.0],
593+
[ pd.Period("2021"), 'All',NO_MEASURE_VALUE, 'rp_20', 'USD', 0.0],
594+
[ pd.Period("2022"), 'All',NO_MEASURE_VALUE, 'rp_20', 'USD', 0.0],
595+
[ pd.Period("2020"), 'All',NO_MEASURE_VALUE, 'rp_50', 'USD', 0.0],
596+
[ pd.Period("2021"), 'All',NO_MEASURE_VALUE, 'rp_50', 'USD', 0.0],
597+
[ pd.Period("2022"), 'All',NO_MEASURE_VALUE, 'rp_50', 'USD', 0.0],
598+
[ pd.Period("2020"), 'All',NO_MEASURE_VALUE, 'rp_500', 'USD', 3750.0],
599+
[ pd.Period("2021"), 'All',NO_MEASURE_VALUE, 'rp_500', 'USD', 19687.5],
600+
[ pd.Period("2022"), 'All',NO_MEASURE_VALUE, 'rp_500', 'USD', 45000.0]],
630601
'index_names': [None],
631602
'column_names': [None]
632603
},
@@ -659,21 +630,21 @@ def expected_interp_metrics_ratechange():
659630
return pd.DataFrame.from_dict(
660631
{'index': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
661632
'columns': [DATE_COL_NAME, GROUP_COL_NAME, MEASURE_COL_NAME, METRIC_COL_NAME, UNIT_COL_NAME, RISK_COL_NAME],
662-
'data': [[ pd.Period(2020), 'All',NO_MEASURE_VALUE, 'aai', 'USD', 18.0],
663-
[ pd.Period(2021), 'All',NO_MEASURE_VALUE, 'aai', 'USD', 90.0],
633+
'data': [[ pd.Period("2020"), 'All',NO_MEASURE_VALUE, 'aai', 'USD', 18.0],
634+
[ pd.Period("2021"), 'All',NO_MEASURE_VALUE, 'aai', 'USD', 90.0],
664635
# Above should indeed not be 216+18 / 2 and slightly
665636
# because as we interpolate each contributor separately,
666637
# the interaction term grows slower.
667-
[ pd.Period(2022), 'All',NO_MEASURE_VALUE, 'aai', 'USD', 195.9183673469],
668-
[ pd.Period(2020), 'All',NO_MEASURE_VALUE, 'rp_20', 'USD', 0.0],
669-
[ pd.Period(2021), 'All',NO_MEASURE_VALUE, 'rp_20', 'USD', 0.0],
670-
[ pd.Period(2022), 'All',NO_MEASURE_VALUE, 'rp_20', 'USD', 0.0],
671-
[ pd.Period(2020), 'All',NO_MEASURE_VALUE, 'rp_50', 'USD', 0.0],
672-
[ pd.Period(2021), 'All',NO_MEASURE_VALUE, 'rp_50', 'USD', 0.0],
673-
[ pd.Period(2022), 'All',NO_MEASURE_VALUE, 'rp_50', 'USD', 0.0],
674-
[ pd.Period(2020), 'All',NO_MEASURE_VALUE, 'rp_100', 'USD', 500.0],
675-
[ pd.Period(2021), 'All',NO_MEASURE_VALUE, 'rp_100', 'USD', 2500.0],
676-
[ pd.Period(2022), 'All',NO_MEASURE_VALUE, 'rp_100', 'USD', 5442.176870]],
638+
[ pd.Period("2022"), 'All',NO_MEASURE_VALUE, 'aai', 'USD', 195.9183673469],
639+
[ pd.Period("2020"), 'All',NO_MEASURE_VALUE, 'rp_20', 'USD', 0.0],
640+
[ pd.Period("2021"), 'All',NO_MEASURE_VALUE, 'rp_20', 'USD', 0.0],
641+
[ pd.Period("2022"), 'All',NO_MEASURE_VALUE, 'rp_20', 'USD', 0.0],
642+
[ pd.Period("2020"), 'All',NO_MEASURE_VALUE, 'rp_50', 'USD', 0.0],
643+
[ pd.Period("2021"), 'All',NO_MEASURE_VALUE, 'rp_50', 'USD', 0.0],
644+
[ pd.Period("2022"), 'All',NO_MEASURE_VALUE, 'rp_50', 'USD', 0.0],
645+
[ pd.Period("2020"), 'All',NO_MEASURE_VALUE, 'rp_100', 'USD', 500.0],
646+
[ pd.Period("2021"), 'All',NO_MEASURE_VALUE, 'rp_100', 'USD', 2500.0],
647+
[ pd.Period("2022"), 'All',NO_MEASURE_VALUE, 'rp_100', 'USD', 5442.176870]],
677648
'index_names': [None],
678649
'column_names': [None]
679650
},

climada/trajectories/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
---
1818
1919
This module implements risk trajectory objects which enable computation and
20-
possibly interpolation of risk metric over multiple dates.
20+
possibly interpolation of risk metrics over multiple dates.
2121
2222
"""
2323

climada/trajectories/calc_risk_metrics.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,9 @@ def impact_computation_strategy(self) -> ImpactComputationStrategy:
209209
def impact_computation_strategy(self, value, /):
210210
if not isinstance(value, ImpactComputationStrategy):
211211
raise ValueError(
212-
"The provided value is not an ImpactComputationStrategy object. See the trajectory module documentation for more information on how to define your own impact computation strategies."
212+
"The provided value is not an ImpactComputationStrategy object. "
213+
"See the trajectory module documentation for more information on "
214+
"how to define your own impact computation strategies."
213215
)
214216

215217
self._impact_computation_strategy = value
@@ -809,7 +811,11 @@ def _per_date_return_periods(
809811
# Actual results
810812

811813
def _calc_eai(self) -> np.ndarray:
812-
"""Compute the EAIs at each date of the risk period (including changes in exposure, hazard and vulnerability)."""
814+
"""Compute the EAIs at each date of the risk period
815+
(including changes in exposure, hazard and vulnerability).
816+
817+
"""
818+
813819
per_date_eai_H0V0, per_date_eai_H1V0, per_date_eai_H0V1, per_date_eai_H1V1 = (
814820
self._per_date_eais_interp("H0V0"),
815821
self._per_date_eais_interp("H1V0"),
@@ -917,7 +923,8 @@ def calc_aai_per_group_metric(self) -> pd.DataFrame | None:
917923
"""
918924
if len(self._group_id_E0) < 1 or len(self._group_id_E1) < 1:
919925
LOGGER.warning(
920-
"No group id defined in at least one of the Exposures object. Per group aai will be empty."
926+
"No group id defined in at least one of the Exposures object. "
927+
"Per group aai will be empty."
921928
)
922929
return None
923930

@@ -973,7 +980,8 @@ def calc_return_periods_metric(self, return_periods: list[int]) -> pd.DataFrame:
973980
974981
"""
975982

976-
# currently mathematicaly wrong, but approximatively correct, to be reworked when concatenating the impact matrices for the interpolation
983+
# currently mathematicaly wrong, but approximatively correct,
984+
# to be reworked when concatenating the impact matrices for the interpolation
977985
per_date_rp_H0V0, per_date_rp_H1V0, per_date_rp_H0V1, per_date_rp_H1V1 = (
978986
self._per_date_return_periods("H0V0", return_periods),
979987
self._per_date_return_periods("H1V0", return_periods),

climada/trajectories/interpolated_trajectory.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@
6565
)
6666
from climada.trajectories.snapshot import Snapshot
6767
from climada.trajectories.trajectory import (
68-
DEFAULT_ALLGROUP_NAME,
6968
DEFAULT_DF_COLUMN_PRIORITY,
7069
DEFAULT_RP,
7170
INDEXING_COLUMNS,
@@ -677,7 +676,7 @@ def _date_to_period_agg(
677676
df_periods.groupby(grouper + ["period_id"], dropna=False, observed=True)[
678677
colname
679678
]
680-
.apply("mean")
679+
.mean()
681680
.reset_index()
682681
)
683682
df_periods = pd.merge(

climada/trajectories/test/test_calc_risk_metrics.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
1717
---
1818
19-
This modules implements different sparce matrices interpolation approaches.
19+
Unit tests for `calc_risk_metrics.py` .
2020
2121
"""
2222

@@ -26,19 +26,14 @@
2626
import pandas as pd
2727
import pytest
2828

29-
from climada.entity.exposures import Exposures
30-
from climada.entity.impact_funcs import ImpactFuncSet
31-
from climada.entity.impact_funcs.trop_cyclone import ImpfTropCyclone
3229
from climada.entity.measures.base import Measure
33-
from climada.hazard import Hazard
3430
from climada.trajectories.calc_risk_metrics import CalcRiskMetricsPoints
3531
from climada.trajectories.constants import (
3632
AAI_METRIC_NAME,
3733
COORD_ID_COL_NAME,
3834
DATE_COL_NAME,
3935
EAI_METRIC_NAME,
4036
GROUP_COL_NAME,
41-
GROUP_ID_COL_NAME,
4237
MEASURE_COL_NAME,
4338
METRIC_COL_NAME,
4439
NO_MEASURE_VALUE,

climada/trajectories/test/test_interpolated_risk_trajectory.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,14 @@
2929
import pandas as pd
3030

3131
from climada.entity.disc_rates.base import DiscRates
32-
from climada.trajectories.calc_risk_metrics import ( # ImpactComputationStrategy, # If needed to mock its base class directly
33-
CalcRiskMetricsPeriod,
34-
)
32+
from climada.trajectories.calc_risk_metrics import CalcRiskMetricsPeriod
3533
from climada.trajectories.constants import (
3634
AAI_METRIC_NAME,
3735
AAI_PER_GROUP_METRIC_NAME,
3836
CONTRIBUTION_BASE_RISK_NAME,
3937
CONTRIBUTION_EXPOSURE_NAME,
4038
CONTRIBUTION_HAZARD_NAME,
4139
CONTRIBUTION_INTERACTION_TERM_NAME,
42-
CONTRIBUTION_TOTAL_RISK_NAME,
4340
CONTRIBUTION_VULNERABILITY_NAME,
4441
CONTRIBUTIONS_METRIC_NAME,
4542
COORD_ID_COL_NAME,

0 commit comments

Comments
 (0)