-
Notifications
You must be signed in to change notification settings - Fork 372
/
Copy pathutils.jl
199 lines (180 loc) · 4.65 KB
/
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
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
import Base: isidentifier, is_id_start_char, is_id_char
# FixMe! Incomplete list. E.g. `true` is missing
const RESERVED_WORDS = Set(["local", "global", "export", "let", "bitstype",
"typealias", "using", "for", "while", "const", "immutable",
"baremodule", "continue", "import", "function", "macro", "if", "else",
"finally", "try", "module", "elseif", "end", "begin", "quote", "do", "break",
"catch", "abstract", "return", "type", "importall"])
VERSION < v"0.6.0-dev.2194" && push!(RESERVED_WORDS, "ccall")
VERSION >= v"0.6.0-dev.2698" && push!(RESERVED_WORDS, "struct")
function identifier(s::AbstractString)
s = normalize_string(s)
if !isidentifier(s)
s = makeidentifier(s)
end
Symbol(in(s, RESERVED_WORDS) ? "_"*s : s)
end
function makeidentifier(s::AbstractString)
i = start(s)
done(s, i) && return "x"
res = IOBuffer(sizeof(s) + 1)
(c, i) = next(s, i)
under = if is_id_start_char(c)
write(res, c)
c == '_'
elseif is_id_char(c)
write(res, 'x', c)
false
else
write(res, '_')
true
end
while !done(s, i)
(c, i) = next(s, i)
if c != '_' && is_id_char(c)
write(res, c)
under = false
elseif !under
write(res, '_')
under = true
end
end
return String(take!(res))
end
function make_unique(names::Vector{Symbol}; allow_duplicates=true)
seen = Set{Symbol}()
names = copy(names)
dups = Int[]
for i in 1:length(names)
name = names[i]
in(name, seen) ? push!(dups, i) : push!(seen, name)
end
if !allow_duplicates && length(dups) > 0
d = unique(names[dups])
msg = """Duplicate variable names: $d.
Pass allow_duplicates=true to make them unique using a suffix automatically."""
throw(ArgumentError(msg))
end
for i in dups
nm = names[i]
k = 1
while true
newnm = Symbol("$(nm)_$k")
if !in(newnm, seen)
names[i] = newnm
push!(seen, newnm)
break
end
k += 1
end
end
return names
end
#' @description
#'
#' Generate standardized names for columns of a DataFrame. The
#' first name will be :x1, the second :x2, etc.
#'
#' @field n::Integer The number of names to generate.
#'
#' @returns names::Vector{Symbol} A vector of standardized column names.
#'
#' @examples
#'
#' DataFrames.gennames(10)
function gennames(n::Integer)
res = Vector{Symbol}(n)
for i in 1:n
res[i] = Symbol(@sprintf "x%d" i)
end
return res
end
#' @description
#'
#' Count the number of missing values in an Array.
#'
#' NOTE: This function always returns 0.
#'
#' @field a::Array The Array whose missing values are to be counted.
#'
#' @returns count::Int The number of missing values in `a`.
#'
#' @examples
#'
#' DataFrames.countna([1, 2, 3])
countna(a::Array) = 0
#' @description
#'
#' Count the number of missing values in a DataArray.
#'
#' @field da::DataArray The DataArray whose missing values are to be counted.
#'
#' @returns count::Int The number of missing values in `a`.
#'
#' @examples
#'
#' DataFrames.countna(@data([1, 2, 3]))
countna(da::DataArray) = sum(da.na)
#' @description
#'
#' Count the number of missing values in a PooledDataArray.
#'
#' @field pda::PooledDataArray The PooledDataArray whose missing values
#' are to be counted.
#'
#' @returns count::Int The number of missing values in `a`.
#'
#' @examples
#'
#' DataFrames.countna(@pdata([1, 2, 3]))
function countna(da::PooledDataArray)
res = 0
for i in 1:length(da)
res += da.refs[i] == 0
end
return res
end
function _setdiff{T}(a::AbstractVector{T}, b::AbstractVector{T})
diff = T[]
for val in a
if !(val in b)
push!(diff, val)
end
end
diff
end
# because unions and parametric types don't compose, yet
function _setdiff{T}(a::AbstractVector{T}, b::T)
diff = T[]
for val in a
if !(val in b)
push!(diff, val)
end
end
diff
end
function _uniqueofsorted(x::Vector)
idx = fill(true, length(x))
lastx = x[1]
for i = 2:length(x)
if lastx == x[i]
idx[i] = false
else
lastx = x[i]
end
end
x[idx]
end
# Gets the name of a function. Used in groupedataframe/grouping.jl
function _fnames{T<:Function}(fs::Vector{T})
λcounter = 0
names = map(fs) do f
name = string(f)
if name == "(anonymous function)" # Anonymous functions with Julia < 0.5
λcounter += 1
name = "λ$(λcounter)"
end
name
end
names
end