-
Notifications
You must be signed in to change notification settings - Fork 1
/
model_api.py
327 lines (255 loc) · 9.53 KB
/
model_api.py
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import logging
from turtle import pd
from openfisca_core.model_api import (
DAY,
MONTH,
YEAR,
ETERNITY,
Variable as CoreVariable,
Reform,
max_,
min_,
)
from openfisca_core.populations import Population
from openfisca_core.entities import Entity
from typing import Callable, List, Tuple, Type, Union
import numpy as np
from numpy.typing import ArrayLike
from pandas import Period
from itertools import product
ReformType = Union[Reform, Tuple[Reform]]
allowed_variable_attributes = ("metadata", "quantity_type")
STOCK = "Stock"
FLOW = "Flow"
class Variable(CoreVariable):
quantity_type: str = FLOW
def __init__(self, baseline_variable=None):
try:
CoreVariable.__init__(self, baseline_variable=baseline_variable)
except ValueError as e:
if all(
[
attribute not in str(e)
for attribute in allowed_variable_attributes
]
):
raise e
self.is_neutralized = False
np.random.seed(0)
def for_each_variable(
entity: Population,
period: Period,
variables: List[str],
agg_func: str = "add",
group_agg_func: str = "add",
options: List[str] = None,
) -> ArrayLike:
"""Applies operations to lists of variables.
Args:
entity (Population): The entity population, as passed in formulas.
period (Period): The period, as pass in formulas.
variables (List[str]): A list of variable names.
agg_func (str, optional): The operation to apply to combine variable results. Defaults to "add".
group_agg_func (str, optional): The operation to apply to transform values to the target entity level. Defaults to "add".
options (List[str], optional): Options to pass to the `entity(variable, period)` call. Defaults to None.
Raises:
ValueError: If any target variable is not at or below the target entity level.
Returns:
ArrayLike: The result of the operation.
"""
result = None
agg_func = dict(
add=lambda x, y: x + y, multiply=lambda x, y: x * y, max=max_, min=min_
)[agg_func]
if not entity.entity.is_person:
group_agg_func = dict(
add=entity.sum, all=entity.all, max=entity.max, min=entity.min
)[group_agg_func]
for variable in variables:
variable_entity = entity.entity.get_variable(variable).entity
if variable_entity.key == entity.entity.key:
values = entity(variable, period, options=options)
else:
try:
values = group_agg_func(
entity.members(variable, period, options=options)
)
except Exception as e:
raise ValueError(
f"Variable {variable} is not defined for {entity.entity.label} or {entity.entity.label} members: {e}"
)
if result is None:
result = values
else:
result = agg_func(result, values)
return result
def add(
entity: Population,
period: Period,
variables: List[str],
options: List[str] = None,
):
"""Sums a list of variables.
Args:
entity (Population): The entity population, as passed in formulas.
period (Period): The period, as pass in formulas.
variables (List[str]): A list of variable names.
options (List[str], optional): Options to pass to the `entity(variable, period)` call. Defaults to None.
Raises:
ValueError: If any target variable is not at or below the target entity level.
Returns:
ArrayLike: The result of the operation.
"""
return for_each_variable(
entity, period, variables, agg_func="add", options=options
)
def aggr(entity, period, variables, options=None):
"""Sums a list of variables belonging to entity members.
Args:
entity (Population): The entity population, as passed in formulas.
period (Period): The period, as pass in formulas.
variables (List[str]): A list of variable names.
options (List[str], optional): Options to pass to the `entity(variable, period)` call. Defaults to None.
Raises:
ValueError: If any target variable is not below the target entity level.
Returns:
ArrayLike: The result of the operation.
"""
return for_each_variable(
entity,
period,
variables,
agg_func="add",
group_agg_func="add",
options=options,
)
def and_(
entity: Population,
period: Period,
variables: List[str],
options: List[str] = None,
):
"""Performs a logical and operation on a list of variables.
Args:
entity (Population): The entity population, as passed in formulas.
period (Period): The period, as pass in formulas.
variables (List[str]): A list of variable names.
options (List[str], optional): Options to pass to the `entity(variable, period)` call. Defaults to None.
Raises:
ValueError: If any target variable is not at the target entity level.
Returns:
ArrayLike: The result of the operation.
"""
return for_each_variable(
entity, period, variables, agg_func="multiply", options=options
)
or_ = add
any_ = or_
multiply = and_
select = np.select
clip = np.clip
inf = np.inf
WEEKS_IN_YEAR = 52
MONTHS_IN_YEAR = 12
def amount_over(amount: ArrayLike, threshold: float) -> ArrayLike:
"""Calculates the amounts over a threshold.
Args:
amount (ArrayLike): The amount to calculate for.
threshold_1 (float): The threshold.
Returns:
ArrayLike: The amounts over the threshold.
"""
logging.debug(
"amount_over(x, y) is deprecated, use max_(x - y, 0) instead."
)
return max_(0, amount - threshold)
def amount_between(
amount: ArrayLike, threshold_1: float, threshold_2: float
) -> ArrayLike:
"""Calculates the amounts between two thresholds.
Args:
amount (ArrayLike): The amount to calculate for.
threshold_1 (float): The lower threshold.
threshold_2 (float): The upper threshold.
Returns:
ArrayLike: The amounts between the thresholds.
"""
return clip(amount, threshold_1, threshold_2) - threshold_1
def random(entity, reset=True):
x = np.random.rand(entity.count)
if reset:
np.random.seed(0)
return x
def is_in(values: ArrayLike, *targets: list) -> ArrayLike:
"""Returns true if the value is in the list of targets.
Args:
values (ArrayLike): The values to test.
Returns:
ArrayLike: True if the value is in the list of targets.
"""
if (len(targets) == 1) and isinstance(targets[0], list):
targets = targets[0]
return np.any([values == target for target in targets], axis=0)
def between(
values: ArrayLike, lower: float, upper: float, inclusive: str = "both"
) -> ArrayLike:
"""Returns true if values are between lower and upper.
Args:
values (ArrayLike): The input array.
lower (float): The lower bound.
upper (float): The upper bound.
inclusive (bool, optional): Whether to include or exclude the bounds. Defaults to True.
Returns:
ArrayLike: The resulting array.
"""
return pd.Series(values).between(lower, upper, inclusive=inclusive)
def uprated(by: str = None, start_year: int = 2015) -> Callable:
"""Attaches a formula applying an uprating factor to input variables (going back as far as 2015).
Args:
by (str, optional): The name of the parameter (under parameters.uprating). Defaults to None (no uprating applied).
Returns:
Callable: A class decorator.
"""
def uprater(variable: Type[Variable]) -> type:
if hasattr(variable, f"formula_{start_year}"):
return variable
formula = variable.formula if hasattr(variable, "formula") else None
def formula_start_year(entity, period, parameters):
if by is None:
return entity(variable.__name__, period.last_year)
else:
uprating = (
parameters(period).uprating[by]
/ parameters(period.last_year).uprating[by]
)
old = entity(variable.__name__, period.last_year)
if (formula is not None) and (all(old) == 0):
# If no values have been inputted, don't uprate and
# instead use the previous formula on the current period.
return formula(entity, period, parameters)
return uprating * old
formula_start_year.__name__ = f"formula_{start_year}"
setattr(variable, formula_start_year.__name__, formula_start_year)
return variable
return uprater
def carried_over(variable: type) -> type:
return uprated()(variable)
def sum_of_variables(variables: Union[List[str], str]) -> Callable:
"""Returns a function that sums the values of a list of variables.
Args:
variables (Union[List[str], str]): A list of variable names.
Returns:
Callable: A function that sums the values of the variables.
"""
def sum_of_variables(entity, period, parameters):
if isinstance(variables, str):
# A string parameter name is passed
node = parameters(period)
for name in variables.split("."):
node = getattr(node, name)
variable_names = node
else:
variable_names = variables
return add(entity, period, variable_names)
return sum_of_variables
any_of_variables = sum_of_variables