From bc50544445437e4fb3fa6fdbfdefd61d0ac0bc33 Mon Sep 17 00:00:00 2001 From: Fabian Greimel Date: Wed, 31 Jul 2019 16:37:48 +0200 Subject: [PATCH 01/10] Provide formatter for labeling categories in `cut` function --- src/extras.jl | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/extras.jl b/src/extras.jl index ca7f9356..e5d66d5b 100644 --- a/src/extras.jl +++ b/src/extras.jl @@ -58,11 +58,13 @@ also accept them. values in `x`, and the upper bound is included in the last interval. * `labels::AbstractVector=[]`: a vector of strings giving the names to use for the intervals; if empty, default labels are used. +* `label_formatter::Function`: a function `f(from,to;extend=false)` that generates the labels from the left and right interval boundaries. Defaults to `string("[", from, ", ", to, extend ? "]" : ")")`, e.g. `"[1, 5)"`. * `allow_missing::Bool=true`: when `true`, values outside of breaks result in missing values. only supported when `x` accepts missing values. """ function cut(x::AbstractArray{T, N}, breaks::AbstractVector; extend::Bool=false, labels::AbstractVector{U}=String[], + label_formatter=_default_formatter_, allow_missing::Bool=false) where {T, N, U<:AbstractString} if !issorted(breaks) breaks = sort(breaks) @@ -102,12 +104,12 @@ function cut(x::AbstractArray{T, N}, breaks::AbstractVector; end levs = Vector{String}(undef, n-1) for i in 1:n-2 - levs[i] = string("[", from[i], ", ", to[i], ")") + levs[i] = label_formatter(from[i], to[i]) end if extend - levs[end] = string("[", from[end], ", ", to[end], "]") + levs[end] = label_formatter(from[end], to[end], extend=extend) else - levs[end] = string("[", from[end], ", ", to[end], ")") + levs[end] = label_formatter(from[end], to[end]) end else length(labels) == n-1 || throw(ArgumentError("labels must be of length $(n-1), but got length $(length(labels))")) @@ -128,5 +130,8 @@ Cut a numeric array into `ngroups` quantiles, determined using [`quantile`](@ref). """ cut(x::AbstractArray, ngroups::Integer; - labels::AbstractVector{U}=String[]) where {U<:AbstractString} = - cut(x, Statistics.quantile(x, (1:ngroups-1)/ngroups); extend=true, labels=labels) + labels::AbstractVector{U}=String[], label_formatter=_default_formatter_) where {U<:AbstractString} = + cut(x, Statistics.quantile(x, (1:ngroups-1)/ngroups); extend=true, labels=labels, label_formatter=label_formatter) + +_default_formatter_(from, to; extend=false) = string("[", from, ", ", to, extend ? "]" : ")") + From d1a3461dbdd17a5566503e36ad3a4bf51cd53415 Mon Sep 17 00:00:00 2001 From: Fabian Greimel Date: Sun, 15 Sep 2019 14:33:50 +0200 Subject: [PATCH 02/10] Use labels kw, group index, add tests --- src/extras.jl | 31 +++++++++++++++++++------------ test/15_extras.jl | 16 ++++++++++++++++ 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/src/extras.jl b/src/extras.jl index e5d66d5b..5381039d 100644 --- a/src/extras.jl +++ b/src/extras.jl @@ -41,6 +41,13 @@ function fill_refs!(refs::AbstractArray, X::AbstractArray{>: Missing}, end end +""" + _default_formatter_ + +Provide the default label format for the `cut` function. +""" +_default_formatter_(from, to, i; extend=false) = string("[", from, ", ", to, extend ? "]" : ")") + """ cut(x::AbstractArray, breaks::AbstractVector; extend::Bool=false, labels::AbstractVector=[], allow_missing::Bool=false) @@ -56,16 +63,16 @@ also accept them. * `extend::Bool=false`: when `false`, an error is raised if some values in `x` fall 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::AbstractVector=[]`: a vector of strings giving the names to use for the - intervals; if empty, default labels are used. -* `label_formatter::Function`: a function `f(from,to;extend=false)` that generates the labels from the left and right interval boundaries. Defaults to `string("[", from, ", ", to, extend ? "]" : ")")`, e.g. `"[1, 5)"`. +* `labels::Union{AbstractVector,Function}=_default_formatter_`: a vector of strings giving the names to use for the + intervals; or a function `f(from,to,i;extend=false)` 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)"`. * `allow_missing::Bool=true`: when `true`, values outside of breaks result in missing values. only supported when `x` accepts missing values. """ function cut(x::AbstractArray{T, N}, breaks::AbstractVector; - extend::Bool=false, labels::AbstractVector{U}=String[], - label_formatter=_default_formatter_, + extend::Bool=false, labels=_default_formatter_, allow_missing::Bool=false) where {T, N, U<:AbstractString} + (labels isa AbstractVector) || (labels isa Function) || throw(ArgumentError("labels must be a formatter function or an AbstractVector")) + if !issorted(breaks) breaks = sort(breaks) end @@ -94,7 +101,7 @@ function cut(x::AbstractArray{T, N}, breaks::AbstractVector; end n = length(breaks) - if isempty(labels) + if labels isa Function @static if VERSION >= v"0.7.0-DEV.4524" from = map(x -> sprint(show, x, context=:compact=>true), breaks[1:n-1]) to = map(x -> sprint(show, x, context=:compact=>true), breaks[2:n]) @@ -104,12 +111,12 @@ function cut(x::AbstractArray{T, N}, breaks::AbstractVector; end levs = Vector{String}(undef, n-1) for i in 1:n-2 - levs[i] = label_formatter(from[i], to[i]) + levs[i] = labels(from[i], to[i], i) end if extend - levs[end] = label_formatter(from[end], to[end], extend=extend) + levs[end] = labels(from[end], to[end], n-1, extend=extend) else - levs[end] = label_formatter(from[end], to[end]) + levs[end] = labels(from[end], to[end], n-1) end else length(labels) == n-1 || throw(ArgumentError("labels must be of length $(n-1), but got length $(length(labels))")) @@ -130,8 +137,8 @@ Cut a numeric array into `ngroups` quantiles, determined using [`quantile`](@ref). """ cut(x::AbstractArray, ngroups::Integer; - labels::AbstractVector{U}=String[], label_formatter=_default_formatter_) where {U<:AbstractString} = - cut(x, Statistics.quantile(x, (1:ngroups-1)/ngroups); extend=true, labels=labels, label_formatter=label_formatter) + labels=_default_formatter_) = + cut(x, Statistics.quantile(x, (1:ngroups-1)/ngroups); extend=true, labels=labels) + -_default_formatter_(from, to; extend=false) = string("[", from, ", ", to, extend ? "]" : ")") diff --git a/test/15_extras.jl b/test/15_extras.jl index 9e8834da..e83a194f 100644 --- a/test/15_extras.jl +++ b/test/15_extras.jl @@ -108,4 +108,20 @@ end @test levels(x) == ["[2.0, 3.5)", "[3.5, 5.0]"] end +@testset "formatter function" begin + my_formatter1(from,to,i;extend=false) = "group $i" + my_formatter2(from,to,i;extend=false) = "$i: $from -- $to" + function my_formatter3(from,to,i;extend=true) + percentile(x) = Int(round(100 * parse.(Float64,x),digits=0)) + string("P",percentile(from),"P",percentile(to)) + end + + x = collect(0.15:0.20:0.95) + p = [0, 0.4, 0.8, 1.0] + + @test cut(x, p, labels=my_formatter1) == ["group 1", "group 1", "group 2", "group 2", "group 3"] + @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_formatter3) == ["P0P40" , "P0P40" , "P40P80" , "P40P80" , "P80P100"] +end + end From b224dac725efb253926ef7205ad7c7b928ad4811 Mon Sep 17 00:00:00 2001 From: Fabian Greimel Date: Wed, 18 Sep 2019 09:33:45 +0200 Subject: [PATCH 03/10] Update src/extras.jl according to @nalimilan's comments Co-Authored-By: Milan Bouchet-Valat --- src/extras.jl | 24 ++++++++++++------------ test/15_extras.jl | 6 +++--- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/extras.jl b/src/extras.jl index 5381039d..337755af 100644 --- a/src/extras.jl +++ b/src/extras.jl @@ -46,11 +46,12 @@ end Provide the default label format for the `cut` function. """ -_default_formatter_(from, to, i; extend=false) = string("[", from, ", ", to, extend ? "]" : ")") +default_formatter(from, to, i; extend=false) = string("[", from, ", ", to, extend ? "]" : ")") """ cut(x::AbstractArray, breaks::AbstractVector; - extend::Bool=false, labels::AbstractVector=[], allow_missing::Bool=false) + labels::Union{AbstractVector{<:AbstractString},Function}, + extend::Bool=false, allow_missing::Bool=false) Cut a numeric array into intervals and return an ordered `CategoricalArray` indicating the interval into which each entry falls. Intervals are of the form `[lower, upper)`, @@ -63,15 +64,17 @@ also accept them. * `extend::Bool=false`: when `false`, an error is raised if some values in `x` fall 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}=_default_formatter_`: a vector of strings giving the names to use for the - intervals; or a function `f(from,to,i;extend=false)` 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)"`. +* `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 + left and right interval boundaries and the group index. Defaults to + `string("[", from, ", ", to, extend ? "]" : ")")`, e.g. `"[1, 5)"`. * `allow_missing::Bool=true`: when `true`, values outside of breaks result in missing values. only supported when `x` accepts missing values. """ function cut(x::AbstractArray{T, N}, breaks::AbstractVector; - extend::Bool=false, labels=_default_formatter_, - allow_missing::Bool=false) where {T, N, U<:AbstractString} - (labels isa AbstractVector) || (labels isa Function) || throw(ArgumentError("labels must be a formatter function or an AbstractVector")) + extend::Bool=false, + labels::Union{AbstractVector{<:AbstractString},Function}=default_formatter, + allow_missing::Bool=false) where {T, N} if !issorted(breaks) breaks = sort(breaks) @@ -137,8 +140,5 @@ Cut a numeric array into `ngroups` quantiles, determined using [`quantile`](@ref). """ cut(x::AbstractArray, ngroups::Integer; - labels=_default_formatter_) = - cut(x, Statistics.quantile(x, (1:ngroups-1)/ngroups); extend=true, labels=labels) - - - + labels::Union{AbstractVector{<:AbstractString},Function}=default_formatter) = + cut(x, Statistics.quantile(x, (1:ngroups-1)/ngroups); extend=true, labels=labels) \ No newline at end of file diff --git a/test/15_extras.jl b/test/15_extras.jl index e83a194f..84ec93ec 100644 --- a/test/15_extras.jl +++ b/test/15_extras.jl @@ -109,9 +109,9 @@ end end @testset "formatter function" begin - my_formatter1(from,to,i;extend=false) = "group $i" - my_formatter2(from,to,i;extend=false) = "$i: $from -- $to" - function my_formatter3(from,to,i;extend=true) + my_formatter1(from, to, i; extend) = "group $i" + my_formatter2(from, to, i; extend) = "$i: $from -- $to" + function my_formatter3(from, to, i; extend) percentile(x) = Int(round(100 * parse.(Float64,x),digits=0)) string("P",percentile(from),"P",percentile(to)) end From 7d06c34e0460ced1da01b42703731e1da757c546 Mon Sep 17 00:00:00 2001 From: Fabian Greimel Date: Wed, 18 Sep 2019 10:28:21 +0200 Subject: [PATCH 04/10] Update doc string of cut(x, ngroups) --- src/extras.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/extras.jl b/src/extras.jl index 337755af..5e00bf70 100644 --- a/src/extras.jl +++ b/src/extras.jl @@ -134,7 +134,7 @@ end """ cut(x::AbstractArray, ngroups::Integer; - labels::AbstractVector=String[]) + labels::Union{AbstractVector{<:AbstractString},Function}) Cut a numeric array into `ngroups` quantiles, determined using [`quantile`](@ref). From 88ecc2f417de6a2edab96dcf903616272fe3cd99 Mon Sep 17 00:00:00 2001 From: Fabian Greimel Date: Wed, 18 Sep 2019 15:51:14 +0200 Subject: [PATCH 05/10] remove tests, use closed i/o extend as kw --- src/extras.jl | 14 +++++--------- test/15_extras.jl | 9 +-------- 2 files changed, 6 insertions(+), 17 deletions(-) diff --git a/src/extras.jl b/src/extras.jl index 5e00bf70..13646706 100644 --- a/src/extras.jl +++ b/src/extras.jl @@ -42,11 +42,11 @@ function fill_refs!(refs::AbstractArray, X::AbstractArray{>: Missing}, end """ - _default_formatter_ + default_formatter(from, to, i; closed=false) Provide the default label format for the `cut` function. """ -default_formatter(from, to, i; extend=false) = string("[", from, ", ", to, extend ? "]" : ")") +default_formatter(from, to, i; closed=false) = string("[", from, ", ", to, closed ? "]" : ")") """ cut(x::AbstractArray, breaks::AbstractVector; @@ -114,13 +114,9 @@ function cut(x::AbstractArray{T, N}, breaks::AbstractVector; end levs = Vector{String}(undef, n-1) for i in 1:n-2 - levs[i] = labels(from[i], to[i], i) - end - if extend - levs[end] = labels(from[end], to[end], n-1, extend=extend) - else - levs[end] = labels(from[end], to[end], n-1) + levs[i] = labels(from[i], to[i], i, closed=false) end + levs[end] = labels(from[end], to[end], n-1, closed=extend) else length(labels) == n-1 || throw(ArgumentError("labels must be of length $(n-1), but got length $(length(labels))")) # Levels must have element type String for type stability of the result @@ -141,4 +137,4 @@ Cut a numeric array into `ngroups` quantiles, determined using """ cut(x::AbstractArray, ngroups::Integer; labels::Union{AbstractVector{<:AbstractString},Function}=default_formatter) = - cut(x, Statistics.quantile(x, (1:ngroups-1)/ngroups); extend=true, labels=labels) \ No newline at end of file + cut(x, Statistics.quantile(x, (1:ngroups-1)/ngroups); extend=true, labels=labels) diff --git a/test/15_extras.jl b/test/15_extras.jl index 84ec93ec..7f807aa8 100644 --- a/test/15_extras.jl +++ b/test/15_extras.jl @@ -109,19 +109,12 @@ end end @testset "formatter function" begin - my_formatter1(from, to, i; extend) = "group $i" - my_formatter2(from, to, i; extend) = "$i: $from -- $to" - function my_formatter3(from, to, i; extend) - percentile(x) = Int(round(100 * parse.(Float64,x),digits=0)) - string("P",percentile(from),"P",percentile(to)) - end + my_formatter(from, to, i; extend) = "$i: $from -- $to" x = collect(0.15:0.20:0.95) p = [0, 0.4, 0.8, 1.0] - @test cut(x, p, labels=my_formatter1) == ["group 1", "group 1", "group 2", "group 2", "group 3"] @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_formatter3) == ["P0P40" , "P0P40" , "P40P80" , "P40P80" , "P80P100"] end end From e90a02f9e3db5e804bed2cac7a2c547bd049acb3 Mon Sep 17 00:00:00 2001 From: Fabian Greimel Date: Wed, 18 Sep 2019 15:52:01 +0200 Subject: [PATCH 06/10] examples for cut in docstring --- src/extras.jl | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/src/extras.jl b/src/extras.jl index 13646706..0a8b510b 100644 --- a/src/extras.jl +++ b/src/extras.jl @@ -48,7 +48,7 @@ Provide the default label format for the `cut` function. """ default_formatter(from, to, i; closed=false) = string("[", from, ", ", to, closed ? "]" : ")") -""" +@doc raw""" cut(x::AbstractArray, breaks::AbstractVector; labels::Union{AbstractVector{<:AbstractString},Function}, extend::Bool=false, allow_missing::Bool=false) @@ -70,6 +70,64 @@ also accept them. `string("[", from, ", ", to, extend ? "]" : ")")`, e.g. `"[1, 5)"`. * `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> cut(x, [0, 1], extend=true) +5-element CategoricalArray{String,1,UInt32}: + "[-1.0, 0.0)" + "[-1.0, 0.0)" + "[0.0, 1.0]" + "[0.0, 1.0]" + "[0.0, 1.0]" + +julia> cut(x, 2) +5-element CategoricalArray{String,1,UInt32}: + "[-1.0, 0.0)" + "[-1.0, 0.0)" + "[0.0, 1.0]" + "[0.0, 1.0]" + "[0.0, 1.0]" + +julia> cut(x, 2, labels=["A", "B"]) +5-element CategoricalArray{String,1,UInt32}: + "A" + "A" + "B" + "B" + "B" + +julia> fmt(from, to, i; closed) = "grp $i ($from//$to)" +fmt (generic function with 1 method) + +julia> cut(x, 3, labels=fmt) +5-element CategoricalArray{String,1,UInt32}: + "grp 1 (-1.0//-0.333333)" + "grp 1 (-1.0//-0.333333)" + "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; extend::Bool=false, From 54e02ce663fc17378fceeb6c213e7f6914463e67 Mon Sep 17 00:00:00 2001 From: Fabian Greimel <6280307+greimel@users.noreply.github.com> Date: Wed, 18 Sep 2019 21:08:04 +0200 Subject: [PATCH 07/10] Apply suggestions from code review Co-Authored-By: Milan Bouchet-Valat --- src/extras.jl | 26 +++++++------------------- test/15_extras.jl | 5 +++-- 2 files changed, 10 insertions(+), 21 deletions(-) diff --git a/src/extras.jl b/src/extras.jl index 0a8b510b..f6657bd2 100644 --- a/src/extras.jl +++ b/src/extras.jl @@ -65,9 +65,9 @@ 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. @@ -75,7 +75,7 @@ also accept them. ```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}: @@ -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)" @@ -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" @@ -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; diff --git a/test/15_extras.jl b/test/15_extras.jl index 7f807aa8..34faa77d 100644 --- a/test/15_extras.jl +++ b/test/15_extras.jl @@ -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 From 9b7cc8726898d7e205e4871efc9c1baa829cc963 Mon Sep 17 00:00:00 2001 From: Fabian Greimel Date: Thu, 19 Sep 2019 18:05:09 +0200 Subject: [PATCH 08/10] fix test --- test/15_extras.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/15_extras.jl b/test/15_extras.jl index 34faa77d..d6e98c52 100644 --- a/test/15_extras.jl +++ b/test/15_extras.jl @@ -109,12 +109,12 @@ end end @testset "formatter function" begin - my_formatter(from, to, i; extend) = "$i: $from -- $to" + my_formatter(from, to, i; closed) = "$i: $from -- $to" x = 0.15:0.20:0.95 p = [0, 0.4, 0.8, 1.0] - @test cut(x, p, labels=my_formatter2) == + @test cut(x, p, labels=my_formatter) == ["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 From 624b7242d9c448975473da81804e0519f1be6f69 Mon Sep 17 00:00:00 2001 From: Fabian Greimel Date: Thu, 19 Sep 2019 21:04:21 +0200 Subject: [PATCH 09/10] no default argument in default formatter --- src/extras.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/extras.jl b/src/extras.jl index f6657bd2..e06b538b 100644 --- a/src/extras.jl +++ b/src/extras.jl @@ -46,7 +46,7 @@ end Provide the default label format for the `cut` function. """ -default_formatter(from, to, i; closed=false) = string("[", from, ", ", to, closed ? "]" : ")") +default_formatter(from, to, i; closed) = string("[", from, ", ", to, closed ? "]" : ")") @doc raw""" cut(x::AbstractArray, breaks::AbstractVector; From 8a0e2e539b6a636884cd065ca288c916e01714b8 Mon Sep 17 00:00:00 2001 From: Fabian Greimel <6280307+greimel@users.noreply.github.com> Date: Fri, 20 Sep 2019 09:42:12 +0200 Subject: [PATCH 10/10] Apply suggestions from code review Co-Authored-By: Milan Bouchet-Valat --- src/extras.jl | 17 ++++------------- test/15_extras.jl | 2 +- 2 files changed, 5 insertions(+), 14 deletions(-) diff --git a/src/extras.jl b/src/extras.jl index e06b538b..9a9e2c62 100644 --- a/src/extras.jl +++ b/src/extras.jl @@ -73,11 +73,7 @@ also accept them. # Examples ```jldoctest -julia> using CategoricalArrays - -julia> x = -1:0.5:1; - -julia> cut(x, [0, 1], extend=true) +julia> cut(-1:0.5:1, [0, 1], extend=true) 5-element CategoricalArray{String,1,UInt32}: "[-1.0, 0.0)" "[-1.0, 0.0)" @@ -85,9 +81,7 @@ 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) +julia> cut(-1:0.5:1, 2) 5-element CategoricalArray{String,1,UInt32}: "[-1.0, 0.0)" "[-1.0, 0.0)" @@ -95,9 +89,7 @@ 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"]) +julia> cut(-1:0.5:1, 2, labels=["A", "B"]) 5-element CategoricalArray{String,1,UInt32}: "A" "A" @@ -108,7 +100,7 @@ julia> cut(x, 2, labels=["A", "B"]) julia> fmt(from, to, i; closed) = "grp $i ($from//$to)" fmt (generic function with 1 method) -julia> cut(x, 3, labels=fmt) +julia> cut(-1:0.5:1, 3, labels=fmt) 5-element CategoricalArray{String,1,UInt32}: "grp 1 (-1.0//-0.333333)" "grp 1 (-1.0//-0.333333)" @@ -121,7 +113,6 @@ function cut(x::AbstractArray{T, N}, breaks::AbstractVector; extend::Bool=false, labels::Union{AbstractVector{<:AbstractString},Function}=default_formatter, allow_missing::Bool=false) where {T, N} - if !issorted(breaks) breaks = sort(breaks) end diff --git a/test/15_extras.jl b/test/15_extras.jl index d6e98c52..6c2b7692 100644 --- a/test/15_extras.jl +++ b/test/15_extras.jl @@ -108,7 +108,7 @@ end @test levels(x) == ["[2.0, 3.5)", "[3.5, 5.0]"] end -@testset "formatter function" begin +@testset "cut with formatter function" begin my_formatter(from, to, i; closed) = "$i: $from -- $to" x = 0.15:0.20:0.95