Common tools for contributors and users.
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.
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
.
ndarray[float32] – A list of the values chosen.
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])
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.
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
.
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])
Concatenate the values of two arrays.
ndarray[str_] – An array with the concatenated values.
Examples
>>> this = ["this", "that"]
>>> that = numpy.array([1, 2.5])
>>> concat(this, that)
array(['this1.0', 'that2.5']...)
Create an empty instance of the same class of the original object.
original (object
) – An object to clone.
object – The cloned, empty, object.
Examples
>>> Foo = type("Foo", (list,), {})
>>> foo = Foo([1, 2, 3])
>>> foo
[1, 2, 3]
>>> bar = empty_clone(foo)
>>> bar
[]
>>> isinstance(bar, Foo)
True
Evaluate a string expression to a numpy array.
expression (str
) – An expression to evaluate.
ndarray – The result of the evaluation.
str – The expression if it couldn’t be evaluated.
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'
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.
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
.
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])
Generate a clean string representation of a numpy array.
str – "None"
if the array
is None
.
str – The stringified array
otherwise.
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...]"
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.
ndarray[float32] – An array with the replaced values.
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])