Repository: JuliaAI/MLJBase.jl
Pull request #611 (2021-08-17 03:12:29)

Add better scitype check for machine constructor when model is not standard
Builds on https://github.com/JuliaAI/MLJModelInterface.jl/pull/109


Patch:
diff --git a/Project.toml b/Project.toml
index 3b324d08..d97dc361 100644
--- a/Project.toml
+++ b/Project.toml
@@ -1,7 +1,7 @@
 name = "MLJBase"
 uuid = "a7f614a8-145f-11e9-1d2a-a57a1082229d"
 authors = ["Anthony D. Blaom <anthony.blaom@gmail.com>"]
-version = "0.18.17"
+version = "0.18.18"
 
 [deps]
 CategoricalArrays = "324d7699-5711-5eae-9e2f-1d82baa6b597"
@@ -33,14 +33,14 @@ ComputationalResources = "0.3"
 Distributions = "0.22, 0.23, 0.24, 0.25"
 InvertedIndices = "1"
 LossFunctions = "0.5, 0.6, 0.7"
-MLJModelInterface = "1.1.3"
+MLJModelInterface = "1.2"
 Missings = "0.4, 1"
 OrderedCollections = "1.1"
 Parameters = "0.12"
 PrettyTables = "1"
 ProgressMeter = "1.7.1"
-ScientificTypes = "2"
-StatisticalTraits = "2"
+ScientificTypes = "2.1"
+StatisticalTraits = "2.1"
 StatsBase = "0.32, 0.33"
 Tables = "0.2, 1.0"
 julia = "1"
diff --git a/src/machines.jl b/src/machines.jl
index 58c8bb4f..84b54376 100644
--- a/src/machines.jl
+++ b/src/machines.jl
@@ -92,6 +92,12 @@ warn_scitype(model::Supervised, X) =
     "`model=$model`:\nscitype(X) = $(elscitype(X))\n"*
     "input_scitype(model) = $(input_scitype(model))."
 
+warn_generic_scitype_mismatch(S, F) =
+    "The scitype of `args` in `machine(model, args...; kwargs)` "*
+    "does not match the scitype "*
+    "expected by model's `fit` method.\n"*
+    "  provided: $S\n  expected by fit: $F"
+
 warn_scitype(model::Supervised, X, y) =
     "The scitype of `y`, in `machine(model, X, y, ...)` "*
     "is incompatible with "*
@@ -111,8 +117,19 @@ err_length_mismatch(model::Supervised) = DimensionMismatch(
 check(model::Any, args...; kwargs) =
     throw(ArgumentError("Expected a `Model` instance, got $model. "))
 
-check(model::Model, args...; kwargs) =
-    @warn "Unrecognized model type $(typeof(model)). Forgoing all checks. "
+function check(model::Model, args...; full=false)
+
+    nowarns = true
+
+    F = fit_data_scitype(model)
+    F == Unknown && return true
+
+    S = Tuple{elscitype.(args)...}
+    if !(S <: F)
+        @warn warn_generic_scitype_mismatch(S, F)
+        nowarns = false
+    end
+end
 
 function check(model::Supervised, args... ; full=false)
 


Test patch:
diff --git a/test/machines.jl b/test/machines.jl
index d852d658..dd01c5bf 100644
--- a/test/machines.jl
+++ b/test/machines.jl
@@ -59,6 +59,24 @@ freeze!(stand)
                machine(tree, (x=categorical(1:N),), y))
 end
 
+struct FooBar <: Model end
+
+MLJBase.fit_data_scitype(::Type{<:FooBar}) =
+    Union{Tuple{AbstractVector{Count}},
+          Tuple{AbstractVector{Count},AbstractVector{Continuous}}}
+
+@testset "machine argument check for generic model" begin
+    X = [1, 2, 3, 4]
+    y = rand(4)
+    model = FooBar()
+    @test_logs machine(model, X, y)
+    @test_logs machine(model, X)
+    @test_logs((:warn,
+                MLJBase.warn_generic_scitype_mismatch(Tuple{scitype(y)},
+                                                      fit_data_scitype(model))),
+                machine(model, y))
+end
+
 @testset "weights" begin
     yraw = ["Perry", "Antonia", "Perry", "Skater"]
     X = (x=rand(4),)