Class: CTioga2::Data::DataColumn

Inherits:
Object
  • Object
show all
Defined in:
lib/ctioga2/data/datacolumn.rb

Overview

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”

Constant Summary collapse

ColumnSpecsRE =
/|min|max|err/i

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values, min = nil, max = nil) ⇒ DataColumn

todo a dup !



46
47
48
49
50
# File 'lib/ctioga2/data/datacolumn.rb', line 46

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

Instance Attribute Details

#max_valuesObject

A Dvector holding maximal values



38
39
40
# File 'lib/ctioga2/data/datacolumn.rb', line 38

def max_values
  @max_values
end

#min_valuesObject

A Dvector holding minimal values



35
36
37
# File 'lib/ctioga2/data/datacolumn.rb', line 35

def min_values
  @min_values
end

#valuesObject

A Dvector holding “real” values



32
33
34
# File 'lib/ctioga2/data/datacolumn.rb', line 32

def values
  @values
end

Class Method Details

.create(number, with_errors = false) ⇒ Object

Creates a DataColumn object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ctioga2/data/datacolumn.rb', line 53

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

.from_hash(spec) ⇒ Object

Creates and returns a DataColumn object according to the spec. See #from_hash for more information.



244
245
246
247
248
# File 'lib/ctioga2/data/datacolumn.rb', line 244

def self.from_hash(spec)
  a = DataColumn.new(nil)
  a.from_hash(spec)
  return a
end

Instance Method Details

#<<(column) ⇒ Object

Concatenates with another DataColumn, making sure the errors and such are not lost.



170
171
172
173
174
175
176
177
178
179
# File 'lib/ctioga2/data/datacolumn.rb', line 170

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

#applyObject

Yields all the vectors in turn to apply a given transformation.



68
69
70
71
72
# File 'lib/ctioga2/data/datacolumn.rb', line 68

def apply
  for v in all_vectors
    yield v if v
  end
end

#column_names(base, expand = false) ⇒ Object

Column names. base is used as a base for the names. If expand is on, always return all the names.



98
99
100
101
102
103
104
# File 'lib/ctioga2/data/datacolumn.rb', line 98

def column_names(base, expand = false)
  if expand || has_errors?
    return [base, "#{base}min", "#{base}max"]
  else
    return [base]
  end
end

#convolve!(kernel, middle = nil) ⇒ Object



278
279
280
281
282
283
284
# File 'lib/ctioga2/data/datacolumn.rb', line 278

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

#ensure_has_errorsObject

Creates dummy errors (ie, min_values = max_values = values) if the datacolumn does not currently have one.



161
162
163
164
165
166
# File 'lib/ctioga2/data/datacolumn.rb', line 161

def ensure_has_errors
  if ! has_errors?
    @min_values = @values.dup
    @max_values = @values.dup
  end
end

#from_hash(spec) ⇒ Object

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
    


216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/ctioga2/data/datacolumn.rb', line 216

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

#has_errors?Boolean

Whether there are error bars.

Returns:

  • (Boolean)


92
93
94
# File 'lib/ctioga2/data/datacolumn.rb', line 92

def has_errors?
  return (@min_values && @max_values)
end

#maxObject

Returns the maximum value of all vectors held in this column



265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/ctioga2/data/datacolumn.rb', line 265

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

#minObject

Returns the minimum value of all vectors held in this column



251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/ctioga2/data/datacolumn.rb', line 251

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

#push_values(value, min = nil, max = nil) ⇒ Object

TODO:

This isn’t very efficient. Does it really matter ?

Appends the given values at the end of the DataColumn



155
156
157
# File 'lib/ctioga2/data/datacolumn.rb', line 155

def push_values(value, min=nil, max=nil)
  set_values_at(@values.size, value, min, max)
end

#reindex(idx_vector) ⇒ Object

Sorts the values according to the index vector given.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/ctioga2/data/datacolumn.rb', line 75

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

#set_values_at(i, value, min = nil, max = nil) ⇒ Object

Sets the values at the given index



143
144
145
146
147
148
149
150
# File 'lib/ctioga2/data/datacolumn.rb', line 143

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

#sizeObject

Returns the number of elements.



138
139
140
# File 'lib/ctioga2/data/datacolumn.rb', line 138

def size
  return @values.size
end

#trim!(nb) ⇒ Object

Only keeps every n points in the DataColumn



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/ctioga2/data/datacolumn.rb', line 182

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

#values_at(i, with_errors = false, expand_nil = true) ⇒ Object

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.



112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/ctioga2/data/datacolumn.rb', line 112

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

#vectorsObject

Vectors: all values if there are error bars, or only the #value one if there isn’t.



129
130
131
132
133
134
135
# File 'lib/ctioga2/data/datacolumn.rb', line 129

def vectors
  if has_errors?
    return [@values, @min_values, @max_values]
  else
    return [@values]
  end
end