From 01b3d0df9dd92063c7de33b7ceab432376de8a94 Mon Sep 17 00:00:00 2001 From: nHackel Date: Thu, 11 Jul 2024 16:33:11 +0200 Subject: [PATCH 1/8] Improve reco package plan handling --- src/RecoPlans/Serialization.jl | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/RecoPlans/Serialization.jl b/src/RecoPlans/Serialization.jl index fb2beaf..d2271c1 100644 --- a/src/RecoPlans/Serialization.jl +++ b/src/RecoPlans/Serialization.jl @@ -6,7 +6,13 @@ function plandir(m::Module) return @get_scratch!(string(m)) end end -planpath(m::Module, name::AbstractString) = joinpath(plandir(m), string(name, ".toml")) +function planpath(m::Module, name::AbstractString) + if m != AbstractImageReconstruction && hasproperty(m, :planpath) + return getproperty(m, :planpath)(name) + else + return joinpath(plandir(m), string(name, ".toml")) + end +end export savePlan savePlan(filename::AbstractString, plan::RecoPlan) = toTOML(filename, plan) @@ -54,7 +60,7 @@ function createModuleDataTypeDict(modules::Vector{Module}) try t = getfield(mod, field) if t isa DataType || t isa UnionAll || t isa Function - typeDict[string(t)] = t + typeDict[string(field)] = t end catch end @@ -93,6 +99,9 @@ function loadPlan!(plan::RecoPlan{T}, dict::Dict{String, Any}, modDict) where {T if t <: AbstractImageReconstructionAlgorithm || t <: AbstractImageReconstructionParameters param = loadPlan!(dict[key], modDict) parent!(param, plan) + + if param isa RecoPlan{ProcessResultCache} + end else param = loadPlanValue(T, name, t, dict[key], modDict) end From b93391e066ab6865f2e6a7b55d68477b9dc11447 Mon Sep 17 00:00:00 2001 From: nHackel Date: Thu, 11 Jul 2024 16:33:39 +0200 Subject: [PATCH 2/8] Improve cache ambig. related method errors --- src/RecoPlans/Cache.jl | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/RecoPlans/Cache.jl b/src/RecoPlans/Cache.jl index 4cd8cd0..d940aef 100644 --- a/src/RecoPlans/Cache.jl +++ b/src/RecoPlans/Cache.jl @@ -4,7 +4,10 @@ Base.@kwdef mutable struct ProcessResultCache{P <: AbstractImageReconstructionPa const maxsize::Int64 = 1 cache::LRU{UInt64, Any} = LRU{UInt64, Any}(maxsize = maxsize) end -function process(algo::Union{A, Type{<:A}}, param::ProcessResultCache, inputs...) where {A<:AbstractImageReconstructionAlgorithm} +process(algo::A, param::ProcessResultCache, inputs...) where {A <: AbstractImageReconstructionAlgorithm} = hashed_process(algo, param, inputs...) +process(algoT::Type{<:A}, param::ProcessResultCache, inputs...) where {A <: AbstractImageReconstructionAlgorithm} = hashed_process(algoT, param, inputs...) + +function hashed_process(algo, param::ProcessResultCache, inputs...) id = hash(param.param, hash(inputs, hash(algo))) result = get!(param.cache, id) do process(algo, param.param, inputs...) @@ -28,9 +31,14 @@ function clear!(plan::RecoPlan{<:ProcessResultCache}, preserve::Bool = true) return plan end -function validvalue(plan, ::Type{T}, value::RecoPlan{<:ProcessResultCache}) where T +function validvalue(plan, union::Type{Union{T, ProcessResultCache{<:T}}}, value::RecoPlan{ProcessResultCache}) where T + innertype = value.param isa RecoPlan ? typeof(value.param).parameters[1] : typeof(value.param) + return ProcessResultCache{<:innertype} <: union +end + +function validvalue(plan, union::UnionAll, value::RecoPlan{ProcessResultCache}) innertype = value.param isa RecoPlan ? typeof(value.param).parameters[1] : typeof(value.param) - return ProcessResultCache{<:innertype} <: T + return ProcessResultCache{<:innertype} <: union end # Do not serialize cache and lock, only param From 9aa3b5b922cb653ef43a90651c3aff04ff6d745c Mon Sep 17 00:00:00 2001 From: nHackel Date: Thu, 11 Jul 2024 16:33:52 +0200 Subject: [PATCH 3/8] Prepare patch release 0.3.3 --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 70a5f85..fa87629 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "AbstractImageReconstruction" uuid = "a4b4fdbf-6459-4ec9-990d-77e1fa24a91b" authors = ["nHackel and contributors"] -version = "0.3.2" +version = "0.3.3" [deps] LRUCache = "8ac3fa9e-de4c-5943-b1dc-09c6b5f20637" From 65ffa5380c098f751e0ea314e626091bacfc7269 Mon Sep 17 00:00:00 2001 From: nHackel Date: Mon, 29 Jul 2024 16:49:42 +0200 Subject: [PATCH 4/8] Add conv. constructor for ProcessResultCache --- src/RecoPlans/Cache.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/RecoPlans/Cache.jl b/src/RecoPlans/Cache.jl index d940aef..0134588 100644 --- a/src/RecoPlans/Cache.jl +++ b/src/RecoPlans/Cache.jl @@ -4,6 +4,7 @@ Base.@kwdef mutable struct ProcessResultCache{P <: AbstractImageReconstructionPa const maxsize::Int64 = 1 cache::LRU{UInt64, Any} = LRU{UInt64, Any}(maxsize = maxsize) end +ProcessResultCache(param::AbstractImageReconstructionParameters; kwargs...) = ProcessResultCache(;param, kwargs...) process(algo::A, param::ProcessResultCache, inputs...) where {A <: AbstractImageReconstructionAlgorithm} = hashed_process(algo, param, inputs...) process(algoT::Type{<:A}, param::ProcessResultCache, inputs...) where {A <: AbstractImageReconstructionAlgorithm} = hashed_process(algoT, param, inputs...) From 2c6fa81463a82c6d4b7b617d1d56c7e22e1ac17d Mon Sep 17 00:00:00 2001 From: nHackel Date: Mon, 29 Jul 2024 16:50:29 +0200 Subject: [PATCH 5/8] Fix race condition in process result cache --- src/RecoPlans/Cache.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/src/RecoPlans/Cache.jl b/src/RecoPlans/Cache.jl index 0134588..244e1ae 100644 --- a/src/RecoPlans/Cache.jl +++ b/src/RecoPlans/Cache.jl @@ -13,7 +13,6 @@ function hashed_process(algo, param::ProcessResultCache, inputs...) result = get!(param.cache, id) do process(algo, param.param, inputs...) end - param.cache[id] = result return result end From a661cc881c1ad53c351193fdf74bc52ebe3eb827 Mon Sep 17 00:00:00 2001 From: Niklas Hackelberg Date: Tue, 30 Jul 2024 15:39:53 +0200 Subject: [PATCH 6/8] Release 0.3.4 --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index fa87629..902051f 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "AbstractImageReconstruction" uuid = "a4b4fdbf-6459-4ec9-990d-77e1fa24a91b" authors = ["nHackel and contributors"] -version = "0.3.3" +version = "0.3.4" [deps] LRUCache = "8ac3fa9e-de4c-5943-b1dc-09c6b5f20637" From 1144aef484296ca981f4ba4c4b857e187b060ef7 Mon Sep 17 00:00:00 2001 From: nHackel Date: Thu, 1 Aug 2024 13:30:50 +0200 Subject: [PATCH 7/8] Bump version to 0.3.4 and improve plan handling for vectors of parameters --- Project.toml | 2 +- src/RecoPlans/RecoPlans.jl | 7 +++++-- src/RecoPlans/Serialization.jl | 6 +++--- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/Project.toml b/Project.toml index fa87629..902051f 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "AbstractImageReconstruction" uuid = "a4b4fdbf-6459-4ec9-990d-77e1fa24a91b" authors = ["nHackel and contributors"] -version = "0.3.3" +version = "0.3.4" [deps] LRUCache = "8ac3fa9e-de4c-5943-b1dc-09c6b5f20637" diff --git a/src/RecoPlans/RecoPlans.jl b/src/RecoPlans/RecoPlans.jl index 9ac29af..b6418eb 100644 --- a/src/RecoPlans/RecoPlans.jl +++ b/src/RecoPlans/RecoPlans.jl @@ -96,6 +96,7 @@ validvalue(plan, ::Type{T}, value::RecoPlan{<:T}) where T = true 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 validvalue(plan, t::Type{Union}, value) = validvalue(plan, t.a, value) || validvalue(plan, t.b, value) validvalue(plan, t, value) = false +validvalue(plan, ::Type{arrT}, value::AbstractArray) where {T, arrT <: AbstractArray{T}} = all(x -> validvalue(plan, T, x), value) #X <: t || X <: RecoPlan{<:t} || ismissing(x) @@ -111,7 +112,7 @@ end export setAll! function setAll!(plan::RecoPlan{T}, name::Symbol, x) where {T<:AbstractImageReconstructionParameters} fields = getfield(plan, :values) - nestedPlans = filter(entry -> isa(last(entry), RecoPlan), fields) + nestedPlans = filter(entry -> isa(last(entry), RecoPlan) || isa(last(entry), AbstractArray{<:RecoPlan}), fields) for (key, nested) in nestedPlans key != name && setAll!(nested, name, x) end @@ -123,6 +124,7 @@ function setAll!(plan::RecoPlan{T}, name::Symbol, x) where {T<:AbstractImageReco end end end +setAll!(plans::AbstractArray{<:RecoPlan}, name::Symbol, x) = foreach(p -> setAll!(p, name, x), plans) setAll!(plan::RecoPlan{<:AbstractImageReconstructionAlgorithm}, name::Symbol, x) = setAll!(plan.parameter, name, x) function setAll!(plan; kwargs...) for key in keys(kwargs) @@ -181,13 +183,14 @@ Base.ismissing(plan::RecoPlan, name::Symbol) = ismissing(getfield(plan, :values) export build function build(plan::RecoPlan{T}) where {T<:AbstractImageReconstructionParameters} fields = copy(getfield(plan, :values)) - nestedPlans = filter(entry -> isa(last(entry), RecoPlan), fields) + nestedPlans = filter(entry -> isa(last(entry), RecoPlan) || isa(last(entry), AbstractArray{<:RecoPlan}), fields) for (name, nested) in nestedPlans fields[name] = build(nested) end fields = filter(entry -> !ismissing(last(entry)), fields) return T(;fields...) end +build(plans::AbstractArray{<:RecoPlan}) = map(build, plans) function build(plan::RecoPlan{T}) where {T<:AbstractImageReconstructionAlgorithm} parameter = build(plan[:parameter]) return T(parameter) diff --git a/src/RecoPlans/Serialization.jl b/src/RecoPlans/Serialization.jl index d2271c1..a0cf71a 100644 --- a/src/RecoPlans/Serialization.jl +++ b/src/RecoPlans/Serialization.jl @@ -99,9 +99,9 @@ function loadPlan!(plan::RecoPlan{T}, dict::Dict{String, Any}, modDict) where {T if t <: AbstractImageReconstructionAlgorithm || t <: AbstractImageReconstructionParameters param = loadPlan!(dict[key], modDict) parent!(param, plan) - - if param isa RecoPlan{ProcessResultCache} - end + elseif t <: Vector{<:AbstractImageReconstructionAlgorithm} || t <: Vector{<:AbstractImageReconstructionParameters} + param = map(x-> loadPlan!(x, modDict), dict[key]) + foreach(p -> parent!(p, plan), param) else param = loadPlanValue(T, name, t, dict[key], modDict) end From 6ede17c160972e1a57a7456b80a716f1f0896ef7 Mon Sep 17 00:00:00 2001 From: Niklas Hackelberg Date: Thu, 1 Aug 2024 16:37:56 +0200 Subject: [PATCH 8/8] Update Project.toml --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 902051f..f863cef 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "AbstractImageReconstruction" uuid = "a4b4fdbf-6459-4ec9-990d-77e1fa24a91b" authors = ["nHackel and contributors"] -version = "0.3.4" +version = "0.3.5" [deps] LRUCache = "8ac3fa9e-de4c-5943-b1dc-09c6b5f20637"