Skip to content

Commit 10baea1

Browse files
committed
Init AbstractRecoPlan type hierarchy
1 parent 16bddfb commit 10baea1

File tree

2 files changed

+20
-19
lines changed

2 files changed

+20
-19
lines changed

src/RecoPlans/RecoPlans.jl

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
export RecoPlan
1+
export AbstractRecoPlan, RecoPlan
2+
abstract type AbstractRecoPlan{T<:Union{AbstractImageReconstructionParameters, AbstractImageReconstructionAlgorithm}} end
23
"""
34
RecoPlan{T <: Union{AbstractImageReconstructionParameters, AbstractImageReconstructionAlgorithm}}
45
@@ -9,8 +10,8 @@ Plans can be nested and form a tree. A parent plan can be accessed with `parent`
910
1011
Plans feature serialization with `toTOML`, `toPlan` and `loadPlan` and the ability to attach callbacks to property changes with `Òbservables` and `on`.
1112
"""
12-
mutable struct RecoPlan{T<:Union{AbstractImageReconstructionParameters, AbstractImageReconstructionAlgorithm}}
13-
parent::Union{Nothing, RecoPlan}
13+
mutable struct RecoPlan{T} <: AbstractRecoPlan{T}
14+
parent::Union{Nothing, AbstractRecoPlan}
1415
values::Dict{Symbol, Observable{Any}}
1516
"""
1617
RecoPlan(::Type{T}; kwargs...) where {T<:AbstractImageReconstructionParameters}
@@ -60,17 +61,17 @@ Return the `Observable` for the `name` property of `plan`. Equivalent to `plan[n
6061
Base.getindex(plan::RecoPlan{T}, name::Symbol) where {T} = getfield(plan, :values)[name]
6162

6263
export types, type
63-
types(::RecoPlan{T}) where {T<:AbstractImageReconstructionParameters} = fieldtypes(T)
64-
type(::RecoPlan{T}, name::Symbol) where {T<:AbstractImageReconstructionParameters} = fieldtype(T, name)
64+
types(::AbstractRecoPlan{T}) where {T<:AbstractImageReconstructionParameters} = fieldtypes(T)
65+
type(::AbstractRecoPlan{T}, name::Symbol) where {T<:AbstractImageReconstructionParameters} = fieldtype(T, name)
6566

66-
function type(plan::RecoPlan{T}, name::Symbol) where {T<:AbstractImageReconstructionAlgorithm}
67+
function type(plan::AbstractRecoPlan{T}, name::Symbol) where {T<:AbstractImageReconstructionAlgorithm}
6768
if name == :parameter
6869
return RecoPlan
6970
else
7071
error("type $(typeof(plan)) has no field $name")
7172
end
7273
end
73-
types(::RecoPlan{T}) where {T<:AbstractImageReconstructionAlgorithm} = [type(plan, name) for name in propertynames(plan)]
74+
types(::AbstractRecoPlan{T}) where {T<:AbstractImageReconstructionAlgorithm} = [type(plan, name) for name in propertynames(plan)]
7475

7576
"""
7677
setproperty!(plan::RecoPlan{T}, name::Symbol, x::X) where {T, X}
@@ -97,9 +98,9 @@ function Base.setproperty!(plan::RecoPlan{T}, name::Symbol, x::X) where {T, X}
9798
end
9899
validvalue(plan, t, value::Missing) = true
99100
validvalue(plan, ::Type{T}, value::X) where {T, X <: T} = true
100-
validvalue(plan, ::Type{T}, value::RecoPlan{<:T}) where T = true
101+
validvalue(plan, ::Type{T}, value::AbstractRecoPlan{<:T}) where T = true
101102
# RecoPlans are stripped of parameters
102-
validvalue(plan, t::UnionAll, ::RecoPlan{T}) where T = T <: t || T <: Base.typename(t).wrapper # Last case doesnt work for Union{...} that is a UnionAll, such as ProcessCache Unio
103+
validvalue(plan, t::UnionAll, ::AbstractRecoPlan{T}) where T = T <: t || T <: Base.typename(t).wrapper # Last case doesnt work for Union{...} that is a UnionAll, such as ProcessCache Unio
103104
validvalue(plan, t::Type{Union}, value) = validvalue(plan, t.a, value) || validvalue(plan, t.b, value)
104105
validvalue(plan, t, value) = false
105106
validvalue(plan, ::Type{arrT}, value::AbstractArray) where {T, arrT <: AbstractArray{T}} = all(x -> validvalue(plan, T, x), value)
@@ -136,15 +137,15 @@ function setAll!(plan::RecoPlan{T}, name::Symbol, x) where {T<:AbstractImageReco
136137
end
137138
end
138139
end
139-
setAll!(plans::AbstractArray{<:RecoPlan}, name::Symbol, x) = foreach(p -> setAll!(p, name, x), plans)
140+
setAll!(plans::AbstractArray{<:AbstractRecoPlan}, name::Symbol, x) = foreach(p -> setAll!(p, name, x), plans)
140141
setAll!(plan::RecoPlan{<:AbstractImageReconstructionAlgorithm}, name::Symbol, x) = setAll!(plan.parameter, name, x)
141-
function setAll!(plan; kwargs...)
142+
function setAll!(plan::AbstractRecoPlan; kwargs...)
142143
for key in keys(kwargs)
143144
setAll!(plan, key, kwargs[key])
144145
end
145146
end
146-
setAll!(plan::RecoPlan, dict::Dict{Symbol, Any}) = setAll!(plan; dict...)
147-
setAll!(plan::RecoPlan, dict::Dict{String, Any}) = setAll!(plan, Dict{Symbol, Any}(Symbol(k) => v for (k,v) in dict))
147+
setAll!(plan::AbstractRecoPlan, dict::Dict{Symbol, Any}) = setAll!(plan; dict...)
148+
setAll!(plan::AbstractRecoPlan, dict::Dict{String, Any}) = setAll!(plan, Dict{Symbol, Any}(Symbol(k) => v for (k,v) in dict))
148149

149150
export clear!
150151
"""
@@ -186,7 +187,7 @@ parent!(plan::RecoPlan, parent::RecoPlan) = setfield!(plan, :parent, parent)
186187
187188
Return a vector of property names of `plan` in its parent, s.t. `getproperty(parent(plan), last(parentproperties(plan))) === plan`. Return an empty vector if `plan` has no parent.
188189
"""
189-
function parentproperties(plan::RecoPlan)
190+
function parentproperties(plan::AbstractRecoPlan)
190191
trace = Symbol[]
191192
return parentproperties!(trace, plan)
192193
end
@@ -195,7 +196,7 @@ end
195196
196197
Return the property name of `plan` in its parent, s.t. `getproperty(parent(plan), parentproperty(plan)) === plan`. Return `nothing` if `plan` has no parent.
197198
"""
198-
function parentproperty(plan::RecoPlan)
199+
function parentproperty(plan::AbstractRecoPlan)
199200
p = parent(plan)
200201
if !isnothing(p)
201202
for property in propertynames(p)
@@ -206,7 +207,7 @@ function parentproperty(plan::RecoPlan)
206207
end
207208
return nothing
208209
end
209-
function parentproperties!(trace::Vector{Symbol}, plan::RecoPlan)
210+
function parentproperties!(trace::Vector{Symbol}, plan::AbstractRecoPlan)
210211
p = parent(plan)
211212
parentprop = parentproperty(plan)
212213
if !isnothing(p) && !isnothing(parentprop)
@@ -247,7 +248,7 @@ function build(plan::RecoPlan{T}) where {T<:AbstractImageReconstructionParameter
247248
return T(;fields...)
248249
end
249250
build(plans::AbstractArray{<:RecoPlan}) = map(build, plans)
250-
function build(plan::RecoPlan{T}) where {T<:AbstractImageReconstructionAlgorithm}
251+
function build(plan::AbstractRecoPlan{T}) where {T<:AbstractImageReconstructionAlgorithm}
251252
parameter = build(getproperty(plan, :parameter))
252253
return T(parameter)
253254
end
@@ -272,7 +273,7 @@ function toPlan(param::AbstractImageReconstructionParameters)
272273
setAll!(plan; args...)
273274
return plan
274275
end
275-
function toPlan(parent::RecoPlan, x)
276+
function toPlan(parent::AbstractRecoPlan, x)
276277
plan = toPlan(x)
277278
parent!(plan, parent)
278279
return plan

src/RecoPlans/Show.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ function Base.show(io::IO, plan::RecoPlan{T}) where {T <: AbstractImageReconstru
66
print(io, "RecoPlan{$T}($(join(propertynames(plan), ", ")))")
77
end
88

9-
function Base.show(io::IO, ::MIME"text/plain", plan::RecoPlan{T}) where {T}
9+
function Base.show(io::IO, ::MIME"text/plain", plan::AbstractRecoPlan{T}) where {T}
1010
if get(io, :compact, false)
1111
show(io, plan)
1212
else

0 commit comments

Comments
 (0)