Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected ordering of nodes in partite graph #4212

Open
StefanUlbrich opened this issue Apr 4, 2025 · 0 comments
Open

Unexpected ordering of nodes in partite graph #4212

StefanUlbrich opened this issue Apr 4, 2025 · 0 comments

Comments

@StefanUlbrich
Copy link

StefanUlbrich commented Apr 4, 2025

Description of bug / unexpected behavior

I want to create a multi-partite graph not fully connected between the layers. It is created by subtracting a probability mass function from layer to layer and therefore, the nodes carry the type tuple[int,int] (layer number, display value). I expect the nodes in the layers / partitions to be either ascending or descending but this is not the case:

Image

Expected behavior

I expect to be able to take influence on how the nodes are rendered by specifying the order in the partitions argument to Graph(). This is not the case.

How to reproduce the issue

Code for reproducing the problem
        loss = [list(range(4)), list(range(5)), list(range(5))]

        layers: list[list[tuple[int, int]]] = [[(0, start)]]

        values = {start}
        edges: list[tuple[tuple[int, int], tuple[int, int]]] = []
        for i, layer_loss in enumerate(loss):
            new_layer = []
            new_values = set()
            edges_layer = []
            for d in layer_loss:
                for v in values:
                    n = int(max(0, v - d))
                    if n not in new_values:
                        new_layer.append((i + 1, n))
                        new_values.add(n)
                    edges_layer.append(((i, v), (i + 1, n)))
            edges += edges_layer
            values = new_values
            layers += [new_layer]

            graph = Graph(
               vertices,
               edges,
               layout="partite",
               partitions=partitions,
            )

Additional comments

I found a solution that works around this behavior:
After digging into networkx, I found out that in networkx.utils.misc.groups the ordering is lost. To avoid calling this tool, one can pass a dictionary that maps the layer number to a sorted list of its nodes:

for i, p in enumerate(partitions):
     subset_key[i] = p

 graph = Graph(
            vertices,
            edges,
            # labels=True,
            labels=labels,
            layout="partite",
            layout_config={"subset_key": subset_key}, # <----- This line suppress a shuffling of nodes
            partitions=partitions,
)

I have the feeling, that this is closer to the expected behavior and should be considered to be added to Graph._partite_layout.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: 🆕 New
Development

No branches or pull requests

1 participant