Skip to content

Commit 22767d4

Browse files
committed
Adding loginputs function
1 parent c4bd0be commit 22767d4

File tree

5 files changed

+70
-19
lines changed

5 files changed

+70
-19
lines changed

src/MLFlowClient.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export
7171
include("services/loggers.jl")
7272
export
7373
logmetric,
74-
logbatch
74+
logbatch,
75+
loginputs
7576

7677
end

src/services/loggers.jl

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,41 @@ logmetric(instance::MLFlow, run::Run, key::String, value::Float64;
2828
logmetric(instance, run.info.run_id, key, value; timestamp=timestamp, step=step)
2929

3030
"""
31-
logbatch(instance::MLFlow, run_id::String; metrics::Array{Metric},
32-
params::Array{Param}, tags::Array{Tag})
31+
logbatch(instance::MLFlow, run_id::String;
32+
metrics::MLFlowUpsertData{Metric}, params::MLFlowUpsertData{Param},
33+
tags::MLFlowUpsertData{Tag})
3334
logbatch(instance::MLFlow, run::Run; metrics::Array{Metric},
34-
params::Array{Param}, tags::Array{Tag})
35+
params::MLFlowUpsertData{Param}, tags::MLFlowUpsertData{Tag})
3536
3637
Log a batch of metrics, params, and tags for a run. In case of error, partial
3738
data may be written.
3839
3940
For more information about this function, check [MLFlow official documentation](https://mlflow.org/docs/latest/rest-api.html#log-batch).
4041
"""
41-
function logbatch(instance::MLFlow, run_id::String;
42+
logbatch(instance::MLFlow, run_id::String;
4243
metrics::MLFlowUpsertData{Metric}=Metric[],
43-
params::MLFlowUpsertData{Param}=Param[], tags::MLFlowUpsertData{Tag}=Tag[])
44+
params::MLFlowUpsertData{Param}=Param[],
45+
tags::MLFlowUpsertData{Tag}=Tag[]) =
4446
mlfpost(instance, "runs/log-batch"; run_id=run_id,
4547
metrics=parse(Metric, metrics), params=parse(Param, params),
4648
tags=parse(Tag, tags))
47-
end
4849
logbatch(instance::MLFlow, run::Run;
4950
metrics::MLFlowUpsertData{Metric}=Metric[],
5051
params::MLFlowUpsertData{Param}=Param[],
5152
tags::MLFlowUpsertData{Tag}=Tag[]) =
5253
logbatch(instance, run.info.run_id; metrics=metrics, params=params,
5354
tags=tags)
55+
56+
"""
57+
loginputs(instance::MLFlow, run_id::String; datasets::Array{DatasetInput})
58+
loginputs(instance::MLFlow, run::Run; datasets::Array{DatasetInput})
59+
60+
# Arguments
61+
- `instance`: [`MLFlow`](@ref) configuration.
62+
- `run_id`: ID of the run to log under This field is required.
63+
- `datasets`: Dataset inputs.
64+
"""
65+
loginputs(instance::MLFlow, run_id::String, datasets::Array{DatasetInput}) =
66+
mlfpost(instance, "runs/log-inputs"; run_id=run_id, datasets=datasets)
67+
loginputs(instance::MLFlow, run::Run, datasets::Array{DatasetInput}) =
68+
loginputs(instance, run.info.run_id, datasets)

src/types/run.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ Metric associated with a run, represented as a key-value pair.
77
- `key::String`: Key identifying this metric.
88
- `value::Float64`: Value associated with this metric.
99
- `timestamp::Int64`: The timestamp at which this metric was recorded.
10-
- `step::Union{Int64, Missing}`: Step at which to log the metric.
10+
- `step::Union{Int64, Nothing}`: Step at which to log the metric.
1111
"""
1212
struct Metric <: LoggingData
1313
key::String
1414
value::Float64
1515
timestamp::Int64
16-
step::Union{Int64, Missing}
16+
step::Union{Int64, Nothing}
1717
end
1818
Metric(data::Dict{String, Any}) = Metric(data["key"], data["value"],
1919
data["timestamp"], data["step"])

src/utils.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function dict_to_T_array(::Type{T}, dict::Dict{String, <:NumberOrString}) where
1717
for (key, value) in dict
1818
if T<:Metric
1919
push!(entities, Metric(key, Float64(value),
20-
round(Int, now() |> datetime2unix), missing))
20+
round(Int, now() |> datetime2unix), nothing))
2121
else
2222
push!(entities, T(key, value |> string))
2323
end
@@ -33,7 +33,7 @@ function pairarray_to_T_array(::Type{T}, pair_array::Array{<:Pair}) where T<:Log
3333
if T<:Metric
3434
value = pair.second
3535
push!(entities, Metric(key, Float64(value),
36-
round(Int, now() |> datetime2unix), missing))
36+
round(Int, now() |> datetime2unix), nothing))
3737
else
3838
value = pair.second |> string
3939
push!(entities, T(key, value))
@@ -55,7 +55,7 @@ function tuplearray_to_T_array(::Type{T},
5555
if T<: Metric
5656
value = tuple |> last
5757
push!(entities, Metric(key, Float64(value),
58-
round(Int, now() |> datetime2unix), missing))
58+
round(Int, now() |> datetime2unix), nothing))
5959
else
6060
value = tuple |> last |> string
6161
push!(entities, T(key, value))
@@ -77,7 +77,7 @@ function dictarray_to_T_array(::Type{T},
7777
else
7878
timestamp = round(Int, now() |> datetime2unix)
7979
end
80-
push!(entities, Metric(key, value, timestamp, missing))
80+
push!(entities, Metric(key, value, timestamp, nothing))
8181
else
8282
value = dict["value"] |> string
8383
push!(entities, T(key, value))

test/services/loggers.jl

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ end
3636

3737
@testset "with run id as string" begin
3838
run = createrun(mlf, experiment_id)
39-
logbatch(mlf, run.info.run_id, metrics=[("gala", 0.1)])
39+
logbatch(mlf, run.info.run_id; metrics=[("gala", 0.1)])
4040

4141
run = refresh(mlf, run)
4242
last_metric = run.data.metrics |> last
@@ -49,7 +49,7 @@ end
4949

5050
@testset "with run" begin
5151
run = createrun(mlf, experiment_id)
52-
logbatch(mlf, run, metrics=[("missy", 0.9)])
52+
logbatch(mlf, run; metrics=[("missy", 0.9)])
5353

5454
run = refresh(mlf, run)
5555
last_metric = run.data.metrics |> last
@@ -62,7 +62,7 @@ end
6262

6363
@testset "with metrics, params and tags as dict" begin
6464
run = createrun(mlf, experiment_id)
65-
logbatch(mlf, run, metrics=Dict("ana" => 0.5),
65+
logbatch(mlf, run; metrics=Dict("ana" => 0.5),
6666
params=Dict("test_param" => "0.9"),
6767
tags=Dict("test_tag" => "gala"))
6868

@@ -88,7 +88,7 @@ end
8888

8989
@testset "with metrics, params and tags as pair array" begin
9090
run = createrun(mlf, experiment_id)
91-
logbatch(mlf, run, metrics=["ana" => 0.5],
91+
logbatch(mlf, run; metrics=["ana" => 0.5],
9292
params=["test_param" => "0.9"], tags=["test_tag" => "gala"])
9393

9494
run = refresh(mlf, run)
@@ -113,7 +113,7 @@ end
113113

114114
@testset "with metrics, params and tags as tuple array" begin
115115
run = createrun(mlf, experiment_id)
116-
logbatch(mlf, run, metrics=[("ana", 0.5)],
116+
logbatch(mlf, run; metrics=[("ana", 0.5)],
117117
params=[("test_param", "0.9")], tags=[("test_tag", "gala")])
118118

119119
run = refresh(mlf, run)
@@ -138,7 +138,7 @@ end
138138

139139
@testset "with metrics, params and tags as dict array" begin
140140
run = createrun(mlf, experiment_id)
141-
logbatch(mlf, run,
141+
logbatch(mlf, run;
142142
metrics=[Dict("key" => "ana", "value" => 0.5, "timestamp" => 123)],
143143
params=[Dict("key" => "test_param", "value" => "0.9")],
144144
tags=[Dict("key" => "test_tag", "value" => "gala")])
@@ -166,3 +166,38 @@ end
166166

167167
deleteexperiment(mlf, experiment_id)
168168
end
169+
170+
@testset verbose = true "log inputs" begin
171+
@ensuremlf
172+
173+
experiment_id = createexperiment(mlf, UUIDs.uuid4() |> string)
174+
175+
@testset "with run id as string" begin
176+
run = createrun(mlf, experiment_id)
177+
inputs = [DatasetInput([Tag("tag_key", "tag_value")],
178+
Dataset("dataset_name", "dataset_digest", "dataset_source_type",
179+
"dataset_source", nothing, nothing))]
180+
loginputs(mlf, run.info.run_id, inputs)
181+
182+
run = refresh(mlf, run)
183+
184+
@test run.inputs.dataset_inputs |> length == 1
185+
186+
dataset_input = run.inputs.dataset_inputs |> first
187+
dataset_input_tag = dataset_input.tags |> first
188+
189+
@test dataset_input_tag isa Tag
190+
@test dataset_input_tag.key == "tag_key"
191+
@test dataset_input_tag.value == "tag_value"
192+
193+
@test dataset_input.dataset isa Dataset
194+
@test dataset_input.dataset.name == "dataset_name"
195+
@test dataset_input.dataset.digest == "dataset_digest"
196+
@test dataset_input.dataset.source_type == "dataset_source_type"
197+
@test dataset_input.dataset.source == "dataset_source"
198+
@test dataset_input.dataset.schema |> isnothing
199+
@test dataset_input.dataset.profile |> isnothing
200+
end
201+
202+
deleteexperiment(mlf, experiment_id)
203+
end

0 commit comments

Comments
 (0)