mirror of
https://github.com/red-data-tools/YouPlot.git
synced 2025-06-25 02:21:12 +08:00
Revert "Use value_counts instead of tally"
This commit is contained in:
parent
715ec8f908
commit
97d6c3e73f
24
lib/youplot/backends/processing.rb
Normal file
24
lib/youplot/backends/processing.rb
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
module YouPlot
|
||||||
|
# plotting functions.
|
||||||
|
module Backends
|
||||||
|
module Processing
|
||||||
|
module_function
|
||||||
|
|
||||||
|
def count_values(arr, tally: true)
|
||||||
|
# tally was added in Ruby 2.7
|
||||||
|
if tally && Enumerable.method_defined?(:tally)
|
||||||
|
arr.tally
|
||||||
|
else
|
||||||
|
# https://github.com/marcandre/backports
|
||||||
|
arr.each_with_object(Hash.new(0)) { |item, res| res[item] += 1 }
|
||||||
|
.tap { |h| h.default = nil }
|
||||||
|
end
|
||||||
|
.sort { |a, b| a[1] <=> b[1] }
|
||||||
|
.reverse
|
||||||
|
.transpose
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -1,5 +1,6 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require_relative 'processing'
|
||||||
require 'unicode_plot'
|
require 'unicode_plot'
|
||||||
|
|
||||||
module YouPlot
|
module YouPlot
|
||||||
@ -15,7 +16,7 @@ module YouPlot
|
|||||||
series = data.series
|
series = data.series
|
||||||
# `uplot count`
|
# `uplot count`
|
||||||
if count
|
if count
|
||||||
series = series[0].value_counts.yield_self { |h| [h.keys, h.values] }
|
series = Processing.count_values(series[0])
|
||||||
params.title = headers[0] if headers
|
params.title = headers[0] if headers
|
||||||
end
|
end
|
||||||
if series.size == 1
|
if series.size == 1
|
||||||
|
12
test/fixtures/simple-count.txt
vendored
12
test/fixtures/simple-count.txt
vendored
@ -1,12 +0,0 @@
|
|||||||
┌ ┐
|
|
||||||
-10 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 1.0
|
|
||||||
10 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 1.0
|
|
||||||
-20 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 1.0
|
|
||||||
20 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 1.0
|
|
||||||
-30 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 1.0
|
|
||||||
30 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 1.0
|
|
||||||
-40 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 1.0
|
|
||||||
40 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 1.0
|
|
||||||
-50 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 1.0
|
|
||||||
50 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 1.0
|
|
||||||
└ ┘
|
|
17
test/youplot/backends/processing_test.rb
Normal file
17
test/youplot/backends/processing_test.rb
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require_relative '../../test_helper'
|
||||||
|
|
||||||
|
class YouPlotCommandTest < Test::Unit::TestCase
|
||||||
|
test :count_values do
|
||||||
|
@m = YouPlot::Backends::Processing
|
||||||
|
assert_equal([%i[a b c], [3, 2, 1]], @m.count_values(%i[a a a b b c]))
|
||||||
|
assert_equal([%i[c b a], [3, 2, 1]], @m.count_values(%i[a b b c c c]))
|
||||||
|
end
|
||||||
|
|
||||||
|
test :count_values_non_tally do
|
||||||
|
@m = YouPlot::Backends::Processing
|
||||||
|
assert_equal([%i[a b c], [3, 2, 1]], @m.count_values(%i[a a a b b c], tally: false))
|
||||||
|
assert_equal([%i[c b a], [3, 2, 1]], @m.count_values(%i[a b b c c c], tally: false))
|
||||||
|
end
|
||||||
|
end
|
@ -112,15 +112,17 @@ class YouPlotSimpleTest < Test::Unit::TestCase
|
|||||||
assert_equal fixture('simple-boxplot.txt'), @stderr_file.read
|
assert_equal fixture('simple-boxplot.txt'), @stderr_file.read
|
||||||
end
|
end
|
||||||
|
|
||||||
test :c do
|
# test :c do
|
||||||
YouPlot::Command.new(['count']).run
|
# omit
|
||||||
assert_equal fixture('simple-count.txt'), @stderr_file.read
|
# YouPlot::Command.new(['count', '-H', '-d,']).run
|
||||||
end
|
# assert_equal fixture('iris-count.txt'), @stderr_file.read
|
||||||
|
# end
|
||||||
|
|
||||||
test :count do
|
# test :count do
|
||||||
YouPlot::Command.new(['c']).run
|
# omit
|
||||||
assert_equal fixture('simple-count.txt'), @stderr_file.read
|
# YouPlot::Command.new(['c', '-H', '-d,']).run
|
||||||
end
|
# assert_equal fixture('iris-count.txt'), @stderr_file.read
|
||||||
|
# end
|
||||||
|
|
||||||
test :plot_output_stdout do
|
test :plot_output_stdout do
|
||||||
YouPlot::Command.new(['line', '-o']).run
|
YouPlot::Command.new(['line', '-o']).run
|
||||||
|
Loading…
Reference in New Issue
Block a user