~ubuntu-branches/ubuntu/trusty/ctioga/trusty

« back to all changes in this revision

Viewing changes to Backends/lib/Backends/dataset.rb

  • Committer: Bazaar Package Importer
  • Author(s): Vincent Fourmond, Arnaud Cornet, Vincent Fourmond
  • Date: 2008-02-26 18:52:06 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20080226185206-apuosaxrvp4rpxaz
Tags: 1.7-1
[ Arnaud Cornet ]
* Use new Homepage dpkg header.

[ Vincent Fourmond ]
* New 'upstream' release
* Already conforms to standards version 3.7.3
* Added 11-manpage-typo to fix a very small typo in the manual page
  that makes lintian unhappy

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# dataset.rb : The implementation of data sets
 
2
# Copyright (C) 2006 Vincent Fourmond
 
3
 
 
4
# This program is free software; you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; either version 2 of the License, or
 
7
# (at your option) any later version.
 
8
 
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details.
 
13
 
 
14
# You should have received a copy of the GNU General Public License
 
15
# along with this program; if not, write to the Free Software
 
16
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 
17
 
 
18
require 'MetaBuilder/metabuilder'
 
19
 
 
20
require 'forwardable'
 
21
 
 
22
module SciYAG
 
23
 
 
24
  module Backends
 
25
 
 
26
 
 
27
    # An abstract class representing a DataSet. You should consider using
 
28
    # a subclass, DataSet2D or DataSet3D. A DataSet must be either 2D or 3D,
 
29
    # and redefine #is_2D? and #is_3D? accordingly.
 
30
    #
 
31
    # As DataSets grow more complex, it is likely that it will be less and
 
32
    # less easy to tweak directly the data inside. So, please, do use the
 
33
    # accessors and tweakers provided in the interface, else you'll expose
 
34
    # yourself to some intensive breakages when I get more clever.
 
35
    # Meanwhile, the apply(meth,where,*rest) method should be used to keep
 
36
    # everything in sync when you apply a method to a Dvector. 
 
37
    class DataSet
 
38
 
 
39
      extend Forwardable
 
40
 
 
41
      # The #creation_context attribute is a hash containing a :backend key
 
42
      # indicating the name of the Backend created and filled with the
 
43
      # contents of the backend's DescriptionInclude#save_state function.
 
44
      attr_accessor :creation_context
 
45
 
 
46
      # The data of the set. 
 
47
      attr_accessor :data
 
48
 
 
49
      # Errors on data, if applicable.
 
50
      # For 2D data, this will just be a hash with the following Dvectors:
 
51
      #  :xmin, :xmax for errors on the x values + :x, to make sure
 
52
      #  we keep it up-to-date.
 
53
      #  :ymin, :ymax for errors on y  + :y.
 
54
      #
 
55
      # Both ?min and ?max have to be specified if any output should be
 
56
      # created.
 
57
      attr_accessor :errors
 
58
 
 
59
      # The metadata given when the set was created.
 
60
      attr_accessor :meta_data
 
61
 
 
62
      def is_2D?
 
63
        return false
 
64
      end
 
65
 
 
66
      def is_3D?
 
67
        return false
 
68
      end
 
69
 
 
70
      # A few forwarded stuff to make the DataSet behave exactly like
 
71
      # Function and it's future 3D counterpart
 
72
 
 
73
      def_delegators :@data, :x, :y, :z
 
74
 
 
75
      def initialize(backend, data, errors = {}, meta_data = {})
 
76
        @creation_context = backend.save_state
 
77
        @creation_context[:backend] = backend.description.name
 
78
        @data = data
 
79
        @errors = errors
 
80
        @meta_data = meta_data
 
81
      end
 
82
 
 
83
      # Apply a Dvector operation to the given dimension, including
 
84
      # everything that could be left somewhere in the error bars.
 
85
      # It should be very powerful in the end.
 
86
      #
 
87
      # Beautiful, isn't it ??
 
88
      def apply(what, where, *rest)
 
89
        self.send(where).send(what, *rest)
 
90
        for key,values in @errors
 
91
          if key.to_s =~ /^#{where}/  # Very much overkill, but, well...
 
92
            values.send(what,*rest)
 
93
          end
 
94
        end
 
95
      end
 
96
 
 
97
      # Does a redirection to the underlying @data if that makes sense.
 
98
      def method_missing(sym, *args, &b)
 
99
        if @data.respond_to?(sym)
 
100
          @data.send(sym, *args, &b)
 
101
        elsif x.respond_to?(sym)  # We are trying to apply something
 
102
          # like data.mul!(:x, factor)
 
103
          apply(sym,args.shift,*args,&b)
 
104
        else
 
105
          super
 
106
        end
 
107
      end
 
108
 
 
109
    end
 
110
 
 
111
    # A 2-dimensionnal DataSet
 
112
    class DataSet2D < DataSet
 
113
 
 
114
      def is_2D?
 
115
        return true
 
116
      end
 
117
    end
 
118
 
 
119
    # A 3-dimensionnal DataSet
 
120
    class DataSet3D < DataSet
 
121
      def is_3D?
 
122
        return true
 
123
      end
 
124
    end
 
125
 
 
126
 
 
127
  end
 
128
end