Class: GnuplotRB::Dataset

Inherits:
Object
  • Object
show all
Includes:
Plottable
Defined in:
lib/gnuplotrb/staff/dataset.rb

Overview

Dataset keeps control of Datablock or String (some math functions like this 'x*sin(x)' or filename) and options related to original dataset in gnuplot (with, title, using etc).

Options

Dataset options are explained in gnuplot docs (pp. 80-101). Several common options:

  • with - set plot style for dataset ('lines', 'points', 'impulses' etc)

  • using - choose which columns of input data gnuplot should use. Takes String (using: 'xtic(1):2:3'). If Daru::Dataframe passed one can use column names instead of numbers (using: 'index:value1:summ' - value1 and summ here are column names).

  • linewidth (lw) - integer line width

  • dashtype (dt) - takes pattern with dash style. Examples: '.. ', '– ', '.- '.

  • pointtype (pt) - takes integer number of point type (works only when :with option is set to 'points'). One can call Terminal::test(term_name) or Terminal#test in order to see which point types are supported by terminal.

Constant Summary collapse

INIT_HANDLERS =

Hash of init handlers for data given in different containers.

Hash.new(:init_default).merge(
  String =>          :init_string,
  Datablock =>       :init_dblock
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Plottable

#method_missing, #option_name, #option_name!, #own_terminal, #respond_to?, #title, #title!, #to_canvas, #to_gif, #to_iruby, #to_png, #to_specific_term, #to_svg, #xrange, #xrange!, #yrange, #yrange!

Methods included from OptionHandling

option_to_string, #options, #options!, ruby_class_to_gnuplot, string_key, valid_terminal?, validate_terminal_options

Constructor Details

#initialize(data, **options) ⇒ Dataset

Create new dataset out of given string with math function or filename. If data isn't a string it will create datablock to store data.

Examples:

Math function:

Dataset.new('x*sin(x)', with: 'lines', lw: 4)

File with points:

Dataset.new('points.data', with: 'lines', title: 'Points from file')

Some data (creates datablock stored in memory):

x = (0..5000).to_a
y = x.map {|xx| xx*xx }
points = [x, y]
Dataset.new(points, with: 'points', title: 'Points')

The same data but datablock stores it in temp file:

Dataset.new(points, with: 'points', title: 'Points', file: true)

65
66
67
68
# File 'lib/gnuplotrb/staff/dataset.rb', line 65

def initialize(data, **options)
  # run method by name
  send(INIT_HANDLERS[data.class], data, options)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class GnuplotRB::Plottable

Instance Attribute Details

#dataObject (readonly)

Data represented by this dataset


24
25
26
# File 'lib/gnuplotrb/staff/dataset.rb', line 24

def data
  @data
end

Instance Method Details

#cloneObject

Own implementation of #clone. Creates new Dataset if data stored in datablock and calls super otherwise.


172
173
174
175
176
177
178
# File 'lib/gnuplotrb/staff/dataset.rb', line 172

def clone
  if @type == :datablock
    new_with_options(@options)
  else
    super
  end
end

#plot(*args) ⇒ Plot

Create new Plot object with only one Dataset given - self. Calls #plot on created Plot. All arguments given to this #plot will be sent to Plot#plot instead.

Examples:

sin = Dataset.new('sin(x)')
sin.plot(term: [qt, size: [300, 300]])
#=> shows qt window 300x300 with sin(x)
sin.to_png('./plot.png')
#=> creates png file with sin(x) plotted

193
194
195
# File 'lib/gnuplotrb/staff/dataset.rb', line 193

def plot(*args)
  Plot.new(self).plot(*args)
end

#to_s(terminal = nil, without_options: false) ⇒ String

Convert Dataset to string containing gnuplot dataset.

Examples:

Dataset.new('points.data', with: 'lines', title: 'Points from file').to_s
#=> "'points.data' with lines title 'Points from file'"
Dataset.new(points, with: 'points', title: 'Points').to_s
#=> "$DATA1 with points title 'Points'"

84
85
86
87
88
# File 'lib/gnuplotrb/staff/dataset.rb', line 84

def to_s(terminal = nil, without_options: false)
  result = "#{@type == :datablock ? @data.name(terminal) : @data} "
  result += options_to_string unless without_options
  result
end

#update(data = nil, **options) ⇒ Object

Create new dataset with updated data and merged options.

Given data is appended to existing. Data is updated only if Dataset stores it in Datablock. Method does nothing if no options given and data isn't stored in in-memory Datablock.

Examples:

Updating dataset with Math formula or filename given:

dataset = Dataset.new('file.data')
dataset.update(data: 'asd')
#=> nothing updated
dataset.update(data: 'asd', title: 'File')
#=> Dataset.new('file.data', title: 'File')

Updating dataset with data stored in Datablock (in-memory):

in_memory_points = Dataset.new(points, title: 'Old one')
in_memory_points.update(data: some_update, title: 'Updated')
#=> Dataset.new(points + some_update, title: 'Updated')

Updating dataset with data stored in Datablock (in-file):

temp_file_points = Dataset.new(points, title: 'Old one', file: true)
temp_file_points.update(data: some_update)
#=> data updated but no new dataset created
temp_file_points.update(data: some_update, title: 'Updated')
#=> data updated and new dataset with title 'Updated' returned

119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/gnuplotrb/staff/dataset.rb', line 119

def update(data = nil, **options)
  if data && @type == :datablock
    new_datablock = @data.update(data)
    if new_datablock == @data
      update_options(options)
    else
      self.class.new(new_datablock, options)
    end
  else
    update_options(options)
  end
end

#update!(data = nil, **options) ⇒ Object

Update Dataset with new data and options.

Given data is appended to existing. Data is updated only if Dataset stores it in Datablock. Method does nothing if no options given and data isn't stored in in-memory Datablock.

Examples:

Updating dataset with Math formula or filename given:

dataset = Dataset.new('file.data')
dataset.update!(data: 'asd')
#=> nothing updated
dataset.update!(data: 'asd', title: 'File')
dataset.title
#=> 'File' # data isn't updated

Updating dataset with data stored in Datablock (in-memory):

in_memory_points = Dataset.new(points, title: 'Old one')
in_memory_points.update!(data: some_update, title: 'Updated')
in_memory_points.data
#=> points + some_update
in_memory_points.title
#=> 'Updated'

Updating dataset with data stored in Datablock (in-file):

temp_file_points = Dataset.new(points, title: 'Old one', file: true)
temp_file_points.update!(data: some_update)
#=> data updated but no new dataset created
temp_file_points.update!(data: some_update, title: 'Updated')
#=> data and options updated

163
164
165
166
167
# File 'lib/gnuplotrb/staff/dataset.rb', line 163

def update!(data = nil, **options)
  @data.update!(data) if data
  options!(options)
  self
end