mirror of
https://github.com/red-data-tools/YouPlot.git
synced 2025-09-19 02:18:08 +08:00
Compare commits
26 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
49626d54e1 | ||
![]() |
f824135653 | ||
![]() |
2eca86268c | ||
![]() |
85d818d8ca | ||
![]() |
b484a3a96f | ||
![]() |
7e97d44caf | ||
![]() |
d30942fef6 | ||
![]() |
72f59887de | ||
![]() |
2005eaf871 | ||
![]() |
6edd66662d | ||
![]() |
977d3beb2d | ||
![]() |
f57090cc9b | ||
![]() |
cfbbc33286 | ||
![]() |
74f7a2435d | ||
![]() |
801c2a4920 | ||
![]() |
8f52955c19 | ||
![]() |
a4e747969f | ||
![]() |
9e15c0ac2a | ||
![]() |
97d6c3e73f | ||
![]() |
715ec8f908 | ||
![]() |
607e490d7a | ||
![]() |
86c0d8ed53 | ||
![]() |
29dfe424e7 | ||
![]() |
7c42d9a444 | ||
![]() |
54b70f1f27 | ||
![]() |
69167a6f44 |
42
README.md
42
README.md
@@ -1,6 +1,6 @@
|
||||
<p align="center">
|
||||
<img src="logo.svg" width="60%" height="60%" />
|
||||
</7>
|
||||
</p>
|
||||
|
||||

|
||||
[](https://badge.fury.io/rb/youplot)
|
||||
@@ -106,8 +106,8 @@ In this example, YouPlot counts the number of chromosomes where the gene is loca
|
||||
|
||||
```sh
|
||||
cat gencode.v35.annotation.gff3 \
|
||||
| grep -v '#' | grep 'gene' | cut -f1 | \
|
||||
uplot count -t "The number of human gene annotations per chromosome" -c blue
|
||||
| grep -v '#' | grep 'gene' | cut -f1 \
|
||||
| uplot count -t "The number of human gene annotations per chromosome" -c blue
|
||||
```
|
||||
|
||||
<p align="center">
|
||||
@@ -119,7 +119,7 @@ This is fine in most cases, as long as the data size is small. If you want to vi
|
||||
|
||||
```sh
|
||||
cat gencode.v35.annotation.gff3 | grep -v '#' | grep 'gene' | cut -f1 \
|
||||
|sort | uniq -c | sort -nrk2 | awk '{print $2,$1}' \
|
||||
| sort | uniq -c | sort -nrk2 | awk '{print $2,$1}' \
|
||||
| uplot bar -d ' ' -t "The number of human gene annotations per chromosome" -c blue
|
||||
```
|
||||
|
||||
@@ -130,15 +130,15 @@ cat gencode.v35.annotation.gff3 | grep -v '#' | grep 'gene' | cut -f1 \
|
||||
Wouldn't it be a pain to have to run R, Python, Julia, gnuplot or whatever REPL just to check your data?
|
||||
YouPlot is a command line tool for this purpose. With YouPlot, you can continue working without leaving your terminal and shell.
|
||||
|
||||
### how to use YouPlot?
|
||||
### How to use YouPlot?
|
||||
|
||||
`uplot` is the shortened form of `youplot`. You can use either.
|
||||
|
||||
| | |
|
||||
|-----------------------------------|------------------------------------------------|
|
||||
| Reads data from standard input | `cat data.tsv \| uplot <command> [options]` |
|
||||
| Reads data from files | `uplot <command> [options] data.tsv ...` |
|
||||
| Outputs data from stdin to stdout | `pipeline1 \| uplot <command> -O \| pipeline2` |
|
||||
| Command | Description |
|
||||
|------------------------------------------------|-----------------------------------|
|
||||
| `cat data.tsv \| uplot <command> [options]` | Take input from stdin |
|
||||
| `uplot <command> [options] data.tsv ...` | Take input from files |
|
||||
| `pipeline1 \| uplot <command> -O \| pipeline2` | Outputs data from stdin to stdout |
|
||||
|
||||
### Where to output the plot?
|
||||
|
||||
@@ -148,7 +148,8 @@ The output file or stream for the plot can be specified with the `-o` option.
|
||||
### Where to output the input data?
|
||||
|
||||
By default, the input data is not shown anywhere.
|
||||
The `-O` option, with no arguments, outputs the input data directly to the standard output. This is useful when passing data to a subsequent pipeline.
|
||||
The `-O` option, with no arguments, outputs the input data directly to the standard output.
|
||||
This is useful when passing data to a subsequent pipeline.
|
||||
|
||||
### What types of plots are available?
|
||||
|
||||
@@ -176,11 +177,21 @@ If your input data contains a header line, you need to specify the `-H` option.
|
||||
|
||||
### How to specify the delimiter?
|
||||
|
||||
Use the `-d` option. To specify a blank space, you can use `uplot bar -d ' ' data.txt`. You do not need to use `-d` option for tab-delimited text since the default value is tab.
|
||||
Use the `-d` option. To specify a blank space, you can use `uplot bar -d ' ' data.txt`.
|
||||
You do not need to use `-d` option for tab-delimited text since the default value is tab.
|
||||
|
||||
### Is there a way to specify a column as the x-axis or y-axis?
|
||||
|
||||
Not yet. In principle, YouPlot treats the first column as the X axis and the second column as the Y axis. When working with multiple series, the first row is the X axis, the second row is series 1, the third row is series 2, and so on. If you pass only one column of data for `line` and `bar`, YouPlot will automatically use a sequential number starting from 1 as the X-axis. The `--fmt xyy`, `--fmt xyxy` and `--fmt yx` options give you a few more choices. See `youplot <command> --help` for more details. YouPlot has limited functionalities, but you can use shell scripts such as `awk '{print $2, $1}'` to swap lines.
|
||||
Not yet.
|
||||
YouPlot treats the first column as the X axis and the second column as the Y axis.
|
||||
When working with multiple series, the first column is the X axis, the second column is series Y1, the third column is series Y2, and so on.
|
||||
If you pass only one column of data for `line` and `bar`, YouPlot will automatically use a sequential number starting from 1 as the X-axis.
|
||||
|
||||
* `--fmt xyy` `--fmt xyxy` `--fmt yx` options give you a few more choices.
|
||||
See `youplot <command> --help` for more details.
|
||||
|
||||
* Use `awk '{print $2, $1}'` to swap lines.
|
||||
* Use `paste` to concatenate series.
|
||||
|
||||
### How to plot real-time data?
|
||||
|
||||
@@ -216,14 +227,15 @@ uplot colors
|
||||
|
||||
## Contributing
|
||||
|
||||
YouPlot is a library under development, so even small improvements like typofix are welcome! Please feel free to send us your pull requests.
|
||||
YouPlot is a library under development, so even small improvements like typofix are welcome!
|
||||
Please feel free to send us your pull requests.
|
||||
|
||||
* [Report bugs](https://github.com/kojix2/youplot/issues)
|
||||
* Fix bugs and [submit pull requests](https://github.com/kojix2/youplot/pulls)
|
||||
* Write, clarify, or fix documentation
|
||||
* English corrections by native speakers are welcome.
|
||||
* Suggest or add new features
|
||||
|
||||
* Make a donation
|
||||
|
||||
### Development
|
||||
|
||||
|
@@ -1,9 +1,9 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'unicode_plot'
|
||||
require 'youplot/version'
|
||||
require 'youplot/dsv'
|
||||
require 'youplot/command'
|
||||
require_relative 'youplot/version'
|
||||
require_relative 'youplot/dsv'
|
||||
require_relative 'youplot/parameters'
|
||||
require_relative 'youplot/command'
|
||||
|
||||
module YouPlot
|
||||
class << self
|
||||
|
@@ -11,12 +11,16 @@ module YouPlot
|
||||
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 }
|
||||
# value_counts Enumerable::Statistics
|
||||
arr.value_counts(dropna: false)
|
||||
end
|
||||
.sort { |a, b| a[1] <=> b[1] }
|
||||
.reverse
|
||||
.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
|
||||
.transpose
|
||||
end
|
||||
end
|
||||
|
@@ -1,12 +1,15 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# UnicodePlot - Plot your data by Unicode characters
|
||||
# https://github.com/red-data-tools/unicode_plot.rb
|
||||
|
||||
require_relative 'processing'
|
||||
require 'unicode_plot'
|
||||
|
||||
module YouPlot
|
||||
# plotting functions.
|
||||
module Backends
|
||||
module UnicodePlotBackend
|
||||
module UnicodePlot
|
||||
class Error < StandardError; end
|
||||
|
||||
module_function
|
||||
@@ -39,7 +42,7 @@ module YouPlot
|
||||
labels = series[x_col]
|
||||
values = series[y_col].map(&:to_f)
|
||||
end
|
||||
UnicodePlot.barplot(labels, values, **params.to_hc)
|
||||
::UnicodePlot.barplot(labels, values, **params.to_hc)
|
||||
end
|
||||
|
||||
def histogram(data, params)
|
||||
@@ -47,7 +50,7 @@ module YouPlot
|
||||
series = data.series
|
||||
params.title ||= data.headers[0] if headers
|
||||
values = series[0].map(&:to_f)
|
||||
UnicodePlot.histogram(values, **params.to_hc)
|
||||
::UnicodePlot.histogram(values, **params.to_hc)
|
||||
end
|
||||
|
||||
def line(data, params, fmt = nil)
|
||||
@@ -57,7 +60,7 @@ module YouPlot
|
||||
# If there is only one series, it is assumed to be sequential data.
|
||||
params.ylabel ||= headers[0] if headers
|
||||
y = series[0].map(&:to_f)
|
||||
UnicodePlot.lineplot(y, **params.to_hc)
|
||||
::UnicodePlot.lineplot(y, **params.to_hc)
|
||||
else
|
||||
# If there are 2 or more series...
|
||||
if fmt == 'yx'
|
||||
@@ -75,7 +78,7 @@ module YouPlot
|
||||
end
|
||||
x = series[x_col].map(&:to_f)
|
||||
y = series[y_col].map(&:to_f)
|
||||
UnicodePlot.lineplot(x, y, **params.to_hc)
|
||||
::UnicodePlot.lineplot(x, y, **params.to_hc)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -94,9 +97,9 @@ module YouPlot
|
||||
end
|
||||
params.xlim ||= series[0].flatten.minmax # why need?
|
||||
params.ylim ||= series[1..-1].flatten.minmax # why need?
|
||||
plot = UnicodePlot.public_send(method1, series[0], series[1], **params.to_hc)
|
||||
plot = ::UnicodePlot.public_send(method1, series[0], series[1], **params.to_hc)
|
||||
2.upto(series.size - 1) do |i|
|
||||
UnicodePlot.public_send(method2, plot, series[0], series[i], name: headers&.[](i))
|
||||
::UnicodePlot.public_send(method2, plot, series[0], series[i], name: headers&.[](i))
|
||||
end
|
||||
plot
|
||||
end
|
||||
@@ -112,9 +115,9 @@ module YouPlot
|
||||
params.xlim ||= series2.map(&:first).flatten.minmax # why need?
|
||||
params.ylim ||= series2.map(&:last).flatten.minmax # why need?
|
||||
x1, y1 = series2.shift
|
||||
plot = UnicodePlot.public_send(method1, x1, y1, **params.to_hc)
|
||||
plot = ::UnicodePlot.public_send(method1, x1, y1, **params.to_hc)
|
||||
series2.each_with_index do |(xi, yi), i|
|
||||
UnicodePlot.public_send(method2, plot, xi, yi, name: headers&.[]((i + 1) * 2))
|
||||
::UnicodePlot.public_send(method2, plot, xi, yi, name: headers&.[]((i + 1) * 2))
|
||||
end
|
||||
plot
|
||||
end
|
||||
@@ -152,13 +155,13 @@ module YouPlot
|
||||
series = data.series
|
||||
headers ||= (1..series.size).map(&:to_s)
|
||||
series.map! { |s| s.map(&:to_f) }
|
||||
UnicodePlot.boxplot(headers, series, **params.to_hc)
|
||||
::UnicodePlot.boxplot(headers, series, **params.to_hc)
|
||||
end
|
||||
|
||||
def colors(color_names = false)
|
||||
# FIXME
|
||||
s = String.new
|
||||
UnicodePlot::StyledPrinter::TEXT_COLORS.each do |k, v|
|
||||
::UnicodePlot::StyledPrinter::TEXT_COLORS.each do |k, v|
|
||||
s << v
|
||||
s << k.to_s
|
||||
unless color_names
|
@@ -1,10 +1,10 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require_relative 'dsv'
|
||||
require_relative 'command/parser'
|
||||
require_relative 'parser'
|
||||
|
||||
# FIXME
|
||||
require_relative 'backends/unicode_plot_backend'
|
||||
require_relative 'backends/unicode_plot'
|
||||
|
||||
module YouPlot
|
||||
Data = Struct.new(:headers, :series)
|
||||
@@ -19,7 +19,7 @@ module YouPlot
|
||||
@command = nil
|
||||
@params = nil
|
||||
@options = nil
|
||||
@backend = YouPlot::Backends::UnicodePlotBackend
|
||||
@backend = YouPlot::Backends::UnicodePlot
|
||||
end
|
||||
|
||||
def run_as_executable
|
||||
@@ -33,21 +33,34 @@ module YouPlot
|
||||
@options ||= parser.options
|
||||
@params ||= parser.params
|
||||
|
||||
# color command
|
||||
if %i[colors color colours colour].include? @command
|
||||
plot = create_plot
|
||||
output_plot(plot)
|
||||
elsif options[:progressive]
|
||||
return
|
||||
end
|
||||
|
||||
# progressive mode
|
||||
if options[:progressive]
|
||||
stop = false
|
||||
Signal.trap(:INT) { stop = true }
|
||||
options[:output].print "\e[?25l" # make cursor invisible
|
||||
|
||||
# make cursor invisible
|
||||
options[:output].print "\e[?25l"
|
||||
|
||||
# mainloop
|
||||
while (input = Kernel.gets)
|
||||
n = main_progressive(input)
|
||||
break if stop
|
||||
|
||||
options[:output].print "\e[#{n}F"
|
||||
end
|
||||
|
||||
options[:output].print "\e[0J"
|
||||
options[:output].print "\e[?25h" # make cursor visible
|
||||
# make cursor visible
|
||||
options[:output].print "\e[?25h"
|
||||
|
||||
# normal mode
|
||||
else
|
||||
# Sometimes the input file does not end with a newline code.
|
||||
while (input = Kernel.gets(nil))
|
||||
@@ -59,23 +72,32 @@ module YouPlot
|
||||
private
|
||||
|
||||
def main(input)
|
||||
# Outputs input data to a file or stdout.
|
||||
output_data(input)
|
||||
|
||||
@data = read_dsv(input)
|
||||
@data = parse_dsv(input)
|
||||
|
||||
# Debug mode, show parsed results
|
||||
pp @data if options[:debug]
|
||||
|
||||
# When run as a program instead of a library
|
||||
if YouPlot.run_as_executable?
|
||||
begin
|
||||
plot = create_plot
|
||||
rescue ArgumentError => e
|
||||
# Show only one line of error.
|
||||
warn e.backtrace[0]
|
||||
# Show error message in purple
|
||||
warn "\e[35m#{e}\e[0m"
|
||||
# Explicitly terminated with exit code: 1
|
||||
exit 1
|
||||
end
|
||||
|
||||
# When running YouPlot as a library (e.g. for testing)
|
||||
else
|
||||
plot = create_plot
|
||||
end
|
||||
|
||||
output_plot(plot)
|
||||
end
|
||||
|
||||
@@ -95,15 +117,28 @@ module YouPlot
|
||||
@raw_data << input
|
||||
|
||||
# FIXME
|
||||
@data = read_dsv(@raw_data)
|
||||
@data = parse_dsv(@raw_data)
|
||||
|
||||
plot = create_plot
|
||||
output_plot_progressive(plot)
|
||||
end
|
||||
|
||||
def read_dsv(input)
|
||||
input = input.dup.force_encoding(options[:encoding]).encode('utf-8') if options[:encoding]
|
||||
DSV.parse(input, options[:delimiter], options[:headers], options[:transpose])
|
||||
def parse_dsv(input)
|
||||
# If encoding is specified, convert to UTF-8
|
||||
if options[:encoding]
|
||||
input.force_encoding(options[:encoding])
|
||||
.encode!('utf-8')
|
||||
end
|
||||
|
||||
begin
|
||||
data = DSV.parse(input, options[:delimiter], options[:headers], options[:transpose])
|
||||
rescue CSV::MalformedCSVError => e
|
||||
warn 'Failed to parse the text. '
|
||||
warn 'Please try to set the correct character encoding with --encoding option.'
|
||||
raise e
|
||||
end
|
||||
|
||||
data
|
||||
end
|
||||
|
||||
def create_plot
|
||||
@@ -147,9 +182,9 @@ module YouPlot
|
||||
|
||||
def output_plot(plot)
|
||||
case options[:output]
|
||||
when IO
|
||||
when IO, StringIO
|
||||
plot.render(options[:output])
|
||||
else
|
||||
when String, Tempfile
|
||||
File.open(options[:output], 'w') do |f|
|
||||
plot.render(f)
|
||||
end
|
||||
@@ -158,7 +193,7 @@ module YouPlot
|
||||
|
||||
def output_plot_progressive(plot)
|
||||
case options[:output]
|
||||
when IO
|
||||
when IO, StringIO
|
||||
# RefactorMe
|
||||
out = StringIO.new(String.new)
|
||||
def out.tty?
|
||||
|
@@ -1,19 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module YouPlot
|
||||
class Command
|
||||
Options = Struct.new(
|
||||
:delimiter,
|
||||
:transpose,
|
||||
:headers,
|
||||
:pass,
|
||||
:output,
|
||||
:fmt,
|
||||
:progressive,
|
||||
:encoding,
|
||||
:color_names,
|
||||
:debug,
|
||||
keyword_init: true
|
||||
)
|
||||
end
|
||||
end
|
@@ -1,305 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'optparse'
|
||||
require_relative 'options'
|
||||
require_relative 'plot_params'
|
||||
|
||||
module YouPlot
|
||||
class Command
|
||||
class Parser
|
||||
class Error < StandardError; end
|
||||
|
||||
attr_reader :command, :options, :params,
|
||||
:main_parser, :sub_parser
|
||||
|
||||
def initialize
|
||||
@command = nil
|
||||
|
||||
@options = Options.new(
|
||||
delimiter: "\t",
|
||||
transpose: false,
|
||||
headers: nil,
|
||||
pass: false,
|
||||
output: $stderr,
|
||||
fmt: 'xyy',
|
||||
progressive: false,
|
||||
encoding: nil,
|
||||
color_names: false,
|
||||
debug: false
|
||||
)
|
||||
|
||||
@params = PlotParams.new
|
||||
end
|
||||
|
||||
def create_default_parser
|
||||
OptionParser.new do |parser|
|
||||
parser.program_name = 'YouPlot'
|
||||
parser.version = YouPlot::VERSION
|
||||
parser.summary_width = 24
|
||||
parser.on_tail('') # Add a blank line at the end
|
||||
parser.separator('')
|
||||
parser.on('Common options:')
|
||||
parser.on('-O', '--pass [FILE]', 'file to output input data to [stdout]',
|
||||
'for inserting YouPlot in the middle of Unix pipes') do |v|
|
||||
options[:pass] = v || $stdout
|
||||
end
|
||||
parser.on('-o', '--output [FILE]', 'file to output plots to [stdout]',
|
||||
'If no option is specified, plot will print to stderr') do |v|
|
||||
options[:output] = v || $stdout
|
||||
end
|
||||
parser.on('-d', '--delimiter DELIM', String, 'use DELIM instead of [TAB] for field delimiter') do |v|
|
||||
options[:delimiter] = v
|
||||
end
|
||||
parser.on('-H', '--headers', TrueClass, 'specify that the input has header row') do |v|
|
||||
options[:headers] = v
|
||||
end
|
||||
parser.on('-T', '--transpose', TrueClass, 'transpose the axes of the input data') do |v|
|
||||
options[:transpose] = v
|
||||
end
|
||||
parser.on('-t', '--title STR', String, 'print string on the top of plot') do |v|
|
||||
params.title = v
|
||||
end
|
||||
parser.on('-x', '--xlabel STR', String, 'print string on the bottom of the plot') do |v|
|
||||
params.xlabel = v
|
||||
end
|
||||
parser.on('-y', '--ylabel STR', String, 'print string on the far left of the plot') do |v|
|
||||
params.ylabel = v
|
||||
end
|
||||
parser.on('-w', '--width INT', Integer, 'number of characters per row') do |v|
|
||||
params.width = v
|
||||
end
|
||||
parser.on('-h', '--height INT', Numeric, 'number of rows') do |v|
|
||||
params.height = v
|
||||
end
|
||||
border_options = UnicodePlot::BORDER_MAP.keys.join(', ')
|
||||
parser.on('-b', '--border STR', String, 'specify the style of the bounding box', "(#{border_options})") do |v|
|
||||
params.border = v.to_sym
|
||||
end
|
||||
parser.on('-m', '--margin INT', Numeric, 'number of spaces to the left of the plot') do |v|
|
||||
params.margin = v
|
||||
end
|
||||
parser.on('--padding INT', Numeric, 'space of the left and right of the plot') do |v|
|
||||
params.padding = v
|
||||
end
|
||||
parser.on('-c', '--color VAL', String, 'color of the drawing') do |v|
|
||||
params.color = v =~ /\A[0-9]+\z/ ? v.to_i : v.to_sym
|
||||
end
|
||||
parser.on('--[no-]labels', TrueClass, 'hide the labels') do |v|
|
||||
params.labels = v
|
||||
end
|
||||
parser.on('-p', '--progress', TrueClass, 'progressive mode [experimental]') do |v|
|
||||
options[:progressive] = v
|
||||
end
|
||||
parser.on('-C', '--color-output', TrueClass, 'colorize even if writing to a pipe') do |v|
|
||||
UnicodePlot::StyledPrinter.define_method(:color?){ |o| true }
|
||||
end
|
||||
parser.on('-M', '--monochrome', TrueClass, 'no colouring even if writing to a tty') do |v|
|
||||
UnicodePlot::StyledPrinter.define_method(:color?){ |o| false }
|
||||
end
|
||||
parser.on('--encoding STR', String, 'Specify the input encoding') do |v|
|
||||
options[:encoding] = v
|
||||
end
|
||||
# Optparse adds the help option, but it doesn't show up in usage.
|
||||
# This is why you need the code below.
|
||||
parser.on('--help', 'print sub-command help menu') do
|
||||
puts parser.help
|
||||
exit if YouPlot.run_as_executable?
|
||||
end
|
||||
parser.on('--debug', TrueClass, 'print preprocessed data') do |v|
|
||||
options[:debug] = v
|
||||
end
|
||||
# yield opt if block_given?
|
||||
end
|
||||
end
|
||||
|
||||
def create_main_parser
|
||||
@main_parser = create_default_parser
|
||||
main_parser.banner = \
|
||||
<<~MSG
|
||||
|
||||
Program: YouPlot (Tools for plotting on the terminal)
|
||||
Version: #{YouPlot::VERSION} (using UnicodePlot #{UnicodePlot::VERSION})
|
||||
Source: https://github.com/kojix2/youplot
|
||||
|
||||
Usage: uplot <command> [options] <in.tsv>
|
||||
|
||||
Commands:
|
||||
barplot bar draw a horizontal barplot
|
||||
histogram hist draw a horizontal histogram
|
||||
lineplot line draw a line chart
|
||||
lineplots lines draw a line chart with multiple series
|
||||
scatter s draw a scatter plot
|
||||
density d draw a density plot
|
||||
boxplot box draw a horizontal boxplot
|
||||
colors color show the list of available colors
|
||||
|
||||
count c draw a baplot based on the number of
|
||||
occurrences (slow)
|
||||
|
||||
General options:
|
||||
--help print command specific help menu
|
||||
--version print the version of YouPlot
|
||||
MSG
|
||||
|
||||
# Help for the main parser is simple.
|
||||
# Simply show the banner above.
|
||||
main_parser.on('--help', 'print sub-command help menu') do
|
||||
puts main_parser.banner
|
||||
puts
|
||||
exit if YouPlot.run_as_executable?
|
||||
end
|
||||
end
|
||||
|
||||
def sub_parser_add_symbol
|
||||
sub_parser.on_head('--symbol STR', String, 'character to be used to plot the bars') do |v|
|
||||
params.symbol = v
|
||||
end
|
||||
end
|
||||
|
||||
def sub_parser_add_xscale
|
||||
xscale_options = UnicodePlot::ValueTransformer::PREDEFINED_TRANSFORM_FUNCTIONS.keys.join(', ')
|
||||
sub_parser.on_head('--xscale STR', String, "axis scaling (#{xscale_options})") do |v|
|
||||
params.xscale = v.to_sym
|
||||
end
|
||||
end
|
||||
|
||||
def sub_parser_add_canvas
|
||||
sub_parser.on_head('--canvas STR', String, 'type of canvas') do |v|
|
||||
params.canvas = v.to_sym
|
||||
end
|
||||
end
|
||||
|
||||
def sub_parser_add_xlim
|
||||
sub_parser.on_head('--xlim FLOAT,FLOAT', Array, 'plotting range for the x coordinate') do |v|
|
||||
params.xlim = v
|
||||
end
|
||||
end
|
||||
|
||||
def sub_parser_add_ylim
|
||||
sub_parser.on_head('--ylim FLOAT,FLOAT', Array, 'plotting range for the y coordinate') do |v|
|
||||
params.ylim = v
|
||||
end
|
||||
end
|
||||
|
||||
def sub_parser_add_grid
|
||||
sub_parser.on_head('--[no-]grid', TrueClass, 'draws grid-lines at the origin') do |v|
|
||||
params.grid = v
|
||||
end
|
||||
end
|
||||
|
||||
def create_sub_parser
|
||||
@sub_parser = create_default_parser
|
||||
sub_parser.banner = \
|
||||
<<~MSG
|
||||
|
||||
Usage: YouPlot #{command} [options] <in.tsv>
|
||||
|
||||
Options for #{command}:
|
||||
MSG
|
||||
|
||||
case command
|
||||
|
||||
# If you type only `uplot` in the terminal.
|
||||
when nil
|
||||
warn main_parser.banner
|
||||
warn "\n"
|
||||
exit 1 if YouPlot.run_as_executable?
|
||||
|
||||
when :barplot, :bar
|
||||
sub_parser_add_symbol
|
||||
sub_parser.on_head('--fmt STR', String, 'xy : header is like x, y...', 'yx : header is like y, x...') do |v|
|
||||
options[:fmt] = v
|
||||
end
|
||||
sub_parser_add_xscale
|
||||
|
||||
when :count, :c
|
||||
sub_parser_add_symbol
|
||||
sub_parser_add_xscale
|
||||
|
||||
when :histogram, :hist
|
||||
sub_parser_add_symbol
|
||||
sub_parser.on_head('--closed STR', String, 'side of the intervals to be closed [left]') do |v|
|
||||
params.closed = v
|
||||
end
|
||||
sub_parser.on_head('-n', '--nbins INT', Numeric, 'approximate number of bins') do |v|
|
||||
params.nbins = v
|
||||
end
|
||||
|
||||
when :lineplot, :line
|
||||
sub_parser_add_canvas
|
||||
sub_parser_add_grid
|
||||
sub_parser.on_head('--fmt STR', String, 'xy : header is like x, y...', 'yx : header is like y, x...') do |v|
|
||||
options[:fmt] = v
|
||||
end
|
||||
sub_parser_add_ylim
|
||||
sub_parser_add_xlim
|
||||
|
||||
when :lineplots, :lines
|
||||
sub_parser_add_canvas
|
||||
sub_parser_add_grid
|
||||
sub_parser.on_head('--fmt STR', String, 'xyxy : header is like x1, y1, x2, y2, x3, y3...',
|
||||
'xyy : header is like x, y1, y2, y2, y3...') do |v|
|
||||
options[:fmt] = v
|
||||
end
|
||||
sub_parser_add_ylim
|
||||
sub_parser_add_xlim
|
||||
|
||||
when :scatter, :s
|
||||
sub_parser_add_canvas
|
||||
sub_parser_add_grid
|
||||
sub_parser.on_head('--fmt STR', String, 'xyxy : header is like x1, y1, x2, y2, x3, y3...',
|
||||
'xyy : header is like x, y1, y2, y2, y3...') do |v|
|
||||
options[:fmt] = v
|
||||
end
|
||||
sub_parser_add_ylim
|
||||
sub_parser_add_xlim
|
||||
|
||||
when :density, :d
|
||||
sub_parser_add_canvas
|
||||
sub_parser_add_grid
|
||||
sub_parser.on('--fmt STR', String, 'xyxy : header is like x1, y1, x2, y2, x3, y3...',
|
||||
'xyy : header is like x, y1, y2, y2, y3...') do |v|
|
||||
options[:fmt] = v
|
||||
end
|
||||
sub_parser_add_ylim
|
||||
sub_parser_add_xlim
|
||||
|
||||
when :boxplot, :box
|
||||
sub_parser_add_xlim
|
||||
|
||||
when :colors, :color, :colours, :colour
|
||||
sub_parser.on_head('-n', '--names', 'show color names only', TrueClass) do |v|
|
||||
options[:color_names] = v
|
||||
end
|
||||
|
||||
else
|
||||
error_message = "uplot: unrecognized command '#{command}'"
|
||||
if YouPlot.run_as_executable?
|
||||
warn error_message
|
||||
exit 1
|
||||
else
|
||||
raise Error, error_message
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def parse_options(argv = ARGV)
|
||||
begin
|
||||
create_main_parser.order!(argv)
|
||||
rescue OptionParser::ParseError => e
|
||||
warn "uplot: #{e.message}"
|
||||
exit 1 if YouPlot.run_as_executable?
|
||||
end
|
||||
|
||||
@command = argv.shift&.to_sym
|
||||
|
||||
begin
|
||||
create_sub_parser&.parse!(argv)
|
||||
rescue OptionParser::ParseError => e
|
||||
warn "uplot: #{e.message}"
|
||||
exit 1 if YouPlot.run_as_executable?
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@@ -1,37 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module YouPlot
|
||||
class Command
|
||||
# UnicodePlot parameters.
|
||||
# * Normally in a Ruby program, you might use hash for the parameter object.
|
||||
# * Here, I use Struct for 2 safety reason.
|
||||
# * The keys are static in Struct.
|
||||
# * Struct does not conflict with keyword arguments. Hash dose.
|
||||
PlotParams = Struct.new(
|
||||
# Sort me!
|
||||
:title,
|
||||
:width,
|
||||
:height,
|
||||
:border,
|
||||
:margin,
|
||||
:padding,
|
||||
:color,
|
||||
:xlabel,
|
||||
:ylabel,
|
||||
:labels,
|
||||
:symbol,
|
||||
:xscale,
|
||||
:nbins,
|
||||
:closed,
|
||||
:canvas,
|
||||
:xlim,
|
||||
:ylim,
|
||||
:grid,
|
||||
:name
|
||||
) do
|
||||
def to_hc
|
||||
to_h.compact
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@@ -3,41 +3,49 @@
|
||||
require 'csv'
|
||||
|
||||
module YouPlot
|
||||
# Read and interpret Delimiter-separated values format file or stream.
|
||||
# Module to handle DSV (Delimiter-separated values) format.
|
||||
# Extract header and series.
|
||||
module DSV
|
||||
module_function
|
||||
|
||||
def parse(input, delimiter, headers, transpose)
|
||||
arr = parse_as_csv(input, delimiter)
|
||||
headers = get_headers(arr, headers, transpose)
|
||||
series = get_series(arr, headers, transpose)
|
||||
if headers.nil?
|
||||
Data.new(headers, series)
|
||||
else
|
||||
if headers.include?(nil)
|
||||
warn "\e[35mHeaders contains nil in it.\e[0m"
|
||||
elsif headers.include? ''
|
||||
warn "\e[35mHeaders contains \"\" in it.\e[0m"
|
||||
end
|
||||
h_size = headers.size
|
||||
s_size = series.size
|
||||
if h_size == s_size
|
||||
Data.new(headers, series)
|
||||
elsif h_size > s_size
|
||||
warn "\e[35mThe number of headers is greater than the number of series.\e[0m"
|
||||
exit 1 if YouPlot.run_as_executable?
|
||||
elsif h_size < s_size
|
||||
warn "\e[35mThe number of headers is less than the number of series.\e[0m"
|
||||
exit 1 if YouPlot.run_as_executable?
|
||||
end
|
||||
end
|
||||
end
|
||||
# Parse as CSV
|
||||
arr = CSV.parse(input, col_sep: delimiter)
|
||||
|
||||
def parse_as_csv(input, delimiter)
|
||||
CSV.parse(input, col_sep: delimiter)
|
||||
.delete_if do |i|
|
||||
i == [] or i.all? nil
|
||||
end
|
||||
# Remove blank lines
|
||||
arr.delete_if do |i|
|
||||
i == [] or i.all? nil
|
||||
end
|
||||
|
||||
# get header
|
||||
headers = get_headers(arr, headers, transpose)
|
||||
|
||||
# get series
|
||||
series = get_series(arr, headers, transpose)
|
||||
|
||||
# Return if No header
|
||||
return Data.new(headers, series) if headers.nil?
|
||||
|
||||
# Warn if header contains nil
|
||||
warn "\e[35mHeaders contains nil in it.\e[0m" if headers.include?(nil)
|
||||
|
||||
# Warn if header contains ''
|
||||
warn "\e[35mHeaders contains \"\" in it.\e[0m" if headers.include? ''
|
||||
|
||||
# Make sure the number of elements in the header matches the number of series.
|
||||
h_size = headers.size
|
||||
s_size = series.size
|
||||
|
||||
if h_size > s_size
|
||||
warn "\e[35mThe number of headers is greater than the number of series.\e[0m"
|
||||
exit 1 if YouPlot.run_as_executable?
|
||||
|
||||
elsif h_size < s_size
|
||||
warn "\e[35mThe number of headers is less than the number of series.\e[0m"
|
||||
exit 1 if YouPlot.run_as_executable?
|
||||
end
|
||||
|
||||
Data.new(headers, series) if h_size == s_size
|
||||
end
|
||||
|
||||
# Transpose different sized ruby arrays
|
||||
@@ -47,31 +55,33 @@ module YouPlot
|
||||
end
|
||||
|
||||
def get_headers(arr, headers, transpose)
|
||||
if headers
|
||||
if transpose
|
||||
arr.map(&:first)
|
||||
else
|
||||
arr[0]
|
||||
end
|
||||
end
|
||||
# header(-)
|
||||
return nil unless headers
|
||||
|
||||
# header(+) trenspose(+)
|
||||
return arr.map(&:first) if transpose
|
||||
|
||||
# header(+) transpose(-)
|
||||
arr[0]
|
||||
end
|
||||
|
||||
def get_series(arr, headers, transpose)
|
||||
if headers
|
||||
if arr.size > 1
|
||||
if transpose
|
||||
arr.map { |row| row[1..-1] }
|
||||
else
|
||||
transpose2(arr[1..-1])
|
||||
end
|
||||
else
|
||||
Array.new(arr[0].size, [])
|
||||
end
|
||||
elsif transpose
|
||||
arr
|
||||
else
|
||||
transpose2(arr)
|
||||
# header(-)
|
||||
unless headers
|
||||
return arr if transpose
|
||||
|
||||
return transpose2(arr)
|
||||
end
|
||||
|
||||
# header(+) but no element in the series.
|
||||
# TODO: should raise error?
|
||||
return Array.new(arr[0].size, []) if arr.size == 1
|
||||
|
||||
# header(+) transpose(+)
|
||||
return arr.map { |row| row[1..-1] } if transpose
|
||||
|
||||
# header(+) transpose(-)
|
||||
transpose2(arr[1..-1])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
18
lib/youplot/options.rb
Normal file
18
lib/youplot/options.rb
Normal file
@@ -0,0 +1,18 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module YouPlot
|
||||
# Command line options that are not Plot parameters
|
||||
Options = Struct.new(
|
||||
:delimiter,
|
||||
:transpose,
|
||||
:headers,
|
||||
:pass,
|
||||
:output,
|
||||
:fmt,
|
||||
:progressive,
|
||||
:encoding,
|
||||
:color_names,
|
||||
:debug,
|
||||
keyword_init: true
|
||||
)
|
||||
end
|
34
lib/youplot/parameters.rb
Normal file
34
lib/youplot/parameters.rb
Normal file
@@ -0,0 +1,34 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module YouPlot
|
||||
# UnicodePlot parameters.
|
||||
# Why Struct, not Hash?
|
||||
# * The keys are static in Struct.
|
||||
# * Struct does not conflict with keyword arguments. Hash dose.
|
||||
Parameters = Struct.new(
|
||||
# Sort me!
|
||||
:title,
|
||||
:width,
|
||||
:height,
|
||||
:border,
|
||||
:margin,
|
||||
:padding,
|
||||
:color,
|
||||
:xlabel,
|
||||
:ylabel,
|
||||
:labels,
|
||||
:symbol,
|
||||
:xscale,
|
||||
:nbins,
|
||||
:closed,
|
||||
:canvas,
|
||||
:xlim,
|
||||
:ylim,
|
||||
:grid,
|
||||
:name
|
||||
) do
|
||||
def to_hc
|
||||
to_h.compact
|
||||
end
|
||||
end
|
||||
end
|
306
lib/youplot/parser.rb
Normal file
306
lib/youplot/parser.rb
Normal file
@@ -0,0 +1,306 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'optparse'
|
||||
require_relative 'options'
|
||||
|
||||
module YouPlot
|
||||
# Class for parsing command line options
|
||||
class Parser
|
||||
class Error < StandardError; end
|
||||
|
||||
attr_reader :command, :options, :params,
|
||||
:main_parser, :sub_parser
|
||||
|
||||
def initialize
|
||||
@command = nil
|
||||
|
||||
@options = Options.new(
|
||||
delimiter: "\t",
|
||||
transpose: false,
|
||||
headers: nil,
|
||||
pass: false,
|
||||
output: $stderr,
|
||||
fmt: 'xyy',
|
||||
progressive: false,
|
||||
encoding: nil,
|
||||
color_names: false,
|
||||
debug: false
|
||||
)
|
||||
|
||||
@params = Parameters.new
|
||||
end
|
||||
|
||||
def create_base_parser
|
||||
OptionParser.new do |parser|
|
||||
parser.program_name = 'YouPlot'
|
||||
parser.version = YouPlot::VERSION
|
||||
parser.summary_width = 23
|
||||
parser.on_tail('') # Add a blank line at the end
|
||||
parser.separator('')
|
||||
parser.on('Common options:')
|
||||
parser.on('-O', '--pass [FILE]', 'file to output input data to [stdout]',
|
||||
'for inserting YouPlot in the middle of Unix pipes') do |v|
|
||||
options[:pass] = v || $stdout
|
||||
end
|
||||
parser.on('-o', '--output [FILE]', 'file to output plots to [stdout]',
|
||||
'If no option is specified, plot will print to stderr') do |v|
|
||||
options[:output] = v || $stdout
|
||||
end
|
||||
parser.on('-d', '--delimiter DELIM', String, 'use DELIM instead of [TAB] for field delimiter') do |v|
|
||||
options[:delimiter] = v
|
||||
end
|
||||
parser.on('-H', '--headers', TrueClass, 'specify that the input has header row') do |v|
|
||||
options[:headers] = v
|
||||
end
|
||||
parser.on('-T', '--transpose', TrueClass, 'transpose the axes of the input data') do |v|
|
||||
options[:transpose] = v
|
||||
end
|
||||
parser.on('-t', '--title STR', String, 'print string on the top of plot') do |v|
|
||||
params.title = v
|
||||
end
|
||||
parser.on('-x', '--xlabel STR', String, 'print string on the bottom of the plot') do |v|
|
||||
params.xlabel = v
|
||||
end
|
||||
parser.on('-y', '--ylabel STR', String, 'print string on the far left of the plot') do |v|
|
||||
params.ylabel = v
|
||||
end
|
||||
parser.on('-w', '--width INT', Integer, 'number of characters per row') do |v|
|
||||
params.width = v
|
||||
end
|
||||
parser.on('-h', '--height INT', Numeric, 'number of rows') do |v|
|
||||
params.height = v
|
||||
end
|
||||
border_options = UnicodePlot::BORDER_MAP.keys.join(', ')
|
||||
parser.on('-b', '--border STR', String, 'specify the style of the bounding box', "(#{border_options})") do |v|
|
||||
params.border = v.to_sym
|
||||
end
|
||||
parser.on('-m', '--margin INT', Numeric, 'number of spaces to the left of the plot') do |v|
|
||||
params.margin = v
|
||||
end
|
||||
parser.on('--padding INT', Numeric, 'space of the left and right of the plot') do |v|
|
||||
params.padding = v
|
||||
end
|
||||
parser.on('-c', '--color VAL', String, 'color of the drawing') do |v|
|
||||
params.color = v =~ /\A[0-9]+\z/ ? v.to_i : v.to_sym
|
||||
end
|
||||
parser.on('--[no-]labels', TrueClass, 'hide the labels') do |v|
|
||||
params.labels = v
|
||||
end
|
||||
parser.on('-p', '--progress', TrueClass, 'progressive mode [experimental]') do |v|
|
||||
options[:progressive] = v
|
||||
end
|
||||
parser.on('-C', '--color-output', TrueClass, 'colorize even if writing to a pipe') do |_v|
|
||||
UnicodePlot::IOContext.define_method(:color?) { true } # FIXME
|
||||
end
|
||||
parser.on('-M', '--monochrome', TrueClass, 'no colouring even if writing to a tty') do |_v|
|
||||
UnicodePlot::IOContext.define_method(:color?) { false } # FIXME
|
||||
end
|
||||
parser.on('--encoding STR', String, 'Specify the input encoding') do |v|
|
||||
options[:encoding] = v
|
||||
end
|
||||
# Optparse adds the help option, but it doesn't show up in usage.
|
||||
# This is why you need the code below.
|
||||
parser.on('--help', 'print sub-command help menu') do
|
||||
puts parser.help
|
||||
exit if YouPlot.run_as_executable?
|
||||
end
|
||||
parser.on('--debug', TrueClass, 'print preprocessed data') do |v|
|
||||
options[:debug] = v
|
||||
end
|
||||
# yield opt if block_given?
|
||||
end
|
||||
end
|
||||
|
||||
def create_main_parser
|
||||
@main_parser = create_base_parser
|
||||
main_parser.banner = \
|
||||
<<~MSG
|
||||
|
||||
Program: YouPlot (Tools for plotting on the terminal)
|
||||
Version: #{YouPlot::VERSION} (using UnicodePlot #{UnicodePlot::VERSION})
|
||||
Source: https://github.com/kojix2/youplot
|
||||
|
||||
Usage: uplot <command> [options] <in.tsv>
|
||||
|
||||
Commands:
|
||||
barplot bar draw a horizontal barplot
|
||||
histogram hist draw a horizontal histogram
|
||||
lineplot line draw a line chart
|
||||
lineplots lines draw a line chart with multiple series
|
||||
scatter s draw a scatter plot
|
||||
density d draw a density plot
|
||||
boxplot box draw a horizontal boxplot
|
||||
colors color show the list of available colors
|
||||
|
||||
count c draw a baplot based on the number of
|
||||
occurrences (slow)
|
||||
|
||||
General options:
|
||||
--help print command specific help menu
|
||||
--version print the version of YouPlot
|
||||
MSG
|
||||
|
||||
# Help for the main parser is simple.
|
||||
# Simply show the banner above.
|
||||
main_parser.on('--help', 'print sub-command help menu') do
|
||||
puts main_parser.banner
|
||||
puts
|
||||
exit if YouPlot.run_as_executable?
|
||||
end
|
||||
end
|
||||
|
||||
def sub_parser_add_symbol
|
||||
sub_parser.on_head('--symbol STR', String, 'character to be used to plot the bars') do |v|
|
||||
params.symbol = v
|
||||
end
|
||||
end
|
||||
|
||||
def sub_parser_add_xscale
|
||||
xscale_options = UnicodePlot::ValueTransformer::PREDEFINED_TRANSFORM_FUNCTIONS.keys.join(', ')
|
||||
sub_parser.on_head('--xscale STR', String, "axis scaling (#{xscale_options})") do |v|
|
||||
params.xscale = v.to_sym
|
||||
end
|
||||
end
|
||||
|
||||
def sub_parser_add_canvas
|
||||
sub_parser.on_head('--canvas STR', String, 'type of canvas') do |v|
|
||||
params.canvas = v.to_sym
|
||||
end
|
||||
end
|
||||
|
||||
def sub_parser_add_xlim
|
||||
sub_parser.on_head('--xlim FLOAT,FLOAT', Array, 'plotting range for the x coordinate') do |v|
|
||||
params.xlim = v
|
||||
end
|
||||
end
|
||||
|
||||
def sub_parser_add_ylim
|
||||
sub_parser.on_head('--ylim FLOAT,FLOAT', Array, 'plotting range for the y coordinate') do |v|
|
||||
params.ylim = v
|
||||
end
|
||||
end
|
||||
|
||||
def sub_parser_add_grid
|
||||
sub_parser.on_head('--[no-]grid', TrueClass, 'draws grid-lines at the origin') do |v|
|
||||
params.grid = v
|
||||
end
|
||||
end
|
||||
|
||||
def sub_parser_add_fmt_xyxy
|
||||
sub_parser.on_head('--fmt STR', String,
|
||||
'xyxy : header is like x1, y1, x2, y2, x3, y3...',
|
||||
'xyy : header is like x, y1, y2, y2, y3...') do |v|
|
||||
options[:fmt] = v
|
||||
end
|
||||
end
|
||||
|
||||
def sub_parser_add_fmt_yx
|
||||
sub_parser.on_head('--fmt STR', String,
|
||||
'xy : header is like x, y...',
|
||||
'yx : header is like y, x...') do |v|
|
||||
options[:fmt] = v
|
||||
end
|
||||
end
|
||||
|
||||
def create_sub_parser
|
||||
@sub_parser = create_base_parser
|
||||
sub_parser.banner = \
|
||||
<<~MSG
|
||||
|
||||
Usage: YouPlot #{command} [options] <in.tsv>
|
||||
|
||||
Options for #{command}:
|
||||
MSG
|
||||
|
||||
case command
|
||||
|
||||
# If you type only `uplot` in the terminal.
|
||||
when nil
|
||||
warn main_parser.banner
|
||||
warn "\n"
|
||||
exit 1 if YouPlot.run_as_executable?
|
||||
|
||||
when :barplot, :bar
|
||||
sub_parser_add_symbol
|
||||
sub_parser_add_fmt_yx
|
||||
sub_parser_add_xscale
|
||||
|
||||
when :count, :c
|
||||
sub_parser_add_symbol
|
||||
sub_parser_add_xscale
|
||||
|
||||
when :histogram, :hist
|
||||
sub_parser_add_symbol
|
||||
sub_parser.on_head('--closed STR', String, 'side of the intervals to be closed [left]') do |v|
|
||||
params.closed = v
|
||||
end
|
||||
sub_parser.on_head('-n', '--nbins INT', Numeric, 'approximate number of bins') do |v|
|
||||
params.nbins = v
|
||||
end
|
||||
|
||||
when :lineplot, :line
|
||||
sub_parser_add_canvas
|
||||
sub_parser_add_grid
|
||||
sub_parser_add_fmt_yx
|
||||
sub_parser_add_ylim
|
||||
sub_parser_add_xlim
|
||||
|
||||
when :lineplots, :lines
|
||||
sub_parser_add_canvas
|
||||
sub_parser_add_grid
|
||||
sub_parser_add_fmt_xyxy
|
||||
sub_parser_add_ylim
|
||||
sub_parser_add_xlim
|
||||
|
||||
when :scatter, :s
|
||||
sub_parser_add_canvas
|
||||
sub_parser_add_grid
|
||||
sub_parser_add_fmt_xyxy
|
||||
sub_parser_add_ylim
|
||||
sub_parser_add_xlim
|
||||
|
||||
when :density, :d
|
||||
sub_parser_add_canvas
|
||||
sub_parser_add_grid
|
||||
sub_parser_add_fmt_xyxy
|
||||
sub_parser_add_ylim
|
||||
sub_parser_add_xlim
|
||||
|
||||
when :boxplot, :box
|
||||
sub_parser_add_xlim
|
||||
|
||||
when :colors, :color, :colours, :colour
|
||||
sub_parser.on_head('-n', '--names', 'show color names only', TrueClass) do |v|
|
||||
options[:color_names] = v
|
||||
end
|
||||
|
||||
else
|
||||
error_message = "uplot: unrecognized command '#{command}'"
|
||||
if YouPlot.run_as_executable?
|
||||
warn error_message
|
||||
exit 1
|
||||
else
|
||||
raise Error, error_message
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def parse_options(argv = ARGV)
|
||||
begin
|
||||
create_main_parser.order!(argv)
|
||||
rescue OptionParser::ParseError => e
|
||||
warn "uplot: #{e.message}"
|
||||
exit 1 if YouPlot.run_as_executable?
|
||||
end
|
||||
|
||||
@command = argv.shift&.to_sym
|
||||
|
||||
begin
|
||||
create_sub_parser&.parse!(argv)
|
||||
rescue OptionParser::ParseError => e
|
||||
warn "uplot: #{e.message}"
|
||||
exit 1 if YouPlot.run_as_executable?
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@@ -1,5 +1,5 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module YouPlot
|
||||
VERSION = '0.3.5'
|
||||
VERSION = '0.4.0'
|
||||
end
|
||||
|
38
test/fixtures/iris-count.txt
vendored
38
test/fixtures/iris-count.txt
vendored
@@ -1,38 +0,0 @@
|
||||
sepal_length
|
||||
┌ ┐
|
||||
5.0 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 10.0
|
||||
6.3 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 9.0
|
||||
5.1 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 9.0
|
||||
6.7 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■ 8.0
|
||||
5.7 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■ 8.0
|
||||
6.4 ┤■■■■■■■■■■■■■■■■■■■■■■■■ 7.0
|
||||
5.5 ┤■■■■■■■■■■■■■■■■■■■■■■■■ 7.0
|
||||
5.8 ┤■■■■■■■■■■■■■■■■■■■■■■■■ 7.0
|
||||
5.6 ┤■■■■■■■■■■■■■■■■■■■■ 6.0
|
||||
6.1 ┤■■■■■■■■■■■■■■■■■■■■ 6.0
|
||||
6.0 ┤■■■■■■■■■■■■■■■■■■■■ 6.0
|
||||
5.4 ┤■■■■■■■■■■■■■■■■■■■■ 6.0
|
||||
4.9 ┤■■■■■■■■■■■■■■■■■■■■ 6.0
|
||||
6.5 ┤■■■■■■■■■■■■■■■■■ 5.0
|
||||
4.8 ┤■■■■■■■■■■■■■■■■■ 5.0
|
||||
7.7 ┤■■■■■■■■■■■■■■ 4.0
|
||||
6.2 ┤■■■■■■■■■■■■■■ 4.0
|
||||
6.9 ┤■■■■■■■■■■■■■■ 4.0
|
||||
5.2 ┤■■■■■■■■■■■■■■ 4.0
|
||||
4.6 ┤■■■■■■■■■■■■■■ 4.0
|
||||
7.2 ┤■■■■■■■■■■ 3.0
|
||||
6.8 ┤■■■■■■■■■■ 3.0
|
||||
5.9 ┤■■■■■■■■■■ 3.0
|
||||
4.4 ┤■■■■■■■■■■ 3.0
|
||||
6.6 ┤■■■■■■■ 2.0
|
||||
4.7 ┤■■■■■■■ 2.0
|
||||
7.9 ┤■■■ 1.0
|
||||
7.4 ┤■■■ 1.0
|
||||
7.3 ┤■■■ 1.0
|
||||
7.6 ┤■■■ 1.0
|
||||
7.1 ┤■■■ 1.0
|
||||
7.0 ┤■■■ 1.0
|
||||
5.3 ┤■■■ 1.0
|
||||
4.5 ┤■■■ 1.0
|
||||
4.3 ┤■■■ 1.0
|
||||
└ ┘
|
12
test/fixtures/simple-count.txt
vendored
Normal file
12
test/fixtures/simple-count.txt
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
┌ ┐
|
||||
-10 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 1.0
|
||||
-20 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 1.0
|
||||
-30 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 1.0
|
||||
-40 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 1.0
|
||||
-50 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 1.0
|
||||
10 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 1.0
|
||||
20 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 1.0
|
||||
30 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 1.0
|
||||
40 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 1.0
|
||||
50 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 1.0
|
||||
└ ┘
|
@@ -2,7 +2,7 @@
|
||||
|
||||
require_relative '../../test_helper'
|
||||
|
||||
class YouPlotCommandTest < Test::Unit::TestCase
|
||||
class ProcessingTest < 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]))
|
||||
|
@@ -1,6 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require_relative '../../test_helper'
|
||||
|
||||
class YouPlotPlotTest < Test::Unit::TestCase
|
||||
end
|
@@ -28,25 +28,21 @@ class YouPlotIRISTest < Test::Unit::TestCase
|
||||
|
||||
def teardown
|
||||
@stderr_file.close
|
||||
@stdout_file.close
|
||||
end
|
||||
|
||||
def fixture(fname)
|
||||
File.read(File.expand_path("../fixtures/#{fname}", __dir__))
|
||||
end
|
||||
|
||||
test :bar do
|
||||
YouPlot::Command.new(['bar', '-H', '-d,', '-t', 'IRIS-BARPLOT']).run
|
||||
assert_equal fixture('iris-barplot.txt'), @stderr_file.read
|
||||
end
|
||||
|
||||
test :barplot do
|
||||
YouPlot::Command.new(['barplot', '-H', '-d,', '-t', 'IRIS-BARPLOT']).run
|
||||
assert_equal fixture('iris-barplot.txt'), @stderr_file.read
|
||||
end
|
||||
|
||||
test :hist do
|
||||
YouPlot::Command.new(['hist', '-H', '-d,', '-t', 'IRIS-HISTOGRAM']).run
|
||||
assert_equal fixture('iris-histogram.txt'), @stderr_file.read
|
||||
test :bar do
|
||||
YouPlot::Command.new(['bar', '-H', '-d,', '-t', 'IRIS-BARPLOT']).run
|
||||
assert_equal fixture('iris-barplot.txt'), @stderr_file.read
|
||||
end
|
||||
|
||||
test :histogram do
|
||||
@@ -54,9 +50,9 @@ class YouPlotIRISTest < Test::Unit::TestCase
|
||||
assert_equal fixture('iris-histogram.txt'), @stderr_file.read
|
||||
end
|
||||
|
||||
test :line do
|
||||
YouPlot::Command.new(['line', '-H', '-d,', '-t', 'IRIS-LINEPLOT']).run
|
||||
assert_equal fixture('iris-lineplot.txt'), @stderr_file.read
|
||||
test :hist do
|
||||
YouPlot::Command.new(['hist', '-H', '-d,', '-t', 'IRIS-HISTOGRAM']).run
|
||||
assert_equal fixture('iris-histogram.txt'), @stderr_file.read
|
||||
end
|
||||
|
||||
test :lineplot do
|
||||
@@ -64,9 +60,9 @@ class YouPlotIRISTest < Test::Unit::TestCase
|
||||
assert_equal fixture('iris-lineplot.txt'), @stderr_file.read
|
||||
end
|
||||
|
||||
test :lines do
|
||||
YouPlot::Command.new(['lines', '-H', '-d,', '-t', 'IRIS-LINEPLOTS']).run
|
||||
assert_equal fixture('iris-lineplots.txt'), @stderr_file.read
|
||||
test :line do
|
||||
YouPlot::Command.new(['line', '-H', '-d,', '-t', 'IRIS-LINEPLOT']).run
|
||||
assert_equal fixture('iris-lineplot.txt'), @stderr_file.read
|
||||
end
|
||||
|
||||
test :lineplots do
|
||||
@@ -74,9 +70,9 @@ class YouPlotIRISTest < Test::Unit::TestCase
|
||||
assert_equal fixture('iris-lineplots.txt'), @stderr_file.read
|
||||
end
|
||||
|
||||
test :s do
|
||||
YouPlot::Command.new(['s', '-H', '-d,', '-t', 'IRIS-SCATTER']).run
|
||||
assert_equal fixture('iris-scatter.txt'), @stderr_file.read
|
||||
test :lines do
|
||||
YouPlot::Command.new(['lines', '-H', '-d,', '-t', 'IRIS-LINEPLOTS']).run
|
||||
assert_equal fixture('iris-lineplots.txt'), @stderr_file.read
|
||||
end
|
||||
|
||||
test :scatter do
|
||||
@@ -84,9 +80,9 @@ class YouPlotIRISTest < Test::Unit::TestCase
|
||||
assert_equal fixture('iris-scatter.txt'), @stderr_file.read
|
||||
end
|
||||
|
||||
test :d do
|
||||
YouPlot::Command.new(['d', '-H', '-d,', '-t', 'IRIS-DENSITY']).run
|
||||
assert_equal fixture('iris-density.txt'), @stderr_file.read
|
||||
test :s do
|
||||
YouPlot::Command.new(['s', '-H', '-d,', '-t', 'IRIS-SCATTER']).run
|
||||
assert_equal fixture('iris-scatter.txt'), @stderr_file.read
|
||||
end
|
||||
|
||||
test :density do
|
||||
@@ -94,9 +90,9 @@ class YouPlotIRISTest < Test::Unit::TestCase
|
||||
assert_equal fixture('iris-density.txt'), @stderr_file.read
|
||||
end
|
||||
|
||||
test :box do
|
||||
YouPlot::Command.new(['box', '-H', '-d,', '-t', 'IRIS-BOXPLOT']).run
|
||||
assert_equal fixture('iris-boxplot.txt'), @stderr_file.read
|
||||
test :d do
|
||||
YouPlot::Command.new(['d', '-H', '-d,', '-t', 'IRIS-DENSITY']).run
|
||||
assert_equal fixture('iris-density.txt'), @stderr_file.read
|
||||
end
|
||||
|
||||
test :boxplot do
|
||||
@@ -104,6 +100,11 @@ class YouPlotIRISTest < Test::Unit::TestCase
|
||||
assert_equal fixture('iris-boxplot.txt'), @stderr_file.read
|
||||
end
|
||||
|
||||
test :box do
|
||||
YouPlot::Command.new(['box', '-H', '-d,', '-t', 'IRIS-BOXPLOT']).run
|
||||
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
|
||||
@@ -141,7 +142,7 @@ class YouPlotIRISTest < Test::Unit::TestCase
|
||||
end
|
||||
|
||||
test :unrecognized_command do
|
||||
assert_raise(YouPlot::Command::Parser::Error) do
|
||||
assert_raise(YouPlot::Parser::Error) do
|
||||
YouPlot::Command.new(['abracadabra', '--hadley', '--wickham']).run
|
||||
end
|
||||
assert_equal '', @stderr_file.read
|
||||
|
@@ -28,22 +28,31 @@ class YouPlotSimpleTest < Test::Unit::TestCase
|
||||
|
||||
def teardown
|
||||
@stderr_file.close
|
||||
@stdout_file.close
|
||||
end
|
||||
|
||||
def fixture(fname)
|
||||
File.read(File.expand_path("../fixtures/#{fname}", __dir__))
|
||||
end
|
||||
|
||||
# Single command
|
||||
# The goal is to verify that the command works without any options.
|
||||
|
||||
test :barplot do
|
||||
assert_raise(ArgumentError) do
|
||||
YouPlot::Command.new(['barplot']).run
|
||||
end
|
||||
end
|
||||
|
||||
test :bar do
|
||||
assert_raise(ArgumentError) do
|
||||
YouPlot::Command.new(['bar']).run
|
||||
end
|
||||
end
|
||||
|
||||
test :barplot do
|
||||
assert_raise(ArgumentError) do
|
||||
YouPlot::Command.new(['barplot']).run
|
||||
end
|
||||
test :histogram do
|
||||
YouPlot::Command.new(['histogram']).run
|
||||
assert_equal fixture('simple-histogram.txt'), @stderr_file.read
|
||||
end
|
||||
|
||||
test :hist do
|
||||
@@ -51,9 +60,9 @@ class YouPlotSimpleTest < Test::Unit::TestCase
|
||||
assert_equal fixture('simple-histogram.txt'), @stderr_file.read
|
||||
end
|
||||
|
||||
test :histogram do
|
||||
YouPlot::Command.new(['histogram']).run
|
||||
assert_equal fixture('simple-histogram.txt'), @stderr_file.read
|
||||
test :lineplot do
|
||||
YouPlot::Command.new(['lineplot']).run
|
||||
assert_equal fixture('simple-lineplot.txt'), @stderr_file.read
|
||||
end
|
||||
|
||||
test :line do
|
||||
@@ -61,50 +70,40 @@ class YouPlotSimpleTest < Test::Unit::TestCase
|
||||
assert_equal fixture('simple-lineplot.txt'), @stderr_file.read
|
||||
end
|
||||
|
||||
test :lineplot do
|
||||
YouPlot::Command.new(['lineplot']).run
|
||||
assert_equal fixture('simple-lineplot.txt'), @stderr_file.read
|
||||
end
|
||||
|
||||
test :lines do
|
||||
assert_raise(YouPlot::Backends::UnicodePlotBackend::Error) do
|
||||
YouPlot::Command.new(['lines']).run
|
||||
end
|
||||
end
|
||||
|
||||
test :lineplots do
|
||||
assert_raise(YouPlot::Backends::UnicodePlotBackend::Error) do
|
||||
assert_raise(YouPlot::Backends::UnicodePlot::Error) do
|
||||
YouPlot::Command.new(['lineplots']).run
|
||||
end
|
||||
end
|
||||
|
||||
test :s do
|
||||
assert_raise(YouPlot::Backends::UnicodePlotBackend::Error) do
|
||||
YouPlot::Command.new(['s']).run
|
||||
test :lines do
|
||||
assert_raise(YouPlot::Backends::UnicodePlot::Error) do
|
||||
YouPlot::Command.new(['lines']).run
|
||||
end
|
||||
end
|
||||
|
||||
test :scatter do
|
||||
assert_raise(YouPlot::Backends::UnicodePlotBackend::Error) do
|
||||
assert_raise(YouPlot::Backends::UnicodePlot::Error) do
|
||||
YouPlot::Command.new(['scatter']).run
|
||||
end
|
||||
end
|
||||
|
||||
test :d do
|
||||
assert_raise(YouPlot::Backends::UnicodePlotBackend::Error) do
|
||||
YouPlot::Command.new(['d']).run
|
||||
test :s do
|
||||
assert_raise(YouPlot::Backends::UnicodePlot::Error) do
|
||||
YouPlot::Command.new(['s']).run
|
||||
end
|
||||
end
|
||||
|
||||
test :density do
|
||||
assert_raise(YouPlot::Backends::UnicodePlotBackend::Error) do
|
||||
assert_raise(YouPlot::Backends::UnicodePlot::Error) do
|
||||
YouPlot::Command.new(['density']).run
|
||||
end
|
||||
end
|
||||
|
||||
test :box do
|
||||
YouPlot::Command.new(['box']).run
|
||||
assert_equal fixture('simple-boxplot.txt'), @stderr_file.read
|
||||
test :d do
|
||||
assert_raise(YouPlot::Backends::UnicodePlot::Error) do
|
||||
YouPlot::Command.new(['d']).run
|
||||
end
|
||||
end
|
||||
|
||||
test :boxplot do
|
||||
@@ -112,17 +111,20 @@ class YouPlotSimpleTest < Test::Unit::TestCase
|
||||
assert_equal fixture('simple-boxplot.txt'), @stderr_file.read
|
||||
end
|
||||
|
||||
# test :c do
|
||||
# omit
|
||||
# YouPlot::Command.new(['count', '-H', '-d,']).run
|
||||
# assert_equal fixture('iris-count.txt'), @stderr_file.read
|
||||
# end
|
||||
test :box do
|
||||
YouPlot::Command.new(['box']).run
|
||||
assert_equal fixture('simple-boxplot.txt'), @stderr_file.read
|
||||
end
|
||||
|
||||
# test :count do
|
||||
# omit
|
||||
# YouPlot::Command.new(['c', '-H', '-d,']).run
|
||||
# assert_equal fixture('iris-count.txt'), @stderr_file.read
|
||||
# end
|
||||
test :count do
|
||||
YouPlot::Command.new(['c']).run
|
||||
assert_equal fixture('simple-count.txt'), @stderr_file.read
|
||||
end
|
||||
|
||||
test :c do
|
||||
YouPlot::Command.new(['count']).run
|
||||
assert_equal fixture('simple-count.txt'), @stderr_file.read
|
||||
end
|
||||
|
||||
test :plot_output_stdout do
|
||||
YouPlot::Command.new(['line', '-o']).run
|
||||
|
Reference in New Issue
Block a user