GraphNeuralNetworks.jl icon indicating copy to clipboard operation
GraphNeuralNetworks.jl copied to clipboard

convenience feature setter

Open CarloLucibello opened this issue 1 year ago • 1 comments

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

CarloLucibello avatar May 22 '23 04:05 CarloLucibello

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

rbSparky avatar Feb 18 '24 23:02 rbSparky