-
Notifications
You must be signed in to change notification settings - Fork 150
Risk Trajectory Split 1 : Snapshots #1197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 4 commits
5efad83
bf00262
7e53650
e12e014
b4f05e1
4a8c770
ad2e774
25fbcda
b8ef41a
30e2d0e
ffbf31e
59ba291
77b76c4
b5d3378
6a07625
f334b8c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| """ | ||
| This file is part of CLIMADA. | ||
|
|
||
| Copyright (C) 2017 ETH Zurich, CLIMADA contributors listed in AUTHORS. | ||
|
|
||
| CLIMADA is free software: you can redistribute it and/or modify it under the | ||
| terms of the GNU General Public License as published by the Free | ||
| Software Foundation, version 3. | ||
|
|
||
| CLIMADA is distributed in the hope that it will be useful, but WITHOUT ANY | ||
| WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A | ||
| PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
|
|
||
| You should have received a copy of the GNU General Public License along | ||
| with CLIMADA. If not, see <https://www.gnu.org/licenses/>. | ||
|
|
||
| --- | ||
|
|
||
| This module implements risk trajectory objects which enable computation and | ||
| possibly interpolation of risk metric over multiple dates. | ||
|
|
||
| """ | ||
|
|
||
| from .snapshot import Snapshot | ||
|
|
||
| __all__ = [ | ||
| "Snapshot", | ||
| ] | ||
spjuhel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,167 @@ | ||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||
| This file is part of CLIMADA. | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| Copyright (C) 2017 ETH Zurich, CLIMADA contributors listed in AUTHORS. | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| CLIMADA is free software: you can redistribute it and/or modify it under the | ||||||||||||||||||||||||||||||||||||||||||||||
| terms of the GNU General Public License as published by the Free | ||||||||||||||||||||||||||||||||||||||||||||||
| Software Foundation, version 3. | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| CLIMADA is distributed in the hope that it will be useful, but WITHOUT ANY | ||||||||||||||||||||||||||||||||||||||||||||||
| WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A | ||||||||||||||||||||||||||||||||||||||||||||||
| PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| You should have received a copy of the GNU General Public License along | ||||||||||||||||||||||||||||||||||||||||||||||
| with CLIMADA. If not, see <https://www.gnu.org/licenses/>. | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| --- | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| This modules implements the Snapshot class. | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| Snapshot are used to store a snapshot of Exposure, Hazard and Vulnerability | ||||||||||||||||||||||||||||||||||||||||||||||
| at a specific date. | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| import copy | ||||||||||||||||||||||||||||||||||||||||||||||
| import datetime | ||||||||||||||||||||||||||||||||||||||||||||||
| import logging | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| import pandas as pd | ||||||||||||||||||||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unused import |
||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| from climada.entity.exposures import Exposures | ||||||||||||||||||||||||||||||||||||||||||||||
| from climada.entity.impact_funcs import ImpactFuncSet | ||||||||||||||||||||||||||||||||||||||||||||||
| from climada.entity.measures.base import Measure | ||||||||||||||||||||||||||||||||||||||||||||||
| from climada.hazard import Hazard | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| LOGGER = logging.getLogger(__name__) | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| __all__ = ["Snapshot"] | ||||||||||||||||||||||||||||||||||||||||||||||
peanutfun marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| class Snapshot: | ||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||
| A snapshot of exposure, hazard, and impact function at a specific date. | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| Parameters | ||||||||||||||||||||||||||||||||||||||||||||||
| ---------- | ||||||||||||||||||||||||||||||||||||||||||||||
| exposure : Exposures | ||||||||||||||||||||||||||||||||||||||||||||||
| hazard : Hazard | ||||||||||||||||||||||||||||||||||||||||||||||
| impfset : ImpactFuncSet | ||||||||||||||||||||||||||||||||||||||||||||||
| date : int | datetime.date | str | ||||||||||||||||||||||||||||||||||||||||||||||
| The date of the Snapshot, it can be an integer representing a year, | ||||||||||||||||||||||||||||||||||||||||||||||
| a datetime object or a string representation of a datetime object | ||||||||||||||||||||||||||||||||||||||||||||||
| with format "YYYY-MM-DD". | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
| ref_only : bool, default False | ||||||||||||||||||||||||||||||||||||||||||||||
| Should the `Snapshot` contain deep copies of the Exposures, Hazard and Impfset (False) | ||||||||||||||||||||||||||||||||||||||||||||||
| or references only (True). | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| Attributes | ||||||||||||||||||||||||||||||||||||||||||||||
| ---------- | ||||||||||||||||||||||||||||||||||||||||||||||
| date : datetime | ||||||||||||||||||||||||||||||||||||||||||||||
| Date of the snapshot. | ||||||||||||||||||||||||||||||||||||||||||||||
| measure: Measure | None | ||||||||||||||||||||||||||||||||||||||||||||||
| The possible measure applied to the snapshot. | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
47
to
64
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Classes don't have parameters, their methods do (the init in particular) |
||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| Notes | ||||||||||||||||||||||||||||||||||||||||||||||
| ----- | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| The object creates deep copies of the exposure hazard and impact function set. | ||||||||||||||||||||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| Also note that exposure, hazard and impfset are read-only properties. | ||||||||||||||||||||||||||||||||||||||||||||||
| Consider snapshot as immutable objects. | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| To create a snapshot with a measure, create a snapshot `snap` without | ||||||||||||||||||||||||||||||||||||||||||||||
| the measure and call `snap.apply_measure(measure)`, which returns a new Snapshot object | ||||||||||||||||||||||||||||||||||||||||||||||
| with the measure applied to its risk dimensions. | ||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| def __init__( | ||||||||||||||||||||||||||||||||||||||||||||||
| self, | ||||||||||||||||||||||||||||||||||||||||||||||
| *, | ||||||||||||||||||||||||||||||||||||||||||||||
| exposure: Exposures, | ||||||||||||||||||||||||||||||||||||||||||||||
| hazard: Hazard, | ||||||||||||||||||||||||||||||||||||||||||||||
| impfset: ImpactFuncSet, | ||||||||||||||||||||||||||||||||||||||||||||||
| date: int | datetime.date | str, | ||||||||||||||||||||||||||||||||||||||||||||||
| ref_only: bool = False, | ||||||||||||||||||||||||||||||||||||||||||||||
| ) -> None: | ||||||||||||||||||||||||||||||||||||||||||||||
spjuhel marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||
| self._exposure = exposure if ref_only else copy.deepcopy(exposure) | ||||||||||||||||||||||||||||||||||||||||||||||
| self._hazard = hazard if ref_only else copy.deepcopy(hazard) | ||||||||||||||||||||||||||||||||||||||||||||||
| self._impfset = impfset if ref_only else copy.deepcopy(impfset) | ||||||||||||||||||||||||||||||||||||||||||||||
| self._measure = None | ||||||||||||||||||||||||||||||||||||||||||||||
| self._date = self._convert_to_date(date) | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| @property | ||||||||||||||||||||||||||||||||||||||||||||||
| def exposure(self) -> Exposures: | ||||||||||||||||||||||||||||||||||||||||||||||
| """Exposure data for the snapshot.""" | ||||||||||||||||||||||||||||||||||||||||||||||
| return self._exposure | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| @property | ||||||||||||||||||||||||||||||||||||||||||||||
| def hazard(self) -> Hazard: | ||||||||||||||||||||||||||||||||||||||||||||||
| """Hazard data for the snapshot.""" | ||||||||||||||||||||||||||||||||||||||||||||||
| return self._hazard | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| @property | ||||||||||||||||||||||||||||||||||||||||||||||
| def impfset(self) -> ImpactFuncSet: | ||||||||||||||||||||||||||||||||||||||||||||||
| """Impact function set data for the snapshot.""" | ||||||||||||||||||||||||||||||||||||||||||||||
| return self._impfset | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| @property | ||||||||||||||||||||||||||||||||||||||||||||||
| def measure(self) -> Measure | None: | ||||||||||||||||||||||||||||||||||||||||||||||
| """(Adaptation) Measure data for the snapshot.""" | ||||||||||||||||||||||||||||||||||||||||||||||
| return self._measure | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| @property | ||||||||||||||||||||||||||||||||||||||||||||||
| def date(self) -> datetime.date: | ||||||||||||||||||||||||||||||||||||||||||||||
| """Date of the snapshot.""" | ||||||||||||||||||||||||||||||||||||||||||||||
| return self._date | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
153
to
176
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a lot of boilerplate code. Maybe just create a custom |
||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| @property | ||||||||||||||||||||||||||||||||||||||||||||||
| def impact_calc_data(self) -> dict: | ||||||||||||||||||||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make clear that this can be plugged in as kwargs?
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
| """Convenience function for ImpactCalc class.""" | ||||||||||||||||||||||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||||||||||||||||||||||
| "exposures": self.exposure, | ||||||||||||||||||||||||||||||||||||||||||||||
| "hazard": self.hazard, | ||||||||||||||||||||||||||||||||||||||||||||||
| "impfset": self.impfset, | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| @staticmethod | ||||||||||||||||||||||||||||||||||||||||||||||
| def _convert_to_date(date_arg) -> datetime.date: | ||||||||||||||||||||||||||||||||||||||||||||||
| """Convert date argument of type int or str to a datetime.date object.""" | ||||||||||||||||||||||||||||||||||||||||||||||
| if isinstance(date_arg, int): | ||||||||||||||||||||||||||||||||||||||||||||||
| # Assume the integer represents a year | ||||||||||||||||||||||||||||||||||||||||||||||
| return datetime.date(date_arg, 1, 1) | ||||||||||||||||||||||||||||||||||||||||||||||
| elif isinstance(date_arg, str): | ||||||||||||||||||||||||||||||||||||||||||||||
| # Try to parse the string as a date | ||||||||||||||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||||||||||||||
| return datetime.datetime.strptime(date_arg, "%Y-%m-%d").date() | ||||||||||||||||||||||||||||||||||||||||||||||
| except ValueError: | ||||||||||||||||||||||||||||||||||||||||||||||
| raise ValueError("String must be in the format 'YYYY-MM-DD'") | ||||||||||||||||||||||||||||||||||||||||||||||
| elif isinstance(date_arg, datetime.date): | ||||||||||||||||||||||||||||||||||||||||||||||
| # Already a date object | ||||||||||||||||||||||||||||||||||||||||||||||
| return date_arg | ||||||||||||||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||||||||||||||
| raise TypeError("date_arg must be an int, str, or datetime.date") | ||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||
| elif isinstance(date_arg, str): | |
| # Try to parse the string as a date | |
| try: | |
| return datetime.datetime.strptime(date_arg, "%Y-%m-%d").date() | |
| except ValueError: | |
| raise ValueError("String must be in the format 'YYYY-MM-DD'") | |
| elif isinstance(date_arg, datetime.date): | |
| # Already a date object | |
| return date_arg | |
| else: | |
| raise TypeError("date_arg must be an int, str, or datetime.date") | |
| if isinstance(date_arg, str): | |
| # Try to parse the string as a date | |
| try: | |
| return datetime.date.fromisoformat(date_arg) | |
| except ValueError: | |
| raise ValueError("String must be in the format 'YYYY-MM-DD'") | |
| if isinstance(date_arg, datetime.date): | |
| # Already a date object | |
| return date_arg | |
| raise TypeError("date_arg must be an int, str, or datetime.date") |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not use format strings in logger messages, let logger format the string. See https://pylint.pycqa.org/en/latest/user_guide/messages/warning/logging-fstring-interpolation.html
peanutfun marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
spjuhel marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,132 @@ | ||||||
| import datetime | ||||||
| import unittest | ||||||
| from unittest.mock import MagicMock | ||||||
|
|
||||||
| import numpy as np | ||||||
| import pandas as pd | ||||||
|
|
||||||
| from climada.entity.exposures import Exposures | ||||||
| from climada.entity.impact_funcs import ImpactFunc, ImpactFuncSet | ||||||
| from climada.entity.measures.base import Measure | ||||||
| from climada.hazard import Hazard | ||||||
| from climada.trajectories.snapshot import Snapshot | ||||||
| from climada.util.constants import EXP_DEMO_H5, HAZ_DEMO_H5 | ||||||
|
|
||||||
|
|
||||||
| class TestSnapshot(unittest.TestCase): | ||||||
|
|
||||||
| def setUp(self): | ||||||
| # Create mock objects for testing | ||||||
| self.mock_exposure = Exposures.from_hdf5(EXP_DEMO_H5) | ||||||
| self.mock_hazard = Hazard.from_hdf5(HAZ_DEMO_H5) | ||||||
| self.mock_impfset = ImpactFuncSet( | ||||||
| [ | ||||||
| ImpactFunc( | ||||||
| "TC", | ||||||
| 3, | ||||||
| intensity=np.array([0, 20]), | ||||||
| mdd=np.array([0, 0.5]), | ||||||
| paa=np.array([0, 1]), | ||||||
| ) | ||||||
| ] | ||||||
| ) | ||||||
| self.mock_measure = MagicMock(spec=Measure) | ||||||
| self.mock_measure.name = "Test Measure" | ||||||
|
|
||||||
| # Setup mock return values for measure.apply | ||||||
| self.mock_modified_exposure = MagicMock(spec=Exposures) | ||||||
| self.mock_modified_hazard = MagicMock(spec=Hazard) | ||||||
| self.mock_modified_impfset = MagicMock(spec=ImpactFuncSet) | ||||||
| self.mock_measure.apply.return_value = ( | ||||||
| self.mock_modified_exposure, | ||||||
| self.mock_modified_impfset, | ||||||
| self.mock_modified_hazard, | ||||||
| ) | ||||||
|
|
||||||
| def test_init_with_int_date(self): | ||||||
| snapshot = Snapshot( | ||||||
| exposure=self.mock_exposure, | ||||||
| hazard=self.mock_hazard, | ||||||
| impfset=self.mock_impfset, | ||||||
| date=2023, | ||||||
| ) | ||||||
| self.assertEqual(snapshot.date, datetime.date(2023, 1, 1)) | ||||||
|
||||||
|
|
||||||
| def test_init_with_str_date(self): | ||||||
| snapshot = Snapshot( | ||||||
| exposure=self.mock_exposure, | ||||||
| hazard=self.mock_hazard, | ||||||
| impfset=self.mock_impfset, | ||||||
| date="2023-01-01", | ||||||
| ) | ||||||
| self.assertEqual(snapshot.date, datetime.date(2023, 1, 1)) | ||||||
|
|
||||||
| def test_init_with_date_object(self): | ||||||
| date_obj = datetime.date(2023, 1, 1) | ||||||
| snapshot = Snapshot( | ||||||
| exposure=self.mock_exposure, | ||||||
| hazard=self.mock_hazard, | ||||||
| impfset=self.mock_impfset, | ||||||
| date=date_obj, | ||||||
| ) | ||||||
| self.assertEqual(snapshot.date, date_obj) | ||||||
|
|
||||||
| def test_init_with_invalid_date(self): | ||||||
| with self.assertRaises(ValueError): | ||||||
spjuhel marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| Snapshot( | ||||||
| exposure=self.mock_exposure, | ||||||
| hazard=self.mock_hazard, | ||||||
| impfset=self.mock_impfset, | ||||||
| date="invalid-date", | ||||||
| ) | ||||||
|
|
||||||
| def test_init_with_invalid_type(self): | ||||||
| with self.assertRaises(TypeError): | ||||||
spjuhel marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| Snapshot( | ||||||
| exposure=self.mock_exposure, | ||||||
| hazard=self.mock_hazard, | ||||||
| impfset=self.mock_impfset, | ||||||
| date=2023.5, # type: ignore | ||||||
| ) | ||||||
|
|
||||||
| def test_properties(self): | ||||||
| snapshot = Snapshot( | ||||||
| exposure=self.mock_exposure, | ||||||
| hazard=self.mock_hazard, | ||||||
| impfset=self.mock_impfset, | ||||||
| date=2023, | ||||||
| ) | ||||||
|
|
||||||
| # We want a new reference | ||||||
| self.assertIsNot(snapshot.exposure, self.mock_exposure) | ||||||
| self.assertIsNot(snapshot.hazard, self.mock_hazard) | ||||||
| self.assertIsNot(snapshot.impfset, self.mock_impfset) | ||||||
|
|
||||||
| # But we want equality | ||||||
| pd.testing.assert_frame_equal(snapshot.exposure.gdf, self.mock_exposure.gdf) | ||||||
|
|
||||||
| self.assertEqual(snapshot.hazard.haz_type, self.mock_hazard.haz_type) | ||||||
| self.assertEqual(snapshot.hazard.intensity.nnz, self.mock_hazard.intensity.nnz) | ||||||
|
||||||
| self.assertEqual(snapshot.hazard.intensity.nnz, self.mock_hazard.intensity.nnz) | |
| np.testing.assert_array_equal(snapshot.hazard.intensity.toarray(), self.mock_hazard.intensity.toarray()) |
Uh oh!
There was an error while loading. Please reload this page.