~ubuntu-branches/ubuntu/oneiric/ctioga2/oneiric

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# datacolumn.rb: a class holding a 'column' of data
# copyright (c) 2009-2011 by Vincent Fourmond
  
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
  
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details (in the COPYING file).

require 'Dobjects/Dvector'
require 'ctioga2/utils'

# This module contains all the classes used by ctioga
module CTioga2

  Version::register_svn_info('$Revision: 229 $', '$Date: 2011-01-17 17:34:57 +0100 (Mon, 17 Jan 2011) $')

  module Data

    # This class holds one column, possibly with error bars.
    #
    # \todo a way to concatenate two DataColumns
    #
    # \todo a way to easily access the by "lines"
    class DataColumn
      
      # A Dvector holding ``real'' values
      attr_accessor :values
      
      # A Dvector holding minimal values
      attr_accessor :min_values

      # A Dvector holding maximal values
      attr_accessor :max_values

      # \todo a method that resembles the code in the old text backend
      # to set errors according to a speficication (relative,
      # absolute, already max/min)

      # \todo a dup !

      def initialize(values, min = nil, max = nil)
        @values = values
        @min_values = min
        @max_values = max
      end

      # Creates a DataColumn object
      def self.create(number, with_errors = false)
        a = Dobjects::Dvector.new(number, NaN::NaN)
        if with_errors
          b = Dobjects::Dvector.new(number, NaN::NaN)
          c = Dobjects::Dvector.new(number, NaN::NaN)
        else
          b = nil
          c = nil
        end
        return self.new(a, b, c)
      end


      # Yields all the vectors in turn to apply a given
      # transformation.
      def apply
        for v in all_vectors
          yield v if v
        end
      end

      # Sorts the values according to the index vector given.
      def reindex(idx_vector)
        for v in all_vectors
          # This is slow !
          # Code should be written in C on the dvector side.
          #
          # Or we could use Function.sort, though this is not very
          # elegant nor efficient. (but it would be memory-efficient,
          # though).
          next unless v
          w = Dobjects::Dvector.new(idx_vector.size) do |i|
            v[idx_vector[i]]
          end
          v.replace(w)
        end
      end

      # Whether there are error bars.
      def has_errors?
        return (@min_values && @max_values)
      end

      # Column names. _base_ is used as a base for the names. If
      # _expand_ is on, always return all the names.
      def column_names(base, expand = false)
        if expand || has_errors?
          return [base, "#{base}min", "#{base}max"]
        else
          return [base]
        end
      end

      # Values at the given index.
      #
      # If _with_errors_ is false, only [value] is returned.
      #
      # If _with_errors_ is true, then, non-existent values are
      # expanded to _nil_ if _expand_nil_ is true or to value if not.
      def values_at(i, with_errors = false, expand_nil = true)
        if ! with_errors 
          return [@values[i]]
        end
        if has_errors?
          return [@values[i], @min_values[i], @max_values[i]]
        else
          if expand_nil
            return [@values[i], nil, nil]
          else
            return [@values[i], @values[i], @values[i]]
          end
        end
      end

      # Vectors: all values if there are error bars, or only the
      # #value one if there isn't.
      def vectors
        if has_errors?
          return [@values, @min_values, @max_values]
        else
          return [@values]
        end
      end

      # Returns the number of elements.
      def size
        return @values.size
      end

      # Sets the values at the given index
      def set_values_at(i, value, min = nil, max = nil)
        @values[i] = value
        if min && max
          ensure_has_errors
          @min_values[i] = min
          @max_vaklues[i] = max
        end
      end

      # Appends the given values at the end of the DataColumn
      #
      # @todo This isn't very efficient. Does it really matter ?
      def push_values(value, min=nil, max=nil)
        set_values_at(@values.size, value, min, max)
      end

      # Creates dummy errors (ie, min_values = max_values = values) if
      # the datacolumn does not currently have one.
      def ensure_has_errors
        if ! has_errors?
          @min_values = @values.dup
          @max_values = @values.dup
        end
      end

      # Concatenates with another DataColumn, making sure the errors
      # and such are not lost.
      def <<(column)
        # If there are error bars, wew make sure we concatenate all of them
        if has_errors? || column.has_errors?
          self.ensure_has_errors
          column.ensure_has_errors
          @min_values.concat(column.min_values)
          @max_values.concat(column.max_values)
        end
        @values.concat(column.values)
      end

      # Only keeps every _n_ points in the DataColumn
      def trim!(nb)
        nb = nb.to_i
        if nb < 2
          return
        end

        new_vects = []
        for v in all_vectors
          if v
            new_values = Dobjects::Dvector.new
            i = 0
            for val in v
              if (i % nb) == 0
                new_values << val
              end
              i+=1
            end
            new_vects << new_values
          else
            new_vects << nil
          end
        end
        set_vectors(new_vects)
      end

      ColumnSpecsRE = /|min|max|err/i

      # This function sets the value of the DataColumn object
      # according to a hash: _spec_ => _vector_.  _spec_ can be any of:
      # * 'value', 'values' or '' : the #values
      # * 'min' : #min
      # * 'max' : #max
      # * 'err' : absolute error: min is value - error, max is value +
      #    error
      def from_hash(spec)
        s = spec.dup
        @values = spec['value'] || spec['values'] || 
          spec[''] 
        if ! @values
          raise "Need a 'value' specification"
        end
        for k in ['value', 'values', '']
          s.delete(k)
        end
        for key in s.keys
          case key
          when /^min$/i
            @min_values = s[key]
          when /^max$/i
            @max_values = s[key]
          when /^err$/i
            @min_values = @values - s[key]
            @max_values = @values + s[key]
          else
            raise "Unkown key: #{key}"
          end
        end
      end


      # Creates and returns a DataColumn object according to the
      # _spec_. See #from_hash for more information.
      def self.from_hash(spec)
        a = DataColumn.new(nil)
        a.from_hash(spec)
        return a
      end

      # Returns the minimum value of all vectors held in this column
      def min
        m = @values.min
        for v in [@min_values, @max_values]
          if v
            m1 = v.min
            if m1 < m           # This also works if m1 is NaN
              m = m1
            end
          end
        end
        return m
      end

      # Returns the maximum value of all vectors held in this column
      def max
        m = @values.max
        for v in [@min_values, @max_values]
          if v
            m1 = v.max
            if m1 > m           # This also works if m1 is NaN
              m = m1
            end
          end
        end
        return m
      end

      def convolve!(kernel, middle = nil)
        middle ||= kernel.size/2
        # We smooth everything, stupidly?
        for v in all_vectors
          v.replace(v.convolve(kernel,middle)) if v
        end
      end
      
      protected

      # All the vectors held by the DataColumn
      def all_vectors
        return [@values, @min_values, @max_values]
      end

      # Sets the vectors to the given list, as might have been
      # returned by #all_vectors
      def set_vectors(vectors)
        @values, @min_values, @max_values = *vectors
      end

    end

  end

end