Пример #1
0
    def test_decorator(self):
        """When applied to a function it allows it to work with scalars."""
        # Setup
        function = MagicMock()
        function.__doc__ = 'Docstring of the original function.'
        function.return_value = np.array(['return_value'])

        instance = MagicMock()
        args = ['positional', 'arguments']
        kwargs = {'keyword': 'arguments'}

        expected_result = 'return_value'

        # Run (Decorator)
        scalarized_function = scalarize(function)

        # Check (Decorator)
        assert callable(scalarized_function)
        assert scalarized_function.__doc__ == 'Docstring of the original function.'

        # Run (Decorated function)
        result = scalarized_function(instance, 0, *args, **kwargs)

        # Check (Decorated function)
        assert result == expected_result

        function.assert_called_once_with(instance, np.array([0]), *args,
                                         **kwargs)

        instance.assert_not_called()
        assert instance.method_calls == []
Пример #2
0
    def _brentq_cdf(self, value):
        """Helper function to compute percent_point.

        As scipy.stats.gaussian_kde doesn't provide this functionality out of the box we need
        to make a numerical approach:

        - First we scalarize and bound cumulative_distribution.
        - Then we define a function `f(x) = cdf(x) - value`, where value is the given argument.
        - As value will be called from ppf we can assume value = cdf(z) for some z that is the
        value we are searching for. Therefore the zeros of the function will be x such that:
        cdf(x) - cdf(z) = 0 => (becasue cdf is monotonous and continous) x = z

        Args:
            value (float):
                cdf value, that is, in [0,1]

        Returns:
            callable:
                function whose zero is the ppf of value.
        """
        # The decorator expects an instance method, but usually are decorated before being bounded
        bound_cdf = partial(scalarize(GaussianKDE.cumulative_distribution),
                            self)

        def f(x):
            return bound_cdf(x) - value

        return f