From 89f5c10de0f55e0395c90b19b82c1905e5ba9e9e Mon Sep 17 00:00:00 2001 From: zzeuuus <74652697+ArunSanganal@users.noreply.github.com> Date: Sat, 4 Mar 2023 16:57:23 +0530 Subject: [PATCH 1/3] proportionmap accepts iterators --- src/counts.jl | 2 +- test/counts.jl | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/counts.jl b/src/counts.jl index d7485fd30..7007c6f31 100644 --- a/src/counts.jl +++ b/src/counts.jl @@ -450,5 +450,5 @@ Return a dictionary mapping each unique value in `x` to its proportion in `x`. If a vector of weights `wv` is provided, the proportion of weights is computed rather than the proportion of raw counts. """ -proportionmap(x::AbstractArray) = _normalize_countmap(countmap(x), length(x)) +proportionmap(x) = _normalize_countmap(countmap(x), length(collect(x))) proportionmap(x::AbstractArray, wv::AbstractWeights) = _normalize_countmap(countmap(x, wv), sum(wv)) diff --git a/test/counts.jl b/test/counts.jl index 262f2bbda..63ea95b9d 100644 --- a/test/counts.jl +++ b/test/counts.jl @@ -209,3 +209,9 @@ if VERSION >= v"1.9.0-DEV" # countmap and proportionmap only support the :dict algorithm for weighted sums. end end + +@testset "proportionmap with iterator" begin + a = [1, 2, 3, 4] + b =[true, true ,false, false, true, false] + @test proportionmap(skipmissing(a)) == Dict(1 => 0.25, 2 => 0.25, 3 => 0.25, 4 => 0.25) + @test proportionmap(skipmissing(b)) == Dict(true => 0.5, false => 0.5) \ No newline at end of file From 5f4bfd052a319547e894f5653e4c848bd8bb7c6c Mon Sep 17 00:00:00 2001 From: Arun sanganal <74652697+ArunSanganal@users.noreply.github.com> Date: Sat, 4 Mar 2023 16:58:36 +0530 Subject: [PATCH 2/3] Update counts.jl --- test/counts.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/counts.jl b/test/counts.jl index 63ea95b9d..fa30969ce 100644 --- a/test/counts.jl +++ b/test/counts.jl @@ -214,4 +214,5 @@ end a = [1, 2, 3, 4] b =[true, true ,false, false, true, false] @test proportionmap(skipmissing(a)) == Dict(1 => 0.25, 2 => 0.25, 3 => 0.25, 4 => 0.25) - @test proportionmap(skipmissing(b)) == Dict(true => 0.5, false => 0.5) \ No newline at end of file + @test proportionmap(skipmissing(b)) == Dict(true => 0.5, false => 0.5) +end From 01d2e69c4d90c48e4e8f48c4609dec22888d2d92 Mon Sep 17 00:00:00 2001 From: zzeuuus <74652697+ArunSanganal@users.noreply.github.com> Date: Sun, 5 Mar 2023 12:21:01 +0530 Subject: [PATCH 3/3] Update counts.jl --- src/counts.jl | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/counts.jl b/src/counts.jl index 7007c6f31..7cbb7a0a7 100644 --- a/src/counts.jl +++ b/src/counts.jl @@ -450,5 +450,13 @@ Return a dictionary mapping each unique value in `x` to its proportion in `x`. If a vector of weights `wv` is provided, the proportion of weights is computed rather than the proportion of raw counts. """ -proportionmap(x) = _normalize_countmap(countmap(x), length(collect(x))) +function proportionmap(x) + countm = Dict{eltype(x), Int}() + n = 0 + for y in x + countm[y] = get(countm, y, 0) + 1 + n += 1 + end + _normalize_countmap(countm, n) +end proportionmap(x::AbstractArray, wv::AbstractWeights) = _normalize_countmap(countmap(x, wv), sum(wv))