-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
538 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
openfisca_core/parameters/vectorial_asof_date_parameter_node_at_instant.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import numpy | ||
|
||
from openfisca_core.parameters.parameter_node_at_instant import ParameterNodeAtInstant | ||
from openfisca_core.parameters.vectorial_parameter_node_at_instant import VectorialParameterNodeAtInstant | ||
|
||
|
||
class VectorialAsofDateParameterNodeAtInstant(VectorialParameterNodeAtInstant): | ||
""" | ||
Parameter node of the legislation at a given instant which has been vectorized along some date. | ||
Vectorized parameters allow requests such as parameters.housing_benefit[date], where date is a np.datetime64 type vector | ||
""" | ||
|
||
@staticmethod | ||
def build_from_node(node): | ||
VectorialParameterNodeAtInstant.check_node_vectorisable(node) | ||
subnodes_name = node._children.keys() | ||
# Recursively vectorize the children of the node | ||
vectorial_subnodes = tuple([ | ||
VectorialAsofDateParameterNodeAtInstant.build_from_node(node[subnode_name]).vector | ||
if isinstance(node[subnode_name], ParameterNodeAtInstant) | ||
else node[subnode_name] | ||
for subnode_name in subnodes_name | ||
]) | ||
# A vectorial node is a wrapper around a numpy recarray | ||
# We first build the recarray | ||
recarray = numpy.array( | ||
[vectorial_subnodes], | ||
dtype=[ | ||
(subnode_name, subnode.dtype if isinstance(subnode, numpy.recarray) else 'float') | ||
for (subnode_name, subnode) in zip(subnodes_name, vectorial_subnodes) | ||
] | ||
) | ||
return VectorialAsofDateParameterNodeAtInstant(node._name, recarray.view(numpy.recarray), node._instant_str) | ||
|
||
def __getitem__(self, key): | ||
# If the key is a string, just get the subnode | ||
if isinstance(key, str): | ||
key = numpy.array([key], dtype='datetime64[D]') | ||
return self.__getattr__(key) | ||
# If the key is a vector, e.g. ['1990-11-25', '1983-04-17', '1969-09-09'] | ||
elif isinstance(key, numpy.ndarray): | ||
assert numpy.issubdtype(key.dtype, numpy.datetime64) | ||
names = list(self.dtype.names) # Get all the names of the subnodes, e.g. ['before_X', 'after_X', 'after_Y'] | ||
values = numpy.asarray([value for value in self.vector[0]]) | ||
names = [ | ||
name | ||
for name in names | ||
if not name.startswith("before") | ||
] | ||
names = [ | ||
numpy.datetime64( | ||
"-".join(name[len("after_"):].split("_")) | ||
) | ||
for name in names | ||
] | ||
conditions = sum([ | ||
name <= key | ||
for name in names | ||
]) | ||
result = values[conditions] | ||
|
||
# If the result is not a leaf, wrap the result in a vectorial node. | ||
if numpy.issubdtype(result.dtype, numpy.record) or numpy.issubdtype(result.dtype, numpy.void): | ||
return VectorialAsofDateParameterNodeAtInstant(self._name, result.view(numpy.recarray), self._instant_str) | ||
|
||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
description: Full rate age | ||
full_rate_age_by_birthdate: | ||
description: Full rate age by birthdate | ||
before_1951_07_01: | ||
description: Born before 01/07/1951 | ||
year: | ||
description: Year | ||
values: | ||
1983-04-01: | ||
value: 65.0 | ||
month: | ||
description: Month | ||
values: | ||
1983-04-01: | ||
value: 0.0 | ||
after_1951_07_01: | ||
description: Born after 01/07/1951 | ||
year: | ||
description: Year | ||
values: | ||
2011-07-01: | ||
value: 65.0 | ||
1983-04-01: | ||
value: null | ||
month: | ||
description: Month | ||
values: | ||
2011-07-01: | ||
value: 4.0 | ||
1983-04-01: | ||
value: null | ||
after_1952_01_01: | ||
description: Born after 01/01/1952 | ||
year: | ||
description: Year | ||
values: | ||
2011-07-01: | ||
value: 65.0 | ||
1983-04-01: | ||
value: null | ||
month: | ||
description: Month | ||
values: | ||
2012-01-01: | ||
value: 9.0 | ||
2011-07-01: | ||
value: 8.0 | ||
1983-04-01: | ||
value: null | ||
after_1953_01_01: | ||
description: Born after 01/01/1953 | ||
year: | ||
description: Year | ||
values: | ||
2011-07-01: | ||
value: 66.0 | ||
1983-04-01: | ||
value: null | ||
month: | ||
description: Month | ||
values: | ||
2012-01-01: | ||
value: 2.0 | ||
2011-07-01: | ||
value: 0.0 | ||
1983-04-01: | ||
value: null | ||
after_1954_01_01: | ||
description: Born after 01/01/1954 | ||
year: | ||
description: Year | ||
values: | ||
2011-07-01: | ||
value: 66.0 | ||
1983-04-01: | ||
value: null | ||
month: | ||
description: Month | ||
values: | ||
2012-01-01: | ||
value: 7.0 | ||
2011-07-01: | ||
value: 4.0 | ||
1983-04-01: | ||
value: null | ||
after_1955_01_01: | ||
description: Born after 01/01/1955 | ||
year: | ||
description: Year | ||
values: | ||
2012-01-01: | ||
value: 67.0 | ||
2011-07-01: | ||
value: 66.0 | ||
1983-04-01: | ||
value: null | ||
month: | ||
description: Month | ||
values: | ||
2012-01-01: | ||
value: 0.0 | ||
2011-07-01: | ||
value: 8.0 | ||
1983-04-01: | ||
value: null | ||
after_1956_01_01: | ||
description: Born after 01/01/1956 | ||
year: | ||
description: Year | ||
values: | ||
2011-07-01: | ||
value: 67.0 | ||
1983-04-01: | ||
value: null | ||
month: | ||
description: Month | ||
values: | ||
2011-07-01: | ||
value: 0.0 | ||
1983-04-01: | ||
value: null |
Oops, something went wrong.