|
| 1 | +module StatTraits |
| 2 | + |
| 3 | +using ScientificTypes |
| 4 | + |
| 5 | + |
| 6 | +## CONSTANTS |
| 7 | + |
| 8 | +const TRAITS = [ |
| 9 | + :input_scitype, |
| 10 | + :output_scitype, |
| 11 | + :target_scitype, |
| 12 | + :is_pure_julia, |
| 13 | + :package_name, |
| 14 | + :package_license, |
| 15 | + :load_path, |
| 16 | + :package_uuid, |
| 17 | + :package_url, |
| 18 | + :is_wrapper, |
| 19 | + :supports_weights, |
| 20 | + :supports_online, |
| 21 | + :docstring, |
| 22 | + :name, |
| 23 | + :is_supervised, |
| 24 | + :prediction_type, |
| 25 | + :hyperparameters, |
| 26 | + :hyperparameter_types, |
| 27 | + :hyperparameter_ranges] |
| 28 | + |
| 29 | + |
| 30 | +## EXPORT |
| 31 | + |
| 32 | +for trait in TRAITS |
| 33 | + eval(:(export $trait)) |
| 34 | +end |
| 35 | + |
| 36 | + |
| 37 | +## HELPERS |
| 38 | + |
| 39 | +""" |
| 40 | +
|
| 41 | + typename(T::Type) |
| 42 | +
|
| 43 | +Return a symbol corresponding to the name of the type `T`, stripped of |
| 44 | +any type-parameters and module qualifications. For example: |
| 45 | +
|
| 46 | + _typename(MLJBase.Machine{MLJModels.ConstantRegressor,true}) |
| 47 | +
|
| 48 | +returns `:Machine`. Where this does not make sense (eg, instances of |
| 49 | +`Union`) `Symbol(string(M))` is returned. |
| 50 | +
|
| 51 | +""" |
| 52 | +function typename(M) |
| 53 | + if isdefined(M, :name) |
| 54 | + return M.name.name |
| 55 | + elseif isdefined(M, :body) |
| 56 | + return _typename(M.body) |
| 57 | + else |
| 58 | + return Symbol(string(M)) |
| 59 | + end |
| 60 | +end |
| 61 | + |
| 62 | + |
| 63 | +## TRAITS |
| 64 | + |
| 65 | +# The following can return any scientific type, that is, any type |
| 66 | +# defined in the package ScientificTypes.jl, and any ordinary type |
| 67 | +# that functions as a scientific type (eg, `Missing`). Here "target" |
| 68 | +# is a synonym for "labels", as in supervised learning; "input" is a |
| 69 | +# synonym for "features": |
| 70 | + |
| 71 | +input_scitype(::Type) = Unknown |
| 72 | +output_scitype(::Type) = Unknown |
| 73 | +target_scitype(::Type) = Unknown |
| 74 | + |
| 75 | +# The following refer to properties of the package defining a type, |
| 76 | +# for use in, say, a registry of machine learning models. All but the |
| 77 | +# first must return a string: |
| 78 | + |
| 79 | +is_pure_julia(::Type) = false # must be `true` or `false` |
| 80 | +package_name(::Type) = "unknown" |
| 81 | +package_license(::Type) = "unknown" |
| 82 | +load_path(::Type) = "unknown" |
| 83 | +package_uuid(::Type) = "unknown" |
| 84 | +package_url(::Type) = "unknown" |
| 85 | + |
| 86 | +# Below "weights" means "per-observation weights": |
| 87 | + |
| 88 | +supports_weights(::Type) = false |
| 89 | +supports_class_weights(::Type) = false |
| 90 | + |
| 91 | +# Possible values of the following are `:deterministic`, `:probabilistic`, |
| 92 | +# `:interval`, or `:unknown`: |
| 93 | + |
| 94 | +prediction_type(::Type) = :unknown # used for measures too |
| 95 | + |
| 96 | +# Miscellaneous: |
| 97 | + |
| 98 | +is_wrapper(::Type) = false # or `true` |
| 99 | +supports_online(::Type) = false # or `true` |
| 100 | +docstring(M::Type) = string(M) # some `String` |
| 101 | +is_supervised(::Type) = false # or `true` |
| 102 | + |
| 103 | +# Returns a tuple, with one entry per field of `T` (the type of some |
| 104 | +# statistical model, for example). Each entry is `nothing` or defines |
| 105 | +# some kind of default "search range" for the corresponding field (as |
| 106 | +# ordered in `fieldnames(T)`). |
| 107 | + |
| 108 | +hyperparameter_ranges(T::Type) = Tuple(fill(nothing, length(fieldnames(T)))) |
| 109 | + |
| 110 | +# Not really traits (you would not ordinarily overload them) but |
| 111 | +# included here as they are often grouped with true traits: |
| 112 | + |
| 113 | +name(M::Type) = string(typename(M)) |
| 114 | +hyperparameters(M::Type) = fieldnames(M) |
| 115 | +hyperparameter_types(M::Type) = string.(fieldtypes(M)) |
| 116 | + |
| 117 | + |
| 118 | +## MAKE TRAITS ACT ON INSTANCES AS WELL AS TYPES |
| 119 | + |
| 120 | +for trait in TRAITS |
| 121 | + ex = quote |
| 122 | + $trait(x) = $trait(typeof(x)) |
| 123 | + end |
| 124 | + eval(ex) |
| 125 | +end |
| 126 | + |
| 127 | +end # module |
0 commit comments