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

iterating elements #32

Closed
filchristou opened this issue Jun 8, 2022 · 10 comments
Closed

iterating elements #32

filchristou opened this issue Jun 8, 2022 · 10 comments
Labels
enhancement New feature or request

Comments

@filchristou
Copy link
Contributor

I find iterating through the elements of a MetaGraph hard.
For the docs example:

julia> colors = MetaGraph( Graph(), VertexData = String, EdgeData = Symbol, graph_data = "graph_of_colors")
Meta graph based on a {0, 0} undirected simple Int64 graph with vertex labels of type Symbol, vertex metadata of type String, edge metadata of type Symbol, graph metadata given by "graph_of_colors", and default weight 1.0
julia> colors[:red] = "warm";
julia> colors[:yellow] = "warm";
julia> colors[:blue] = "warm";  # wrong metadata
julia> colors[:blue] = "cool";
julia> colors[:red, :yellow] = :orange;
julia> colors[:red, :blue] = :violet;
julia> colors[:yellow, :blue] = :green;

I would do

julia> [colors[label_for(colors,v)] for v in vertices(colors)]
3-element Vector{String}:
 "warm"
 "warm"
 "cool"

which is a little bit tedious to write and I would love some syntactic sugar like:

[get_vertex(colors, v) for v in vertices(colors)]

The iteration of edges is even more tedious to write:

julia> [colors[label_for(colors, e.src), label_for(colors, e.dst)] for e in edges(colors)]
3-element Vector{Symbol}:
 :orange
 :violet
 :green

Ofc it's also ridiculously simple to write such functions alone, but I think such functionality should be provided from the library.

Or is there another easier to way to iterate MetaGraph elements already ?

@filchristou
Copy link
Contributor Author

Locally I create a function which returns all the node-data like following:

julia> get_vertices(x) = getindex.(values(x.vertex_properties), 2)
julia> get_vertices(colors)
3-element Vector{String}:
 "warm"
 "warm"
 "cool"

@bramtayl
Copy link
Collaborator

bramtayl commented Jun 8, 2022

Well, you can use the fields of the objects directly, but that's not really a public interface. I agree, a public interface would be nice. Maybe something like

EdgeIterator which would collect to

[:blue => "cool", :red => "warm", :yellow => "warm"]

and

VertexIterator which would collect to

[(:red, :yellow) => :orange, (:red, :blue) => :violet, (:yellow, :blue) => :green)]

?

`

@filchristou
Copy link
Contributor Author

Probably you meant it the other way around, but yes, something like that would prevent boilerplate code.

@gdalle gdalle added the enhancement New feature or request label Feb 22, 2023
@gdalle
Copy link
Member

gdalle commented Feb 22, 2023

Well, you can use the fields of the objects directly, but that's not really a public interface. I agree, a public interface would be nice. Maybe something like

EdgeIterator which would collect to

[:blue => "cool", :red => "warm", :yellow => "warm"]

and

VertexIterator which would collect to

[(:red, :yellow) => :orange, (:red, :blue) => :violet, (:yellow, :blue) => :green)]

?

`

This sounds good, gonna take a look

@gdalle
Copy link
Member

gdalle commented Feb 22, 2023

Related: #39 and #44

@gdalle
Copy link
Member

gdalle commented Mar 2, 2023

#45 added a labels function

@gdalle
Copy link
Member

gdalle commented Mar 9, 2023

We should also add edge_labels and (in/out)neighbor_labels

@gdalle
Copy link
Member

gdalle commented Mar 9, 2023

And make sure that the order is coherent with the iterators vertices, edges and (in/out)neighbors

@gdalle
Copy link
Member

gdalle commented Mar 9, 2023

thanks @thchr

@gdalle
Copy link
Member

gdalle commented Mar 18, 2023

Solved by #50

@gdalle gdalle closed this as completed Mar 18, 2023
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

3 participants