Skip to content

Commit 9cf959d

Browse files
tlienartaviatesk
andauthored
Adding schema to the interface (#40)
* schema interafce Co-authored-by: Shuhei Kadowaki <[email protected]>
1 parent 8c346f0 commit 9cf959d

File tree

5 files changed

+44
-5
lines changed

5 files changed

+44
-5
lines changed

.travis.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ language: julia
33
os:
44
- linux
55
julia:
6-
- 1.1
7-
- 1.2
8-
- 1.3
6+
- 1.0
97
- 1.4
108
- nightly
119
matrix:

Project.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "MLJModelInterface"
22
uuid = "e80e1ace-859a-464e-9ed9-23947d8ae3ea"
33
authors = ["Thibaut Lienart and Anthony Blaom"]
4-
version = "0.2.2"
4+
version = "0.2.3"
55

66
[deps]
77
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
@@ -16,9 +16,10 @@ CategoricalArrays = "324d7699-5711-5eae-9e2f-1d82baa6b597"
1616
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
1717
Distances = "b4f34e82-e78d-54a5-968a-f98e89d6e8f7"
1818
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
19+
MLJBase = "a7f614a8-145f-11e9-1d2a-a57a1082229d"
1920
MLJScientificTypes = "2e2323e0-db8b-457b-ae0d-bdfb3bc63afd"
2021
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
2122
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
2223

2324
[targets]
24-
test = ["Test", "Tables", "Distances", "CategoricalArrays", "InteractiveUtils", "DataFrames", "MLJScientificTypes"]
25+
test = ["Test", "Tables", "Distances", "CategoricalArrays", "InteractiveUtils", "DataFrames", "MLJScientificTypes", "MLJBase"]

src/data_utils.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,23 @@ classes(x) = classes(get_interface_mode(), x)
123123

124124
classes(::LightInterface, x) = errlight("classes")
125125

126+
# ------------------------------------------------------------------------
127+
# schema
128+
129+
"""
130+
schema(X)
131+
132+
If `X` is tabular, inspect the column types and scitypes, otherwise return
133+
`nothing`.
134+
"""
135+
schema(X; kw...) = schema(get_interface_mode(), vtrait(X), X; kw...)
136+
137+
schema(::Mode, ::Val{:other}, X; kw...) = nothing
138+
139+
schema(::LightInterface, ::Val{:other}, X; kw...) = errlight("schema")
140+
141+
schema(::LightInterface, ::Val{:table}, X; kw...) = errlight("schema")
142+
126143
# ------------------------------------------------------------------------
127144
# decoder
128145

test/data_utils.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,28 @@ end
5858
@test classes(x[1]) == ['a', 'b']
5959
end
6060
# ------------------------------------------------------------------------
61+
@testset "schema-light" begin
62+
# throw error for any input anyway
63+
setlight()
64+
ary = rand(10, 3)
65+
@test_throws M.InterfaceError M.schema(ary)
66+
df = DataFrame(rand(10, 3))
67+
@test_throws M.InterfaceError M.schema(df)
68+
end
69+
@testset "schema-full" begin
70+
setfull()
71+
ary = rand(10, 3)
72+
@test M.schema(ary) === nothing
73+
M.schema(::FI, ::Val{:table}, X; kw...) = MLJBase.schema(X; kw...) # this would be defined in MLJBase.jl
74+
df = DataFrame(A = rand(10), B = categorical(rand('a':'c', 10)))
75+
sch = M.schema(df)
76+
@test sch.names == (:A, :B)
77+
@test sch.types[1] <: Float64
78+
@test sch.types[2] <: CategoricalValue
79+
@test sch.scitypes[1] <: Continuous
80+
@test sch.scitypes[2] <: Multiclass
81+
end
82+
# ------------------------------------------------------------------------
6183
@testset "decoder-light" begin
6284
setlight()
6385
x = 5

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 ScientificTypes, MLJScientificTypes
33
using Tables, Distances, CategoricalArrays, InteractiveUtils
44
import DataFrames: DataFrame
5+
import MLJBase
56

67
const M = MLJModelInterface
78
const FI = M.FullInterface

0 commit comments

Comments
 (0)