Class: SciYAG::Backends::MDBBackend
- Includes:
- Dobjects
- Defined in:
- lib/SciYAG/Backends/mdb.rb
Direct Known Subclasses
Defined Under Namespace
Classes: DataSet
Constant Summary collapse
- COL_SEP =
The separator for columns when reading the file.
"###"
- MDB_EXPORT =
The command to run mdb-export
"mdb-export"
- SUBSET_RE =
This RE tells if a set looks like it might contain a subset. Slurps anything until the last @.
/(.+)@(.+)/
Instance Attribute Summary collapse
-
#current_database ⇒ Object
readonly
Returns the value of attribute current_database.
-
#data_set_name_col ⇒ Object
If this attribute is set, the sets are further split for the different values of that column.
-
#data_set_table_name ⇒ Object
The table of the file where we want to look for data.
-
#data_set_x_col ⇒ Object
The name of the X, Y and Z columns in the table.
-
#data_set_y_col ⇒ Object
The name of the X, Y and Z columns in the table.
-
#data_set_z_col ⇒ Object
The name of the X, Y and Z columns in the table.
Instance Method Summary collapse
- #expand_sets(set) ⇒ Object
-
#get_data(set) ⇒ Object
Internal function to get the data corresponding to one set.
-
#initialize ⇒ MDBBackend
constructor
A new instance of MDBBackend.
-
#internal_to_external(x, y, z) ⇒ Object
An internal function saying how to make a X,Y dataset from a x,y,z dataset.
-
#mdb_export_table(file, table, header = false) ⇒ Object
Reads a table and returns the table of lines, optionnally beginning with the header.
-
#mdb_table_to_hash(file, table) ⇒ Object
Reads a table and turns it into a list of hashes.
-
#query_xy_data(set) ⇒ Object
This is called by the architecture to get the data.
-
#read_mdb_file(file) ⇒ Object
Reads the contents of a MDB file according to different defaults.
-
#safe_float(x) ⇒ Object
Small helper function.
-
#sets_available ⇒ Object
I think it is better not to take the cycles into account: they will hinder the display for no good reason.
-
#string_to_set(str) ⇒ Object
Transforms a string into a set.
-
#turn_header_into_hash(lines) ⇒ Object
Turns the first line returned by mdb_export_table into a nice header hash.
Methods inherited from Backend
#base_line=, #clear_xy_filters, default_state, describe, #get_cached_entry, #has_set?, list_backends, list_descriptions, logger=, #meta_data, #pop_xy_filter, #push_xy_filter, #set_type, #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
#initialize ⇒ MDBBackend
Returns a new instance of MDBBackend.
114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/SciYAG/Backends/mdb.rb', line 114 def initialize super() @data_set_table_name = nil # has to be redefined by children # A hash holding dataset_name -> DataSet object @data_sets = {} @data_set_id = [] # The same as upper, # but with the set number # The column for different sets @data_set_name_col = "Series" # The current database @current_database = "" end |
Instance Attribute Details
#current_database ⇒ Object (readonly)
Returns the value of attribute current_database.
194 195 196 |
# File 'lib/SciYAG/Backends/mdb.rb', line 194 def current_database @current_database end |
#data_set_name_col ⇒ Object
If this attribute is set, the sets are further split for the different values of that column
49 50 51 |
# File 'lib/SciYAG/Backends/mdb.rb', line 49 def data_set_name_col @data_set_name_col end |
#data_set_table_name ⇒ Object
The table of the file where we want to look for data.
42 43 44 |
# File 'lib/SciYAG/Backends/mdb.rb', line 42 def data_set_table_name @data_set_table_name end |
#data_set_x_col ⇒ Object
The name of the X, Y and Z columns in the table.
45 46 47 |
# File 'lib/SciYAG/Backends/mdb.rb', line 45 def data_set_x_col @data_set_x_col end |
#data_set_y_col ⇒ Object
The name of the X, Y and Z columns in the table.
45 46 47 |
# File 'lib/SciYAG/Backends/mdb.rb', line 45 def data_set_y_col @data_set_y_col end |
#data_set_z_col ⇒ Object
The name of the X, Y and Z columns in the table.
45 46 47 |
# File 'lib/SciYAG/Backends/mdb.rb', line 45 def data_set_z_col @data_set_z_col end |
Instance Method Details
#expand_sets(set) ⇒ Object
322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/SciYAG/Backends/mdb.rb', line 322 def (set) if s = string_to_set(set) subs = s.subsets # we sort so that everything ids # looking fine. if subs.empty? return [set] else return subs.collect {|s| "#{set}@#{s}"} end end return [set] end |
#get_data(set) ⇒ Object
Internal function to get the data corresponding to one set.
289 290 291 292 293 294 295 296 297 298 299 300 301 |
# File 'lib/SciYAG/Backends/mdb.rb', line 289 def get_data(set) if set =~ SUBSET_RE s = string_to_set($1) raise "Set #{$1} is unkown" unless s return internal_to_external(s.x_data($2), s.y_data($2), s.z_data($2)) else s = string_to_set(set) raise "Set #{set} is unkown" unless s return internal_to_external(s.x_data, s.y_data, s.z_data) end end |
#internal_to_external(x, y, z) ⇒ Object
An internal function saying how to make a X,Y dataset from a x,y,z dataset. Can depend on many things. We just output X and Y cols here. Better be redefined by children.
284 285 286 |
# File 'lib/SciYAG/Backends/mdb.rb', line 284 def internal_to_external(x,y,z) return Function.new(x,y) end |
#mdb_export_table(file, table, header = false) ⇒ Object
Reads a table and returns the table of lines, optionnally beginning with the header
136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/SciYAG/Backends/mdb.rb', line 136 def mdb_export_table(file, table, header = false) cmd_line = "#{MDB_EXPORT} -Q -d '#{COL_SEP}' " + if header " " else "-H " end + "'#{file}' '#{table}' " + "| sort -n" # we sort numerically with the program sort, # decently faster mdb = IO.popen(cmd_line) l = mdb.readlines return l end |
#mdb_table_to_hash(file, table) ⇒ Object
Reads a table and turns it into a list of hashes
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/SciYAG/Backends/mdb.rb', line 152 def mdb_table_to_hash(file, table) pipe = IO.popen("#{MDB_EXPORT} -Q -d '#{COL_SEP}' -R '&&&&&' " + "#{file} #{table}") header = pipe.readline.chomp.split(COL_SEP) raw_entries = pipe.read.split('&&&&&').map {|l| l.split(COL_SEP)} entries = [] for raw_entry in raw_entries entry = {} header.length.times do |i| entry[header[i]] = raw_entry[i] end entries << entry end return entries 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
306 307 308 309 |
# File 'lib/SciYAG/Backends/mdb.rb', line 306 def query_xy_data(set) data = get_data(set) return Function.new(data.x.dup, data.y.dup) end |
#read_mdb_file(file) ⇒ Object
Reads the contents of a MDB file according to different defaults.
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 |
# File 'lib/SciYAG/Backends/mdb.rb', line 200 def read_mdb_file(file) # First, make out the datasets from the file if @current_database == file return # We already read it !! end @current_database = file data_sets_properties = mdb_table_to_hash(file, 'DataSetProperties') prospective_data_sets = [] for set in data_sets_properties prospective_data_sets << DataSet.new(set["DataSetName"], set["DataSetID"]) end # We turn it into another array indexed on DataSetID for faster lookup @data_sets_ids = [] for ds in prospective_data_sets @data_sets_ids[ds.data_set_id.to_i] = ds end # Now, we need to get data from the files raise "@data_set_table_name has to be set" unless @data_set_table_name raise "@data_set_y_col has to be set" unless @data_set_y_col raise "@data_set_x_col has to be set" unless @data_set_x_col raise "@data_set_z_col has to be set" unless @data_set_z_col l = mdb_export_table(file, @data_set_table_name, true) h = turn_header_into_hash(l) if h.key?(@data_set_x_col) and h.key?(@data_set_y_col) and h.key?(@data_set_z_col) # We first sort the data respective to the first column # l.sort! do |a,b| # i_a = a[0,a.index(COL_SEP)].to_i # i_b = b[0,b.index(COL_SEP)].to_i # i_a <=> i_b # end x_col = h[@data_set_x_col] y_col = h[@data_set_y_col] z_col = h[@data_set_z_col] set_col = if @data_set_name_col h[@data_set_name_col] else false end id = h["DataSetID"] l.each do |l| a = l.split(COL_SEP) ds = @data_sets_ids[a[id].to_i] x = safe_float(a[x_col]) y = safe_float(a[y_col]) z = safe_float(a[z_col]) if data_set_name_col ds.add_point(x,y,z,a[set_col]) else ds.add_point(x,y,z) end end else raise "It looks like #{@data_set_x_col} or #{@data_set_y_col} is " + "missing from the table #{@data_set_table_name} of file #{file}" end # Now, we populate the data_sets hash for ds in prospective_data_sets @data_sets[ds.name] = ds end end |
#safe_float(x) ⇒ Object
Small helper function
183 184 185 186 187 188 189 |
# File 'lib/SciYAG/Backends/mdb.rb', line 183 def safe_float(x) begin Float(x) rescue 0.0/0.0 end end |
#sets_available ⇒ Object
I think it is better not to take the cycles into account: they will hinder the display for no good reason.
337 338 339 |
# File 'lib/SciYAG/Backends/mdb.rb', line 337 def sets_available return @data_sets.keys.sort end |
#string_to_set(str) ⇒ Object
Transforms a string into a set. If the set starts with a #, it means we’re directly interested in the number, not in the name. Can be a great deal useful, and much shorter…
314 315 316 317 318 319 320 |
# File 'lib/SciYAG/Backends/mdb.rb', line 314 def string_to_set(str) if str =~ /^\#(\d+)$/ return @data_sets_ids[$1.to_i] else return @data_sets[str] end end |
#turn_header_into_hash(lines) ⇒ Object
Turns the first line returned by mdb_export_table into a nice header hash
170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/SciYAG/Backends/mdb.rb', line 170 def turn_header_into_hash(lines) header = lines.shift header = header.split(COL_SEP) header_hash = {} i = 0 for name in header header_hash[name] = i i+=1 end return header_hash end |