@@ -5,38 +5,78 @@ using LearnBase: getobs, nobs, default_obsdim
55@test typeof (LearnBase. gettargets) <: Function
66@test typeof (LearnBase. datasubset) <: Function
77
8- @testset " getobs" begin
9-
10- function LearnBase. getobs (x:: AbstractArray{T,N} , idx; obsdim= default_obsdim (x)) where {T,N}
11- _idx = ntuple (i-> i == obsdim ? idx : Colon (), N)
12- return x[_idx... ]
8+ @testset " getobs and nobs" begin
9+
10+ @testset " array" begin
11+ a = rand (2 ,3 )
12+ @test nobs (a) == 3
13+ @test @inferred getobs (a, 1 ) == a[:,1 ]
14+ @test @inferred getobs (a, 2 ) == a[:,2 ]
15+ @test @inferred getobs (a, 1 : 2 ) == a[:,1 : 2 ]
16+ @test @inferred getobs (a, 1 , obsdim= 1 ) == a[1 ,:]
17+ @test @inferred getobs (a, 2 , obsdim= 1 ) == a[2 ,:]
18+ @test @inferred getobs (a, 2 , obsdim= nothing ) ≈ a[:,2 ]
19+ end
20+
21+ @testset " tuple" begin
22+ # A dataset with 3 observations, each with 2 input features
23+ X, Y = rand (2 , 3 ), rand (3 )
24+ dataset = (X, Y)
25+ @test nobs (dataset) == 3
26+ if VERSION >= v " 1.6"
27+ o = @inferred getobs (dataset, 2 )
28+ else
29+ o = getobs (dataset, 2 )
30+ end
31+ @test o[1 ] == X[:,2 ]
32+ @test o[2 ] == Y[2 ]
33+
34+ if VERSION >= v " 1.6"
35+ o = @inferred getobs (dataset, 1 : 2 )
36+ else
37+ o = getobs (dataset, 1 : 2 )
38+ end
39+
40+ @test o[1 ] == X[:,1 : 2 ]
41+ @test o[2 ] == Y[1 : 2 ]
42+ end
43+
44+
45+ @testset " named tuple" begin
46+ X, Y = rand (2 , 3 ), rand (3 )
47+ dataset = (x= X, y= Y)
48+ @test nobs (dataset) == 3
49+ if VERSION >= v " 1.6"
50+ o = @inferred getobs (dataset, 2 )
51+ else
52+ o = getobs (dataset, 2 )
53+ end
54+ @test o. x == X[:,2 ]
55+ @test o. y == Y[2 ]
56+
57+ if VERSION >= v " 1.6"
58+ o = @inferred getobs (dataset, 1 : 2 )
59+ else
60+ o = getobs (dataset, 1 : 2 )
61+ end
62+ @test o. x == X[:,1 : 2 ]
63+ @test o. y == Y[1 : 2 ]
64+ end
65+
66+ @testset " dict" begin
67+ X, Y = rand (2 , 3 ), rand (3 )
68+ dataset = Dict (" X" => X, " Y" => Y)
69+ @test nobs (dataset) == 3
70+
71+ # o = @inferred getobs(dataset, 2) # not inferred
72+ o = getobs (dataset, 2 )
73+ @test o[" X" ] == X[:,2 ]
74+ @test o[" Y" ] == Y[2 ]
75+
76+ o = getobs (dataset, 1 : 2 )
77+ @test o[" X" ] == X[:,1 : 2 ]
78+ @test o[" Y" ] == Y[1 : 2 ]
1379 end
14- LearnBase. nobs (x:: AbstractArray ; obsdim= default_obsdim (x)) = size (x, obsdim)
15-
16- a = rand (2 ,3 )
17- @test nobs (a) == 3
18- @test getobs (a, 1 ) ≈ a[:,1 ]
19- @test getobs (a, 2 ) ≈ a[:,2 ]
20- @test getobs (a, 1 , obsdim= 1 ) ≈ a[1 ,:]
21- @test getobs (a, 2 , obsdim= 1 ) ≈ a[2 ,:]
22-
23- # Here we use Ref to protect idx against broadcasting
24- LearnBase. getobs (t:: Tuple , idx) = getobs .(t, Ref (idx))
25- # Assume all elements have the same nummber of observations.
26- # It would be safer to check explicitely though.
27- LearnBase. nobs (t:: Tuple ) = nobs (t[1 ])
28-
29- # A dataset with 3 observations, each with 2 input features
30- X, Y = rand (2 , 3 ), rand (3 )
31- dataset = (X, Y)
32-
33- o = getobs (dataset, 2 ) # -> (X[:,2], Y[2])
34- @test o[1 ] ≈ X[:,2 ]
35- @test o[2 ] == Y[2 ]
36-
37- o = getobs (dataset, 1 : 2 ) # -> (X[:,1:2], Y[1:2])
38- @test o[1 ] ≈ X[:,1 : 2 ]
39- @test o[2 ] == Y[1 : 2 ]
4080end
4181
4282
0 commit comments