jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def np_array_equal(a1, a2):

    if not (type_can_asarray(a1) and type_can_asarray(a2)):
        raise TypingError('Both arguments to "array_equals" must be array-like')

    accepted = (types.Boolean, types.Number)
    if isinstance(a1, accepted) and isinstance(a2, accepted):
        # special case
        def impl(a1, a2):
            return a1 == a2
    else:
        def impl(a1, a2):
            a = np.asarray(a1)
            b = np.asarray(a2)
            if a.shape == b.shape:
                return np.all(a == b)
            return False

    return impl
//  [python]
def __call__(self, y, proba, sample_weight=None):
        # assert proba.shape == self.probabilities_shape, 'Wrong shape of probabilities'
        assert proba.all(self.classes_ < proba.shape[1]), 'number of classes in predictions is greater than expected'
        correct_probabilities = proba[self.samples_indices, self.class_indices]
        return - (numpy.log(correct_probabilities + self.regularization) * self.sample_weight).sum()
//  [python]
def __call__(self, y, proba, sample_weight=None):
        # assert proba.shape == self.probabilities_shape, 'Wrong shape of probabilities'
        assert numpy.all(self.classes_ < proba.shape[1]), 'number of classes in predictions is greater than expected'
        correct_probabilities = proba[self.samples_indices, self.class_indices]
        return - (numpy.log(correct_probabilities + self.regularization) * self.sample_weight).sum()
//  [python]
def _collect_percentiles_inner(a, q):
    #TODO: This needs rewriting to be closer to NumPy, particularly the nan/inf
    # handling which is generally subject to algorithmic changes.
    n = len(a)

    if n == 1:
        # single element array; output same for all percentiles
        out = np.full(len(q), a[0], dtype=np.float64)
    else:
        out = np.empty(len(q), dtype=np.float64)
        for i in range(len(q)):
            percentile = q[i]

            # bypass pivoting where requested percentile is 100
            if percentile == 100:
                val = np.max(a)
                # heuristics to handle infinite values a la NumPy
                if ~np.alltrue(np.isfinite(a)):
                    if ~np.isfinite(val):
                        val = np.nan

            # bypass pivoting where requested percentile is 0
            elif percentile == 0:
                val = np.min(a)
                # convoluted heuristics to handle infinite values a la NumPy
                if ~np.alltrue(np.isfinite(a)):
                    num_pos_inf = np.sum(a == np.inf)
                    num_neg_inf = np.sum(a == -np.inf)
                    num_finite = n - (num_neg_inf + num_pos_inf)
                    if num_finite == 0:
                        val = np.nan
                    if num_pos_inf == 1 and n == 2:
                        val = np.nan
                    if num_neg_inf > 1:
                        val = np.nan
                    if num_finite == 1:
                        if num_pos_inf > 1:
                            if num_neg_inf != 1:
                                val = np.nan

            else:
                # linear interp between closest ranks
                rank = 1 + (n - 1) * np.true_divide(percentile, 100.0)
                f = math.floor(rank)
                m = rank - f
                lower, upper = _select_two(a, k=int(f - 1), low=0, high=(n - 1))
                val = lower * (1 - m) + upper * m
            out[i] = val

    return out
//  [python]
def _collect_percentiles_inner(a, q):
    #TODO: This needs rewriting to be closer to NumPy, particularly the nan/inf
    # handling which is generally subject to algorithmic changes.
    n = len(a)

    if n == 1:
        # single element array; output same for all percentiles
        out = np.full(len(q), a[0], dtype=np.float64)
    else:
        out = np.empty(len(q), dtype=np.float64)
        for i in range(len(q)):
            percentile = q[i]

            # bypass pivoting where requested percentile is 100
            if percentile == 100:
                val = np.max(a)
                # heuristics to handle infinite values a la NumPy
                if ~np.all(np.isfinite(a)):
                    if ~np.isfinite(val):
                        val = np.nan

            # bypass pivoting where requested percentile is 0
            elif percentile == 0:
                val = np.min(a)
                # convoluted heuristics to handle infinite values a la NumPy
                if ~np.all(np.isfinite(a)):
                    num_pos_inf = np.sum(a == np.inf)
                    num_neg_inf = np.sum(a == -np.inf)
                    num_finite = n - (num_neg_inf + num_pos_inf)
                    if num_finite == 0:
                        val = np.nan
                    if num_pos_inf == 1 and n == 2:
                        val = np.nan
                    if num_neg_inf > 1:
                        val = np.nan
                    if num_finite == 1:
                        if num_pos_inf > 1:
                            if num_neg_inf != 1:
                                val = np.nan

            else:
                # linear interp between closest ranks
                rank = 1 + (n - 1) * np.true_divide(percentile, 100.0)
                f = math.floor(rank)
                m = rank - f
                lower, upper = _select_two(a, k=int(f - 1), low=0, high=(n - 1))
                val = lower * (1 - m) + upper * m
            out[i] = val

    return out
//  [python]
def check_valid_sudoku_puzzle(puzzle):
  """Checks if a sudoku puzzle is valid."""
  rows = np.zeros((9, 9))
  cols = np.zeros((9, 9))
  boxes = np.zeros((9, 9))

  for i in range(len(puzzle)):
    row_num = int(i // 9)
    col_num = int(i % 9)
    rows[row_num, int(puzzle[i] - 1)] += 1
    cols[col_num, int(puzzle[i] - 1)] += 1
    boxes[int(3 * (row_num // 3) + (col_num // 3)), int(puzzle[i] - 1)] += 1

  if np.all(rows) and np.all(cols) and np.all(boxes):
    return True
  else:
    return False
//  [python]
def solve_theta(self, theta_point: numpy.ndarray) -> Optional[SolverOutput]:
        r"""
        Substitutes a particular realization of θ into the multiparametric problem and solves the resulting
        optimization problem.

        .. math::
            \begin{align}
            \tilde{b} &= b + F\theta\\
            \tilde{b}_eq &= b_{eq} + F_{eq}\theta\\
            \tilde{c}^T &= c^T + \theta^T H^T
            \end{align}

        .. math::
            \begin{align}
            \min_{x}\quad  &\frac{1}{2}x^TQx + \tilde{c}^Tx\\
            \text{s.t.} \quad Ax &\leq \tilde{b}\\
            A_{eq}x &= \tilde{b}_{eq}\\
            x &\in \mathbb{R}^n
            \end{align}

        :param theta_point: An uncertainty realization
        :return: The Solver output of the substituted problem, returns None if not solvable
        """

        if not n.all(self.A_t @ theta_point <= self.b_t):
            return None

        sol_obj = self.solver.solve_qp(Q=self.Q, c=self.H @ theta_point + self.c, A=self.A,
                                       b=self.b + (self.F @ theta_point),
                                       equality_constraints=self.equality_indices)

        if sol_obj is not None:
            sol_obj.obj += self.c_c + self.c_t.T @ theta_point + 0.5 * theta_point.T @ self.Q_t @ theta_point
            return sol_obj

        return None
//  [python]
def solve_theta(self, theta_point: numpy.ndarray) -> Optional[SolverOutput]:
        r"""
        Substitutes a particular realization of θ into the multiparametric problem and solves the resulting
        optimization problem.

        .. math::
            \begin{align}
            \tilde{b} &= b + F\theta\\
            \tilde{b}_eq &= b_{eq} + F_{eq}\theta\\
            \tilde{c}^T &= c^T + \theta^T H^T
            \end{align}

        .. math::
            \begin{align}
            \min_{x}\quad  &\frac{1}{2}x^TQx + \tilde{c}^Tx\\
            \text{s.t.} \quad Ax &\leq \tilde{b}\\
            A_{eq}x &= \tilde{b}_{eq}\\
            x &\in \mathbb{R}^n
            \end{align}

        :param theta_point: An uncertainty realization
        :return: The Solver output of the substituted problem, returns None if not solvable
        """

        if not numpy.all(self.A_t @ theta_point <= self.b_t):
            return None

        sol_obj = self.solver.solve_qp(Q=self.Q, c=self.H @ theta_point + self.c, A=self.A,
                                       b=self.b + (self.F @ theta_point),
                                       equality_constraints=self.equality_indices)

        if sol_obj is not None:
            sol_obj.obj += self.c_c + self.c_t.T @ theta_point + 0.5 * theta_point.T @ self.Q_t @ theta_point
            return sol_obj

        return None
//  [python]
def test_matrix_inverse():
    x0_np = np.random.uniform(-2, 2, (3, 4, 4)).astype(np.float32)
    x0 = Tensor(x0_np)
    expect0 = inv(x0_np)
    error0 = np.ones(shape=expect0.shape) * 1.0e-3

    context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
    matrix_inverse = NetMatrixInverse()
    output0 = matrix_inverse(x0)
    diff0 = output0.asnumpy() - expect0
    assert np.all(diff0 < error0)
    assert output0.shape == expect0.shape

    context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
    matrix_inverse = NetMatrixInverse()
    output0 = matrix_inverse(x0)
    diff0 = output0.asnumpy() - expect0
    assert np.all(diff0 < error0)
    assert output0.shape == expect0.shape
//  [python]
def test_complex_matrix_inverse():
    """
    Feature: test MatrixInverse in complex.
    Description: Test case of MatrixInverse complex on GPU.
    Expectation: success.
    """
    # PyNative mode
    context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
    shape = (32, 32)
    input_x = np.random.randn(*shape).astype(np.complex64)
    matrix_inverse = NetMatrixInverse()
    output0 = matrix_inverse(Tensor(input_x))
    error0 = np.ones(shape=shape) * 1.0e-3
    input_t = torch.from_numpy(input_x)
    output1 = torch.linalg.inv(torch.tensor(input_t))
    diff0 = output1.numpy() - output0.asnumpy()
    assert np.all(diff0 < error0)
//  [python]
def _build_transition_matrix(num_items,
                             concentration_parameter,
                             random_state,
                             atol=0.001):

    def _is_doubly_stochastic(matrix, atol):

        return (n.all(n.abs(1.0 - matrix.sum(axis=0)) < atol) and
                n.all(n.abs(1.0 - matrix.sum(axis=1)) < atol))

    transition_matrix = random_state.dirichlet(
        np.repeat(concentration_parameter, num_items),
        num_items)

    for _ in range(100):

        if _is_doubly_stochastic(transition_matrix, atol):
            break

        transition_matrix /= transition_matrix.sum(axis=0)
        transition_matrix /= transition_matrix.sum(1)[:, np.newaxis]

    return transition_matrix
//  [python]
def _build_transition_matrix(num_items,
                             concentration_parameter,
                             random_state,
                             atol=0.001):

    def _is_doubly_stochastic(matrix, atol):

        return (np.all(np.abs(1.0 - matrix.sum(axis=0)) < atol) and
                np.all(np.abs(1.0 - matrix.sum(axis=1)) < atol))

    transition_matrix = random_state.dirichlet(
        np.repeat(concentration_parameter, num_items),
        num_items)

    for _ in range(100):

        if _is_doubly_stochastic(transition_matrix, atol):
            break

        transition_matrix /= transition_matrix.sum(axis=0)
        transition_matrix /= transition_matrix.sum(1)[:, np.newaxis]

    return transition_matrix
//  [python]
def test_gammaSetGetMatch():
    """test that repeatedly getting and setting the gamma table has no
    cumulative effect."""

    startGammaTable = None

    n_repeats = 2

    for _ in range(n_repeats):

        win = visual.Window([600, 600], autoLog=False)

        for _ in range(5):
            win.flip()

        if startGammaTable is None:
            startGammaTable = win.backend.getGammaRamp()
        else:
            currGammaTable = win.backend.getGammaRamp()

            assert n.all(currGammaTable == startGammaTable)

        win.close()

    utils.skip_under_travis()
//  [python]
def test_gammaSetGetMatch():
    """test that repeatedly getting and setting the gamma table has no
    cumulative effect."""

    startGammaTable = None

    n_repeats = 2

    for _ in range(n_repeats):

        win = visual.Window([600, 600], autoLog=False)

        for _ in range(5):
            win.flip()

        if startGammaTable is None:
            startGammaTable = win.backend.getGammaRamp()
        else:
            currGammaTable = win.backend.getGammaRamp()

            assert numpy.all(currGammaTable == startGammaTable)

        win.close()

    utils.skip_under_travis()
//  [python]
def test_positive_definite(self):
        """Test the positive_definite parameter of the matrix constructor."""
        negative_index = UniformTermSimilarityIndex(self.dictionary, term_similarity=-0.5)
        matrix = SparseTermSimilarityMatrix(
            negative_index, self.dictionary, nonzero_limit=2).matrix.todense()
        expected_matrix = numpy.array([
            [1.0, -.5, -.5, 0.0, 0.0],
            [-.5, 1.0, 0.0, -.5, 0.0],
            [-.5, 0.0, 1.0, 0.0, 0.0],
            [0.0, -.5, 0.0, 1.0, 0.0],
            [0.0, 0.0, 0.0, 0.0, 1.0]])
        self.assertTrue(numpy.all(expected_matrix == matrix))

        matrix = SparseTermSimilarityMatrix(
            negative_index, self.dictionary, nonzero_limit=2, positive_definite=True).matrix.todense()
        expected_matrix = numpy.array([
            [1.0, -.5, 0.0, 0.0, 0.0],
            [-.5, 1.0, 0.0, 0.0, 0.0],
            [0.0, 0.0, 1.0, 0.0, 0.0],
            [0.0, 0.0, 0.0, 1.0, 0.0],
            [0.0, 0.0, 0.0, 0.0, 1.0]])
        self.assertTrue(numpy.all(expected_matrix == matrix))
//  [python]
def test_setGammaRamp():
    """test that the gamma ramp is set as requested"""

    testGamma = 2.2

    win = visual.Window([600,600], autoLog=False)
    desiredRamp = numpy.tile(
        visual.gamma.createLinearRamp(
            rampSize=win.backend.getGammaRampSize(),
            driver=win.backend._driver
        ),
        (3, 1)
    )

    if n.all(testGamma == 1.0) == False:
        # correctly handles 1 or 3x1 gamma vals
        desiredRamp = desiredRamp**(1.0/numpy.array(testGamma))

    win.gamma = testGamma

    for n in range(5):
        win.flip()

    setRamp = win.backend.getGammaRamp()

    win.close()

    # can't get/set LUT in travis
    utils.skip_under_travis()

    assert numpy.allclose(desiredRamp, setRamp, atol=1.0 / desiredRamp.shape[1])
//  [python]
def test_setGammaRamp():
    """test that the gamma ramp is set as requested"""

    testGamma = 2.2

    win = visual.Window([600,600], autoLog=False)
    desiredRamp = numpy.tile(
        visual.gamma.createLinearRamp(
            rampSize=win.backend.getGammaRampSize(),
            driver=win.backend._driver
        ),
        (3, 1)
    )

    if numpy.all(testGamma == 1.0) == False:
        # correctly handles 1 or 3x1 gamma vals
        desiredRamp = desiredRamp**(1.0/numpy.array(testGamma))

    win.gamma = testGamma

    for n in range(5):
        win.flip()

    setRamp = win.backend.getGammaRamp()

    win.close()

    # can't get/set LUT in travis
    utils.skip_under_travis()

    assert numpy.allclose(desiredRamp, setRamp, atol=1.0 / desiredRamp.shape[1])
//  [python]
def all(self, *args, **kwargs):
        """
        Return whether all elements are Truthy.

        Parameters
        ----------
        *args
            Required for compatibility with numpy.
        **kwargs
            Required for compatibility with numpy.

        Returns
        -------
        bool or array-like (if axis is specified)
            A single element array-like may be converted to bool.

        See Also
        --------
        Index.any : Return whether any element in an Index is True.
        Series.any : Return whether any element in a Series is True.
        Series.all : Return whether all elements in a Series are True.

        Notes
        -----
        Not a Number (NaN), positive infinity and negative infinity
        evaluate to True because these are not equal to zero.

        Examples
        --------
        True, because nonzero integers are considered True.

        >>> pd.Index([1, 2, 3]).all()
        True

        False, because ``0`` is considered False.

        >>> pd.Index([0, 1, 2]).all()
        False
        """
        nv.validate_all(args, kwargs)
        self._maybe_disable_logical_methods("all")
        vals = self._values
        if not isinstance(vals, np.ndarray):
            # i.e. EA, call _reduce instead of "all" to get TypeError instead
            #  of AttributeError
            return vals._reduce("all")
        return n.all(vals)
//  [python]
def all(self, *args, **kwargs):
        """
        Return whether all elements are Truthy.

        Parameters
        ----------
        *args
            Required for compatibility with numpy.
        **kwargs
            Required for compatibility with numpy.

        Returns
        -------
        bool or array-like (if axis is specified)
            A single element array-like may be converted to bool.

        See Also
        --------
        Index.any : Return whether any element in an Index is True.
        Series.any : Return whether any element in a Series is True.
        Series.all : Return whether all elements in a Series are True.

        Notes
        -----
        Not a Number (NaN), positive infinity and negative infinity
        evaluate to True because these are not equal to zero.

        Examples
        --------
        True, because nonzero integers are considered True.

        >>> pd.Index([1, 2, 3]).all()
        True

        False, because ``0`` is considered False.

        >>> pd.Index([0, 1, 2]).all()
        False
        """
        nv.validate_all(args, kwargs)
        self._maybe_disable_logical_methods("all")
        vals = self._values
        if not isinstance(vals, np.ndarray):
            # i.e. EA, call _reduce instead of "all" to get TypeError instead
            #  of AttributeError
            return vals._reduce("all")
        return np.all(vals)
//  [python]
def _lazyselect(condlist, choicelist, arrays, default=0):
    """
    Mimic `np.select(condlist, choicelist)`.

    Notice, it assumes that all `arrays` are of the same shape or can be
    broadcasted together.

    All functions in `choicelist` must accept array arguments in the order
    given in `arrays` and must return an array of the same shape as broadcasted
    `arrays`.

    Examples
    --------
    >>> x = np.arange(6)
    >>> np.select([x <3, x > 3], [x**2, x**3], default=0)
    array([  0,   1,   4,   0,  64, 125])

    >>> _lazyselect([x < 3, x > 3], [lambda x: x**2, lambda x: x**3], (x,))
    array([   0.,    1.,    4.,   0.,   64.,  125.])

    >>> a = -np.ones_like(x)
    >>> _lazyselect([x < 3, x > 3],
    ...             [lambda x, a: x**2, lambda x, a: a * x**3],
    ...             (x, a), default=np.nan)
    array([   0.,    1.,    4.,   nan,  -64., -125.])

    """
    arrays = np.broadcast_arrays(*arrays)
    tcode = np.mintypecode([a.dtype.char for a in arrays])
    out = np.full(np.shape(arrays[0]), fill_value=default, dtype=tcode)
    for index in range(len(condlist)):
        func, cond = choicelist[index], condlist[index]
        if np.all(cond is False):
            continue
        cond, _ = np.broadcast_arrays(cond, arrays[0])
        temp = tuple(np.extract(cond, arr) for arr in arrays)
        np.place(out, cond, func(*temp))
    return out
//  [python]
def all(self):
        """
        Return whether all elements are Truthy.

        Parameters
        ----------
        *args
            These parameters will be passed to numpy.all.
        **kwargs
            These parameters will be passed to numpy.all.

        Returns
        -------
        all : bool or array_like (if axis is specified)
            A single element array_like may be converted to bool.

        See Also
        --------
        Index.any : Return whether any element in an Index is True.
        Series.any : Return whether any element in a Series is True.
        Series.all : Return whether all elements in a Series are True.

        Notes
        -----
        Not a Number (NaN), positive infinity and negative infinity
        evaluate to True because these are not equal to zero.

        Examples
        --------
        **all**

        True, because nonzero integers are considered True.

        >>> pd.Index([1, 2, 3]).all()
        True

        False, because ``0`` is considered False.

        >>> pd.Index([0, 1, 2]).all()
        False

        **any**

        True, because ``1`` is considered True.

        >>> pd.Index([0, 0, 1]).any()
        True

        False, because ``0`` is considered False.

        >>> pd.Index([0, 0, 0]).any()
        False
        """
        # FIXME: docstr inaccurate, args/kwargs not passed

        self._maybe_disable_logical_methods("all")
        return n.all(self.values)
//  [python]
def all(self):
        """
        Return whether all elements are Truthy.

        Parameters
        ----------
        *args
            These parameters will be passed to numpy.all.
        **kwargs
            These parameters will be passed to numpy.all.

        Returns
        -------
        all : bool or array_like (if axis is specified)
            A single element array_like may be converted to bool.

        See Also
        --------
        Index.any : Return whether any element in an Index is True.
        Series.any : Return whether any element in a Series is True.
        Series.all : Return whether all elements in a Series are True.

        Notes
        -----
        Not a Number (NaN), positive infinity and negative infinity
        evaluate to True because these are not equal to zero.

        Examples
        --------
        **all**

        True, because nonzero integers are considered True.

        >>> pd.Index([1, 2, 3]).all()
        True

        False, because ``0`` is considered False.

        >>> pd.Index([0, 1, 2]).all()
        False

        **any**

        True, because ``1`` is considered True.

        >>> pd.Index([0, 0, 1]).any()
        True

        False, because ``0`` is considered False.

        >>> pd.Index([0, 0, 0]).any()
        False
        """
        # FIXME: docstr inaccurate, args/kwargs not passed

        self._maybe_disable_logical_methods("all")
        return np.all(self.values)
//  [python]
def minmax(m, num=10, metric='tanimoto', seed=42):
    """ MinMax selection algorithm

    :param m: {array} Input matrix with vectors to make MinMax selection from
    :param num: {int} Number of selections to do
    :param metric: {str} Metric to use for distance / similarity calculation (tanimoto, cosine, euclidean)
    :param seed: {int} random seed to use for initialization
    :returns: indices of the vectors that were selected
    """
    np.random.seed(seed)
    if num > m.shape[0]:
        raise ValueError("Number of selections can't be larger than number of instances in M.")

    start = time()  # tic
    pool = m  # Store pool from which the selections get removed

    # Randomly selecting first molecule into the sele
    idx = int(np.random.randint(0, m.shape[0], 1))
    sele = pool[idx:idx + 1, :]
    minmaxidx = np.where(np.all(m == pool[idx:idx + 1, :], axis=1))[0].tolist()  # store original indices to return

    # Deleting selected molecule in selection from pool
    pool = np.delete(pool, idx, axis=0)

    pbar = ProgressBar()
    for _ in pbar(range(num - 1)):
        # Calculating distance from selected instances to the rest of the pool
        dist = parallel_pairwise_similarities(pool, sele, metric)
        if metric.lower() == 'tanimoto':
            dist = 1 - dist

        # Choosing maximal distances for every selected instance
        maxidx = np.argmax(dist, axis=0)  # index of most distant instance to each of compounds that were selected
        maxcols = np.max(dist, axis=0)  # value of most distant instance to each of compounds that were selected

        # Choosing minimal distance among the maximal distances
        minmax = np.argmin(maxcols)  # index of lowest distance value within maxcols
        idx = int(maxidx[minmax])  # index of the least most distant instance to each of the selected ones

        # Adding it to selection minmax indices and removing from pool
        sele = np.vstack((sele, pool[idx:idx + 1, :]))
        minmaxidx.extend(np.where(np.all(m == pool[idx:idx + 1, :], axis=1))[0])
        pool = np.delete(pool, idx, axis=0)

    print("MinMax selection took %.1f" % (time() - start))  # toc
    return minmaxidx
//  [python]
def convert(entry, slice_ok=True):
        """
        Change references to Variables into references to Types.

        The "idx_list" field is unique to each Subtensor instance.
        It is not unique to each Apply node, so it should not refer to
        specific Variables.
        TODO: WRITEME: This method also accepts "entry" already being a Type;
            when would that happen?

        """
        invalid_scal_types = [scal.float64, scal.float32, scal.float16]
        scal_types = [scal.int64, scal.int32, scal.int16, scal.int8]
        tensor_types = [theano.tensor.lscalar, theano.tensor.iscalar,
                        theano.tensor.wscalar, theano.tensor.bscalar]
        invalid_tensor_types = [theano.tensor.fscalar, theano.tensor.dscalar,
                                theano.tensor.cscalar, theano.tensor.zscalar]
        if (isinstance(entry, gof.Variable) and
            (entry.type in invalid_scal_types or
             entry.type in invalid_tensor_types)):
            raise TypeError("Expected an integer")

        if isinstance(entry, gof.Variable) and entry.type in scal_types:
            return entry.type
        elif isinstance(entry, gof.Type) and entry in scal_types:
            return entry

        if (isinstance(entry, gof.Variable) and
                entry.type in tensor_types and
                n.all(entry.type.broadcastable)):
            return scal.get_scalar_type(entry.type.dtype)
        elif (isinstance(entry, gof.Type) and
              entry in tensor_types and
              n.all(entry.broadcastable)):
            return scal.get_scalar_type(entry.dtype)
        elif slice_ok and isinstance(entry, slice):
            a = entry.start
            b = entry.stop
            c = entry.step

            if a is not None:
                slice_a = Subtensor.convert(a, False)
            else:
                slice_a = None

            if b is not None and b != sys.maxsize:
                # The special "maxsize" case is probably not needed here,
                # as slices containing maxsize are not generated by
                # __getslice__ anymore.
                slice_b = Subtensor.convert(b, False)
            else:
                slice_b = None

            if c is not None:
                slice_c = Subtensor.convert(c, False)
            else:
                slice_c = None

            return slice(slice_a, slice_b, slice_c)
        elif isinstance(entry, (integer_types, numpy.integer)):
            # Disallow the use of python scalars in idx_list
            raise TypeError("Python scalar in idx_list."
                            "Please report this error to theano-dev.")
        else:
            raise AdvancedIndexingError(Subtensor.e_indextype, entry)
//  [python]
def convert(entry, slice_ok=True):
        """
        Change references to Variables into references to Types.

        The "idx_list" field is unique to each Subtensor instance.
        It is not unique to each Apply node, so it should not refer to
        specific Variables.
        TODO: WRITEME: This method also accepts "entry" already being a Type;
            when would that happen?

        """
        invalid_scal_types = [scal.float64, scal.float32, scal.float16]
        scal_types = [scal.int64, scal.int32, scal.int16, scal.int8]
        tensor_types = [theano.tensor.lscalar, theano.tensor.iscalar,
                        theano.tensor.wscalar, theano.tensor.bscalar]
        invalid_tensor_types = [theano.tensor.fscalar, theano.tensor.dscalar,
                                theano.tensor.cscalar, theano.tensor.zscalar]
        if (isinstance(entry, gof.Variable) and
            (entry.type in invalid_scal_types or
             entry.type in invalid_tensor_types)):
            raise TypeError("Expected an integer")

        if isinstance(entry, gof.Variable) and entry.type in scal_types:
            return entry.type
        elif isinstance(entry, gof.Type) and entry in scal_types:
            return entry

        if (isinstance(entry, gof.Variable) and
                entry.type in tensor_types and
                numpy.all(entry.type.broadcastable)):
            return scal.get_scalar_type(entry.type.dtype)
        elif (isinstance(entry, gof.Type) and
              entry in tensor_types and
              numpy.all(entry.broadcastable)):
            return scal.get_scalar_type(entry.dtype)
        elif slice_ok and isinstance(entry, slice):
            a = entry.start
            b = entry.stop
            c = entry.step

            if a is not None:
                slice_a = Subtensor.convert(a, False)
            else:
                slice_a = None

            if b is not None and b != sys.maxsize:
                # The special "maxsize" case is probably not needed here,
                # as slices containing maxsize are not generated by
                # __getslice__ anymore.
                slice_b = Subtensor.convert(b, False)
            else:
                slice_b = None

            if c is not None:
                slice_c = Subtensor.convert(c, False)
            else:
                slice_c = None

            return slice(slice_a, slice_b, slice_c)
        elif isinstance(entry, (integer_types, numpy.integer)):
            # Disallow the use of python scalars in idx_list
            raise TypeError("Python scalar in idx_list."
                            "Please report this error to theano-dev.")
        else:
            raise AdvancedIndexingError(Subtensor.e_indextype, entry)
//  [python]
def test_basic_property(self, shape, dtype, upper):
        np.random.seed(1)
        a = np.random.randn(*shape)
        if np.issubdtype(dtype, np.complexfloating):
            a = a + 1j*np.random.randn(*shape)

        t = list(range(len(shape)))
        t[-2:] = -1, -2

        a = np.matmul(a.transpose(t).conj(), a)
        a = np.asarray(a, dtype=dtype)

        c = np.linalg.cholesky(a, upper=upper)

        # Check A = L L^H or A = U^H U
        if upper:
            b = np.matmul(c.transpose(t).conj(), c)
        else:
            b = np.matmul(c, c.transpose(t).conj())
        with np._no_nep50_warning():
            atol = 500 * a.shape[0] * np.finfo(dtype).eps
        assert_allclose(b, a, atol=atol, err_msg=f'{shape} {dtype}\n{a}\n{c}')

        # Check diag(L or U) is real and positive
        d = np.diagonal(c, axis1=-2, axis2=-1)
        assert_(np.all(np.isreal(d)))
        assert_(np.all(d >= 0))
//  [python]
def _make_eigvals(alpha, beta, homogeneous_eigvals):
    if homogeneous_eigvals:
        if beta is None:
            return np.vstack((alpha, np.ones_like(alpha)))
        else:
            return np.vstack((alpha, beta))
    else:
        if beta is None:
            return alpha
        else:
            w = np.empty_like(alpha)
            alpha_zero = (alpha == 0)
            beta_zero = (beta == 0)
            beta_nonzero = ~beta_zero
            w[beta_nonzero] = alpha[beta_nonzero]/beta[beta_nonzero]
            # Use np.inf for complex values too since
            # 1/np.inf = 0, i.e., it correctly behaves as projective
            # infinity.
            w[~alpha_zero & beta_zero] = np.inf
            if n.all(alpha.imag == 0):
                w[alpha_zero & beta_zero] = np.nan
            else:
                w[alpha_zero & beta_zero] = complex(np.nan, np.nan)
            return w
//  [python]
def _make_eigvals(alpha, beta, homogeneous_eigvals):
    if homogeneous_eigvals:
        if beta is None:
            return np.vstack((alpha, np.ones_like(alpha)))
        else:
            return np.vstack((alpha, beta))
    else:
        if beta is None:
            return alpha
        else:
            w = np.empty_like(alpha)
            alpha_zero = (alpha == 0)
            beta_zero = (beta == 0)
            beta_nonzero = ~beta_zero
            w[beta_nonzero] = alpha[beta_nonzero]/beta[beta_nonzero]
            # Use np.inf for complex values too since
            # 1/np.inf = 0, i.e., it correctly behaves as projective
            # infinity.
            w[~alpha_zero & beta_zero] = np.inf
            if np.all(alpha.imag == 0):
                w[alpha_zero & beta_zero] = np.nan
            else:
                w[alpha_zero & beta_zero] = complex(np.nan, np.nan)
            return w
//  [python]
def _geneig(a1, b1, left, right, overwrite_a, overwrite_b,
            homogeneous_eigvals):
    ggev, = get_lapack_funcs(('ggev',), (a1, b1))
    cvl, cvr = left, right
    res = ggev(a1, b1, lwork=-1)
    lwork = res[-2][0].real.astype(np.int_)
    if ggev.typecode in 'cz':
        alpha, beta, vl, vr, work, info = ggev(a1, b1, cvl, cvr, lwork,
                                               overwrite_a, overwrite_b)
        w = _make_eigvals(alpha, beta, homogeneous_eigvals)
    else:
        alphar, alphai, beta, vl, vr, work, info = ggev(a1, b1, cvl, cvr,
                                                        lwork, overwrite_a,
                                                        overwrite_b)
        alpha = alphar + _I * alphai
        w = _make_eigvals(alpha, beta, homogeneous_eigvals)
    _check_info(info, 'generalized eig algorithm (ggev)')

    only_real = n.all(w.imag == 0.0)
    if not (ggev.typecode in 'cz' or only_real):
        t = w.dtype.char
        if left:
            vl = _make_complex_eigvecs(w, vl, t)
        if right:
            vr = _make_complex_eigvecs(w, vr, t)

    # the eigenvectors returned by the lapack function are NOT normalized
    for i in range(vr.shape[0]):
        if right:
            vr[:, i] /= norm(vr[:, i])
        if left:
            vl[:, i] /= norm(vl[:, i])

    if not (left or right):
        return w
    if left:
        if right:
            return w, vl, vr
        return w, vl
    return w, vr
//  [python]
def _geneig(a1, b1, left, right, overwrite_a, overwrite_b,
            homogeneous_eigvals):
    ggev, = get_lapack_funcs(('ggev',), (a1, b1))
    cvl, cvr = left, right
    res = ggev(a1, b1, lwork=-1)
    lwork = res[-2][0].real.astype(np.int_)
    if ggev.typecode in 'cz':
        alpha, beta, vl, vr, work, info = ggev(a1, b1, cvl, cvr, lwork,
                                               overwrite_a, overwrite_b)
        w = _make_eigvals(alpha, beta, homogeneous_eigvals)
    else:
        alphar, alphai, beta, vl, vr, work, info = ggev(a1, b1, cvl, cvr,
                                                        lwork, overwrite_a,
                                                        overwrite_b)
        alpha = alphar + _I * alphai
        w = _make_eigvals(alpha, beta, homogeneous_eigvals)
    _check_info(info, 'generalized eig algorithm (ggev)')

    only_real = np.all(w.imag == 0.0)
    if not (ggev.typecode in 'cz' or only_real):
        t = w.dtype.char
        if left:
            vl = _make_complex_eigvecs(w, vl, t)
        if right:
            vr = _make_complex_eigvecs(w, vr, t)

    # the eigenvectors returned by the lapack function are NOT normalized
    for i in range(vr.shape[0]):
        if right:
            vr[:, i] /= norm(vr[:, i])
        if left:
            vl[:, i] /= norm(vl[:, i])

    if not (left or right):
        return w
    if left:
        if right:
            return w, vl, vr
        return w, vl
    return w, vr
//  [python]
def test_trace_one_segment():
    """Trace a single segment"""
    module = cellprofiler.modules.untangleworms.UntangleWorms()
    image = numpy.zeros((10, 20), bool)
    image[5, 1:18] = True
    expected_order = numpy.zeros(image.shape, int)
    expected_order[image] = numpy.arange(numpy.sum(image))
    i, j, label, order, distance, count = module.trace_segments(image)
    assert count == 1
    assert n.all(label == 1)
    for x in (i, j, order, distance):
        assert len(x) == numpy.sum(image)

    result_order = numpy.zeros(image.shape, int)
    result_order[i, j] = order
    assert n.all(image[i, j])
    assert n.all(expected_order == result_order)
//  [python]
def test_trace_one_segment():
    """Trace a single segment"""
    module = cellprofiler.modules.untangleworms.UntangleWorms()
    image = numpy.zeros((10, 20), bool)
    image[5, 1:18] = True
    expected_order = numpy.zeros(image.shape, int)
    expected_order[image] = numpy.arange(numpy.sum(image))
    i, j, label, order, distance, count = module.trace_segments(image)
    assert count == 1
    assert numpy.all(label == 1)
    for x in (i, j, order, distance):
        assert len(x) == numpy.sum(image)

    result_order = numpy.zeros(image.shape, int)
    result_order[i, j] = order
    assert numpy.all(image[i, j])
    assert numpy.all(expected_order == result_order)
//  [python]
def test_trace_short_segment():
    """Trace a segment of a single point"""
    module = cellprofiler.modules.untangleworms.UntangleWorms()
    image = numpy.zeros((10, 20), bool)
    for i in range(1, 3):
        image[5, 10 : (10 + i)] = True
        expected_order = numpy.zeros(image.shape, int)
        expected_order[image] = numpy.arange(numpy.sum(image))
        i, j, label, order, distance, count = module.trace_segments(image)
        assert count == 1
        assert n.all(label == 1)
        for x in (i, j, order, distance):
            assert len(x) == numpy.sum(image)

        result_order = numpy.zeros(image.shape, int)
        result_order[i, j] = order
        assert n.all(image[i, j])
        assert n.all(expected_order == result_order)
//  [python]
def test_trace_short_segment():
    """Trace a segment of a single point"""
    module = cellprofiler.modules.untangleworms.UntangleWorms()
    image = numpy.zeros((10, 20), bool)
    for i in range(1, 3):
        image[5, 10 : (10 + i)] = True
        expected_order = numpy.zeros(image.shape, int)
        expected_order[image] = numpy.arange(numpy.sum(image))
        i, j, label, order, distance, count = module.trace_segments(image)
        assert count == 1
        assert numpy.all(label == 1)
        for x in (i, j, order, distance):
            assert len(x) == numpy.sum(image)

        result_order = numpy.zeros(image.shape, int)
        result_order[i, j] = order
        assert numpy.all(image[i, j])
        assert numpy.all(expected_order == result_order)
//  [python]
def amplitude_to_density(
        data: numpy.ndarray,
        dmin: Union[int, float] = 30,
        mmult: Union[int, float] = 40,
        data_mean: Union[None, int, float] = None) -> numpy.ndarray:
    """
    Convert to density data for remap.

    This is a digested version of contents presented in a 1994 pulication
    entitled "Softcopy Display of SAR Data" by Kevin Mangis. It is unclear where
    this was first published or where it may be publicly available.

    Parameters
    ----------
    data : numpy.ndarray
        The (presumably complex) data to remap
    dmin : float|int
        A dynamic range parameter. Lower this widens the range, will raising it
        narrows the range. This was historically fixed at 30.
    mmult : float|int
        A contrast parameter. Low values will result is higher contrast and quicker
        saturation, while high values will decrease contrast and slower saturation.
        There is some balance between the competing effects in the `dmin` and `mmult`
        parameters.
    data_mean : None|float|int
        The data mean (for this or the parent array for continuity), which will
        be calculated if not provided.

    Returns
    -------
    numpy.ndarray
    """

    dmin = float(dmin)
    if not (0 <= dmin < 255):
        raise ValueError('Invalid dmin value {}'.format(dmin))

    mmult = float(mmult)
    if mmult < 1:
        raise ValueError('Invalid mmult value {}'.format(mmult))

    EPS = 1e-5
    amplitude = numpy.abs(data)
    if numpy.all(amplitude == 0):
        return amplitude
    else:
        if not data_mean:
            data_mean = numpy.mean(amplitude[numpy.isfinite(amplitude)])
        # remap parameters
        C_L = 0.8*data_mean
        C_H = mmult*C_L  # decreasing mmult will result in higher contrast (and quicker saturation)
        slope = (255 - dmin)/numpy.log10(C_H/C_L)
        constant = dmin - (slope*numpy.log10(C_L))
        # NB: C_H/C_L trivially collapses to mmult, but this is maintained for
        # clarity in historical reference
        # Originally, C_L and C_H were static values drawn from a determined set
        # of remap look-up tables. The C_L/C_H values were presumably based roughly
        # on mean amplitude and desired remap brightness/contrast. The dmin value
        # was fixed as 30.
        return slope*numpy.log10(numpy.maximum(amplitude, EPS)) + constant
//  [python]
def test_trace_two():
    """Trace two objects"""
    module = cellprofiler.modules.untangleworms.UntangleWorms()
    image = numpy.zeros((10, 20), bool)
    image[1:-1, 5] = True
    image[1:-1, 15] = True
    i, j, label, order, distance, count = module.trace_segments(image)
    assert count == 2
    result_order = numpy.zeros(image.shape, int)
    result_order[i, j] = order
    for j in (5, 15):
        assert n.all(result_order[1:-1, j] == numpy.arange(image.shape[0] - 2))
//  [python]
def test_trace_two():
    """Trace two objects"""
    module = cellprofiler.modules.untangleworms.UntangleWorms()
    image = numpy.zeros((10, 20), bool)
    image[1:-1, 5] = True
    image[1:-1, 15] = True
    i, j, label, order, distance, count = module.trace_segments(image)
    assert count == 2
    result_order = numpy.zeros(image.shape, int)
    result_order[i, j] = order
    for j in (5, 15):
        assert numpy.all(result_order[1:-1, j] == numpy.arange(image.shape[0] - 2))
//  [python]
def test_make_incidence_matrix_of_things_that_do_not_touch():
    module = cellprofiler.modules.untangleworms.UntangleWorms()
    L1 = numpy.zeros((10, 20), int)
    L2 = numpy.zeros((10, 20), int)
    L1[5, 5] = 1
    L2[5, 15] = 1
    result = module.make_incidence_matrix(L1, 1, L2, 1)
    assert tuple(result.shape) == (1, 1)
    assert not result.any()
//  [python]
def test_make_incidence_matrix_of_things_that_do_not_touch():
    module = cellprofiler.modules.untangleworms.UntangleWorms()
    L1 = numpy.zeros((10, 20), int)
    L2 = numpy.zeros((10, 20), int)
    L1[5, 5] = 1
    L2[5, 15] = 1
    result = module.make_incidence_matrix(L1, 1, L2, 1)
    assert tuple(result.shape) == (1, 1)
    assert numpy.all(~result)
//  [python]
def test_trace_loop():
    """Trace an object that loops on itself"""
    module = cellprofiler.modules.untangleworms.UntangleWorms()
    image = numpy.zeros((10, 20), bool)
    image[1:-1, 1:-1] = True
    image[2:-2, 2:-2] = False
    # Lop off the corners as would happen if skeletonized
    image[1, 1] = image[1, -2] = image[-2, 1] = image[-2, -2] = False
    #
    # It should go clockwise, starting from 1,2
    #
    expected_order = numpy.zeros(image.shape, int)
    i, j = numpy.mgrid[0 : image.shape[0], 0 : image.shape[1]]
    slices = (
        (1, slice(2, -2)),
        (slice(2, -2), -2),
        (-2, slice(-3, 1, -1)),
        (slice(-3, 1, -1), 1),
    )
    ii, jj = numpy.array((2, 0), int)
    ii = numpy.hstack([i[islice, jslice].flatten() for islice, jslice in slices])
    jj = numpy.hstack([j[islice, jslice].flatten() for islice, jslice in slices])
    expected_order[ii, jj] = numpy.arange(len(ii))
    i, j, label, order, distance, count = module.trace_segments(image)
    result_order = numpy.zeros(image.shape, int)
    result_order[i, j] = order
    assert n.all(expected_order == result_order)
//  [python]
def test_trace_loop():
    """Trace an object that loops on itself"""
    module = cellprofiler.modules.untangleworms.UntangleWorms()
    image = numpy.zeros((10, 20), bool)
    image[1:-1, 1:-1] = True
    image[2:-2, 2:-2] = False
    # Lop off the corners as would happen if skeletonized
    image[1, 1] = image[1, -2] = image[-2, 1] = image[-2, -2] = False
    #
    # It should go clockwise, starting from 1,2
    #
    expected_order = numpy.zeros(image.shape, int)
    i, j = numpy.mgrid[0 : image.shape[0], 0 : image.shape[1]]
    slices = (
        (1, slice(2, -2)),
        (slice(2, -2), -2),
        (-2, slice(-3, 1, -1)),
        (slice(-3, 1, -1), 1),
    )
    ii, jj = numpy.array((2, 0), int)
    ii = numpy.hstack([i[islice, jslice].flatten() for islice, jslice in slices])
    jj = numpy.hstack([j[islice, jslice].flatten() for islice, jslice in slices])
    expected_order[ii, jj] = numpy.arange(len(ii))
    i, j, label, order, distance, count = module.trace_segments(image)
    result_order = numpy.zeros(image.shape, int)
    result_order[i, j] = order
    assert numpy.all(expected_order == result_order)
//  [python]
def test_make_incidence_matrix_of_things_that_touch():
    module = cellprofiler.modules.untangleworms.UntangleWorms()
    L1 = numpy.zeros((10, 20), int)
    L2 = numpy.zeros((10, 20), int)
    L1[5, 5] = 1
    for i2, j2 in ((4, 4), (4, 5), (4, 6), (5, 4), (5, 6), (6, 4), (6, 5), (6, 6)):
        L2[i2, j2] = 1
        result = module.make_incidence_matrix(L1, 1, L2, 1)
        assert tuple(result.shape) == (1, 1)
        assert numpy.alltrue(result)
//  [python]
def test_make_incidence_matrix_of_things_that_touch():
    module = cellprofiler.modules.untangleworms.UntangleWorms()
    L1 = numpy.zeros((10, 20), int)
    L2 = numpy.zeros((10, 20), int)
    L1[5, 5] = 1
    for i2, j2 in ((4, 4), (4, 5), (4, 6), (5, 4), (5, 6), (6, 4), (6, 5), (6, 6)):
        L2[i2, j2] = 1
        result = module.make_incidence_matrix(L1, 1, L2, 1)
        assert tuple(result.shape) == (1, 1)
        assert numpy.all(result)
//  [python]
def test_sample_non_linear_control_points():
    module = cellprofiler.modules.untangleworms.UntangleWorms()
    path_coords = numpy.array([numpy.arange(11)] * 2).transpose()
    distances = numpy.sqrt(numpy.arange(11))
    result = module.sample_control_points(path_coords, distances, 6)
    assert n.all(result[:, 0] >= numpy.linspace(0.0, 1.0, 6) ** 2 * 10)
    assert n.all(result[:, 0] < numpy.linspace(0.0, 1.0, 6) ** 2 * 10 + 0.5)
//  [python]
def test_sample_non_linear_control_points():
    module = cellprofiler.modules.untangleworms.UntangleWorms()
    path_coords = numpy.array([numpy.arange(11)] * 2).transpose()
    distances = numpy.sqrt(numpy.arange(11))
    result = module.sample_control_points(path_coords, distances, 6)
    assert numpy.all(result[:, 0] >= numpy.linspace(0.0, 1.0, 6) ** 2 * 10)
    assert numpy.all(result[:, 0] < numpy.linspace(0.0, 1.0, 6) ** 2 * 10 + 0.5)
//  [python]
def test_make_incidence_matrix_of_many_things():
    module = cellprofiler.modules.untangleworms.UntangleWorms()
    L1 = numpy.zeros((10, 20), int)
    L2 = numpy.zeros((10, 20), int)
    L1[2, 1:5] = 1
    L1[4, 6:10] = 2
    L1[6, 11:15] = 3
    L1[1:6, 16] = 4
    L1[0, 2:15] = 5
    L2[1, 1] = 1
    L2[3, 5] = 2
    L2[5, 10] = 3
    L2[6, 15] = 4
    L2[1, 15] = 5
    expected = numpy.zeros((5, 5), bool)
    expected[0, 0] = True
    expected[0, 1] = True
    expected[1, 1] = True
    expected[1, 2] = True
    expected[2, 2] = True
    expected[2, 3] = True
    expected[3, 3] = True
    expected[3, 4] = True
    expected[4, 4] = True
    expected[4, 0] = True
    result = module.make_incidence_matrix(L1, 5, L2, 5)
    assert n.all(result == expected)