Class: CTioga2::Data::DataStack

Inherits:
Object
  • Object
show all
Includes:
Log
Defined in:
lib/ctioga2/data/stack.rb

Overview

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

  • interpolation: pops the last object from the stack and add its interpolated values on the element before.

  • mathematical functions on each column (DataColumn)

  • other stack-based operations.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Log

context, counts, debug, error, fatal, #format_exception, #identify, info, init_logger, log_to, logger, set_level, #spawn, warn

Constructor Details

#initializeDataStack

Creates a new DataStack object.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# 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

Instance Attribute Details

#backend_factoryObject

The BackendFactory used for retrieving data from named sets.



47
48
49
# File 'lib/ctioga2/data/stack.rb', line 47

def backend_factory
  @backend_factory
end

#dataset_hookObject

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

This is a list of Instruction



56
57
58
# File 'lib/ctioga2/data/stack.rb', line 56

def dataset_hook
  @dataset_hook
end

#named_datasetsObject

Named datasets



50
51
52
# File 'lib/ctioga2/data/stack.rb', line 50

def named_datasets
  @named_datasets
end

#stackObject

The array containing all the Dataset used so far.



44
45
46
# File 'lib/ctioga2/data/stack.rb', line 44

def stack
  @stack
end

Instance Method Details

#add_datasets(datasets, options = {}) ⇒ Object

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.



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# 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) ⇒ Object

Appends a set of commands to the dataset hook



231
232
233
234
235
236
237
# 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) ⇒ Object

Add all the given datasets to the current one.



250
251
252
253
254
255
256
257
258
259
260
# 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) ⇒ Object

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



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# 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) ⇒ Object

Drops the dataset corresponding to the given spec from the stack



305
306
307
308
309
310
311
312
313
314
315
# 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) ⇒ Object

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



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# 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

#lastObject

Returns the last Dataset pushed onto the stack.



273
274
275
# File 'lib/ctioga2/data/stack.rb', line 273

def last
  return @stack.last
end

#latest_datasets(opts) ⇒ Object

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



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# 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) ⇒ Object

Merges one or more datasets into the last one.

The last dataset of the stack is overwritten.



265
266
267
268
269
270
# 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

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



241
242
243
244
245
246
247
# File 'lib/ctioga2/data/stack.rb', line 241

def print_dataset(dataset, io)
  io.puts "# #{dataset.name}"
  io.puts "# #{dataset.column_names.join("\t")}"
  dataset.each_values do |i, *vals|
    io.puts vals.join("\t")
  end
end

#showObject

Displays the contents of the stack



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# 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) ⇒ Object

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.



182
183
184
185
186
187
188
189
190
# 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) ⇒ Object

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

Makes use of Plotmaker.plotmaker



196
197
198
199
200
201
202
203
204
205
206
207
208
209
# 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) ⇒ Object

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



132
133
134
# File 'lib/ctioga2/data/stack.rb', line 132

def stored_dataset(spec)
  return dataset_xref(spec)[0]
end