Skip to content

Commit

Permalink
Merge pull request #8 from JuliaAI/getorcreateexperiment
Browse files Browse the repository at this point in the history
#7 - getorcreateexperiment method.
  • Loading branch information
deyandyankov authored Nov 19, 2021
2 parents 12db7ed + 8cfc1b5 commit c659e78
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 16 deletions.
1 change: 1 addition & 0 deletions docs/src/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ MLFlowRunStatus
```@docs
createexperiment
getexperiment
getorcreateexperiment
listexperiments
deleteexperiment
```
Expand Down
1 change: 1 addition & 0 deletions src/MLFlowClient.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ include("experiments.jl")
export
createexperiment,
getexperiment,
getorcreateexperiment,
deleteexperiment,
listexperiments

Expand Down
59 changes: 54 additions & 5 deletions src/experiments.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Creates an MLFlow experiment.
- `tags`: a Dictionary of key-values which tag the experiment.
# Returns
Experiment identifier (integer).
An object of type [`MLFlowExperiment`](@ref).
"""
function createexperiment(mlf::MLFlow; name=missing, artifact_location=missing, tags=missing)
Expand All @@ -19,7 +19,8 @@ function createexperiment(mlf::MLFlow; name=missing, artifact_location=missing,
name = string(UUIDs.uuid4())
end
result = mlfpost(mlf, endpoint; name=name, artifact_location=artifact_location, tags=tags)
parse(Int, result["experiment_id"])
experiment_id = parse(Int, result["experiment_id"])
getexperiment(mlf, experiment_id)
end

"""
Expand Down Expand Up @@ -82,18 +83,66 @@ function _getexperimentbyname(mlf::MLFlow, experiment_name::String)
end

"""
deleteexperiment(mlf::MLFlow, experiment_id)
getorcreateexperiment(mlf::MLFlow, experiment_name::String)
Gets an experiment if one alrady exists, or creates a new one.
# Arguments
- `mlf`: [`MLFlow`](@ref) configuration.
- `experiment_name`: Experiment name.
# Returns
An instance of type [`MLFlowExperiment`](@ref)
"""
function getorcreateexperiment(mlf::MLFlow, experiment_name::String)
exp = getexperiment(mlf, experiment_name)
if ismissing(exp)
exp = createexperiment(mlf, name=experiment_name)
end
exp
end

"""
deleteexperiment(mlf::MLFlow, experiment_id::Integer)
Deletes an MLFlow experiment.
# Arguments
- `mlf`: [`MLFlow`](@ref) configuration.
- `experiment_id`: experiment identifier.
# Returns
`true` if successful. Otherwise, raises exception.
"""
function deleteexperiment(mlf::MLFlow, experiment_id)
function deleteexperiment(mlf::MLFlow, experiment_id::Integer)
endpoint = "experiments/delete"
mlfpost(mlf, endpoint; experiment_id=experiment_id)
try
result = mlfpost(mlf, endpoint; experiment_id=experiment_id)
catch e
if isa(e, HTTP.ExceptionRequest.StatusError) && e.status == 404
# experiment already deleted
return true
end
throw(e)
end
true
end
"""
deleteexperiment(mlf::MLFlow, experiment::MLFlowExperiment)
Deletes an MLFlow experiment.
# Arguments
- `mlf`: [`MLFlow`](@ref) configuration.
- `experiment`: an object of type [`MLFlowExperiment`](@ref)
Dispatches to `deleteexperiment(mlf::MLFlow, experiment_id::Integer)`.
"""
deleteexperiment(mlf::MLFlow, experiment::MLFlowExperiment) =
deleteexperiment(mlf, experiment.experiment_id)

"""
listexperiments(mlf::MLFlow)
Expand Down
7 changes: 7 additions & 0 deletions src/runs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ function createrun(mlf::MLFlow, experiment_id; start_time=missing, tags=missing)
result = mlfpost(mlf, endpoint; experiment_id=experiment_id, start_time=string(start_time), tags=tags)
MLFlowRun(result["run"]["info"], result["run"]["data"])
end
"""
createrun(mlf::MLFlow, experiment::MLFlowExperiment; start_time=missing, tags=missing)
Dispatches to `createrun(mlf::MLFlow, experiment_id; start_time=start_time, tags=tags)`
"""
createrun(mlf::MLFlow, experiment::MLFlowExperiment; start_time=missing, tags=missing) =
createrun(mlf, experiment.experiment_id; start_time=start_time, tags=tags)

"""
getrun(mlf::MLFlow, run_id)
Expand Down
5 changes: 4 additions & 1 deletion src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ Base type which defines location and version for MLFlow API service.
# Constructors
- `MLFlow(baseuri; apiversion=2.0)`
- `MLFlow()` - defaults to `MLFlow("http://localhost:5000")`
# Examples
``` julia-repl
julia> mlf = MLFlow("http://localhost:5000")
julia> mlf = MLFlow()
MLFlow("http://localhost:5000", 2.0)
```
Expand All @@ -22,6 +24,7 @@ struct MLFlow
apiversion
MLFlow(baseuri; apiversion=2.0) = new(baseuri, apiversion)
end
MLFlow() = MLFlow("http://localhost:5000")

"""
MLFlowExperiment
Expand Down
44 changes: 34 additions & 10 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,23 @@ function mlflow_server_is_running(mlf::MLFlow)
end
end

@testset "MLFlowClient.jl" begin
mlflowbaseuri = "http://localhost:5000"
mlf = MLFlow(mlflowbaseuri)
@test mlf.baseuri == mlflowbaseuri
@testset "MLFlow" begin
mlf = MLFlow()
@test mlf.baseuri == "http://localhost:5000"
@test mlf.apiversion == 2.0
end

if !mlflow_server_is_running(mlf)
return nothing
end
@testset "MLFlowClient.jl" begin
mlf = MLFlow()
mlflow_server_is_running(mlf) || return nothing

exptags = [:key => "val"]
expname = "expname-$(UUIDs.uuid4())"

@test ismissing(getexperiment(mlf, "$(UUIDs.uuid4()) - $(UUIDs.uuid4())"))

experiment_id = createexperiment(mlf; name=expname, tags=exptags)
experiment = getexperiment(mlf, experiment_id)
@test experiment.experiment_id == experiment_id
experiment = createexperiment(mlf; name=expname, tags=exptags)
experiment_id = experiment.experiment_id
experimentbyname = getexperiment(mlf, expname)
@test experimentbyname.name == experiment.name

Expand Down Expand Up @@ -92,3 +91,28 @@ end
@test experiment.experiment_id == experiment_id
@test experiment.lifecycle_stage == "deleted"
end

@testset "createrun" begin
mlf = MLFlow()
mlflow_server_is_running(mlf) || return nothing

expname = "getorcreate-$(UUIDs.uuid4())"
e = getorcreateexperiment(mlf, expname)
r = createrun(mlf, e.experiment_id)
@test isa(r, MLFlowRun)
rr = createrun(mlf, e)
@test isa(rr, MLFlowRun)
end
@testset "getorcreateexperiment" begin
mlf = MLFlow()
mlflow_server_is_running(mlf) || return nothing

expname = "getorcreate"
e = getorcreateexperiment(mlf, expname)
@test isa(e, MLFlowExperiment)
ee = getorcreateexperiment(mlf, expname)
@test isa(ee, MLFlowExperiment)
@test e === ee
@test deleteexperiment(mlf, ee)
@test deleteexperiment(mlf, ee)
end

0 comments on commit c659e78

Please sign in to comment.