Skip to content

Commit 781e5fd

Browse files
committed
add doc_header utility for creating model docstring headers.
1 parent a496f50 commit 781e5fd

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

src/metadata_utils.jl

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,36 @@ function metadata_model(
133133
end
134134
parentmodule(T).eval(ex)
135135
end
136+
137+
function doc_header(model)
138+
name = MLJModelInterface.name(model)
139+
human_name = MLJModelInterface.human_name(model)
140+
package_name = MLJModelInterface.package_name(model)
141+
package_url = MLJModelInterface.package_url(model)
142+
params = MLJModelInterface.hyperparameters(model)
143+
144+
ret =
145+
"""
146+
$name
147+
148+
Model type for $human_name, based on [$package_name]($package_url).
149+
150+
From MLJ, the type can be imported using
151+
152+
$name = @load $name pkg=$package_name
153+
154+
Construct an instance with default hyper-parameters using the syntax
155+
`model = $name()`.
156+
""" |> chomp
157+
158+
isempty(params) && return ret
159+
160+
p = first(params)
161+
ret *=
162+
"""
163+
Provide keyword arguments to override hyper-parameter defaults, as in
164+
`$name($p=...)`.
165+
""" |> chomp
166+
167+
return ret
168+
end

test/metadata_utils.jl

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ metadata_model(FooRegressor,
2020
target=AbstractVector{Continuous},
2121
descr="La di da")
2222

23+
const HEADER = MLJModelInterface.doc_header(FooRegressor)
24+
25+
@doc """
26+
$HEADER
27+
28+
Yes, we have no bananas. We have no bananas today!
29+
""" FooRegressor
30+
2331
@testset "metadata" begin
2432
setfull()
2533
M.implemented_methods(::FI, M::Type{<:MLJType}) =
@@ -47,3 +55,34 @@ metadata_model(FooRegressor,
4755
@test infos[:hyperparameter_types] == ("Int64", "Any")
4856
@test infos[:hyperparameter_ranges] == (nothing, nothing)
4957
end
58+
59+
@testset "doc_header(model)" begin
60+
61+
# we test markdown parsed strings for less fussy comparison
62+
63+
header = Markdown.parse(HEADER)
64+
comparison =
65+
"""
66+
```
67+
FooRegressor
68+
```
69+
70+
Model type for foo regressor, based on [FooRegressorPkg](http://existentialcomics.com/).
71+
72+
From MLJ, the type can be imported using
73+
74+
```
75+
FooRegressor = @load FooRegressor pkg=FooRegressorPkg
76+
```
77+
78+
Construct an instance with default hyper-parameters using the syntax
79+
`model = FooRegressor()`. Provide keyword arguments to override hyper-parameter
80+
defaults, as in `FooRegressor(a=...)`.
81+
""" |> chomp |> Markdown.parse
82+
83+
end
84+
85+
@testset "document string" begin
86+
doc = (@doc FooRegressor) |> string |> chomp
87+
@test endswith(doc, "We have no bananas today!")
88+
end

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ using Test, MLJModelInterface
22
using ScientificTypesBase, ScientificTypes
33
using Tables, Distances, CategoricalArrays, InteractiveUtils
44
import DataFrames: DataFrame
5+
import Markdown
56

67
const M = MLJModelInterface
78
const FI = M.FullInterface

0 commit comments

Comments
 (0)