Class: CTioga2::Data::DataStack
- Inherits:
-
Object
- Object
- CTioga2::Data::DataStack
- 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
-
#backend_factory ⇒ Object
The BackendFactory used for retrieving data from named sets.
-
#dataset_hook ⇒ Object
A hook executed every time a dataset is pushed unto the stack using #add_dataset.
-
#named_datasets ⇒ Object
Named datasets.
-
#stack ⇒ Object
The array containing all the Dataset used so far.
Instance Method Summary collapse
-
#add_datasets(datasets, options = {}) ⇒ Object
Adds a series of datasets, and perform various operations according to the hash options:.
-
#add_to_dataset_hook(commands) ⇒ Object
Appends a set of commands to the dataset hook.
-
#concatenate_datasets(datasets, name = nil) ⇒ Object
Add all the given datasets to the current one.
-
#dataset_xref(spec) ⇒ Object
Returns the [dataset, index, name] of the given dataset.
-
#drop_from_stack(spec) ⇒ Object
Drops the dataset corresponding to the given spec from the stack.
-
#get_datasets(set, options = {}) ⇒ Object
Performs expansion on the given set with the current backend, retrieves corresponding Dataset objects, pushes them onto the stack and returns them.
-
#initialize ⇒ DataStack
constructor
Creates a new DataStack object.
-
#last ⇒ Object
Returns the last Dataset pushed onto the stack.
-
#latest_datasets(opts) ⇒ Object
Returns a list of datasets, either a named dataset, or the last datasets from the stack.
-
#merge_datasets_into_last(datasets, columns = [0], precision = nil) ⇒ Object
Merges one or more datasets into the last one.
-
#print_dataset(dataset, io) ⇒ Object
Writes the contents of the the given dataset (a DataSet object) to the given io stream.
-
#show ⇒ Object
Displays the contents of the stack.
-
#specified_dataset(options, full = false) ⇒ Object
Gets a dataset from the given options hash.
-
#store_dataset(dataset, ignore_hooks = false) ⇒ Object
Adds a Dataset object onto the stack, running hooks if necessary.
-
#stored_dataset(spec) ⇒ Object
Returns the stored dataset, either using its index in the stack, or its name in the dataset.
Methods included from Log
context, debug, error, fatal, #format_exception, #identify, info, init_logger, log_to, logger, set_level, #spawn, warn
Constructor Details
#initialize ⇒ DataStack
Creates a new DataStack object.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/ctioga2/data/stack.rb', line 65 def initialize @stack = Array.new @named_datasets = Hash.new # Defaults to the 'text' backend @backend_factory = Data::Backends::BackendFactory.new('text') # 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. # text /separator=/[,;]/ EOH end |
Instance Attribute Details
#backend_factory ⇒ Object
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_hook ⇒ Object
A hook executed every time a dataset is pushed unto the stack using #add_dataset.
todo this string is parsed for each call to #add_dataset. Perhaps it would be good to provide a way to record a Command call, without parsing it from scratch ???
Although, with variables, that could be interesting to reparse everytime, since any change in the variables would be taken into account.
62 63 64 |
# File 'lib/ctioga2/data/stack.rb', line 62 def dataset_hook @dataset_hook end |
#named_datasets ⇒ Object
Named datasets
50 51 52 |
# File 'lib/ctioga2/data/stack.rb', line 50 def named_datasets @named_datasets end |
#stack ⇒ Object
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.
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/ctioga2/data/stack.rb', line 115 def add_datasets(datasets, = {}) i = 0 for ds in datasets store_dataset(ds, ['ignore_hooks']) # Selection if ['where'] ds.select_formula!(['where']) end if ['name'] @named_datasets[['name'] % [i]] = ds end i += 1 end end |
#add_to_dataset_hook(commands) ⇒ Object
Appends a set of commands to the dataset hook
223 224 225 226 227 228 229 |
# File 'lib/ctioga2/data/stack.rb', line 223 def add_to_dataset_hook(commands) if @dataset_hook @dataset_hook << "\n#{commands}" else @dataset_hook = commands end end |
#concatenate_datasets(datasets, name = nil) ⇒ Object
Add all the given datasets to the current one.
242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/ctioga2/data/stack.rb', line 242 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
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 |
# File 'lib/ctioga2/data/stack.rb', line 140 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 @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
297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/ctioga2/data/stack.rb', line 297 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 = {}) ⇒ Object
Performs expansion on the given set with the current backend, retrieves corresponding Dataset objects, pushes them onto the stack and returns them.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/ctioga2/data/stack.rb', line 91 def get_datasets(set, = {}) backend = @backend_factory.specified_backend() sets = backend.(set) datasets = [] for s in sets begin datasets << backend.dataset(s) rescue Exception => e error { "Could not load dataset #{s} -- #{e}" } debug { "#{e.backtrace.join("\n")}" } end end add_datasets(datasets, ) return datasets end |
#last ⇒ Object
Returns the last Dataset pushed onto the stack.
265 266 267 |
# File 'lib/ctioga2/data/stack.rb', line 265 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
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/ctioga2/data/stack.rb', line 205 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.
257 258 259 260 261 262 |
# File 'lib/ctioga2/data/stack.rb', line 257 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) ⇒ Object
Writes the contents of the the given dataset (a DataSet object) to the given io stream.
233 234 235 236 237 238 239 |
# File 'lib/ctioga2/data/stack.rb', line 233 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 |
#show ⇒ Object
Displays the contents of the stack
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
# File 'lib/ctioga2/data/stack.rb', line 270 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.
175 176 177 178 179 180 181 182 183 |
# File 'lib/ctioga2/data/stack.rb', line 175 def specified_dataset(, full = false) spec = if && ['which'] ['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
189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/ctioga2/data/stack.rb', line 189 def store_dataset(dataset, ignore_hooks = false) @stack << dataset if @dataset_hook && (! ignore_hooks) # \todo error handling begin PlotMaker.plotmaker.interpreter.run_commands(@dataset_hook) rescue Exception => e error { "There was a problem running the dataset hook '#{@dataset_hook}', disabling it" } @dataset_hook = nil info { "-> '#{format_exception e}'" } end end end |
#stored_dataset(spec) ⇒ Object
Returns the stored dataset, either using its index in the stack, or its name in the dataset.
134 135 136 |
# File 'lib/ctioga2/data/stack.rb', line 134 def stored_dataset(spec) return dataset_xref(spec)[0] end |