|
| 1 | +module AutoMLFlowClassifications |
| 2 | +using Statistics |
| 3 | +using Serialization |
| 4 | +import PythonCall |
| 5 | +const PYC = PythonCall |
| 6 | + |
| 7 | +using DataFrames: DataFrame |
| 8 | +using Random |
| 9 | +using ..AbsTypes |
| 10 | +using ..Utils |
| 11 | +using ..AutoClassifications |
| 12 | +using ..AutoMLPipeline: getiris |
| 13 | + |
| 14 | +import ..AbsTypes: fit, fit!, transform, transform! |
| 15 | +export fit, fit!, transform, transform! |
| 16 | +export mlfcldriver, AutoMLFlowClassification |
| 17 | + |
| 18 | +const MLF = PYC.pynew() |
| 19 | +const REQ = PYC.pynew() |
| 20 | + |
| 21 | +function __init__() |
| 22 | + PYC.pycopy!(MLF, PYC.pyimport("mlflow")) |
| 23 | + PYC.pycopy!(REQ, PYC.pyimport("requests")) |
| 24 | +end |
| 25 | + |
| 26 | +mutable struct AutoMLFlowClassification <: Workflow |
| 27 | + name::String |
| 28 | + model::Dict{Symbol,Any} |
| 29 | + |
| 30 | + function AutoMLFlowClassification(args=Dict()) |
| 31 | + default_args = Dict( |
| 32 | + :name => "AutoMLClassification", |
| 33 | + :projectname => "AutoMLClassification", |
| 34 | + :url => "http://localhost:8080", |
| 35 | + :description => "Automated Classification", |
| 36 | + :projecttype => "classification", |
| 37 | + :artifact_name => "autoclass.bin", |
| 38 | + :impl_args => Dict() |
| 39 | + ) |
| 40 | + cargs = nested_dict_merge(default_args, args) |
| 41 | + #cargs[:name] = cargs[:name] * "_" * randstring(3) |
| 42 | + experiment_tags = Dict( |
| 43 | + "projectname" => cargs[:projectname], |
| 44 | + "projecttype" => cargs[:projecttype], |
| 45 | + "notes" => cargs[:description] |
| 46 | + ) |
| 47 | + # check if mlflow server exists |
| 48 | + try |
| 49 | + httpget = getproperty(REQ, "get") |
| 50 | + res = httpget(cargs[:url] * "/health") |
| 51 | + catch |
| 52 | + @error("Mlflow Server Unreachable") |
| 53 | + exit(1) |
| 54 | + end |
| 55 | + MLF.set_tracking_uri(uri=cargs[:url]) |
| 56 | + name = cargs[:name] |
| 57 | + experiment = MLF.search_experiments(filter_string="name = \'$name\'") |
| 58 | + if PYC.pylen(experiment) != 0 |
| 59 | + MLF.set_experiment(experiment[0].name) |
| 60 | + else |
| 61 | + theexperiment = MLF.create_experiment(name=name, tags=experiment_tags) |
| 62 | + cargs[:experiment_id] = theexperiment |
| 63 | + end |
| 64 | + new(cargs[:name], cargs) |
| 65 | + end |
| 66 | +end |
| 67 | + |
| 68 | +function AutoMLFlowClassification(name::String, args::Dict) |
| 69 | + AutoMLFlowClassification(Dict(:name => name, args...)) |
| 70 | +end |
| 71 | + |
| 72 | +function AutoMLFlowClassification(name::String; args...) |
| 73 | + AutoMLFlowClassification(Dict(Dict(pairs(args))...)) |
| 74 | +end |
| 75 | + |
| 76 | +function (obj::AutoMLFlowClassification)(; args...) |
| 77 | + model = obj.model |
| 78 | + cargs = nested_dict_merge(model, Dict(pairs(args))) |
| 79 | + obj.model = cargs |
| 80 | + return obj |
| 81 | +end |
| 82 | + |
| 83 | +function fit!(mlfcl::AutoMLFlowClassification, X::DataFrame, Y::Vector) |
| 84 | + # end any running experiment |
| 85 | + # MLF.end_run() |
| 86 | + # generate run name |
| 87 | + run_name = mlfcl.model[:name] * "_" * "fit" * "_" * randstring(3) |
| 88 | + mlfcl.model[:run_name] = run_name |
| 89 | + MLF.set_experiment(mlfcl.model[:name]) |
| 90 | + MLF.start_run(run_name=run_name) |
| 91 | + # get run_id |
| 92 | + run = MLF.active_run() |
| 93 | + mlfcl.model[:run_id] = run.info.run_id |
| 94 | + # automate classification |
| 95 | + autoclass = AutoClassification() |
| 96 | + fit_transform!(autoclass, X, Y) |
| 97 | + bestmodel = autoclass.model[:bestpipeline].model[:description] |
| 98 | + MLF.log_param("bestmodel", bestmodel) |
| 99 | + MLF.log_param("pipelines", autoclass.model[:dfpipelines].Description) |
| 100 | + MLF.log_metric("bestperformance", autoclass.model[:performance].mean[1]) |
| 101 | + # save model in mlflow |
| 102 | + artifact_name = mlfcl.model[:artifact_name] |
| 103 | + # use temporary directory |
| 104 | + tmpdir = tempdir() |
| 105 | + artifact_location = joinpath(tmpdir, artifact_name) |
| 106 | + serialize(artifact_location, autoclass) |
| 107 | + MLF.log_artifact(artifact_location) |
| 108 | + # save model in memory |
| 109 | + mlfcl.model[:autoclass] = autoclass |
| 110 | + bestmodel_uri = MLF.get_artifact_uri(artifact_path=artifact_name) |
| 111 | + # save model uri location |
| 112 | + mlfcl.model[:bestmodel_uri] = bestmodel_uri |
| 113 | + MLF.end_run() |
| 114 | +end |
| 115 | + |
| 116 | +function fit(mlfcl::AutoMLFlowClassification, X::DataFrame, Y::Vector) |
| 117 | + mlfcopy = deepcopy(mlfcl) |
| 118 | + fit!(mlfcopy, X, Y) |
| 119 | + return mlfcopy |
| 120 | +end |
| 121 | + |
| 122 | +function transform!(mlfcl::AutoMLFlowClassification, X::DataFrame) |
| 123 | + MLF.end_run() |
| 124 | + # download model artifact |
| 125 | + run_id = mlfcl.model[:run_id] |
| 126 | + artifact_name = mlfcl.model[:artifact_name] |
| 127 | + |
| 128 | + try |
| 129 | + model_artifacts = MLF.artifacts.list_artifacts(run_id=run_id) |
| 130 | + @assert model_artifacts[0].path |> string == "autoclass.bin" |
| 131 | + catch e |
| 132 | + @info e |
| 133 | + throw("Artifact $artifact_name does not exist in run_id = $run_id") |
| 134 | + end |
| 135 | + |
| 136 | + run_name = mlfcl.model[:name] * "_" * "transform" * "_" * randstring(3) |
| 137 | + mlfcl.model[:run_name] = run_name |
| 138 | + MLF.set_experiment(mlfcl.model[:name]) |
| 139 | + MLF.start_run(run_name=run_name) |
| 140 | + pylocalpath = MLF.artifacts.download_artifacts(run_id=run_id, artifact_path=artifact_name) |
| 141 | + bestmodel = deserialize(string(pylocalpath)) |
| 142 | + Y = transform!(bestmodel, X) |
| 143 | + MLF.log_param("output", Y) |
| 144 | + MLF.end_run() |
| 145 | + return Y |
| 146 | +end |
| 147 | + |
| 148 | +function mlfcldriver() |
| 149 | + df = getiris() |
| 150 | + X = df[:, 1:end-1] |
| 151 | + Y = df[:, end] |> collect |
| 152 | + |
| 153 | + mlfclass = AutoMLFlowClassification() |
| 154 | + Yc = fit_transform!(mlfclass, X, Y) |
| 155 | + println("accuracy = ", mean(Y .== Yc)) |
| 156 | + |
| 157 | + # test prediction using exisiting trained model from artifacts |
| 158 | + run_id = mlfclass.model[:run_id] |
| 159 | + newmfclass = AutoMLFlowClassification(Dict(:run_id => run_id)) |
| 160 | + newmfclass = AutoMLFlowClassification() |
| 161 | + newmfclass(; run_id=run_id) |
| 162 | + Yn = transform!(newmfclass, X) |
| 163 | + println("accuracy = ", mean(Yn .== Y)) |
| 164 | + |
| 165 | + return nothing |
| 166 | +end |
| 167 | + |
| 168 | +end |
0 commit comments