CarloLucibello / GraphNeuralNetworks.jl

Graph Neural Networks in Julia
https://carlolucibello.github.io/GraphNeuralNetworks.jl/dev/
MIT License
214 stars 47 forks source link

convenience feature setter #284

Closed CarloLucibello closed 2 weeks ago

CarloLucibello commented 1 year ago

Right now we can conveniently add new features with

julia> g = rand_graph(3, 2)
GNNGraph:
  num_nodes: 3
  num_edges: 2

julia> g.ndata.x = rand(1, 3)
1×3 Matrix{Float64}:
 0.31553  0.824744  0.86907

Shoul we also allow the following (currently erroring)?

julia> g.x = rand(1, 3)
ERROR: type GNNGraph has no field x
Stacktrace:
 [1] setproperty!(x::GNNGraph{Tuple{Vector{Int64}, Vector{Int64}, Nothing}}, f::Symbol, v::Matrix{Float64})
   @ Base ./Base.jl:38
 [2] top-level scope
   @ REPL[9]:1
rbSparky commented 6 months ago

Would this include editing the Base.setproperty! function in datastore.jl to handle this specific case?

function Base.setproperty!(ds::DataStore, s::Symbol, x)
    @assert s != :_n "cannot set _n directly"
    @assert s != :_data "cannot set _data directly"

    # changes

    if getn(ds) >= 0
        numobs(x) == getn(ds) || throw(DimensionMismatch("expected $(getn(ds)) object features but got $(numobs(x))."))
    end
    return getdata(ds)[s] = x
end
CarloLucibello commented 2 weeks ago

This would involve implementing setproperty!(g::GNNGraph, ...). I think it is better to not proceed with this though, g.ndata.x = ... is much more transparent and convenient enough.