~ubuntu-branches/ubuntu/wily/ctioga2/wily

« back to all changes in this revision

Viewing changes to lib/ctioga2/data/stack.rb

  • Committer: Package Import Robot
  • Author(s): Vincent Fourmond
  • Date: 2013-07-08 20:58:17 UTC
  • mfrom: (6.1.1 experimental)
  • Revision ID: package-import@ubuntu.com-20130708205817-cephnc6etndyxrrp
Tags: 0.4-2
* Upload to unstable
* Already conforms to newer standards

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
# This module contains all the classes used by ctioga
26
26
module CTioga2
27
27
 
28
 
  Version::register_svn_info('$Revision: 233 $', '$Date: 2011-01-20 10:26:42 +0100 (Thu, 20 Jan 2011) $')
 
28
  Version::register_svn_info('$Revision: 367 $', '$Date: 2012-12-28 16:14:52 +0100 (Fri, 28 Dec 2012) $')
29
29
 
30
30
 
31
31
  module Data
80
80
      def get_datasets(set, options = {})
81
81
        backend = @backend_factory.current
82
82
        sets = backend.expand_sets(set)
83
 
        datasets = sets.map do |s|
84
 
          backend.dataset(s)
 
83
        datasets = []
 
84
        for s in sets
 
85
          begin
 
86
            datasets << backend.dataset(s)
 
87
          rescue Exception => e
 
88
            error { "Could not load dataset #{s} -- #{e}" }
 
89
            debug { "#{e.backtrace.join("\n")}" }
 
90
          end
85
91
        end
86
92
        add_datasets(datasets, options)
87
93
        return datasets
157
163
        end
158
164
      end
159
165
 
 
166
      # Returns a list of datasets, either a named dataset, or the
 
167
      # last datasets from the stack
 
168
      def latest_datasets(opts)
 
169
        if opts['which']
 
170
          if opts['number']
 
171
            warn { "Cannot use both which and number" }
 
172
          end
 
173
          datasets = [ specified_dataset(opts) ]
 
174
        else
 
175
          nb = opts['number'] || 2
 
176
          if @stack.size < nb
 
177
            raise "Not enough datasets on the stack"
 
178
          end
 
179
          datasets = @stack[(- nb).. -2]
 
180
          datasets.reverse!
 
181
        end
 
182
      end
 
183
        
 
184
        
160
185
      # Appends a set of commands to the dataset hook
161
186
      def add_to_dataset_hook(commands)
162
187
        if @dataset_hook
176
201
        end
177
202
      end
178
203
 
179
 
      # Pops the last _n_ datasets off the stack and pushes back the
180
 
      def concatenate_datasets(n = 2)
 
204
      # Add all the given datasets to the current one.
 
205
      def concatenate_datasets(datasets, name = nil)
181
206
        ds = @stack.pop
182
207
        raise "Nothing on the stack" unless ds
183
 
        (n-1).times do
184
 
          ds2 = @stack.pop
185
 
          raise "Not enough datasets on the stack" unless ds2
 
208
 
 
209
        for ds2 in datasets
186
210
          ds << ds2
187
211
        end
188
212
        @stack.push(ds)
 
213
        # Name the dataset
 
214
        @named_datasets[name] = ds if name
189
215
      end
190
216
 
191
 
      # Merges the last datasets into a new one.
 
217
      # Merges one or more datasets into the last one.
192
218
      #
193
 
      # Over
194
 
      def merge_datasets(n = 2, columns = [0], precision = nil)
 
219
      # The last dataset of the stack is overwritten.
 
220
      def merge_datasets_into_last(datasets, columns = [0], precision = nil)
195
221
        ds = @stack.pop
196
222
        raise "Nothing on the stack" unless ds
197
 
        datasets = @stack[-1..-(n-1)]
198
223
        ds.merge_datasets_in(datasets, columns, precision)
199
224
        @stack.push(ds)
200
225
      end
204
229
        return @stack.last
205
230
      end
206
231
 
 
232
      # Displays the contents of the stack
 
233
      def show
 
234
        STDERR.puts "Stack contents"
 
235
        i = 0
 
236
        # Swap the named dataset stuff
 
237
        ## @todo Maybe a hash pair should be maintained in permanence ?
 
238
        rev = {}
 
239
        for k,v in @named_datasets
 
240
          rev[v] = k
 
241
        end
 
242
 
 
243
        for ds in @stack
 
244
          name = rev[ds]
 
245
          if name
 
246
            name = "(named: '#{name}')"
 
247
          else
 
248
            name = ""
 
249
          end
 
250
          
 
251
          pref = sprintf("#%-2d %-3d:", i, - @stack.size + i)
 
252
          
 
253
          STDERR.puts " * #{pref} #{ds.name} -- #{ds.ys.size + 1} columns #{name}"
 
254
          i += 1
 
255
        end
 
256
      end
 
257
 
207
258
      
208
259
    end
209
260
 
284
335
EOH
285
336
 
286
337
    ConcatLastCommand = 
287
 
      Cmd.new("join-datasets", nil, "--join-datasets", 
288
 
              [], {'number' => CmdArg.new('integer')}) do |plotmaker, opts|
289
 
      nb = opts['number'] || 2
290
 
      plotmaker.data_stack.concatenate_datasets(nb)
 
338
      Cmd.new("join-datasets", "-j", "--join-datasets", 
 
339
              [], 
 
340
              { 
 
341
                'number' => CmdArg.new('integer'),
 
342
                'which' => CmdArg.new('stored-dataset'),
 
343
                'name' => CmdArg.new('text') 
 
344
              }) do |plotmaker, opts|
 
345
      stack = plotmaker.data_stack
 
346
      datasets = stack.latest_datasets(opts)
 
347
      stack.concatenate_datasets(datasets, opts['name'])
291
348
    end
292
349
    
293
350
    ConcatLastCommand.describe("Concatenates the last datasets on the stack",
294
351
                               <<EOH, DataStackGroup)
295
352
Pops the last two (or number, if it is specified) datasets from the
296
353
stack, concatenates them (older last) and push them back onto the
297
 
stack.
298
 
EOH
299
 
 
300
 
 
 
354
stack. The name option can be used to give a name to the new dataset. 
 
355
EOH
 
356
 
 
357
    ApplyLastCommand =
 
358
      Cmd.new("apply-formula", nil, "--apply-formula",
 
359
              [CmdArg.new('text')], 
 
360
              {
 
361
                'which' => CmdArg.new('stored-dataset'),
 
362
                'name' => CmdArg.new('text'),
 
363
              }) do |plotmaker, formula, opts|
 
364
      ds = plotmaker.data_stack.specified_dataset(opts)
 
365
      newds = ds.apply_formulas(formula)
 
366
      plotmaker.data_stack.add_datasets([newds], opts)
 
367
    end
 
368
    
 
369
    ApplyLastCommand.describe("Applies a formula to the last dataset",
 
370
                              <<EOH, DataStackGroup)
 
371
Applies a formula to the last dataset (or the named one)
 
372
EOH
 
373
 
 
374
    ShowStackCommand = 
 
375
      Cmd.new("show-stack", nil, "--show-stack", 
 
376
              [], 
 
377
              { }
 
378
              ) do |plotmaker, opts|
 
379
      plotmaker.data_stack.show
 
380
    end
 
381
    
 
382
    ShowStackCommand.describe("Displays the content of the stack",
 
383
                              <<EOH, DataStackGroup)
 
384
Displays the current contents of the dataset stack. 
 
385
 
 
386
Mostly used for debugging when operations like {command: merge-datasets}
 
387
or {command: join-datasets} don't work as expected.
 
388
EOH
 
389
 
 
390
 
 
391
    ## @todo Add column selection ?
301
392
    MergeToLastCommand = 
302
393
      Cmd.new("merge-datasets", nil, "--merge-datasets", 
303
 
              [], {'number' => CmdArg.new('integer')}) do |plotmaker, opts|
304
 
      nb = opts['number'] || 2
305
 
      ## @todo all
306
 
      plotmaker.data_stack.merge_datasets(nb)
 
394
              [], 
 
395
              {
 
396
                'number' => CmdArg.new('integer'), 
 
397
                'which' => CmdArg.new('stored-dataset')
 
398
              }
 
399
              ) do |plotmaker, opts|
 
400
      stack = plotmaker.data_stack
 
401
      datasets = stack.latest_datasets(opts)
 
402
      plotmaker.data_stack.merge_datasets_into_last(datasets)
307
403
    end
308
404
    
309
 
    MergeToLastCommand.describe("....",
310
 
                               <<EOH, DataStackGroup)
311
 
....
 
405
    MergeToLastCommand.describe("Merge datasets based on X column",
 
406
                                <<EOH, DataStackGroup)
 
407
This commands merges data with matching X values from a dataset (by
 
408
default the one before the last) into the last one. Data points that
 
409
have no corresponding X value in the current dataset are simply
 
410
ignored.
 
411
 
 
412
This can be used to build 3D datasets for {cmd: xyz-map} or 
 
413
{cmd: xy-parametric}.
312
414
EOH
313
415
 
314
416
    XYReglinCommand = 
325
427
    
326
428
    XYReglinCommand.describe("....",
327
429
                             <<EOH, DataStackGroup)
328
 
....
 
430
...
 
431
 
 
432
This command will get documented some day.
 
433
EOH
 
434
 
 
435
 
 
436
    ComputeContourCommand = 
 
437
      Cmd.new("compute-contour", nil, "--compute-contour", 
 
438
              [CmdArg.new("float")], {
 
439
                'which' => CmdArg.new('stored-dataset')
 
440
              }) do |plotmaker, level, opts|
 
441
      stack = plotmaker.data_stack
 
442
      ds = stack.specified_dataset(opts)
 
443
      f = ds.make_contour(level)
 
444
      newds = Dataset.new("Contour #{level}", [f.x, f.y])
 
445
      stack.store_dataset(newds, true)
 
446
    end
 
447
    
 
448
    ComputeContourCommand.describe("computes the contour and push it to data stack",
 
449
                             <<EOH, DataStackGroup)
 
450
Computes the contour at the given level for the given dataset (or the
 
451
last on the stack if none is specified) and pushes it onto the data
 
452
stack.
 
453
 
 
454
You can further manipulate it as usual.
329
455
EOH
330
456
 
331
457
    SetDatasetHookCommand =