SciML / SciML/ComponentArrays.jl

Merging of ComponentArrays

Open
#69 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Julia
Stars
375
Forks
42
Avg merge
7h 25m
Merged PRs (30d)
17

Description

Thanks for the really useful package! It has helped a lot to clean up our model code.

One functionality I missed is to merge component arrays. With tuples we can do:

t1 = (a=1, b=2, c=3)
t2 = (a=111, d=444)
merge(t1, t2)            # (a = 111, b = 2, c = 3, d = 444)

I can simulate this behavior, but I think my implementation is not very optimal:

function merge(a::T, b::T) where T <: ComponentVector
    ComponentVector(merge(NamedTuple(a), NamedTuple(b)))
end
function merge(a::ComponentVector, b::NamedTuple) 
    ComponentVector(merge(NamedTuple(a), b))
end
function merge(a::NamedTuple, b::ComponentVector) 
    ComponentVector(merge(a, NamedTuple(b)))
end

ca1 = ComponentVector(a=1, b=2, c=3)
ca2 = ComponentVector(b=22, d=44)

merge(ca1, ca2)     # ComponentVector{Int64}(a = 1, b = 22, c = 3, d = 44)
# also useful to add a parameter
merge(ca1, (;new=222)) # ComponentVector{Int64}(a = 1, b = 2, c = 3, new = 222)

# it works with ForwardDiff but with with Zygote
f(x) = sum(merge2(ca1, x))
f(ca2)
Zygote.gradient(f, ca2)

A good use case would be optimizing some parameters while keeping others fix:

foo(ca) = ca.a + ca.b + ca.c + ca.d
ca_fix = ComponentVector(a=1, b=2)
# optimize only parameter 'c' and 'd'
optim(ca_opt -> foo(merge(ca_fix, ca_opt))
      ...
    )

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the ComponentVector and Julia merge behavior, then trace how ComponentArrays are constructed from NamedTuples. Use the examples in the issue to define coverage for merging two ComponentVectors and combinations with NamedTuples, including the optimization and differentiation cases. Done means the requested merges work without breaking the shown ForwardDiff and Zygote use cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
data
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.