Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

2.0 KB
Raw Blame
# /// script
# requires-python = ">=3.9"
# dependencies = [
#     "pymde==0.1.18",
# ]
# ///

import marimo

__generated_with = "0.19.7"
app = marimo.App()


@app.cell
def _(mo, n_items):
    mo.md(
        f"""
        # Embedding binary trees

        In this example, we'll use PyMDE to draw binary trees. Sweep the
        slider to draw bigger trees ...

        {n_items}
        """
    )
    return


@app.cell
def _(mo, n_items):
    mo.md(f"Let's embed a binary tree on **{n_items.value}** vertices.")
    return


@app.cell
def _(embedding, pymde, tree):
    pymde.plot(embedding, edges=tree.edges)
    return


@app.cell
def _(embed, make_graph, n_items):
    tree, graph = make_graph(n_items.value)
    embedding = embed(graph)
    return embedding, tree


@app.cell
def _(mo):
    n_items = mo.ui.slider(64, 1024, step=64)
    return (n_items,)


@app.cell
def _(functools, pymde, torch):
    @functools.cache
    def make_graph(n_items):
        edges = []
        stack = [0]
        while stack:
            root = stack.pop()
            c1 = root * 2 + 1
            c2 = root * 2 + 2
            if c1 < n_items:
                edges.append((root, c1))
                stack.append(c1)
            if c2 < n_items:
                edges.append((root, c2))
                stack.append(c2)
        tree = pymde.Graph.from_edges(torch.tensor(edges))
        return tree, pymde.preprocess.graph.shortest_paths(tree)

    return (make_graph,)


@app.cell
def _(functools, pymde):
    @functools.cache
    def embed(shortest_paths_graph):
        mde = pymde.MDE(
            shortest_paths_graph.n_items,
            embedding_dim=2,
            edges=shortest_paths_graph.edges,
            distortion_function=pymde.losses.WeightedQuadratic(
                shortest_paths_graph.distances
            ),
        )
        return mde.embed(verbose=True)

    return (embed,)


@app.cell
def _():
    import functools

    import pymde
    import torch

    import marimo as mo

    return functools, mo, pymde, torch


if __name__ == "__main__":
    app.run()