Skip to content

Commit 4b4bed6

Browse files
committed
logartifact now stores bytes objects; some test refactoring.
1 parent 3fbdf6f commit 4b4bed6

File tree

5 files changed

+108
-32
lines changed

5 files changed

+108
-32
lines changed

Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ version = "0.1.0"
55

66
[deps]
77
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
8+
FilePathsBase = "48062228-2e41-5def-b9a4-89aafe57970f"
89
HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3"
910
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
1011
URIs = "5c2747f8-b7ea-4ff2-ba2e-563bfd36b1d4"
1112
UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
1213

1314
[compat]
15+
FilePathsBase = "0.9"
1416
HTTP = "0.9"
1517
JSON = "0.21"
1618
URIs = "1"

src/MLFlowClient.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ using UUIDs
1717
using HTTP
1818
using URIs
1919
using JSON
20+
using FilePathsBase: AbstractPath
2021

2122
include("types.jl")
2223
export

src/logging.jl

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ end
6262

6363

6464
"""
65-
logartifact(mlf::MLFlow, run, filename)
65+
logartifact(mlf::MLFlow, run, basefilename, data)
6666
6767
Stores an artifact (file) in the run's artifact location.
6868
@@ -73,13 +73,60 @@ Stores an artifact (file) in the run's artifact location.
7373
# Arguments
7474
- `mlf::MLFlow`: [`MLFlow`](@ref) onfiguration. Currently not used, but when this method is extended to support `S3`, information from `mlf` will be needed.
7575
- `run`: one of [`MLFlowRun`](@ref), [`MLFlowRunInfo`](@ref) or `String`.
76-
- `filename`: path to the artifact that needs to be sent to `MLFlow`.
76+
- `basefilename`: name of the file to be written.
77+
- `data`: artifact content, an object that can be written directly to a file handle.
78+
79+
# Throws
80+
- an `ErrorException` if an exception occurs during writing artifact.
81+
82+
# Returns
83+
84+
path of the artifact that was created.
7785
"""
78-
function logartifact(mlf::MLFlow, run_id::String, filename)
86+
function logartifact(mlf::MLFlow, run_id::AbstractString, basefilename::AbstractString, data)
7987
mlflowrun = getrun(mlf, run_id)
8088
artifact_uri = mlflowrun.info.artifact_uri
8189
mkpath(artifact_uri)
82-
cp(filename, joinpath(artifact_uri, basename(filename)))
90+
filepath = joinpath(artifact_uri, basefilename)
91+
try
92+
f = open(filepath, "w")
93+
write(f, data)
94+
close(f)
95+
catch e
96+
error("Unable to create artifact $(filepath): $e")
97+
end
98+
filepath
99+
end
100+
logartifact(mlf::MLFlow, run::MLFlowRun, basefilename::AbstractString, data) =
101+
logartifact(mlf, run.info, basefilename, data)
102+
logartifact(mlf::MLFlow, run_info::MLFlowRunInfo, basefilename::AbstractString, data) =
103+
logartifact(mlf, run_info.run_id, basefilename, data)
104+
105+
"""
106+
logartifact(mlf::MLFlow, run, filepath)
107+
108+
Stores an artifact (file) in the run's artifact location.
109+
The name of the artifact is calculated using `basename(filepath)`.
110+
111+
Dispatches on `logartifact(mlf::MLFlow, run, basefilename, data)` where `data` is the contents of `filepath`.
112+
113+
# Throws
114+
- an `ErrorException` if `filepath` does not exist.
115+
- an exception if such occurs while trying to read the contents of `filepath`.
116+
117+
"""
118+
function logartifact(mlf::MLFlow, run_id::AbstractString, filepath::Union{AbstractPath,AbstractString})
119+
isfile(filepath) || error("File $filepath does not exist.")
120+
try
121+
f = open(filepath, "r")
122+
data = read(f)
123+
close(f)
124+
return logartifact(mlf, run_id, basename(filepath), data)
125+
catch e
126+
throw(e)
127+
end
83128
end
84-
logartifact(mlf::MLFlow, run::MLFlowRun, filename) = logartifact(mlf, run.info, filename)
85-
logartifact(mlf::MLFlow, run_info::MLFlowRunInfo, filename) = logartifact(mlf, run_info.run_id, filename)
129+
logartifact(mlf::MLFlow, run::MLFlowRun, filepath::Union{AbstractPath,AbstractString}) =
130+
logartifact(mlf, run.info, filepath)
131+
logartifact(mlf::MLFlow, run_info::MLFlowRunInfo, filepath::Union{AbstractPath,AbstractString}) =
132+
logartifact(mlf, run_info.run_id, filepath)

src/runs.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,15 @@ Deletes an experiment's run.
9494
# Arguments
9595
- `mlf`: [`MLFlow`](@ref) configuration.
9696
- `run`: one of [`MLFlowRun`](@ref), [`MLFlowRunInfo`](@ref), or `String`.
97+
98+
# Returns
99+
`true` if successful.
100+
97101
"""
98102
function deleterun(mlf::MLFlow, run_id::String)
99103
endpoint = "runs/delete"
100104
mlfpost(mlf, endpoint; run_id=run_id)
105+
true
101106
end
102107
deleterun(mlf::MLFlow, run_info::MLFlowRunInfo) = deleterun(mlf, run_info.run_id)
103108
deleterun(mlf::MLFlow, run::MLFlowRun) = deleterun(mlf, run.info)

test/runtests.jl

Lines changed: 47 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,50 @@ end
2525
mlf = MLFlow()
2626
@test mlf.baseuri == "http://localhost:5000"
2727
@test mlf.apiversion == 2.0
28+
mlf = MLFlow("https://localhost:5001", apiversion=3.0)
29+
@test mlf.baseuri == "https://localhost:5001"
30+
@test mlf.apiversion == 3.0
31+
end
32+
33+
@testset "createexperiment" begin
34+
@ensuremlf
35+
exp = createexperiment(mlf)
36+
@test isa(exp, MLFlowExperiment)
37+
@test deleteexperiment(mlf, exp)
38+
experiment = getexperiment(mlf, exp.experiment_id)
39+
@test experiment.experiment_id == exp.experiment_id
40+
@test experiment.lifecycle_stage == "deleted"
41+
end
42+
43+
@testset "createrun" begin
44+
@ensuremlf
45+
expname = "getorcreate-$(UUIDs.uuid4())"
46+
e = getorcreateexperiment(mlf, expname)
47+
r = createrun(mlf, e.experiment_id)
48+
@test isa(r, MLFlowRun)
49+
@test deleterun(mlf, r)
50+
rr = createrun(mlf, e)
51+
@test isa(rr, MLFlowRun)
52+
@test deleterun(mlf, rr)
53+
@test deleteexperiment(mlf, e)
54+
end
55+
56+
@testset "getorcreateexperiment" begin
57+
@ensuremlf
58+
expname = "getorcreate"
59+
e = getorcreateexperiment(mlf, expname)
60+
@test isa(e, MLFlowExperiment)
61+
ee = getorcreateexperiment(mlf, expname)
62+
@test isa(ee, MLFlowExperiment)
63+
@test e === ee
64+
@test deleteexperiment(mlf, ee)
65+
@test deleteexperiment(mlf, ee)
2866
end
2967

3068
@testset "MLFlowClient.jl" begin
3169
@ensuremlf
70+
exp = createexperiment(mlf)
71+
@test isa(exp, MLFlowExperiment)
3272

3373
exptags = [:key => "val"]
3474
expname = "expname-$(UUIDs.uuid4())"
@@ -63,9 +103,14 @@ end
63103
f = open(tmpfiletoupload, "w")
64104
write(f, "samplecontents")
65105
close(f)
66-
logartifact(mlf, retrieved_run, tmpfiletoupload)
106+
artifactpath = logartifact(mlf, retrieved_run, tmpfiletoupload)
107+
@test isfile(artifactpath)
108+
@test_throws SystemError logartifact(mlf, retrieved_run, "/etc/shadow")
67109
rm(tmpfiletoupload)
68110

111+
artifactpath = logartifact(mlf, retrieved_run, "randbytes.bin", b"some rand bytes here")
112+
@test isfile(artifactpath)
113+
69114
running_run = updaterun(mlf, exprunid, "RUNNING")
70115
@test running_run.info.experiment_id == experiment_id
71116
@test running_run.info.status == MLFlowRunStatus("RUNNING")
@@ -94,31 +139,7 @@ end
94139
deleterun(mlf, exprunid)
95140
deleterun(mlf, exprun2)
96141

97-
deleteexperiment(mlf, experiment_id)
98-
experiment = getexperiment(mlf, experiment_id)
99-
@test experiment.experiment_id == experiment_id
100-
@test experiment.lifecycle_stage == "deleted"
142+
deleteexperiment(mlf, exp)
101143
end
102144

103-
@testset "createrun" begin
104-
@ensuremlf
105-
106-
expname = "getorcreate-$(UUIDs.uuid4())"
107-
e = getorcreateexperiment(mlf, expname)
108-
r = createrun(mlf, e.experiment_id)
109-
@test isa(r, MLFlowRun)
110-
rr = createrun(mlf, e)
111-
@test isa(rr, MLFlowRun)
112-
end
113145

114-
@testset "getorcreateexperiment" begin
115-
@ensuremlf
116-
expname = "getorcreate"
117-
e = getorcreateexperiment(mlf, expname)
118-
@test isa(e, MLFlowExperiment)
119-
ee = getorcreateexperiment(mlf, expname)
120-
@test isa(ee, MLFlowExperiment)
121-
@test e === ee
122-
@test deleteexperiment(mlf, ee)
123-
@test deleteexperiment(mlf, ee)
124-
end

0 commit comments

Comments
 (0)