ruby-plot 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.0
1
+ 0.6.0
data/lib/plot_bars.rb CHANGED
@@ -6,6 +6,7 @@ module RubyPlot
6
6
  #puts "'"+title+"',"+measures.inspect+","+algorithms.inspect+","
7
7
 
8
8
  measures = measures.collect{|m| m.gsub(/_/,'-')}
9
+ algorithms = algorithms.each{|m| m[0].gsub!(/_/,'-')}
9
10
 
10
11
  Gnuplot.open do |gp|
11
12
  Gnuplot::Plot.new( gp ) do |plot|
@@ -63,11 +64,11 @@ module RubyPlot
63
64
 
64
65
  def self.test_plot_bars
65
66
  x = ['ACC_with_very_long_name_consider_that', 'AUC', 'SPEC', 'SENS']
66
- data = [['Alg1', 1.00, 1.00, 1.00, 1.00], ['Alg2', 0.75, 0.75, 0.75, 0.75], ['Alg3', 0.50, 0.50, 0.50, 0.50]]
67
+ data = [['Baller_Algorithm_1', 1.00, 1.00, 1.00, 1.00], ['Alg2', 0.75, 0.75, 0.75, 0.75], ['Alg3', 0.50, 0.50, 0.50, 0.50]]
67
68
  plot_bars('Vergleich der Algorithmen', x, data, '/tmp/hist.svg')
68
69
 
69
70
  x = ['ACC_with_very_long_name_consider_that', 'AUC', 'SPEC', 'SENS']
70
- data = [['Alg1', 1.00, 1.00, 1.00, 1.00], ['Alg2', 0.75, 0.75, 0.75, 0.75], ['Alg3', 0.50, 0.50, 0.50, 0.50]]
71
+ data = [['Baller_Algorithm_1', 1.00, 1.00, 1.00, 1.00], ['Alg2', 0.75, 0.75, 0.75, 0.75], ['Alg3', 0.50, 0.50, 0.50, 0.50]]
71
72
  plot_bars('Vergleich der Algorithmen', x, data, '/tmp/hist.png')
72
73
  end
73
74
 
data/lib/plot_box.rb ADDED
@@ -0,0 +1,113 @@
1
+ require "tempfile"
2
+
3
+ module RubyPlot
4
+
5
+ def self.box_plot(path, title, y_lable, names, values)
6
+
7
+ LOGGER.debug "plot box -- names "+names.inspect
8
+ LOGGER.debug "plot box -- values "+values.inspect
9
+
10
+ # STDOUT.sync = true
11
+ raise if names.length != values.length
12
+ gnuplot = '/home/martin/software/gnuplot-dev/install/bin/gnuplot'
13
+
14
+ tmp_datasets = []
15
+ tmp_file = Tempfile.new("data.dat")
16
+ tmp_datasets << tmp_file
17
+ value_string = []
18
+ values.first.size.times do |i|
19
+ v = ""
20
+ values.each do |val|
21
+ v += val[i].to_s+" "
22
+ end
23
+ value_string << v.to_s
24
+ end
25
+ #puts value_string.join("\n")
26
+
27
+ tmp_file.puts value_string.join("\n")
28
+ tmp_file.close
29
+ LOGGER.debug "plot box -- dataset "+tmp_datasets.collect{|d| d.path}.inspect
30
+
31
+ #"name1" 1, "name2" 2, "name3" 3 ...
32
+ xtics_string = ""
33
+ names.size.times do |i|
34
+ xtics_string += "\""+names[i].to_s+"\" "+(i+1).to_s
35
+ xtics_string += ", " if i<names.size-1
36
+ end
37
+ #puts xtics_string
38
+
39
+ #plot '#{tmp_file.path}' using (1):1, '' using (2):2, '' using (3):3 ...
40
+ plot_string = "plot '#{tmp_file.path}' using (1):1"
41
+ if names.size>1
42
+ plot_string += ","
43
+ names.size.times do |i|
44
+ if i>0
45
+ plot_string += " '' using (#{i+1}):#{i+1}"
46
+ plot_string += "," if i<names.size-1
47
+ end
48
+ end
49
+ end
50
+ #puts plot_string
51
+ #exit
52
+
53
+ plt = <<EOF
54
+ set terminal svg
55
+ set output '#{path}'
56
+ set border 2 front linetype -1 linewidth 1.000
57
+ set boxwidth 0.75 absolute
58
+ set style fill solid 0.25 border lt -1
59
+ unset key
60
+ set pointsize 0.5
61
+ set style data boxplot
62
+ set xtics border in scale 0,0 nomirror rotate offset character 0, 0, 0
63
+ set xtics norangelimit
64
+ set xtics (#{xtics_string})
65
+ set ytics border in scale 1,0.5 nomirror norotate offset character 0, 0, 0
66
+ #set yrange [ 0.00000 : 100.000 ] noreverse nowriteback
67
+ #plot '#{tmp_file.path}' using (1):1, '' using (2):2, '' using (3):3
68
+ EOF
69
+
70
+ plt += plot_string
71
+ #puts plt
72
+ #exit
73
+
74
+ tmp_file = Tempfile.new("config.plt")
75
+ tmp_datasets << tmp_file
76
+ tmp_file.puts plt
77
+ tmp_file.close
78
+
79
+ # start gnuplot with created *.plt file
80
+ cmd = gnuplot+" "+tmp_file.path+" 2>&1"
81
+ LOGGER.debug "plot box -- running gnuplot '"+cmd+"'"
82
+ response = ""
83
+ IO.popen(cmd) do |f|
84
+ while line = f.gets
85
+ response += line
86
+ end
87
+ end
88
+ raise "gnuplot failes (cmd: "+cmd.to_s+", out: "+response.to_s+")" unless $?==0
89
+ LOGGER.info "plot box -- RESULT: #{path}"
90
+
91
+ tmp_datasets.each{|f| f.delete}
92
+ end
93
+
94
+ def self.test_plot_box
95
+ # regression_point_plot("/tmp/regression.png" , "name of title", "x-values", "y-values", ["this-one-has-a-very-very-very-long-name", "test" ],
96
+ # [[0.20,0.60,0.80,0.20,1.0,0.001], [0.10,0.25,0.70,0.95,0.2,0.3434]],
97
+ # [[0.15,0.50,0.90,0.2,9,0.5],[0.20,0.40,0.50,0.70,0.3,0.234589]])
98
+ # accuracy_confidence_plot("/tmp/accuracy-conf.png" , "name of title", "x-values", "y-values", ["test" ],
99
+ # [[0.9,0.5,0.3,0.1]],
100
+ # [[100,90,70,30]])
101
+ x=[]; y=[]; z=[]; zz=[]
102
+ 30.times do
103
+ x << 5 + rand * 4 * (rand<0.5 ? 1 : -1)
104
+ y << 5 + rand * 5 * (rand<0.5 ? 1 : -1)
105
+ z << 5 + rand * 6 * (rand<0.5 ? 1 : -1)
106
+ zz << 5 + rand * 5 * (rand<0.5 ? 1 : -1)
107
+ end
108
+
109
+ box_plot("/tmp/test-plot.svg" , "name of title", "values", ["x","y","z","zz"], [x,y,z,zz])
110
+ end
111
+
112
+ end
113
+
data/lib/plot_lines.rb CHANGED
@@ -23,14 +23,13 @@ module RubyPlot
23
23
 
24
24
  # gnuplot check
25
25
  gnuplot=`which gnuplot | grep -o gnuplot`
26
- if gnuplot == "gnuplot\n"
27
- LOGGER.debug "Gnuplot is already installed."
28
- else
26
+ if gnuplot!="gnuplot\n"
29
27
  raise "Please install gnuplot.\n"+
30
28
  "sudo apt-get install gnuplot"
31
29
  end
32
30
 
33
31
  output_dat_arr = Array.new
32
+ tmp_datasets = []
34
33
 
35
34
  # -----------------------------------------------------
36
35
  # create *.dat files of imported data for gnuplot
@@ -63,16 +62,16 @@ module RubyPlot
63
62
  #write *.dat files
64
63
  #-----------------------------------------------------
65
64
  #write output_dat_arr content in new *.dat file
66
- File.open( "data#{i}.dat", "w" ) do |the_file|
67
- the_file.puts output_dat_arr
68
- end
69
- LOGGER.debug "data#{i}.dat created."
65
+ tmp_file = Tempfile.new("data#{i}.dat")
66
+ tmp_datasets << tmp_file
67
+ tmp_file.puts output_dat_arr
68
+ tmp_file.close
70
69
  output_dat_arr.clear
71
-
72
70
  else
73
71
  raise "num x-values != y-values: "+plot_data[i].inspect
74
72
  end
75
73
  end
74
+ LOGGER.debug "plot lines -- datasets "+tmp_datasets.collect{|d| d.path}.inspect
76
75
 
77
76
  # -----------------------------------------------------
78
77
  # create *.plt file for gnuplot
@@ -135,9 +134,9 @@ module RubyPlot
135
134
  style = plot_data[i].faint ? "lw 2" : "lw 4"
136
135
 
137
136
  if i == plot_data.length-1
138
- output_plt_arr.push " 'data#{i}.dat' using 2:1 title '#{plot_data[i].name}' with lines #{style}"
137
+ output_plt_arr.push " '"+tmp_datasets[i].path+"' using 2:1 title '#{plot_data[i].name}' with lines #{style}"
139
138
  else
140
- output_plt_arr.push " 'data#{i}.dat' using 2:1 title '#{plot_data[i].name}' with lines #{style}, \\"
139
+ output_plt_arr.push " '"+tmp_datasets[i].path+"' using 2:1 title '#{plot_data[i].name}' with lines #{style}, \\"
141
140
  end
142
141
  end
143
142
  output_plt_arr.push ""
@@ -147,13 +146,14 @@ module RubyPlot
147
146
  # write *.plt files
148
147
  # -----------------------------------------------------
149
148
  # write output_dat_arr content in new *.dat file
150
- File.open( "config.plt", "w" ) do |the_file|
151
- the_file.puts output_plt_arr
152
- end
153
- LOGGER.debug "config.plt created, running gnuplot"
149
+ tmp_file = Tempfile.new("config.plt")
150
+ tmp_datasets << tmp_file
151
+ tmp_file.puts output_plt_arr
152
+ tmp_file.close
154
153
 
155
154
  # start gnuplot with created *.plt file
156
- cmd = "gnuplot config.plt 2>&1"
155
+ cmd = "gnuplot "+tmp_file.path+" 2>&1"
156
+ LOGGER.debug "plot lines -- running gnuplot '"+cmd+"'"
157
157
  response = ""
158
158
  IO.popen(cmd) do |f|
159
159
  while line = f.gets
@@ -161,18 +161,12 @@ module RubyPlot
161
161
  end
162
162
  end
163
163
  raise "gnuplot failes (cmd: "+cmd.to_s+", out: "+response.to_s+")" unless $?==0
164
-
165
- LOGGER.debug "#{path} created. "
164
+ LOGGER.info "plot lines -- RESULT: #{path}"
166
165
 
167
166
  # -----------------------------------------------------
168
167
  # remove *.plt and *.dat files
169
168
  # -----------------------------------------------------
170
- `rm config.plt`
171
- LOGGER.debug "config.plt removed."
172
- for i in 0..plot_data.length-1
173
- `rm data#{i}.dat`
174
- LOGGER.debug "data#{i}.dat removed."
175
- end
169
+ tmp_datasets.each{|f| f.delete}
176
170
  end
177
171
 
178
172
  def self.test_plot_lines
data/lib/plot_points.rb CHANGED
@@ -10,6 +10,10 @@ module RubyPlot
10
10
 
11
11
  def self.regression_point_plot(path, title, x_lable, y_lable, names, x_values, y_values, log=true) #, quadratic_scale=true, line_points=false, reverse_x=false)
12
12
 
13
+ LOGGER.debug "plot regr -- names "+names.inspect
14
+ LOGGER.debug "plot regr -- x "+x_values.inspect
15
+ LOGGER.debug "plot regr -- y "+y_values.inspect
16
+
13
17
  min = Float::MAX
14
18
  max = -Float::MAX
15
19
  (0..x_values.size-1).each do |i|
@@ -40,7 +44,11 @@ module RubyPlot
40
44
  plot_points(path, title, x_lable, y_lable, names, x_values, y_values, log, x_range, y_range, true, true, false, false, false)
41
45
  end
42
46
 
43
- def self.accuracy_confidence_plot(path, title, x_lable, y_lable, names, x_values, y_values, reverse_y=false) #y_range=nil,
47
+ def self.confidence_plot(path, title, x_lable, y_lable, names, x_values, y_values, y_range=nil)
48
+
49
+ LOGGER.debug "plot conf -- names "+names.inspect
50
+ LOGGER.debug "plot conf -- x "+x_values.inspect
51
+ LOGGER.debug "plot conf -- y "+y_values.inspect
44
52
 
45
53
  min = Float::MAX
46
54
  max = -Float::MAX
@@ -51,18 +59,24 @@ module RubyPlot
51
59
  border = (max-min)*0.1
52
60
  min_border = min-border
53
61
  max_border = max+border
54
- y_range = min==max ? nil : [min_border, max_border]
55
-
56
- plot_points(path, title, x_lable, y_lable, names, x_values, y_values, false, nil, y_range, false, false, true, true, reverse_y)
62
+
63
+ if (y_range==nil) # use own computed range only if not explicitly definded...
64
+ y_range = min==max ? nil : [min_border, max_border]
65
+ elsif (y_range[0] > max_border ) #.. or if values out of scope
66
+ y_range[0] = min_border
67
+ elsif (y_range[1] < min_border )
68
+ y_range[1] = max_border
69
+ end
70
+ plot_points(path, title, x_lable, y_lable, names, x_values, y_values, false, nil, y_range, false, false, true, true, false)
57
71
  end
58
72
 
59
73
  def self.plot_points(path, title, x_lable, y_lable, names, x_values, y_values,
60
74
  log=true, x_range=nil, y_range=nil, quadratic_scale=true, draw_diagonale=true, line_points=false, reverse_x=false, reverse_y=false)
61
75
 
62
- LOGGER.debug "ruby-plot: names "+names.inspect
63
- LOGGER.debug "ruby-plot: x "+x_values.inspect
64
- LOGGER.debug "ruby-plot: y "+y_values.inspect
65
- LOGGER.debug "ruby-plot: y_range "+y_range.inspect
76
+ LOGGER.debug "plot points -- names "+names.inspect
77
+ LOGGER.debug "plot points -- x "+x_values.inspect
78
+ LOGGER.debug "plot points -- y "+y_values.inspect
79
+ LOGGER.debug "plot points -- y_range "+y_range.inspect
66
80
 
67
81
  data = []
68
82
  (0..x_values.size-1).each do |i|
@@ -77,7 +91,7 @@ module RubyPlot
77
91
  # -----------------------------------------------------
78
92
  # check parameters
79
93
  status=false
80
- LOGGER.debug "#{names.length} algs entered"
94
+ #LOGGER.debug "#{names.length} algs entered"
81
95
 
82
96
  #LOGGER.debug names.inspect
83
97
  #LOGGER.debug data.inspect
@@ -94,17 +108,12 @@ module RubyPlot
94
108
 
95
109
  # gnuplot check
96
110
  gnuplot=`which gnuplot | grep -o gnuplot`
97
- if gnuplot == "gnuplot\n"
98
- LOGGER.debug "Gnuplot is already installed."
99
- else
111
+ if gnuplot != "gnuplot\n"
100
112
  raise "Please install gnuplot.\n"+
101
113
  "sudo apt-get install gnuplot"
102
114
  end
103
-
104
- dat_number=0
105
-
106
115
  output_dat_arr = Array.new
107
-
116
+ tmp_datasets = []
108
117
 
109
118
  # -----------------------------------------------------
110
119
  # create *.dat files of imported data for gnuplot
@@ -134,16 +143,17 @@ module RubyPlot
134
143
  #write *.dat files
135
144
  #-----------------------------------------------------
136
145
  #write output_dat_arr content in new *.dat file
137
- File.open( "data#{i}.dat", "w" ) do |the_file|
138
- the_file.puts output_dat_arr
139
- end
140
- LOGGER.debug "data#{i}.dat created."
146
+
147
+ tmp_file = Tempfile.new("data#{i}.dat")
148
+ tmp_datasets << tmp_file
149
+ tmp_file.puts output_dat_arr
150
+ tmp_file.close
141
151
  output_dat_arr.clear
142
-
143
152
  else
144
153
  raise "Data pair of #{names[i]} have no the same number of elements."
145
154
  end
146
155
  end
156
+ LOGGER.debug "plot points -- datasets "+tmp_datasets.collect{|d| d.path}.inspect
147
157
 
148
158
  # -----------------------------------------------------
149
159
  # create *.plt file for gnuplot
@@ -203,28 +213,38 @@ module RubyPlot
203
213
  i = 0
204
214
  for i in 0..names.length-1
205
215
  if i == names.length-1
206
- output_plt_arr.push " 'data#{i}.dat' using 2:1 title '#{names[i]}' with "+style.to_s
216
+ output_plt_arr.push " '"+tmp_datasets[i].path+"' using 2:1 title '#{names[i]}' with "+style.to_s
207
217
  else
208
- output_plt_arr.push " 'data#{i}.dat' using 2:1 title '#{names[i]}' with "+style.to_s+", \\"
218
+ output_plt_arr.push " '"+tmp_datasets[i].path+"' using 2:1 title '#{names[i]}' with "+style.to_s+", \\"
209
219
  end
220
+
221
+ #output_plt_arr.push " '"+tmp_datasets[i].path+"' using 2:1 title '#{names[i]}' with "+style.to_s
222
+ #output_plt_arr[-1] = output_plt_arr[-1]+", \\" if names.size==1 or i<names.length-1
223
+ #
224
+ #if names.size==1
225
+ # output_plt_arr.push " '"+tmp_datasets[i].path+"' using 2:1 smooth bezier notitle with lines"
226
+ # output_plt_arr[-1] = output_plt_arr[-1]+", \\" if i<names.length-1
227
+ #end
210
228
  end
211
229
  output_plt_arr.push ""
212
230
  output_plt_arr.push ""
213
-
214
-
231
+
232
+ #puts output_plt_arr.join("\n")
233
+
215
234
  #output_plt_arr << "plot f(x)"
216
235
 
217
236
  # -----------------------------------------------------
218
237
  # write *.plt files
219
238
  # -----------------------------------------------------
220
239
  # write output_dat_arr content in new *.dat file
221
- File.open( "config.plt", "w" ) do |the_file|
222
- the_file.puts output_plt_arr
223
- end
224
- LOGGER.debug "config.plt created, running gnuplot"
240
+ tmp_file = Tempfile.new("config.plt")
241
+ tmp_datasets << tmp_file
242
+ tmp_file.puts output_plt_arr
243
+ tmp_file.close
225
244
 
226
245
  # start gnuplot with created *.plt file
227
- cmd = "gnuplot config.plt 2>&1"
246
+ cmd = "gnuplot "+tmp_file.path+" 2>&1"
247
+ LOGGER.debug "plot points -- running gnuplot '"+cmd+"'"
228
248
  response = ""
229
249
  IO.popen(cmd) do |f|
230
250
  while line = f.gets
@@ -232,28 +252,34 @@ module RubyPlot
232
252
  end
233
253
  end
234
254
  raise "gnuplot failes (cmd: "+cmd.to_s+", out: "+response.to_s+")" unless $?==0
235
-
236
- LOGGER.debug "#{path} created. "
255
+ LOGGER.info "plot points -- RESULT: #{path}"
237
256
 
238
257
  # -----------------------------------------------------
239
258
  # remove *.plt and *.dat files
240
259
  # -----------------------------------------------------
241
- `rm config.plt`
242
- LOGGER.debug "config.plt removed."
243
- for i in 0..names.length-1
244
- `rm data#{i}.dat`
245
- LOGGER.debug "data#{i}.dat removed."
246
- end
260
+ tmp_datasets.each{|f| f.delete}
247
261
  end
248
262
 
249
263
  def self.test_plot_points
250
- regression_point_plot("/tmp/regression.png" , "name of title", "x-values", "y-values", ["this-one-has-a-very-very-very-long-name", "test" ],
251
- [[0.20,0.60,0.80,0.20,1.0,0.001], [0.10,0.25,0.70,0.95,0.2,0.3434]],
252
- [[0.15,0.50,0.90,0.2,9,0.5],[0.20,0.40,0.50,0.70,0.3,0.234589]])
253
-
254
- accuracy_confidence_plot("/tmp/accuracy-conf.png" , "name of title", "x-values", "y-values", ["test" ],
255
- [[0.9,0.5,0.3,0.1]],
256
- [[100,90,70,30]])
264
+ # regression_point_plot("/tmp/regression.png" , "name of title", "x-values", "y-values", ["this-one-has-a-very-very-very-long-name", "test" ],
265
+ # [[0.20,0.60,0.80,0.20,1.0,0.001], [0.10,0.25,0.70,0.95,0.2,0.3434]],
266
+ # [[0.15,0.50,0.90,0.2,9,0.5],[0.20,0.40,0.50,0.70,0.3,0.234589]])
267
+ # accuracy_confidence_plot("/tmp/accuracy-conf.png" , "name of title", "x-values", "y-values", ["test" ],
268
+ # [[0.9,0.5,0.3,0.1]],
269
+ # [[100,90,70,30]])
270
+
271
+ x = []
272
+ y = []
273
+ noise = 0
274
+ 100.times do |i|
275
+ i += 1
276
+ noise += rand**2 * (rand<0.5 ? 1 : -1)
277
+ x << i
278
+ y << 1/i + noise
279
+ end
280
+ confidence_plot("/tmp/test-plot.svg" , "name of title", "x-values", "y-values", ["test"],
281
+ [x],
282
+ [y])
257
283
  end
258
284
 
259
285
  private
data/lib/ruby-plot.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'rubygems'
2
2
  require 'logger'
3
3
  require 'gnuplot'
4
+ require 'tempfile'
4
5
 
5
6
  unless(defined? LOGGER)
6
7
  LOGGER = Logger.new(STDOUT)
@@ -10,7 +11,9 @@ end
10
11
  require "plot_bars.rb"
11
12
  require "plot_lines.rb"
12
13
  require "plot_points.rb"
14
+ require "plot_box.rb"
13
15
 
14
16
  #RubyPlot::test_plot_lines
15
17
  #RubyPlot::test_plot_bars
16
18
  #RubyPlot::test_plot_points
19
+ #RubyPlot::test_plot_box
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-plot
3
3
  version: !ruby/object:Gem::Version
4
- hash: 11
4
+ hash: 7
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 5
8
+ - 6
9
9
  - 0
10
- version: 0.5.0
10
+ version: 0.6.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - David Vorgrimmler
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2011-05-24 00:00:00 +02:00
19
+ date: 2011-11-25 00:00:00 +01:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency
@@ -46,6 +46,7 @@ files:
46
46
  - Rakefile
47
47
  - VERSION
48
48
  - lib/plot_bars.rb
49
+ - lib/plot_box.rb
49
50
  - lib/plot_lines.rb
50
51
  - lib/plot_points.rb
51
52
  - lib/ruby-plot.rb