Skip to content

Commit 14c1a7b

Browse files
committed
simplify GenericRegistry.run method
1 parent 380deab commit 14c1a7b

File tree

3 files changed

+40
-57
lines changed

3 files changed

+40
-57
lines changed

docs/src/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ Home of the MLJ Model Registry
55
# What's this repository for?
66

77
[MLJ](https://juliaml.ai) (Machine Learning in Julia) is a suite of Julia software
8-
packages providing a machine learning toolbox. The MLJModelRegistry.jl has two functions:
8+
packages providing a machine learning toolbox. The MLJModelRegistry.jl repository has two
9+
functions:
910

1011
- It hosts the *MLJ Model Registry*, a list of packages providing MLJ interfaces to
1112
machine learning algorithms, together with metadata about those models, such as the type

src/GenericRegistry.jl

Lines changed: 32 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,14 @@ Module providing basic tools to manage a *package registry*, by which is meant a
66
environment, together with "package metata", in the form of a dictionary of TOML-parsable
77
values, keyed on the environment's package dependencies, which is stored in a TOML
88
file. (This file is called Metadata.toml and is located in the same folder as environment
9-
Project.toml file.)re Not to be confused with a package registry in the sense of the
9+
Project.toml file.) Not to be confused with a package registry in the sense of the
1010
standard library, `Pkg`.
1111
1212
# Methods
1313
1414
- `GenericRegistry.dependencies(environment)`: Get a list of the environment's
1515
dependencies (vector of package name strings).
1616
17-
- [`GenericRegistry.run`](@ref): In a new Julia process, load a package (or packages) from
18-
the package environment and execute a Julia expression there; results are returned as
19-
`Future` objects, to allow asynchronous `run` calls. Useful for generating metadata about
20-
a package.
21-
22-
- `GenericRegistry.close(future)`: Shut down the process intitiated by the `run`
23-
call that returned `future` (after calling `fetch(future)` to get the result of
24-
evaluation).
25-
2617
- [`GenericRegistry.put`](@ref): Insert an item in the metadata dictionary
2718
2819
- [`GenericRegistry.get`](@ref): Inspect the metadata
@@ -31,6 +22,14 @@ standard library, `Pkg`.
3122
no longer dependencies in the environment. (In any case, `get` will return `nothing` for
3223
any `pkg` not currently a dependency.)
3324
25+
- [`GenericRegistry.run`](@ref): In a new Julia process, load a package or packages and
26+
execute a Julia expression there; results are returned as `Future` objects, to allow
27+
asynchronous `run` calls. Useful for generating metadata about a package.
28+
29+
- [`GenericRegistry.close(future)`](@ref): Shut down the process intitiated by the `run`
30+
call that returned `future` (after calling `fetch(future)` to get the result of
31+
evaluation).
32+
3433
# Example
3534
3635
```julia
@@ -44,13 +43,13 @@ Pkg.status()
4443
4544
Pkg.activate(temp=true)
4645
Pkg.add("MLJModelRegistry")
47-
using MLJModels.GenericRegistry
46+
using MLJModelRegistry.GenericRegistry
4847
packages = GenericRegistry.dependencies(env)
4948
# 2-element Vector{String}:
5049
# "Tables"
5150
# "Example"
5251
53-
future = GenericRegistry.run(:(names(Tables)), ["Tables",], env)
52+
future = GenericRegistry.run(["Tables",], :(names(Tables)))
5453
value = fetch(future)
5554
# 3-element Vector{Symbol}:
5655
# :Tables
@@ -59,8 +58,8 @@ value = fetch(future)
5958
6059
GenericRegistry.close(future)
6160
GenericRegistry.put("Tables", string.(value), env)
62-
less("/Users/anthony/MyEnv/Metadata.toml")
63-
# Tables = ["Tables", "columntable", "rowtable"]
61+
read("/Users/anthony/MyEnv/Metadata.toml", String)
62+
# "Tables = [\"Tables\", \"columntable\", \"rowtable\"]\n"
6463
6564
GenericRegistry.get("Tables", env)
6665
# 3-element Vector{String}:
@@ -78,11 +77,6 @@ import Pkg.TOML as TOML
7877

7978
# # LOGGING
8079

81-
err_missing_packages(pkgs) = ArgumentError(
82-
"One or more of the following specified packages are not "*
83-
"dependencies in the specified environment. "
84-
)
85-
8680
err_invalid_package(pkg) = ArgumentError(
8781
"The package \"$pkg\" is an invalid key, as it is "*
8882
"not a dependency in the specified environment. "
@@ -117,47 +111,44 @@ end
117111
# # METHODS
118112

119113
"""
120-
GenericRegistry.run(ex, pkgs[, environment])
114+
GenericRegistry.run([setup,] packages, program)
115+
116+
In a temporary Julia process, and using a temporary package environment, do this:
121117
122-
In a temporary Julia process, evaluate the expression `ex` after importing the specified
123-
packages, `pkgs`, using an instantiated version of the specified package `environment`,
124-
when specified. If `environment` is omitted, a fresh temporary environment is created, and
125-
populated by only the specified packages, before instantiation.
118+
1. Evaluate the `setup` expression (if present).
119+
120+
2. Add the specified `packages` to the environment, and instantiate.
121+
122+
3. Evaluate the `program` expression.
126123
127124
The returned value is a `Future` object which must be `fetch`ed to get the actual
128125
evaluated expression. Shut the temporary process down by calling `close` on the `Future`.
129126
130127
"""
131-
function run(ex, pkgs, env)
132-
pkgs isa Vector || (pkgs = [pkgs,])
133-
issubset(pkgs, dependencies(env)) || throw(err_missing_packages(pkgs))
134-
imports = [:(import $(Symbol(pkg))) for pkg in pkgs]
135-
program = quote
136-
using Pkg
137-
Pkg.activate($env)
138-
Pkg.instantiate()
139-
$(imports...)
140-
$ex
141-
end
142-
return run_in_temporary_process(program)
143-
end
144-
145-
function run(ex, pkgs)
128+
function run(setup, pkgs, program)
146129
pkgs isa Vector || (pkgs = [pkgs,])
147130
additions = [:(Pkg.add($pkg)) for pkg in pkgs]
148131
imports = [:(import $(Symbol(pkg))) for pkg in pkgs]
149132
program = quote
150133
using Pkg
151134
Pkg.activate(temp=true)
152135
$(additions...)
136+
$setup
153137
Pkg.instantiate()
154138
$(imports...)
155-
$ex
139+
$program
156140
end
157-
@show program
158141
return run_in_temporary_process(program)
159142
end
143+
run(pkgs, program) = run(:(), pkgs, program)
144+
145+
"""
146+
GenericRegistry.close(future)
147+
148+
Shut down the Julia process whose output was encapsulated by the `Future` instance,
149+
`future`.
160150
151+
"""
161152
close(future) = rmprocs(future.where)
162153

163154
"""

test/GenericRegistry.jl

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,36 +17,27 @@ import MLJModelRegistry.GenericRegistry as R
1717
n = nprocs()
1818
@suppress begin
1919

20-
# invalid list of packages:
21-
@test_throws(
22-
R.err_missing_packages(["RoguePkg", "Tables"]),
23-
R.run(:(nothing), ["RoguePkg", "Tables"], ENVIRONMENT),
24-
)
25-
2620
# bunch of runs:
27-
future1 = R.run(:(names(Tables)), ["Tables",], ENVIRONMENT)
21+
future1 = R.run(["Tables",], :(names(Tables)))
2822
future2 = R.run(
23+
:(Pkg.add("Example")),
24+
[],
2925
:(using Example; hello("Julia")),
30-
["Tables", "Pkg"],
31-
ENVIRONMENT,
3226
)
33-
future3 = R.run(:(names(Tables)), ["Tables",])
34-
future4 = R.run(
27+
future3 = R.run(
28+
"Tables",
3529
:(using Example; hello("Julia")),
36-
["Tables", "Pkg"],
3730
)
3831

3932
# fetch and test the outcomes
4033
@test issubset([:Tables, :columntable, :rowtable], fetch(future1))
4134
@test fetch(future2) == "Hello, Julia"
42-
@test issubset([:Tables, :columntable, :rowtable], fetch(future3))
43-
@test_throws(RemoteException, fetch(future4))
35+
@test_throws(RemoteException, fetch(future3))
4436

4537
# shutdown the `run` processes:
4638
R.close(future1)
4739
R.close(future2)
4840
R.close(future3)
49-
R.close(future4)
5041
end
5142
@test nprocs() == n
5243
end

0 commit comments

Comments
 (0)