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.
-
#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) ⇒ 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
debug, error, fatal, #format_exception, #identify, info, init_logger, logger, set_level, #spawn, warn
Constructor Details
#initialize ⇒ DataStack
Creates a new DataStack object.
68 69 70 71 72 73 74 75 |
# File 'lib/ctioga2/data/stack.rb', line 68 def initialize @stack = Array.new @named_datasets = Hash.new # Defaults to the 'text' backend @backend_factory = Data::Backends::BackendFactory.new('text') end |
Instance Attribute Details
#backend_factory ⇒ Object
The BackendFactory used for retrieving data from named sets.
50 51 52 |
# File 'lib/ctioga2/data/stack.rb', line 50 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.
65 66 67 |
# File 'lib/ctioga2/data/stack.rb', line 65 def dataset_hook @dataset_hook end |
#named_datasets ⇒ Object
Named datasets
53 54 55 |
# File 'lib/ctioga2/data/stack.rb', line 53 def named_datasets @named_datasets end |
#stack ⇒ Object
The array containing all the Dataset used so far.
47 48 49 |
# File 'lib/ctioga2/data/stack.rb', line 47 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.
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/ctioga2/data/stack.rb', line 104 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
186 187 188 189 190 191 192 |
# File 'lib/ctioga2/data/stack.rb', line 186 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.
205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/ctioga2/data/stack.rb', line 205 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 |
#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.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/ctioga2/data/stack.rb', line 80 def get_datasets(set, = {}) backend = @backend_factory.current 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.
228 229 230 |
# File 'lib/ctioga2/data/stack.rb', line 228 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
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/ctioga2/data/stack.rb', line 168 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.stack.size < nb raise "Not enough datasets on the stack" end datasets = stack.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.
220 221 222 223 224 225 |
# File 'lib/ctioga2/data/stack.rb', line 220 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.
196 197 198 199 200 201 202 |
# File 'lib/ctioga2/data/stack.rb', line 196 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
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/ctioga2/data/stack.rb', line 233 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 #{name}" i += 1 end end |
#specified_dataset(options) ⇒ 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.
139 140 141 142 143 144 145 146 |
# File 'lib/ctioga2/data/stack.rb', line 139 def specified_dataset() spec = if && ['which'] ['which'] else -1 end return stored_dataset(spec) end |
#store_dataset(dataset, ignore_hooks = false) ⇒ Object
Adds a Dataset object onto the stack, running hooks if necessary.
Makes use of Plotmaker.plotmaker
152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/ctioga2/data/stack.rb', line 152 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.
123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/ctioga2/data/stack.rb', line 123 def stored_dataset(spec) if spec.is_a? Numeric or spec =~ /^\s*-?\d+\s*$/ spec = spec.to_i return @stack[spec] else if @named_datasets.key? spec return @named_datasets[spec] else raise "Unkown named dataset from the stack: '#{spec}'" end end end |