jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def test_global_seed_and_device_seed(self, measurements, max_workers):
        """Test that a global seed does not affect the result of devices
        provided with a seed"""
        qs = qml.tape.QuantumScript([qml.Hadamard(0)], measurements, shots=1000)

        # expected result
        dev1 = DefaultQubit(seed=123, max_workers=max_workers)
        result1 = dev1.execute(qs)

        # set a global seed both before initialization of the
        # device and before execution of the tape
        np.random.seed(456)
        dev2 = DefaultQubit(seed=123, max_workers=max_workers)
        np.random.seed(789)
        result2 = dev2.execute(qs)

        if len(measurements) == 1:
            result1, result2 = [result1], [result2]

        assert all(n.all(res1 == res2) for res1, res2 in zip(result1, result2))
//  [python]
def test_global_seed_and_device_seed(self, measurements, max_workers):
        """Test that a global seed does not affect the result of devices
        provided with a seed"""
        qs = qml.tape.QuantumScript([qml.Hadamard(0)], measurements, shots=1000)

        # expected result
        dev1 = DefaultQubit(seed=123, max_workers=max_workers)
        result1 = dev1.execute(qs)

        # set a global seed both before initialization of the
        # device and before execution of the tape
        np.random.seed(456)
        dev2 = DefaultQubit(seed=123, max_workers=max_workers)
        np.random.seed(789)
        result2 = dev2.execute(qs)

        if len(measurements) == 1:
            result1, result2 = [result1], [result2]

        assert all(np.all(res1 == res2) for res1, res2 in zip(result1, result2))
//  [python]
def test_alloc_diag_values(self):
        for x, test_val in self._generator():
            for offset, axis1, axis2 in [
                (0, 0, 1),
                (0, 1, 2),
                (1, 0, 1),
                (0, 1, 3),
                (0, 2, 3),
                (1, 2, 3),
                (-1, 0, 1),
                (-2, 0, 1),
                (-1, 1, 2),
            ]:
                # Test AllocDiag values
                if np.maximum(axis1, axis2) > len(test_val.shape):
                    continue
                adiag_op = self.alloc_diag(offset=offset, axis1=axis1, axis2=axis2)
                f = aesara.function([x], adiag_op(x))
                # AllocDiag and extract the diagonal again
                # to check
                diag_arr = f(test_val)
                rediag = np.diagonal(diag_arr, offset=offset, axis1=axis1, axis2=axis2)
                assert np.all(rediag == test_val)

                # Test infer_shape
                f_shape = aesara.function([x], adiag_op(x).shape, mode="FAST_RUN")

                # aesara.printing.debugprint(f_shape.maker.fgraph.outputs[0])
                output_shape = f_shape(test_val)
                assert not any(
                    isinstance(node.op, self.alloc_diag)
                    for node in f_shape.maker.fgraph.toposort()
                )
                rediag_shape = np.diagonal(
                    np.ones(output_shape), offset=offset, axis1=axis1, axis2=axis2
                ).shape
                assert np.all(rediag_shape == test_val.shape)

                diag_x = adiag_op(x)
                sum_diag_x = at_sum(diag_x)
                grad_x = aesara.grad(sum_diag_x, x)
                grad_diag_x = aesara.grad(sum_diag_x, diag_x)
                f_grad_x = aesara.function([x], grad_x, mode=self.mode)
                f_grad_diag_x = aesara.function([x], grad_diag_x, mode=self.mode)
                grad_input = f_grad_x(test_val)
                grad_diag_input = f_grad_diag_x(test_val)
                true_grad_input = np.diagonal(
                    grad_diag_input, offset=offset, axis1=axis1, axis2=axis2
                )

                assert np.all(true_grad_input == grad_input)
//  [python]
def test_transpose():
    x1 = dvector("x1")
    x2 = dmatrix("x2")
    x3 = dtensor3("x3")

    x1v = np.arange(24)
    x2v = np.arange(24).reshape(2, 12)
    x3v = np.arange(24).reshape(2, 3, 4)

    f = aesara.function(
        [x1, x2, x3],
        [
            at.transpose(x1),
            at.transpose(x2),
            at.transpose(x3),
            x1.transpose(),
            x2.transpose(),
            x3.transpose(),
            x2.transpose(0, 1),
            x3.transpose((0, 2, 1)),
            at.transpose(x2, [0, 1]),
            at.transpose(x3, [0, 2, 1]),
        ],
    )

    t1, t2, t3, t1b, t2b, t3b, t2c, t3c, t2d, t3d = f(x1v, x2v, x3v)
    assert t1.shape == np.transpose(x1v).shape
    assert t2.shape == np.transpose(x2v).shape
    assert t3.shape == np.transpose(x3v).shape
    assert n.all(t1 == np.transpose(x1v))
    assert n.all(t2 == np.transpose(x2v))
    assert n.all(t3 == np.transpose(x3v))
    assert n.all(t1b == x1v.transpose())
    assert n.all(t2b == x2v.transpose())
    assert n.all(t3b == x3v.transpose())
    assert t2c.shape == (2, 12)
    assert t3c.shape == (2, 4, 3)
    assert n.all(t2c == x2v.transpose([0, 1]))
    assert n.all(t3c == x3v.transpose([0, 2, 1]))
    assert t2d.shape == (2, 12)
    assert t3d.shape == (2, 4, 3)
    assert n.all(t2d == np.transpose(x2v, [0, 1]))
    assert n.all(t3d == np.transpose(x3v, [0, 2, 1]))

    # Check that we create a name.
    assert at.transpose(x1).name == "x1.T"
    assert at.transpose(x2).name == "x2.T"
    assert at.transpose(x3).name == "x3.T"
    assert at.transpose(dmatrix()).name is None
//  [python]
def test_transpose():
    x1 = dvector("x1")
    x2 = dmatrix("x2")
    x3 = dtensor3("x3")

    x1v = np.arange(24)
    x2v = np.arange(24).reshape(2, 12)
    x3v = np.arange(24).reshape(2, 3, 4)

    f = aesara.function(
        [x1, x2, x3],
        [
            at.transpose(x1),
            at.transpose(x2),
            at.transpose(x3),
            x1.transpose(),
            x2.transpose(),
            x3.transpose(),
            x2.transpose(0, 1),
            x3.transpose((0, 2, 1)),
            at.transpose(x2, [0, 1]),
            at.transpose(x3, [0, 2, 1]),
        ],
    )

    t1, t2, t3, t1b, t2b, t3b, t2c, t3c, t2d, t3d = f(x1v, x2v, x3v)
    assert t1.shape == np.transpose(x1v).shape
    assert t2.shape == np.transpose(x2v).shape
    assert t3.shape == np.transpose(x3v).shape
    assert np.all(t1 == np.transpose(x1v))
    assert np.all(t2 == np.transpose(x2v))
    assert np.all(t3 == np.transpose(x3v))
    assert np.all(t1b == x1v.transpose())
    assert np.all(t2b == x2v.transpose())
    assert np.all(t3b == x3v.transpose())
    assert t2c.shape == (2, 12)
    assert t3c.shape == (2, 4, 3)
    assert np.all(t2c == x2v.transpose([0, 1]))
    assert np.all(t3c == x3v.transpose([0, 2, 1]))
    assert t2d.shape == (2, 12)
    assert t3d.shape == (2, 4, 3)
    assert np.all(t2d == np.transpose(x2v, [0, 1]))
    assert np.all(t3d == np.transpose(x3v, [0, 2, 1]))

    # Check that we create a name.
    assert at.transpose(x1).name == "x1.T"
    assert at.transpose(x2).name == "x2.T"
    assert at.transpose(x3).name == "x3.T"
    assert at.transpose(dmatrix()).name is None
//  [python]
def test_same_device_prng_key(self, max_workers):
        """Test a device with a given jax.random.PRNGKey will produce
        the same samples repeatedly."""
        import jax

        qs = qml.tape.QuantumScript([qml.Hadamard(0)], [qml.sample(wires=0)], shots=1000)
        config = ExecutionConfig(interface="jax")

        dev = DefaultQubit(max_workers=max_workers, seed=jax.random.PRNGKey(123))
        result1 = dev.execute(qs, config)
        for _ in range(10):
            result2 = dev.execute(qs, config)

            assert np.all(result1 == result2)
//  [python]
def zpk2tf(z, p, k):
    """Return polynomial transfer function representation from zeros
    and poles

    Parameters
    ----------
    z : ndarray
        Zeros of the transfer function.
    p : ndarray
        Poles of the transfer function.
    k : float
        System gain.

    Returns
    -------
    b : ndarray
        Numerator polynomial.
    a : ndarray
        Denominator polynomial.

    """
    z = atleast_1d(z)
    k = atleast_1d(k)
    if len(z.shape) > 1:
        temp = poly(z[0])
        b = zeros((z.shape[0], z.shape[1] + 1), temp.dtype.char)
        if len(k) == 1:
            k = [k[0]] * z.shape[0]
        for i in range(z.shape[0]):
            b[i] = k[i] * poly(z[i])
    else:
        b = k * poly(z)
    a = atleast_1d(poly(p))

    # Use real output if possible.  Copied from numpy.poly, since
    # we can't depend on a specific version of numpy.
    if issubclass(b.dtype.type, numpy.complexfloating):
        # if complex roots are all complex conjugates, the roots are real.
        roots = numpy.asarray(z, complex)
        pos_roots = numpy.compress(roots.imag > 0, roots)
        neg_roots = numpy.conjugate(numpy.compress(roots.imag < 0, roots))
        if len(pos_roots) == len(neg_roots):
            if numpy.alltrue(numpy.sort_complex(neg_roots) ==
                         numpy.sort_complex(pos_roots)):
                b = b.real.copy()

    if issubclass(a.dtype.type, numpy.complexfloating):
        # if complex roots are all complex conjugates, the roots are real.
        roots = numpy.asarray(p, complex)
        pos_roots = numpy.compress(roots.imag > 0, roots)
        neg_roots = numpy.conjugate(numpy.compress(roots.imag < 0, roots))
        if len(pos_roots) == len(neg_roots):
            if numpy.alltrue(numpy.sort_complex(neg_roots) ==
                         numpy.sort_complex(pos_roots)):
                a = a.real.copy()

    return b, a
//  [python]
def zpk2tf(z, p, k):
    """Return polynomial transfer function representation from zeros
    and poles

    Parameters
    ----------
    z : ndarray
        Zeros of the transfer function.
    p : ndarray
        Poles of the transfer function.
    k : float
        System gain.

    Returns
    -------
    b : ndarray
        Numerator polynomial.
    a : ndarray
        Denominator polynomial.

    """
    z = atleast_1d(z)
    k = atleast_1d(k)
    if len(z.shape) > 1:
        temp = poly(z[0])
        b = zeros((z.shape[0], z.shape[1] + 1), temp.dtype.char)
        if len(k) == 1:
            k = [k[0]] * z.shape[0]
        for i in range(z.shape[0]):
            b[i] = k[i] * poly(z[i])
    else:
        b = k * poly(z)
    a = atleast_1d(poly(p))

    # Use real output if possible.  Copied from numpy.poly, since
    # we can't depend on a specific version of numpy.
    if issubclass(b.dtype.type, numpy.complexfloating):
        # if complex roots are all complex conjugates, the roots are real.
        roots = numpy.asarray(z, complex)
        pos_roots = numpy.compress(roots.imag > 0, roots)
        neg_roots = numpy.conjugate(numpy.compress(roots.imag < 0, roots))
        if len(pos_roots) == len(neg_roots):
            if numpy.all(numpy.sort_complex(neg_roots) ==
                         numpy.sort_complex(pos_roots)):
                b = b.real.copy()

    if issubclass(a.dtype.type, numpy.complexfloating):
        # if complex roots are all complex conjugates, the roots are real.
        roots = numpy.asarray(p, complex)
        pos_roots = numpy.compress(roots.imag > 0, roots)
        neg_roots = numpy.conjugate(numpy.compress(roots.imag < 0, roots))
        if len(pos_roots) == len(neg_roots):
            if numpy.all(numpy.sort_complex(neg_roots) ==
                         numpy.sort_complex(pos_roots)):
                a = a.real.copy()

    return b, a
//  [python]
def test_prng_key_multi_tapes(self, max_workers):
        """Test a device with a given jax.random.PRNGKey will produce
        different results for the same (batched) tape."""
        import jax

        qs = qml.tape.QuantumScript([qml.Hadamard(0)], [qml.sample(wires=0)], shots=1000)
        config = ExecutionConfig(interface="jax")

        dev = DefaultQubit(max_workers=max_workers, seed=jax.random.PRNGKey(123))
        result1, result2 = dev.execute([qs] * 2, config)

        assert not np.all(result1 == result2)
//  [python]
def test_different_executions_same_prng_key(self, max_workers):
        """Test that the same device will produce the same results every execution if
        the seed is a jax.random.PRNGKey"""
        import jax

        qs = qml.tape.QuantumScript([qml.Hadamard(0)], [qml.sample(wires=0)], shots=1000)
        config = ExecutionConfig(interface="jax")

        dev = DefaultQubit(max_workers=max_workers, seed=jax.random.PRNGKey(77))
        result1 = dev.execute(qs, config)
        result2 = dev.execute(qs, config)

        assert np.all(result1 == result2)
//  [python]
def test_same_prng_key(self, max_workers):
        """Test that different devices given the same random jax.random.PRNGKey as a seed will produce
        the same results for sample, even with different seeds"""
        import jax

        qs = qml.tape.QuantumScript([qml.Hadamard(0)], [qml.sample(wires=0)], shots=1000)
        config = ExecutionConfig(interface="jax")

        dev1 = DefaultQubit(max_workers=max_workers, seed=jax.random.PRNGKey(123))
        result1 = dev1.execute(qs, config)

        dev2 = DefaultQubit(max_workers=max_workers, seed=jax.random.PRNGKey(123))
        result2 = dev2.execute(qs, config)

        assert np.all(result1 == result2)
//  [python]
def test_different_max_workers_same_prng_key(self):
        """Test that devices with the same jax.random.PRNGKey but different threading will produce
        the same samples."""
        import jax

        qs = qml.tape.QuantumScript([qml.Hadamard(0)], [qml.sample(wires=0)], shots=1000)
        config = ExecutionConfig(interface="jax")

        dev1 = DefaultQubit(max_workers=None, seed=jax.random.PRNGKey(123))
        result1 = dev1.execute([qs] * 10, config)
        for max_workers in range(1, 3):
            dev2 = DefaultQubit(max_workers=max_workers, seed=jax.random.PRNGKey(123))
            result2 = dev2.execute([qs] * 10, config)

            assert len(result1) == len(result2)
            for r1, r2 in zip(result1, result2):
                assert n.all(r1 == r2)
//  [python]
def test_different_max_workers_same_prng_key(self):
        """Test that devices with the same jax.random.PRNGKey but different threading will produce
        the same samples."""
        import jax

        qs = qml.tape.QuantumScript([qml.Hadamard(0)], [qml.sample(wires=0)], shots=1000)
        config = ExecutionConfig(interface="jax")

        dev1 = DefaultQubit(max_workers=None, seed=jax.random.PRNGKey(123))
        result1 = dev1.execute([qs] * 10, config)
        for max_workers in range(1, 3):
            dev2 = DefaultQubit(max_workers=max_workers, seed=jax.random.PRNGKey(123))
            result2 = dev2.execute([qs] * 10, config)

            assert len(result1) == len(result2)
            for r1, r2 in zip(result1, result2):
                assert np.all(r1 == r2)
//  [python]
def test_shape_and_dtype(self, max_workers, n_qubits):
        """Test that the shape and dtype of the measurement is correct"""
        dev = DefaultQubit(max_workers=max_workers)

        ops = [qml.Hadamard(i) for i in range(n_qubits)]
        qs = qml.tape.QuantumScript(ops, [qml.classical_shadow(range(n_qubits))], shots=100)
        res = dev.execute(qs)

        assert res.shape == (2, 100, n_qubits)
        assert res.dtype == np.int8

        # test that the bits are either 0 and 1
        assert n.all(n.logical_or(res[0] == 0, res[0] == 1))

        # test that the recipes are either 0, 1, or 2 (X, Y, or Z)
        assert n.all(n.logical_or(n.logical_or(res[1] == 0, res[1] == 1), res[1] == 2))
//  [python]
def test_shape_and_dtype(self, max_workers, n_qubits):
        """Test that the shape and dtype of the measurement is correct"""
        dev = DefaultQubit(max_workers=max_workers)

        ops = [qml.Hadamard(i) for i in range(n_qubits)]
        qs = qml.tape.QuantumScript(ops, [qml.classical_shadow(range(n_qubits))], shots=100)
        res = dev.execute(qs)

        assert res.shape == (2, 100, n_qubits)
        assert res.dtype == np.int8

        # test that the bits are either 0 and 1
        assert np.all(np.logical_or(res[0] == 0, res[0] == 1))

        # test that the recipes are either 0, 1, or 2 (X, Y, or Z)
        assert np.all(np.logical_or(np.logical_or(res[1] == 0, res[1] == 1), res[1] == 2))
//  [python]
def test_multiple_shadow_measurements(self, n_qubits, max_workers):
        """Test that multiple classical shadow measurements work as expected"""
        dev = DefaultQubit(max_workers=max_workers)

        ops = [qml.Hadamard(i) for i in range(n_qubits)]
        mps = [qml.classical_shadow(range(n_qubits)), qml.classical_shadow(range(n_qubits))]
        qs = qml.tape.QuantumScript(ops, mps, shots=100)
        res = dev.execute(qs)

        assert isinstance(res, tuple)
        assert len(res) == 2

        for r in res:
            assert r.shape == (2, 100, n_qubits)
            assert r.dtype == np.int8

            # test that the bits are either 0 and 1
            assert n.all(n.logical_or(r[0] == 0, r[0] == 1))

            # test that the recipes are either 0, 1, or 2 (X, Y, or Z)
            assert n.all(n.logical_or(n.logical_or(r[1] == 0, r[1] == 1), r[1] == 2))

        # check that the samples are different
        assert not n.all(res[0] == res[1])
//  [python]
def test_multiple_shadow_measurements(self, n_qubits, max_workers):
        """Test that multiple classical shadow measurements work as expected"""
        dev = DefaultQubit(max_workers=max_workers)

        ops = [qml.Hadamard(i) for i in range(n_qubits)]
        mps = [qml.classical_shadow(range(n_qubits)), qml.classical_shadow(range(n_qubits))]
        qs = qml.tape.QuantumScript(ops, mps, shots=100)
        res = dev.execute(qs)

        assert isinstance(res, tuple)
        assert len(res) == 2

        for r in res:
            assert r.shape == (2, 100, n_qubits)
            assert r.dtype == np.int8

            # test that the bits are either 0 and 1
            assert np.all(np.logical_or(r[0] == 0, r[0] == 1))

            # test that the recipes are either 0, 1, or 2 (X, Y, or Z)
            assert np.all(np.logical_or(np.logical_or(r[1] == 0, r[1] == 1), r[1] == 2))

        # check that the samples are different
        assert not np.all(res[0] == res[1])
//  [python]
def test_shot_vectors(self, max_workers, n_qubits, shots):
        """Test that classical shadows works when given a shot vector"""
        dev = DefaultQubit(max_workers=max_workers)
        shots = qml.measurements.Shots(shots)

        ops = [qml.Hadamard(i) for i in range(n_qubits)]
        qs = qml.tape.QuantumScript(ops, [qml.classical_shadow(range(n_qubits))], shots=shots)
        res = dev.execute(qs)

        assert isinstance(res, tuple)
        assert len(res) == len(list(shots))

        for r, s in zip(res, shots):
            assert r.shape == (2, s, n_qubits)
            assert r.dtype == np.int8

            # test that the bits are either 0 and 1
            assert n.all(n.logical_or(r[0] == 0, r[0] == 1))

            # test that the recipes are either 0, 1, or 2 (X, Y, or Z)
            assert n.all(n.logical_or(n.logical_or(r[1] == 0, r[1] == 1), r[1] == 2))
//  [python]
def test_shot_vectors(self, max_workers, n_qubits, shots):
        """Test that classical shadows works when given a shot vector"""
        dev = DefaultQubit(max_workers=max_workers)
        shots = qml.measurements.Shots(shots)

        ops = [qml.Hadamard(i) for i in range(n_qubits)]
        qs = qml.tape.QuantumScript(ops, [qml.classical_shadow(range(n_qubits))], shots=shots)
        res = dev.execute(qs)

        assert isinstance(res, tuple)
        assert len(res) == len(list(shots))

        for r, s in zip(res, shots):
            assert r.shape == (2, s, n_qubits)
            assert r.dtype == np.int8

            # test that the bits are either 0 and 1
            assert np.all(np.logical_or(r[0] == 0, r[0] == 1))

            # test that the recipes are either 0, 1, or 2 (X, Y, or Z)
            assert np.all(np.logical_or(np.logical_or(r[1] == 0, r[1] == 1), r[1] == 2))
//  [python]
def test_04_04_get_all_object_measurements(self):
        r = numpy.random.RandomState()
        m = cellprofiler_core.measurement.Measurements()
        r.seed(42)
        vals = [r.uniform(size=r.randint(10, 100)) for _ in range(100)]
        bad_order = r.permutation(numpy.arange(1, 101))
        for image_number in bad_order:
            m.add_measurement(
                OBJECT_NAME,
                "Feature",
                vals[image_number - 1],
                image_set_number=image_number,
            )
        result = m.get_all_measurements(OBJECT_NAME, "Feature")
        assert all(
            [n.all(r == v) and len(r) == len(v) for r, v in zip(result, vals)]
        )
//  [python]
def test_04_04_get_all_object_measurements(self):
        r = numpy.random.RandomState()
        m = cellprofiler_core.measurement.Measurements()
        r.seed(42)
        vals = [r.uniform(size=r.randint(10, 100)) for _ in range(100)]
        bad_order = r.permutation(numpy.arange(1, 101))
        for image_number in bad_order:
            m.add_measurement(
                OBJECT_NAME,
                "Feature",
                vals[image_number - 1],
                image_set_number=image_number,
            )
        result = m.get_all_measurements(OBJECT_NAME, "Feature")
        assert all(
            [numpy.all(r == v) and len(r) == len(v) for r, v in zip(result, vals)]
        )
//  [python]
def test_04_04b_get_all_object_measurements_arrayinterface(self):
        r = numpy.random.RandomState()
        m = cellprofiler_core.measurement.Measurements()
        r.seed(42)
        vals = [r.uniform(size=r.randint(10, 100)) for _ in range(100)]
        bad_order = r.permutation(numpy.arange(1, 101))
        for image_number in bad_order:
            m[OBJECT_NAME, "Feature", image_number] = vals[image_number - 1]
        result = m[OBJECT_NAME, "Feature", :]
        assert all(
            [numpy.all(r == v) and len(r) == len(v) for r, v in zip(result, vals)]
        )
//  [python]
def _check_input_compatibility(self, xs, ys=None, batch_size=None):
        if ys is not None:
            if not self.has_multiple_inputs and len(xs) != len(ys):
                raise RuntimeError('When provided, ys must have the same batch size as xs (xs has batch size {} and ys {})'.format(len(xs), len(ys)))
            elif self.has_multiple_inputs and n.all([len(i) != len(ys) for i in xs]):
                raise RuntimeError('When provided, ys must have the same batch size as all elements of xs')
        if batch_size is not None and batch_size > 0:
            if self.T.shape[0].value is not None and self.T.shape[0].value is not batch_size:
                raise RuntimeError('When using batch evaluation, the first dimension of the target tensor '
                                   'must be compatible with the batch size. Found %s instead' % self.T.shape[0].value)
            if isinstance(self.X, list):
                for x in self.X:
                    if x.shape[0].value is not None and x.shape[0].value is not batch_size:
                        raise RuntimeError('When using batch evaluation, the first dimension of the input tensor '
                                           'must be compatible with the batch size. Found %s instead' % x.shape[
                                               0].value)
            else:
                if self.X.shape[0].value is not None and self.X.shape[0].value is not batch_size:
                    raise RuntimeError('When using batch evaluation, the first dimension of the input tensor '
                                       'must be compatible with the batch size. Found %s instead' % self.X.shape[0].value)
//  [python]
def _check_input_compatibility(self, xs, ys=None, batch_size=None):
        if ys is not None:
            if not self.has_multiple_inputs and len(xs) != len(ys):
                raise RuntimeError('When provided, ys must have the same batch size as xs (xs has batch size {} and ys {})'.format(len(xs), len(ys)))
            elif self.has_multiple_inputs and np.all([len(i) != len(ys) for i in xs]):
                raise RuntimeError('When provided, ys must have the same batch size as all elements of xs')
        if batch_size is not None and batch_size > 0:
            if self.T.shape[0].value is not None and self.T.shape[0].value is not batch_size:
                raise RuntimeError('When using batch evaluation, the first dimension of the target tensor '
                                   'must be compatible with the batch size. Found %s instead' % self.T.shape[0].value)
            if isinstance(self.X, list):
                for x in self.X:
                    if x.shape[0].value is not None and x.shape[0].value is not batch_size:
                        raise RuntimeError('When using batch evaluation, the first dimension of the input tensor '
                                           'must be compatible with the batch size. Found %s instead' % x.shape[
                                               0].value)
            else:
                if self.X.shape[0].value is not None and self.X.shape[0].value is not batch_size:
                    raise RuntimeError('When using batch evaluation, the first dimension of the input tensor '
                                       'must be compatible with the batch size. Found %s instead' % self.X.shape[0].value)
//  [python]
def _is_numeric_array(point):
    """Test whether a point is numerical object or an array containing only numerical objects"""

    def _is_numeric(item):
        return isinstance(item, (numbers.Number, numpy.ndarray))

    try:
        return numpy.alltrue(numpy.vectorize(_is_numeric)(point))
    except TypeError:
        return _is_numeric(point)

    return False
//  [python]
def _is_numeric_array(point):
    """Test whether a point is numerical object or an array containing only numerical objects"""

    def _is_numeric(item):
        return isinstance(item, (numbers.Number, numpy.ndarray))

    try:
        return numpy.all(numpy.vectorize(_is_numeric)(point))
    except TypeError:
        return _is_numeric(point)

    return False
//  [python]
def __contains__(self, point):
        """Check if constraints hold for this `point` of `Dimension`.

        :param point: a parameter corresponding to this `Dimension`.
        :type point: numeric or array-like

        .. note:: Default `Dimension` does not have any extra constraints.
           It just checks whether point lies inside the support and the shape.

        """
        if not _is_numeric_array(point):
            return False

        low, high = self.interval()

        point_ = numpy.asarray(point)

        if point_.shape != self.shape:
            return False

        return numpy.all(point_ >= low) and numpy.all(point_ <= high)
//  [python]
def __contains__(self, point):
        """Check if constraints hold for this `point` of `Dimension`.

        :param point: a parameter corresponding to this `Dimension`.
        :type point: numeric or array-like

        `Integer` will check whether `point` contains only integers.

        """
        if not _is_numeric_array(point):
            return False

        point_ = numpy.asarray(point)
        if not numpy.all(numpy.equal(numpy.mod(point_, 1), 0)):
            return False

        return super().__contains__(point)
//  [python]
def _run_repeat_layer(session, net, input_data_layer):
    repetitions_data_layer = InternalLayer(
        name="repetitions",
        network=net,
        output=Data(**{"name": "repetitions", "shape": (None,), "feature_dim_axis": None, "dtype": "int32"}),
    )
    repetitions_data_layer.output.placeholder = tf.constant(
        [[1, 3, 2, 1, 3, 4, 1, 1, 2, 1], [3, 2, 1, 3, 0, 1, 1, 0, 0, 1]], dtype="int32"
    )  # [B, T] (sparse), additional 1 in second sequence to test masking
    repetitions_data_layer.output.size_placeholder = {0: tf.constant([10, 7], dtype="int32")}  # [B]

    opts = {
        "network": net,
        "name": "repeat_layer_test",
        "sources": [input_data_layer],
        "repetitions": repetitions_data_layer,
        "axis": "T",
    }
    out_data = RepeatLayer.get_out_data_from_opts(**opts)
    out_data.sanity_check()
    print(out_data)
    repeat_layer = RepeatLayer(output=out_data, **opts)
    print(repeat_layer.output)

    output, size_placeholder = session.run(
        [repeat_layer.output.placeholder, repeat_layer.output.size_placeholder.as_dict()],
        feed_dict=make_feed_dict(net.extern_data, n_batch=2),
    )
    assert n.all(n.equal(size_placeholder[0], n.asarray([19, 11])))
    assert n.all(n.equal(output.shape, n.asarray([2, 19, 5])))
    # the 6 last positions of the second sequence need to be padded with zeros
    assert n.all(n.equal(output[1, 11:], 0))
    assert out_data.shape == (None, 5)
    assert out_data.batch_dim_axis == 0
    assert out_data.time_dim_axis == 1
//  [python]
def _run_repeat_layer(session, net, input_data_layer):
    repetitions_data_layer = InternalLayer(
        name="repetitions",
        network=net,
        output=Data(**{"name": "repetitions", "shape": (None,), "feature_dim_axis": None, "dtype": "int32"}),
    )
    repetitions_data_layer.output.placeholder = tf.constant(
        [[1, 3, 2, 1, 3, 4, 1, 1, 2, 1], [3, 2, 1, 3, 0, 1, 1, 0, 0, 1]], dtype="int32"
    )  # [B, T] (sparse), additional 1 in second sequence to test masking
    repetitions_data_layer.output.size_placeholder = {0: tf.constant([10, 7], dtype="int32")}  # [B]

    opts = {
        "network": net,
        "name": "repeat_layer_test",
        "sources": [input_data_layer],
        "repetitions": repetitions_data_layer,
        "axis": "T",
    }
    out_data = RepeatLayer.get_out_data_from_opts(**opts)
    out_data.sanity_check()
    print(out_data)
    repeat_layer = RepeatLayer(output=out_data, **opts)
    print(repeat_layer.output)

    output, size_placeholder = session.run(
        [repeat_layer.output.placeholder, repeat_layer.output.size_placeholder.as_dict()],
        feed_dict=make_feed_dict(net.extern_data, n_batch=2),
    )
    assert numpy.all(numpy.equal(size_placeholder[0], numpy.asarray([19, 11])))
    assert numpy.all(numpy.equal(output.shape, numpy.asarray([2, 19, 5])))
    # the 6 last positions of the second sequence need to be padded with zeros
    assert numpy.all(numpy.equal(output[1, 11:], 0))
    assert out_data.shape == (None, 5)
    assert out_data.batch_dim_axis == 0
    assert out_data.time_dim_axis == 1
//  [python]
def __contains__(self, point):
        """Check if constraints hold for this `point` of `Dimension`.

        :param point: a parameter corresponding to this `Dimension`.
        :type point: numeric or array-like

        """
        point_ = numpy.asarray(point, dtype=object)
        if point_.shape != self.shape:
            return False
        _check = numpy.vectorize(lambda x: x in self.categories)
        return numpy.all(_check(point_))
//  [python]
def predict(self, instance, node=None):
        # If no starting node is specified, start at the root of the tree
        if node is None:
            node = self.root

        # If we have reached a leaf node, return the corresponding class probability
        if 'class' in node:
            return node['class']

        # Otherwise, compare the instance's feature values to the node's threshold values and traverse the appropriate child
        threshold_values = node['threshold_values']
        one_hot_encoding = node['one_hot_encoding']
        feature_values = instance[one_hot_encoding == 1]
        if np.alltrue(feature_values <= threshold_values):
            return self.predict(instance, node['left'])
        else:
            return self.predict(instance, node['right'])
//  [python]
def predict(self, instance, node=None):
        # If no starting node is specified, start at the root of the tree
        if node is None:
            node = self.root

        # If we have reached a leaf node, return the corresponding class probability
        if 'class' in node:
            return node['class']

        # Otherwise, compare the instance's feature values to the node's threshold values and traverse the appropriate child
        threshold_values = node['threshold_values']
        one_hot_encoding = node['one_hot_encoding']
        feature_values = instance[one_hot_encoding == 1]
        if np.all(feature_values <= threshold_values):
            return self.predict(instance, node['left'])
        else:
            return self.predict(instance, node['right'])
//  [python]
def fillval(cls, v):
        """Check for FILLVAL presence, type, value

        Checks variable for existence of `FILLVAL
        <https://spdf.gsfc.nasa.gov/istp_guide/vattributes.html#FILLVAL>`_
        attribute and makes sure it is the same type as variable and matches
        ISTP value.

        Parameters
        ----------
        v : `~.pycdf.Var`
            Variable to check

        Returns
        -------
        list of str
            Description of each validation failure.

        See Also
        --------
        spacepy.pycdf.istp.fillval : Automatic setting of this value.
        """
        errs = []
        if not 'FILLVAL' in v.attrs:
            return ['No FILLVAL attribute.']
        if v.attrs.type('FILLVAL') != v.type():
            errs.append(
                'FILLVAL type {} does not match variable type {}.'.format(
                    spacepy.pycdf.lib.cdftypenames[v.attrs.type('FILLVAL')],
                    spacepy.pycdf.lib.cdftypenames[v.type()]))
        expected = fillval(v, ret=True)
        timetype = v.type() in spacepy.pycdf.lib.timetypes
        actual = (v.cdf_file.raw_var(v.name()) if timetype else v)\
                 .attrs['FILLVAL']
        match = numpy.isclose(
            actual, expected, atol=0, rtol=1e-7)\
            if numpy.issubdtype(v.dtype, numpy.floating)\
            else n.all(actual == expected)
        if not match:
            if timetype:
                converted_expected = {
                    spacepy.pycdf.const.CDF_EPOCH.value:
                    spacepy.pycdf.lib.v_epoch_to_datetime,
                    spacepy.pycdf.const.CDF_EPOCH16.value:
                    spacepy.pycdf.lib.v_epoch16_to_datetime,
                    spacepy.pycdf.const.CDF_TIME_TT2000.value:
                    spacepy.pycdf.lib.v_tt2000_to_datetime
                }[v.type()](expected)
                errs.append(
                    'FILLVAL {} ({}), should be {} ({}) for variable type {}.'
                    .format(
                        actual,
                        v.attrs['FILLVAL'],
                        expected,
                        converted_expected,
                        spacepy.pycdf.lib.cdftypenames[v.type()]))
            else:
                errs.append(
                    'FILLVAL {}, should be {} for variable type {}.'.format(
                        actual, expected,
                        spacepy.pycdf.lib.cdftypenames[v.type()]))
        return errs
//  [python]
def fillval(cls, v):
        """Check for FILLVAL presence, type, value

        Checks variable for existence of `FILLVAL
        <https://spdf.gsfc.nasa.gov/istp_guide/vattributes.html#FILLVAL>`_
        attribute and makes sure it is the same type as variable and matches
        ISTP value.

        Parameters
        ----------
        v : `~.pycdf.Var`
            Variable to check

        Returns
        -------
        list of str
            Description of each validation failure.

        See Also
        --------
        spacepy.pycdf.istp.fillval : Automatic setting of this value.
        """
        errs = []
        if not 'FILLVAL' in v.attrs:
            return ['No FILLVAL attribute.']
        if v.attrs.type('FILLVAL') != v.type():
            errs.append(
                'FILLVAL type {} does not match variable type {}.'.format(
                    spacepy.pycdf.lib.cdftypenames[v.attrs.type('FILLVAL')],
                    spacepy.pycdf.lib.cdftypenames[v.type()]))
        expected = fillval(v, ret=True)
        timetype = v.type() in spacepy.pycdf.lib.timetypes
        actual = (v.cdf_file.raw_var(v.name()) if timetype else v)\
                 .attrs['FILLVAL']
        match = numpy.isclose(
            actual, expected, atol=0, rtol=1e-7)\
            if numpy.issubdtype(v.dtype, numpy.floating)\
            else numpy.all(actual == expected)
        if not match:
            if timetype:
                converted_expected = {
                    spacepy.pycdf.const.CDF_EPOCH.value:
                    spacepy.pycdf.lib.v_epoch_to_datetime,
                    spacepy.pycdf.const.CDF_EPOCH16.value:
                    spacepy.pycdf.lib.v_epoch16_to_datetime,
                    spacepy.pycdf.const.CDF_TIME_TT2000.value:
                    spacepy.pycdf.lib.v_tt2000_to_datetime
                }[v.type()](expected)
                errs.append(
                    'FILLVAL {} ({}), should be {} ({}) for variable type {}.'
                    .format(
                        actual,
                        v.attrs['FILLVAL'],
                        expected,
                        converted_expected,
                        spacepy.pycdf.lib.cdftypenames[v.type()]))
            else:
                errs.append(
                    'FILLVAL {}, should be {} for variable type {}.'.format(
                        actual, expected,
                        spacepy.pycdf.lib.cdftypenames[v.type()]))
        return errs
//  [python]
def __enter__(self):
        self.entry_tab = self.db.get_node(self.entry_path)
        if not n.all(n.equal(self.entry_tab.col('EntryNr'),
                                     n.arange(1, len(self.entry_tab) + 1))):
            raise RuntimeError('entry table is not sorted')

        root, name = os.path.split(self.buffer_path)
        self.desc_buf = self.db.create_earray(root, name,
                                              tables.StringAtom(1), (0,), 'concatenated protein descriptions',
                                              expectedrows=len(self.entry_tab) * 100)
        self.cur_eNr = None
        self.cur_desc = []
        bufindex_dtype = numpy.dtype([(col, self.entry_tab.coldtypes[col])
                                      for col in ('DescriptionOffset', 'DescriptionLength')])
        # columns to be stored in entry table with buffer index data
        self.buf_index = numpy.zeros(len(self.entry_tab), dtype=bufindex_dtype)
        return self
//  [python]
def __enter__(self):
        self.entry_tab = self.db.get_node(self.entry_path)
        if not numpy.all(numpy.equal(self.entry_tab.col('EntryNr'),
                                     numpy.arange(1, len(self.entry_tab) + 1))):
            raise RuntimeError('entry table is not sorted')

        root, name = os.path.split(self.buffer_path)
        self.desc_buf = self.db.create_earray(root, name,
                                              tables.StringAtom(1), (0,), 'concatenated protein descriptions',
                                              expectedrows=len(self.entry_tab) * 100)
        self.cur_eNr = None
        self.cur_desc = []
        bufindex_dtype = numpy.dtype([(col, self.entry_tab.coldtypes[col])
                                      for col in ('DescriptionOffset', 'DescriptionLength')])
        # columns to be stored in entry table with buffer index data
        self.buf_index = numpy.zeros(len(self.entry_tab), dtype=bufindex_dtype)
        return self
//  [python]
def offset_map(output_stride):
    for x in np.log2(output_stride):
        assert np.float.is_integer(x), 'Stride must be power of 2; is: '+str(output_stride)
    if np.all(output_stride == 2):
        return  np.array([[0,0,0],[0,0,1],[0,1,0],[0,1,1],[1,0,0],[1,0,1],[1,1,0],[1,1,1]])
    else:
        prev = offset_map(output_stride/2)
        current = []
        for i in range(2):
            for j in range(2):
                for k in range(2):
                    for p in prev:
                        new = p.copy()
                        new[0] += i*2
                        new[1] += j*2
                        new[2] += k*2
                        current.append(new)
        return np.array(current)
//  [python]
def _verify_constraints(self) -> None:
        r = self._ra
        q = self._qa
        if self._num_params is not None:
            if r.shape[1] != self._num_params:
                raise ValueError(
                    "r is incompatible with the number of model " "parameters"
                )
        rq = np.c_[r, q[:, None]]
        if not np.all(np.isfinite(rq)) or matrix_rank(rq) < rq.shape[0]:
            raise ValueError("Constraints must be non-redundant")
        qr = np.linalg.qr(rq)
        if matrix_rank(qr[1][:, :-1]) != matrix_rank(qr[1]):
            raise ValueError("One or more constraints are infeasible")
//  [python]
def add_domain_covered_sites_counts(self):
        """Stores the number of AA covered by a DomainAnnotation.

        This method adds to the hdf5 file a /Protein/DomainCoverage array that
        contains the number of AA sites covered by a domain. The position
        corresponds to the protein entry numbers in /Protein/Entries.

        :Note: The method assumes that the domains are all non-overlapping.
            If they are not, the reported coverage will be too high!

        :return: covered fractions by domains for each protein
        :rtype: numpy.array"""
        self.logger.info("Counting covered sites by domains")
        cov_sites_df = self.compute_domaincovered_sites()

        prot_tab = self.h5.get_node('/Protein/Entries')
        enr_col = prot_tab.col('EntryNr')
        assert n.all(n.equal(enr_col, n.arange(1, len(prot_tab)+1)))

        cov_sites = n.zeros(len(prot_tab), dtype=n.uint32)
        for eNr, coverage in zip(cov_sites_df.index, cov_sites_df.DLen.values):
            cov_sites[eNr-1] = coverage
        create_node = False
        try:
            dom_cov_tab = self.h5.get_node('/Protein/CoveredSitesByDomains')
            if len(dom_cov_tab) != len(cov_sites):
                self.h5.remove_node('/Protein/CoveredSitesByDomains')
                create_node = True
        except tables.NoSuchNodeError:
            create_node = True
        if create_node:
            dom_cov_tab = self.h5.create_carray('/Protein', 'CoveredSitesByDomains',
                                                tables.UInt32Atom(), (len(cov_sites),))
        dom_cov_tab[0:len(cov_sites)] = cov_sites
        return cov_sites / (prot_tab.col('SeqBufferLength') - 1)
//  [python]
def add_domain_covered_sites_counts(self):
        """Stores the number of AA covered by a DomainAnnotation.

        This method adds to the hdf5 file a /Protein/DomainCoverage array that
        contains the number of AA sites covered by a domain. The position
        corresponds to the protein entry numbers in /Protein/Entries.

        :Note: The method assumes that the domains are all non-overlapping.
            If they are not, the reported coverage will be too high!

        :return: covered fractions by domains for each protein
        :rtype: numpy.array"""
        self.logger.info("Counting covered sites by domains")
        cov_sites_df = self.compute_domaincovered_sites()

        prot_tab = self.h5.get_node('/Protein/Entries')
        enr_col = prot_tab.col('EntryNr')
        assert numpy.all(numpy.equal(enr_col, numpy.arange(1, len(prot_tab)+1)))

        cov_sites = numpy.zeros(len(prot_tab), dtype=numpy.uint32)
        for eNr, coverage in zip(cov_sites_df.index, cov_sites_df.DLen.values):
            cov_sites[eNr-1] = coverage
        create_node = False
        try:
            dom_cov_tab = self.h5.get_node('/Protein/CoveredSitesByDomains')
            if len(dom_cov_tab) != len(cov_sites):
                self.h5.remove_node('/Protein/CoveredSitesByDomains')
                create_node = True
        except tables.NoSuchNodeError:
            create_node = True
        if create_node:
            dom_cov_tab = self.h5.create_carray('/Protein', 'CoveredSitesByDomains',
                                                tables.UInt32Atom(), (len(cov_sites),))
        dom_cov_tab[0:len(cov_sites)] = cov_sites
        return cov_sites / (prot_tab.col('SeqBufferLength') - 1)
//  [python]
def fast_histogram(a, b, na, nb):
    '''
    fast histogram calculation
    ---
    * a, b: non negative label ids, a.shape == b.shape, a in [0, ... na-1], b in [0, ..., nb-1]
    '''
    assert a.shape == b.shape
    assert np.all((a >= 0) & (a < na) & (b >= 0) & (b < nb))
    # k = (a >= 0) & (a < na) & (b >= 0) & (b < nb)
    hist = np.bincount(
        nb * a.reshape([-1]).astype(int) + b.reshape([-1]).astype(int),
        minlength=na * nb).reshape(na, nb)
    assert np.sum(hist) == a.size
    return hist
//  [python]
def is_monotonic(int_array: np.ndarray) -> bool:
    """is the array monotonic?"""
    return np.all(int_array[1:] >= int_array[:-1])
//  [python]
def calculate_distance(reference, moving_component, reference_is_image=False, **kwargs):
    """ For calculating error, reference should be preds (how far do we need to move the GTs)
        For post-process, reference should be GT image/pts (where should we move this pred to?)

    Args:
        reference:
        moving_component:
        reference_is_image:

    Returns:

    """
    if reference_is_image:
        reference = convert_reference(reference, threshold=0, rescale=False) # lower left origin, 0 index, X,Y

    if "kd" in kwargs and kwargs["kd"] is not None:
        kd = kwargs["kd"]
    else:
        warnings.warn("Generating KD tree")
        # Add the corners in
        # r2 = reference[:, :2]+np.array([-1,1])
        # reference = np.unique(np.r_[reference, r2], axis=0)

        # Convert reference to pixel centers
        reference = reference[:, :2] + np.array([.5, .5])
        kd = KDTree(reference[:, :2])

    moving_component = moving_component[:,:2]*61
    distances, neighbor_indices = kd.query(moving_component[:, :2])  # How far do we have to move the pred to nearest GT? Based on 0-1 height scale
    reference_nearest_points = reference[neighbor_indices]
    corner_reference = np.concatenate([np.floor(reference_nearest_points[:,:1]), np.floor(reference_nearest_points[:,1:])], axis=1)
    corner_pred =      np.concatenate([np.floor(moving_component[:,:1]), np.floor(moving_component[:,1:])], axis=1)
    same_pixel = np.all(corner_pred == corner_reference, axis=1) # if nn rounds to the same pixel, distance is 0
    distances[same_pixel] = 0

    #d = np.linalg.norm
    def d(idx=0):
        x = moving_component[i, idx]
        xr = reference_nearest_points[i, idx] # reference x
        if np.floor(xr) <= x <= np.floor(xr) + 1: # if predicted x is in the same pixel row of the reference
            x_dif = 0
        else:
            x_dif = np.min([abs(np.floor(xr) - x), abs(np.floor(xr) + 1 - x)])
        return x_dif

    for i in np.argwhere(distances>0).flatten().tolist():
        distances[i] = np.linalg.norm([d(0),d(1)])

    # Minimum distance:
    # Distance to edge of current pixel - either going to be a corner OR have the same X/Y value as the pixel in question
    # Only look at those pixels where: distance is > 0 and has matching floor(X/Y) coordinate

    return reference_nearest_points, distances/61, kd, reference
//  [python]
def _get_raw_labels(self):
        if self._raw_labels is None:
            self._raw_labels = self._load_raw_labels() if self._use_labels else None
            if self._raw_labels is None:
                self._raw_labels = np.zeros([self._raw_shape[0], 0], dtype=np.float32)
            assert isinstance(self._raw_labels, np.ndarray)
            assert self._raw_labels.shape[0] == self._raw_shape[0]
            assert self._raw_labels.dtype in [np.float32, np.int64]
            if self._raw_labels.dtype == np.int64:
                assert self._raw_labels.ndim == 1
                assert np.all(self._raw_labels >= 0)
        return self._raw_labels