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
To implement graph union and graph difference by vertex names in Python, in the same way it is already implemented in R #269
Comments
I needed a similar functionality. I have written a proof of concept for such a function below, however this isn't extensively tested. I might polish it and make a merge request if I find time. For now it only maintains vertex attributes. import igraph as ig
def union(g_1, g_2, v_attr_name):
assert len(set(g_1.vs[v_attr_name])) == len(g_1.vs[v_attr_name]), "Merging attribute must be unique"
assert len(set(g_2.vs[v_attr_name])) == len(g_2.vs[v_attr_name]), "Merging attribute must be unique"
v_attr_1 = g_1.vs[v_attr_name]
v_attr_2 = g_2.vs[v_attr_name]
edge_list_by_attribute_1 = set([ tuple(sorted([v_attr_1[u], v_attr_1[v]])) for u,v in g_1.get_edgelist()])
edge_list_by_attribute_2 = set([ tuple(sorted([v_attr_2[u], v_attr_2[v]])) for u,v in g_2.get_edgelist()])
edge_list_by_attribute_merged = edge_list_by_attribute_1.union(edge_list_by_attribute_2)
v_attr_merged = sorted(list(set(g2.vs[v_attr_name]).union(set(g1.vs[v_attr_name]))))
attribute_to_ind = {v_attr_merged:i for i, v_attr_merged in enumerate(v_attr_merged)}
edge_list_merged = [ (attribute_to_ind[i], attribute_to_ind[j]) for i, j in edge_list_by_attribute_merged]
graph_merged = ig.Graph(edge_list_merged)
graph_merged.vs[v_attr_name] = v_attr_merged
# Include attributes that are in both g_1 and g_2. If different attribute values are present in a vertex,
# then the one of g_1 is used
for attr_name_other in set(g_2.vs.attributes()).intersection(set(g_1.vs.attributes())).difference([v_attr_name]):
attr_other = dict()
for v in g_2.vs():
attr_other[ attribute_to_ind[v[v_attr_name]]] = v[attr_name_other]
for v in g_1.vs():
attr_other[ attribute_to_ind[v[v_attr_name]]] = v[attr_name_other]
graph_merged.vs[attr_name_other] = [attr_other[i] for i in xrange(graph_merged.vcount())]
return graph_merged |
Thanks a lot for your work! I'll be happy to review it if you send a pull request. A few comments for the current version:
|
Thanks for the feedback.
Is it safe to assume this behaviour?
|
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Hi people. I think the issue is solved, right? See https://github.com/igraph/igraph/milestone/8 Btw: thanks you all for your hard work and efforts! |
No, it's not solved yet; there are too many corner cases to consider (see my earlier comment and I haven't had time to implement it properly. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs. Thank you for your contributions. |
Implemented in #310 so closing. |
The native C core of igraph supports graph union only by IDs and not
by vertex names. The R interface contains an implementation of "graph
union by vertex names", and it is selected automatically if your graph
has vertex names. However, There is no equivalent function in the Python
interface.
It would be great if you could implement graph union and graph difference by vertex names in Python, in the same way it is already implemented in R.
This issue was also discussed here: https://lists.nongnu.org/archive/html/igraph-help/2015-09/msg00083.html
Thanks
The text was updated successfully, but these errors were encountered: