Skip to content

Commit bac582a

Browse files
committed
refactor again to use the manifest to get metatdata by default
1 parent 53ab64f commit bac582a

File tree

15 files changed

+1100
-243
lines changed

15 files changed

+1100
-243
lines changed

Manifest.toml

Lines changed: 411 additions & 1 deletion
Large diffs are not rendered by default.

Project.toml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,17 @@ Suppressor = "fd094767-a336-5f1f-9728-57cf17d0bbfb"
1515
Distributed = "1.11.0"
1616
InteractiveUtils = "1.11.0"
1717
MLJModelInterface = "1.11.1"
18-
OrderedCollections = "1.8.1"
18+
MLJModels = "0.17"
19+
OrderedCollections = "1.8"
1920
Pkg = "1.11.0"
21+
Random = "1.11.0"
2022
Suppressor = "0.2"
2123
julia = "1.10"
2224

2325
[extras]
26+
MLJModels = "d491faf4-2d78-11e9-2867-c94bc002c0b7"
27+
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
2428
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
2529

2630
[targets]
27-
test = ["Test",]
31+
test = ["Test", "MLJModels", "Random"]

registry/Metadata.toml

Lines changed: 365 additions & 146 deletions
Large diffs are not rendered by default.

registry/Project.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,16 @@ MLJGLMInterface = "caf8df21-4939-456d-ac9c-5fefbfb04c0c"
1818
MLJIteration = "614be32b-d00c-4edb-bd02-1eb411ab5e55"
1919
MLJLIBSVMInterface = "61c7150f-6c77-4bb1-949c-13197eac2a52"
2020
MLJLinearModels = "6ee0df7b-362f-4a72-a706-9e79364fb692"
21+
MLJModelInterface = "e80e1ace-859a-464e-9ed9-23947d8ae3ea"
22+
MLJModelRegistry = "0a96183e-380b-4aa6-be10-c555140810f2"
2123
MLJModels = "d491faf4-2d78-11e9-2867-c94bc002c0b7"
2224
MLJMultivariateStatsInterface = "1b6a4a23-ba22-4f51-9698-8599985d3728"
2325
MLJNaiveBayesInterface = "33e4bacb-b9e2-458e-9a13-5d9a90b235fa"
2426
MLJScikitLearnInterface = "5ae90465-5518-4432-b9d2-8a1def2f0cab"
2527
MLJTSVDInterface = "7fa162e1-0e29-41ca-a6fa-c000ca4e7e7e"
2628
MLJTestInterface = "72560011-54dd-4dc2-94f3-c5de45b75ecd"
2729
MLJText = "5e27fcf9-6bac-46ba-8580-b5712f3d6387"
30+
MLJTransforms = "23777cdb-d90c-4eb0-a694-7c2b83d5c1d6"
2831
MLJTuning = "03970b2e-30c4-11ea-3135-d1576263f10f"
2932
MLJXGBoostInterface = "54119dfa-1dab-4055-a167-80440f4f7a91"
3033
Maxnet = "81f79f80-22f2-4e41-ab86-00c11cf0f26f"

src/GenericRegistry.jl

Lines changed: 55 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -111,36 +111,74 @@ end
111111
# # METHODS
112112

113113
"""
114-
GenericRegistry.run([setup,] packages, program)
114+
GenericRegistry.run([setup,] packages, program; environment=nothing)
115115
116-
In a temporary Julia process, and using a temporary package environment, do this:
116+
Assuming a package `environment` path is specified, do the following in a new Julia
117+
process:
117118
118-
1. Evaluate the `setup` expression (if present).
119+
1.Activate `environment`.
119120
120-
2. Add the specified `packages` to the environment, and instantiate.
121+
2. Evaluate the `setup` expression, if specified.
122+
123+
3. Instantiate the environment.
124+
125+
4. `import` all packages specified in `packages`.
121126
122127
3. Evaluate the `program` expression.
123128
124-
The returned value is a `Future` object which must be `fetch`ed to get the actual
125-
evaluated expression. Shut the temporary process down by calling `close` on the `Future`.
129+
The returned value is a `Future` object which must be `fetch`ed to get the final evaluated
130+
expression. Shut the temporary process down by calling `GenericRegistry.close` on the
131+
`Future`.
132+
133+
Step 3 might typically close by reversing any actions mutating the `environment`, but
134+
remember only the last evaluated expression is passed to the `Future`.
135+
136+
If `environment` is unspecified, then a fresh temporary environment is activated, and the
137+
packages listed in `packages` are manually added between Steps 2 and 3 above.
126138
127139
"""
128-
function run(setup, pkgs, program)
140+
function run(setup, pkgs, program; environment=nothing)
129141
pkgs isa Vector || (pkgs = [pkgs,])
130-
additions = [:(Pkg.add($pkg)) for pkg in pkgs]
131142
imports = [:(import $(Symbol(pkg))) for pkg in pkgs]
132-
program = quote
143+
ex = quote
133144
using Pkg
134-
Pkg.activate(temp=true)
135-
$(additions...)
136-
$setup
137-
Pkg.instantiate()
138-
$(imports...)
139-
$program
140145
end
141-
return run_in_temporary_process(program)
146+
if isnothing(environment)
147+
push!(
148+
ex.args,
149+
quote
150+
Pkg.activate(temp=true)
151+
end,
152+
)
153+
else
154+
push!(
155+
ex.args,
156+
quote
157+
Pkg.activate($environment)
158+
end,
159+
)
160+
end
161+
push!(ex.args, quote $setup end)
162+
if isnothing(environment)
163+
additions = [:(Pkg.add($pkg)) for pkg in pkgs]
164+
push!(
165+
ex.args,
166+
quote
167+
$(additions...)
168+
end,
169+
)
170+
end
171+
push!(
172+
ex.args,
173+
quote
174+
Pkg.instantiate()
175+
$(imports...)
176+
$program
177+
end,
178+
)
179+
return run_in_temporary_process(ex)
142180
end
143-
run(pkgs, program) = run(:(), pkgs, program)
181+
run(pkgs, program; kwargs...) = run(:(), pkgs, program; kwargs...)
144182

145183
"""
146184
GenericRegistry.close(future)

src/MLJModelRegistry.jl

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,18 @@ Module providing methods for managing the MLJ Model Registry. To modify the regi
1515
1616
- To add or update the metadata associated with a package, run [`update(pkg)`](@ref).
1717
18-
- Alternatively, to update the metadata for *all* packages in the registry (optional but
19-
recommended), run [`update()`](@ref).
18+
- Assuming this is successful, update the metadata for *all* packages in the registry
19+
by running [`update()`](@ref).
2020
2121
- When satisfied, commit your changes to the clone and make a pull request to the master
2222
MLJModelRegistry.jl repository.
2323
24-
!!! important
24+
!!! note
2525
2626
Removing a package from the "/registry/" enviroment does not remove its metadata from
27-
the Model Registry (i.e., from "/registry/Metatdata.toml"). Unless you later call
28-
`update()` to update all package metadata (slow), you must call
29-
[`MLJModelRegistry.gc()`](@ref) to specifically remove metadata for all orphaned
30-
packages (fast).
27+
the Model Registry (i.e., from "/registry/Metatdata.toml"). However if you call
28+
`update()` to update all package metadata (or call [`MLJModelRegistry.gc()`](@ref))
29+
the metadata for all orphaned packages is removed.
3130
3231
# Protocol for adding new packages to the registry environment
3332
@@ -70,8 +69,14 @@ struct Quiet end
7069
# The MLJ Model Registry is a special case of a "generic model registry", as described in
7170
# this file, defining the `GenericRegistry` module (which has methods, no types):
7271
include("GenericRegistry.jl")
72+
73+
# method to apply smoke tests to trait values for a model type:
7374
include("check_traits.jl")
75+
76+
# methods called on remote processes to help extract metadata for a package's models:
7477
include("remote_methods.jl")
78+
79+
# top-level methods for registry management:
7580
include("methods.jl")
7681

7782
export update

src/check_traits.jl

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,42 @@
1-
# TODO: This method should live at MLJModelInterface !
1+
# # LOGGING
2+
3+
err_bad_trait(M) = ErrorException("$M has a bad trait declaration. ")
4+
function err_bad_trait(M, message)
5+
@error message
6+
throw(err_bad_trait(M))
7+
end
8+
9+
# # HELPERS
210

311
ismissing_or_isa(x, T) = ismissing(x) || x isa T
412

13+
14+
# # METHOD TO CHECK TRAITS
15+
516
function check_traits(M)
6-
message = "$M has a bad trait declaration.\n"
7-
ismissing_or_isa(MLJModelInterface.is_pure_julia(M), Bool) ||
8-
error(message*"`is_pure_julia` must return true or false")
9-
ismissing_or_isa(MLJModelInterface.supports_weights(M), Bool) ||
10-
error(message*"`supports_weights` must return `true`, "*
11-
"`false` or `missing`. ")
12-
ismissing_or_isa(MLJModelInterface.supports_class_weights(M), Bool) ||
13-
error(message*"`supports_class_weights` must return `true`, "*
17+
if !ismissing_or_isa(MLJModelInterface.is_pure_julia(M), Bool)
18+
err_bad_trait(M, "`is_pure_julia` must return true or false")
19+
end
20+
if !ismissing_or_isa(MLJModelInterface.supports_weights(M), Bool)
21+
err_bad_trait(M, "`supports_weights` must return `true`, "*
22+
"`false` or `missing`. ")
23+
end
24+
if !ismissing_or_isa(MLJModelInterface.supports_class_weights(M), Bool)
25+
err_bad_trait(M, "`supports_class_weights` must return `true`, "*
1426
"`false` or `missing`. ")
15-
MLJModelInterface.is_wrapper(M) isa Bool ||
16-
error(message*"`is_wrapper` must return `true` or `false`. ")
27+
end
28+
if !(MLJModelInterface.is_wrapper(M) isa Bool)
29+
err_bad_trait(M, "`is_wrapper` must return `true` or `false`. ")
30+
end
1731
load_path = MLJModelInterface.load_path(M)
1832
load_path isa String ||
19-
error(message*"`load_path` must return a `String`. ")
33+
err_bad_trait(M, "`load_path` must return a `String`. ")
2034
contains(load_path, "unknown") &&
21-
error(message*"`load_path` return value contains string \"unknown\". ")
35+
err_bad_trait(M, "`load_path` return value contains string \"unknown\". ")
2236
pkg = MLJModelInterface.package_name(M)
23-
pkg isa String || error(message*"`package_name` must return a `String`. ")
37+
pkg isa String || err_bad_trait(M, "`package_name` must return a `String`. ")
2438
api_pkg = split(load_path, '.') |> first
25-
pkg == "unknown" && error(message*"`package_name` returns \"unknown\". ")
39+
pkg == "unknown" && err_bad_trait(M, "`package_name` returns \"unknown\". ")
2640
load_path_ex = Meta.parse(load_path)
2741
api_pkg_ex = Symbol(api_pkg)
2842
import_ex = :(import $api_pkg_ex)
@@ -33,7 +47,7 @@ function check_traits(M)
3347
try
3448
Main.eval(program_to_test_load_path)
3549
catch excptn
36-
error(message*"Cannot import value of `load_path` (parsed as expression). ")
50+
err_bad_trait(M, "Cannot import value of `load_path` (parsed as expression). ")
3751
rethrow(excptn)
3852
end
3953
end

0 commit comments

Comments
 (0)