1
- """
2
- docstring_ext
3
-
4
- Internal function to help generate the docstring for a package. See
5
- [`metadata_model`](@ref).
6
- """
7
- function docstring_ext (T; descr:: String = " " )
8
- package_name = MLJModelInterface. package_name (T)
9
- package_url = MLJModelInterface. package_url (T)
10
- model_name = MLJModelInterface. name (T)
11
- # the message to return
12
- message = " $descr "
13
- message *= " \n → based on [$package_name ]($package_url )."
14
- message *= " \n → do `@load $model_name pkg=\" $package_name \" ` to " *
15
- " use the model."
16
- message *= " \n → do `?$model_name ` for documentation."
17
- end
18
-
19
1
"""
20
2
metadata_pkg(T; args...)
21
3
@@ -72,19 +54,31 @@ function metadata_pkg(
72
54
parentmodule (T). eval (ex)
73
55
end
74
56
57
+ # Extend `program` (an expression) to include trait definition for
58
+ # specified `trait` and type `T`.
59
+ function _extend! (program:: Expr , trait:: Symbol , value, T)
60
+ if value != = nothing
61
+ push! (program. args, quote
62
+ MLJModelInterface.$ trait (:: Type{<:$T} ) = $ value
63
+ end )
64
+ end
65
+ end
66
+
67
+ const WARN_MISSING_LOAD_PATH = " No `load_path` defined. "
68
+
69
+
75
70
"""
76
71
metadata_model(`T`; args...)
77
72
78
73
Helper function to write the metadata for a model `T`.
79
74
80
75
## Keywords
81
76
82
- * `input_scitype=Unknown` : allowed scientific type of the input data
83
- * `target_scitype=Unknown`: allowed sc. type of the target (supervised)
84
- * `output_scitype=Unknown`: allowed sc. type of the transformed data (unsupervised)
85
- * `supports_weights=false` : whether the model supports sample weights
86
- * `docstring=""` : short description of the model
87
- * `load_path=""` : where the model is (usually `PackageName.ModelName`)
77
+ * `input_scitype=Unknown`: allowed scientific type of the input data
78
+ * `target_scitype=Unknown`: allowed scitype of the target (supervised)
79
+ * `output_scitype=Unkonwn`: allowed scitype of the transformed data (unsupervised)
80
+ * `supports_weights=false`: whether the model supports sample weights
81
+ * `load_path="unknown"`: where the model is (usually `PackageName.ModelName`)
88
82
89
83
## Example
90
84
@@ -93,43 +87,40 @@ metadata_model(KNNRegressor,
93
87
input_scitype=MLJModelInterface.Table(MLJModelInterface.Continuous),
94
88
target_scitype=AbstractVector{MLJModelInterface.Continuous},
95
89
supports_weights=true,
96
- docstring="K-Nearest Neighbors classifier: ...",
97
90
load_path="NearestNeighbors.KNNRegressor")
98
91
```
99
92
"""
100
93
function metadata_model (
101
94
T;
102
95
# aliases:
103
- input= Unknown ,
104
- target= Unknown ,
105
- output= Unknown ,
106
- weights:: Bool = false ,
107
- descr:: String = " " ,
108
- path:: String = " " ,
96
+ input= nothing ,
97
+ target= nothing ,
98
+ output= nothing ,
99
+ weights:: Union{Nothing, Bool} = nothing ,
100
+ descr:: Union{Nothing, String} = nothing ,
101
+ path:: Union{Nothing, String} = nothing ,
109
102
110
103
# preferred names, corresponding to trait names:
111
104
input_scitype= input,
112
105
target_scitype= target,
113
106
output_scitype= output,
114
- supports_weights= weights,
115
- docstring= descr,
116
- load_path= path,
107
+ supports_weights:: Union{Nothing,Bool} = weights,
108
+ docstring:: Union{Nothing,String} = descr,
109
+ load_path:: Union{Nothing,String} = path,
117
110
)
118
- if isempty (load_path)
119
- pname = MLJModelInterface. package_name (T)
120
- mname = MLJModelInterface. name (T)
121
- load_path = " MLJModels.$(pname) _.$(mname) "
122
- end
123
- ex = quote
124
- MLJModelInterface. input_scitype (:: Type{<:$T} ) = $ input_scitype
125
- MLJModelInterface. output_scitype (:: Type{<:$T} ) = $ output_scitype
126
- MLJModelInterface. target_scitype (:: Type{<:$T} ) = $ target_scitype
127
- MLJModelInterface. supports_weights (:: Type{<:$T} ) = $ supports_weights
128
- MLJModelInterface. load_path (:: Type{<:$T} ) = $ load_path
129
-
130
- function MLJModelInterface. docstring (:: Type{<:$T} )
131
- return MLJModelInterface. docstring_ext ($ T; descr= $ docstring)
132
- end
133
- end
134
- parentmodule (T). eval (ex)
111
+ load_path === nothing && @warn WARN_MISSING_LOAD_PATH
112
+
113
+ program = quote end
114
+
115
+ # Note: Naively using metaprogramming to roll up the following
116
+ # code does not work. Only change this if you really know what
117
+ # you're doing.
118
+ _extend! (program, :input_scitype , input_scitype, T)
119
+ _extend! (program, :target_scitype , target_scitype, T)
120
+ _extend! (program, :output_scitype , output_scitype, T)
121
+ _extend! (program, :supports_weights , supports_weights, T)
122
+ _extend! (program, :docstring , docstring, T)
123
+ _extend! (program, :load_path , load_path, T)
124
+
125
+ parentmodule (T). eval (program)
135
126
end
0 commit comments