def test_inhomogenous_3(): parameters = ParameterNode(directory_path=LOCAL_DIR) parameters.rate.couple.tenant.add_child( 'z4', ParameterNode('toto', data={ 'amount': { 'values': { "2015-01-01": { 'value': 550 }, "2016-01-01": { 'value': 600 } } } })) P = parameters.rate('2015-01-01') zone = np.asarray(['z1', 'z2', 'z2', 'z1']) try: P.couple.tenant[zone] except ValueError as e: assert_in("'rate.couple.tenant.z4' is a node", e.message) assert_in("'rate.couple.tenant.z2' is not", e.message) raise
def test_clone(): path = os.path.join(BASE_DIR, 'filesystem_hierarchy') parameters = ParameterNode('', directory_path=path) parameters_at_instant = parameters('2016-01-01') assert parameters_at_instant.node1.param == 1.0 clone = parameters.clone() clone_at_instant = clone('2016-01-01') assert clone_at_instant.node1.param == 1.0 assert id(clone) != id(parameters) assert id(clone.node1) != id(parameters.node1) assert id(clone.node1.param) != id(parameters.node1.param)
def test_combine_tax_scales(): from openfisca_core.parameters import ParameterNode node = ParameterNode('baremes', data = { 'health': { 'brackets': [ { 'rate': {'2015-01-01': 0.05}, 'threshold': {'2015-01-01': 0} }, { 'rate': {'2015-01-01': 0.10}, 'threshold': {'2015-01-01': 2000} }, ]}, 'retirement': { 'brackets': [ { 'rate': {'2015-01-01': 0.02}, 'threshold': {'2015-01-01': 0} }, { 'rate': {'2015-01-01': 0.04}, 'threshold': {'2015-01-01': 3000} }, ] }, })(2015) bareme = combine_tax_scales(node) assert_near(bareme.thresholds, [0, 2000, 3000]) assert_near(bareme.rates, [0.07, 0.12, 0.14], 1e-13)
def test_inhomogenous_2(): parameters = ParameterNode(directory_path = LOCAL_DIR) parameters.rate.couple.tenant.add_child('toto', Parameter('toto', { "values": { "2015-01-01": { "value": 1000 }, } })) P = parameters.rate('2015-01-01') housing_occupancy_status = np.asarray(['owner', 'owner', 'tenant', 'tenant']) with pytest.raises(ValueError) as e: P.couple[housing_occupancy_status] assert "'rate.couple.tenant.toto' exists" in get_message(e.value) assert "'rate.couple.owner.toto' doesn't" in get_message(e.value)
def load_extension(self, extension): """ Loads an extension to the tax and benefit system. :param string extension: The extension to load. Can be an absolute path pointing to an extension directory, or the name of an OpenFisca extension installed as a pip package. """ if path.isdir(extension): if find_packages(extension): # Load extension from a package directory extension_directory = path.join(extension, find_packages(extension)[0]) else: # Load extension from a simple directory extension_directory = extension else: # Load extension from installed pip package try: package = importlib.import_module(extension) extension_directory = package.__path__[0] except ImportError: message = linesep.join([ traceback.format_exc(), 'Error loading extension: `{}` is neither a directory, nor a package.' .format(extension), 'Are you sure it is installed in your environment? If so, look at the stack trace above to determine the origin of this error.', 'See more at <https://github.com/openfisca/openfisca-extension-template#installing>.' ]) raise ValueError(message) self.add_variables_from_directory(extension_directory) param_dir = path.join(extension_directory, 'parameters') if path.isdir(param_dir): extension_parameters = ParameterNode(directory_path=param_dir) self.parameters.merge(extension_parameters)
def test_inhomogenous_3(): parameters = ParameterNode(directory_path = LOCAL_DIR) parameters.rate.couple.tenant.add_child('z4', ParameterNode('toto', data = { 'amount': { 'values': { "2015-01-01": {'value': 550}, "2016-01-01": {'value': 600} } } })) P = parameters.rate('2015-01-01') zone = np.asarray(['z1', 'z2', 'z2', 'z1']) with pytest.raises(ValueError) as e: P.couple.tenant[zone] assert "'rate.couple.tenant.z4' is a node" in get_message(e.value) assert re.findall(r"'rate.couple.tenant.z(1|2|3)' is not", get_message(e.value))
def test_inhomogenous_2(): parameters = ParameterNode(directory_path=LOCAL_DIR) parameters.rate.couple.tenant.add_child( 'toto', Parameter('toto', {"values": { "2015-01-01": { "value": 1000 }, }})) P = parameters.rate('2015-01-01') housing_occupancy_status = np.asarray( ['owner', 'owner', 'tenant', 'tenant']) try: P.couple[housing_occupancy_status] except ValueError as e: assert_in("'rate.couple.tenant.toto' exists", e.message) assert_in("'rate.couple.owner.toto' doesn't", e.message) raise
def modify_parameters(reference_parameters): reform_parameters_subtree = ParameterNode( 'new_node', data = { 'new_param': { 'values': {"2000-01-01": {'value': True}, "2015-01-01": {'value': None}} }, }, ) reference_parameters.children['new_node'] = reform_parameters_subtree return reference_parameters
def test_name(): parameter_data = { "description": "Parameter indexed by a numeric key", "2010": { "values": { '2006-01-01': 0.0075 } } } parameter = ParameterNode('root', data=parameter_data) assert parameter.children["2010"].name == "root.2010"
def load_parameters(self, path_to_yaml_dir): """ Loads the legislation parameter for a directory containing YAML parameters files. :param path_to_yaml_dir: Absolute path towards the YAML parameter directory. Example: >>> self.load_parameters('/path/to/yaml/parameters/dir') """ parameters = ParameterNode('', directory_path = path_to_yaml_dir) if self.preprocess_parameters is not None: parameters = self.preprocess_parameters(parameters) self.parameters = parameters
# -*- coding: utf-8 -*- import os import re import numpy as np import pytest from openfisca_core.tools import assert_near from openfisca_core.parameters import ParameterNode, Parameter, ParameterNotFound from openfisca_core.model_api import * # noqa LOCAL_DIR = os.path.dirname(os.path.abspath(__file__)) parameters = ParameterNode(directory_path=LOCAL_DIR) P = parameters.rate('2015-01-01') def get_message(error): return error.args[0] def test_on_leaf(): zone = np.asarray(['z1', 'z2', 'z2', 'z1']) assert_near(P.single.owner[zone], [100, 200, 200, 100]) def test_on_node(): housing_occupancy_status = np.asarray( ['owner', 'owner', 'tenant', 'tenant'])
def test_yaml_hierarchy(): path = os.path.join(BASE_DIR, 'yaml_hierarchy') parameters = ParameterNode('', directory_path=path) parameters_at_instant = parameters('2016-01-01') assert parameters_at_instant.node1.param == 1.0
# -*- coding: utf-8 -*- import os import re import numpy as np import pytest from openfisca_core.tools import assert_near from openfisca_core.parameters import ParameterNode, Parameter, ParameterNotFound from openfisca_core.model_api import * # noqa LOCAL_DIR = os.path.dirname(os.path.abspath(__file__)) parameters = ParameterNode(directory_path = LOCAL_DIR) P = parameters.rate('2015-01-01') def get_message(error): return error.args[0] def test_on_leaf(): zone = np.asarray(['z1', 'z2', 'z2', 'z1']) assert_near(P.single.owner[zone], [100, 200, 200, 100]) def test_on_node(): housing_occupancy_status = np.asarray(['owner', 'owner', 'tenant', 'tenant'])