class CTioga2::Data::DataColumn

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”

Constants

ColumnSpecsRE

Attributes

max_values[RW]

A Dvector holding maximal values

min_values[RW]

A Dvector holding minimal values

values[RW]

A Dvector holding “real'' values

Public Class Methods

create(number, with_errors = false) click to toggle source

Creates a DataColumn object

# File lib/ctioga2/data/datacolumn.rb, line 51
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) click to toggle source

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

# File lib/ctioga2/data/datacolumn.rb, line 245
def self.from_hash(spec)
  a = DataColumn.new(nil)
  a.from_hash(spec)
  return a
end
new(values, min = nil, max = nil) click to toggle source

todo a dup !

# File lib/ctioga2/data/datacolumn.rb, line 44
def initialize(values, min = nil, max = nil)
  @values = values
  @min_values = min
  @max_values = max
end

Public Instance Methods

<<(column) click to toggle source

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

# File lib/ctioga2/data/datacolumn.rb, line 168
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
apply() { |v| ... } click to toggle source

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

# File lib/ctioga2/data/datacolumn.rb, line 66
def apply
  for v in all_vectors
    yield v if v
  end
end
average_over(istart, iend, itgt, mode = :avg) click to toggle source

Averages over the given indices, and puts the result at the target index.

Different averaging modes are available.

# File lib/ctioga2/data/datacolumn.rb, line 336
def average_over(istart, iend, itgt, mode = :avg)
  case mode
  when :avg
    # Stupidly average over all the values
    for v in all_vectors
      if v
        av = Utils::average_vector(v, istart, iend)
        v[itgt] = av[0]
      end
    end
  when :stddev
    # Ignore errors, and set them from the standard deviation
    @min_values ||= @values.dup
    @max_values ||= @values.dup

    av = Utils::average_vector(@values, istart, iend, 2)
    @values[itgt] = av[0]
    stddev = (av[1] - av[0]**2)**0.5
    @min_values[itgt] = av[0] - stddev
    @max_values[itgt] = av[0] + stddev
  end
end
bin(min, max, nb, normalize = false) click to toggle source

Bins the values of the columns into nb bins between min and max. Values outside that range are not taken into account. If @a normalize is true, then each value is normalized by the total number of samples.

It returns [xv, yv], where xv are the centers of the bins, and yv the counts

Does not take into account the error columns.

# File lib/ctioga2/data/datacolumn.rb, line 303
def bin(min, max, nb, normalize = false)
  total = @values.size*1.0
  xv = Dobjects::Dvector.new(nb)
  yv = Dobjects::Dvector.new(nb)

  0.upto(nb-1) do |i|
    xv[i] = min + (max-min)*(i+0.5)/nb
    yv[i] = 0
  end
  
  for v in @values
    idx = (v-min)/(max-min)
    if idx > 1.0 or idx < 0
      next
    end
    idx = (idx*nb).to_i
    if idx == nb
      idx = nb-1          # case v = max
    end
    yv[idx] += 1
  end

  if normalize
    yv *= (1.0/total)
  end

  return [xv, yv]
end
column_names(base, expand = false) click to toggle source

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

# File lib/ctioga2/data/datacolumn.rb, line 96
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) click to toggle source
# File lib/ctioga2/data/datacolumn.rb, line 279
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_errors() click to toggle source

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

# File lib/ctioga2/data/datacolumn.rb, line 159
def ensure_has_errors
  if ! has_errors?
    @min_values = @values.dup
    @max_values = @values.dup
  end
end
from_hash(spec) click to toggle source

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
    
# File lib/ctioga2/data/datacolumn.rb, line 214
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]
    when /^rerr$/i
      @min_values = @values *(1 - s[key])
      @max_values = @values *(1 + s[key])
    else
      raise "Unkown key: #{key}"
    end
  end
end
has_errors?() click to toggle source

Whether there are error bars.

# File lib/ctioga2/data/datacolumn.rb, line 90
def has_errors?
  return (@min_values && @max_values)
end
max() click to toggle source

Returns the maximum value of all vectors held in this column

# File lib/ctioga2/data/datacolumn.rb, line 266
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
min() click to toggle source

Returns the minimum value of all vectors held in this column

# File lib/ctioga2/data/datacolumn.rb, line 252
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) click to toggle source

Appends the given values at the end of the DataColumn

@todo This isn't very efficient. Does it really matter ?

# File lib/ctioga2/data/datacolumn.rb, line 153
def push_values(value, min=nil, max=nil)
  set_values_at(@values.size, value, min, max)
end
reindex(idx_vector) click to toggle source

Sorts the values according to the index vector given.

# File lib/ctioga2/data/datacolumn.rb, line 73
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
resize!(new_size) click to toggle source

Resize all the columns to the given size.

# File lib/ctioga2/data/datacolumn.rb, line 288
def resize!(new_size)
  for v in all_vectors
    v.resize(new_size) if v
  end
end
set_values_at(i, value, min = nil, max = nil) click to toggle source

Sets the values at the given index

# File lib/ctioga2/data/datacolumn.rb, line 141
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
size() click to toggle source

Returns the number of elements.

# File lib/ctioga2/data/datacolumn.rb, line 136
def size
  return @values.size
end
trim!(nb) click to toggle source

Only keeps every n points in the DataColumn

# File lib/ctioga2/data/datacolumn.rb, line 180
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) click to toggle source

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.

# File lib/ctioga2/data/datacolumn.rb, line 110
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() click to toggle source

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

# File lib/ctioga2/data/datacolumn.rb, line 127
def vectors
  if has_errors?
    return [@values, @min_values, @max_values]
  else
    return [@values]
  end
end

Protected Instance Methods

all_vectors() click to toggle source

All the vectors held by the DataColumn

# File lib/ctioga2/data/datacolumn.rb, line 362
def all_vectors
  return [@values, @min_values, @max_values]
end
set_vectors(vectors) click to toggle source

Sets the vectors to the given list, as might have been returned by all_vectors

# File lib/ctioga2/data/datacolumn.rb, line 368
def set_vectors(vectors)
  @values, @min_values, @max_values = *vectors
end