@@ -12,11 +12,13 @@ an array based on rules
12
12
autotype, `only_changes` should be true.
13
13
* `rules=(:few_to_finite,)`: the set of rules to apply.
14
14
"""
15
- autotype (X; kw... ) = _autotype (X, Val (ST . trait (X) ); kw... )
15
+ autotype (X; kw... ) = _autotype (X, vtrait (X ); kw... )
16
16
17
17
# For an array object (trait:other)
18
- function _autotype (X:: Arr , :: Val{:other} ;
19
- rules:: NTuple{N,Symbol} where N= (:few_to_finite ,))
18
+ function _autotype (
19
+ X:: Arr , :: Val{:other} ;
20
+ rules:: NTuple{N,Symbol} where N = (:few_to_finite ,)
21
+ )
20
22
# check that the rules are recognised
21
23
_check_rules (rules)
22
24
# inspect the current element scitype
@@ -36,8 +38,11 @@ function _autotype(X::Arr, ::Val{:other};
36
38
end
37
39
38
40
# For a table object (trait:table)
39
- function _autotype (X, :: Val{:table} ; only_changes:: Bool = true ,
40
- rules:: NTuple{N,Symbol} where N= (:few_to_finite ,))
41
+ function _autotype (
42
+ X, :: Val{:table} ;
43
+ only_changes:: Bool = true ,
44
+ rules:: NTuple{N,Symbol} where N = (:few_to_finite ,)
45
+ )
41
46
# check that the rules are recognised
42
47
_check_rules (rules)
43
48
# recuperate the schema of `X`
@@ -59,7 +64,7 @@ function _autotype(X, ::Val{:table}; only_changes::Bool=true,
59
64
# doesn't really matter, there are few rules and sugg is fast
60
65
sugg_type = stype
61
66
for rule in rules
62
- sugg_type = eval (:($ rule ($ sugg_type, $ col, $ sch . nrows)))
67
+ sugg_type = eval (:($ rule ($ sugg_type, $ col, $ ( nrows (X)) )))
63
68
end
64
69
# store the suggested type
65
70
suggested_types[name] = sugg_type
@@ -76,16 +81,49 @@ function _autotype(X, ::Val{:table}; only_changes::Bool=true,
76
81
end
77
82
78
83
# convenience functions to pass a single rule at the time
79
- autotype (X, rule:: Symbol ; args... ) =
80
- autotype (X; rules= (rule,), args... )
84
+ autotype (X, rule:: Symbol ; args... ) = autotype (X; rules= (rule,), args... )
81
85
# convenience function to splat rules
82
- autotype (X, rules:: NTuple{N,Symbol} where N; args... ) =
83
- autotype (X; rules= rules, args... )
86
+ autotype (X, rules:: NTuple{N,Symbol} where N; args... ) = autotype (X; rules= rules, args... )
87
+
88
+ """
89
+ nrows(X)
90
+
91
+ Helper method to return the number of rows a table `X` has.
92
+
93
+ **Note**
94
+ A more general version of this method is defined in `MLJModelInterface.jl`.
95
+ This method is needed here in order for `auto_type` method to run.
96
+ """
97
+ function nrows (X)
98
+ if ! Tables. istable (X)
99
+ throw (ArgumentError (" input argument must be a Tables.jl compatible table" ))
100
+ end
101
+ if Tables. rowaccess (X)
102
+ rows = Tables. rows (X)
103
+ return _nrows_rat (Base. IteratorSize (typeof (rows)), rows)
104
+
105
+ else
106
+ cols = Tables. columns (X)
107
+ return _nrows_cat (cols)
108
+ end
109
+ end
110
+
111
+ # number of rows for columnaccessed table
112
+ function _nrows_cat (cols)
113
+ names = Tables. columnnames (cols)
114
+ ! isempty (names) || return 0
115
+ return length (Tables. getcolumn (cols, names[1 ]))
116
+ end
117
+
118
+ # number of rows for rowaccessed table
119
+ _nrows_rat (:: Base.HasShape , rows) = size (rows, 1 )
120
+ _nrows_rat (:: Base.HasLength , rows) = length (rows)
121
+ _nrows_rat (iter_size, rows) = length (collect (rows))
84
122
85
123
# -----------------------------------------------------------------
86
124
# rules
87
125
88
- function _check_rules (rules:: NTuple{N,Symbol} where N)
126
+ function _check_rules (rules:: NTuple{N, Symbol} where N)
89
127
for rule in rules
90
128
rule in (:few_to_finite ,
91
129
:discrete_to_continuous ,
@@ -161,15 +199,14 @@ end
161
199
Helper function to suggest a finite type corresponding to `T` when there are
162
200
few unique values.
163
201
"""
164
- function sugg_finite (:: Type{<:Union{Missing,T}} ) where T
165
- T <: Real && return OrderedFactor
166
- return Multiclass
202
+ function sugg_finite (type:: Type )
203
+ return ifelse (nonmissing (type) <: Real , OrderedFactor, Multiclass)
167
204
end
168
205
169
206
"""
170
207
T_or_Union_Missing_T(type, T)
171
208
172
- Helper function to return either `T` or `Union{Missing,T}`.
209
+ Helper function to return either `T` or `Union{Missing, T}`.
173
210
"""
174
211
function T_or_Union_Missing_T (type:: Type , T:: Type )
175
212
return ifelse (type >: Missing , Union{Missing, T}, T)
0 commit comments