Class: VisualisationUtils::GnuPlotTool

Inherits:
Object
  • Object
show all
Defined in:
lib/visualisation-utils.rb

Overview

Wraps the gnu plot invocation and basic scaffold for shared options around output format.

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ GnuPlotTool



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/visualisation-utils.rb', line 22

def initialize(opts)
    @title = opts[:title] || ""
    @opts = opts
    @debug = opts[:debug]

    if (! system 'which gnuplot')
        STDERR.puts("This utility depends on gnuplot. Please install gnuplot using your favourite package manager.")
        exit(1)
    end

    font = @opts[:font] || "Helvetica"
    font_size = @opts[:font_size ] || "12"

    @extra_header = @opts[:extra_header] || ""

    dimensions=@opts[:dimensions] || "1200,800"

    if (@opts[:outfile])
        filename = @opts[:outfile]
        extension = filename.gsub(/[^.]*\./, "")
        if (extension == "png")
            @terminal="    set term pngcairo font '\#{font},\#{font_size}' \#{@opts[:transparent]?\"transparent\":\"\"} size \#{dimensions}\n    set output '\#{filename}'\n"
        elsif (extension == "eps")
            @terminal="    set term epscairo size \#{dimensions} font '\#{font},\#{font_size}'\n    set output '\#{filename}'\n"
        elsif (extension == "pdf")
            @terminal="    set term pdfcairo size 22cm,14cm font '\#{font},\#{font_size}'\n    set output '\#{filename}'\n"
        elsif (extension == "svg")
            @terminal="    set term svg size \#{dimensions} font '\#{font},\#{font_size}'\n    set output '\#{filename}'\n"
        else
            throw "Unknown output format '.#{extension}'."
        end
    end
end

Instance Method Details

#debug?Boolean



68
69
70
# File 'lib/visualisation-utils.rb', line 68

def debug?
    @debug
end

#plot(script) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/visualisation-utils.rb', line 72

def plot(script)
    plot_file_name="/tmp/plot-"+`date  "+%s"`.strip+".plt"
    full_script="\#{@terminal}\n        set title \"\#{@title}\"\n\n        \#{@extra_header}\n\n        \#{script}\n"

    if (debug?)
        STDERR.puts "plot file " + plot_file_name
    end

    File.open(plot_file_name, "w") { |f| f.write(full_script) }

    persistent_opt = @opts[:outfile] ? "" : "-p"

    `gnuplot #{persistent_opt} #{plot_file_name}`

end