Skip to content

Commit c65ce76

Browse files
committed
Add abstract type for utility process parameter
1 parent 10baea1 commit c65ce76

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

docs/src/literate/howto/caching.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ include("../../literate/example/example_include_all.jl") #hide
1111

1212
# ## ProcessResultCache
1313
# The caching mechanism is based on the `ProcessResultCache` type. This type wraps around a different `ÀbstractImageReconstructionParameter` and caches the result of a processing step.
14+
# Such a `process`ing step which offer functionality to other `process` steps is a `AbstractUtilityReconstructionParameter`. These utility steps should return the same result as if the inner step was called directly.
15+
1416
# The cache itself is connected to a `RecoPlan` and any instances build from the same plan instance share this cache and can reuse the result of the processing step.
1517

1618
# Let's implement the `ProcessResultCache` type for the Radon preprocessing step. We first define a struct a very costly preprocessing step:
@@ -27,8 +29,8 @@ function AbstractImageReconstruction.process(::Type{<:AbstractRadonAlgorithm}, p
2729
end
2830

2931
# Now we can define a processing step that internally uses another processing step. We allow this inner parameter to be cached by considering the following `Union`:
30-
Base.@kwdef struct RadonCachedPreprocessingParameters{P <: AbstractRadonPreprocessingParameters} <: AbstractRadonPreprocessingParameters
31-
params::Union{P, ProcessResultCache{P}}
32+
Base.@kwdef struct RadonCachedPreprocessingParameters{P <: AbstractRadonPreprocessingParameters, PU <: AbstractUtilityReconstructionParameters{P}} <: AbstractRadonPreprocessingParameters
33+
params::Union{P, PU}
3234
end
3335
# Note that this case is a bit artifical and a more sensible place would be the algorithm parameters themselves. However, for the case of simplicity we did not introduce the concept in the example.
3436
# In this artifical case we just pass the parameters to the processing step. A real implementation might do some more processing with the result of the inner processing step:

src/AlgorithmInterface.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ Abstract type for image reconstruction parameters. An algorithm consists of one
1414
"""
1515
abstract type AbstractImageReconstructionParameters end
1616

17+
export AbstractUtilityReconstructionParameters
18+
"""
19+
AbstractUtilityReconstructionParameters{T <: AbstractImageReconstructionParameters}
20+
21+
Abstract type that offer utility functions for a given reconstruction parameter and its associated `process` steps. Utility `process` steps should return the same result as `T` for the same inputs.
22+
"""
23+
abstract type AbstractUtilityReconstructionParameters{T <: AbstractImageReconstructionParameters} end
24+
1725
export put!, take!
1826
"""
1927
put!(algo::AbstractImageReconstructionAlgorithm, inputs...)
@@ -91,7 +99,12 @@ If not implemented for an instance of `algo`, the default implementation is call
9199
"""
92100
function process end
93101
process(algo::AbstractImageReconstructionAlgorithm, param::AbstractImageReconstructionParameters, inputs...) = process(typeof(algo), param, inputs...)
102+
"""
103+
process(algo::Union{A, Type{A}}, param::AbstractUtilityReconstructionParameters{P}, inputs...) where {A <: AbstractImageReconstructionAlgorithm, P <: AbstractImageReconstructionParameters}
94104
105+
Process `inputs` with algorithm `algo` and return the result as if the arguments were given to `P`. Examples of utility `process` are processes which offer caching or remote execution.
106+
"""
107+
process(::A, param::AbstractUtilityReconstructionParameters{P}, inputs...) where {A, P} = error("$(typeof(param)) must implement `process` for $A and given inputs")
95108
"""
96109
Enable multiple process steps by supplying a Vector of parameters
97110
"""

src/RecoPlans/Cache.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export ProcessResultCache
55
Cache of size `maxsize` for the result of `process` methods. The cache is based on the `hash` of the inputs of the `process` function. Cache is shared between all algorithms constructed from the same plan.
66
The cache is transparent for properties of the underlying parameter. Cache can be invalidated by calling `empty!` on the cache.
77
"""
8-
Base.@kwdef mutable struct ProcessResultCache{P <: AbstractImageReconstructionParameters} <: AbstractImageReconstructionParameters
8+
Base.@kwdef mutable struct ProcessResultCache{P} <: AbstractUtilityReconstructionParameters{P}
99
param::P
1010
const maxsize::Int64 = 1
1111
cache::LRU{UInt64, Any} = LRU{UInt64, Any}(maxsize = maxsize)

0 commit comments

Comments
 (0)