Skip to content

Commit

Permalink
Update src/extras.jl according to @nalimilan's comments
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 d1a3461 commit b224dac
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
24 changes: 12 additions & 12 deletions src/extras.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)`,
Expand All @@ -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)
Expand Down Expand Up @@ -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)
6 changes: 3 additions & 3 deletions test/15_extras.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b224dac

Please sign in to comment.