Skip to content

fix: support Colon() in middle of indices for NodeLabel (#275)#303

Open
ArpanC6 wants to merge 3 commits into
ReactiveBayes:mainfrom
ArpanC6:main
Open

fix: support Colon() in middle of indices for NodeLabel (#275)#303
ArpanC6 wants to merge 3 commits into
ReactiveBayes:mainfrom
ArpanC6:main

Conversation

@ArpanC6
Copy link
Copy Markdown

@ArpanC6 ArpanC6 commented May 19, 2026

Fixes #275
Fixes #293

While working with multi-dimensional latent variables in a model I noticed that using a colon : in the middle of an index expression like media[g, :, i] causes a crash with a MethodError. For example, this model

@model function my_model(y)
    media ~ Normal(zeros(2, 5, 3), ones(2, 5, 3))
    for g in 1:2
        for i in 1:3
            media_effect[g, i] := media[g, :, i]
        end
    end
    y ~ Normal(0, 1)
end

would throw

MethodError: no method matching getindex(::GraphPPL.NodeLabel, ::Int64, ::Colon, ::Int64)

The issue is that NodeLabel only had a single-argument getindex overload:

Base.getindex(label::NodeLabel, any) = label

When Julia sees media[g, :, i] on a NodeLabel, it tries to call getindex with three arguments, which didn't have a matching method. Since NodeLabel represents a node in the factor graph and not an actual array the right behaviour is to just return the label itself regardless of the indices which is exactly what the existing single index overload already does.

The fix is a one-line addition:

Base.getindex(label::NodeLabel, i1, is...) = label

I also added a test that covers this case to make sure it doesn't regress.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Possibly can improve constraint set error Colon is not working if in the middle of other indices

1 participant