From 4651c697d4493f97333d6f056ce04c13506a2783 Mon Sep 17 00:00:00 2001 From: kojix2 <2xijok@gmail.com> Date: Mon, 31 May 2021 22:50:11 +0900 Subject: [PATCH] Add reverse to count --- lib/youplot/backends/processing.rb | 36 ++++++++++++++++---------- lib/youplot/backends/unicode_plot.rb | 4 +-- lib/youplot/command.rb | 2 +- lib/youplot/options.rb | 3 ++- lib/youplot/parser.rb | 9 +++++-- test/fixtures/iris-count.txt | 38 ++++++++++++++++++++++++++++ test/youplot/iris_test.rb | 25 ++++++++++++------ 7 files changed, 89 insertions(+), 28 deletions(-) create mode 100644 test/fixtures/iris-count.txt diff --git a/lib/youplot/backends/processing.rb b/lib/youplot/backends/processing.rb index ab373a9..d7bf909 100644 --- a/lib/youplot/backends/processing.rb +++ b/lib/youplot/backends/processing.rb @@ -6,22 +6,30 @@ module YouPlot module Processing module_function - def count_values(arr, tally: true) + def count_values(arr, tally: true, reverse: false) # tally was added in Ruby 2.7 - if tally && Enumerable.method_defined?(:tally) - arr.tally - else - # value_counts Enumerable::Statistics - arr.value_counts(dropna: false) - end - .sort do |a, b| - # compare values - r = b[1] <=> a[1] - # If the values are the same, compare by name - r = a[0] <=> b[0] if r == 0 - r + result = \ + if tally && Enumerable.method_defined?(:tally) + arr.tally + else + # value_counts Enumerable::Statistics + arr.value_counts(dropna: false) end - .transpose + + # sorting + result = result.sort do |a, b| + # compare values + r = b[1] <=> a[1] + # If the values are the same, compare by name + r = a[0] <=> b[0] if r == 0 + r + end + + # --reverse option + result.reverse! if reverse + + # prepare for barplot + result.transpose end end end diff --git a/lib/youplot/backends/unicode_plot.rb b/lib/youplot/backends/unicode_plot.rb index 4a9fcf3..8a2a14c 100644 --- a/lib/youplot/backends/unicode_plot.rb +++ b/lib/youplot/backends/unicode_plot.rb @@ -14,12 +14,12 @@ module YouPlot module_function - def barplot(data, params, fmt = nil, count: false) + def barplot(data, params, fmt = nil, count: false, reverse: false) headers = data.headers series = data.series # `uplot count` if count - series = Processing.count_values(series[0]) + series = Processing.count_values(series[0], reverse: reverse) params.title = headers[0] if headers end if series.size == 1 diff --git a/lib/youplot/command.rb b/lib/youplot/command.rb index ba98ba2..51dab9e 100644 --- a/lib/youplot/command.rb +++ b/lib/youplot/command.rb @@ -146,7 +146,7 @@ module YouPlot when :bar, :barplot @backend.barplot(data, params, options[:fmt]) when :count, :c - @backend.barplot(data, params, count: true) + @backend.barplot(data, params, count: true, reverse: options[:reverse]) when :hist, :histogram @backend.histogram(data, params) when :line, :lineplot diff --git a/lib/youplot/options.rb b/lib/youplot/options.rb index 1fb2a3d..8c4aba0 100644 --- a/lib/youplot/options.rb +++ b/lib/youplot/options.rb @@ -11,7 +11,8 @@ module YouPlot :fmt, :progressive, :encoding, - :color_names, + :reverse, # count + :color_names, # color :debug, keyword_init: true ) diff --git a/lib/youplot/parser.rb b/lib/youplot/parser.rb index 5d8323d..94519b3 100644 --- a/lib/youplot/parser.rb +++ b/lib/youplot/parser.rb @@ -23,6 +23,7 @@ module YouPlot fmt: 'xyy', progressive: false, encoding: nil, + reverse: false, color_names: false, debug: false ) @@ -163,7 +164,8 @@ module YouPlot end def sub_parser_add_canvas - sub_parser.on_head('--canvas STR', String, 'type of canvas') do |v| + canvas_types = UnicodePlot::Canvas::CANVAS_CLASS_MAP.keys.join(", ") + sub_parser.on_head('--canvas STR', String, "type of canvas", "(#{canvas_types})") do |v| params.canvas = v.to_sym end end @@ -226,6 +228,9 @@ module YouPlot sub_parser_add_xscale when :count, :c + sub_parser.on_head('-r', '--reverse', TrueClass, 'reverse the result of comparisons') do |v| + options.reverse = v + end sub_parser_add_symbol sub_parser_add_xscale @@ -270,7 +275,7 @@ module YouPlot sub_parser_add_xlim when :colors, :color, :colours, :colour - sub_parser.on_head('-n', '--names', 'show color names only', TrueClass) do |v| + sub_parser.on_head('-n', '--names', TrueClass, 'show color names only') do |v| options[:color_names] = v end diff --git a/test/fixtures/iris-count.txt b/test/fixtures/iris-count.txt new file mode 100644 index 0000000..7f1c9d2 --- /dev/null +++ b/test/fixtures/iris-count.txt @@ -0,0 +1,38 @@ + sepal_length + ┌ ┐ + 5.0 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 10.0 + 5.1 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 9.0 + 6.3 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 9.0 + 5.7 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■ 8.0 + 6.7 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■ 8.0 + 5.5 ┤■■■■■■■■■■■■■■■■■■■■■■■■ 7.0 + 5.8 ┤■■■■■■■■■■■■■■■■■■■■■■■■ 7.0 + 6.4 ┤■■■■■■■■■■■■■■■■■■■■■■■■ 7.0 + 4.9 ┤■■■■■■■■■■■■■■■■■■■■ 6.0 + 5.4 ┤■■■■■■■■■■■■■■■■■■■■ 6.0 + 5.6 ┤■■■■■■■■■■■■■■■■■■■■ 6.0 + 6.0 ┤■■■■■■■■■■■■■■■■■■■■ 6.0 + 6.1 ┤■■■■■■■■■■■■■■■■■■■■ 6.0 + 4.8 ┤■■■■■■■■■■■■■■■■■ 5.0 + 6.5 ┤■■■■■■■■■■■■■■■■■ 5.0 + 4.6 ┤■■■■■■■■■■■■■■ 4.0 + 5.2 ┤■■■■■■■■■■■■■■ 4.0 + 6.2 ┤■■■■■■■■■■■■■■ 4.0 + 6.9 ┤■■■■■■■■■■■■■■ 4.0 + 7.7 ┤■■■■■■■■■■■■■■ 4.0 + 4.4 ┤■■■■■■■■■■ 3.0 + 5.9 ┤■■■■■■■■■■ 3.0 + 6.8 ┤■■■■■■■■■■ 3.0 + 7.2 ┤■■■■■■■■■■ 3.0 + 4.7 ┤■■■■■■■ 2.0 + 6.6 ┤■■■■■■■ 2.0 + 4.3 ┤■■■ 1.0 + 4.5 ┤■■■ 1.0 + 5.3 ┤■■■ 1.0 + 7.0 ┤■■■ 1.0 + 7.1 ┤■■■ 1.0 + 7.3 ┤■■■ 1.0 + 7.4 ┤■■■ 1.0 + 7.6 ┤■■■ 1.0 + 7.9 ┤■■■ 1.0 + └ ┘ diff --git a/test/youplot/iris_test.rb b/test/youplot/iris_test.rb index 7db2eb0..bb9223c 100644 --- a/test/youplot/iris_test.rb +++ b/test/youplot/iris_test.rb @@ -40,6 +40,8 @@ class YouPlotIRISTest < Test::Unit::TestCase assert_equal fixture('iris-barplot.txt'), @stderr_file.read end + # barplot doesn't make sense, but just to make sure it works. + test :bar do YouPlot::Command.new(['bar', '-H', '-d,', '-t', 'IRIS-BARPLOT']).run assert_equal fixture('iris-barplot.txt'), @stderr_file.read @@ -55,6 +57,8 @@ class YouPlotIRISTest < Test::Unit::TestCase assert_equal fixture('iris-histogram.txt'), @stderr_file.read end + # Yeah, lineplot/lineplots don't make sense too. + test :lineplot do YouPlot::Command.new(['lineplot', '-H', '-d,', '-t', 'IRIS-LINEPLOT']).run assert_equal fixture('iris-lineplot.txt'), @stderr_file.read @@ -105,15 +109,20 @@ class YouPlotIRISTest < Test::Unit::TestCase assert_equal fixture('iris-boxplot.txt'), @stderr_file.read end - # test :c do - # YouPlot::Command.new(['count', '-H', '-d,']).run - # assert_equal fixture('iris-count.txt'), @stderr_file.read - # end + # Yeah, lineplot/lineplots don't make sense too. + # Just checking the behavior. - # test :count do - # YouPlot::Command.new(['c', '-H', '-d,']).run - # assert_equal fixture('iris-count.txt'), @stderr_file.read - # end + test :c do + YouPlot::Command.new(['count', '-H', '-d,']).run + assert_equal fixture('iris-count.txt'), @stderr_file.read + end + + test :count do + YouPlot::Command.new(['c', '-H', '-d,']).run + assert_equal fixture('iris-count.txt'), @stderr_file.read + end + + # Output options. test :plot_output_stdout do YouPlot::Command.new(['bar', '-o', '-H', '-d,', '-t', 'IRIS-BARPLOT']).run