Skip to content

Commit 824e05c

Browse files
authored
Merge pull request #134 from JuliaAI/dev
For a 1.3.5 release
2 parents 597e2dd + 2a77b27 commit 824e05c

File tree

5 files changed

+86
-10
lines changed

5 files changed

+86
-10
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ jobs:
1818
matrix:
1919
version:
2020
- '1.0'
21+
- '1.6'
2122
- '1'
2223
os:
2324
- ubuntu-latest

Project.toml

Lines changed: 1 addition & 1 deletion
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 = "1.3.4"
4+
version = "1.3.5"
55

66
[deps]
77
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

src/MLJModelInterface.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ end
9393

9494
# data operations
9595
export matrix, int, classes, decoder, table,
96-
nrows, selectrows, selectcols, select, info
96+
nrows, selectrows, selectcols, select, info, scitype
9797

9898
# equality
9999
export is_same_except, isrepresented

src/data_utils.jl

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,41 @@ classes(x) = classes(get_interface_mode(), x)
151151

152152
classes(::LightInterface, x) = errlight("classes")
153153

154+
# ------------------------------------------------------------------------
155+
# scitype
156+
157+
"""
158+
scitype(X)
159+
160+
The scientific type (interpretation) of `X`, distinct from its
161+
machine type.
162+
163+
### Examples
164+
```julia
165+
julia> scitype(3.14)
166+
Continuous
167+
168+
julia> scitype([1, 2, missing])
169+
AbstractVector{Union{Missing, Count}}
170+
171+
julia> scitype((5, "beige"))
172+
Tuple{Count, Textual}
173+
174+
julia> using CategoricalArrays
175+
176+
julia> X = (gender = categorical(['M', 'M', 'F', 'M', 'F']),
177+
ndevices = [1, 3, 2, 3, 2]);
178+
179+
julia> scitype(X)
180+
Table{Union{AbstractVector{Count}, AbstractVector{Multiclass{2}}}}
181+
```
182+
"""
183+
scitype(X) = scitype(get_interface_mode(), vtrait(X, "scitype"), X)
184+
185+
function scitype(::LightInterface, m, X)
186+
return errlight("scitype")
187+
end
188+
154189
# ------------------------------------------------------------------------
155190
# schema
156191

@@ -187,14 +222,15 @@ istable(::Mode, ::Val{:table}) = true
187222
# decoder
188223

189224
"""
190-
d = decoder(x)
225+
decoder(x)
191226
192-
A callable object for decoding the integer representation of a
227+
Return a callable object for decoding the integer representation of a
193228
`CategoricalString` or `CategoricalValue` sharing the same pool as
194229
`x`. (Here `x` is of one of these two types.) Specifically, one has
195-
`d(int(y)) == y` for all `y in classes(x)`. One can also call `d` on
196-
integer arrays, in which case `d` is broadcast over all elements.
230+
`decoder(x)(int(y)) == y` for all `y in classes(x)`. One can also call `decoder(x)` on
231+
integer arrays, in which case `decoder(x)` is broadcast over all elements.
197232
233+
### Examples
198234
```julia
199235
julia> v = categorical(["c", "b", "c", "a"])
200236
4-element CategoricalArrays.CategoricalArray{String,1,UInt32}:

test/data_utils.jl

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
x = 1:5
44
@test_throws M.InterfaceError M.categorical(x)
55
end
6+
67
@testset "cat-full" begin
78
setfull()
89
M.categorical(::FI, a...; kw...) = categorical(a...; kw...)
910
x = 1:5
1011
@test M.categorical(x) == categorical(x)
1112
end
13+
1214
# ------------------------------------------------------------------------
1315
@testset "matrix-light" begin
1416
setlight()
@@ -25,18 +27,21 @@ end
2527
X = (a=[1, 2, 3], b=[1, 2, 3])
2628
@test_throws M.InterfaceError matrix(X)
2729
end
30+
2831
@testset "matrix-full" begin
2932
setfull()
3033
M.matrix(::FI, ::Val{:table}, X; kw...) = Tables.matrix(X; kw...)
3134
X = (a=[1, 2, 3], b=[1, 2, 3])
3235
@test matrix(X) == hcat([1, 2, 3], [1, 2, 3])
3336
end
37+
3438
# ------------------------------------------------------------------------
3539
@testset "int-light" begin
3640
setlight()
3741
x = categorical([1, 2, 3])
3842
@test_throws M.InterfaceError int(x)
3943
end
44+
4045
@testset "int-full" begin
4146
setfull()
4247
M.int(::FI, x::CategoricalValue) = CategoricalArrays.refcode(x)
@@ -61,6 +66,31 @@ end
6166
x = categorical(['a','b','a'])
6267
@test classes(x[1]) == ['a', 'b']
6368
end
69+
70+
# ------------------------------------------------------------------------
71+
@testset "scitype-light" begin
72+
# throw error for any input anyway
73+
setlight()
74+
75+
ary = rand(10, 3)
76+
@test_throws M.InterfaceError M.scitype(ary)
77+
78+
df = DataFrame(rand(10, 3), :auto)
79+
@test_throws M.InterfaceError M.scitype(df)
80+
end
81+
82+
@testset "scitype-full" begin
83+
setfull()
84+
M.scitype(::FI, v, X) = ScientificTypes.scitype(X)
85+
86+
ary = rand(10, 3)
87+
@test M.scitype(ary) == AbstractArray{Continuous, 2}
88+
89+
df = DataFrame(A = rand(10), B = categorical(rand('a':'c', 10)))
90+
sch = M.scitype(df)
91+
@test sch <: Table(Continuous, Multiclass)
92+
end
93+
6494
# ------------------------------------------------------------------------
6595
@testset "schema-light" begin
6696
# throw error for any input anyway
@@ -70,14 +100,14 @@ end
70100
df = DataFrame(rand(10, 3), :auto)
71101
@test_throws M.InterfaceError M.schema(df)
72102
end
103+
73104
@testset "schema-full" begin
74105
setfull()
106+
M.schema(::FI, v, X) = ScientificTypes.schema(X)
107+
75108
ary = rand(10, 3)
76-
M.schema(::FI, ::Val{:table}, X; kw...) =
77-
ScientificTypes.schema(X; kw...)
78-
M.schema(::FI, ::Val{:other}, X; kw...) = nothing
109+
@test_throws ArgumentError M.schema(ary)
79110

80-
@test M.schema(ary) === nothing
81111
df = DataFrame(A = rand(10), B = categorical(rand('a':'c', 10)))
82112
sch = M.schema(df)
83113
@test sch.names == (:A, :B)
@@ -86,6 +116,7 @@ end
86116
@test sch.scitypes[1] <: Continuous
87117
@test sch.scitypes[2] <: Multiclass
88118
end
119+
89120
# ------------------------------------------------------------------------
90121
@testset "istable" begin
91122
# Nothing stops someone from implementing a Tables.jl
@@ -106,24 +137,28 @@ end
106137
X = DataFrame(A=rand(10))
107138
@test M.istable(X)
108139
end
140+
109141
# ------------------------------------------------------------------------
110142
@testset "decoder-light" begin
111143
setlight()
112144
x = 5
113145
@test_throws M.InterfaceError decoder(x)
114146
end
147+
115148
@testset "decoder-full" begin
116149
setfull()
117150
# toy test because I don't want to copy the decoder logic here
118151
M.decoder(::FI, x) = 0
119152
@test decoder(nothing) == 0
120153
end
154+
121155
# ------------------------------------------------------------------------
122156
@testset "table-light" begin
123157
setlight()
124158
X = ones(3, 2)
125159
@test_throws M.InterfaceError table(X)
126160
end
161+
127162
@testset "table-full" begin
128163
setfull()
129164
function M.table(::FI, A::AbstractMatrix; names=nothing)
@@ -135,13 +170,15 @@ end
135170
@test Tables.istable(T)
136171
@test Tables.matrix(T) == X
137172
end
173+
138174
# ------------------------------------------------------------------------
139175
@testset "nrows-light" begin
140176
setlight()
141177
X = (a=[4, 2, 1], b=[3, 2, 1])
142178
@test_throws M.InterfaceError nrows(X)
143179
@test nrows(nothing) == 0
144180
end
181+
145182
@testset "nrows-full" begin
146183
setfull()
147184
X = ones(5)
@@ -157,6 +194,7 @@ end
157194
X = (a=[4, 2, 1], b=[3, 2, 1])
158195
@test nrows(X) == 3
159196
end
197+
160198
# ------------------------------------------------------------------------
161199
@testset "select-light" begin
162200
setlight()
@@ -179,6 +217,7 @@ end
179217
@test_throws M.InterfaceError selectcols(X, 1)
180218
@test_throws M.InterfaceError select(X, 1, 1)
181219
end
220+
182221
@testset "select-full" begin
183222
setfull()
184223

0 commit comments

Comments
 (0)