gnuplotr 0.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.
Files changed (4) hide show
  1. data/README.markdown +34 -0
  2. data/lib/gnuplotr.rb +115 -0
  3. data/test.rb +32 -0
  4. metadata +59 -0
data/README.markdown ADDED
@@ -0,0 +1,34 @@
1
+ GNUPlotr: an easy interface between Ruby and GNUPlot
2
+ ====================================================
3
+
4
+ Introduction
5
+ ------------
6
+
7
+ Here's a quick example:
8
+
9
+ require "gnuplotr"
10
+
11
+ # Instantiate
12
+ gp = GNUPlotr.new
13
+
14
+ # add an empty data series
15
+ gp.new_series :parabola
16
+
17
+ # fill the series with pairs. This creates the parabola.dat file
18
+ gp.fill_series(:parabola) do |series|
19
+ (0..99).each do |i|
20
+ series << [i, i**2]
21
+ end
22
+ end
23
+
24
+ # enable command history recording
25
+ gp.record = true
26
+
27
+ # issue plotting commands, either with named data series
28
+ gp.plot :parabola, "using 1:2 with points"
29
+
30
+ # or with formulas. Options are collected in a string passed as second optional argument
31
+ gp.replot "x**2", "with lines"
32
+
33
+ # command history can be dumper and possibly saved on file to be edited or loaded again later on.
34
+ puts gp.dump_input
data/lib/gnuplotr.rb ADDED
@@ -0,0 +1,115 @@
1
+ #!/usr/bin/env ruby
2
+ # gnuplotr.rb
3
+
4
+ # Created by Paolo Bosetti on 2011-02-23.
5
+ # Copyright (c) 2011 University of Trento. All rights reserved.
6
+
7
+ require "open3"
8
+
9
+ class DataSeries
10
+ def initialize(name)
11
+ raise ArgumentError, "name must be a Symbol" unless name.kind_of? Symbol
12
+ @name = name
13
+ @width = nil
14
+ @length = 0
15
+ @handle = File.open("#{@name.to_s}.dat", "w")
16
+ @handle.puts "\# #{@name} datafile generated on #{Time.now} by GNUPlotr"
17
+ end
18
+
19
+ def <<(ary)
20
+ @width = ary.size unless @width
21
+ raise ArgumentError, "record size mismatch (not #{@width})" unless ary.size == @width
22
+ @handle.puts(ary * "\t")
23
+ @length += 1
24
+ end
25
+
26
+ def close
27
+ @handle.close
28
+ end
29
+
30
+ end
31
+
32
+ class GNUPlotr
33
+ attr_accessor :gnuplot_path, :series, :record
34
+ def initialize(path=nil)
35
+ @gnuplot_path = (path || '/opt/local/bin/gnuplot')
36
+ raise RuntimeError, "Could not find #{@gnuplot_path}" unless File.exist? @gnuplot_path
37
+ @gnuplot, @stdout, @stderr = Open3.popen3(@gnuplot_path)
38
+ Thread.new { capture(@stdout) {|line| puts "> " + line } }
39
+ Thread.new { capture(@stderr) {|line| warn ">>" + line } }
40
+ @series = {}
41
+ @record = false
42
+ @record_list = []
43
+ end
44
+
45
+ def new_series(name)
46
+ raise ArgumentError, "name must be a Symbol" unless name.kind_of? Symbol
47
+ @series[name] = DataSeries.new(name)
48
+ end
49
+
50
+ def fill_series(name)
51
+ raise "Need a block" unless block_given?
52
+ yield @series[name]
53
+ @series[name].close
54
+ end
55
+
56
+ def raw(message)
57
+ @record_list << message if @record
58
+ @gnuplot.puts message
59
+ end
60
+
61
+ def plot(arg, opts=nil)
62
+ _plot("plot", arg, opts)
63
+ end
64
+
65
+ def replot(arg, opts=nil)
66
+ _plot("replot", arg, opts)
67
+ end
68
+
69
+ def dump_input
70
+ header = "\# Gnuplot inputfile generated on #{Time.now} by GNUPlotr\n"
71
+ header + @record_list * "\n"
72
+ end
73
+
74
+ def reset
75
+ @record_list = []
76
+ end
77
+
78
+ private
79
+ def _plot(cmd, arg, opts=nil)
80
+ case arg
81
+ when String
82
+ raw "#{cmd} #{arg} #{opts}"
83
+ when Symbol
84
+ raw "#{cmd} '#{arg.to_s}.dat' #{opts}"
85
+ else
86
+ raise ArgumentError
87
+ end
88
+ end
89
+
90
+ def capture(handle, &block)
91
+ loop do
92
+ line = handle.gets.chomp
93
+ yield line if line =~ /^\s*\w+/
94
+ end
95
+ end
96
+ end
97
+
98
+
99
+
100
+ if $0 == __FILE__
101
+ gp = GNUPlotr.new
102
+ gp.new_series :parabola
103
+ gp.fill_series(:parabola) do |s|
104
+ (0..99).each do |i|
105
+ s << [i, i**2]
106
+ end
107
+ end
108
+ gp.record = true
109
+ gp.plot :parabola, "using 1:2"
110
+ gp.replot "x**2", "with linespoints"
111
+ gp.raw "show term"
112
+ gp.raw "quit()"
113
+ sleep 0.1
114
+ puts gp.dump_input
115
+ end
data/test.rb ADDED
@@ -0,0 +1,32 @@
1
+ ##!/usr/bin/env ruby
2
+ # test.rb
3
+
4
+ # Created by Paolo Bosetti on 2011-02-22.
5
+ # Copyright (c) 2011 University of Trento. All rights reserved.
6
+
7
+ require './lib/gnuplotr'
8
+
9
+ # Instantiate
10
+ gp = GNUPlotr.new
11
+
12
+ # add an empty data series
13
+ gp.new_series :parabola
14
+
15
+ # fill the series with pairs. This creates the parabola.dat file
16
+ gp.fill_series(:parabola) do |series|
17
+ (0..99).each do |i|
18
+ series << [i, i**2]
19
+ end
20
+ end
21
+
22
+ # enable command history recording
23
+ gp.record = true
24
+
25
+ # issue plotting commands, either with named data series
26
+ gp.plot :parabola, "using 1:2 with points"
27
+
28
+ # or with formulas. Options are collected in a string passed as second optional argument
29
+ gp.replot "x**2", "with lines"
30
+
31
+ # command history can be dumper and possibly saved on file to be edited or loaded again later on.
32
+ puts gp.dump_input
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gnuplotr
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: "0.1"
6
+ platform: ruby
7
+ authors:
8
+ - Paolo Bosetti
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-02-23 00:00:00 -08:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description: Interface between Ruby and GNUPlot
18
+ email: paolo.bosetti@me.com
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files: []
24
+
25
+ files:
26
+ - README.markdown
27
+ - lib/gnuplotr.rb
28
+ - test.rb
29
+ has_rdoc: true
30
+ homepage: http://github.com/pbosetti/gnuplotr
31
+ licenses: []
32
+
33
+ post_install_message:
34
+ rdoc_options:
35
+ - --inline-source
36
+ - --charset=UTF-8
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ requirements: []
52
+
53
+ rubyforge_project: gnuplotr
54
+ rubygems_version: 1.5.2
55
+ signing_key:
56
+ specification_version: 3
57
+ summary: Interface between Ruby and GNUPlot, aimed at being as easy to use and as light as possible
58
+ test_files: []
59
+