Skip to content

Commit 10a7315

Browse files
committed
refactor again to make REGISTRY_PATH a user-defined, external, path
1 parent bac582a commit 10a7315

File tree

11 files changed

+177
-5236
lines changed

11 files changed

+177
-5236
lines changed

registry/Metadata.toml

Lines changed: 0 additions & 5115 deletions
This file was deleted.

registry/Project.toml

Lines changed: 0 additions & 42 deletions
This file was deleted.

src/GenericRegistry.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ end
116116
Assuming a package `environment` path is specified, do the following in a new Julia
117117
process:
118118
119-
1.Activate `environment`.
119+
1. Activate `environment`.
120120
121121
2. Evaluate the `setup` expression, if specified.
122122

src/MLJModelRegistry.jl

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,42 @@
33
44
Module providing methods for managing the MLJ Model Registry. To modify the registry:
55
6-
- Create a local clone of the [MLJModelRegistry.jl
7-
repository](https://github.com/JuliaAI/MLJModelRegistry.jl)
6+
!!! important
7+
8+
In any pull request to update the Model Registry you should note the final output of
9+
`Pkg.status(outdated=true)` when you have MLJModels/registry activated.
10+
11+
- Create a local clone of [MLJModels.jl](https://github.com/JuliaAI/MLJModels.jl), which
12+
hosts the registry. After making changes, you will be making a MLJModels.jl pull
13+
request.
814
915
- Use Julia's package manager to add or remove items from the list of registered packages
10-
in the environment "/registry/", located in the root directory of your clone. If adding
11-
a new item, see the protocol below.
16+
in the environment "MLJModels/registry/". If adding a new item, see the protocol below.
17+
18+
- After adding MLJModelRegistry to some other Julia pkg environment you have activated
19+
(e.g., a fresh temporary one) run `using MLJModelRegistry` to make the management tools
20+
available.
1221
13-
- In a fresh temporary environment, run `Pkg.develop("path_to_clone")` and `using
14-
MLJModelRegistry`.
22+
- Point MLJModelRegistry to the location of the registry itself within your MLJModels.jl
23+
clone, using `setpath(path_to_registry)`, as in `setpath("MyPkgs/MLJModels/registry")`.
1524
1625
- To add or update the metadata associated with a package, run [`update(pkg)`](@ref).
1726
1827
- Assuming this is successful, update the metadata for *all* packages in the registry
1928
by running [`update()`](@ref).
2029
21-
- When satisfied, commit your changes to the clone and make a pull request to the master
22-
MLJModelRegistry.jl repository.
30+
- When satisfied, commit your changes to the clone and make a pull request to the
31+
MLJModelRegistry.jl repository that you cloned.
2332
2433
!!! note
2534
26-
Removing a package from the "/registry/" enviroment does not remove its metadata from
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.
35+
Removing a package from the registry environment does not remove its metadata (i.e.,
36+
from "/registry/Metatdata.toml"). However if you call `update()` to update all package
37+
metadata (or call [`MLJModelRegistry.ac()`](@ref)) the metadata for all orphaned
38+
packages is removed.
3039
3140
# Protocol for adding new packages to the registry environment
3241
33-
!!! important
34-
35-
In any pull request to update the Model Registry you should note the final
36-
output of `Pkg.status(outdated=true)`.
37-
3842
1. In your local clone of MLJModelRegistry.jl, `activate` the environment at "/registry/".
3943
4044
2. `update` the environment
@@ -58,14 +62,18 @@ using InteractiveUtils
5862
using Suppressor
5963
using Distributed
6064

61-
# Location of the MLJ model registry (a Julia pkg environment + metadata):
62-
const ROOT = joinpath(@__DIR__, "..")
63-
const REGISTRY = joinpath(ROOT, "registry")
64-
6565
# for controlling logging:
6666
struct Loud end
6767
struct Quiet end
6868

69+
const ROOT = joinpath(@__DIR__, "..")
70+
71+
# initializes REGISTRY_PATH to "":
72+
include("init.jl")
73+
74+
# setters and getters for REGISTRY_PATH:
75+
include("setpath.jl")
76+
6977
# The MLJ Model Registry is a special case of a "generic model registry", as described in
7078
# this file, defining the `GenericRegistry` module (which has methods, no types):
7179
include("GenericRegistry.jl")
@@ -79,6 +87,6 @@ include("remote_methods.jl")
7987
# top-level methods for registry management:
8088
include("methods.jl")
8189

82-
export update
90+
export update, setpath
8391

8492
end # module

src/init.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function __init__()
2+
global REGISTRY_PATH=Ref("")
3+
end

src/methods.jl

Lines changed: 74 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -25,34 +25,60 @@ function clean!(dic, pkg)
2525
return dic
2626
end
2727

28+
# develop MLJModelRegistry into the specifified `registry` project:
29+
function setup(registry)
30+
ex = quote
31+
# REMOVE THIS NEXT LINE AFTER TAGGING NEW MLJMODELINTERFACE
32+
Pkg.develop(path="/Users/anthony/MLJ/MLJModelInterface/")
33+
Pkg.develop(path=$ROOT) # MLJModelRegistry
34+
end
35+
future = GenericRegistry.run([], ex; environment=registry)
36+
fetch(future)
37+
GenericRegistry.close(future)
38+
end
39+
40+
# remove MLJModelRegistry from the specifified `registry` project:
41+
function cleanup(registry)
42+
ex = quote
43+
Pkg.rm("MLJModelRegistry")
44+
end
45+
future = GenericRegistry.run([], ex; environment=registry)
46+
fetch(future)
47+
GenericRegistry.close(future)
48+
end
49+
2850
"""
29-
metadata(pkg; manifest=true, check_traits=true)
51+
metadata(pkg; registry="", check_traits=true)
3052
3153
*Private method.*
3254
3355
Extract the metadata for a package. Returns a `Future` object that must be `fetch`ed to
3456
get the metadata. See, [`MLJModelRegistry.update`](@ref), which calls this method, for
3557
more details.
3658
59+
Assumes that MLJModelRegistry has been `develop`ed into `registry` if this is non-empty.
60+
3761
"""
38-
function metadata(pkg; environment=nothing, check_traits=true)
39-
if !isnothing(environment)
40-
pkg in GenericRegistry.dependencies(environment) ||
41-
throw(err_missing_package(pkg, environment))
42-
end
43-
setup = quote
44-
# REMOVE THIS NEXT LINE AFTER TAGGING NEW MLJMODELINTERFACE
45-
Pkg.develop(path="/Users/anthony/MLJ/MLJModelInterface/")
46-
Pkg.develop(path=$ROOT)
62+
function metadata(pkg; registry="", check_traits=true)
63+
if !isempty(registry)
64+
pkg in GenericRegistry.dependencies(registry) ||
65+
throw(err_missing_package(pkg, registry))
66+
setup=()
67+
else
68+
setup = quote
69+
# REMOVE THIS NEXT LINE AFTER TAGGING NEW MLJMODELINTERFACE
70+
Pkg.develop(path="/Users/anthony/MLJ/MLJModelInterface/")
71+
Pkg.develop(path=$ROOT) # MLJModelRegistry
72+
end
4773
end
4874
program = quote
4975
import MLJModelRegistry
50-
dic = MLJModelRegistry.traits_given_constructor_name(
76+
MLJModelRegistry.traits_given_constructor_name(
5177
$pkg,
5278
check_traits=$check_traits,
5379
)
5480
end
55-
return GenericRegistry.run(setup, pkg, program; environment)
81+
return GenericRegistry.run(setup, pkg, program; environment=registry)
5682
end
5783

5884

@@ -67,7 +93,7 @@ registry.
6793
This is performed automatically after `update()`, but not after `update(pkg)`.
6894
6995
"""
70-
gc() = GenericRegistry.gc(REGISTRY)
96+
gc() = GenericRegistry.gc(registry_path())
7197

7298

7399
"""
@@ -78,28 +104,27 @@ strings, and record this in the MLJ model registry (write it to
78104
`/registry/Metadata.toml`).
79105
80106
Assumes `pkg` is already a dependency in the Julia environment defined at `/registry/` and
81-
uses the version of `pkg` consistent with the current environment manifest. See
82-
documentation for details on the registration process.
107+
uses the version of `pkg` consistent with the current environment manifest, after
108+
MLJModelRegistry.jl has been `develop`ed into that environment (it is removed again after
109+
the update). See documentation for details on the registration process.
83110
84111
```julia-repl
85112
julia> update("MLJDecisionTreeInterface")
86113
```
87114
88115
# Return value
89116
90-
A set of all names of all models (more precisely, constructors) for which metadata was
91-
recorded.
117+
The metadata dictionary, keyed on models (more precisely, constructors, thereof).
92118
93119
# Advanced options
94120
95121
!!! warning
96122
97123
Advanced options are intented primarily for diagnostic purposes.
98124
99-
- `manifest=true`: Set to `false` to ignore the registry environment manifest (at
100-
`/registry/Manifest.toml`) and instead add only the specified packages to a new
101-
temporary environment. Useful to temporarily force latest versions if these are being
102-
blocked by other packages.
125+
- `manifest=true`: Set to `false` to ignore the registry environment manifest and instead
126+
add only the specified packages to a new temporary environment. Useful to temporarily
127+
force latest versions if these are being blocked by other packages.
103128
104129
- `debug=false`: Calling `update` opens a temporary Julia process to extract the trait
105130
metadata (see [`MLJModelRegistry.GenericRegistry.run`](@ref)). By default, this process
@@ -109,23 +134,26 @@ recorded.
109134
110135
"""
111136
function update(pkg; debug=false, manifest=true, check_traits=true)
112-
environment = manifest ? REGISTRY : nothing
137+
registry = manifest ? registry_path() : ""
113138
@info INFO_BE_PATIENT1
114-
update(pkg, debug ? Loud() : Quiet(), environment, check_traits)
139+
update(pkg, debug ? Loud() : Quiet(), registry, check_traits)
115140
end
116-
update(pkg, ::Loud, environment, check_traits) = _update(pkg, environment, check_traits)
117-
update(pkg, ::Quiet, environment, check_traits) =
118-
@suppress _update(pkg, environment, check_traits)
119-
function _update(pkg, environment, check_traits)
120-
future = MLJModelRegistry.metadata(pkg; environment, check_traits)
141+
update(pkg, ::Loud, registry, check_traits) = _update(pkg, true, registry, check_traits)
142+
update(pkg, ::Quiet, registry, check_traits) =
143+
@suppress _update(pkg, false, registry, check_traits)
144+
function _update(pkg, debug, registry, check_traits)
145+
isempty(registry) || setup(registry)
146+
future = MLJModelRegistry.metadata(pkg; registry, check_traits)
121147
metadata = try
122148
fetch(future)
123149
catch excptn
150+
isempty(registry) || cleanup(registry)
124151
debug || GenericRegistry.close(future)
125152
rethrow(excptn)
126153
end
127-
GenericRegistry.close(future)
128-
GenericRegistry.put(pkg, metadata, REGISTRY)
154+
isempty(registry) || cleanup(registry)
155+
debug || GenericRegistry.close(future)
156+
GenericRegistry.put(pkg, metadata, registry_path())
129157
end
130158

131159
"""
@@ -147,8 +175,10 @@ A set of all names of all packages for which metadata was recorded.
147175
updates in parallel. Metadata is extracted in parallel, but written to file
148176
sequentially.
149177
150-
- `debug=false`, `manifest=true`: These are applied as indicated above for each package
151-
added.
178+
- `debug=false`: Set to `true` to leave temporary processes open; see the `update(pkg;
179+
...)` document string above.
180+
181+
- `manifest=true`: See the `update(pkg; ...)` document string above.
152182
153183
"""
154184
function update(
@@ -158,35 +188,38 @@ function update(
158188
manifest=true,
159189
check_traits=true,
160190
)
161-
environment = manifest ? REGISTRY : nothing
162-
allpkgs = GenericRegistry.dependencies(REGISTRY)
163-
if !isnothing(environment)
164-
issubset(skip, allpkgs) || throw(err_invalid_packages(skip, environment))
191+
registry = manifest ? registry_path() : ""
192+
allpkgs = GenericRegistry.dependencies(registry_path())
193+
if !isempty(registry)
194+
issubset(skip, allpkgs) || throw(err_invalid_packages(skip, registry))
195+
@suppress setup(registry)
165196
end
166197
pkgs = setdiff(allpkgs, skip)
167198
N = length(pkgs)
168199
pos = 1
169-
@info "Processing $nworkers packages at a time. "
200+
@info "Processing up to $nworkers packages at a time. "
170201
@info INFO_BE_PATIENT10
171202
while N 1
172203
print("\rPackages remaining: $N ")
173204
n = min(nworkers, N)
174205
batch = pkgs[pos:pos + n - 1]
175206
@suppress begin
176207
futures =
177-
[MLJModelRegistry.metadata(pkg; environment, check_traits) for pkg in batch]
208+
[MLJModelRegistry.metadata(pkg; registry, check_traits) for pkg in batch]
178209
try
179210
for (i, f) in enumerate(futures)
180-
GenericRegistry.put(batch[i], fetch(f), REGISTRY)
211+
GenericRegistry.put(batch[i], fetch(f), registry_path())
181212
end
182213
catch excptn
214+
isempty(registry) || cleanup(registry)
183215
debug || GenericRegistry.close.(futures)
184216
rethrow(excptn)
185217
end
186-
GenericRegistry.close.(futures)
218+
debug || GenericRegistry.close.(futures)
187219
end
188220
N -= n
189221
end
222+
isempty(registry) || @suppress cleanup(registry)
190223
gc()
191224
println("\rPackages remaining: 0 ")
192225
return pkgs
@@ -200,4 +233,4 @@ Inspect the model trait metadata recorded in the Model Registry for those models
200233
see [`MLJModelRegistry.encode_dic`](@ref).
201234
202235
"""
203-
get(pkg) = GenericRegistry.get(pkg, REGISTRY)
236+
get(pkg) = GenericRegistry.get(pkg, registry_path())

src/setpath.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# # LOGGING
2+
3+
const ERR_REGISTRY_PATH = ErrorException(
4+
"No path to the registry has been set. Run "*
5+
"`setpath(path)`, where `path` is the path to the registry in your local "*
6+
"MLJModels.jl clone. That is, do something like "*
7+
"`setpath(\"~/MyPkgs/MLJModels/registry\")`. "
8+
)
9+
10+
# for accessing or changing the location of the registry (initially empty):
11+
function registry_path()
12+
out = REGISTRY_PATH[]
13+
isempty(out) && throw(ERR_REGISTRY_PATH)
14+
return out
15+
end
16+
setpath(path) = (REGISTRY_PATH[] = expanduser(path))

0 commit comments

Comments
 (0)