forked from JuliaAI/StatisticalMeasures.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.jl
More file actions
73 lines (61 loc) · 2.02 KB
/
Copy pathtools.jl
File metadata and controls
73 lines (61 loc) · 2.02 KB
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
function is_uppercase(char::Char)
i = Int(char)
i > 64 && i < 91
end
"""
snakecase(str, del='_')
Return the snake case version of the abstract string or symbol, `str`, as in
snakecase("TheLASERBeam") == "the_laser_beam"
"""
function snakecase(str::AbstractString; delim='_')
snake = Char[]
n = length(str)
for i in eachindex(str)
char = str[i]
if is_uppercase(char)
if i != 1 && i < n &&
!(is_uppercase(str[i + 1]) && is_uppercase(str[i - 1]))
push!(snake, delim)
end
push!(snake, lowercase(char))
else
push!(snake, char)
end
end
return join(snake)
end
snakecase(s::Symbol) = Symbol(snakecase(string(s)))
"""
check_pools(A::UnivariateFiniteArray, B::CategoricalArrays.CatArrOrSub)
*Private method.*
Check that the class pool of `A` coincides with the class pool of `B`, as sets. If both
`A` and `B` are ordered, check the pools have the same ordering.
If a check fails, throw an exception, and otherwise return `nothing`.
"""
function API.check_pools(
A::UnivariateFiniteArray,
B::CategoricalArrays.CatArrOrSub,
)
first_nonmissing_index = findfirst(x->!ismissing(x), A)
element_of_A = A[first_nonmissing_index]
classes_a = CategoricalArrays.levels(element_of_A)
classes_b = CategoricalArrays.levels(B)
if CategoricalArrays.isordered(A) && CategoricalArrays.isordered(B)
classes_a == classes_b || throw(API.ERR_POOL_ORDER)
else
Set(classes_a) == Set(classes_b) || throw(API.ERR_POOL)
end
return nothing
end
# Throw a warning if levels are not explicitly ordered
function warn_unordered(levels)
levels isa CategoricalArray && CategoricalArrays.isordered(levels) && return
raw_levels = CategoricalArrays.unwrap.(levels)
ret = "Levels not explicitly ordered. "*
"Using the order $raw_levels. "
if length(levels) == 2
ret *= "The \"positive\" level is $(raw_levels[2]). "
end
@warn ret
return ret
end