Skip to content

Implementes structured update #304

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/ComponentArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export AbstractAxis, Axis, PartitionedAxis, ShapedAxis, Shaped1DAxis, ViewAxis,

include("componentarray.jl")
export ComponentArray, ComponentVector, ComponentMatrix, getaxes, getdata, valkeys
export update_component_array

include("componentindex.jl")
export KeepIndex
Expand Down
45 changes: 45 additions & 0 deletions src/componentarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -360,3 +360,48 @@ julia> sum(prod(ca[k]) for k in valkeys(ca))
return :($k)
end
valkeys(ca::ComponentVector) = valkeys(getaxes(ca)[1])

"""
update_component_array(default::ComponentArray{T}, update::ComponentArray) where {T}

Update a ComponentArray with values from another ComponentArray while preserving structure and type stability.
Only matching entries are updated, allowing for partial updates of nested structures.

# Arguments
- `default::ComponentArray{T}`: The ComponentArray to update, serving as both template and target
- `update::ComponentArray`: The ComponentArray containing the new values

# Returns
- A new ComponentArray with the same type and structure as `default`, updated with values from `update`

# Example
```julia
default = ComponentArray(sig = (mu = 1.0, sigma = 2.0), bg = 3.0)
update = ComponentArray(sig = (mu = 1.1,), bg = 3.3)
result = update_component_array(default, update)
# result.sig.mu == 1.1
# result.sig.sigma == 2.0
# result.bg == 3.3
```
"""
function update_component_array(default::ComponentArray{T}, update::ComponentArray) where {T}
result = copy(default)

# Update matching fields using ComponentArray's natural indexing
for key in propertynames(update)
if hasproperty(default, key)
update_val = update[key]
default_val = default[key]

if default_val isa ComponentArray && update_val isa ComponentArray
# Recursively update nested ComponentArrays
result[key] = update_component_array(default_val, update_val)
else
# Direct update for leaf values
result[key] = update_val
end
end
end

return result
end
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -942,3 +942,7 @@ end
@testset "Reactant" begin
include("reactant_tests.jl")
end

@testset "ComponentArrays" begin
include("update_tests.jl")
end
54 changes: 54 additions & 0 deletions test/update_tests.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using ComponentArrays
using Test

@testset "ComponentArray Update" begin
# Test 1: Basic update
default = ComponentArray(sig = (mu = 1.0, sigma = 2.0), bg = 3.0)
update = ComponentArray(sig = (mu = 1.1,), bg = 3.3)

result = update_component_array(default, update)

@test result.sig.mu ≈ 1.1
@test result.sig.sigma ≈ 2.0
@test result.bg ≈ 3.3

# Test 2: Structure preservation
@test typeof(result) == typeof(default)
@test getaxes(result) == getaxes(default)

# Test 3: Deep nested update
deep_default = ComponentArray(
outer = (
inner = (x = 1.0, y = 2.0),
other = 3.0,
),
top = 4.0,
)

deep_update = ComponentArray(
outer = (
inner = (x = 10.0,),
),
)

deep_result = update_component_array(deep_default, deep_update)

@test deep_result.outer.inner.x ≈ 10.0
@test deep_result.outer.inner.y ≈ 2.0
@test deep_result.outer.other ≈ 3.0
@test deep_result.top ≈ 4.0

# Test 4: Non-existing field update
nonexist_update = ComponentArray(
sig = (mu = 1.1,),
nonexistent = 42.0, # This field doesn't exist in default
)
result_nonexist = update_component_array(default, nonexist_update)

# Should preserve structure and only update existing fields
@test result_nonexist.sig.mu ≈ 1.1
@test result_nonexist.sig.sigma ≈ 2.0
@test !hasproperty(result_nonexist, :nonexistent)
@test typeof(result_nonexist) == typeof(default)
@test getaxes(result_nonexist) == getaxes(default)
end