flood-adapt 0.3.9__py3-none-any.whl → 0.3.10__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- flood_adapt/__init__.py +26 -22
- flood_adapt/adapter/__init__.py +9 -9
- flood_adapt/adapter/fiat_adapter.py +1541 -1541
- flood_adapt/adapter/interface/hazard_adapter.py +70 -70
- flood_adapt/adapter/interface/impact_adapter.py +36 -36
- flood_adapt/adapter/interface/model_adapter.py +89 -89
- flood_adapt/adapter/interface/offshore.py +19 -19
- flood_adapt/adapter/sfincs_adapter.py +1848 -1848
- flood_adapt/adapter/sfincs_offshore.py +193 -193
- flood_adapt/config/config.py +248 -248
- flood_adapt/config/fiat.py +219 -219
- flood_adapt/config/gui.py +331 -331
- flood_adapt/config/sfincs.py +481 -336
- flood_adapt/config/site.py +129 -129
- flood_adapt/database_builder/database_builder.py +2210 -2210
- flood_adapt/database_builder/templates/default_units/imperial.toml +9 -9
- flood_adapt/database_builder/templates/default_units/metric.toml +9 -9
- flood_adapt/database_builder/templates/green_infra_table/green_infra_lookup_table.csv +10 -10
- flood_adapt/database_builder/templates/infographics/OSM/config_charts.toml +90 -90
- flood_adapt/database_builder/templates/infographics/OSM/config_people.toml +57 -57
- flood_adapt/database_builder/templates/infographics/OSM/config_risk_charts.toml +121 -121
- flood_adapt/database_builder/templates/infographics/OSM/config_roads.toml +65 -65
- flood_adapt/database_builder/templates/infographics/OSM/styles.css +45 -45
- flood_adapt/database_builder/templates/infographics/US_NSI/config_charts.toml +126 -126
- flood_adapt/database_builder/templates/infographics/US_NSI/config_people.toml +60 -60
- flood_adapt/database_builder/templates/infographics/US_NSI/config_risk_charts.toml +121 -121
- flood_adapt/database_builder/templates/infographics/US_NSI/config_roads.toml +65 -65
- flood_adapt/database_builder/templates/infographics/US_NSI/styles.css +45 -45
- flood_adapt/database_builder/templates/infometrics/OSM/metrics_additional_risk_configs.toml +4 -4
- flood_adapt/database_builder/templates/infometrics/OSM/with_SVI/infographic_metrics_config.toml +143 -143
- flood_adapt/database_builder/templates/infometrics/OSM/with_SVI/infographic_metrics_config_risk.toml +153 -153
- flood_adapt/database_builder/templates/infometrics/OSM/without_SVI/infographic_metrics_config.toml +127 -127
- flood_adapt/database_builder/templates/infometrics/OSM/without_SVI/infographic_metrics_config_risk.toml +57 -57
- flood_adapt/database_builder/templates/infometrics/US_NSI/metrics_additional_risk_configs.toml +4 -4
- flood_adapt/database_builder/templates/infometrics/US_NSI/with_SVI/infographic_metrics_config.toml +191 -191
- flood_adapt/database_builder/templates/infometrics/US_NSI/with_SVI/infographic_metrics_config_risk.toml +153 -153
- flood_adapt/database_builder/templates/infometrics/US_NSI/without_SVI/infographic_metrics_config.toml +178 -178
- flood_adapt/database_builder/templates/infometrics/US_NSI/without_SVI/infographic_metrics_config_risk.toml +57 -57
- flood_adapt/database_builder/templates/infometrics/mandatory_metrics_config.toml +9 -9
- flood_adapt/database_builder/templates/infometrics/mandatory_metrics_config_risk.toml +65 -65
- flood_adapt/database_builder/templates/output_layers/bin_colors.toml +5 -5
- flood_adapt/database_builder.py +16 -16
- flood_adapt/dbs_classes/__init__.py +21 -21
- flood_adapt/dbs_classes/database.py +495 -684
- flood_adapt/dbs_classes/dbs_benefit.py +77 -76
- flood_adapt/dbs_classes/dbs_event.py +61 -59
- flood_adapt/dbs_classes/dbs_measure.py +112 -111
- flood_adapt/dbs_classes/dbs_projection.py +34 -34
- flood_adapt/dbs_classes/dbs_scenario.py +137 -137
- flood_adapt/dbs_classes/dbs_static.py +274 -273
- flood_adapt/dbs_classes/dbs_strategy.py +130 -129
- flood_adapt/dbs_classes/dbs_template.py +279 -278
- flood_adapt/dbs_classes/interface/database.py +107 -139
- flood_adapt/dbs_classes/interface/element.py +121 -121
- flood_adapt/dbs_classes/interface/static.py +47 -47
- flood_adapt/flood_adapt.py +1207 -1178
- flood_adapt/misc/database_user.py +16 -16
- flood_adapt/misc/exceptions.py +22 -0
- flood_adapt/misc/log.py +183 -183
- flood_adapt/misc/path_builder.py +54 -54
- flood_adapt/misc/utils.py +185 -185
- flood_adapt/objects/__init__.py +82 -82
- flood_adapt/objects/benefits/benefits.py +61 -61
- flood_adapt/objects/events/event_factory.py +135 -135
- flood_adapt/objects/events/event_set.py +88 -84
- flood_adapt/objects/events/events.py +234 -234
- flood_adapt/objects/events/historical.py +58 -58
- flood_adapt/objects/events/hurricane.py +68 -67
- flood_adapt/objects/events/synthetic.py +46 -50
- flood_adapt/objects/forcing/__init__.py +92 -92
- flood_adapt/objects/forcing/csv.py +68 -68
- flood_adapt/objects/forcing/discharge.py +66 -66
- flood_adapt/objects/forcing/forcing.py +150 -150
- flood_adapt/objects/forcing/forcing_factory.py +182 -182
- flood_adapt/objects/forcing/meteo_handler.py +93 -93
- flood_adapt/objects/forcing/netcdf.py +40 -40
- flood_adapt/objects/forcing/plotting.py +453 -429
- flood_adapt/objects/forcing/rainfall.py +98 -98
- flood_adapt/objects/forcing/tide_gauge.py +191 -191
- flood_adapt/objects/forcing/time_frame.py +90 -90
- flood_adapt/objects/forcing/timeseries.py +564 -564
- flood_adapt/objects/forcing/unit_system.py +580 -580
- flood_adapt/objects/forcing/waterlevels.py +108 -108
- flood_adapt/objects/forcing/wind.py +124 -124
- flood_adapt/objects/measures/measure_factory.py +92 -92
- flood_adapt/objects/measures/measures.py +529 -529
- flood_adapt/objects/object_model.py +74 -68
- flood_adapt/objects/projections/projections.py +103 -103
- flood_adapt/objects/scenarios/scenarios.py +22 -22
- flood_adapt/objects/strategies/strategies.py +89 -89
- flood_adapt/workflows/benefit_runner.py +579 -554
- flood_adapt/workflows/floodmap.py +85 -85
- flood_adapt/workflows/impacts_integrator.py +85 -85
- flood_adapt/workflows/scenario_runner.py +70 -70
- {flood_adapt-0.3.9.dist-info → flood_adapt-0.3.10.dist-info}/LICENSE +674 -674
- {flood_adapt-0.3.9.dist-info → flood_adapt-0.3.10.dist-info}/METADATA +866 -865
- flood_adapt-0.3.10.dist-info/RECORD +140 -0
- flood_adapt-0.3.9.dist-info/RECORD +0 -139
- {flood_adapt-0.3.9.dist-info → flood_adapt-0.3.10.dist-info}/WHEEL +0 -0
- {flood_adapt-0.3.9.dist-info → flood_adapt-0.3.10.dist-info}/top_level.txt +0 -0
|
@@ -1,135 +1,135 @@
|
|
|
1
|
-
from pathlib import Path
|
|
2
|
-
from typing import Any, List
|
|
3
|
-
|
|
4
|
-
import tomli
|
|
5
|
-
|
|
6
|
-
from flood_adapt.objects.events.event_set import EventSet
|
|
7
|
-
from flood_adapt.objects.events.events import (
|
|
8
|
-
Event,
|
|
9
|
-
Mode,
|
|
10
|
-
Template,
|
|
11
|
-
)
|
|
12
|
-
from flood_adapt.objects.events.historical import HistoricalEvent
|
|
13
|
-
from flood_adapt.objects.events.hurricane import (
|
|
14
|
-
HurricaneEvent,
|
|
15
|
-
TranslationModel,
|
|
16
|
-
)
|
|
17
|
-
from flood_adapt.objects.events.synthetic import SyntheticEvent
|
|
18
|
-
|
|
19
|
-
__all__ = ["TranslationModel", "EventSet"]
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
class EventFactory:
|
|
23
|
-
"""Factory class for creating events.
|
|
24
|
-
|
|
25
|
-
This class is used to create events based on a template.
|
|
26
|
-
|
|
27
|
-
Attributes
|
|
28
|
-
----------
|
|
29
|
-
_EVENT_TEMPLATES : dict[str, (Event, Event)]
|
|
30
|
-
Dictionary mapping event templates to event classes and models
|
|
31
|
-
"""
|
|
32
|
-
|
|
33
|
-
_EVENT_TEMPLATES = {
|
|
34
|
-
Template.Hurricane: HurricaneEvent,
|
|
35
|
-
Template.Historical: HistoricalEvent,
|
|
36
|
-
Template.Synthetic: SyntheticEvent,
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
@staticmethod
|
|
40
|
-
def get_event_from_template(template: Template) -> type[Event]:
|
|
41
|
-
"""Get the event class corresponding to the template.
|
|
42
|
-
|
|
43
|
-
Parameters
|
|
44
|
-
----------
|
|
45
|
-
template : str
|
|
46
|
-
Name of the event template
|
|
47
|
-
|
|
48
|
-
Returns
|
|
49
|
-
-------
|
|
50
|
-
Type[Event]
|
|
51
|
-
Event template
|
|
52
|
-
"""
|
|
53
|
-
if template not in EventFactory._EVENT_TEMPLATES:
|
|
54
|
-
raise ValueError(f"Invalid event template: {template}")
|
|
55
|
-
return EventFactory._EVENT_TEMPLATES[template]
|
|
56
|
-
|
|
57
|
-
@staticmethod
|
|
58
|
-
def read_template(filepath: Path) -> Template:
|
|
59
|
-
"""Get event template from toml file."""
|
|
60
|
-
if not filepath.exists():
|
|
61
|
-
raise FileNotFoundError(f"File not found: {filepath}")
|
|
62
|
-
with open(filepath, mode="rb") as fp:
|
|
63
|
-
toml = tomli.load(fp)
|
|
64
|
-
if (template := toml.get("template")) is None:
|
|
65
|
-
raise ValueError(f"Event template not found in {filepath}")
|
|
66
|
-
|
|
67
|
-
return Template(template)
|
|
68
|
-
|
|
69
|
-
@staticmethod
|
|
70
|
-
def read_mode(filepath: Path) -> Mode:
|
|
71
|
-
"""Get event mode from toml file."""
|
|
72
|
-
if not filepath.exists():
|
|
73
|
-
raise FileNotFoundError(f"File not found: {filepath}")
|
|
74
|
-
with open(filepath, mode="rb") as fp:
|
|
75
|
-
toml = tomli.load(fp)
|
|
76
|
-
if toml.get("mode") is None:
|
|
77
|
-
raise ValueError(f"Event mode not found in {filepath}")
|
|
78
|
-
return Mode(toml.get("mode"))
|
|
79
|
-
|
|
80
|
-
@staticmethod
|
|
81
|
-
def load_file(toml_file: Path) -> Event:
|
|
82
|
-
"""Return event object based on toml file.
|
|
83
|
-
|
|
84
|
-
Parameters
|
|
85
|
-
----------
|
|
86
|
-
toml_file : str
|
|
87
|
-
Template name
|
|
88
|
-
|
|
89
|
-
Returns
|
|
90
|
-
-------
|
|
91
|
-
Event
|
|
92
|
-
Event object
|
|
93
|
-
"""
|
|
94
|
-
mode = EventFactory.read_mode(toml_file)
|
|
95
|
-
if mode == Mode.risk:
|
|
96
|
-
|
|
97
|
-
elif mode == Mode.single_event:
|
|
98
|
-
template = Template(EventFactory.read_template(toml_file))
|
|
99
|
-
event_type = EventFactory.get_event_from_template(template)
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
@staticmethod
|
|
105
|
-
def load_dict(attrs: dict[str, Any] | Event) -> Event | EventSet:
|
|
106
|
-
"""Return event object based on attrs dict.
|
|
107
|
-
|
|
108
|
-
Parameters
|
|
109
|
-
----------
|
|
110
|
-
attrs : dict[str, Any]
|
|
111
|
-
Event attributes
|
|
112
|
-
|
|
113
|
-
Returns
|
|
114
|
-
-------
|
|
115
|
-
Event
|
|
116
|
-
Event object based on template
|
|
117
|
-
"""
|
|
118
|
-
if isinstance(attrs, Event):
|
|
119
|
-
mode = attrs.mode
|
|
120
|
-
template = attrs.template
|
|
121
|
-
else:
|
|
122
|
-
mode = Mode(attrs.get("mode"))
|
|
123
|
-
template = Template(attrs.get("template"))
|
|
124
|
-
|
|
125
|
-
if mode == Mode.risk:
|
|
126
|
-
# TODO Load events
|
|
127
|
-
return EventSet(**attrs)
|
|
128
|
-
elif mode == Mode.single_event:
|
|
129
|
-
return EventFactory.get_event_from_template(template)(**attrs)
|
|
130
|
-
else:
|
|
131
|
-
raise ValueError(f"Invalid event mode: {mode}")
|
|
132
|
-
|
|
133
|
-
@staticmethod
|
|
134
|
-
def get_allowed_forcings(template) -> dict[str, List[str]]:
|
|
135
|
-
return EventFactory.get_event_from_template(template).get_allowed_forcings()
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
from typing import Any, List
|
|
3
|
+
|
|
4
|
+
import tomli
|
|
5
|
+
|
|
6
|
+
from flood_adapt.objects.events.event_set import EventSet
|
|
7
|
+
from flood_adapt.objects.events.events import (
|
|
8
|
+
Event,
|
|
9
|
+
Mode,
|
|
10
|
+
Template,
|
|
11
|
+
)
|
|
12
|
+
from flood_adapt.objects.events.historical import HistoricalEvent
|
|
13
|
+
from flood_adapt.objects.events.hurricane import (
|
|
14
|
+
HurricaneEvent,
|
|
15
|
+
TranslationModel,
|
|
16
|
+
)
|
|
17
|
+
from flood_adapt.objects.events.synthetic import SyntheticEvent
|
|
18
|
+
|
|
19
|
+
__all__ = ["TranslationModel", "EventSet"]
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class EventFactory:
|
|
23
|
+
"""Factory class for creating events.
|
|
24
|
+
|
|
25
|
+
This class is used to create events based on a template.
|
|
26
|
+
|
|
27
|
+
Attributes
|
|
28
|
+
----------
|
|
29
|
+
_EVENT_TEMPLATES : dict[str, (Event, Event)]
|
|
30
|
+
Dictionary mapping event templates to event classes and models
|
|
31
|
+
"""
|
|
32
|
+
|
|
33
|
+
_EVENT_TEMPLATES = {
|
|
34
|
+
Template.Hurricane: HurricaneEvent,
|
|
35
|
+
Template.Historical: HistoricalEvent,
|
|
36
|
+
Template.Synthetic: SyntheticEvent,
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
@staticmethod
|
|
40
|
+
def get_event_from_template(template: Template) -> type[Event]:
|
|
41
|
+
"""Get the event class corresponding to the template.
|
|
42
|
+
|
|
43
|
+
Parameters
|
|
44
|
+
----------
|
|
45
|
+
template : str
|
|
46
|
+
Name of the event template
|
|
47
|
+
|
|
48
|
+
Returns
|
|
49
|
+
-------
|
|
50
|
+
Type[Event]
|
|
51
|
+
Event template
|
|
52
|
+
"""
|
|
53
|
+
if template not in EventFactory._EVENT_TEMPLATES:
|
|
54
|
+
raise ValueError(f"Invalid event template: {template}")
|
|
55
|
+
return EventFactory._EVENT_TEMPLATES[template]
|
|
56
|
+
|
|
57
|
+
@staticmethod
|
|
58
|
+
def read_template(filepath: Path) -> Template:
|
|
59
|
+
"""Get event template from toml file."""
|
|
60
|
+
if not filepath.exists():
|
|
61
|
+
raise FileNotFoundError(f"File not found: {filepath}")
|
|
62
|
+
with open(filepath, mode="rb") as fp:
|
|
63
|
+
toml = tomli.load(fp)
|
|
64
|
+
if (template := toml.get("template")) is None:
|
|
65
|
+
raise ValueError(f"Event template not found in {filepath}")
|
|
66
|
+
|
|
67
|
+
return Template(template)
|
|
68
|
+
|
|
69
|
+
@staticmethod
|
|
70
|
+
def read_mode(filepath: Path) -> Mode:
|
|
71
|
+
"""Get event mode from toml file."""
|
|
72
|
+
if not filepath.exists():
|
|
73
|
+
raise FileNotFoundError(f"File not found: {filepath}")
|
|
74
|
+
with open(filepath, mode="rb") as fp:
|
|
75
|
+
toml = tomli.load(fp)
|
|
76
|
+
if toml.get("mode") is None:
|
|
77
|
+
raise ValueError(f"Event mode not found in {filepath}")
|
|
78
|
+
return Mode(toml.get("mode"))
|
|
79
|
+
|
|
80
|
+
@staticmethod
|
|
81
|
+
def load_file(toml_file: Path, load_all: bool = False) -> Event | EventSet:
|
|
82
|
+
"""Return event object based on toml file.
|
|
83
|
+
|
|
84
|
+
Parameters
|
|
85
|
+
----------
|
|
86
|
+
toml_file : str
|
|
87
|
+
Template name
|
|
88
|
+
|
|
89
|
+
Returns
|
|
90
|
+
-------
|
|
91
|
+
Event
|
|
92
|
+
Event object
|
|
93
|
+
"""
|
|
94
|
+
mode = EventFactory.read_mode(toml_file)
|
|
95
|
+
if mode == Mode.risk:
|
|
96
|
+
return EventSet.load_file(toml_file, load_all=load_all)
|
|
97
|
+
elif mode == Mode.single_event:
|
|
98
|
+
template = Template(EventFactory.read_template(toml_file))
|
|
99
|
+
event_type = EventFactory.get_event_from_template(template)
|
|
100
|
+
return event_type.load_file(toml_file)
|
|
101
|
+
else:
|
|
102
|
+
raise ValueError(f"Invalid event mode: {mode}")
|
|
103
|
+
|
|
104
|
+
@staticmethod
|
|
105
|
+
def load_dict(attrs: dict[str, Any] | Event) -> Event | EventSet:
|
|
106
|
+
"""Return event object based on attrs dict.
|
|
107
|
+
|
|
108
|
+
Parameters
|
|
109
|
+
----------
|
|
110
|
+
attrs : dict[str, Any]
|
|
111
|
+
Event attributes
|
|
112
|
+
|
|
113
|
+
Returns
|
|
114
|
+
-------
|
|
115
|
+
Event
|
|
116
|
+
Event object based on template
|
|
117
|
+
"""
|
|
118
|
+
if isinstance(attrs, Event):
|
|
119
|
+
mode = attrs.mode
|
|
120
|
+
template = attrs.template
|
|
121
|
+
else:
|
|
122
|
+
mode = Mode(attrs.get("mode"))
|
|
123
|
+
template = Template(attrs.get("template"))
|
|
124
|
+
|
|
125
|
+
if mode == Mode.risk:
|
|
126
|
+
# TODO Load events
|
|
127
|
+
return EventSet(**attrs)
|
|
128
|
+
elif mode == Mode.single_event:
|
|
129
|
+
return EventFactory.get_event_from_template(template)(**attrs)
|
|
130
|
+
else:
|
|
131
|
+
raise ValueError(f"Invalid event mode: {mode}")
|
|
132
|
+
|
|
133
|
+
@staticmethod
|
|
134
|
+
def get_allowed_forcings(template) -> dict[str, List[str]]:
|
|
135
|
+
return EventFactory.get_event_from_template(template).get_allowed_forcings()
|
|
@@ -1,84 +1,88 @@
|
|
|
1
|
-
import os
|
|
2
|
-
from pathlib import Path
|
|
3
|
-
from typing import List, Optional
|
|
4
|
-
|
|
5
|
-
import tomli
|
|
6
|
-
from pydantic import BaseModel
|
|
7
|
-
|
|
8
|
-
from flood_adapt.objects.events.events import Event, Mode
|
|
9
|
-
from flood_adapt.objects.object_model import Object
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
class SubEventModel(BaseModel):
|
|
13
|
-
"""The accepted input for a sub event in FloodAdapt.
|
|
14
|
-
|
|
15
|
-
Attributes
|
|
16
|
-
----------
|
|
17
|
-
name : str
|
|
18
|
-
The name of the sub event.
|
|
19
|
-
frequency : float
|
|
20
|
-
The frequency of the sub event.
|
|
21
|
-
"""
|
|
22
|
-
|
|
23
|
-
name: str
|
|
24
|
-
frequency: float
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
class EventSet(Object):
|
|
28
|
-
"""BaseModel describing the expected variables and data types for parameters of EventSet.
|
|
29
|
-
|
|
30
|
-
An EventSet is a collection of events that can be used to create a scenario and perform a
|
|
31
|
-
|
|
32
|
-
Attributes
|
|
33
|
-
----------
|
|
34
|
-
name : str
|
|
35
|
-
The name of the event.
|
|
36
|
-
description : str
|
|
37
|
-
The description of the event. Defaults to "".
|
|
38
|
-
mode : Mode
|
|
39
|
-
The mode of the event. Defaults to Mode.risk.
|
|
40
|
-
sub_events : List[SubEventModel]
|
|
41
|
-
The sub events of the event set.
|
|
42
|
-
"""
|
|
43
|
-
|
|
44
|
-
_events: Optional[List[Event]] = None
|
|
45
|
-
|
|
46
|
-
mode: Mode = Mode.risk
|
|
47
|
-
sub_events: List[SubEventModel]
|
|
48
|
-
|
|
49
|
-
def load_sub_events(
|
|
50
|
-
self,
|
|
51
|
-
sub_events: Optional[List[Event]] = None,
|
|
52
|
-
file_path: Optional[Path] = None,
|
|
53
|
-
) -> None:
|
|
54
|
-
"""Load sub events from a list or from a file path."""
|
|
55
|
-
if sub_events is not None:
|
|
56
|
-
self._events = sub_events
|
|
57
|
-
elif file_path is not None:
|
|
58
|
-
from flood_adapt.objects.events.event_factory import EventFactory
|
|
59
|
-
|
|
60
|
-
sub_events = []
|
|
61
|
-
for sub_event in self.sub_events:
|
|
62
|
-
sub_toml = (
|
|
63
|
-
Path(file_path).parent / sub_event.name / f"{sub_event.name}.toml"
|
|
64
|
-
)
|
|
65
|
-
sub_events.append(EventFactory.load_file(sub_toml))
|
|
66
|
-
|
|
67
|
-
self._events = sub_events
|
|
68
|
-
else:
|
|
69
|
-
raise ValueError("Either `sub_events` or `file_path` must be provided.")
|
|
70
|
-
|
|
71
|
-
@classmethod
|
|
72
|
-
def load_file(
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
1
|
+
import os
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
from typing import List, Optional
|
|
4
|
+
|
|
5
|
+
import tomli
|
|
6
|
+
from pydantic import BaseModel
|
|
7
|
+
|
|
8
|
+
from flood_adapt.objects.events.events import Event, Mode
|
|
9
|
+
from flood_adapt.objects.object_model import Object
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class SubEventModel(BaseModel):
|
|
13
|
+
"""The accepted input for a sub event in FloodAdapt.
|
|
14
|
+
|
|
15
|
+
Attributes
|
|
16
|
+
----------
|
|
17
|
+
name : str
|
|
18
|
+
The name of the sub event.
|
|
19
|
+
frequency : float
|
|
20
|
+
The frequency of the sub event.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
name: str
|
|
24
|
+
frequency: float
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class EventSet(Object):
|
|
28
|
+
"""BaseModel describing the expected variables and data types for parameters of EventSet.
|
|
29
|
+
|
|
30
|
+
An EventSet is a collection of events that can be used to create a scenario and perform a probabilistic risk assessment.
|
|
31
|
+
|
|
32
|
+
Attributes
|
|
33
|
+
----------
|
|
34
|
+
name : str
|
|
35
|
+
The name of the event.
|
|
36
|
+
description : str
|
|
37
|
+
The description of the event. Defaults to "".
|
|
38
|
+
mode : Mode
|
|
39
|
+
The mode of the event. Defaults to Mode.risk.
|
|
40
|
+
sub_events : List[SubEventModel]
|
|
41
|
+
The sub events of the event set.
|
|
42
|
+
"""
|
|
43
|
+
|
|
44
|
+
_events: Optional[List[Event]] = None
|
|
45
|
+
|
|
46
|
+
mode: Mode = Mode.risk
|
|
47
|
+
sub_events: List[SubEventModel]
|
|
48
|
+
|
|
49
|
+
def load_sub_events(
|
|
50
|
+
self,
|
|
51
|
+
sub_events: Optional[List[Event]] = None,
|
|
52
|
+
file_path: Optional[Path] = None,
|
|
53
|
+
) -> None:
|
|
54
|
+
"""Load sub events from a list or from a file path."""
|
|
55
|
+
if sub_events is not None:
|
|
56
|
+
self._events = sub_events
|
|
57
|
+
elif file_path is not None:
|
|
58
|
+
from flood_adapt.objects.events.event_factory import EventFactory
|
|
59
|
+
|
|
60
|
+
sub_events = []
|
|
61
|
+
for sub_event in self.sub_events:
|
|
62
|
+
sub_toml = (
|
|
63
|
+
Path(file_path).parent / sub_event.name / f"{sub_event.name}.toml"
|
|
64
|
+
)
|
|
65
|
+
sub_events.append(EventFactory.load_file(sub_toml))
|
|
66
|
+
|
|
67
|
+
self._events = sub_events
|
|
68
|
+
else:
|
|
69
|
+
raise ValueError("Either `sub_events` or `file_path` must be provided.")
|
|
70
|
+
|
|
71
|
+
@classmethod
|
|
72
|
+
def load_file(
|
|
73
|
+
cls, file_path: Path | str | os.PathLike, load_all: bool = False
|
|
74
|
+
) -> "EventSet":
|
|
75
|
+
"""Load object from file."""
|
|
76
|
+
with open(file_path, mode="rb") as fp:
|
|
77
|
+
event_set = tomli.load(fp)
|
|
78
|
+
event_set = EventSet(**event_set)
|
|
79
|
+
if load_all:
|
|
80
|
+
# Load all sub events from the file path
|
|
81
|
+
event_set.load_sub_events(file_path=file_path)
|
|
82
|
+
return event_set
|
|
83
|
+
|
|
84
|
+
def save_additional(self, output_dir: Path | str | os.PathLike) -> None:
|
|
85
|
+
for sub_event in self._events:
|
|
86
|
+
sub_dir = Path(output_dir) / sub_event.name
|
|
87
|
+
sub_dir.mkdir(parents=True, exist_ok=True)
|
|
88
|
+
sub_event.save(sub_dir / f"{sub_event.name}.toml")
|