Comments (4)
Actually, I think the problem is a bit different:
julia> g = MetaGraph(Graph(5))
{5, 0} undirected Int64 metagraph with Float64 weights defined by :weight (default weight 1.0)
julia> for v in vertices(g)
set_prop!(g, v, :name, string(v))
end
julia> get_prop(g,1,:name)
"1"
julia> rem_vertex!(g,1)
true
julia> get_prop(g,5,:name)
"5"
julia> get_prop(g,4,:name)
"4"
julia> get_prop(g,3,:name)
"3"
julia> get_prop(g,2,:name)
"2"
julia> get_prop(g,1,:name)
ERROR: KeyError: key :name not found
Stacktrace:
[1] getindex at ./dict.jl:474 [inlined]
[2] get_prop(::MetaGraphs.MetaGraph{Int64,Float64}, ::Int64, ::Symbol) at /Users/bromberger1/.julia/v0.6/MetaGraphs/src/MetaGraphs.jl:138
julia> g
{4, 0} undirected Int64 metagraph with Float64 weights defined by :weight (default weight 1.0)
from metagraphs.jl.
@my-little-repository this should now perform properly (that is,
mga = MetaGraph(Graph(5))
for v in vertices(mga)
set_prop!(mga, v, :name, string(v))
end
@test get_prop(mga, 1, :name) == "1"
@test get_prop(mga, 5, :name) == "5"
@test rem_vertex!(mga, 1)
@test get_prop(mga, 1, :name) == "5"
@test isempty(props(mga, 5))
). Let me know if you continue to see issues.
from metagraphs.jl.
Note that the code in my first post in this thread now returns an error.
let g = MetaGraph(Graph(5))
for v in vertices(g)
set_prop!(g, v, :name, string(v))
end
for v in vertices(g)
println("vertex ", v, " under scrutiny")
if get_prop(g, v, :name) == "1"
rem_vertex!(g, v)
println("vertex ", v, " removed")
end
end
map(v -> (v, get_prop(g, v, :name)), filter_vertices(g, :name))
end
vertex 1 under scrutiny
vertex 1 removed
vertex 2 under scrutiny
vertex 3 under scrutiny
vertex 4 under scrutiny
vertex 5 under scrutiny
ERROR: KeyError: key :name not found
Stacktrace:
[1] getindex at ./dict.jl:474 [inlined]
[2] get_prop(::MetaGraphs.MetaGraph{Int64,Float64}, ::Int64, ::Symbol) at /home/yves/.julia/v0.6/MetaGraphs/src/MetaGraphs.jl:142
[3] anonymous at ./<missing>:7
It seems that it is an error to erase a non-existent vertex. In LightGraphs, rem_vertex!
does not return an error if the vertex is not there but instead returns false.
let g = Graph(5), labels = Dict()
for v in vertices(g)
labels[v] = string(v)
end
for v in vertices(g)
println("vertex ", v, " under scrutiny")
if labels[v] == "1"
rem_vertex!(g, v)
labels[v] = labels[end]
println("vertex ", v, " removed")
end
end
map(v -> (v, labels[v]) , vertices(g))
end
vertex 1 under scrutiny
vertex 1 removed
vertex 2 under scrutiny
vertex 3 under scrutiny
vertex 4 under scrutiny
vertex 5 under scrutiny
4-element Array{Tuple{Int64,String},1}:
(1, "5")
(2, "2")
(3, "3")
(4, "4")
Finally note that rem_vertex!
does not play well with iterators such as filter_vertices
.
from metagraphs.jl.
Note that the code in my first post in this thread now returns an error.
...
Finally note that rem_vertex! does not play well with iterators such as filter_vertices.
You cannot iterate over vertices (or edges, or neighbors, or...) while modifying the graph in a loop. This is true for all graph types since we make no guarantees as to how the modification is performed. Any modification of the graph invalidates the loop variable.
One solution is to use an iterator to gather a list of vertices you want to keep (or remove), and then use induced_subgraph
to create a new graph with the vertices you want to keep. induced_subgraph
will return a vector that maps the new vertices to their original ones:
julia> mga = MetaGraph(Graph(5))
{5, 0} undirected Int64 metagraph with Float64 weights defined by :weight (default weight 1.0)
julia> for v in vertices(mga)
set_prop!(mga, v, :name, string(v))
end
julia> z = induced_subgraph(mga, [1,3,5])
({3, 0} undirected Int64 metagraph with Float64 weights defined by :weight (default weight 1.0), [1, 3, 5])
julia> g = z[1]
{3, 0} undirected Int64 metagraph with Float64 weights defined by :weight (default weight 1.0)
julia> props(g, 1)
Dict{Symbol,Any} with 1 entry:
:name => "1"
julia> props(g, 2)
Dict{Symbol,Any} with 1 entry:
:name => "3"
julia> props(g, 3)
Dict{Symbol,Any} with 1 entry:
:name => "5"
It seems that it is an error to erase a non-existent vertex.
This is, strictly speaking, not true:
julia> mga = MetaGraph(Graph(5))
{5, 0} undirected Int64 metagraph with Float64 weights defined by :weight (default weight 1.0)
julia> for v in vertices(mga)
set_prop!(mga, v, :name, string(v))
end
julia> rem_vertex!(mga,1)
true
julia> rem_vertex!(mga,6)
false
from metagraphs.jl.
Related Issues (20)
- How to update vertex / node color in a loop HOT 1
- Register 0.6.4 HOT 1
- Register v0.6.5 HOT 7
- Multi-threading with MetaGraphs HOT 1
- Method to check whether an index exists (`has_indexing_prop`)
- Error : cannot assign a value to variable LightGraphs.Graph from module Main HOT 2
- Use getproperty() to access a meta graph vertex via a label index HOT 5
- feature request: need `squash()` for MetaGraph and MetaDiGraph HOT 1
- weight matrix error in the function `dijkstra_shortest_paths()`
- Feature Request: Extend `rem_vertices!` to MetaGraphs
- Roadmap to 1.0? HOT 3
- Property of edge lost when going from MetaDiGraph with inversed edge direction to MetaGraph HOT 2
- JLD2 compat for v0.3 HOT 1
- Directed Graphs HOT 2
- set_prop! returns Set{Symbol} if an indexing property is being set
- Register v0.6.7 HOT 5
- Disallow construction of `MetaGraph` from directed graph? HOT 3
- Metaweight filters for shortest path routing on attributes
- `rem_vertices!` missing
- `add_vertex!` fails if setting multiple indexing properties using a `Dict`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from metagraphs.jl.