jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def opt_val(self, f: np.ndarray, y: np.ndarray, m: ComparisonGP, sqrt_nu: np.ndarray) -> float:
        """
        computes the acquisition function value by sampling

        :param f: latent function samples of the posterior
        :param y: observations from the latent functio (latent function values corrupted with noise)
        :param m: the model which posterior is used
        :param sqrt_nu: noise std for the observations
        :return: Sum of variances at the input
        """
        liks = np.zeros(f.shape[1])
        for i in range(f.shape[1]):  # Loop through each member being the batch winner (lower value better)
            mi = [k for k in range(f.shape[1]) if k is not i]  # select all indices but the batch winner
            prs = np.prod(
                phi((f[:, mi] - y[:, i, None]) / sqrt_nu[None, mi]), axis=1
            )  # Equation 3 in the draft (or the simplified version of it at the end of Section 2.2)
            prs2 = prs**2
            liks[i] = (
                np.mean(prs2, axis=0) - np.mean(prs, axis=0) ** 2
            )  # Variance of p(y_i \leq y_j \forall i \neq j) weighted by p(y_i \leq y_j \forall i \neq j)
        return np.sum(liks)
//  [python]
def opt_val_s(self, f: np.ndarray, y: np.ndarray, m: ComparisonGP, sqrt_nu: np.ndarray) -> np.ndarray:
        """
        Computes $ \prod_{i=1, i \neq j}^q p(y_j < y_i)$ for each sample from the latent function.

        :param f: latent function samples of the posterior
        :param y: observations from the latent functio (latent function values corrupted with noise)
        :param m: the model which posterior is used
        :param sqrt_nu: noise std for the observations
        :return: product of probabilities of comparisons for each sample
        """
        opt_vals = np.zeros(f.shape)
        for i in range(f.shape[1]):
            mi = [k for k in range(f.shape[1]) if k is not i]
            opt_vals[:, i] = np.exp(np.sum(np.log(phi((f[:, mi] - y[:, i, None]) / sqrt_nu[None, mi])), axis=1))
        return opt_vals
//  [python]
def opt_val_s(self, f: np.ndarray, y: np.ndarray, m: ComparisonGP, sqrt_nu: np.ndarray) -> np.ndarray:
        """
        Computes $ \prod_{i=1, i \neq j}^q p(y_j < y_i)$ for each sample from the latent function.

        :param f: latent function samples of the posterior
        :param y: observations from the latent functio (latent function values corrupted with noise)
        :param m: the model which posterior is used
        :param sqrt_nu: noise std for the observations
        :return: product of probabilities of comparisons for each sample
        """
        opt_vals = np.zeros(f.shape)
        for i in range(f.shape[1]):
            mi = [k for k in range(f.shape[1]) if k is not i]
            opt_vals[:, i] = np.prod(phi((f[:, mi] - y[:, i, None]) / sqrt_nu[None, mi]), axis=1)
        return opt_vals
//  [python]
def opt_val(self, f: np.ndarray, y: np.ndarray, m: ComparisonGP, sqrt_nu: np.ndarray) -> float:
        """
        computes the acquisition function value by sampling

        :param f: latent function samples of the posterior
        :param y: observations from the latent functio (latent function values corrupted with noise)
        :param m: the model which posterior is used
        :param sqrt_nu: noise std for the observations
        :return: q Expected Improvement at the input
        """
        yw = m.get_current_best()
        liks = np.zeros(f.shape)
        for i in range(f.shape[1]):  # Loop through batch winners (lower value better)
            mi = [k for k in range(f.shape[1]) if k is not i]
            liks[:, i] = (
                (y[:, i] < yw)
                * np.array(yw - y[:, i])
                * np.multiply.reduce(phi((f[:, mi] - y[:, i, None]) / sqrt_nu[None, mi]), axis=1)
            )
        return np.mean(np.sum(liks, axis=1), axis=0)
//  [python]
def opt_val(self, f: np.ndarray, y: np.ndarray, m: ComparisonGP, sqrt_nu: np.ndarray) -> float:
        """
        computes the acquisition function value by sampling

        :param f: latent function samples of the posterior
        :param y: observations from the latent functio (latent function values corrupted with noise)
        :param m: the model which posterior is used
        :param sqrt_nu: noise std for the observations
        :return: q Expected Improvement at the input
        """
        yw = m.get_current_best()
        liks = np.zeros(f.shape)
        for i in range(f.shape[1]):  # Loop through batch winners (lower value better)
            mi = [k for k in range(f.shape[1]) if k is not i]
            liks[:, i] = (
                (y[:, i] < yw)
                * np.array(yw - y[:, i])
                * np.prod(phi((f[:, mi] - y[:, i, None]) / sqrt_nu[None, mi]), axis=1)
            )
        return np.mean(np.sum(liks, axis=1), axis=0)
//  [python]
def decode_numpy(value):
    if not value:
        raise RuntimeError("Value to decode should not be empty")
    archive = OutArchive(value)
    shape_size = archive.get_size()
    shape = []
    for _ in range(shape_size):
        shape.append(archive.get_size())
    dtype = _context_protocol_to_numpy_dtype(archive.get_int())
    array_size = archive.get_size()
    check_argument(array_size == reduce(lambda x, y: x * y, shape))
    if dtype is object:
        data_copy = []
        for _ in range(array_size):
            data_copy.append(archive.get_string())
        if shape and shape[0] > 1:
            array = np.reshape(data_copy, shape)
        else:
            array = np.array(data_copy, dtype=dtype)
    else:
        array = np.ndarray(
            shape=shape,
            dtype=dtype,
            buffer=archive.get_block(array_size * dtype.itemsize),
            order="C",
        )
    return array
//  [python]
def decode_numpy(value):
    if not value:
        raise RuntimeError("Value to decode should not be empty")
    archive = OutArchive(value)
    shape_size = archive.get_size()
    shape = []
    for _ in range(shape_size):
        shape.append(archive.get_size())
    dtype = _context_protocol_to_numpy_dtype(archive.get_int())
    array_size = archive.get_size()
    check_argument(array_size == np.prod(shape))
    if dtype is object:
        data_copy = []
        for _ in range(array_size):
            data_copy.append(archive.get_string())
        if shape and shape[0] > 1:
            array = np.reshape(data_copy, shape)
        else:
            array = np.array(data_copy, dtype=dtype)
    else:
        array = np.ndarray(
            shape=shape,
            dtype=dtype,
            buffer=archive.get_block(array_size * dtype.itemsize),
            order="C",
        )
    return array
//  [python]
def dopt_val_s_df(self, f: np.ndarray, y: np.ndarray, m: ComparisonGP, sqrt_nu: np.ndarray) -> np.ndarray:
        """
        Computes the gradient of $\prod_{i=1, i \neq j}^q p(y_j < y_i)$ for each sample from the latent function with respect to the GP

        :param f: latent function samples of the posterior
        :param y: observations from the latent functio (latent function values corrupted with noise)
        :param m: the model which posterior is used
        :param sqrt_nu: noise std for the observations
        :return: gradient of product of probabilities of comparisons for each sample with respect to the GP
        """
        dl = np.zeros((f.shape[0], f.shape[1], f.shape[1]))  # samples, winner , derivative index
        for i in range(f.shape[1]):  # loop through winner index
            for j in range(f.shape[1]):  # loop through f to derivate with respect to
                if j is not i:
                    mij = [k for k in range(f.shape[1]) if (k is not i) and (k is not j)]
                    dl[:, i, i] += (
                        -1.0
                        / sqrt_nu[j]
                        * npdf((f[:, j] - y[:, i]) / sqrt_nu[j], 0, 1)
                        * np.prod(phi((f[:, mij] - y[:, i, None]) / sqrt_nu[None, mij]), axis=1)
                    )
                    dl[:, i, j] = (
                        1.0
                        / sqrt_nu[j]
                        * npdf((f[:, j] - y[:, i]) / sqrt_nu[j], 0, 1)
                        * np.prod(phi((f[:, mij] - y[:, i, None]) / sqrt_nu[None, mij]), axis=1)
                    )
        return dl
//  [python]
def orbital_id(self, grid_coordinates, spin=None):
        """Return the tensor factor of a orbital
        with given coordinates and spin.

        Args:
            grid_coordinates: List or tuple of ints giving coordinates of grid
                element. Acceptable to provide an int(instead of tuple or list)
                for 1D case.
            spin (bool): 0 means spin down and 1 means spin up.
                If None, assume spinless model.

        Returns:
            tensor_factor (int):
                tensor factor associated with provided orbital label.
        """
        # Initialize.
        if isinstance(grid_coordinates, int):
            grid_coordinates = [grid_coordinates]

        # Loop through dimensions of coordinate tuple.
        tensor_factor = 0
        for dimension, grid_coordinate in enumerate(grid_coordinates):
            # Make sure coordinate is an integer in the correct bounds.
            if isinstance(grid_coordinate, int) and grid_coordinate < self.length[dimension]:
                tensor_factor += grid_coordinate * int(n.prod(self.length[:dimension]))
            else:
                # Raise for invalid model.
                raise OrbitalSpecificationError('Invalid orbital coordinates provided.')

        # Account for spin and return.
        if spin is None:
            return tensor_factor
        else:
            tensor_factor *= 2
            tensor_factor += spin
            return tensor_factor
//  [python]
def orbital_id(self, grid_coordinates, spin=None):
        """Return the tensor factor of a orbital
        with given coordinates and spin.

        Args:
            grid_coordinates: List or tuple of ints giving coordinates of grid
                element. Acceptable to provide an int(instead of tuple or list)
                for 1D case.
            spin (bool): 0 means spin down and 1 means spin up.
                If None, assume spinless model.

        Returns:
            tensor_factor (int):
                tensor factor associated with provided orbital label.
        """
        # Initialize.
        if isinstance(grid_coordinates, int):
            grid_coordinates = [grid_coordinates]

        # Loop through dimensions of coordinate tuple.
        tensor_factor = 0
        for dimension, grid_coordinate in enumerate(grid_coordinates):
            # Make sure coordinate is an integer in the correct bounds.
            if isinstance(grid_coordinate, int) and grid_coordinate < self.length[dimension]:
                tensor_factor += grid_coordinate * int(numpy.prod(self.length[:dimension]))
            else:
                # Raise for invalid model.
                raise OrbitalSpecificationError('Invalid orbital coordinates provided.')

        # Account for spin and return.
        if spin is None:
            return tensor_factor
        else:
            tensor_factor *= 2
            tensor_factor += spin
            return tensor_factor
//  [python]
def load_dump_file(filename):
    """
    See dump_to_file() in NativeOp.cpp.

    :param str filename:
    :rtype: numpy.ndarray
    """
    import numpy
    from struct import unpack

    with open(filename, "rb") as f:

        def _read_uint64():
            return int(unpack("Q", f.read(8))[0])

        def _read_bytes():
            size = _read_uint64()
            return f.read(size)

        def _read_str():
            return _read_bytes().decode("utf8")

        header = _read_str()
        assert header == "NativeOp_dump"
        dtype_name = _read_str()
        if dtype_name == "float":
            dtype_name = "float32"
        dtype = numpy.dtype(dtype_name)
        dtype_size = _read_uint64()
        assert dtype.itemsize == dtype_size, "dtype %r %r: %r != %r" % (dtype_name, dtype, dtype.itemsize, dtype_size)
        ndim = _read_uint64()
        dims = [_read_uint64() for _ in range(ndim)]
        data = _read_bytes()
        assert len(data) == numpy.multiply.reduce(dims) * dtype.itemsize
        # noinspection PyTypeChecker
        v_flat = numpy.fromstring(data, dtype=dtype)
        v = v_flat.reshape(dims)
        return v
//  [python]
def load_dump_file(filename):
    """
    See dump_to_file() in NativeOp.cpp.

    :param str filename:
    :rtype: numpy.ndarray
    """
    import numpy
    from struct import unpack

    with open(filename, "rb") as f:

        def _read_uint64():
            return int(unpack("Q", f.read(8))[0])

        def _read_bytes():
            size = _read_uint64()
            return f.read(size)

        def _read_str():
            return _read_bytes().decode("utf8")

        header = _read_str()
        assert header == "NativeOp_dump"
        dtype_name = _read_str()
        if dtype_name == "float":
            dtype_name = "float32"
        dtype = numpy.dtype(dtype_name)
        dtype_size = _read_uint64()
        assert dtype.itemsize == dtype_size, "dtype %r %r: %r != %r" % (dtype_name, dtype, dtype.itemsize, dtype_size)
        ndim = _read_uint64()
        dims = [_read_uint64() for _ in range(ndim)]
        data = _read_bytes()
        assert len(data) == numpy.prod(dims) * dtype.itemsize
        # noinspection PyTypeChecker
        v_flat = numpy.fromstring(data, dtype=dtype)
        v = v_flat.reshape(dims)
        return v
//  [python]
def sum_sparse_matrix(matrix, axis: int = 0) -> np.ndarray:
    """Sum a sparse matrix along an axis."""
    axis_sum = np.asarray(matrix.sum(axis=axis))  # sum along given axis
    max_dim = np.multiply.reduce(axis_sum.shape)  # get the max dimension
    return axis_sum.reshape((max_dim,))
//  [python]
def sum_sparse_matrix(matrix, axis: int = 0) -> np.ndarray:
    """Sum a sparse matrix along an axis."""
    axis_sum = np.asarray(matrix.sum(axis=axis))  # sum along given axis
    max_dim = np.prod(axis_sum.shape)  # get the max dimension
    return axis_sum.reshape((max_dim,))
//  [python]
def dopt_val_df(self, f: np.ndarray, y: np.ndarray, m: ComparisonGP, sqrt_nu: np.ndarray) -> np.ndarray:
        """
        computes the gradient of the acquisition function value with respect to each input location and gp sample

        :param f: latent function samples of the posterior
        :param y: observations from the latent functio (latent function values corrupted with noise)
        :param m: the model which posterior is used
        :param sqrt_nu: noise std for the observations
        :return: gradient of the acquisition function value with respect to each input location and gp sample
        """
        yw = m.get_current_best()
        dl = np.zeros(f.shape)
        for i in range(f.shape[1]):  # loop through f to derivate with respect to
            mi = [k for k in range(f.shape[1]) if (k is not i)]
            dl[:, i] = dl[:, i] - np.exp(np.sum(np.log(phi((f[:, mi] - y[:, i, None]) / sqrt_nu[None, mi])), axis=1)) * (y[:, i] < yw)
            for j in range(f.shape[1]):
                if j is not i:
                    mij = [k for k in range(f.shape[1]) if (k is not i) and (k is not j)]
                    dl[:, i] = (
                        dl[:, i]
                        - (y[:, i] < yw)
                        * (yw - y[:, i])
                        / sqrt_nu[j]
                        * npdf((f[:, j] - y[:, i]) / sqrt_nu[j], 0, 1)
                        * np.exp(np.sum(np.log(phi((f[:, mij] - y[:, i, None]) / sqrt_nu[None, mij])), axis=1))
                        + (y[:, j] < yw)
                        * (yw - y[:, j])
                        / sqrt_nu[i]
                        * npdf((f[:, i] - y[:, j]) / sqrt_nu[i], 0, 1)
                        * np.exp(np.sum(np.log(phi((f[:, mij] - y[:, j, None]) / sqrt_nu[None, mij])), axis=1))
                    )  # first line: i is winner second line: j is winner
        return dl
//  [python]
def dopt_val_df(self, f: np.ndarray, y: np.ndarray, m: ComparisonGP, sqrt_nu: np.ndarray) -> np.ndarray:
        """
        computes the gradient of the acquisition function value with respect to each input location and gp sample

        :param f: latent function samples of the posterior
        :param y: observations from the latent functio (latent function values corrupted with noise)
        :param m: the model which posterior is used
        :param sqrt_nu: noise std for the observations
        :return: gradient of the acquisition function value with respect to each input location and gp sample
        """
        yw = m.get_current_best()
        dl = np.zeros(f.shape)
        for i in range(f.shape[1]):  # loop through f to derivate with respect to
            mi = [k for k in range(f.shape[1]) if (k is not i)]
            dl[:, i] = dl[:, i] - np.prod(phi((f[:, mi] - y[:, i, None]) / sqrt_nu[None, mi]), axis=1) * (y[:, i] < yw)
            for j in range(f.shape[1]):
                if j is not i:
                    mij = [k for k in range(f.shape[1]) if (k is not i) and (k is not j)]
                    dl[:, i] = (
                        dl[:, i]
                        - (y[:, i] < yw)
                        * (yw - y[:, i])
                        / sqrt_nu[j]
                        * npdf((f[:, j] - y[:, i]) / sqrt_nu[j], 0, 1)
                        * np.prod(phi((f[:, mij] - y[:, i, None]) / sqrt_nu[None, mij]), axis=1)
                        + (y[:, j] < yw)
                        * (yw - y[:, j])
                        / sqrt_nu[i]
                        * npdf((f[:, i] - y[:, j]) / sqrt_nu[i], 0, 1)
                        * np.prod(phi((f[:, mij] - y[:, j, None]) / sqrt_nu[None, mij]), axis=1)
                    )  # first line: i is winner second line: j is winner
        return dl
//  [python]
def grid_indices(self, qubit_id, spinless):
        """This function is the inverse of orbital_id.

        Args:
            qubit_id (int): The tensor factor to map to grid indices.
            spinless (bool): Whether to use the spinless model or not.

        Returns:
            grid_indices (numpy.ndarray[int]):
                The location of the qubit on the grid.
        """
        if not (n.prod(self.length) * (2 - spinless) > qubit_id >= 0):
            raise OrbitalSpecificationError('Invalid qubit_id provided.')

        # Remove spin degree of freedom if it exists.
        orbital_id = qubit_id

        if not spinless:
            orbital_id //= 2

        # Get grid indices.
        grid_indices = []
        for dimension in range(self.dimensions):
            remainder = orbital_id % int(n.prod(self.length[: dimension + 1]))
            grid_index = remainder // int(n.prod(self.length[:dimension]))
            grid_indices += [grid_index]
        return grid_indices
//  [python]
def grid_indices(self, qubit_id, spinless):
        """This function is the inverse of orbital_id.

        Args:
            qubit_id (int): The tensor factor to map to grid indices.
            spinless (bool): Whether to use the spinless model or not.

        Returns:
            grid_indices (numpy.ndarray[int]):
                The location of the qubit on the grid.
        """
        if not (numpy.prod(self.length) * (2 - spinless) > qubit_id >= 0):
            raise OrbitalSpecificationError('Invalid qubit_id provided.')

        # Remove spin degree of freedom if it exists.
        orbital_id = qubit_id

        if not spinless:
            orbital_id //= 2

        # Get grid indices.
        grid_indices = []
        for dimension in range(self.dimensions):
            remainder = orbital_id % int(numpy.prod(self.length[: dimension + 1]))
            grid_index = remainder // int(numpy.prod(self.length[:dimension]))
            grid_indices += [grid_index]
        return grid_indices
//  [python]
def test_tensorarray_grad():
    def gen(shape, offset):
        return (numpy.arange(numpy.multiply.reduce(shape)) + offset).reshape(shape).astype("float32")

    n_time = 3
    n_batch = 2
    n_dim = 2
    x = gen((n_time, n_batch, n_dim), offset=-4)
    dy = gen((n_time, n_batch, n_dim), offset=-7)
    start = 0
    step = 1

    x = tf.convert_to_tensor(x)
    x.set_shape(tf.TensorShape((n_time, n_batch, n_dim)))
    with tf.name_scope("gradients"):
        # Note that tensor_array_grad._GetGradSource() has this ugly hack
        # which requires that we have the "gradients" prefix.
        dy = tf.identity(tf.convert_to_tensor(dy), name="dy")
    dy.set_shape(tf.TensorShape((n_time, n_batch, n_dim)))

    def ta_identity(x):
        h = tf.TensorArray(tf.float32, size=n_time, element_shape=tf.TensorShape((n_batch, n_dim)))
        for t in range(n_time):
            h = h.write(index=t, value=x[t])
        h = h.stack()
        h.set_shape(tf.TensorShape((n_time, n_batch, n_dim)))
        return h

    def wrapped(x):
        x_ = x[start::step]
        x_.set_shape(tf.TensorShape((n_time, n_batch, n_dim)))
        h = ta_identity(x=x_)
        h.set_shape(tf.TensorShape((n_time, n_batch, n_dim)))

        def reverse_strided_slice(shape, out):
            # return out
            # if start == 0 and step == 1:
            #  return out
            from tensorflow.python.ops.gen_array_ops import strided_slice_grad

            out = strided_slice_grad(
                shape=tf.convert_to_tensor(shape),
                begin=tf.constant([start]),
                end=tf.constant([0], dtype=tf.int32),
                end_mask=1,
                strides=tf.constant([step]),
                dy=out,
            )
            assert isinstance(out, tf.Tensor)
            out.set_shape(tf.TensorShape(shape))
            return out

        h = reverse_strided_slice(shape=(n_time, n_batch, n_dim), out=h)
        return h

    y1 = ta_identity(x)
    y2 = wrapped(x)
    # y1 = tf.identity(y1)
    # y1 = y1[start::step]
    # y2 = tf.constant(0)
    (dx1,) = tf.gradients(ys=[y1], grad_ys=[dy], xs=[x])
    (dx2,) = tf.gradients(ys=[y2], grad_ys=[dy], xs=[x])
    # dx2 = tf.constant(-1)
    if dx1 is None:
        dx1 = tf.constant(-1)
    if dx2 is None:
        dx2 = tf.constant(-1)
    vx, vdy, vy1, vy2, vdx1, vdx2 = session.run([x, dy, y1, y2, dx1, dx2])
    print("x:")
    print(vx)
    print("y1:")
    print(vy1)
    print("y2:")
    print(vy2)
    print("dy:")
    print(vdy)
    print("dx1:")
    print(vdx1)
    print("dx2:")
    print(vdx2)
    assert_allclose(vx, vy1)
    assert_allclose(vy1[start::step], vy2[start::step])
    assert_allclose(vdy, vdx1)
    assert_allclose(vdx1[start::step], vdx2[start::step])
//  [python]
def test_tensorarray_grad():
    def gen(shape, offset):
        return (numpy.arange(numpy.prod(shape)) + offset).reshape(shape).astype("float32")

    n_time = 3
    n_batch = 2
    n_dim = 2
    x = gen((n_time, n_batch, n_dim), offset=-4)
    dy = gen((n_time, n_batch, n_dim), offset=-7)
    start = 0
    step = 1

    x = tf.convert_to_tensor(x)
    x.set_shape(tf.TensorShape((n_time, n_batch, n_dim)))
    with tf.name_scope("gradients"):
        # Note that tensor_array_grad._GetGradSource() has this ugly hack
        # which requires that we have the "gradients" prefix.
        dy = tf.identity(tf.convert_to_tensor(dy), name="dy")
    dy.set_shape(tf.TensorShape((n_time, n_batch, n_dim)))

    def ta_identity(x):
        h = tf.TensorArray(tf.float32, size=n_time, element_shape=tf.TensorShape((n_batch, n_dim)))
        for t in range(n_time):
            h = h.write(index=t, value=x[t])
        h = h.stack()
        h.set_shape(tf.TensorShape((n_time, n_batch, n_dim)))
        return h

    def wrapped(x):
        x_ = x[start::step]
        x_.set_shape(tf.TensorShape((n_time, n_batch, n_dim)))
        h = ta_identity(x=x_)
        h.set_shape(tf.TensorShape((n_time, n_batch, n_dim)))

        def reverse_strided_slice(shape, out):
            # return out
            # if start == 0 and step == 1:
            #  return out
            from tensorflow.python.ops.gen_array_ops import strided_slice_grad

            out = strided_slice_grad(
                shape=tf.convert_to_tensor(shape),
                begin=tf.constant([start]),
                end=tf.constant([0], dtype=tf.int32),
                end_mask=1,
                strides=tf.constant([step]),
                dy=out,
            )
            assert isinstance(out, tf.Tensor)
            out.set_shape(tf.TensorShape(shape))
            return out

        h = reverse_strided_slice(shape=(n_time, n_batch, n_dim), out=h)
        return h

    y1 = ta_identity(x)
    y2 = wrapped(x)
    # y1 = tf.identity(y1)
    # y1 = y1[start::step]
    # y2 = tf.constant(0)
    (dx1,) = tf.gradients(ys=[y1], grad_ys=[dy], xs=[x])
    (dx2,) = tf.gradients(ys=[y2], grad_ys=[dy], xs=[x])
    # dx2 = tf.constant(-1)
    if dx1 is None:
        dx1 = tf.constant(-1)
    if dx2 is None:
        dx2 = tf.constant(-1)
    vx, vdy, vy1, vy2, vdx1, vdx2 = session.run([x, dy, y1, y2, dx1, dx2])
    print("x:")
    print(vx)
    print("y1:")
    print(vy1)
    print("y2:")
    print(vy2)
    print("dy:")
    print(vdy)
    print("dx1:")
    print(vdx1)
    print("dx2:")
    print(vdx2)
    assert_allclose(vx, vy1)
    assert_allclose(vy1[start::step], vy2[start::step])
    assert_allclose(vdy, vdx1)
    assert_allclose(vdx1[start::step], vdx2[start::step])
//  [python]
def update_summary(self):
        self.summary_nbvoxels_label.setText(
            f"Total number of voxels: {numpy.prod(self.cropped_image[0].shape)}"
        )
        self.summary_nbytes_label.setText(
            f"Total size in bytes: {human_readable_byte_size(self.cropped_image[0].nbytes)}"
        )
//  [python]
def julian_day(self, date):
        date = to_datetime(date)
        delta = date - datetime.datetime(date.year, 1, 1)
        julian_day = delta.days + delta.seconds / 86400.0
        return np.full((n,), julian_day)
//  [python]
def julian_day(self, date):
        date = to_datetime(date)
        delta = date - datetime.datetime(date.year, 1, 1)
        julian_day = delta.days + delta.seconds / 86400.0
        return np.full((np.prod(self.field.shape),), julian_day)
//  [python]
def __call__(self, arrs, name):
        """ Merge multiple Numpy array.

        Parameters
        ----------
        arrs : list of arrays.
            The input arrays.
        name : str
            The name of the external memory array.

        Returns
        -------
        Numpy array : an array stored in external memory.
        """
        assert isinstance(arrs, list)
        shape = _get_tot_shape(arrs)
        # If external memory workspace is not initialized or the feature size is smaller
        # than a threshold, we don't do anything.
        if self._ext_mem_workspace is None or reduce(lambda x, y: x * y, shape[1:]) < self._ext_mem_feat_size:
            if len(arrs) == 1 and isinstance(arrs[0], ExtMemArrayWrapper):
                return arrs[0].to_numpy()
            elif len(arrs) == 1:
                return arrs[0]
            else:
                return _merge_arrs(arrs, None)

        # We need to create the workspace directory if it doesn't exist.
        os.makedirs(self._ext_mem_workspace, exist_ok=True)
        tensor_path = os.path.join(self._ext_mem_workspace, name + ".npy")
        self._tensor_files.append(tensor_path)
        if len(arrs) > 1:
            return _merge_arrs(arrs, tensor_path)
        else:
            return convert_to_ext_mem_numpy(tensor_path, arrs[0])
//  [python]
def __call__(self, arrs, name):
        """ Merge multiple Numpy array.

        Parameters
        ----------
        arrs : list of arrays.
            The input arrays.
        name : str
            The name of the external memory array.

        Returns
        -------
        Numpy array : an array stored in external memory.
        """
        assert isinstance(arrs, list)
        shape = _get_tot_shape(arrs)
        # If external memory workspace is not initialized or the feature size is smaller
        # than a threshold, we don't do anything.
        if self._ext_mem_workspace is None or np.prod(shape[1:]) < self._ext_mem_feat_size:
            if len(arrs) == 1 and isinstance(arrs[0], ExtMemArrayWrapper):
                return arrs[0].to_numpy()
            elif len(arrs) == 1:
                return arrs[0]
            else:
                return _merge_arrs(arrs, None)

        # We need to create the workspace directory if it doesn't exist.
        os.makedirs(self._ext_mem_workspace, exist_ok=True)
        tensor_path = os.path.join(self._ext_mem_workspace, name + ".npy")
        self._tensor_files.append(tensor_path)
        if len(arrs) > 1:
            return _merge_arrs(arrs, tensor_path)
        else:
            return convert_to_ext_mem_numpy(tensor_path, arrs[0])
//  [python]
def __call__(self, points):
        """Evaluate polyonmial at given points.

        :param points: a point :math:`x` or array of points
          :math:`x_1,\\dots,x_m` with ``points.shape==(m,)``.
        :returns: :math:`p(x)` or array of shape ``(m,)`` with
          :math:`p(x_1),\\dots,p(x_m)`.
        """
        # check input
        p = numpy.asarray(points)
        if len(p.shape) > 1:
            raise ArgumentError(
                "scalar or one-dimensional array of points " "expected."
            )
        n = self.roots.shape[0]
        vals = 1 - p / self.roots.reshape(n, 1)

        # prevent under/overflow by multiplying interlaced large and small
        # values
        for j in range(vals.shape[1]):
            sort_tmp = numpy.argsort(numpy.abs(vals[:, j]))
            sort = numpy.zeros((n,), dtype=numpy.int)
            mid = int(numpy.ceil(float(n) / 2))
            sort[::2] = sort_tmp[:mid]
            sort[1::2] = sort_tmp[mid:][::-1]
            vals[:, j] = vals[sort, j]

        # form product of each column
        vals = numpy.multiply.reduce(vals, axis=0)

        if numpy.isscalar(points):
            return vals.item()
        return vals
//  [python]
def __call__(self, points):
        """Evaluate polyonmial at given points.

        :param points: a point :math:`x` or array of points
          :math:`x_1,\\dots,x_m` with ``points.shape==(m,)``.
        :returns: :math:`p(x)` or array of shape ``(m,)`` with
          :math:`p(x_1),\\dots,p(x_m)`.
        """
        # check input
        p = numpy.asarray(points)
        if len(p.shape) > 1:
            raise ArgumentError(
                "scalar or one-dimensional array of points " "expected."
            )
        n = self.roots.shape[0]
        vals = 1 - p / self.roots.reshape(n, 1)

        # prevent under/overflow by multiplying interlaced large and small
        # values
        for j in range(vals.shape[1]):
            sort_tmp = numpy.argsort(numpy.abs(vals[:, j]))
            sort = numpy.zeros((n,), dtype=numpy.int)
            mid = int(numpy.ceil(float(n) / 2))
            sort[::2] = sort_tmp[:mid]
            sort[1::2] = sort_tmp[mid:][::-1]
            vals[:, j] = vals[sort, j]

        # form product of each column
        vals = numpy.prod(vals, axis=0)

        if numpy.isscalar(points):
            return vals.item()
        return vals
//  [python]
def pre_checks(self):
        """
        Checks on the number of sites, atomic groups and size of the
        disaggregation matrix.
        """
        if self.N >= 32768:
            raise ValueError('You can disaggregate at max 32,768 sites')
        few = self.oqparam.max_sites_disagg
        if self.N > few:
            raise ValueError(
                'The number of sites is to disaggregate is %d, but you have '
                'max_sites_disagg=%d' % (self.N, few))
        self.oqparam.mags_by_trt = self.datastore['source_mags']
        all_edges, shapedic = disagg.get_edges_shapedic(
            self.oqparam, self.sitecol, self.R)
        *b, trts = all_edges
        T = len(trts)
        shape = [len(bin) - 1 for bin in
                 (b[0], b[1], b[2][0], b[3][0], b[4])] + [T]
        matrix_size = n.prod(shape)  # 6D
        if matrix_size > 1E6:
            raise ValueError(
                'The disaggregation matrix is too large '
                '(%d elements): fix the binning!' % matrix_size)
//  [python]
def pre_checks(self):
        """
        Checks on the number of sites, atomic groups and size of the
        disaggregation matrix.
        """
        if self.N >= 32768:
            raise ValueError('You can disaggregate at max 32,768 sites')
        few = self.oqparam.max_sites_disagg
        if self.N > few:
            raise ValueError(
                'The number of sites is to disaggregate is %d, but you have '
                'max_sites_disagg=%d' % (self.N, few))
        self.oqparam.mags_by_trt = self.datastore['source_mags']
        all_edges, shapedic = disagg.get_edges_shapedic(
            self.oqparam, self.sitecol, self.R)
        *b, trts = all_edges
        T = len(trts)
        shape = [len(bin) - 1 for bin in
                 (b[0], b[1], b[2][0], b[3][0], b[4])] + [T]
        matrix_size = numpy.prod(shape)  # 6D
        if matrix_size > 1E6:
            raise ValueError(
                'The disaggregation matrix is too large '
                '(%d elements): fix the binning!' % matrix_size)
//  [python]
def cheb1ap(N, rp):
    """Return (z,p,k) zero, pole, gain for Nth order Chebyshev type I lowpass
    analog filter prototype with `rp` decibels of ripple in the passband.

    The filter's angular (e.g. rad/s) cutoff frequency is normalized to 1,
    defined as the point at which the gain first drops below -`rp`.

    """
    if abs(int(N)) != N:
        raise ValueError("Filter order must be a nonnegative integer")
    elif N == 0:
        # Avoid divide-by-zero error
        # Even order filters have DC gain of -rp dB
        return numpy.array([]), numpy.array([]), 10**(-rp/20)
    z = numpy.array([])

    # Ripple factor (epsilon)
    eps = numpy.sqrt(10 ** (0.1 * rp) - 1.0)
    mu = 1.0 / N * arcsinh(1 / eps)

    # Arrange poles in an ellipse on the left half of the S-plane
    m = numpy.arange(-N+1, N, 2)
    theta = pi * m / (2*N)
    p = -sinh(mu + 1j*theta)

    k = n.prod(-p, axis=0).real
    if N % 2 == 0:
        k = k / sqrt((1 + eps * eps))

    return z, p, k
//  [python]
def cheb1ap(N, rp):
    """Return (z,p,k) zero, pole, gain for Nth order Chebyshev type I lowpass
    analog filter prototype with `rp` decibels of ripple in the passband.

    The filter's angular (e.g. rad/s) cutoff frequency is normalized to 1,
    defined as the point at which the gain first drops below -`rp`.

    """
    if abs(int(N)) != N:
        raise ValueError("Filter order must be a nonnegative integer")
    elif N == 0:
        # Avoid divide-by-zero error
        # Even order filters have DC gain of -rp dB
        return numpy.array([]), numpy.array([]), 10**(-rp/20)
    z = numpy.array([])

    # Ripple factor (epsilon)
    eps = numpy.sqrt(10 ** (0.1 * rp) - 1.0)
    mu = 1.0 / N * arcsinh(1 / eps)

    # Arrange poles in an ellipse on the left half of the S-plane
    m = numpy.arange(-N+1, N, 2)
    theta = pi * m / (2*N)
    p = -sinh(mu + 1j*theta)

    k = numpy.prod(-p, axis=0).real
    if N % 2 == 0:
        k = k / sqrt((1 + eps * eps))

    return z, p, k
//  [python]
def generate(self, rng, shape):
        axis_ind = pack(self.axis)
        other_ind = [i for i in range(len(shape)) if i not in axis_ind]
        axis_shapes = [shape[i] for i in axis_ind]
        other_shapes = [shape[i] for i in other_ind]
        matrix = super(SparseND, self).generate(rng,
                                                (n.prod(axis_shapes),
                                                 n.prod(other_shapes)))
        unflattened = matrix.reshape(tuple(axis_shapes) + tuple(other_shapes))
        wrong_ind = axis_ind + other_ind
        transp_ind = [wrong_ind.index(i) for i in range(len(shape))]
        return unflattened.transpose(transp_ind)
//  [python]
def generate(self, rng, shape):
        axis_ind = pack(self.axis)
        other_ind = [i for i in range(len(shape)) if i not in axis_ind]
        axis_shapes = [shape[i] for i in axis_ind]
        other_shapes = [shape[i] for i in other_ind]
        matrix = super(SparseND, self).generate(rng,
                                                (numpy.prod(axis_shapes),
                                                 numpy.prod(other_shapes)))
        unflattened = matrix.reshape(tuple(axis_shapes) + tuple(other_shapes))
        wrong_ind = axis_ind + other_ind
        transp_ind = [wrong_ind.index(i) for i in range(len(shape))]
        return unflattened.transpose(transp_ind)
//  [python]
def _mom(self, k, a):
        if k > a:
            return self._upper(a)
        return 1.0 / n.prod(a - n.arange(1, k.item() + 1))
//  [python]
def _mom(self, k, a):
        if k > a:
            return self._upper(a)
        return 1.0 / numpy.prod(a - numpy.arange(1, k.item() + 1))
//  [python]
def cheb2ap(N, rs):
    """Return (z,p,k) zero, pole, gain for Nth order Chebyshev type II lowpass
    analog filter prototype with `rs` decibels of ripple in the stopband.

    The filter's angular (e.g. rad/s) cutoff frequency is normalized to 1,
    defined as the point at which the gain first reaches -`rs`.

    """
    if abs(int(N)) != N:
        raise ValueError("Filter order must be a nonnegative integer")
    elif N == 0:
        # Avoid divide-by-zero warning
        return numpy.array([]), numpy.array([]), 1

    # Ripple factor (epsilon)
    de = 1.0 / sqrt(10 ** (0.1 * rs) - 1)
    mu = arcsinh(1.0 / de) / N

    if N % 2:
        m = numpy.concatenate((numpy.arange(-N+1, 0, 2),
                               numpy.arange(   2, N, 2)))
    else:
        m = numpy.arange(-N+1, N, 2)

    z = -conjugate(1j / sin(m * pi / (2.0 * N)))

    # Poles around the unit circle like Butterworth
    p = -exp(1j * pi * numpy.arange(-N+1, N, 2) / (2 * N))
    # Warp into Chebyshev II
    p = sinh(mu) * p.real + 1j * cosh(mu) * p.imag
    p = 1.0 / p

    k = (n.prod(-p, axis=0) / n.prod(-z, axis=0)).real
    return z, p, k
//  [python]
def cheb2ap(N, rs):
    """Return (z,p,k) zero, pole, gain for Nth order Chebyshev type II lowpass
    analog filter prototype with `rs` decibels of ripple in the stopband.

    The filter's angular (e.g. rad/s) cutoff frequency is normalized to 1,
    defined as the point at which the gain first reaches -`rs`.

    """
    if abs(int(N)) != N:
        raise ValueError("Filter order must be a nonnegative integer")
    elif N == 0:
        # Avoid divide-by-zero warning
        return numpy.array([]), numpy.array([]), 1

    # Ripple factor (epsilon)
    de = 1.0 / sqrt(10 ** (0.1 * rs) - 1)
    mu = arcsinh(1.0 / de) / N

    if N % 2:
        m = numpy.concatenate((numpy.arange(-N+1, 0, 2),
                               numpy.arange(   2, N, 2)))
    else:
        m = numpy.arange(-N+1, N, 2)

    z = -conjugate(1j / sin(m * pi / (2.0 * N)))

    # Poles around the unit circle like Butterworth
    p = -exp(1j * pi * numpy.arange(-N+1, N, 2) / (2 * N))
    # Warp into Chebyshev II
    p = sinh(mu) * p.real + 1j * cosh(mu) * p.imag
    p = 1.0 / p

    k = (numpy.prod(-p, axis=0) / numpy.prod(-z, axis=0)).real
    return z, p, k
//  [python]
def _empty_aligned(shape, alignment=8):
    if alignment <= 1:
        return numpy.empty(shape)

    size = numpy.prod(shape)
    buf = numpy.empty(size + alignment - 1)
    align8 = alignment * 8
    offset = buf.ctypes.data % align8
    if offset != 0:
        offset = (align8 - offset) // 8
    return numpy.ndarray(size, buffer=buf[offset:offset+size]).reshape(shape)
//  [python]
def calc(self, pl, pr, lhs, rhs, *args, **kwargs):
        if self.do_pad and len(args[0]) == n.prod(self.dshape):
            self.do_pad = False

        data = rhs(pr, *self.args, **self.kwargs)
        (data, dshape) = self.init_data(data)

        if self.kernel is None or not self.frozen:
            kernel = lhs(pl, *self.args, **self.kwargs)
            (self.kernel, self.skshape) = self.init_kernel(kernel)

        vals = self.convolve(data, dshape, self.kernel, self.skshape)
        return self.deinit(vals)
//  [python]
def calc(self, pl, pr, lhs, rhs, *args, **kwargs):
        if self.do_pad and len(args[0]) == numpy.prod(self.dshape):
            self.do_pad = False

        data = rhs(pr, *self.args, **self.kwargs)
        (data, dshape) = self.init_data(data)

        if self.kernel is None or not self.frozen:
            kernel = lhs(pl, *self.args, **self.kwargs)
            (self.kernel, self.skshape) = self.init_kernel(kernel)

        vals = self.convolve(data, dshape, self.kernel, self.skshape)
        return self.deinit(vals)
//  [python]
def run(self, xs, ys=None, batch_size=None):
        self._check_input_compatibility(xs, ys, batch_size)
        input_shape = xs.shape[1:]
        batch_size = xs.shape[0]
        total_dim = np.asscalar(np.multiply.reduce(input_shape))

        # Create mask
        index_matrix = np.arange(total_dim).reshape(input_shape)
        idx_patches = view_as_windows(index_matrix, self.window_shape, self.step).reshape((-1,) + self.window_shape)
        heatmap = np.zeros_like(xs, dtype=np.float32).reshape((-1), total_dim)
        w = np.zeros_like(heatmap)

        # Compute original output
        eval0 = self._session_run(self.T, xs, ys, batch_size)

        # Start perturbation loop
        for i, p in enumerate(idx_patches):
            mask = np.ones(input_shape).flatten()
            mask[p.flatten()] = self.replace_value
            masked_xs = mask.reshape((1,) + input_shape) * xs
            delta = eval0 - self._session_run(self.T, masked_xs, ys, batch_size)
            delta_aggregated = np.sum(delta.reshape((batch_size, -1)), -1, keepdims=True)
            heatmap[:, p.flatten()] += delta_aggregated
            w[:, p.flatten()] += p.size

        attribution = np.reshape(heatmap / w, xs.shape)
        if np.isnan(attribution).any():
            warnings.warn('Attributions generated by Occlusion method contain nans, '
                          'probably because window_shape and step do not allow to cover the all input.')
        return attribution
//  [python]
def run(self, xs, ys=None, batch_size=None):
        self._check_input_compatibility(xs, ys, batch_size)
        input_shape = xs.shape[1:]
        batch_size = xs.shape[0]
        total_dim = np.asscalar(np.prod(input_shape))

        # Create mask
        index_matrix = np.arange(total_dim).reshape(input_shape)
        idx_patches = view_as_windows(index_matrix, self.window_shape, self.step).reshape((-1,) + self.window_shape)
        heatmap = np.zeros_like(xs, dtype=np.float32).reshape((-1), total_dim)
        w = np.zeros_like(heatmap)

        # Compute original output
        eval0 = self._session_run(self.T, xs, ys, batch_size)

        # Start perturbation loop
        for i, p in enumerate(idx_patches):
            mask = np.ones(input_shape).flatten()
            mask[p.flatten()] = self.replace_value
            masked_xs = mask.reshape((1,) + input_shape) * xs
            delta = eval0 - self._session_run(self.T, masked_xs, ys, batch_size)
            delta_aggregated = np.sum(delta.reshape((batch_size, -1)), -1, keepdims=True)
            heatmap[:, p.flatten()] += delta_aggregated
            w[:, p.flatten()] += p.size

        attribution = np.reshape(heatmap / w, xs.shape)
        if np.isnan(attribution).any():
            warnings.warn('Attributions generated by Occlusion method contain nans, '
                          'probably because window_shape and step do not allow to cover the all input.')
        return attribution
//  [python]
def run(self, xs, ys=None, batch_size=None):
        xs_shape = list(xs.shape)
        batch_size = xs.shape[0]
        n_features = int(np.asscalar(np.prod([xs.shape[i] for i in self.sampling_dims])))
        result = np.zeros((xs_shape[0], n_features))

        run_shape = list(xs_shape)  # a copy
        run_shape = np.delete(run_shape, self.sampling_dims).tolist()
        run_shape.insert(1, -1)

        reconstruction_shape = [xs_shape[0]]
        for j in self.sampling_dims:
            reconstruction_shape.append(xs_shape[j])

        for r in range(self.samples):
            p = np.random.permutation(n_features)
            x = xs.copy().reshape(run_shape)
            y = None
            for i in p:
                if y is None:
                    y = self._session_run(self.T, x.reshape(xs_shape), ys, batch_size)
                x[:, i] = 0
                y0 = self._session_run(self.T, x.reshape(xs_shape), ys, batch_size)
                delta = y - y0
                delta_aggregated = np.sum(delta.reshape((batch_size, -1)), -1, keepdims=False)
                result[:, i] += delta_aggregated
                y = y0

        shapley = result / self.samples
        return shapley.reshape(reconstruction_shape)
//  [python]
def make_CP_decoder(buffers):
    def decoder(dct, buffers=buffers):
        if "__ndarray__" in dct:
            buf = memoryview(buffers[dct["idx"]])
            shape = dct["shape"]
            dtype = dct["dtype"]
            if n.prod(shape) == 0:
                return numpy.zeros(shape, dtype)
            return numpy.frombuffer(buf, dtype=dtype).reshape(shape).copy()
        if "__buffer__" in dct:
            return memoryview(buffers[dct["idx"]])
        if "__CPGridInfo__" in dct:
            grid = cellprofiler_core.utilities.grid.Grid()
            grid.deserialize(dct)
            return grid
        return dct

    return decoder
//  [python]
def make_CP_decoder(buffers):
    def decoder(dct, buffers=buffers):
        if "__ndarray__" in dct:
            buf = memoryview(buffers[dct["idx"]])
            shape = dct["shape"]
            dtype = dct["dtype"]
            if numpy.prod(shape) == 0:
                return numpy.zeros(shape, dtype)
            return numpy.frombuffer(buf, dtype=dtype).reshape(shape).copy()
        if "__buffer__" in dct:
            return memoryview(buffers[dct["idx"]])
        if "__CPGridInfo__" in dct:
            grid = cellprofiler_core.utilities.grid.Grid()
            grid.deserialize(dct)
            return grid
        return dct

    return decoder
//  [python]
def pprod(p, axis=None):
    """
    Probability product 1 - prod(1-p)
    """
    return 1. - n.prod(1. - p, axis)
//  [python]
def pprod(p, axis=None):
    """
    Probability product 1 - prod(1-p)
    """
    return 1. - numpy.prod(1. - p, axis)
//  [python]
def test_compute_slices(memory_ds):
    ds = memory_ds
    shape = (64, 16, 16, 16)
    data = np.arange(np.prod(shape)).reshape(shape)
    ds.create_tensor("data")
    ds.data.extend(data)

    _check_tensor(ds.data[:], data[:])
    _check_tensor(ds.data[10:20], data[10:20])
    _check_tensor(ds.data[5], data[5])
    _check_tensor(ds.data[0][:], data[0][:])
    _check_tensor(ds.data[-1][:], data[-1][:])
    _check_tensor(ds.data[3, 3], data[3, 3])
    _check_tensor(ds.data[30:40, :, 8:11, 4], data[30:40, :, 8:11, 4])
    _check_tensor(ds.data[16, 4, 5, 1:3], data[16, 4, 5, 1:3])
    _check_tensor(ds[[0, 1, 2, 5, 6, 10, 60]].data, data[[0, 1, 2, 5, 6, 10, 60]])
    _check_tensor(ds.data[[0, 1, -2, 5, -6, 10, 60]], data[[0, 1, -2, 5, -6, 10, 60]])
    _check_tensor(ds.data[0][[0, 1, 2, 5, 6, 10, 15]], data[0][[0, 1, 2, 5, 6, 10, 15]])
    _check_tensor(ds.data[[3, 2, 1, 0]][0], data[[3, 2, 1, 0]][0])
    _check_tensor(ds[[3, 2, 1, 0]][0].data, data[[3, 2, 1, 0]][0])
    _check_tensor(ds[[3, 2, 1, 0]].data[0], data[3])
    _check_tensor(ds[(0, 1, 6, 10, 15), :].data, data[(0, 1, 6, 10, 15), :])
    _check_tensor(ds.data[(0, 1, 6, 10, 15), :], data[(0, 1, 6, 10, 15), :])
    _check_tensor(ds.data[0][(0, 1, 6, 10, 15), :], data[0][(0, 1, 6, 10, 15), :])
    _check_tensor(ds.data[0, (0, 1, 5)], data[0, (0, 1, 5)])
    _check_tensor(ds.data[:, :][0], data[:, :][0])
    _check_tensor(ds.data[:, :][0:2], data[:, :][0:2])
    _check_tensor(ds.data[0, :][0:2], data[0, :][0:2])
    _check_tensor(ds.data[:, 0][0:2], data[:, 0][0:2])
    _check_tensor(ds.data[:, 0][0:2], data[:, 0][0:2])
    _check_tensor(ds.data[:, :][0][(0, 1, 2), 0], data[:, :][0][(0, 1, 2), 0])
    _check_tensor(ds.data[0][(0, 1, 2), 0][1], data[0][(0, 1, 2), 0][1])
    _check_tensor(ds.data[:, :][0][(0, 1, 2), 0][1], data[:, :][0][(0, 1, 2), 0][1])
    _check_tensor(ds.data[::-1], data[::-1])
    _check_tensor(ds.data[::-3], data[::-3])
    _check_tensor(ds.data[::-3][4], data[::-3][4])
    _check_tensor(ds.data[-2:], data[-2:])
    _check_tensor(ds.data[-6:][3], data[-6:][3])
    _check_tensor(ds.data[:-6:][3], data[:-6:][3])