Skip to content

Commit

Permalink
Merge pull request #34 from Open-EO/fix_optional_process_params
Browse files Browse the repository at this point in the history
Fix optional and required args
  • Loading branch information
danlooo authored Sep 11, 2024
2 parents 2369525 + a2a0f7e commit c0dd257
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
matrix:
version:
- "1.9"
- "nightly"
- "1.10"
os:
- ubuntu-latest
arch:
Expand Down
6 changes: 3 additions & 3 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ password = ENV["OPENEO_PASSWORD"]
c = connect("earthengine.openeo.org", "v1.0", username, password)
step1 = c.load_collection(
"COPERNICUS/S2", BoundingBox(west=16.06, south=48.06, east=16.65, north=48.35),
["2020-01-01", "2020-01-31"], ["B10"]
["2020-01-01", "2020-01-31"]; bands = ["B10"]
)
step2 = c.reduce_dimension(step1, ProcessGraph("median"), "t", nothing)
step3 = c.save_result(step2, "GTIFF-ZIP", Dict())
step2 = c.reduce_dimension(step1, ProcessGraph("median"), "t")
step3 = c.save_result(step2, "GTIFF")
path = c.compute_result(step3)
```

Expand Down
39 changes: 23 additions & 16 deletions src/Processes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,35 @@ struct Process
experimental::Union{Nothing,Bool}
end
StructTypes.StructType(::Type{Process}) = StructTypes.Struct()
function (process::Process)(args...)
params = get_parameters(process.parameters)
length(args) == length(params) || throw(ArgumentError("Number of arguments does not match for process $(process.id)"))
argument_dict = Dict{Symbol,Any}()
for i in 1:length(args)
argname, argtype = params[i]
args[i] isa argtype || throw(ArgumentError("Type of argument number $i does not match, expected $argtype but got $(typeof(args[i]))"))
argument_dict[argname] = args[i]

function (process::Process)(args...; kwargs...)
required_args = get_parameters(process.parameters, :required)

length(required_args) == length(args) || error("Must provide $(length(required_args)) positional arguments")

args_d = Dict{Symbol,Any}(zip(map(x -> x.first, required_args), args))
merge!(args_d, Dict(kwargs))

if isnothing(args_d)
args_d = Dict{Symbol,Any}()
end
ProcessCall("$(process.id)", argument_dict)
ProcessCall("$(process.id)", args_d)
end

function Docs.getdoc(process::Process)
arguments = get_parameters(process.parameters)
args_str = join(["$(k)::$(v)" for (k, v) in arguments], ", ")
docs = """ $(process.id)($(args_str))
args_str = join(["$(k)::$(v)" for (k, v) in get_parameters(process.parameters, :required)], ", ")
kwargs_str = join(["$(k)::$(v)" for (k, v) in get_parameters(process.parameters, :optional)], ", ")
docs = """ $(process.id)($(args_str); $(kwargs_str))
$(process.description)
"""
Markdown.parse(docs)
end
Base.Docs.doc(p::Process, ::Type=Union{}) = Base.Docs.getdoc(p)

function Base.show(io::IO, ::MIME"text/plain", p::Process)
print(io, "$(p.id)($(join([x.name for x in p.parameters], ", "))): $(p.summary)")
args_str = join(["$(k)::$(v)" for (k, v) in get_parameters(p.parameters, :required)], ", ")
kwargs_str = join(["$(k)::$(v)" for (k, v) in get_parameters(p.parameters, :optional)], ", ")
print(io, "$(p.id)($(args_str); $(kwargs_str)): $(p.summary)")
end

# root e.g. https://earthengine.openeo.org/v1.0/processes
Expand All @@ -76,7 +82,6 @@ mutable struct ProcessCall <: AbstractProcessCall
const arguments::Dict{Symbol,Any}
result::Bool
end
ProcessCall(id, process_id, arguments) = ProcessCall(id, process_id, arguments, false)
StructTypes.StructType(::Type{ProcessCall}) = StructTypes.Mutable()
StructTypes.excludes(::Type{ProcessCall}) = (:id,)

Expand Down Expand Up @@ -112,7 +117,7 @@ function Base.show(io::IO, ::MIME"text/plain", p::ProcessCall)
pretty_print(io, Dict(:result => p.result))
end

function get_parameters(parameters)
function get_parameters(parameters, keep=:all)
# openEO type string to Julia type
julia_types_map = Dict(
"string" => String,
Expand Down Expand Up @@ -145,7 +150,9 @@ function get_parameters(parameters)
julia_types = [get(julia_types_map, t, String) for t in types]
julia_type = Union{julia_types...}

push!(res, name => julia_type)
if keep == :all || (keep == :optional && p.optional == true) || (keep == :required && isnothing(p.optional))
push!(res, name => julia_type)
end
end
return res
end
Expand Down
6 changes: 3 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ password = ENV["OPENEO_PASSWORD"]
# test sequential workflow
step1 = c2.load_collection(
"COPERNICUS/S2", BoundingBox(west=16.06, south=48.06, east=16.65, north=48.35),
["2020-01-20", "2020-01-30"], ["B10"]
["2020-01-20", "2020-01-30"]; bands=["B10"]
)
@test step1.id == "load_collection_tQ79zrFEGi8="
@test step1.process_id == "load_collection"
@test Set(keys(step1.arguments)) == Set([:bands, :id, :spatial_extent, :temporal_extent])
@test step1.arguments[:bands] == ["B10"]

step2 = c2.reduce_dimension(step1, ProcessGraph("median"), "t", nothing)
step3 = c2.save_result(step2, "JPEG", Dict())
step2 = c2.reduce_dimension(step1, ProcessGraph("median"), "t")
step3 = c2.save_result(step2, "JPEG")
result = c2.compute_result(step3)
@test result == "out.jpeg"

Expand Down

0 comments on commit c0dd257

Please sign in to comment.