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

Partial coloring of the GO graph #5

Open
asher-pembroke opened this issue Dec 7, 2020 · 6 comments
Open

Partial coloring of the GO graph #5

asher-pembroke opened this issue Dec 7, 2020 · 6 comments
Labels
enhancement New feature or request

Comments

@asher-pembroke
Copy link

asher-pembroke commented Dec 7, 2020

Is there a way to initialize a few nodes of the output graph? I'm trying to propagate known colors for some of the nodes, but my output graph appears to ignore them. If need be I'll post some example code.

@lamelizard
Copy link
Owner

Ah, that's one of the modifications I designed but never programmed.

GOs node colors are AFAIK never even read in the first place, in reset():

        # set possible colors per node
        for node in self.GO.nodes():
            self._values_per_node[node] = self._all_colors.copy()

Changing that should not take long.

What annoys me is, that there currently is no interface to change while it runs / change the allowed isomorphisms.
But your case should be 95% of what everyone wants anyway, so for the time being that should be enough.

@lamelizard
Copy link
Owner

According to my notes from 2 years back I also needed to change the constraint propagation -> done.

@asher-pembroke, I created a new branch, (did not test it enough to release), if you could check it out, that would be great (python setup.py install, you may need to remove the version from pip first)

I tried the following code

import networkx as nx
import graphwfc
GI = nx.Graph([(1,2),(2,3),(3,4)])
GI.add_nodes_from([(1,{'c':'b'}),(2,{'c':'b'}),(3,{'c':'r'}),(4,{'c':'y'})])
GL = nx.Graph([(1,2)])
GO = nx.random_tree(40)
leaves = [x for x in GO.nodes() if GO.degree(x)==1]
for n in leaves:
    GO.add_node(n, c='b')

S = graphwfc.GraphWFCState(GO=GO,GLs=[GL],GI=GI,node_attr='c')
while not S.run():
    S.reset()

import matplotlib.pyplot as plt
colors = list(nx.get_node_attributes(S.GO,'c').values())
nx.draw(S.GO, node_color=colors, node_size=100)
plt.show()

@lamelizard
Copy link
Owner

Note to self: changing c='b' to c='y' tends to break (expected exception) while c='r' looks cool -> could use as example in readme if everything works out

@lamelizard lamelizard added the enhancement New feature or request label Dec 7, 2020
@asher-pembroke
Copy link
Author

Hey,
the update works for my graph! This is great.

What annoys me is, that there currently is no interface to change while it runs / change the allowed isomorphisms.
But your case should be 95% of what everyone wants anyway, so for the time being that should be enough.

Not sure if this is what you mean, but I'm trying to restrict a node's initial coloring to a subset of the available ones. I tried to specify a list for that node's color attribute, but that raises TypeError: unhashable type: 'list'

@lamelizard
Copy link
Owner

What I meant was, you call run(1) to do a one step iteration, afterwards you would limit the colors, then call run() again, so that you can interact with the algorithm. AFAIK there exists a WFC implementation or two that can do that, but I would need to come up with a good interface.

Currently I create a set with the one element given as the color attribute -> setting multiple would not work.
I think it should work if I check if the attribute is one element of all colors or if it is a subset (there is technically an edge case, though...)
Let's see...

@lamelizard
Copy link
Owner

Updated the code;
You should now be able to mix colors as node attributes and node attributes that can be converted to sets of colors (it has to be a subset of the colors in GI)

import networkx as nx
import graphwfc
GI = nx.Graph([(1,2),(2,3),(3,4)])
GI.add_nodes_from([(1,{'c':'b'}),(2,{'c':'b'}),(3,{'c':'r'}),(4,{'c':'y'})])
GL = nx.Graph([(1,2)])
GO = nx.random_tree(40)
leaves = [x for x in GO.nodes() if GO.degree(x)==1]
for n in leaves:
    GO.add_node(n, c=('r','y'))

S = graphwfc.GraphWFCState(GO=GO,GLs=[GL],GI=GI,node_attr='c')
while not S.run():
    S.reset()

import matplotlib.pyplot as plt
colors = list(nx.get_node_attributes(S.GO,'c').values())
nx.draw(S.GO, node_color=colors, node_size=100)
plt.show()

as we override the attributes in GO, you should get the values back with S.reset()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants