jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def infer_shape(self, fgraph, node, i_shapes):
        from aesara.tensor.math import ceil, maximum

        # Note start, stop and step can be float numbers.
        start, stop, step = node.inputs

        def is_constant_value(var, value):
            try:
                v = get_scalar_constant_value(var)
                return np.all(v == value)
            except NotScalarConstantError:
                pass
            return False

        def upcast(var):
            if (
                var.dtype in integer_dtypes
                and
                # We do not want to cast uint64 to int64 as this can
                # loose information. If we upcast uint64 with int64,
                # this give float64. This is safer then checking for
                # uint64 in case we support [u]int128 or other in the
                # future.
                aes.upcast(var.dtype, "int64") == "int64"
            ):
                return cast(var, "int64")
            return var

        if is_constant_value(step, 1):
            if is_constant_value(start, 0):
                return [(cast(stop, "int64"),)]
            else:
                stop = upcast(stop)
                start = upcast(start)
                return [(maximum(cast(stop - start, "int64"), 0),)]
        else:
            stop = upcast(stop)
            start = upcast(start)
            return [
                (
                    maximum(
                        cast(ceil(cast((stop - start), "float64") / step), "int64"), 0
                    ),
                )
            ]
//  [python]
def test_string_numpy():
    strs = numpy.array([item[0] for item in testcases], dtype='S')
    lons, lats = geohash.decode(strs, round=True)
    assert n.all(
        n.abs(  # type: ignore
            lons - numpy.array([item[2] for item in testcases])) < 1e-6)
    assert n.all(
        n.abs(  # type: ignore
            lats - numpy.array([item[1] for item in testcases])) < 1e-6)

    strs = numpy.array([item[0] for item in testcases], dtype='U')
    with pytest.raises(ValueError):
        geohash.decode(strs, round=True)
    strs = numpy.array([item[0] for item in testcases],
                       dtype='S').reshape(5, 2)
    with pytest.raises(ValueError):
        geohash.decode(strs, round=True)
    strs = numpy.array([b'0' * 24], dtype='S')
    with pytest.raises(ValueError):
        geohash.decode(strs, round=True)
    strs = numpy.array([item[0] for item in testcases], dtype='S')
    strs = numpy.vstack((strs[:5], strs[5:]))
    indexes = geohash.where(strs)
    assert isinstance(indexes, dict)

    with pytest.raises(ValueError):
        indexes = geohash.where(strs.astype('U'))

    strs = numpy.array([item[0] for item in testcases], dtype='S')
    strs.reshape(1, 2, 5)
    with pytest.raises(ValueError):
        indexes = geohash.where(strs)
//  [python]
def test_string_numpy():
    strs = numpy.array([item[0] for item in testcases], dtype='S')
    lons, lats = geohash.decode(strs, round=True)
    assert numpy.all(
        numpy.abs(  # type: ignore
            lons - numpy.array([item[2] for item in testcases])) < 1e-6)
    assert numpy.all(
        numpy.abs(  # type: ignore
            lats - numpy.array([item[1] for item in testcases])) < 1e-6)

    strs = numpy.array([item[0] for item in testcases], dtype='U')
    with pytest.raises(ValueError):
        geohash.decode(strs, round=True)
    strs = numpy.array([item[0] for item in testcases],
                       dtype='S').reshape(5, 2)
    with pytest.raises(ValueError):
        geohash.decode(strs, round=True)
    strs = numpy.array([b'0' * 24], dtype='S')
    with pytest.raises(ValueError):
        geohash.decode(strs, round=True)
    strs = numpy.array([item[0] for item in testcases], dtype='S')
    strs = numpy.vstack((strs[:5], strs[5:]))
    indexes = geohash.where(strs)
    assert isinstance(indexes, dict)

    with pytest.raises(ValueError):
        indexes = geohash.where(strs.astype('U'))

    strs = numpy.array([item[0] for item in testcases], dtype='S')
    strs.reshape(1, 2, 5)
    with pytest.raises(ValueError):
        indexes = geohash.where(strs)
//  [python]
def testPlotComplexImage(self):
        """Test that a complex image is displayed as its absolute value."""
        data = numpy.linspace(1, 1j, 100).reshape(10, 10)
        self.plot.addImage(data, legend="complex")

        image = self.plot.getActiveImage()
        retrievedData = image.getData(copy=False)
        self.assertTrue(n.all(n.equal(retrievedData, n.absolute(data))))
//  [python]
def testPlotComplexImage(self):
        """Test that a complex image is displayed as its absolute value."""
        data = numpy.linspace(1, 1j, 100).reshape(10, 10)
        self.plot.addImage(data, legend="complex")

        image = self.plot.getActiveImage()
        retrievedData = image.getData(copy=False)
        self.assertTrue(numpy.all(numpy.equal(retrievedData, numpy.absolute(data))))
//  [python]
def testPlotBooleanImage(self):
        """Test that a boolean image is displayed and converted to int8."""
        data = numpy.zeros((10, 10), dtype=bool)
        data[::2, ::2] = True
        self.plot.addImage(data, legend="boolean")

        image = self.plot.getActiveImage()
        retrievedData = image.getData(copy=False)
        self.assertTrue(n.all(n.equal(retrievedData, data)))
        self.assertIs(retrievedData.dtype.type, numpy.int8)
//  [python]
def testPlotBooleanImage(self):
        """Test that a boolean image is displayed and converted to int8."""
        data = numpy.zeros((10, 10), dtype=bool)
        data[::2, ::2] = True
        self.plot.addImage(data, legend="boolean")

        image = self.plot.getActiveImage()
        retrievedData = image.getData(copy=False)
        self.assertTrue(numpy.all(numpy.equal(retrievedData, data)))
        self.assertIs(retrievedData.dtype.type, numpy.int8)
//  [python]
def testGetItems(self):
        """Test getItems method"""
        curve_x = 1, 2
        self.plot.addCurve(curve_x, (3, 4))
        image = (0, 1), (2, 3)
        self.plot.addImage(image)
        scatter_x = 10, 11
        self.plot.addScatter(scatter_x, (12, 13), (0, 1))
        marker_pos = 5, 5
        self.plot.addMarker(*marker_pos)
        marker_x = 6
        self.plot.addXMarker(marker_x)
        self.plot.addShape((0, 5), (2, 10), shape="rectangle")

        items = self.plot.getItems()
        self.assertEqual(len(items), 6)
        self.assertTrue(n.all(n.equal(items[0].getXData(), curve_x)))
        self.assertTrue(n.all(n.equal(items[1].getData(), image)))
        self.assertTrue(n.all(n.equal(items[2].getXData(), scatter_x)))
        self.assertTrue(n.all(n.equal(items[3].getPosition(), marker_pos)))
        self.assertTrue(n.all(n.equal(items[4].getPosition()[0], marker_x)))
        self.assertEqual(items[5].getType(), "rectangle")
//  [python]
def testGetItems(self):
        """Test getItems method"""
        curve_x = 1, 2
        self.plot.addCurve(curve_x, (3, 4))
        image = (0, 1), (2, 3)
        self.plot.addImage(image)
        scatter_x = 10, 11
        self.plot.addScatter(scatter_x, (12, 13), (0, 1))
        marker_pos = 5, 5
        self.plot.addMarker(*marker_pos)
        marker_x = 6
        self.plot.addXMarker(marker_x)
        self.plot.addShape((0, 5), (2, 10), shape="rectangle")

        items = self.plot.getItems()
        self.assertEqual(len(items), 6)
        self.assertTrue(numpy.all(numpy.equal(items[0].getXData(), curve_x)))
        self.assertTrue(numpy.all(numpy.equal(items[1].getData(), image)))
        self.assertTrue(numpy.all(numpy.equal(items[2].getXData(), scatter_x)))
        self.assertTrue(numpy.all(numpy.equal(items[3].getPosition(), marker_pos)))
        self.assertTrue(numpy.all(numpy.equal(items[4].getPosition()[0], marker_x)))
        self.assertEqual(items[5].getType(), "rectangle")
//  [python]
def setup(self, P):
        """
        Parameters
        ----------
            P: array
                Symmetric positive definite matrix defining the ellipsoid
                shape.
        """
        if not np.allclose(P, P.T):
            raise ValueError(global_vars.ERROR+"P must be symmetric")
        if not np.all(la.eigvals(P) > 0):
            raise ValueError(global_vars.ERROR+"P must be positive definite")
        if la.cond(P) > 1e5:
            raise ValueError(global_vars.ERROR+"P is very badly conditioned (condition number "
                            "%.4e)"%(la.cond(P)))
            
        self.P = P # {x : x^T*P*x <= t}
        self.S = la.inv(P) # "Covariance" matrix, {x : x^T*S^-1*x <= t}
        self.L = la.cholesky(self.S)
//  [python]
def test_copysort(self):
        # copy table
        oldNode = self.h5file.get_node('/foo')

        # create completely sorted index on a main column
        oldNode.colinstances[self.sort_field].create_csindex()

        # this fails on ade2ba123efd267fd31
        # see gh-156
        new_node = oldNode.copy(newname='foo2', overwrite=True,
                                sortby=self.sort_field, checkCSI=True,
                                propindexes=True)

        # check column is sorted
        self.assertTrue(n.all(
            new_node.col(self.sort_field) ==
            sorted(oldNode.col(self.sort_field))))
        # check index is available
        self.assertTrue(self.sort_field in new_node.colindexes)
        # check CSI was propagated
        self.assertTrue(new_node.colindexes[self.sort_field].is_csi)
//  [python]
def test_copysort(self):
        # copy table
        oldNode = self.h5file.get_node('/foo')

        # create completely sorted index on a main column
        oldNode.colinstances[self.sort_field].create_csindex()

        # this fails on ade2ba123efd267fd31
        # see gh-156
        new_node = oldNode.copy(newname='foo2', overwrite=True,
                                sortby=self.sort_field, checkCSI=True,
                                propindexes=True)

        # check column is sorted
        self.assertTrue(numpy.all(
            new_node.col(self.sort_field) ==
            sorted(oldNode.col(self.sort_field))))
        # check index is available
        self.assertTrue(self.sort_field in new_node.colindexes)
        # check CSI was propagated
        self.assertTrue(new_node.colindexes[self.sort_field].is_csi)
//  [python]
def fraction_timeseries(
    adata,
    xkey="clusters",
    tkey="dpt_pseudotime",
    bins=30,
    legend_loc="best",
    title=None,
    fontsize=None,
    ax=None,
    figsize=None,
    dpi=None,
    xlabel=None,
    ylabel=None,
    show=True,
):
    """TODO."""
    t = np.linspace(0, 1 + 1 / bins, bins)
    types = np.unique(adata.obs[xkey].values)

    y = []
    for i in range(bins - 1):
        mask = np.logical_and(
            adata.obs[tkey].values <= t[i + 1], adata.obs[tkey].values > t[i]
        )
        x = list(adata[mask].obs[xkey].values)
        y.append([])
        for name in types:
            occur = x.count(name)
            y[-1].append(occur)
        y[-1] /= np.sum(y[-1])
    y = np.array(y).T

    ax = pl.figure(figsize=figsize, dpi=dpi) if ax is None else ax

    c = None
    if "clusters_colors" in adata.uns.keys():
        c = adata.uns["clusters_colors"]
    pl.stackplot(t[:-1], y, baseline="zero", labels=types, colors=c, edgecolor="white")

    pl.legend(types, loc=legend_loc)
    if title is not None:
        pl.title(title, fontsize=fontsize)
    pl.xlabel(tkey if xlabel is None else xlabel, fontsize=fontsize)
    pl.ylabel(f"{xkey} fractions" if ylabel is None else ylabel, fontsize=fontsize)
    pl.xlim(adata.obs[tkey].values.min(), adata.obs[tkey].values.max())
    pl.ylim(0, 1)

    if not show:
        return ax
    else:
        pl.show()
//  [python]
def fraction_timeseries(
    adata,
    xkey="clusters",
    tkey="dpt_pseudotime",
    bins=30,
    legend_loc="best",
    title=None,
    fontsize=None,
    ax=None,
    figsize=None,
    dpi=None,
    xlabel=None,
    ylabel=None,
    show=True,
):
    """TODO."""
    t = np.linspace(0, 1 + 1 / bins, bins)
    types = np.unique(adata.obs[xkey].values)

    y = []
    for i in range(bins - 1):
        mask = np.all(
            [adata.obs[tkey].values <= t[i + 1], adata.obs[tkey].values > t[i]], axis=0
        )
        x = list(adata[mask].obs[xkey].values)
        y.append([])
        for name in types:
            occur = x.count(name)
            y[-1].append(occur)
        y[-1] /= np.sum(y[-1])
    y = np.array(y).T

    ax = pl.figure(figsize=figsize, dpi=dpi) if ax is None else ax

    c = None
    if "clusters_colors" in adata.uns.keys():
        c = adata.uns["clusters_colors"]
    pl.stackplot(t[:-1], y, baseline="zero", labels=types, colors=c, edgecolor="white")

    pl.legend(types, loc=legend_loc)
    if title is not None:
        pl.title(title, fontsize=fontsize)
    pl.xlabel(tkey if xlabel is None else xlabel, fontsize=fontsize)
    pl.ylabel(f"{xkey} fractions" if ylabel is None else ylabel, fontsize=fontsize)
    pl.xlim(adata.obs[tkey].values.min(), adata.obs[tkey].values.max())
    pl.ylim(0, 1)

    if not show:
        return ax
    else:
        pl.show()
//  [python]
def record_undo(self):
        """Push an undo record onto the undo stack"""
        #
        # The undo record is a diff between the last ijv and
        # the current, plus the current state of the artists.
        #
        ijv = self.calculate_ijv()
        if ijv.shape[0] == 0:
            ijvx = numpy.zeros((0, 4), int)
        else:
            #
            # Sort the current and last ijv together, adding
            # an old_new_indicator.
            #
            ijvx = numpy.vstack(
                (
                    numpy.column_stack((ijv, numpy.zeros(ijv.shape[0], ijv.dtype))),
                    numpy.column_stack(
                        (self.last_ijv, numpy.ones(self.last_ijv.shape[0], ijv.dtype))
                    ),
                )
            )
            order = numpy.lexsort((ijvx[:, 3], ijvx[:, 2], ijvx[:, 1], ijvx[:, 0]))
            ijvx = ijvx[order, :]
            #
            # Then mark all prev and next where i,j,v match (in both sets)
            #
            matches = numpy.hstack(
                (
                    (
                        numpy.alltrue((ijvx[:-1, :3] == ijvx[1:, :3]).reshape(-1,3), axis=1)
                        & (ijvx[:-1, 3] == 0)
                        & (ijvx[1:, 3] == 1)
                    ),
                    [False],
                )
            )
            matches[1:] = matches[1:] | matches[:-1]
            ijvx = ijvx[~matches, :]
        artist_save = [(a.get_data(), self.artists[a].copy()) for a in self.artists]
        self.undo_stack.append((ijvx, self.last_artist_save, self.last_to_keep))
        self.last_artist_save = artist_save
        self.last_ijv = ijv
        self.last_to_keep = self.to_keep
        self.undo_button.Enable(True)
//  [python]
def record_undo(self):
        """Push an undo record onto the undo stack"""
        #
        # The undo record is a diff between the last ijv and
        # the current, plus the current state of the artists.
        #
        ijv = self.calculate_ijv()
        if ijv.shape[0] == 0:
            ijvx = numpy.zeros((0, 4), int)
        else:
            #
            # Sort the current and last ijv together, adding
            # an old_new_indicator.
            #
            ijvx = numpy.vstack(
                (
                    numpy.column_stack((ijv, numpy.zeros(ijv.shape[0], ijv.dtype))),
                    numpy.column_stack(
                        (self.last_ijv, numpy.ones(self.last_ijv.shape[0], ijv.dtype))
                    ),
                )
            )
            order = numpy.lexsort((ijvx[:, 3], ijvx[:, 2], ijvx[:, 1], ijvx[:, 0]))
            ijvx = ijvx[order, :]
            #
            # Then mark all prev and next where i,j,v match (in both sets)
            #
            matches = numpy.hstack(
                (
                    (
                        numpy.all(ijvx[:-1, :3] == ijvx[1:, :3], 1)
                        & (ijvx[:-1, 3] == 0)
                        & (ijvx[1:, 3] == 1)
                    ),
                    [False],
                )
            )
            matches[1:] = matches[1:] | matches[:-1]
            ijvx = ijvx[~matches, :]
        artist_save = [(a.get_data(), self.artists[a].copy()) for a in self.artists]
        self.undo_stack.append((ijvx, self.last_artist_save, self.last_to_keep))
        self.last_artist_save = artist_save
        self.last_ijv = ijv
        self.last_to_keep = self.to_keep
        self.undo_button.Enable(True)
//  [python]
def counts_to_PR(ngt, nres, ninter, mode="overall"):
    """ computes a  precision-recall for a ser of queries.
    ngt = nb of GT results per query
    nres = nb of found results per query
    ninter = nb of correct results per query (smaller than nres of course)
    """

    if mode == "overall":
        ngt, nres, ninter = ngt.sum(), nres.sum(), ninter.sum()

        if nres > 0:
            precision = ninter / nres
        else:
            precision = 1.0

        if ngt > 0:
            recall = ninter / ngt
        elif nres == 0:
            recall = 1.0
        else:
            recall = 0.0

        return precision, recall

    elif mode == "average":
        # average precision and recall over queries

        mask = ngt == 0
        ngt[mask] = 1

        recalls = ninter / ngt
        recalls[mask] = (nres[mask] == 0).astype(float)

        # avoid division by 0
        mask = nres == 0
        assert n.all(ninter[mask] == 0)
        ninter[mask] = 1
        nres[mask] = 1

        precisions = ninter / nres

        return precisions.mean(), recalls.mean()

    else:
        raise AssertionError()
//  [python]
def counts_to_PR(ngt, nres, ninter, mode="overall"):
    """ computes a  precision-recall for a ser of queries.
    ngt = nb of GT results per query
    nres = nb of found results per query
    ninter = nb of correct results per query (smaller than nres of course)
    """

    if mode == "overall":
        ngt, nres, ninter = ngt.sum(), nres.sum(), ninter.sum()

        if nres > 0:
            precision = ninter / nres
        else:
            precision = 1.0

        if ngt > 0:
            recall = ninter / ngt
        elif nres == 0:
            recall = 1.0
        else:
            recall = 0.0

        return precision, recall

    elif mode == "average":
        # average precision and recall over queries

        mask = ngt == 0
        ngt[mask] = 1

        recalls = ninter / ngt
        recalls[mask] = (nres[mask] == 0).astype(float)

        # avoid division by 0
        mask = nres == 0
        assert np.all(ninter[mask] == 0)
        ninter[mask] = 1
        nres[mask] = 1

        precisions = ninter / nres

        return precisions.mean(), recalls.mean()

    else:
        raise AssertionError()
//  [python]
def check_ref_knn_with_draws(Dref, Iref, Dnew, Inew, rtol=1e-5):
    """ test that knn search results are identical, with possible ties.
    Raise if not. """
    np.testing.assert_allclose(Dref, Dnew, rtol=rtol)
    # here we have to be careful because of draws
    testcase = unittest.TestCase()   # because it makes nice error messages
    for i in range(len(Iref)):
        if n.all(Iref[i] == Inew[i]): # easy case
            continue

        # otherwise collect elements per distance
        r = rtol * Dref[i].max()

        DrefC, DnewC = _cluster_tables_with_tolerance(Dref[i], Dnew[i], r)

        for dis in np.unique(DrefC):
            if dis == DrefC[-1]:
                continue
            mask = DrefC == dis
            testcase.assertEqual(set(Iref[i, mask]), set(Inew[i, mask]))
//  [python]
def check_ref_knn_with_draws(Dref, Iref, Dnew, Inew, rtol=1e-5):
    """ test that knn search results are identical, with possible ties.
    Raise if not. """
    np.testing.assert_allclose(Dref, Dnew, rtol=rtol)
    # here we have to be careful because of draws
    testcase = unittest.TestCase()   # because it makes nice error messages
    for i in range(len(Iref)):
        if np.all(Iref[i] == Inew[i]): # easy case
            continue

        # otherwise collect elements per distance
        r = rtol * Dref[i].max()

        DrefC, DnewC = _cluster_tables_with_tolerance(Dref[i], Dnew[i], r)

        for dis in np.unique(DrefC):
            if dis == DrefC[-1]:
                continue
            mask = DrefC == dis
            testcase.assertEqual(set(Iref[i, mask]), set(Inew[i, mask]))
//  [python]
def on_mouse_button_up_delete_mode(self, event):
        to_delete = []
        if self.delete_mode_filter is not None:
            for artist in self.artists:
                x, y = artist.get_data()
                f = ~self.delete_mode_filter(x, y)
                if n.all(f):
                    continue
                xy = n.column_stack((x[f], y[f]))
                if len(xy) < 3 or len(xy) == 3 and n.all(xy[0] == xy[-1]):
                    to_delete.append(artist)
                else:
                    if n.all(xy[0] != xy[-1]):
                        xy = n.vstack((xy, xy[:1]))
                    artist.set_data(xy.transpose())
                    self.artists[artist][self.K_EDITED] = True
        object_numbers = set()
        for artist in to_delete:
            object_numbers.add(self.artists[artist][self.K_LABEL])
            artist.remove()
            del self.artists[artist]
        for artist, d in list(self.artists.items()):
            if d[self.K_LABEL] in object_numbers:
                d[self.K_EDITED] = True
                object_numbers.remove(d[self.K_LABEL])
        if len(object_numbers) > 0:
            # Exit delete mode here because self.display() wipes artists
            self.exit_delete_mode(event)
            for object_number in object_numbers:
                self.remove_label(object_number)
            self.init_labels()
            self.display()
        else:
            self.exit_delete_mode(event)
        self.record_undo()
//  [python]
def on_mouse_button_up_delete_mode(self, event):
        to_delete = []
        if self.delete_mode_filter is not None:
            for artist in self.artists:
                x, y = artist.get_data()
                f = ~self.delete_mode_filter(x, y)
                if numpy.all(f):
                    continue
                xy = numpy.column_stack((x[f], y[f]))
                if len(xy) < 3 or len(xy) == 3 and numpy.all(xy[0] == xy[-1]):
                    to_delete.append(artist)
                else:
                    if numpy.all(xy[0] != xy[-1]):
                        xy = numpy.vstack((xy, xy[:1]))
                    artist.set_data(xy.transpose())
                    self.artists[artist][self.K_EDITED] = True
        object_numbers = set()
        for artist in to_delete:
            object_numbers.add(self.artists[artist][self.K_LABEL])
            artist.remove()
            del self.artists[artist]
        for artist, d in list(self.artists.items()):
            if d[self.K_LABEL] in object_numbers:
                d[self.K_EDITED] = True
                object_numbers.remove(d[self.K_LABEL])
        if len(object_numbers) > 0:
            # Exit delete mode here because self.display() wipes artists
            self.exit_delete_mode(event)
            for object_number in object_numbers:
                self.remove_label(object_number)
            self.init_labels()
            self.display()
        else:
            self.exit_delete_mode(event)
        self.record_undo()
//  [python]
def undo(self, event=None):
        """Pop an entry from the undo stack and apply"""
        #
        # Mix what's on the undo ijv with what's in self.last_ijv
        # and remove any 0/1 pairs.
        #
        ijvx, artist_save, self.to_keep = self.undo_stack.pop()
        ijvx = numpy.vstack(
            (
                ijvx,
                numpy.column_stack(
                    (
                        self.last_ijv,
                        numpy.ones(self.last_ijv.shape[0], self.last_ijv.dtype),
                    )
                ),
            )
        )
        order = numpy.lexsort((ijvx[:, 3], ijvx[:, 2], ijvx[:, 1], ijvx[:, 0]))
        ijvx = ijvx[order, :]
        #
        # Then mark all prev and next where i,j,v match (in both sets)
        #
        matches = numpy.hstack((numpy.all(ijvx[:-1, :3] == ijvx[1:, :3], axis=1), [False]))
        matches[1:] = matches[1:] | matches[:-1]
        ijvx = ijvx[~matches, :]
        self.last_ijv = ijvx[:, :3]
        self.last_artist_save = artist_save
        self.last_to_keep = self.to_keep
        temp = Objects()
        temp.set_ijv(self.last_ijv, shape=self.shape)
        self.labels = [l for l, c in temp.get_labels()]
        self.init_labels()
        #
        # replace the artists
        #
        for artist in self.artists:
            artist.remove()
        self.artists = {}
        for (x, y), d in artist_save:
            object_number = d[self.K_LABEL]
            artist = matplotlib.lines.Line2D(
                x,
                y,
                marker="o",
                markerfacecolor="r",
                markersize=6,
                color=self.colormap[object_number, :],
                animated=True,
            )
            self.artists[artist] = d
            self.orig_axes.add_line(artist)
        self.display()
        if len(self.undo_stack) == 0:
            self.undo_button.Enable(False)
//  [python]
def undo(self, event=None):
        """Pop an entry from the undo stack and apply"""
        #
        # Mix what's on the undo ijv with what's in self.last_ijv
        # and remove any 0/1 pairs.
        #
        ijvx, artist_save, self.to_keep = self.undo_stack.pop()
        ijvx = numpy.vstack(
            (
                ijvx,
                numpy.column_stack(
                    (
                        self.last_ijv,
                        numpy.ones(self.last_ijv.shape[0], self.last_ijv.dtype),
                    )
                ),
            )
        )
        order = numpy.lexsort((ijvx[:, 3], ijvx[:, 2], ijvx[:, 1], ijvx[:, 0]))
        ijvx = ijvx[order, :]
        #
        # Then mark all prev and next where i,j,v match (in both sets)
        #
        matches = numpy.hstack((numpy.all(ijvx[:-1, :3] == ijvx[1:, :3], 1), [False]))
        matches[1:] = matches[1:] | matches[:-1]
        ijvx = ijvx[~matches, :]
        self.last_ijv = ijvx[:, :3]
        self.last_artist_save = artist_save
        self.last_to_keep = self.to_keep
        temp = Objects()
        temp.set_ijv(self.last_ijv, shape=self.shape)
        self.labels = [l for l, c in temp.get_labels()]
        self.init_labels()
        #
        # replace the artists
        #
        for artist in self.artists:
            artist.remove()
        self.artists = {}
        for (x, y), d in artist_save:
            object_number = d[self.K_LABEL]
            artist = matplotlib.lines.Line2D(
                x,
                y,
                marker="o",
                markerfacecolor="r",
                markersize=6,
                color=self.colormap[object_number, :],
                animated=True,
            )
            self.artists[artist] = d
            self.orig_axes.add_line(artist)
        self.display()
        if len(self.undo_stack) == 0:
            self.undo_button.Enable(False)
//  [python]
def compare_keys(self, k1, k2):
        if n.all(k1 >= k2):
            return 1
        if n.all(k2 >= k1):
            return -1
        return 0
//  [python]
def compare_keys(self, k1, k2):
        if np.all(k1 >= k2):
            return 1
        if np.all(k2 >= k1):
            return -1
        return 0
//  [python]
def check_ref_range_results(Lref, Dref, Iref,
                            Lnew, Dnew, Inew):
    """ compare range search results wrt. a reference result,
    throw if it fails """
    np.testing.assert_array_equal(Lref, Lnew)
    nq = len(Lref) - 1
    for i in range(nq):
        l0, l1 = Lref[i], Lref[i + 1]
        Ii_ref = Iref[l0:l1]
        Ii_new = Inew[l0:l1]
        Di_ref = Dref[l0:l1]
        Di_new = Dnew[l0:l1]
        if n.all(Ii_ref == Ii_new): # easy
            pass
        else:
            def sort_by_ids(I, D):
                o = I.argsort()
                return I[o], D[o]
            # sort both
            (Ii_ref, Di_ref) = sort_by_ids(Ii_ref, Di_ref)
            (Ii_new, Di_new) = sort_by_ids(Ii_new, Di_new)
            np.testing.assert_array_equal(Ii_ref, Ii_new)
        np.testing.assert_array_almost_equal(Di_ref, Di_new, decimal=5)
//  [python]
def check_ref_range_results(Lref, Dref, Iref,
                            Lnew, Dnew, Inew):
    """ compare range search results wrt. a reference result,
    throw if it fails """
    np.testing.assert_array_equal(Lref, Lnew)
    nq = len(Lref) - 1
    for i in range(nq):
        l0, l1 = Lref[i], Lref[i + 1]
        Ii_ref = Iref[l0:l1]
        Ii_new = Inew[l0:l1]
        Di_ref = Dref[l0:l1]
        Di_new = Dnew[l0:l1]
        if np.all(Ii_ref == Ii_new): # easy
            pass
        else:
            def sort_by_ids(I, D):
                o = I.argsort()
                return I[o], D[o]
            # sort both
            (Ii_ref, Di_ref) = sort_by_ids(Ii_ref, Di_ref)
            (Ii_new, Di_new) = sort_by_ids(Ii_new, Di_new)
            np.testing.assert_array_equal(Ii_ref, Ii_new)
        np.testing.assert_array_almost_equal(Di_ref, Di_new, decimal=5)
//  [python]
def is_finite(matrix):
    """Returns true only if all the values of a ndarray or sparse matrix are finite"""
    if issparse(matrix):
        return np.alltrue(np.isfinite(matrix.tocoo().data))
    else:
        return np.alltrue(np.isfinite(matrix))
//  [python]
def is_finite(matrix):
    """Returns true only if all the values of a ndarray or sparse matrix are finite"""
    if issparse(matrix):
        return np.all(np.isfinite(matrix.tocoo().data))
    else:
        return np.all(np.isfinite(matrix))
//  [python]
def test_points(load_points):
  _, points_numpy, _ = load_points

  points = small_gicp.PointCloud(points_numpy)
  assert points.size() == points_numpy.shape[0]
  assert n.all(n.abs(points.points() - points_numpy) < 1e-6)

  points = small_gicp.PointCloud(points_numpy[:, :3])
  assert points.size() == points_numpy.shape[0]
  assert n.all(n.abs(points.points() - points_numpy) < 1e-6)
  
  for i in range(10):
    assert n.all(n.abs(points.point(i) - points_numpy[i]) < 1e-6)
//  [python]
def test_points(load_points):
  _, points_numpy, _ = load_points

  points = small_gicp.PointCloud(points_numpy)
  assert points.size() == points_numpy.shape[0]
  assert numpy.all(numpy.abs(points.points() - points_numpy) < 1e-6)

  points = small_gicp.PointCloud(points_numpy[:, :3])
  assert points.size() == points_numpy.shape[0]
  assert numpy.all(numpy.abs(points.points() - points_numpy) < 1e-6)
  
  for i in range(10):
    assert numpy.all(numpy.abs(points.point(i) - points_numpy[i]) < 1e-6)
//  [python]
def faiss_sanity_check():
    ds = SyntheticDataset(256, 0, 100, 100)
    xq = ds.get_queries()
    xb = ds.get_database()
    index_cpu = faiss.IndexFlat(ds.d)
    index_gpu = faiss.index_cpu_to_all_gpus(index_cpu)
    index_cpu.add(xb)
    index_gpu.add(xb)
    D_cpu, I_cpu = index_cpu.search(xq, 10)
    D_gpu, I_gpu = index_gpu.search(xq, 10)
    assert n.all(I_cpu == I_gpu), "faiss sanity check failed"
    assert n.all(n.isclose(D_cpu, D_gpu)), "faiss sanity check failed"
//  [python]
def faiss_sanity_check():
    ds = SyntheticDataset(256, 0, 100, 100)
    xq = ds.get_queries()
    xb = ds.get_database()
    index_cpu = faiss.IndexFlat(ds.d)
    index_gpu = faiss.index_cpu_to_all_gpus(index_cpu)
    index_cpu.add(xb)
    index_gpu.add(xb)
    D_cpu, I_cpu = index_cpu.search(xq, 10)
    D_gpu, I_gpu = index_gpu.search(xq, 10)
    assert np.all(I_cpu == I_gpu), "faiss sanity check failed"
    assert np.all(np.isclose(D_cpu, D_gpu)), "faiss sanity check failed"
//  [python]
def get_finite_row_indices(matrix):
    """Returns the indices of the purely finite rows of a sparse matrix or dense ndarray"""
    if issparse(matrix):
        row_indices = np.array(
            [i for i, row in enumerate(matrix.tolil().data) if np.all(np.isfinite(row))]
        )
    else:
        row_indices = np.where(np.isfinite(matrix).sum(axis=1) == matrix.shape[1])[0]
    return row_indices
//  [python]
def inv_filter(img, S, noise=0.001):

    # If you want to call inv_filter, you have to impute first to avoid
    # spectral artifacts
    assert(n.all(img.mask == False))

    # unfortunately scipy deconvolution messes with our scale...
    F = pad2(img)

    # Do my scaling here to keep scipy happy!
    machine_scale = F.max()
    F /= machine_scale

    S = S / S.sum()  # avoids any scaling effects

    img_w, img_h, img_ch = img.shape
    out = np.zeros_like(F)
    for ch in range(img_ch):
        out[:, :, ch] = deconvolution.wiener(F[:, :, ch].data.astype(float),
                                             S, noise)

    out = machine_scale * out[:img_w, :img_h]
    # out[img.mask] = 0.
    return np.ma.masked_array(out, mask=img.mask)
//  [python]
def inv_filter(img, S, noise=0.001):

    # If you want to call inv_filter, you have to impute first to avoid
    # spectral artifacts
    assert(np.all(img.mask == False))

    # unfortunately scipy deconvolution messes with our scale...
    F = pad2(img)

    # Do my scaling here to keep scipy happy!
    machine_scale = F.max()
    F /= machine_scale

    S = S / S.sum()  # avoids any scaling effects

    img_w, img_h, img_ch = img.shape
    out = np.zeros_like(F)
    for ch in range(img_ch):
        out[:, :, ch] = deconvolution.wiener(F[:, :, ch].data.astype(float),
                                             S, noise)

    out = machine_scale * out[:img_w, :img_h]
    # out[img.mask] = 0.
    return np.ma.masked_array(out, mask=img.mask)
//  [python]
def test_deterministic(self):
        with pm.Model() as model:
            data_values = np.array([0.5, 0.4, 5, 2])
            X = pytensor.shared(np.asarray(data_values, dtype=pytensor.config.floatX), borrow=True)
            pm.Normal("y", 0, 1, observed=X)
            assert n.all(
                n.isclose(model.compile_logp(sum=False)({}), st.norm().logpdf(data_values))
            )
//  [python]
def test_deterministic(self):
        with pm.Model() as model:
            data_values = np.array([0.5, 0.4, 5, 2])
            X = pytensor.shared(np.asarray(data_values, dtype=pytensor.config.floatX), borrow=True)
            pm.Normal("y", 0, 1, observed=X)
            assert np.all(
                np.isclose(model.compile_logp(sum=False)({}), st.norm().logpdf(data_values))
            )
//  [python]
def test_model_logp(jacobian):
    with pm.Model() as m:
        x = pm.Normal("x", 0, 1, size=2)
        y = pm.LogNormal("y", 0, 1, size=2)

    test_vals = np.array([0.0, 1.0])

    expected_x_logp = st.norm().logpdf(test_vals)
    expected_y_logp = expected_x_logp.copy()
    if not jacobian:
        expected_y_logp -= np.array([0.0, 1.0])

    x_logp, y_logp = m.compile_logp(sum=False, jacobian=jacobian)(
        {"x": test_vals, "y_log__": test_vals}
    )
    assert n.all(n.isclose(x_logp, expected_x_logp))
    assert n.all(n.isclose(y_logp, expected_y_logp))

    x_logp2 = m.compile_logp(vars=[x], sum=False, jacobian=jacobian)({"x": test_vals})
    assert n.all(n.isclose(x_logp2, expected_x_logp))

    y_logp2 = m.compile_logp(vars=[y], sum=False, jacobian=jacobian)({"y_log__": test_vals})
    assert n.all(n.isclose(y_logp2, expected_y_logp))

    logp_sum = m.compile_logp(sum=True, jacobian=jacobian)({"x": test_vals, "y_log__": test_vals})
    assert n.isclose(logp_sum, expected_x_logp.sum() + expected_y_logp.sum())
//  [python]
def test_model_logp(jacobian):
    with pm.Model() as m:
        x = pm.Normal("x", 0, 1, size=2)
        y = pm.LogNormal("y", 0, 1, size=2)

    test_vals = np.array([0.0, 1.0])

    expected_x_logp = st.norm().logpdf(test_vals)
    expected_y_logp = expected_x_logp.copy()
    if not jacobian:
        expected_y_logp -= np.array([0.0, 1.0])

    x_logp, y_logp = m.compile_logp(sum=False, jacobian=jacobian)(
        {"x": test_vals, "y_log__": test_vals}
    )
    assert np.all(np.isclose(x_logp, expected_x_logp))
    assert np.all(np.isclose(y_logp, expected_y_logp))

    x_logp2 = m.compile_logp(vars=[x], sum=False, jacobian=jacobian)({"x": test_vals})
    assert np.all(np.isclose(x_logp2, expected_x_logp))

    y_logp2 = m.compile_logp(vars=[y], sum=False, jacobian=jacobian)({"y_log__": test_vals})
    assert np.all(np.isclose(y_logp2, expected_y_logp))

    logp_sum = m.compile_logp(sum=True, jacobian=jacobian)({"x": test_vals, "y_log__": test_vals})
    assert np.isclose(logp_sum, expected_x_logp.sum() + expected_y_logp.sum())
//  [python]
def _amIdoneIterating(self, f_k_new, relative_tolerance, iteration, maximum_iterations, print_warning, verbose):
        """
        Convenience function to test whether we are done iterating, same for all iteration types

        REQUIRED ARGUMENTS
          f_k_new (array): new free energies
          f_k (array) : older free energies
          relative_tolerance (float): the relative tolerance for terminating
          verbose (bool): verbose response
          iterations (int): current number of iterations
          print_warning (bool): sometimes, we want to surpress the warning.

        RETURN VALUES
           yesIam (bool): indicates that the iteration has converged.

        """
        yesIam = False

        # Compute change from old to new estimate.
        Delta_f_k = f_k_new - self.f_k[self.states_with_samples]

        # Check convergence criteria.
        # Terminate when max((f - fold) / f) < relative_tolerance for all
        # nonzero f.
        max_delta = np.max(
            np.abs(Delta_f_k) / np.max(np.abs(f_k_new)))

        # Update stored free energies.
        f_k = f_k_new.copy()
        self.f_k[self.states_with_samples] = f_k

        # write out current estimate
        if verbose:
            print("current f_k for states with samples =")
            print(f_k)
            print("relative max_delta = %e" % max_delta)

        # Check convergence criteria.
        # Terminate when max((f - fold) / f) < relative_tolerance for all
        # nonzero f.
        if np.isnan(max_delta) or (max_delta < relative_tolerance):
            yesIam = True

        if (yesIam):
            # Report convergence, or warn user if convergence was not achieved.
            if np.all(self.f_k == 0.0):
                # all f_k appear to be zero
                print('WARNING: All f_k appear to be zero.')
            elif (max_delta < relative_tolerance):
                # Convergence achieved.
                if verbose:
                    print('Converged to tolerance of %e in %d iterations.' % (max_delta, iteration + 1))
            elif (print_warning):
                # Warn that convergence was not achieved.
                # many times, self-consistent iteration is used in conjunction with another program.  In that case,
                # we don't really need to warn about anything, since we are not
                # running it to convergence.
                print('WARNING: Did not converge to within specified tolerance.')
                print('max_delta = %e, TOLERANCE = %e, MAX_ITS = %d, iterations completed = %d' % (max_delta, relative_tolerance, maximum_iterations, iteration))

        return yesIam
//  [python]
def test_model_d2logp(jacobian):
    with pm.Model() as m:
        x = pm.Normal("x", 0, 1, size=2)
        y = pm.LogNormal("y", 0, 1, size=2)

    test_vals = np.array([0.0, -1.0])
    state = {"x": test_vals, "y_log__": test_vals}

    expected_x_d2logp = expected_y_d2logp = np.eye(2)

    dlogps = m.compile_d2logp(jacobian=jacobian)(state)
    assert n.all(n.isclose(dlogps[:2, :2], expected_x_d2logp))
    assert n.all(n.isclose(dlogps[2:, 2:], expected_y_d2logp))

    x_dlogp2 = m.compile_d2logp(vars=[x], jacobian=jacobian)(state)
    assert n.all(n.isclose(x_dlogp2, expected_x_d2logp))

    y_dlogp2 = m.compile_d2logp(vars=[y], jacobian=jacobian)(state)
    assert n.all(n.isclose(y_dlogp2, expected_y_d2logp))
//  [python]
def test_model_d2logp(jacobian):
    with pm.Model() as m:
        x = pm.Normal("x", 0, 1, size=2)
        y = pm.LogNormal("y", 0, 1, size=2)

    test_vals = np.array([0.0, -1.0])
    state = {"x": test_vals, "y_log__": test_vals}

    expected_x_d2logp = expected_y_d2logp = np.eye(2)

    dlogps = m.compile_d2logp(jacobian=jacobian)(state)
    assert np.all(np.isclose(dlogps[:2, :2], expected_x_d2logp))
    assert np.all(np.isclose(dlogps[2:, 2:], expected_y_d2logp))

    x_dlogp2 = m.compile_d2logp(vars=[x], jacobian=jacobian)(state)
    assert np.all(np.isclose(x_dlogp2, expected_x_d2logp))

    y_dlogp2 = m.compile_d2logp(vars=[y], jacobian=jacobian)(state)
    assert np.all(np.isclose(y_dlogp2, expected_y_d2logp))
//  [python]
def test_missing_vector_parameter(self):
        with pm.Model() as m:
            with pytest.warns(ImputationWarning):
                x = pm.Normal(
                    "x",
                    np.array([-10, 10]),
                    0.1,
                    observed=np.array([[np.nan, 10], [-10, np.nan], [np.nan, np.nan]]),
                )
        x_draws = x.eval()
        assert x_draws.shape == (3, 2)
        assert n.all(x_draws[:, 0] < 0)
        assert n.all(x_draws[:, 1] > 0)
        assert np.isclose(
            m.compile_logp()({"x_unobserved": np.array([-10, 10, -10, 10])}),
            st.norm(scale=0.1).logpdf(0) * 6,
        )
//  [python]
def test_missing_vector_parameter(self):
        with pm.Model() as m:
            with pytest.warns(ImputationWarning):
                x = pm.Normal(
                    "x",
                    np.array([-10, 10]),
                    0.1,
                    observed=np.array([[np.nan, 10], [-10, np.nan], [np.nan, np.nan]]),
                )
        x_draws = x.eval()
        assert x_draws.shape == (3, 2)
        assert np.all(x_draws[:, 0] < 0)
        assert np.all(x_draws[:, 1] > 0)
        assert np.isclose(
            m.compile_logp()({"x_unobserved": np.array([-10, 10, -10, 10])}),
            st.norm(scale=0.1).logpdf(0) * 6,
        )
//  [python]
def test_model_dlogp(jacobian):
    with pm.Model() as m:
        x = pm.Normal("x", 0, 1, size=2)
        y = pm.LogNormal("y", 0, 1, size=2)

    test_vals = np.array([0.0, -1.0])
    state = {"x": test_vals, "y_log__": test_vals}

    expected_x_dlogp = expected_y_dlogp = np.array([0.0, 1.0])
    if not jacobian:
        expected_y_dlogp = np.array([-1.0, 0.0])

    dlogps = m.compile_dlogp(jacobian=jacobian)(state)
    assert n.all(n.isclose(dlogps[:2], expected_x_dlogp))
    assert n.all(n.isclose(dlogps[2:], expected_y_dlogp))

    x_dlogp2 = m.compile_dlogp(vars=[x], jacobian=jacobian)(state)
    assert n.all(n.isclose(x_dlogp2, expected_x_dlogp))

    y_dlogp2 = m.compile_dlogp(vars=[y], jacobian=jacobian)(state)
    assert n.all(n.isclose(y_dlogp2, expected_y_dlogp))
//  [python]
def test_model_dlogp(jacobian):
    with pm.Model() as m:
        x = pm.Normal("x", 0, 1, size=2)
        y = pm.LogNormal("y", 0, 1, size=2)

    test_vals = np.array([0.0, -1.0])
    state = {"x": test_vals, "y_log__": test_vals}

    expected_x_dlogp = expected_y_dlogp = np.array([0.0, 1.0])
    if not jacobian:
        expected_y_dlogp = np.array([-1.0, 0.0])

    dlogps = m.compile_dlogp(jacobian=jacobian)(state)
    assert np.all(np.isclose(dlogps[:2], expected_x_dlogp))
    assert np.all(np.isclose(dlogps[2:], expected_y_dlogp))

    x_dlogp2 = m.compile_dlogp(vars=[x], jacobian=jacobian)(state)
    assert np.all(np.isclose(x_dlogp2, expected_x_dlogp))

    y_dlogp2 = m.compile_dlogp(vars=[y], jacobian=jacobian)(state)
    assert np.all(np.isclose(y_dlogp2, expected_y_dlogp))
//  [python]
def convert_monic_to_orthonormal_recursion_coefficients(ab_monic, probability):
    assert n.all(ab_monic[:, 1] >= 0)
    ab = ab_monic.copy()
    ab[:, 1] = np.sqrt(ab[:, 1])
    if probability:
        ab[0, 1] = 1
    return ab
//  [python]
def convert_monic_to_orthonormal_recursion_coefficients(ab_monic, probability):
    assert np.all(ab_monic[:, 1] >= 0)
    ab = ab_monic.copy()
    ab[:, 1] = np.sqrt(ab[:, 1])
    if probability:
        ab[0, 1] = 1
    return ab
//  [python]
def setup_data(p, test_set=False):
    if p.dataset in ['cifar10','mnist']:
        dataset_class, training_set_size = {
            'cifar10': (CIFAR10, 40000),
            'mnist': (MNIST, 50000),
        }[p.dataset]
    else:
        from fuel.datasets import H5PYDataset
        from fuel.utils import find_in_data_path
        from functools import partial
        fn=p.dataset
        fn=os.path.join(fn, fn + '.hdf5')
        def dataset_class(which_sets):
            return H5PYDataset(file_or_path=find_in_data_path(fn),
                               which_sets=which_sets,
                               load_in_memory=True)
        training_set_size = None

    train_set = dataset_class(["train"])

    # Allow overriding the default from command line
    if p.get('unlabeled_samples') is not None and p.unlabeled_samples >= 0:
        training_set_size = p.unlabeled_samples
    elif training_set_size is None:
        training_set_size = train_set.num_examples

    # Make sure the MNIST data is in right format
    if p.dataset == 'mnist':
        d = train_set.data_sources[train_set.sources.index('features')]
        assert n.all(d <= 1.0) and n.all(d >= 0.0), \
            'Make sure data is in float format and in range 0 to 1'

    # Take all indices and permutate them
    all_ind = numpy.arange(train_set.num_examples)
    if p.get('dseed'):
        rng = numpy.random.RandomState(seed=p.dseed)
        rng.shuffle(all_ind)

    d = AttributeDict()

    # Choose the training set
    d.train = train_set
    d.train_ind = all_ind[:training_set_size]

    # Then choose validation set from the remaining indices
    d.valid = train_set
    d.valid_ind = numpy.setdiff1d(all_ind, d.train_ind)[:p.valid_set_size]
    logger.info('Using %d examples for validation' % len(d.valid_ind))

    # Only touch test data if requested
    if test_set:
        d.test = dataset_class(["test"])
        d.test_ind = numpy.arange(d.test.num_examples)

    # Setup optional whitening, only used for Cifar-10
    in_dim = train_set.data_sources[train_set.sources.index('features')].shape[1:]
    if len(in_dim) > 1 and p.whiten_zca > 0:
        assert numpy.product(in_dim) == p.whiten_zca, \
            'Need %d whitening dimensions, not %d' % (numpy.product(in_dim),
                                                      p.whiten_zca)
    cnorm = ContrastNorm(p.contrast_norm) if p.contrast_norm != 0 else None

    def get_data(d, i):
        data = d.get_data(request=i)[d.sources.index('features')]
        # Fuel provides Cifar in uint8, convert to float32
        data = numpy.require(data, dtype=numpy.float32)
        return data if cnorm is None else cnorm.apply(data)

    if p.whiten_zca > 0:
        logger.info('Whitening using %d ZCA components' % p.whiten_zca)
        whiten = ZCA()
        whiten.fit(p.whiten_zca, get_data(d.train, d.train_ind))
    else:
        whiten = None

    return in_dim, d, whiten, cnorm