Attributes API Reference
This page documents all vertex, edge, and network attribute functions in NetworkCore.jl.
Vertex Attributes
get_vertex_attribute
NetworkCore.get_vertex_attribute — Function
get_vertex_attribute(net::Network, attr::Symbol) -> DictGet a vertex attribute by name. Returns a Dict mapping vertex IDs to values.
Example
net = load_dataset(:florentine_marriage)
names = get_vertex_attribute(net, :name) # Dict{Int64, Any}: 1 => "Acciaiuoli", ...
names[9] # "Medici"get_vertex_attribute(net::Network, attr::Symbol, v::Integer) -> AnyGet the attribute value for a specific vertex.
get_vertex_attribute(net::Network{T}, attr::Symbol, ::Type{V}) -> Dict{T,V}Get a vertex attribute as a concretely typed Dict{T,V}, converting every value to V in a single pass.
Attribute storage is untyped (Dict{T,Any}), which makes every read in a tight loop type-unstable. This accessor is the fast path for model code: convert once up front, then work with the concretely typed dictionary. Throws if any stored value cannot be converted to V. Returns an empty Dict{T,V} if the attribute does not exist.
See also vertex_attribute_vector for a dense Vector{V} in vertex order.
Example
net = load_dataset(:florentine_marriage)
wealth = get_vertex_attribute(net, :wealth, Float64) # Dict{Int,Float64}
wealth[1] # 10.0 (Acciaiuoli)vertex_attribute_vector
NetworkCore.vertex_attribute_vector — Function
vertex_attribute_vector(net::Network, attr::Symbol, ::Type{V}; default) -> Vector{V}Get a vertex attribute as a dense, concretely typed Vector{V} with one entry per vertex (in vertex order 1:nv(net)), converting every value to V in a single pass.
Like the typed get_vertex_attribute(net, attr, V), this is the fast path for model code that reads attributes in hot loops: the returned vector is concretely typed and indexable by vertex ID. Vertices without a stored value take default; if no default is given and any vertex lacks a value (or the attribute does not exist), an ArgumentError naming the attribute and the vertex is thrown — and, when the attribute does not exist at all, listing the attributes the network does have (so a mistyped name is caught where it is made rather than three calls later).
Example
net = load_dataset(:florentine_marriage)
wealth = vertex_attribute_vector(net, :wealth, Float64) # 16-element Vector{Float64}
set_vertex_attribute!(net, :group, Dict(9 => "Medici"))
group = vertex_attribute_vector(net, :group, String; default="?") # gaps filled with "?"
try vertex_attribute_vector(net, :group, String) catch err; err end # ArgumentError: not set for vertex 1set_vertex_attribute!
NetworkCore.set_vertex_attribute! — Function
set_vertex_attribute!(net::Network, attr::Symbol, values::Dict)Set a vertex attribute from a Dict mapping vertex IDs to values. Every key must be an existing vertex (ArgumentError otherwise, nothing stored).
Example
net = network(3)
set_vertex_attribute!(net, :name, Dict(1 => "Alice", 2 => "Bob"))
get_vertex_attribute(net, :name, 2) # "Bob"set_vertex_attribute!(net::Network, attr::Symbol, v::Integer, value)Set the attribute value for a specific vertex. Throws an ArgumentError when v is not a vertex of the network (1 <= v <= nv(net)), as R's set.vertex.attribute does for an out-of-range ID, instead of storing a value nobody can reach by a legal vertex ID.
Example
net = network(3)
set_vertex_attribute!(net, :name, 1, "Alice")
get_vertex_attribute(net, :name, 1) # "Alice"
try set_vertex_attribute!(net, :name, 9, "Zed") catch err; err end # ArgumentError: vertex 9 does not existset_vertex_attribute!(net::Network, attr::Symbol, values::Vector)Set a vertex attribute from a Vector (assumes values are in vertex order 1:n).
delete_vertex_attribute!
NetworkCore.delete_vertex_attribute! — Function
delete_vertex_attribute!(net::Network, attr::Symbol)Delete a vertex attribute (every vertex's value). A no-op when the attribute does not exist.
Example
net = network(3)
set_vertex_attribute!(net, :name, ["a", "b", "c"])
delete_vertex_attribute!(net, :name)
list_vertex_attributes(net) # Symbol[]list_vertex_attributes
NetworkCore.list_vertex_attributes — Function
list_vertex_attributes(net::Network) -> Vector{Symbol}List all vertex attribute names (unsorted).
Example
net = network(3)
set_vertex_attribute!(net, :age, [30, 40, 50])
set_vertex_attribute!(net, :name, ["a", "b", "c"])
sort(list_vertex_attributes(net)) # [:age, :name]Edge Attributes
get_edge_attribute
NetworkCore.get_edge_attribute — Function
get_edge_attribute(net::Network, attr::Symbol) -> DictGet an edge attribute by name. Returns a Dict mapping (source, target) tuples to values.
get_edge_attribute(net::Network, attr::Symbol, i::Integer, j::Integer) -> AnyGet the attribute value for a specific edge.
get_edge_attribute(net::Network{T}, attr::Symbol, ::Type{V}) -> Dict{Tuple{T,T},V}Get an edge attribute as a concretely typed Dict{Tuple{T,T},V}, converting every value to V in a single pass. Keys are canonical edges ((i,j) as stored; minmax(i,j) for undirected networks).
This is the fast path for model code reading edge attributes in hot loops; see the typed get_vertex_attribute(net, attr, V) for rationale. Throws if any stored value cannot be converted to V. Returns an empty dict if the attribute does not exist.
Example
net = network(3)
add_edges!(net, [(1, 2), (2, 3)])
set_edge_attribute!(net, :weight, Dict((1, 2) => 1, (2, 3) => 2.5))
weights = get_edge_attribute(net, :weight, Float64) # Dict{Tuple{Int,Int},Float64}
weights[(1, 2)] # 1.0set_edge_attribute!
NetworkCore.set_edge_attribute! — Function
set_edge_attribute!(net::Network, attr::Symbol, values::Dict; require_edge::Bool=true)Set an edge attribute from a Dict mapping (source, target) tuples to values.
Validates exactly as the single-edge form does, and all-or-nothing: every key is checked before anything is written. Both endpoints of every key must be vertices of the network, and — by default — every key must be an existing edge; an ArgumentError names the first offending key otherwise and the attribute is left untouched. Pass require_edge=false to store values ahead of the edges deliberately (the same opt-out as the single-edge form).
Example
net = network(3)
add_edges!(net, [(1, 2), (2, 3)])
set_edge_attribute!(net, :weight, Dict((1,2) => 1.5, (2,3) => 2.0))
get_edge_attribute(net, :weight, 2, 3) # 2.0
try set_edge_attribute!(net, :w, Dict((1,2) => 1.0, (1,3) => 2.0)) catch err; err end # ArgumentError: (1, 3) is not an edge
:w in list_edge_attributes(net) # false — nothing was stored
set_edge_attribute!(net, :w, Dict((1,3) => 2.0); require_edge=false) # stored ahead of the edgeset_edge_attribute!(net::Network, attr::Symbol, i::Integer, j::Integer, value;
require_edge::Bool=true)Set the attribute value for a specific edge.
Both endpoints must be vertices of the network, and — by default — (i, j) must be an existing edge: an ArgumentError is thrown otherwise, matching R's set.edge.attribute, which can only address an edge that exists. Storing a value on a dyad that carries no tie is almost always a typo'd endpoint or a forgotten add_edge!, and the value would otherwise sit unreachable until an edge happened to appear there. Pass require_edge=false to store the value ahead of the edge deliberately (the attribute is then reported by get_edge_attribute(net, attr) and applies as soon as the edge is added).
Example
net = network(3)
add_edge!(net, 1, 2)
set_edge_attribute!(net, :weight, 1, 2, 2.5)
get_edge_attribute(net, :weight, 1, 2) # 2.5
try set_edge_attribute!(net, :weight, 2, 3, 1.0) catch err; err end # ArgumentError: (2, 3) is not an edge
set_edge_attribute!(net, :weight, 2, 3, 1.0; require_edge=false) # stored aheaddelete_edge_attribute!
NetworkCore.delete_edge_attribute! — Function
delete_edge_attribute!(net::Network, attr::Symbol)Delete an edge attribute (every edge's value). A no-op when the attribute does not exist.
Example
net = network(3)
add_edge!(net, 1, 2)
set_edge_attribute!(net, :weight, 1, 2, 2.5)
delete_edge_attribute!(net, :weight)
get_edge_attribute(net, :weight, 1, 2) === nothing # truelist_edge_attributes
NetworkCore.list_edge_attributes — Function
list_edge_attributes(net::Network) -> Vector{Symbol}List all edge attribute names (unsorted).
Example
net = network(3)
add_edge!(net, 1, 2)
set_edge_attribute!(net, :weight, 1, 2, 2.5)
list_edge_attributes(net) # [:weight]Network Attributes
get_network_attribute
NetworkCore.get_network_attribute — Function
get_network_attribute(net::Network, attr::Symbol) -> AnyGet a network-level attribute by name; nothing when it is not set. The Julia counterpart of R's net %n% "attr".
Example
net = network(3)
set_network_attribute!(net, :title, "Advice network")
get_network_attribute(net, :title) # "Advice network"
get_network_attribute(net, :year) # nothingset_network_attribute!
NetworkCore.set_network_attribute! — Function
set_network_attribute!(net::Network, attr::Symbol, value)Set a network-level attribute.
Example
net = network(3)
set_network_attribute!(net, :title, "Friendship Network")
get_network_attribute(net, :title) # "Friendship Network"delete_network_attribute!
NetworkCore.delete_network_attribute! — Function
delete_network_attribute!(net::Network, attr::Symbol)Delete a network-level attribute. A no-op when the attribute does not exist.
Example
net = network(3)
set_network_attribute!(net, :title, "Advice network")
delete_network_attribute!(net, :title)
get_network_attribute(net, :title) # nothinglist_network_attributes
NetworkCore.list_network_attributes — Function
list_network_attributes(net::Network) -> Vector{Symbol}List all network-level attribute names (unsorted).
Example
net = network(3)
set_network_attribute!(net, :title, "Advice network")
list_network_attributes(net) # [:title]