-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathtest_utils.jl
165 lines (142 loc) · 4.95 KB
/
test_utils.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
### TRAITS CHECKS ###########
# see https://github.com/JuliaML/MLUtils.jl/issues/67
numobs_df(x) = numobs(x)
getobs_df(x, i) = getobs(x, i)
getobs_df(x::NamedTuple, i) = map(y -> getobs_df(y, i), x)
getobs_df(x::DataFrame, i) = x[i, :]
numobs_df(x::DataFrame) = size(x, 1)
function test_inmemory_supervised_table_dataset(d::D;
n_obs, n_features, n_targets,
feature_names=nothing, target_names=nothing,
Tx=Any, Ty=Any) where {D<:SupervisedDataset}
@test hasfield(D, :metadata)
@test hasfield(D, :features)
@test hasfield(D, :targets)
@test hasfield(D, :dataframe)
@test d.metadata isa Dict{String, Any}
as_df = d.features isa DataFrame
if as_df
@test d.features isa DataFrame
@test d.targets isa DataFrame
@test d.dataframe isa DataFrame
@test isempty(intersect(names(d.features), names(d.targets)))
@test size(d.dataframe) == (n_obs, n_features + n_targets)
@test size(d.features) == (n_obs, n_features)
@test size(d.targets) == (n_obs, n_targets)
for c in names(d.dataframe)
if c in names(d.targets)
@test d.dataframe[!, c] == d.targets[!,c]
else
@test d.dataframe[!, c] == d.features[!,c]
end
end
if feature_names !== nothing
@test names(d.features) == feature_names
end
if target_names !== nothing
@test names(d.targets) == target_names
end
else
@test d.features isa Matrix{<:Tx}
@test d.targets isa Matrix{<:Ty}
@test d.dataframe === nothing
@test size(d.features) == (n_features, n_obs)
@test size(d.targets) == (n_targets, n_obs)
if feature_names !== nothing
@test d.metadata["feature_names"] == feature_names
end
if target_names !== nothing
@test d.metadata["target_names"] == target_names
end
end
@test d[:] === (; d.features, d.targets)
@test length(d) == n_obs
@test numobs(d) == n_obs
idx = rand(1:n_obs)
@test isequal(d[idx], getobs_df((; d.features, d.targets), idx))
@test isequal(d[idx], getobs(d, idx))
idxs = rand(1:n_obs, 2)
@test isequal(d[idxs], getobs_df((; d.features, d.targets), idxs))
@test isequal(d[idxs], getobs(d, idxs))
end
function test_supervised_array_dataset(d::D;
n_obs, n_features, n_targets,
Tx=Any, Ty=Any,
conv2img=false) where {D<:SupervisedDataset}
if n_features isa Int
@assert n_features != 0 "use n_features=() if you don't want features dimensions"
Nx == 2
else # tuple
Nx = length(n_features) + 1
end
Ny = map(x -> x == 1 ? 1 : 2, n_targets)
@test d.features isa Array{Tx, Nx}
@test size(d.features) == (n_features..., n_obs)
if Ny isa NamedTuple
for k in keys(Ny)
ny = Ny[k]
@test d.targets[k] isa Array{Ty, ny}
if ny == 1
@test size(d.targets[k]) == (n_obs,)
else
@test size(d.targets[k]) == (ny, n_obs)
end
end
else
@assert Ny isa Int
if Ny == 1
@test size(d.targets) == (n_obs,)
else
@test size(d.targets) == (Ny, n_obs)
end
end
@test length(d) == n_obs
@test numobs(d) == n_obs
X, y = d[:]
@test X === d.features
@test y === d.targets
idx = rand(1:n_obs)
@test isequal(d[idx], getobs((; d.features, d.targets), idx))
@test isequal(d[idx], getobs(d, idx))
idxs = rand(1:n_obs, 2)
@test isequal(d[idxs], getobs((; d.features, d.targets), idxs))
@test isequal(d[idxs], getobs(d, idxs))
if conv2img
img = convert2image(d, 1)
@test img isa AbstractArray{<:Color}
x = d[1].features
@test convert2image(D, x) == img
@test convert2image(d, x) == img
end
end
function test_unsupervised_array_dataset(d::D;
n_obs, n_features,
Tx=Any,
conv2img=false) where {D<:UnsupervisedDataset}
n_features = n_features === nothing ? () : n_features
if n_features isa Int
@assert n_features != 0 "use n_features = () if you don't want features dimensions"
Nx == 2
else # tuple
Nx = length(n_features) + 1
end
@test d.features isa Array{Tx, Nx}
@test size(d.features) == (n_features..., n_obs)
@test length(d) == n_obs
@test numobs(d) == n_obs
X = d[:]
@test X === d.features
idx = rand(1:n_obs)
@test isequal(d[idx], getobs(d.features, idx))
@test isequal(d[idx], getobs(d, idx))
idxs = rand(1:n_obs, 2)
@test isequal(d[idxs], getobs(d.features, idxs))
@test isequal(d[idxs], getobs(d, idxs))
if conv2img
img = convert2image(d, 1)
@test img isa AbstractArray{<:Color}
x = d[1].features
@test convert2image(D, x) == img
@test convert2image(d, x) == img
end
end