Skip to content

Commit 9925fe0

Browse files
committed
add final trait tests
1 parent ca07406 commit 9925fe0

File tree

5 files changed

+65
-10
lines changed

5 files changed

+65
-10
lines changed

Project.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1212
MLUtils = "f1d291b0-491e-4a28-83b9-f70985020b54"
1313
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
1414
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
15+
ScientificTypes = "321657f4-b219-11e9-178b-2701a2544e81"
16+
ScientificTypesBase = "30f210dd-8aff-4c5f-94ba-8e64358c1161"
1517
Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
1618
StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3"
1719
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
@@ -27,6 +29,8 @@ LearnAPI = "0.1.0"
2729
LinearAlgebra = "<0.0.1, 1"
2830
MLUtils = "0.4"
2931
Random = "<0.0.1, 1"
32+
ScientificTypes = "3.0.2"
33+
ScientificTypesBase = "3.0.0"
3034
Serialization = "<0.0.1, 1"
3135
StableRNGs = "1"
3236
Statistics = "<0.0.1, 1"

src/LearnTestAPI.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ import StableRNGs
3434
import InteractiveUtils
3535
import MacroTools
3636
import IsURL
37+
import ScientificTypes
38+
39+
# duplication of import/using statements in files at src/learners not appearing above, for
40+
# test learners:
41+
import Distributions
42+
using ScientificTypesBase
43+
using Tables
44+
using LinearAlgebra
45+
using Random
46+
using Statistics
47+
using UnPack
3748

3849
include("tools.jl")
3950
include("logging.jl")

src/learners/ensembling.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ using LinearAlgebra
88
using Random
99
using Statistics
1010
using UnPack
11-
11+
using ScientificTypesBase
1212

1313
# # ENSEMBLE OF REGRESSORS (A MODEL WRAPPER)
1414

@@ -542,6 +542,8 @@ end
542542
iteration_parameter = :ntrees,
543543
kinds_of_proxy = (Point(),),
544544
tags = ("regression", "ensembling", "iterative algorithms"),
545+
fit_scitype = Tuple{AbstractVector{Continuous},AbstractVector{Continuous}},
546+
target_observation_scitype = Continuous,
545547
functions = (
546548
:(LearnAPI.fit),
547549
:(LearnAPI.learner),

src/logging.jl

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,12 +402,23 @@ const HUMAN_NAME = """
402402
Checking that `LearnAPI.human_name(learner)` is a string.
403403
404404
"""
405+
const FIT_SCITYPE = """
405406
406-
function MISSING_TRAITS(missing_traits)
407+
Checking that `data` supplied for testing satisfies the constraints articulated by
408+
`LearnAPI.fit_scitype(learner)`.
409+
410+
"""
411+
const TARGET_OBSERVATION_SCITYPE = """
412+
413+
Checking that `LearnAPI.target(learner, observatoins)` satisifies the constraints
414+
articulated by `LearnAPI.target_observation_scitype(learner)`.
415+
416+
"""
417+
function MISSING_TRAITS(missing_traits, name)
407418
list = join( map(ex->"`$ex`",collect(missing_traits)), ", ", " and ")
408419
return """
409420
410-
The following optional traits have not been overloaded:
421+
The following traits, deemed optional for $name, have not been overloaded:
411422
412423
$list.
413424

src/testapi.jl

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
@testapi learner dataset1, dataset2 ... verbosity=1
2+
@testapi learner dataset1 dataset2 ... verbosity=1
33
44
Test that `learner` correctly implements the LearnAPI.jl interface, by checking
55
contracts against one or more data sets.
@@ -37,15 +37,20 @@ The following are *not* tested:
3737
3838
- That the output of `LearnAPI.target(learner, data)` is indeed a target, in the sense
3939
that it can be paired, in some way, with the output of `predict`. Such a test would be
40-
to suitably pair the output with a predicted proxy for the target, using, say, a proper
41-
scoring rule, in the case of probabilistic predictions.
40+
to suitably pair the output with a predicted proxy for the target, using, for example, a
41+
proper scoring rule, in the case of probabilistic predictions.
4242
4343
- That `inverse_transform` is an approximate left or right inverse to `transform`
4444
4545
- That the one-line convenience methods, `transform(learner, ...)` or `predict(learner,
4646
...)`, where implemented, have the same effect as the two-line calls they combine.
4747
48-
- The veracity of `is_pure_julia(learner)`.
48+
- The veracity of `LearnAPI.is_pure_julia(learner)`.
49+
50+
- The second of the two contracts appearing in the
51+
[`LearnAPI.target_observation_scitype`](@extref) docstring. The first contract is only
52+
tested if `LearnAPI.data_interface(learner)` is `LearnAPI.RandomAccess()` or
53+
`LearnAPI.FiniteIterable()`.
4954
5055
Whenever the internal `learner` algorithm involves case distinctions around data or
5156
hyperparameters, it is recommended that multiple datasets, and learners with a variety of
@@ -637,12 +642,34 @@ macro testapi(learner, data...)
637642
end
638643

639644
@logged_testset $HUMAN_NAME verbosity begin
640-
human_name = LearnAPI.human_name(learner)
641-
Test.@test human_name isa String
645+
Test.@test _human_name isa String
646+
end
647+
648+
@logged_testset $FIT_SCITYPE verbosity begin
649+
S = LearnAPI.fit_scitype(learner)
650+
if S == Union{}
651+
push!(missing_traits, :(LearnAPI.fit_scitype))
652+
else
653+
Test.@test ScientificTypes.scitype(data) <: S
654+
end
655+
end
656+
657+
S = LearnAPI.target_observation_scitype(learner)
658+
testable = :(LearnAPI.target) in _functions &&
659+
_data_interface in (LearnAPI.RandomAccess(), LearnAPI.FiniteIterable())
660+
if S == Any
661+
push!(missing_traits, :(LearnAPI.target_observation_scitype))
662+
elseif testable
663+
@logged_testset $TARGET_OBSERVATION_SCITYPE verbosity begin
664+
Test.@test all([o for o in _y]) do o
665+
ScientificTypes.scitype(o) <: S
666+
end
667+
end
642668
end
643669

644670
end # for loop over datasets
645-
verbosity > 0 && !isempty(missing_traits) && @info $MISSING_TRAITS(missing_traits)
671+
verbosity > 0 && !isempty(missing_traits) &&
672+
@info $MISSING_TRAITS(missing_traits, _human_name)
646673
verbosity > 0 && @info "------ @testapi - $_human_name - tests complete ------"
647674
nothing
648675
end # quote

0 commit comments

Comments
 (0)