-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcoerce.jl
242 lines (195 loc) · 7.23 KB
/
coerce.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
const ColKey = Union{Symbol, AbstractString}
"""
coerce(A, S)
Return new version of the array `A` whose scientific element type is `S`.
```
julia> v = coerce([3, 7, 5], Continuous)
3-element Vector{Float64}:
3.0
7.0
5.0
julia> scitype(v)
AbstractVector{Continuous}
```
coerce(X, specs...; tight=false, verbosity=1)
Given a table `X`, return a copy of `X`, ensuring that the element
scitypes of the columns match the new specification, `specs`. There
are three valid specifications:
(i) one or more `column_name=>Scitype` pairs:
coerce(X, col1=>Scitype1, col2=>Scitype2, ... ; verbosity=1)
(ii) one or more `OldScitype=>NewScitype` pairs (`OldScitype` covering
both the `OldScitype` and `Union{Missing,OldScitype}` cases):
coerce(X, OldScitype1=>NewScitype1, OldScitype2=>NewScitype2, ... ; verbosity=1)
(iii) a dictionary of scientific types keyed on column names:
coerce(X, d::AbstractDict{<:ColKey, <:Type}; verbosity=1)
where `ColKey = Union{Symbol,AbstractString}`.
### Examples
Specifying `column_name=>Scitype` pairs:
```
using CategoricalArrays, DataFrames, Tables
X = DataFrame(name=["Siri", "Robo", "Alexa", "Cortana"],
height=[152, missing, 148, 163],
rating=[1, 5, 2, 1])
Xc = coerce(X, :name=>Multiclass, :height=>Continuous, :rating=>OrderedFactor)
schema(Xc).scitypes # (Multiclass, Continuous, OrderedFactor)
```
Specifying `OldScitype=>NewScitype` pairs:
```
X = (x = [1, 2, 3],
y = rand(3),
z = [10, 20, 30])
Xc = coerce(X, Count=>Continuous)
schema(Xfixed).scitypes # (Continuous, Continuous, Continuous)
```
"""
coerce(X, a...; kw...) = coerce(vtrait(X), X, a...; kw...)
# Non tabular data is not supported
coerce(::Val{:other}, X, a...; kw...) =
throw(CoercionError("`coerce` is undefined for non-tabular data."))
_bad_dictionary() = throw(ArgumentError(
"A dictionary specifying a scitype conversion "*
"must have type `AbstractDict{<:ColKey, <:Type}`. It's keys must "*
"be column names and its values be scientific types. "*
"E.g., `Dict(:cats=>Continuous, :dogs=>Textual`. "))
coerce(::Val{:table}, X, types_dict::AbstractDict; kw...) =
_bad_dictionary()
_bad_specs() =
throw(ArgumentError(
"Invalid `specs` in `coerce(X, specs...; kwargs...)`. "*
"Valid `specs` are: (i) one or more pairs of "*
"the form `column_name=>Scitype`; (ii) one or more pairs "*
"of the from `OldScitype=>NewScitype`; or (iii) a "*
"dictionary of scientific "*
"types keyed on column names. "))
coerce(::Val{:table}, X, specs...; kw...) = _bad_specs()
function coerce(::Val{:table},
X,
types_dict::AbstractDict{<:ColKey, <:Type};
kw...)
isempty(types_dict) && return X
names = schema(X).names
X_ct = Tables.columntable(X)
ct_new = (_coerce_col(X_ct, col, types_dict; kw...) for col in names)
return Tables.materializer(X)(NamedTuple{names}(ct_new))
end
# -------------------------------------------------------------
# utilities for coerce
struct CoercionError <: Exception
m::String
end
function _coerce_col(Xcol,
name,
types_dict::AbstractDict{Symbol, <:Type};
kw...)
y = Tables.getcolumn(Xcol, name)
if haskey(types_dict, name)
coerce_type = types_dict[name]
return coerce(y, coerce_type; kw...)
end
return y
end
# -------------------------------------------------------------
# alternative ways to do coercion, both for coerce and coerce!
# The following extends the two methods so that a mixture of
# symbol=>type and type=>type pairs can be specified in place of a
# dictionary:
feature_scitype_pairs(p::Pair{<:ColKey,<:Type}, X) = [Symbol(first(p)) => last(p), ]
function feature_scitype_pairs(p::Pair{<:Type,<:Type}, X)
from_scitype = first(p)
to_scitype = last(p)
sch = schema(X)
ret = Pair{Symbol,Type}[]
for j in eachindex(sch.names)
if sch.scitypes[j] <: Union{Missing,from_scitype}
push!(ret, Pair(sch.names[j], to_scitype))
end
end
return ret
end
for c in (:coerce, :coerce!)
ex = quote
function $c(::Val{:table},
X,
mixed_pairs::Pair{<:Union{<:ColKey,<:Type},<:Type}...;
kw...)
components = map(p -> feature_scitype_pairs(p, X), mixed_pairs)
pairs = vcat(components...)
# must construct dictionary by hand to check no conflicts:
scitype_given_feature = Dict{Symbol,Type}()
for p in pairs
feature = first(p)
if haskey(scitype_given_feature, feature)
throw(ArgumentError("`coerce` argments cannot be "*
"resolved to determined a "*
"*unique* scitype for each "*
"feature. "))
else
scitype_given_feature[feature] = last(p)
end
end
return $c(X, scitype_given_feature; kw...)
end
end
eval(ex)
end
# -------------------------------------------------------------
# In place coercion
"""
coerce!(X, ...)
Same as [`ScientificTypes.coerce`](@ref) except it does the
modification in place provided `X` supports in-place modification (eg,
DataFrames). An error is thrown otherwise. The arguments are the same
as `coerce`.
"""
coerce!(X, a...; kw...) = begin
coerce!(vtrait(X), X, a...; kw...)
end
coerce!(::Val{:other}, X, a...; kw...) =
throw(CoercionError("`coerce!` is undefined for non-tabular data."))
coerce!(::Val{:table}, X, types_dict::AbstractDict; kw...) =
_bad_dictionary()
coerce!(::Val{:table}, X, specs...; kw...) = _bad_specs()
function coerce!(::Val{:table},
X,
types_dict::AbstractDict{<:ColKey, <:Type};
kw...)
# DataFrame --> coerce_df!
if is_type(X, :DataFrames, :DataFrame)
return coerce_df!(X, types_dict; kw...)
end
# Everything else
throw(ArgumentError("In place coercion not supported for $(typeof(X))." *
"Try `coerce` instead."))
end
# -------------------------------------------------------------
# utilities for coerce!
"""
coerce_df!(df, pairs...; kw...)
In place coercion for a dataframe.(Unexported method)
"""
function coerce_df!(df, tdict::AbstractDict{<:ColKey, <:Type}; kw...)
names = schema(df).names
for name in names
name in keys(tdict) || continue
coerce_type = tdict[name]
df[!, name] = coerce(df[!, name], coerce_type; kw...)
end
return df
end
"""
is_type(X, spkg, stype)
Check that an object `X` is of a given type that may be defined in a package
that is not loaded in the current environment.
As an example say `DataFrames` is not loaded in the current environment, a
function from some package could still return a DataFrame in which case it
can be checked with
```
is_type(X, :DataFrames, :DataFrame)
```
"""
function is_type(X, spkg::Symbol, stype::Symbol)
# If the package is loaded, then it will just be `stype`
# otherwise it will be `spkg.stype`
rx = Regex("^($spkg\\.)?$stype")
return ifelse(match(rx, "$(typeof(X))") === nothing, false, true)
end