Skip to content

Commit 8940fe2

Browse files
committed
Merge branch 'tools-to-build-doc-strings' into improved-doc-string-support
2 parents bc854f5 + bd6be3d commit 8940fe2

File tree

4 files changed

+143
-1
lines changed

4 files changed

+143
-1
lines changed

Project.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ CategoricalArrays = "324d7699-5711-5eae-9e2f-1d82baa6b597"
1818
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
1919
Distances = "b4f34e82-e78d-54a5-968a-f98e89d6e8f7"
2020
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
21+
Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a"
2122
ScientificTypes = "321657f4-b219-11e9-178b-2701a2544e81"
2223
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
2324
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
2425

2526
[targets]
26-
test = ["CategoricalArrays", "DataFrames", "Distances", "InteractiveUtils", "ScientificTypes", "Tables", "Test"]
27+
test = ["CategoricalArrays", "DataFrames", "Distances", "InteractiveUtils", "Markdown", "ScientificTypes", "Tables", "Test"]

src/metadata_utils.jl

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,103 @@ function metadata_model(
132132

133133
parentmodule(T).eval(program)
134134
end
135+
136+
# TODO: After `human_name` trait is added as model trait, include in
137+
# example given in the docstring for `doc_header`.
138+
139+
"""
140+
MLJModelInterface.doc_header(SomeModelType)
141+
142+
Return a string suitable for interpolation in the document string of
143+
an MLJ model type. In the example given below, the header expands to
144+
something like this:
145+
146+
> `FooRegressor`
147+
>
148+
>Model type for foo regressor, based on [FooRegressorPkg.jl](http://existentialcomics.com/).
149+
>
150+
>From MLJ, the type can be imported using
151+
>
152+
>
153+
> `FooRegressor = @load FooRegressor pkg=FooRegressorPkg`
154+
>
155+
>Construct an instance with default hyper-parameters using the syntax
156+
>`model = FooRegressor()`. Provide keyword arguments to override
157+
>hyper-parameter defaults, as in `FooRegressor(a=...)`.
158+
159+
Ordinarily, `doc_header` is used in document strings defined *after*
160+
the model type definition, as `doc_header` assumes model traits (in
161+
particular, `package_name` and `package_url`) to be defined; see also
162+
[`MLJModelInterface.metadata_pkg`](@ref).
163+
164+
165+
### Example
166+
167+
Suppose a model type and traits have been defined by:
168+
169+
```
170+
mutable struct FooRegressor
171+
a::Int
172+
b::Float64
173+
end
174+
175+
metadata_pkg(FooRegressor,
176+
name="FooRegressorPkg",
177+
uuid="10745b16-79ce-11e8-11f9-7d13ad32a3b2",
178+
url="http://existentialcomics.com/",
179+
)
180+
metadata_model(FooRegressor,
181+
input=Table(Continuous),
182+
target=AbstractVector{Continuous},
183+
descr="La di da")
184+
```
185+
186+
Then the docstring is defined post-facto with the following code:
187+
188+
```
189+
const HEADER = MLJModelInterface.doc_header(FooRegressor)
190+
191+
@doc \"\"\"
192+
\$HEADER
193+
194+
### Training data
195+
196+
In MLJ or MLJBase, bind an instance `model` ...
197+
198+
<rest of doc string goes here>
199+
200+
\"\"\" FooRegressor
201+
```
202+
203+
"""
204+
function doc_header(SomeModelType)
205+
name = MLJModelInterface.name(SomeModelType)
206+
human_name = MLJModelInterface.human_name(SomeModelType)
207+
package_name = MLJModelInterface.package_name(SomeModelType)
208+
package_url = MLJModelInterface.package_url(SomeModelType)
209+
params = MLJModelInterface.hyperparameters(SomeModelType)
210+
211+
ret =
212+
"""
213+
$name
214+
215+
Model type for $human_name, based on [$(package_name).jl]($package_url).
216+
217+
From MLJ, the type can be imported using
218+
219+
$name = @load $name pkg=$package_name
220+
221+
Do `model = $name()` to construct an instance with default hyper-parameters.
222+
""" |> chomp
223+
224+
isempty(params) && return ret
225+
226+
p = first(params)
227+
ret *=
228+
"""
229+
Provide keyword arguments to override hyper-parameter defaults, as in
230+
`$name($p=...)`.
231+
""" |> chomp
232+
233+
return ret
234+
end

test/metadata_utils.jl

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ metadata_model(FooRegressor,
3232
supports_class_weights=true,
3333
load_path="goo goo")
3434

35+
const HEADER = MLJModelInterface.doc_header(FooRegressor)
36+
37+
@doc """
38+
$HEADER
39+
40+
Yes, we have no bananas. We have no bananas today!
41+
""" FooRegressor
42+
43+
3544
infos = Dict(trait => eval(:(MLJModelInterface.$trait))(FooRegressor) for
3645
trait in M.MODEL_TRAITS)
3746

@@ -59,3 +68,34 @@ infos = Dict(trait => eval(:(MLJModelInterface.$trait))(FooRegressor) for
5968
@test infos[:hyperparameter_types] == ("Int64", "Any")
6069
@test infos[:hyperparameter_ranges] == (nothing, nothing)
6170
end
71+
72+
@testset "doc_header(ModelType)" begin
73+
74+
# we test markdown parsed strings for less fussy comparison
75+
76+
header = Markdown.parse(HEADER)
77+
comparison =
78+
"""
79+
```
80+
FooRegressor
81+
```
82+
83+
Model type for foo regressor, based on [FooRegressorPkg.jl](http://existentialcomics.com/).
84+
85+
From MLJ, the type can be imported using
86+
87+
```
88+
FooRegressor = @load FooRegressor pkg=FooRegressorPkg
89+
```
90+
91+
Do `model = FooRegressor()` to construct an instance with default hyper-parameters.
92+
Provide keyword arguments to override hyper-parameter
93+
defaults, as in `FooRegressor(a=...)`.
94+
""" |> chomp |> Markdown.parse
95+
96+
end
97+
98+
@testset "document string" begin
99+
doc = (@doc FooRegressor) |> string |> chomp
100+
@test endswith(doc, "We have no bananas today!")
101+
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)