-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Inject context and refactor interface (#35)
- Loading branch information
Showing
13 changed files
with
305 additions
and
237 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
""" | ||
SciML Operations interface for the simulation service | ||
""" | ||
module Interface | ||
|
||
import CSV | ||
|
||
include("./ProblemInputs.jl"); import .ProblemInputs: conversions_for_valid_inputs | ||
include("./SystemInputs.jl"); import .SystemInputs: Context | ||
include("../operations/Operations.jl"); import .Operations | ||
include("../Settings.jl"); import .Settings: settings | ||
|
||
export get_operation, conversions_for_valid_inputs, Context | ||
|
||
""" | ||
Sim runs that can be created using the `/runs/sciml/{operation}` endpoint. | ||
""" | ||
function get_operation(raw_operation) | ||
operation = Symbol(raw_operation) | ||
if in(operation, names(Operations; all=false, imported=true)) | ||
return getfield(Operations, operation) | ||
else | ||
return nothing | ||
end | ||
end | ||
|
||
""" | ||
Return an operation wrapped with necessary handlers | ||
""" | ||
function use_operation(context::Context) | ||
operation = get_operation(context.operation) | ||
|
||
# NOTE: This runs inside the job so we can't use it to validate on request ATM | ||
function coerced_operation(arglist::Dict{Symbol, Any}) | ||
# TODO(five): Fail properly on extra params | ||
fixed_args = Dict( | ||
name => conversions_for_valid_inputs[name](value) | ||
for (name, value) in arglist | ||
) | ||
operation(;fixed_args..., context=context) | ||
end | ||
end | ||
|
||
end # module Interface |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
""" | ||
User-provided, problem-specific inputs | ||
""" | ||
module ProblemInputs | ||
|
||
import AlgebraicPetri: PropertyLabelledReactionNet, LabelledPetriNet, AbstractPetriNet | ||
import Catlab.CategoricalAlgebra: parse_json_acset | ||
import DataFrames: DataFrame | ||
|
||
export conversions_for_valid_inputs | ||
|
||
""" | ||
Transform string into dataframe before it is used as input | ||
""" | ||
coerce_dataset(val::String) = CSV.read(IOBuffer(val), DataFrame) | ||
|
||
""" | ||
Act as identity since the value is already coerced | ||
""" | ||
coerce_dataset(val::DataFrame) = val | ||
|
||
""" | ||
Inputs converted from payload to arguments expanded in operations. | ||
""" | ||
conversions_for_valid_inputs = Dict{Symbol,Function}( | ||
:model => (val) -> parse_json_acset(PropertyLabelledReactionNet{Number, Number, Dict}, val), # hack for mira | ||
:tspan => (val) -> Tuple{Float64,Float64}(val), | ||
:params => (val) -> Dict{String,Float64}(val), | ||
:initials => (val) -> Dict{String,Float64}(val), | ||
:dataset => coerce_dataset, | ||
:feature_mappings => (val) -> Dict{String, String}(val), | ||
:timesteps_column => (val) -> String(val) | ||
) | ||
|
||
end # module ProblemInputs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
""" | ||
System provided information that is made available to operations | ||
""" | ||
module SystemInputs | ||
|
||
export Context | ||
|
||
struct Context | ||
job_id::Int64 | ||
interactivity_hook::Function | ||
operation::Symbol | ||
end | ||
|
||
function Base.iterate(context::Context, state=nothing) | ||
if isnothing(state) | ||
state = Set(fieldnames(Context)) | ||
end | ||
unused = intersect(Set(fieldnames(Context)), state) | ||
if isempty(unused) | ||
return nothing | ||
end | ||
chosen_field = pop!(unused) | ||
(chosen_field=>getfield(context, chosen_field), unused) | ||
end | ||
|
||
end # module SystemInputs |
Oops, something went wrong.