youplot 0.3.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d270d30bd2d7d0766d6e26704383ec25fd742d2ed12b8e3738f91c985e2b4f80
4
- data.tar.gz: '01149b5a5bd05dbbfc544e207ea4345c602468f68e39d3965025c4f7c1ddb13f'
3
+ metadata.gz: a9b1c58784cb37da38b1c2bb24ed86961a8d7ddded67899027f9ed725ece7ea8
4
+ data.tar.gz: 1f6050c3707ecaa29997ee1af65b440ba016f0eacb47d4dc9c38a15ac59afe9a
5
5
  SHA512:
6
- metadata.gz: 80c2894983ca5cc3a9e9833a95f7d11a8928db454fecc0b97b585a4903dd0ae77cf89a1929dc4cb526dc6e7b0f8b0019d5d0769ab6f1a63ab4acb3db8a125c35
7
- data.tar.gz: 4c6b532a8b95ab2b9ba8aa5def4b368edd93fe5e9af224d1f7fc4f87a4b40d0a07cf7f874565c10f7630d257e393fb8175ec087e6da9e4740b4e31e76c3d1d12
6
+ metadata.gz: 5d62aeed65f6a4692f115453b085cb926d54a8efe184e7e39250a720235f8d13ecf8ec2026099ae2d6a3ab17385e607a9281fff1b12e81914314c991bf471ca4
7
+ data.tar.gz: 1e3ad16f26c8287385fdee2a4f13626b369a58c63f2d488678b02bdc15dfb7e22a8b8ed0f31c1d6324537c59aa86c4367187f01e6080f6bf51f3bedade3905f4
@@ -2,7 +2,7 @@
2
2
 
3
3
  require 'unicode_plot'
4
4
  require 'youplot/version'
5
- require 'youplot/preprocessing'
5
+ require 'youplot/dsv_reader'
6
6
  require 'youplot/command'
7
7
 
8
8
  module YouPlot
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module YouPlot
4
+ # plotting functions.
5
+ module Backends
6
+ module Processing
7
+ module_function
8
+
9
+ def count_values(arr)
10
+ # tally was added in Ruby 2.7
11
+ if Enumerable.method_defined? :tally
12
+ arr.tally
13
+ else
14
+ # https://github.com/marcandre/backports
15
+ arr.each_with_object(Hash.new(0)) { |item, res| res[item] += 1 }
16
+ .tap { |h| h.default = nil }
17
+ end
18
+ .sort { |a, b| a[1] <=> b[1] }
19
+ .reverse
20
+ .transpose
21
+ end
22
+ end
23
+ end
24
+ end
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'processing'
3
4
  require 'unicode_plot'
4
5
 
5
6
  module YouPlot
@@ -8,23 +9,33 @@ module YouPlot
8
9
  module UnicodePlotBackend
9
10
  module_function
10
11
 
11
- def barplot(data, params, count: false)
12
+ def barplot(data, params, fmt = nil, count: false)
12
13
  headers = data.headers
13
14
  series = data.series
14
15
  # `uplot count`
15
16
  if count
16
- series = Preprocessing.count_values(series[0])
17
+ series = Processing.count_values(series[0])
17
18
  params.title = headers[0] if headers
18
19
  end
19
20
  if series.size == 1
20
- # If there is only one series, use the line number for label.
21
+ # If there is only one series.use the line number for label.
21
22
  params.title ||= headers[0] if headers
22
23
  labels = Array.new(series[0].size) { |i| (i + 1).to_s }
23
24
  values = series[0].map(&:to_f)
24
25
  else
25
- params.title ||= headers[1] if headers
26
- labels = series[0]
27
- values = series[1].map(&:to_f)
26
+ # If there are 2 or more series...
27
+ if fmt == 'yx'
28
+ # assume that the first 2 series are the y and x series respectively.
29
+ x_col = 1
30
+ y_col = 0
31
+ else
32
+ # assume that the first 2 series are the x and y series respectively.
33
+ x_col = 0
34
+ y_col = 1
35
+ end
36
+ params.title ||= headers[y_col] if headers
37
+ labels = series[x_col]
38
+ values = series[y_col].map(&:to_f)
28
39
  end
29
40
  UnicodePlot.barplot(labels, values, **params.to_hc)
30
41
  end
@@ -37,7 +48,7 @@ module YouPlot
37
48
  UnicodePlot.histogram(values, **params.to_hc)
38
49
  end
39
50
 
40
- def line(data, params)
51
+ def line(data, params, fmt = nil)
41
52
  headers = data.headers
42
53
  series = data.series
43
54
  if series.size == 1
@@ -46,14 +57,22 @@ module YouPlot
46
57
  y = series[0].map(&:to_f)
47
58
  UnicodePlot.lineplot(y, **params.to_hc)
48
59
  else
49
- # If there are 2 or more series,
50
- # assume that the first 2 series are the x and y series respectively.
60
+ # If there are 2 or more series...
61
+ if fmt == 'yx'
62
+ # assume that the first 2 series are the y and x series respectively.
63
+ x_col = 1
64
+ y_col = 0
65
+ else
66
+ # assume that the first 2 series are the x and y series respectively.
67
+ x_col = 0
68
+ y_col = 1
69
+ end
51
70
  if headers
52
- params.xlabel ||= headers[0]
53
- params.ylabel ||= headers[1]
71
+ params.xlabel ||= headers[x_col]
72
+ params.ylabel ||= headers[y_col]
54
73
  end
55
- x = series[0].map(&:to_f)
56
- y = series[1].map(&:to_f)
74
+ x = series[x_col].map(&:to_f)
75
+ y = series[y_col].map(&:to_f)
57
76
  UnicodePlot.lineplot(x, y, **params.to_hc)
58
77
  end
59
78
  end
@@ -102,6 +121,8 @@ module YouPlot
102
121
  plot_xyy(data, method1, params)
103
122
  when 'xyxy'
104
123
  plot_xyxy(data, method1, params)
124
+ when 'yx'
125
+ raise "Incorrect format: #{fmt}"
105
126
  else
106
127
  raise "Unknown format: #{fmt}"
107
128
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'preprocessing'
3
+ require_relative 'dsv_reader'
4
4
  require_relative 'command/parser'
5
5
 
6
6
  # FIXME
@@ -30,6 +30,7 @@ module YouPlot
30
30
  pass = parser.pass
31
31
  output = parser.output
32
32
  fmt = parser.fmt
33
+ @encoding = parser.encoding
33
34
  @debug = parser.debug
34
35
 
35
36
  if command == :colors
@@ -39,18 +40,37 @@ module YouPlot
39
40
 
40
41
  # Sometimes the input file does not end with a newline code.
41
42
  while (input = Kernel.gets(nil))
42
- input.freeze
43
- @data = Preprocessing.input(input, delimiter, headers, transpose)
43
+
44
+ # Pass the input to subsequent pipelines
45
+ case pass
46
+ when IO
47
+ pass.print(input)
48
+ else
49
+ if pass
50
+ File.open(pass, 'w') do |f|
51
+ f.print(input)
52
+ end
53
+ end
54
+ end
55
+
56
+ @data = if @encoding
57
+ input2 = input.dup.force_encoding(@encoding).encode('utf-8')
58
+ DSVReader.input(input2, delimiter, headers, transpose)
59
+ else
60
+ DSVReader.input(input, delimiter, headers, transpose)
61
+ end
62
+
44
63
  pp @data if @debug
64
+
45
65
  plot = case command
46
66
  when :bar, :barplot
47
- @backend.barplot(data, params)
67
+ @backend.barplot(data, params, fmt)
48
68
  when :count, :c
49
69
  @backend.barplot(data, params, count: true)
50
70
  when :hist, :histogram
51
71
  @backend.histogram(data, params)
52
72
  when :line, :lineplot
53
- @backend.line(data, params)
73
+ @backend.line(data, params, fmt)
54
74
  when :lines, :lineplots
55
75
  @backend.lines(data, params, fmt)
56
76
  when :scatter, :s
@@ -72,16 +92,6 @@ module YouPlot
72
92
  end
73
93
  end
74
94
 
75
- case pass
76
- when IO
77
- pass.print(input)
78
- else
79
- if pass
80
- File.open(pass, 'w') do |f|
81
- f.print(input)
82
- end
83
- end
84
- end
85
95
  end
86
96
  end
87
97
  end
@@ -8,7 +8,7 @@ module YouPlot
8
8
  class Parser
9
9
  attr_reader :command, :params,
10
10
  :delimiter, :transpose, :headers, :pass, :output, :fmt,
11
- :color_names, :debug
11
+ :color_names, :encoding, :debug
12
12
 
13
13
  def initialize
14
14
  @command = nil
@@ -20,6 +20,7 @@ module YouPlot
20
20
  @pass = false
21
21
  @output = $stderr
22
22
  @fmt = 'xyy'
23
+ @encoding = nil
23
24
  @debug = false
24
25
  @color_names = false
25
26
  end
@@ -78,8 +79,8 @@ module YouPlot
78
79
  opt.on('--[no-]labels', TrueClass, 'hide the labels') do |v|
79
80
  params.labels = v
80
81
  end
81
- opt.on('--fmt VAL', String, 'xyxy : header is like x1, y1, x2, y2, x3, y3...', 'xyy : header is like x, y1, y2, y2, y3...') do |v|
82
- @fmt = v
82
+ opt.on('--encoding VAL', String, 'Specify the input encoding') do |v|
83
+ @encoding = v
83
84
  end
84
85
  # Optparse adds the help option, but it doesn't show up in usage.
85
86
  # This is why you need the code below.
@@ -161,6 +162,9 @@ module YouPlot
161
162
  parser.on_head('--xscale VAL', String, 'axis scaling') do |v|
162
163
  params.xscale = v
163
164
  end
165
+ parser.on_head('--fmt VAL', String, 'xy : header is like x, y...', 'yx : header is like y, x...') do |v|
166
+ @fmt = v
167
+ end
164
168
 
165
169
  when :count, :c
166
170
  parser.on_head('--symbol VAL', String, 'character to be used to plot the bars') do |v|
@@ -188,6 +192,9 @@ module YouPlot
188
192
  parser.on_head('--ylim VAL', Array, 'plotting range for the y coordinate') do |v|
189
193
  params.ylim = v.take(2)
190
194
  end
195
+ parser.on_head('--fmt VAL', String, 'xy : header is like x, y...', 'yx : header is like y, x...') do |v|
196
+ @fmt = v
197
+ end
191
198
 
192
199
  when :lineplots, :lines
193
200
  parser.on_head('--canvas VAL', String) do |v|
@@ -199,6 +206,9 @@ module YouPlot
199
206
  parser.on_head('--ylim VAL', Array, 'plotting range for the y coordinate') do |v|
200
207
  params.ylim = v.take(2)
201
208
  end
209
+ parser.on_head('--fmt VAL', String, 'xyxy : header is like x1, y1, x2, y2, x3, y3...', 'xyy : header is like x, y1, y2, y2, y3...') do |v|
210
+ @fmt = v
211
+ end
202
212
 
203
213
  when :scatter, :s
204
214
  parser.on_head('--canvas VAL', String) do |v|
@@ -210,6 +220,9 @@ module YouPlot
210
220
  parser.on_head('--ylim VAL', Array, 'plotting range for the y coordinate') do |v|
211
221
  params.ylim = v.take(2)
212
222
  end
223
+ parser.on_head('--fmt VAL', String, 'xyxy : header is like x1, y1, x2, y2, x3, y3...', 'xyy : header is like x, y1, y2, y2, y3...') do |v|
224
+ @fmt = v
225
+ end
213
226
 
214
227
  when :density, :d
215
228
  parser.on_head('--grid', TrueClass) do |v|
@@ -221,6 +234,9 @@ module YouPlot
221
234
  parser.on_head('--ylim VAL', Array, 'plotting range for the y coordinate') do |v|
222
235
  params.ylim = v.take(2)
223
236
  end
237
+ parser.on('--fmt VAL', String, 'xyxy : header is like x1, y1, x2, y2, x3, y3...', 'xyy : header is like x, y1, y2, y2, y3...') do |v|
238
+ @fmt = v
239
+ end
224
240
 
225
241
  when :boxplot, :box
226
242
  parser.on_head('--xlim VAL', Array, 'plotting range for the x coordinate') do |v|
@@ -3,7 +3,8 @@
3
3
  require 'csv'
4
4
 
5
5
  module YouPlot
6
- module Preprocessing
6
+ # Read and interpret Delimiter-separated values format file or stream.
7
+ module DSVReader
7
8
  module_function
8
9
 
9
10
  def input(input, delimiter, headers, transpose)
@@ -68,19 +69,5 @@ module YouPlot
68
69
  transpose2(arr)
69
70
  end
70
71
  end
71
-
72
- def count_values(arr)
73
- # tally was added in Ruby 2.7
74
- if Enumerable.method_defined? :tally
75
- arr.tally
76
- else
77
- # https://github.com/marcandre/backports
78
- arr.each_with_object(Hash.new(0)) { |item, res| res[item] += 1 }
79
- .tap { |h| h.default = nil }
80
- end
81
- .sort { |a, b| a[1] <=> b[1] }
82
- .reverse
83
- .transpose
84
- end
85
72
  end
86
73
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module YouPlot
4
- VERSION = '0.3.0'
4
+ VERSION = '0.3.1'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: youplot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - kojix2
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-11-23 00:00:00.000000000 Z
11
+ date: 2020-12-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: unicode_plot
@@ -109,11 +109,12 @@ files:
109
109
  - exe/uplot
110
110
  - exe/youplot
111
111
  - lib/youplot.rb
112
+ - lib/youplot/backends/processing.rb
112
113
  - lib/youplot/backends/unicode_plot_backend.rb
113
114
  - lib/youplot/command.rb
114
115
  - lib/youplot/command/params.rb
115
116
  - lib/youplot/command/parser.rb
116
- - lib/youplot/preprocessing.rb
117
+ - lib/youplot/dsv_reader.rb
117
118
  - lib/youplot/version.rb
118
119
  homepage: https://github.com/kojix2/youplot
119
120
  licenses: