Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-Authored-By: Milan Bouchet-Valat <nalimilan@club.fr>
  • Loading branch information
greimel and nalimilan committed Sep 18, 2019
1 parent e90a02f commit 54e02ce
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 21 deletions.
26 changes: 7 additions & 19 deletions src/extras.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,17 @@ also accept them.
outside of the breaks; when `true`, breaks are automatically added to include all
values in `x`, and the upper bound is included in the last interval.
* `labels::Union{AbstractVector,Function}: a vector of strings giving the names to use for
the intervals; or a function `f(from, to, i; extend)` that generates the labels from the
the intervals; or a function `f(from, to, i; closed)` that generates the labels from the
left and right interval boundaries and the group index. Defaults to
`string("[", from, ", ", to, extend ? "]" : ")")`, e.g. `"[1, 5)"`.
`"[from, to)"` (or `"[from, to]"` for the rightmost interval if `extend == true`).
* `allow_missing::Bool=true`: when `true`, values outside of breaks result in missing values.
only supported when `x` accepts missing values.
# Examples
```jldoctest
julia> using CategoricalArrays
julia> x = collect(-1:0.5:1);
julia> x = -1:0.5:1;
julia> cut(x, [0, 1], extend=true)
5-element CategoricalArray{String,1,UInt32}:
Expand All @@ -85,6 +85,8 @@ julia> cut(x, [0, 1], extend=true)
"[0.0, 1.0]"
"[0.0, 1.0]"
julia> x = -1:0.5:1;
julia> cut(x, 2)
5-element CategoricalArray{String,1,UInt32}:
"[-1.0, 0.0)"
Expand All @@ -93,6 +95,8 @@ julia> cut(x, 2)
"[0.0, 1.0]"
"[0.0, 1.0]"
julia> x = -1:0.5:1;
julia> cut(x, 2, labels=["A", "B"])
5-element CategoricalArray{String,1,UInt32}:
"A"
Expand All @@ -111,22 +115,6 @@ julia> cut(x, 3, labels=fmt)
"grp 2 (-0.333333//0.333333)"
"grp 3 (0.333333//1.0)"
"grp 3 (0.333333//1.0)"
julia> using StatsBase: ecdf
julia> percentile(x) = round(Int,100*parse(Float64,x))
percentile (generic function with 1 method)
julia> fmt2(from, to, i; closed) = "P$(percentile(from))P$(percentile(to))"
fmt2 (generic function with 1 method)
julia> cut(ecdf(x)(x), 3, labels=fmt2)
5-element CategoricalArray{String,1,UInt32}:
"P20P47"
"P20P47"
"P47P73"
"P73P100"
"P73P100"
```
"""
function cut(x::AbstractArray{T, N}, breaks::AbstractVector;
Expand Down
5 changes: 3 additions & 2 deletions test/15_extras.jl
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,11 @@ end
@testset "formatter function" begin
my_formatter(from, to, i; extend) = "$i: $from -- $to"

x = collect(0.15:0.20:0.95)
x = 0.15:0.20:0.95
p = [0, 0.4, 0.8, 1.0]

@test cut(x, p, labels=my_formatter2) == ["1: 0.0 -- 0.4", "1: 0.0 -- 0.4", "2: 0.4 -- 0.8", "2: 0.4 -- 0.8", "3: 0.8 -- 1.0"]
@test cut(x, p, labels=my_formatter2) ==
["1: 0.0 -- 0.4", "1: 0.0 -- 0.4", "2: 0.4 -- 0.8", "2: 0.4 -- 0.8", "3: 0.8 -- 1.0"]
end

end

0 comments on commit 54e02ce

Please sign in to comment.