Class: Plot::GenericPlot

Inherits:
Object show all
Defined in:
lib/redshift/util/plot.rb

Direct Known Subclasses

Gnuplot

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ GenericPlot

Returns a new instance of GenericPlot.



27
28
29
30
31
32
33
# File 'lib/redshift/util/plot.rb', line 27

def initialize app
  @app = app
  @files = []
  @command_queue = []
  @command_history = []
  @data_stuff = []
end

Instance Attribute Details

#command_historyObject (readonly)

Array of strings that have been sent to the plot program.



25
26
27
# File 'lib/redshift/util/plot.rb', line 25

def command_history
  @command_history
end

Instance Method Details

#add(data = [], options = nil, path = nil) ⇒ Object

Add a data element to the plot with specified data array and options and, optionally, a path. If data is a string, it is assumed to be a expression defining a function, such as “sin(x)+1”.



82
83
84
85
86
87
88
89
# File 'lib/redshift/util/plot.rb', line 82

def add data = [], options = nil, path = nil
  case data
  when String
    @data_stuff << [nil, [data, options].join(" "), nil]
  else
    @data_stuff << [data, options, path]
  end
end

#clear_dataObject



35
36
37
# File 'lib/redshift/util/plot.rb', line 35

def clear_data
  @data_stuff = []
end

#closeObject

Closes the plot window and cleans up tempfiles, if you want to do that before the user closes the window explicitly. Doesn’t work with the -persist option typically used on windows.



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/redshift/util/plot.rb', line 42

def close
  if @pipe
    @pipe.close
    @pipe = nil
  elsif @pid # only when parent has forked the plot handler
    Process.kill("TERM", @pid)
    Process.wait(@pid)
    @pid = nil
  else
    raise "can't close plot"
  end
  @files = nil # let tempfiles be GC-ed and deleted
end

#command(str) ⇒ Object

Send str to the plot program, or, if #show has not been called yet, queue the string to be sent later when #show is called.



58
59
60
61
62
63
64
65
# File 'lib/redshift/util/plot.rb', line 58

def command str
  command_history << str
  if @pipe
    @pipe.puts str
  else
    @command_queue << str
  end
end

#dump(data, path = nil) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/redshift/util/plot.rb', line 91

def dump data, path = nil
  file =
    if path
      File.new(path, "w")
    else
      Tempfile.new("ruby_plot")
      ## might be better (at least on windows) to send the data
      ## inline, to avoid the "no file" errors when you try to
      ## zoom or replot
    end
  
  @files << file
      # This is here to prevent GC from collecting Tempfile object
      # and thereby deleting the temp file from disk.
  
  path = file.path

  if (data.first.first.respond_to? :join rescue false)
    data.each do |group|
      group.each do |point|
        file.puts point.join("\t")
      end
      file.puts
    end
  
  elsif (data.first.respond_to? :join rescue false) # assume one group
    for d in data
      if d.respond_to? :join
        file.puts d.join("\t")
      elsif d == nil
        file.puts
      else
        file.puts d.inspect
      end
    end
  
  elsif (data.first.respond_to? :each rescue false)
    data.each do |group|
      group.each do |point|
        file.puts point.to_a.join("\t") # to_a in case of narray
      end
      file.puts
    end
  
  else # assume one group
    for d in data
      if d.respond_to? :join
        file.puts d.join("\t")
      elsif d == nil
        file.puts
      else
        file.puts d.inspect
      end
    end
  end
  
  path

ensure
  file.close unless file.closed?
end

#showObject

Start the plotting program by opening a pipe to it. Send all queued commands. You can continue to send commands by calling #command.



69
70
71
72
73
74
75
76
77
# File 'lib/redshift/util/plot.rb', line 69

def show
  unless @pipe
    @pipe = IO.popen @app, "w"
    @pipe.sync = true
    while (cmd=@command_queue.shift)
      @pipe.puts cmd
    end
  end
end