-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathentity_def.py
More file actions
executable file
·185 lines (154 loc) · 6.03 KB
/
entity_def.py
File metadata and controls
executable file
·185 lines (154 loc) · 6.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
"""
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/>.
---
Define Entity Class.
"""
__all__ = ["Entity"]
import logging
from typing import Optional
import pandas as pd
from climada.entity._legacy_measures.measure_set import Measure, MeasureSet
from climada.entity.disc_rates.base import DiscRates
from climada.entity.exposures.base import Exposures
from climada.entity.impact_funcs.impact_func_set import ImpactFuncSet
LOGGER = logging.getLogger(__name__)
class Entity:
"""Collects exposures, impact functions, measures and discount rates.
Default values set when empty constructor.
Attributes
----------
exposures : Exposures
exposures
impact_funcs : ImpactFuncSet
impact functions set
measures : MeasureSet
measures
disc_rates : DiscRates
discount rates
def_file : str
Default file from configuration file
"""
def __init__(
self,
exposures: Optional[Exposures] = None,
disc_rates: Optional[DiscRates] = None,
impact_func_set: Optional[ImpactFuncSet] = None,
measure_set: Optional[MeasureSet] = None,
):
"""
Initialize entity
Parameters
----------
exposures : climada.entity.Exposures, optional
Exposures of the entity. The default is None (empty Exposures()).
disc_rates : climada.entity.DiscRates, optional
Disc rates of the entity. The default is None (empty DiscRates()).
impact_func_set : climada.entity.ImpactFuncSet, optional
The impact function set. The default is None (empty ImpactFuncSet()).
measure_set : climada.entity.Measures, optional
The measures. The default is None (empty MeasuresSet().
"""
self.exposures = Exposures() if exposures is None else exposures
self.disc_rates = DiscRates() if disc_rates is None else disc_rates
self.impact_funcs = (
ImpactFuncSet() if impact_func_set is None else impact_func_set
)
self.measures = MeasureSet() if measure_set is None else measure_set
@classmethod
def from_mat(cls, file_name):
"""Read MATLAB file of climada.
Parameters
----------
file_name : str, optional
file name(s) or folder name
containing the files to read
Returns
-------
ent : climada.entity.Entity
The entity from matlab file
"""
return cls(
exposures=Exposures.from_mat(file_name),
disc_rates=DiscRates.from_mat(file_name),
impact_func_set=ImpactFuncSet.from_mat(file_name),
measure_set=MeasureSet.from_mat(file_name),
)
def read_mat(self, *args, **kwargs):
"""This function is deprecated, use Entity.from_mat instead."""
LOGGER.warning(
"The use of Entity.read_mat is deprecated." "Use Entity.from_mat instead."
)
self.__dict__ = Entity.from_mat(*args, **kwargs).__dict__
@classmethod
def from_excel(cls, file_name):
"""Read csv or xls or xlsx file following climada's template.
Parameters
----------
file_name : str, optional
file name(s) or folder name
containing the files to read
description : str or list(str), optional
one description of the
data or a description of each data file
Returns
-------
ent : climada.entity.Entity
The entity from excel file
"""
exp = Exposures(pd.read_excel(file_name))
disc_rates = DiscRates.from_excel(file_name)
impf_set = ImpactFuncSet.from_excel(file_name)
meas_set = MeasureSet.from_excel(file_name)
return cls(
exposures=exp,
disc_rates=disc_rates,
impact_func_set=impf_set,
measure_set=meas_set,
)
def read_excel(self, *args, **kwargs):
"""This function is deprecated, use Entity.from_excel instead."""
LOGGER.warning(
"The use of Entity.read_excel is deprecated."
" Use Entity.from_excel instead."
)
self.__dict__ = Entity.from_excel(*args, **kwargs).__dict__
def write_excel(self, file_name):
"""Write excel file following template."""
self.exposures.gdf.to_excel(file_name)
self.impact_funcs.write_excel(file_name)
self.measures.write_excel(file_name)
self.disc_rates.write_excel(file_name)
def check(self):
"""Check instance attributes.
Raises
------
ValueError
"""
self.disc_rates.check()
self.exposures.check()
self.impact_funcs.check()
self.measures.check()
def __setattr__(self, name, value):
"""Check input type before set"""
if name == "exposures":
if not isinstance(value, Exposures):
raise ValueError("Input value is not (sub)class of Exposures.")
elif name == "impact_funcs":
if not isinstance(value, ImpactFuncSet):
raise ValueError("Input value is not (sub)class of ImpactFuncSet.")
elif name == "measures":
if not isinstance(value, MeasureSet):
raise ValueError("Input value is not (sub)class of MeasureSet.")
elif name == "disc_rates":
if not isinstance(value, DiscRates):
raise ValueError("Input value is not (sub)class of DiscRates.")
super().__setattr__(name, value)