Skip to content
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

Switch parameters to YAML #552

Merged
merged 106 commits into from
Aug 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
106 commits
Select commit Hold shift + click to select a range
002ab4c
Move XML parser to scripts dir
Jun 30, 2017
60ae1d7
Fix dependency to XSD file
Jun 30, 2017
50bbefc
Output parameters as YAML files
Jun 30, 2017
8e6f06c
Parse YAML parameters
Jun 30, 2017
be70688
Load parameters from YAML instead of XML
Jun 30, 2017
668fcfa
Order values history
Jun 30, 2017
9f2b0db
Remove unused validation code
Jun 30, 2017
5901de7
Update internal param representation
Jun 30, 2017
2461b9f
Add .vscode to gitignore
Jul 28, 2017
4302f21
Script to transform XML files to YAML
Jul 28, 2017
70f5370
Update test_parameters.py
Jul 28, 2017
773b82b
Test YAML validation
Jul 31, 2017
4f69688
Add script to transform CountryTemplate legilation
Jul 31, 2017
02771e5
Remove parameter with_source_file_infos
Jul 31, 2017
1871074
Move function update() in class legislation.ValuesHistory()
Jul 31, 2017
1cd37b9
Deprecate reforms.update_legislation()
Jul 31, 2017
8917631
Remove compose_reforms()
Jul 31, 2017
59b7098
Test reforms
Jul 31, 2017
63a2d7f
Update Reform class (legislations)
Jul 31, 2017
bfe5eba
Add script to transform ExtensionTemplate legilation
Aug 1, 2017
e3a3e4a
Move parameters XML schema
Aug 1, 2017
2a5cd0f
The 1st XML file is not special in xml_to_json.py
Aug 1, 2017
12f2b5e
Ignore param file __root__.xml in country-template
Aug 1, 2017
01db7e9
Specify test dir in extension test
Aug 1, 2017
75e50e8
Rename to legislation_at_instant_cache
Aug 1, 2017
1df89fd
Add underscores to private function names
Aug 1, 2017
444b771
Rename to xml_to_yaml
Aug 1, 2017
a3c099c
Rename yaml_data to validated_yaml
Aug 1, 2017
613c707
Improve exceptions for YAML validation
Aug 1, 2017
8d31033
Add the class Parameter as subclass of ValuesHistory
Aug 1, 2017
daaf120
Update legislation walk of api-preview
Aug 1, 2017
b90bf51
Fix JSON schema for params
Aug 1, 2017
352e9fc
Fix xml_to_yaml script
Aug 1, 2017
c5ba88e
Add navigation API for legislation
Aug 1, 2017
86610be
Remove reforms.update_legislation
Aug 1, 2017
6a49b8b
Bump version number
Aug 2, 2017
12feef4
Update changelog
Aug 2, 2017
32cebf0
Remove `json` from variable names
Aug 2, 2017
c98e16f
Document the interface
Aug 2, 2017
2cbe663
Fix tests of legislation parameters
Aug 2, 2017
d9c8a90
Update requirements
Aug 3, 2017
1050847
Implement dynamic construction of Bracket
Aug 3, 2017
24416f1
Remove XML to YAML scripts
Aug 3, 2017
f2d2359
Scripts XML->YAML
Aug 16, 2017
d832a03
Do not use YAML key type
Aug 16, 2017
5afd8b3
Store node metadata in index.yaml
Aug 16, 2017
558ac65
Make parameters serializable
Aug 17, 2017
e7cc4bb
Cache parameters
Aug 17, 2017
21db6f8
Remove alternative constructors for parameters
Aug 17, 2017
686cf4b
Adapte LinearAverageRateTaxScale
Aug 17, 2017
06416e8
Improve a test of parameter validation
Aug 17, 2017
d3d646d
Parse dates as str in YAML, check schemas once
Aug 18, 2017
12d0f50
Dump dates without quotes in YAML
Aug 18, 2017
3022d1f
Improve doc of reforms
Aug 18, 2017
4b63d73
Do not package legislation.xsd
Aug 18, 2017
2d31a7b
Rename *legislation* to *parameters*
Aug 18, 2017
5d0851c
Rename *legislation* to *parameters*
Aug 18, 2017
9526d35
legislation.xsd
Aug 18, 2017
416deef
minor fix model_api.py
Aug 18, 2017
4710a94
Improve changelog
Aug 18, 2017
92d9be2
Make variables private
Aug 18, 2017
1d0f51a
Improve Changelog
Aug 18, 2017
b526363
Restore renamed function for deprecated OpenFisca-Parsers v.1.0.2
sandcha Aug 21, 2017
67541a6
Remove parameters cache
fpagnoux Aug 21, 2017
bef9818
Use CLoader if available
fpagnoux Aug 21, 2017
61fae36
fixup!: Remove parameters cache
fpagnoux Aug 21, 2017
36c3b6b
Fix parameter validation tests
fpagnoux Aug 21, 2017
058faae
Validate parameters
fpagnoux Aug 27, 2017
7c49f37
Validate scales and brackets
fpagnoux Aug 27, 2017
afee990
Validate ValuesAtInstant
fpagnoux Aug 27, 2017
3cb4d14
Validate value history keys
fpagnoux Aug 27, 2017
5d7289a
Clean parameters.py
fpagnoux Aug 27, 2017
c7fab85
Print YAML file path when parsing fails
fpagnoux Aug 27, 2017
aa5dfac
Pass tests
fpagnoux Aug 27, 2017
e22c2c9
Don't raise if a non YAML file is in the param dir
fpagnoux Aug 28, 2017
b21a53b
Improve error when YAML is invalid
fpagnoux Aug 28, 2017
bb5debc
Simplify parameters loading
fpagnoux Aug 28, 2017
c9533ed
Rename Node and load_file
fpagnoux Aug 28, 2017
e93141a
Remove paremeters.load_parameters
fpagnoux Aug 28, 2017
a958d20
Handle directories with load_parameter_file
fpagnoux Aug 28, 2017
0eb3840
Refactor tests
fpagnoux Aug 28, 2017
c736893
Test parameter validation
fpagnoux Aug 28, 2017
06518a0
Refactor validation
fpagnoux Aug 28, 2017
236f7de
Make '' the default name of a ParameterNode
fpagnoux Aug 28, 2017
bdf2bab
Fix extensions param loading
fpagnoux Aug 28, 2017
3e777e6
Fix ValueHistory.update
fpagnoux Aug 28, 2017
07d8782
Remove web API doc
fpagnoux Aug 28, 2017
9d5d678
Improve ref doc
fpagnoux Aug 28, 2017
3a827ad
Remove assertions
fpagnoux Aug 28, 2017
cf93d38
Expose tab_benefit_system.parameters
fpagnoux Aug 28, 2017
08fdacd
Enable parameter.update
fpagnoux Aug 28, 2017
556693d
Improve Changelog
fpagnoux Aug 28, 2017
fdff60d
Make ValueHistory repr work cross platform
fpagnoux Aug 28, 2017
7c594f0
Improve reforms ref doc
fpagnoux Aug 28, 2017
d8ef5f5
Don't accept index.yaml
fpagnoux Aug 28, 2017
9c14510
Don't use assertions
fpagnoux Aug 28, 2017
2f6e662
Remove retro-compatibility layers
fpagnoux Aug 28, 2017
74a6453
Don't rely on type names
fpagnoux Aug 28, 2017
facc774
Rename yaml_object to data
fpagnoux Aug 28, 2017
19b2dea
Enable auto-completion in node exploring
fpagnoux Aug 28, 2017
8a19ba4
Allow description for nodes and index.yaml files
fpagnoux Aug 28, 2017
39ba0b4
Make tbs.get_parameters_at_instant public
fpagnoux Aug 28, 2017
c7cf5f7
Apply renaming
fpagnoux Aug 28, 2017
f9fce0f
Fix rebase mistakes
fpagnoux Aug 28, 2017
b3456c0
Fix migration scripts
fpagnoux Aug 28, 2017
4a74f80
Upgrade country package version
fpagnoux Aug 28, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.project
.spyderproject
.pydevproject
.vscode
.settings/
build/
dist/
Expand All @@ -12,4 +13,4 @@ dist/
/.coverage
/tags
.tags*
.noseids
.noseids
194 changes: 194 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,199 @@
# Changelog

# 17.0.0 - [#552](https://github.com/openfisca/openfisca-core/pull/552)

#### Breaking changes

##### Parameter files

* Load the legislation parameters from a directory `parameters` containing YAML files, instead of XML files

Before:
```XML
<NODE code="root">
<NODE code="impot">
<CODE code="taux" description="" format="percent">
<END deb="2016-01-01"/>
<VALUE deb="2015-01-01" valeur="0.32" />
<VALUE deb="1998-01-01" valeur="0.3" />
</CODE>
</NODE>
</NODE>
```

Now:
```yaml
impot:
taux:
description: Taux d'impôt sur les salaires
unit: /1
values:
2016-01-01:
value: null
2015-01-01:
value: 0.32
1991-01-01:
value: 0.3
```

- The XML attributes `format` and `type` are replaced by the YAML attribute `unit`, which is a free text field.

##### reforms

* Rename `Reform.modify_legislation_json()` -> `Reform.modify_parameters()`
* Remove `reforms.compose_reforms()`.
* Remove `reforms.update_legislation()`.
- To modify an existing parameter, use `ValuesHistory.update()` instead.
- You can navigate the parameters using `.` (e.g. `parameters.taxes.tax_on_salaries.public_sector.rate`)
- Before:
```python
new_legislation_parameters = update_legislation(
legislation_json = original_legislation_parameters,
path = ('children', 'impot_revenu', 'children', 'bareme', 'brackets', 1, 'threshold'),
period = reform_period,
value = 6011,
)
```
- Now:
```python
parameters.impot_revenu.bareme[1].threshold.update(period = reform_period, value = 6011)
```

* Change the syntax to dynamically create new parameters
- Before :
```python
reform_legislation_subtree = {
"@type": "Node",
"description": "PLF 2016 sur revenus 2015",
"children": {
"decote_seuil_celib": {
"@type": "Parameter",
"description": "Seuil de la décôte pour un célibataire",
"format": "integer",
"unit": "currency",
"values": [
{'start': u'2016-01-01', },
{'start': u'2015-01-01', 'value': round(1135 * (1 + inflation))},
],
},
"decote_seuil_couple": {
"@type": "Parameter",
"description": "Seuil de la décôte pour un couple",
"format": "integer",
"unit": "currency",
"values": [
{'start': u'2065-01-01', },
{'start': u'2015-01-01', 'value': round(1870 * (1 + inflation))},
],
},
},
}
reference_legislation_copy['children']['plf2016_conterfactual'] = reform_legislation_subtree
```

- Now:
```python
from openfisca_core.parameters import ParameterNode

inflation = .001
reform_parameters_subtree = ParameterNode('plf2016_conterfactual', data = {
'decote_seuil_celib': {
'values': {
"2015-01-01": {'value': round(1135 * (1 + inflation))},
"2016-01-01": {'value': None}
}
},
'decote_seuil_couple': {
'values': {
"2015-01-01": {'value': round(1870 * (1 + inflation))},
"2065-01-01": {'value': None}
}
},
})
reference_parameters.add_child('plf2016_conterfactual', reform_parameters_subtree)
```

- Note that this way of creating parameters is only recommanded when using dynamically computed values (for instance `round(1135 * (1 + inflation))` in the previous example). If the values are static, the new parameters can be directly built from YAML (See New features section).

##### TaxBenefitSystem

* Remove parameter `legislation_json` in constructor
* Remove methods/attributes:
- `compact_legislation_by_instant_cache`
- `get_baseline_compact_legislation`
- `compute_legislation`
- `get_legislation`
+ We can now directly use the `parameters` attribute.
* Rename
- `preprocess_legislation` -> `preprocess_parameters`
- `add_legislation_params` -> `load_parameters`
- `get_compact_legislation` -> `get_parameters_at_instant`
+ The signature of the method has changed. Check the [documentation](http://openfisca.readthedocs.io/en/latest/tax-benefit-system.html#openfisca_core.taxbenefitsystems.TaxBenefitSystem.load_parameters).

##### Simulation

* Remove methods/attributes:
- `compact_legislation_by_instant_cache`
- `get_baseline_compact_legislation`
* Rename
`baseline_compact_legislation_by_instant_cache` -> `baseline_parameters_at_instant_cache`
`legislation_at` -> `parameters_at`

#### New features

* In reforms, new parameters can be added from a YAML file.
- The function `parameters.load_parameter_file()` loads a YAML file.
- The function `ParameterNode.add_child()` adds a new child to an existing legislation node.
- Example:
```python
import os
from openfisca_core.parameters import load_parameter_file

dir_path = os.path.dirname(__file__)

def reform_modify_parameters(parameters):
file_path = os.path.join(dir_path, 'plf2016.yaml')
reform_parameters_subtree = load_parameter_file(name = 'plf2016', file_path=file_path)
parameters.add_child('plf2016', reform_parameters_subtree)
return parameters

...
```

* In module model_api, add classes that are needed to build reforms:
- `load_parameter_file`
- `ParameterNodeNode`
- `Scale`
- `Bracket`
- `Parameter`
- `ValuesHistory`
- `periods.period`.


#### Technical changes

* Refactor the internal representation and the interface of legislation parameters
- The parameters of a legislation are wraped into the classes `Node`, `Parameter`, `Scale`, `Bracket`, `ValuesHistory`, `ValueAtInstant` instead of bare python dict.
- The parameters of a legislation at a given instant are wraped into the classes `NodeAtInstant`, `ValueAtInstant` and tax scales instead of bare python objects.
- The file `parameters.py` and the classes defined inside are responsible both for loading and accessing the parameters. Before the loading was implemented in `legislationsxml.py` and the other processings were implemented in `legislations.py`
- The validation of the XML files was performed against a XML schema defined in `legislation.xsd`. Now the YAML files are loaded with the library `yaml` and then validated in basic Python.

* The word "legislation" is replaced by the word "parameters" in several internal variables and internal methods. It Reduced the ambiguity between the legislation as a tax and benefit system and the legislation as the parameters.
- `TaxBenefitSystem.compact_legislation_by_instant_cache` -> `TaxBenefitSystem._parameters_at_instant_cache`
- `TaxBenefitSystem.get_baseline_compact_legislation()` -> `TaxBenefitSystem._get_baseline_parameters_at_instant()`
- `Simulation.compact_legislation_by_instant_cache` -> `Simulation._parameters_at_instant_cache`
- `Simulation.get_compact_legislation()` -> `Simulation._get_parameters_at_instant()`
- `Simulation.get_baseline_compact_legislation()` -> `Simulation._get_baseline_parameters_at_instant()`

* The optionnal parameter `traced_simulation` is removed in function `TaxBenefitSystem.get_compact_legislation()` (now `TaxBenefitSystem.get_parameters_at_instant()`). This parameter had no effect.

* The optional parameter `with_source_file_infos` is removed in functions `TaxBenefitSystem.compute_legislation()` (now `TaxBenefitSystem._compute_parameters()`) and `TaxBenefitSystem.get_legislation()`. This parameter had no effect.

* In the API preview, update the internal transformation of the parameters.

* In the directory `script`, add a subdirectory `migrations`.


## 16.3.0

- Support `reference` attributes on all parameter XML nodes.
Expand Down
1 change: 0 additions & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
recursive-include openfisca_core/scripts *
include openfisca_core/legislation.xsd
include openfisca_web_api_preview/openAPI.yml
1 change: 1 addition & 0 deletions doc/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
html_context = {
'css_files': ['_static/css/custom.css']
}
autoclass_content = 'both'
15 changes: 2 additions & 13 deletions doc/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Modules:
:maxdepth: 1

tax-benefit-system
parameters
reforms
test_runner

Scripts:
Expand All @@ -16,19 +18,6 @@ Scripts:

openfisca-run-test

Web-API routes:

.. toctree::
:maxdepth: 1

parameter
parameters
variable
variables




Indices and tables
==================

Expand Down
90 changes: 0 additions & 90 deletions doc/source/parameter.rst

This file was deleted.

50 changes: 5 additions & 45 deletions doc/source/parameters.rst
Original file line number Diff line number Diff line change
@@ -1,46 +1,6 @@
==============
``/parameters``
==============
==========
Parameters
==========

.. http:get:: /parameters

Get the parameters contained in the tax and benefit system.

The response is formatted as an object whose keys are the parameters id and values objects with a single property, description.

:resheader Country-Package: The name of the country package served by the API
:resheader Country-Package-Version: The version of the country package served by the API
:statuscode 200: The list of all parameters is sent back in the response body.

**Example request**:

.. sourcecode:: http

GET /parameters HTTP/1.1

**Example response**:

.. sourcecode:: http

HTTP/1.1 200 OK
Country-Package: openfisca-france
Country-Package-Version: 18.2.1
Content-Type: application/json

{
"benefits.basic_income": {
"description": "Amount of the basic income"
},
"benefits.housing_allowance": {
"description": "Housing allowance amount (as a fraction of the rent)"
},
"general.age_of_majority": {
"description": "Age of majority"
},
"taxes.income_tax_rate": {
"description": "Income tax rate"
},
"taxes.social_security_contribution": {
"description": "Social security contribution tax scale"
}
}
.. automodule:: openfisca_core.parameters
:members:
Loading