unicode_plot 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/test/test-canvas.rb CHANGED
@@ -4,7 +4,9 @@ module CanvasTestCases
4
4
 
5
5
  CANVAS_CLASSES = {
6
6
  ascii: UnicodePlot::AsciiCanvas,
7
- braille: UnicodePlot::BrailleCanvas
7
+ braille: UnicodePlot::BrailleCanvas,
8
+ density: UnicodePlot::DensityCanvas,
9
+ dot: UnicodePlot::DotCanvas
8
10
  }.freeze
9
11
 
10
12
  def self.included(mod)
@@ -122,3 +124,15 @@ class AsciiCanvasTest < Test::Unit::TestCase
122
124
 
123
125
  include CanvasTestCases
124
126
  end
127
+
128
+ class DensityCanvasTest < Test::Unit::TestCase
129
+ CANVAS_NAME = :density
130
+
131
+ include CanvasTestCases
132
+ end
133
+
134
+ class DotCanvasTest < Test::Unit::TestCase
135
+ CANVAS_NAME = :dot
136
+
137
+ include CanvasTestCases
138
+ end
@@ -0,0 +1,38 @@
1
+ class DensityplotTest < Test::Unit::TestCase
2
+ include Helper::Fixture
3
+ include Helper::WithTerm
4
+
5
+ def setup
6
+ randn = fixture_path("randn_1338_2000.txt").read.each_line.map(&:to_f)
7
+ @dx = randn[0, 1000].compact
8
+ @dy = randn[1000, 1000].compact
9
+ assert_equal(1000, @dx.length)
10
+ assert_equal(1000, @dy.length)
11
+ end
12
+
13
+ test("default") do
14
+ plot = UnicodePlot.densityplot(@dx, @dy)
15
+ dx2 = @dx.map {|x| x + 2 }
16
+ dy2 = @dy.map {|y| y + 2 }
17
+ assert_same(plot,
18
+ UnicodePlot.densityplot!(plot, dx2, dy2))
19
+ _, output = with_term { plot.render($stdout) }
20
+ expected = fixture_path("scatterplot/densityplot.txt").read
21
+ assert_equal(output, expected)
22
+ end
23
+
24
+ test("parameters") do
25
+ plot = UnicodePlot.densityplot(@dx, @dy,
26
+ name: "foo",
27
+ color: :red,
28
+ title: "Title",
29
+ xlabel: "x")
30
+ dx2 = @dx.map {|x| x + 2 }
31
+ dy2 = @dy.map {|y| y + 2 }
32
+ assert_same(plot,
33
+ UnicodePlot.densityplot!(plot, dx2, dy2, name: "bar"))
34
+ _, output = with_term { plot.render($stdout) }
35
+ expected = fixture_path("scatterplot/densityplot_parameters.txt").read
36
+ assert_equal(output, expected)
37
+ end
38
+ end
@@ -0,0 +1,110 @@
1
+ class HistogramTest < Test::Unit::TestCase
2
+ include Helper::Fixture
3
+ include Helper::WithTerm
4
+
5
+ sub_test_case("UnicodePlot.histogram") do
6
+ def setup
7
+ @x = fixture_path("randn.txt").read.lines.map(&:to_f)
8
+ end
9
+
10
+ test("default") do
11
+ plot = UnicodePlot.histogram(@x)
12
+ _, output = with_term { plot.render($stdout) }
13
+ assert_equal(fixture_path("histogram/default.txt").read,
14
+ output)
15
+ end
16
+
17
+ test("nocolor") do
18
+ plot = UnicodePlot.histogram(@x)
19
+ output = StringIO.open do |sio|
20
+ plot.render(sio)
21
+ sio.close
22
+ sio.string
23
+ end
24
+ assert_equal(fixture_path("histogram/default_nocolor.txt").read,
25
+ output)
26
+ end
27
+
28
+ test("losed: :left") do
29
+ plot = UnicodePlot.histogram(@x, closed: :left)
30
+ _, output = with_term { plot.render($stdout) }
31
+ assert_equal(fixture_path("histogram/default.txt").read,
32
+ output)
33
+ end
34
+
35
+ test("x 100") do
36
+ x100 = @x.map {|a| a * 100 }
37
+ plot = UnicodePlot.histogram(x100)
38
+ _, output = with_term { plot.render($stdout) }
39
+ assert_equal(fixture_path("histogram/default_1e2.txt").read,
40
+ output)
41
+ end
42
+
43
+ test("x0.01") do
44
+ x100 = @x.map {|a| a * 0.01 }
45
+ plot = UnicodePlot.histogram(x100)
46
+ _, output = with_term { plot.render($stdout) }
47
+ assert_equal(fixture_path("histogram/default_1e-2.txt").read,
48
+ output)
49
+ end
50
+
51
+ test("xscale: :log10") do
52
+ plot = UnicodePlot.histogram(@x, xscale: :log10)
53
+ _, output = with_term { plot.render($stdout) }
54
+ assert_equal(fixture_path("histogram/log10.txt").read,
55
+ output)
56
+ end
57
+
58
+ test("xscale: :log10 with custom label") do
59
+ plot = UnicodePlot.histogram(@x, xscale: :log10, xlabel: "custom label")
60
+ _, output = with_term { plot.render($stdout) }
61
+ assert_equal(fixture_path("histogram/log10_label.txt").read,
62
+ output)
63
+ end
64
+
65
+ test("nbins: 5, closed: :right") do
66
+ plot = UnicodePlot.histogram(@x, nbins: 5, closed: :right)
67
+ _, output = with_term { plot.render($stdout) }
68
+ assert_equal(fixture_path("histogram/hist_params.txt").read,
69
+ output)
70
+ end
71
+
72
+ test("with title, xlabel, color, margin, and padding") do
73
+ plot = UnicodePlot.histogram(@x,
74
+ title: "My Histogram",
75
+ xlabel: "Absolute Frequency",
76
+ color: :blue,
77
+ margin: 7,
78
+ padding: 3)
79
+ _, output = with_term { plot.render($stdout) }
80
+ assert_equal(fixture_path("histogram/parameters1.txt").read,
81
+ output)
82
+ end
83
+
84
+ test("with title, xlabel, color, margin, padding, and labels: false") do
85
+ plot = UnicodePlot.histogram(@x,
86
+ title: "My Histogram",
87
+ xlabel: "Absolute Frequency",
88
+ color: :blue,
89
+ margin: 7,
90
+ padding: 3,
91
+ labels: false)
92
+ _, output = with_term { plot.render($stdout) }
93
+ assert_equal(fixture_path("histogram/parameters1_nolabels.txt").read,
94
+ output)
95
+ end
96
+
97
+ test("with title, xlabel, color, border, symbol, and width") do
98
+ plot = UnicodePlot.histogram(@x,
99
+ title: "My Histogram",
100
+ xlabel: "Absolute Frequency",
101
+ color: :yellow,
102
+ border: :solid,
103
+ symbol: "=",
104
+ width: 50)
105
+ _, output = with_term { plot.render($stdout) }
106
+ assert_equal(fixture_path("histogram/parameters2.txt").read,
107
+ output)
108
+ end
109
+ end
110
+ end
@@ -125,6 +125,73 @@ class LineplotTest < Test::Unit::TestCase
125
125
  output)
126
126
  end
127
127
 
128
+ test("limits") do
129
+ plot = UnicodePlot.lineplot(@x, @y, xlim: [-1.5, 3.5], ylim: [-5.5, 2.5])
130
+ _, output = with_term { plot.render($stdout) }
131
+ assert_equal(fixture_path("lineplot/limits.txt").read,
132
+ output)
133
+ end
134
+
135
+ test("nogrid") do
136
+ plot = UnicodePlot.lineplot(@x, @y, grid: false)
137
+ _, output = with_term { plot.render($stdout) }
138
+ assert_equal(fixture_path("lineplot/nogrid.txt").read,
139
+ output)
140
+ end
141
+
142
+ test("color: :blue") do
143
+ plot = UnicodePlot.lineplot(@x, @y, color: :blue, name: "points1")
144
+ _, output = with_term { plot.render($stdout) }
145
+ assert_equal(fixture_path("lineplot/blue.txt").read,
146
+ output)
147
+ end
148
+
149
+ test("parameters") do
150
+ plot = UnicodePlot.lineplot(@x, @y,
151
+ name: "points1",
152
+ title: "Scatter",
153
+ xlabel: "x",
154
+ ylabel: "y")
155
+ _, output = with_term { plot.render($stdout) }
156
+ assert_equal(fixture_path("lineplot/parameters1.txt").read,
157
+ output)
158
+
159
+ assert_same(plot,
160
+ UnicodePlot.lineplot!(plot,
161
+ [0.5, 1, 1.5],
162
+ name: "points2"))
163
+ _, output = with_term { plot.render($stdout) }
164
+ assert_equal(fixture_path("lineplot/parameters2.txt").read,
165
+ output)
166
+
167
+ assert_same(plot,
168
+ UnicodePlot.lineplot!(plot,
169
+ [-0.5, 0.5, 1.5],
170
+ [0.5, 1, 1.5],
171
+ name: "points3"))
172
+ _, output = with_term { plot.render($stdout) }
173
+ assert_equal(fixture_path("lineplot/parameters3.txt").read,
174
+ output)
175
+ output = StringIO.open do |sio|
176
+ plot.render(sio)
177
+ sio.close
178
+ sio.string
179
+ end
180
+ assert_equal(fixture_path("lineplot/nocolor.txt").read,
181
+ output)
182
+ end
183
+
184
+ test("canvas size") do
185
+ plot = UnicodePlot.lineplot(@x, @y,
186
+ title: "Scatter",
187
+ canvas: :dot,
188
+ width: 10,
189
+ height: 5)
190
+ _, output = with_term { plot.render($stdout) }
191
+ assert_equal(fixture_path("lineplot/canvassize.txt").read,
192
+ output)
193
+ end
194
+
128
195
  # TODO: functions
129
196
 
130
197
  # TODO: stairs
@@ -0,0 +1,146 @@
1
+ class ScatterplotTest < Test::Unit::TestCase
2
+ include Helper::Fixture
3
+ include Helper::WithTerm
4
+
5
+ def setup
6
+ @x = [-1, 1, 3, 3, -1]
7
+ @y = [2, 0, -5, 2, -5]
8
+ end
9
+
10
+ test("errors") do
11
+ assert_raise(ArgumentError) do
12
+ UnicodePlot.scatterplot()
13
+ end
14
+ assert_raise(ArgumentError) do
15
+ UnicodePlot.scatterplot([1, 2], [1, 2, 3])
16
+ end
17
+ assert_raise(ArgumentError) do
18
+ UnicodePlot.scatterplot([1, 2, 3], [1, 2])
19
+ end
20
+ assert_raise(ArgumentError) do
21
+ UnicodePlot.scatterplot(1..3, 1..2)
22
+ end
23
+ end
24
+
25
+ test("default") do
26
+ plot = UnicodePlot.scatterplot(@x, @y)
27
+ _, output = with_term { plot.render($stdout) }
28
+ assert_equal(fixture_path("scatterplot/default.txt").read,
29
+ output)
30
+ end
31
+
32
+ test("y only") do
33
+ plot = UnicodePlot.scatterplot(@y)
34
+ _, output = with_term { plot.render($stdout) }
35
+ assert_equal(fixture_path("scatterplot/y_only.txt").read,
36
+ output)
37
+ end
38
+
39
+ test("one range") do
40
+ plot = UnicodePlot.scatterplot(6..10)
41
+ _, output = with_term { plot.render($stdout) }
42
+ assert_equal(fixture_path("scatterplot/range1.txt").read,
43
+ output)
44
+ end
45
+
46
+ test("two ranges") do
47
+ plot = UnicodePlot.scatterplot(11..15, 6..10)
48
+ _, output = with_term { plot.render($stdout) }
49
+ assert_equal(fixture_path("scatterplot/range2.txt").read,
50
+ output)
51
+ end
52
+
53
+ test("scale1") do
54
+ x = @x.map {|a| a * 1e3 + 15 }
55
+ y = @y.map {|a| a * 1e-3 - 15 }
56
+ plot = UnicodePlot.scatterplot(x, y)
57
+ _, output = with_term { plot.render($stdout) }
58
+ assert_equal(fixture_path("scatterplot/scale1.txt").read,
59
+ output)
60
+ end
61
+
62
+ test("scale2") do
63
+ x = @x.map {|a| a * 1e-3 + 15 }
64
+ y = @y.map {|a| a * 1e3 - 15 }
65
+ plot = UnicodePlot.scatterplot(x, y)
66
+ _, output = with_term { plot.render($stdout) }
67
+ assert_equal(fixture_path("scatterplot/scale2.txt").read,
68
+ output)
69
+ end
70
+
71
+ test("scale3") do
72
+ miny = -1.2796649117521434e218
73
+ maxy = -miny
74
+ plot = UnicodePlot.scatterplot([1], [miny], xlim: [1, 1], ylim: [miny, maxy])
75
+ _, output = with_term { plot.render($stdout) }
76
+ expected = fixture_path("scatterplot/scale3.txt").read
77
+ assert_equal(expected, output)
78
+ end
79
+
80
+ test("limits") do
81
+ plot = UnicodePlot.scatterplot(@x, @y, xlim: [-1.5, 3.5], ylim: [-5.5, 2.5])
82
+ _, output = with_term { plot.render($stdout) }
83
+ assert_equal(fixture_path("scatterplot/limits.txt").read,
84
+ output)
85
+ end
86
+
87
+ test("nogrid") do
88
+ plot = UnicodePlot.scatterplot(@x, @y, grid: false)
89
+ _, output = with_term { plot.render($stdout) }
90
+ assert_equal(fixture_path("scatterplot/nogrid.txt").read,
91
+ output)
92
+ end
93
+
94
+ test("blue") do
95
+ plot = UnicodePlot.scatterplot(@x, @y, color: :blue, name: "points1")
96
+ _, output = with_term { plot.render($stdout) }
97
+ assert_equal(fixture_path("scatterplot/blue.txt").read,
98
+ output)
99
+ end
100
+
101
+ test("parameters") do
102
+ plot = UnicodePlot.scatterplot(@x, @y,
103
+ name: "points1",
104
+ title: "Scatter",
105
+ xlabel: "x",
106
+ ylabel: "y")
107
+ _, output = with_term { plot.render($stdout) }
108
+ expected = fixture_path("scatterplot/parameters1.txt").read
109
+ assert_equal(expected, output)
110
+
111
+ assert_same(plot,
112
+ UnicodePlot.scatterplot!(plot,
113
+ [0.5, 1, 1.5],
114
+ name: "points2"))
115
+ _, output = with_term { plot.render($stdout) }
116
+ assert_equal(fixture_path("scatterplot/parameters2.txt").read,
117
+ output)
118
+
119
+ assert_same(plot,
120
+ UnicodePlot.scatterplot!(plot,
121
+ [-0.5, 0.5, 1.5],
122
+ [0.5, 1, 1.5],
123
+ name: "points3"))
124
+ _, output = with_term { plot.render($stdout) }
125
+ assert_equal(fixture_path("scatterplot/parameters3.txt").read,
126
+ output)
127
+ output = StringIO.open do |sio|
128
+ plot.render(sio)
129
+ sio.close
130
+ sio.string
131
+ end
132
+ assert_equal(fixture_path("scatterplot/nocolor.txt").read,
133
+ output)
134
+ end
135
+
136
+ test("canvas size") do
137
+ plot = UnicodePlot.scatterplot(@x, @y,
138
+ title: "Scatter",
139
+ canvas: :dot,
140
+ width: 10,
141
+ height: 5)
142
+ _, output = with_term { plot.render($stdout) }
143
+ expected = fixture_path("scatterplot/canvassize.txt").read
144
+ assert_equal(expected, output)
145
+ end
146
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: unicode_plot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - mrkn
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-06-14 00:00:00.000000000 Z
11
+ date: 2019-06-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: enumerable-statistics
@@ -83,9 +83,16 @@ files:
83
83
  - lib/unicode_plot/boxplot.rb
84
84
  - lib/unicode_plot/braille_canvas.rb
85
85
  - lib/unicode_plot/canvas.rb
86
+ - lib/unicode_plot/density_canvas.rb
87
+ - lib/unicode_plot/densityplot.rb
88
+ - lib/unicode_plot/dot_canvas.rb
89
+ - lib/unicode_plot/grid_plot.rb
90
+ - lib/unicode_plot/histogram.rb
86
91
  - lib/unicode_plot/lineplot.rb
92
+ - lib/unicode_plot/lookup_canvas.rb
87
93
  - lib/unicode_plot/plot.rb
88
94
  - lib/unicode_plot/renderer.rb
95
+ - lib/unicode_plot/scatterplot.rb
89
96
  - lib/unicode_plot/styled_printer.rb
90
97
  - lib/unicode_plot/utils.rb
91
98
  - lib/unicode_plot/value_transformer.rb
@@ -97,7 +104,10 @@ files:
97
104
  - test/test-barplot.rb
98
105
  - test/test-boxplot.rb
99
106
  - test/test-canvas.rb
107
+ - test/test-densityplot.rb
108
+ - test/test-histogram.rb
100
109
  - test/test-lineplot.rb
110
+ - test/test-scatterplot.rb
101
111
  - unicode_plot.gemspec
102
112
  homepage: https://github.com/red-data-tools/unicode_plot.rb
103
113
  licenses:
@@ -123,11 +133,14 @@ signing_key:
123
133
  specification_version: 4
124
134
  summary: Plot your data by Unicode characters
125
135
  test_files:
136
+ - test/test-scatterplot.rb
126
137
  - test/run-test.rb
127
138
  - test/test-barplot.rb
128
139
  - test/helper.rb
129
140
  - test/test-lineplot.rb
141
+ - test/test-densityplot.rb
130
142
  - test/test-canvas.rb
143
+ - test/test-histogram.rb
131
144
  - test/helper/with_term.rb
132
145
  - test/helper/fixture.rb
133
146
  - test/test-boxplot.rb