Commons

Common tools for contributors and users.

class openfisca_core.commons.Dummy[source]

A class that did nothing.

Examples

>>> Dummy()
<openfisca_core.commons.dummy.Dummy object...

Deprecated since version 34.7.0: Dummy has been deprecated and it will be removed in the future.

openfisca_core.commons.apply_thresholds(input, thresholds, choices)[source]

Makes a choice based on an input and thresholds.

From a list of choices, this function selects one of these values based on a list of inputs, depending on the value of each input within a list of thresholds.

Parameters:
  • input (ndarray[Any, dtype[float32]]) – A list of inputs to make a choice from.

  • thresholds (Sequence[float]) – A list of thresholds to choose.

  • choices (Sequence[float]) – A list of the possible values to choose from.

Returns:

ndarray[float32] – A list of the values chosen.

Return type:

ndarray[Any, dtype[float32]]

Examples

>>> input = numpy.array([4, 5, 6, 7, 8])
>>> thresholds = [5, 7]
>>> choices = [10, 15, 20]
>>> apply_thresholds(input, thresholds, choices)
array([10, 10, 15, 15, 20])
openfisca_core.commons.average_rate(target, varying, trim=None)[source]

Compute the average rate of a target net income.

Given a target net income, and according to the varying gross income. Optionally, a trim can be applied consisting of the lower and upper bounds of the average rate to be computed.

Note

Usually, target and varying are the same size.

Parameters:
Returns:

ndarray[float32] – The average rate for each target. When trim is provided, values that are out of the provided bounds are replaced by numpy.nan.

Return type:

ndarray[Any, dtype[float32]]

Examples

>>> target = numpy.array([1, 2, 3])
>>> varying = [2, 2, 2]
>>> trim = [-1, 0.25]
>>> average_rate(target, varying, trim)
array([ nan,  0. , -0.5])
openfisca_core.commons.concat(this, that)[source]

Concatenate the values of two arrays.

Parameters:
Returns:

ndarray[str_] – An array with the concatenated values.

Return type:

ndarray[Any, dtype[str_]]

Examples

>>> this = ["this", "that"]
>>> that = numpy.array([1, 2.5])
>>> concat(this, that)
array(['this1.0', 'that2.5']...)
openfisca_core.commons.empty_clone(original)[source]

Create an empty instance of the same class of the original object.

Parameters:

original (object) – An object to clone.

Returns:

object – The cloned, empty, object.

Return type:

object

Examples

>>> Foo = type("Foo", (list,), {})
>>> foo = Foo([1, 2, 3])
>>> foo
[1, 2, 3]
>>> bar = empty_clone(foo)
>>> bar
[]
>>> isinstance(bar, Foo)
True
openfisca_core.commons.eval_expression(expression)[source]

Evaluate a string expression to a numpy array.

Parameters:

expression (str) – An expression to evaluate.

Returns:
  • ndarray – The result of the evaluation.

  • str – The expression if it couldn’t be evaluated.

Return type:

str | ndarray[Any, dtype[bool_]] | ndarray[Any, dtype[int32]] | ndarray[Any, dtype[float32]]

Examples

>>> eval_expression("1 + 2")
array(3, dtype=int32)
>>> eval_expression("salary")
'salary'
openfisca_core.commons.marginal_rate(target, varying, trim=None)[source]

Compute the marginal rate of a target net income.

Given a target net income, and according to the varying gross income. Optionally, a trim can be applied consisting of the lower and upper bounds of the marginal rate to be computed.

Note

Usually, target and varying are the same size.

Parameters:
Returns:

ndarray[float32] – The marginal rate for each target. When trim is provided, values that are out of the provided bounds are replaced by numpy.nan.

Return type:

ndarray[Any, dtype[float32]]

Examples

>>> target = numpy.array([1, 2, 3])
>>> varying = numpy.array([1, 2, 4])
>>> trim = [0.25, 0.75]
>>> marginal_rate(target, varying, trim)
array([nan, 0.5])
openfisca_core.commons.stringify_array(array)[source]

Generate a clean string representation of a numpy array.

Parameters:

array (None | ndarray[Any, dtype[generic]]) – An array.

Returns:
  • str"None" if the array is None.

  • str – The stringified array otherwise.

Return type:

str

Examples

>>> import numpy
>>> stringify_array(None)
'None'
>>> array = numpy.array([10, 20.0])
>>> stringify_array(array)
'[10.0, 20.0]'
>>> array = numpy.array(["10", "Twenty"])
>>> stringify_array(array)
'[10, Twenty]'
>>> array = numpy.array([list, dict(), stringify_array])
>>> stringify_array(array)
"[<class 'list'>, {}, <function stringify_array...]"
openfisca_core.commons.switch(conditions, value_by_condition)[source]

Mimick a switch statement.

Given an array of conditions, returns an array of the same size, replacing each condition item with the matching given value.

Parameters:
Returns:

ndarray[float32] – An array with the replaced values.

Return type:

ndarray[Any, dtype[float32]]

Examples

>>> conditions = numpy.array([1, 1, 1, 2])
>>> value_by_condition = {1: 80, 2: 90}
>>> switch(conditions, value_by_condition)
array([80, 80, 80, 90])