Repository: scipy/scipy
File: scipy/integrate/tests/test_quadrature.py (Python)

Code before:

        # determine shapes
        shape = [5, 6, x_len]
        shape[axis], shape[-1] = shape[-1], shape[axis]
        shape_len_1 = shape.copy()
        shape_len_1[axis] = 1
        i_shape = shape_len_1 if i_ndim == 3 else ()

        # initialize arguments
        y = xp.asarray(rng.random(size=shape))
        x, dx = None, None
        if dx:
            dx = rng.random(size=shape_len_1) if x_ndim > 1 else rng.random()
            dx = xp.asarray(dx)
        else:
            x = (np.sort(rng.random(size=shape), axis=axis) if x_ndim > 1
                 else np.sort(rng.random(size=shape[axis])))
            x = xp.asarray(x)
        initial = None if i_ndim is None else xp.asarray(rng.random(size=i_shape))

        # compare results
        res = cumulative_simpson(y, x=x, dx=dx, initial=initial, axis=axis)
        ref = cumulative_simpson_nd_ref(
            np.asarray(y), x=np.asarray(x), dx=None if dx is None else np.asarray(dx),
            initial=None if initial is None else np.asarray(initial), axis=axis
        )
        xp_assert_close(res, xp.asarray(ref), rtol=1e-15)

    @pytest.mark.filterwarnings(
        "ignore:Creating a tensor from a list of numpy.ndarrays:UserWarning"
    )
    @pytest.mark.parametrize(('message', 'kwarg_update'), [
        ("x must be strictly increasing", dict(x=[2, 2, 3, 4])),
        ("x must be strictly increasing", dict(x=[x0, [2, 2, 4, 8]], y=[y0, y0])),
        ("x must be strictly increasing", dict(x=[x0, x0, x0], y=[y0, y0, y0], axis=0)),
        ("At least one point is required", dict(x=[], y=[])),
        ("`axis=4` is not valid for `y` with `y.ndim=1`", dict(axis=4)),
        ("shape of `x` must be the same as `y` or 1-D", dict(x=np.arange(5))),
        ("`initial` must either be a scalar or...", dict(initial=np.arange(5))),
        ("`dx` must either be a scalar or...", dict(x=None, dx=np.arange(5))),
    ])
    def test_simpson_exceptions(self, message, kwarg_update, xp):
        kwargs0 = dict(y=xp.asarray(self.y0), x=xp.asarray(self.x0), dx=None,
                       initial=None, axis=-1)
        kwarg_update = {k: xp.asarray(v) if isinstance(v, list) else v
                        for k, v in kwarg_update.items()}
        with pytest.raises(ValueError, match=message):
            cumulative_simpson(**dict(kwargs0, **kwarg_update))

    def test_special_cases(self, xp):
        # Test special cases not checked elsewhere

Review comment (suggestion):
Should there be a comment explaining why we are using `np.` here and `xp.` above? This seems like just partial support being added/partially converted test? `cpu_only` on its own doesn't seem sufficient

Code after:

        # determine shapes
        shape = [5, 6, x_len]
        shape[axis], shape[-1] = shape[-1], shape[axis]
        shape_len_1 = shape.copy()
        shape_len_1[axis] = 1
        i_shape = shape_len_1 if i_ndim == 3 else ()

        # initialize arguments
        y = xp.asarray(rng.random(size=shape))
        x, dx = None, None
        if dx:
            dx = rng.random(size=shape_len_1) if x_ndim > 1 else rng.random()
            dx = xp.asarray(dx)
        else:
            x = (np.sort(rng.random(size=shape), axis=axis) if x_ndim > 1
                 else np.sort(rng.random(size=shape[axis])))
            x = xp.asarray(x)
        initial = None if i_ndim is None else xp.asarray(rng.random(size=i_shape))

        # compare results
        res = cumulative_simpson(y, x=x, dx=dx, initial=initial, axis=axis)
        # use np to generate `ref` as `cumulative_simpson_nd_ref`
        # uses `apply_along_axis`
        ref = cumulative_simpson_nd_reference(
            np.asarray(y), x=np.asarray(x), dx=None if dx is None else np.asarray(dx),
            initial=None if initial is None else np.asarray(initial), axis=axis
        )
        xp_assert_close(res, xp.asarray(ref), rtol=1e-15)

    @pytest.mark.parametrize(('message', 'kwarg_update'), [
        ("x must be strictly increasing", dict(x=[2, 2, 3, 4])),
        ("x must be strictly increasing", dict(x=[x0, [2, 2, 4, 8]], y=[y0, y0])),
        ("x must be strictly increasing", dict(x=[x0, x0, x0], y=[y0, y0, y0], axis=0)),
        ("At least one point is required", dict(x=[], y=[])),
        ("`axis=4` is not valid for `y` with `y.ndim=1`", dict(axis=4)),
        ("shape of `x` must be the same as `y` or 1-D", dict(x=np.arange(5))),
        ("`initial` must either be a scalar or...", dict(initial=np.arange(5))),
        ("`dx` must either be a scalar or...", dict(x=None, dx=np.arange(5))),
    ])
    def test_simpson_exceptions(self, message, kwarg_update, xp):
        kwargs0 = dict(y=xp.asarray(self.y0), x=xp.asarray(self.x0), dx=None,
                       initial=None, axis=-1)
        kwarg_update = {k: xp.asarray(np.asarray(v)) if isinstance(v, list) else v
                        for k, v in kwarg_update.items()}
        with pytest.raises(ValueError, match=message):
            cumulative_simpson(**dict(kwargs0, **kwarg_update))

    def test_special_cases(self, xp):
        # Test special cases not checked elsewhere
        rng = np.random.default_rng(82456839535679456794)