class CTioga2::Data::DataStack

This is the central class for acquisition and handling of Dataset objects, retrieved from from a Backends::BackendFactory.

todo provide real stack manipulation functions such as

Attributes

backend_factory[RW]

The BackendFactory used for retrieving data from named sets.

dataset_hook[RW]

A hook executed every time a dataset is pushed unto the stack using add_dataset.

This is a list of Instruction

named_datasets[RW]

Named datasets

stack[RW]

The array containing all the Dataset used so far.

Public Class Methods

new() click to toggle source

Creates a new DataStack object.

# File lib/ctioga2/data/stack.rb, line 59
      def initialize
        @stack = Array.new

        @named_datasets = Hash.new

        # Defaults to the 'text' backend
        @backend_factory = Data::Backends::BackendFactory.new('text')

        @dataset_hook = []

        # Probably a bit out of place...
        csv = 
          Cmd.new('csv', nil, '--csv', []) do |plotmaker|
          plotmaker.interpreter.
            run_commands("text /separator=/[,;]/")
        end
        
        csv.describe("reads CSV files", 
                     <<"EOH", "backend-text")
Now parse the following data files as CSV. Equivalent to

# text /separator=/[,;]/
EOH
      end

Public Instance Methods

add_datasets(datasets, options = {}) click to toggle source

Adds a series of datasets, and perform various operations according to the hash options:

  • 'name' to name each element added to the stack. A %d will be replaced by the number of the dataset within the ones just added.

Additional members of the Hash are simply ignored.

# File lib/ctioga2/data/stack.rb, line 113
def add_datasets(datasets, options = {})
  i = 0
  for ds in datasets
    store_dataset(ds, options['ignore_hooks'])

    # Selection
    if options['where']
      ds.select_formula!(options['where'])
    end

    if options['name']
      @named_datasets[options['name'] % [i]] = ds
    end
    i += 1
  end
end
add_to_dataset_hook(commands) click to toggle source

Appends a set of commands to the dataset hook

# File lib/ctioga2/data/stack.rb, line 231
def add_to_dataset_hook(commands)
  if @dataset_hook
    @dataset_hook += [commands].flatten
  else
    @dataset_hook = [commands].flatten
  end
end
concatenate_datasets(datasets, name = nil) click to toggle source

Add all the given datasets to the current one.

# File lib/ctioga2/data/stack.rb, line 250
def concatenate_datasets(datasets, name = nil)
  ds = @stack.pop
  raise "Nothing on the stack" unless ds

  for ds2 in datasets
    ds << ds2
  end
  @stack.push(ds)
  # Name the dataset
  @named_datasets[name] = ds if name
end
dataset_xref(spec) click to toggle source

Returns the [dataset, index, name] of the given dataset

# File lib/ctioga2/data/stack.rb, line 138
def dataset_xref(spec)
  ds = nil
  index = nil
  name = nil
  if spec.is_a? Numeric or spec =~ /^\s*-?\d+\s*$/
    spec = spec.to_i
    index = spec
    name = nil
    ds = @stack[index]
    for k,v in @named_datasets
      if v == ds
        name = k
      end
    end
  else
    if spec =~ /^\s*#(.*)/
      # graph idea -> get dataset from the plot element
      eln = $1
      obj = Elements::TiogaElement.find_object(eln)
      if !obj.respond_to(:dataset)
        raise "Object '##{eln}' does not name a plot"
      end
      ds = obj.dataset
      index = @stack.index(ds)
    elsif @named_datasets.key? spec
      name = spec
      ds = @named_datasets[spec]
      i = 0
      for d in @stack
        if d == ds
          index = i
        end
        i += 1
      end
    else
      raise "Unkown named dataset from the stack: '#{spec}'"
    end
  end
  return [ds, index, name]
end
drop_from_stack(spec) click to toggle source

Drops the dataset corresponding to the given spec from the stack

# File lib/ctioga2/data/stack.rb, line 305
def drop_from_stack(spec)
  xr = dataset_xref(spec)
  if xr[1]                # But that should always be the case ?
    @stack.delete_at(xr[1])
  else
    warn { "For some reason, dataset '#{spec}' is not in the stack !"}
  end
  if xr[2]
    @named_datasets.delete(xr[2])
  end
end
get_datasets(set, options = {}, add = true) click to toggle source

Performs expansion on the given set with the current backend, retrieves corresponding Dataset objects, pushes them onto the stack and returns them.

# File lib/ctioga2/data/stack.rb, line 87
def get_datasets(set, options = {}, add = true)
  backend = @backend_factory.specified_backend(options)
  sets = backend.expand_sets(set)
  datasets = []
  for s in sets
    begin
      datasets << backend.dataset(s)
    rescue Exception => e
      error { "Could not load dataset '#{s}' with backend '#{backend.description.name }':\n\t -> #{e}" }
      debug { "#{e.backtrace.join("\n")}" }
    end
  end
  if add
    add_datasets(datasets, options)
  end
  return datasets
end
last() click to toggle source

Returns the last Dataset pushed onto the stack.

# File lib/ctioga2/data/stack.rb, line 273
def last
  return @stack.last
end
latest_datasets(opts) click to toggle source

Returns a list of datasets, either a named dataset, or the last datasets from the stack

# File lib/ctioga2/data/stack.rb, line 213
def latest_datasets(opts)
  if opts['which']
    if opts['number']
      warn { "Cannot use both which and number" }
    end
    datasets = [ specified_dataset(opts) ]
  else
    nb = opts['number'] || 2
    if @stack.size < nb
      raise "Not enough datasets on the stack"
    end
    datasets = @stack[(- nb).. -2]
    datasets.reverse!
  end
end
merge_datasets_into_last(datasets, columns = [0], precision = nil) click to toggle source

Merges one or more datasets into the last one.

The last dataset of the stack is overwritten.

# File lib/ctioga2/data/stack.rb, line 265
def merge_datasets_into_last(datasets, columns = [0], precision = nil)
  ds = @stack.pop
  raise "Nothing on the stack" unless ds
  ds.merge_datasets_in(datasets, columns, precision)
  @stack.push(ds)
end
print_dataset(dataset, io) click to toggle source

Writes the contents of the the given dataset (a DataSet object) to the given io stream.

show() click to toggle source

Displays the contents of the stack

# File lib/ctioga2/data/stack.rb, line 278
def show
  STDERR.puts "Stack contents"
  i = 0
  # Swap the named dataset stuff
  ## @todo Maybe a hash pair should be maintained in permanence ?
  rev = {}
  for k,v in @named_datasets
    rev[v] = k
  end

  for ds in @stack
    name = rev[ds]
    if name
      name = "(named: '#{name}')"
    else
      name = ""
    end
    
    pref = sprintf("#%-2d %-3d:", i, - @stack.size + i)
    
    STDERR.puts " * #{pref} #{ds.name} -- #{ds.ys.size + 1} columns, #{ds.x.size} points #{name}"
    i += 1
  end
end
specified_dataset(options, full = false) click to toggle source

Gets a dataset from the given options hash. If a 'which' key is present, it is used as an argument for stored_dataset; else, -1 is used.

# File lib/ctioga2/data/stack.rb, line 182
def specified_dataset(options, full = false)
  spec = if options && options['which']
           options['which']
         else
           -1
         end
  xr = dataset_xref(spec)
  return (full ? xr : xr[0])
end
store_dataset(dataset, ignore_hooks = false) click to toggle source

Adds a Dataset object onto the stack, running hooks if necessary.

Makes use of Plotmaker.plotmaker

# File lib/ctioga2/data/stack.rb, line 196
def store_dataset(dataset, ignore_hooks = false)
  @stack << dataset
  if @dataset_hook && (! ignore_hooks)
    for ins in @dataset_hook
      begin
        ins.run(PlotMaker.plotmaker)
      rescue Exception => e
        error { "There was a problem running the dataset hook '#{ins.to_s}', disabling it" }
        @dataset_hook.delete(ins)
        info { "-> '#{format_exception e}'" }
      end
    end
  end
end
stored_dataset(spec) click to toggle source

Returns the stored dataset, either using its index in the stack, or its name in the dataset.

# File lib/ctioga2/data/stack.rb, line 132
def stored_dataset(spec)
  return dataset_xref(spec)[0]
end