Class: SciYAG::Backends::BinnerBackend

Inherits:
Backend
  • Object
show all
Includes:
Dobjects
Defined in:
lib/SciYAG/Backends/binner.rb

Instance Method Summary collapse

Methods inherited from Backend

#base_line=, #clear_xy_filters, default_state, describe, #expand_sets, #get_cached_entry, #has_set?, list_backends, list_descriptions, logger=, #meta_data, #pop_xy_filter, #push_xy_filter, #set_type, #sets_available, #xy_data, #xyz_data

Methods included from MetaBuilder::DescriptionExtend

#base_description, #create_factory, #describe, #description, #factory_class, #factory_description, #factory_description_hash, #factory_description_list, #group, #has_factory?, #inherit_parameters, #param, #param_accessor, #param_reader, #param_writer, #register_class, #set_description

Methods included from MetaBuilder::DescriptionInclude

#description, #get_param, #get_param_raw, #long_name, #option_parser_banner, #option_parser_fill, #option_parser_options, #parameter, #restore_state, #save_state, #set_param, #set_param_raw

Constructor Details

#initializeBinnerBackend

Returns a new instance of BinnerBackend.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/SciYAG/Backends/binner.rb', line 51

def initialize
  @number = 20
  @skip = 0
  @separator = /\s+/
  @default_column_spec = "1"

  @x_range = false
  @normalize = false

  @bin_size = false

  super()

  @cache = {}
end

Instance Method Details

#get_data(col_spec) ⇒ Object

Reads the data using the columns specification, provided that the appropriate fle has already been loaded into @current. For now no single sanity check.



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
# File 'lib/SciYAG/Backends/binner.rb', line 114

def get_data(col_spec)
  if col_spec =~ /\$/       # There is a formula in the specification
    formula = col_spec.gsub(/\$(\d+)/, 'column[\1]')
    debug "Using formula '#{formula}'"
    data = Dvector.
      compute_formula(formula, 
                      @current_data,[])
  else
    data = @current_data[col_spec.to_i].dup
  end

  # Now, data is a single Dvector of numbers. We want to bin it:
  if @x_range
    min = @x_range.first
    max = @x_range.last
  else
    min = data.min
    max = data.max
  end
  

  if @bin_size 
    delta = @bin_size
    nb_bins = (((max - min).abs)/@bin_size).ceil.to_i + 1
  else
    nb_bins = @number
    delta = (max - min)/(nb_bins - 1)
  end

  # We create a X Dvector:
  x_values = Dvector.new(nb_bins)
  y_values = Dvector.new(nb_bins)
  nb_bins.times do |i|
    x_values[i] = min + delta * i
    y_values[i] = 0
  end
  
  # Now, we bin it:
  for val in data
    i = ((val - min)/delta).round
    y_values[i] += 1
  end
  
  if @normalize
    y_values /= data.size
  end

  return [x_values, y_values]
end

#query_xy_data(set) ⇒ Object

This is called by the architecture to get the data. It splits the set name into filename@cols, reads the file if necessary and calls get_data



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/SciYAG/Backends/binner.rb', line 95

def query_xy_data(set)
  if set =~ /(.*)@(.*)/
    col_spec = $2
    file = $1
  else
    col_spec = @default_column_spec
    file = set
  end
  if file.length > 0
    @current_data = read_file(file)
    @current = file
  end
  x,y = get_data(col_spec)
  return Function.new(x,y)
end

#read_file(file) ⇒ Object

Reads data from a file. If needed, extract the file from the set specification.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/SciYAG/Backends/binner.rb', line 70

def read_file(file)         
  if file =~ /(.*)@.*/
    file = $1
  end
  name = file               # As file will be modified.
  if ! @cache.key?(file)    # Read the file if it is not cached.
    if file == "-"
      file = $stdin
    elsif file =~ /(.*?)\|\s*$/
      file = IO.popen($1)
    end
    fancy_read_options = {'index_col' => true,
      'skip_first' => @skip,
      'sep' => @separator
    }
    debug "Fancy read '#{file}', options #{fancy_read_options.inspect}"
    @cache[name] = Dvector.fancy_read(file, nil, fancy_read_options)
  end
  return @cache[name]
end