Skip to content

Commit 27bc917

Browse files
authored
Merge pull request #19 from JuliaImageRecon/nh/setwarn
Add warning if property could not be found or set in setAll!
2 parents c1bb2a8 + dca2ec2 commit 27bc917

File tree

4 files changed

+40
-41
lines changed

4 files changed

+40
-41
lines changed

src/Progress.jl

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/RecoPlans/RecoPlans.jl

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,21 @@ export setAll!
131131
132132
Recursively set the property `name` of each nested `RecoPlan` of `plan` to `x`.
133133
"""
134-
function setAll!(plan::RecoPlan{T}, name::Symbol, x) where {T<:AbstractImageReconstructionParameters}
134+
function setAll!(plan::AbstractRecoPlan, name::Symbol, x)
135+
set = Ref(false)
136+
found = Ref(false)
137+
setAll!(plan, name, x, set, found)
138+
if !set[]
139+
if !found[]
140+
@warn "Could not set all `$name` properties of plan: Property not found"
141+
else
142+
@warn "Could not set all `$name` properties of plan: Property of type $(typeof(x)) cannot be converted to the correct type"
143+
end
144+
end
145+
return nothing
146+
end
147+
148+
function setAll!(plan::RecoPlan{T}, name::Symbol, x, set, found) where {T<:AbstractImageReconstructionParameters}
135149
fields = getfield(plan, :values)
136150

137151
# Filter out nested plans
@@ -142,21 +156,23 @@ function setAll!(plan::RecoPlan{T}, name::Symbol, x) where {T<:AbstractImageReco
142156

143157
# Recursively call setAll! on nested plans
144158
for (key, nested) in nestedPlans
145-
key != name && setAll!(Observables.to_value(nested), name, x)
159+
key != name && setAll!(Observables.to_value(nested), name, x, set, found)
146160
end
147161

148162
# Set the value of the field
149163
if hasproperty(plan, name)
150164
try
165+
found[] |= true
151166
Base.setproperty!(plan, name, x)
167+
set[] |= true
152168
catch ex
153-
@error ex
154169
@warn "Could not set $name of $T with value of type $(typeof(x))"
155170
end
156171
end
172+
return nothing
157173
end
158-
setAll!(plans::AbstractArray{<:AbstractRecoPlan}, name::Symbol, x) = foreach(p -> setAll!(p, name, x), plans)
159-
setAll!(plan::RecoPlan{<:AbstractImageReconstructionAlgorithm}, name::Symbol, x) = setAll!(plan.parameter, name, x)
174+
setAll!(plans::AbstractArray{<:AbstractRecoPlan}, name::Symbol, args...) = foreach(p -> setAll!(p, name, args...), plans)
175+
setAll!(plan::RecoPlan{<:AbstractImageReconstructionAlgorithm}, name::Symbol, args...) = setAll!(plan.parameter, name, args...)
160176
"""
161177
setAll!(plan::AbstractRecoPlan; kwargs...)
162178

test/reco_plan.jl

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
reco = IterativeRadonReconstructionParameters(; shape = size(images)[1:3], angles = angles, iterations = 1, reg = [L2Regularization(0.001), PositiveRegularization()], solver = CGNR);
44
algo = IterativeRadonAlgorithm(IterativeRadonParameters(pre, reco))
55

6-
6+
77
@testset "Construction" begin
88
# From algorithm
99
plan_fromAlgo = toPlan(algo)
@@ -167,6 +167,23 @@
167167
end
168168

169169
@testset "Traversal" begin
170+
plan = toPlan(algo)
171+
172+
parameter = plan.parameter
173+
@test parameter isa RecoPlan
174+
@test parameter == first(children(plan))
175+
@test plan === AbstractTrees.parent(parameter)
176+
177+
pre_plan = parameter.pre
178+
reco_plan = parameter.reco
179+
param_children = children(parameter)
180+
@test length(param_children) == 2
181+
for child in [pre_plan, reco_plan]
182+
@test child isa RecoPlan
183+
@test parameter == AbstractTrees.parent(child)
184+
@test in(child, param_children)
185+
end
186+
170187
end
171188

172189
end

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using AbstractImageReconstruction
22
using AbstractImageReconstruction.Observables
3+
using AbstractImageReconstruction.AbstractTrees
34
using Test
45
using RegularizedLeastSquares
56

0 commit comments

Comments
 (0)