Class: Jcsv::MDArrayReader

Inherits:
MapReader show all
Defined in:
lib/mdarray_reader.rb

Overview

Instance Attribute Summary

Attributes inherited from Reader

#chunk_size, #col_sep, #column_mapping, #comment_matches, #comment_starts, #data_labels, #dimensions_names, #filename, #format, #headers, #ignore_empty_lines, #processed_column, #quote_char, #rows, #strings_as_keys, #suppress_warnings, #surrounding_space_need_quotes

Instance Method Summary collapse

Methods inherited from MapReader

#mapping=

Methods inherited from Reader

#[], #dimensions, #each, #mapping=

Constructor Details

#initialize(*params) ⇒ MDArrayReader





39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/mdarray_reader.rb', line 39

def initialize(*params)

  filter = nil
  
  @dtype = params[1].delete(:dtype)
  
  case @dtype
  when :byte, :short, :int
    filter = Jcsv.int
  when :long
    filter = Jcsv.long
  when :float, :double
    filter = Jcsv.double
  else
    raise "Cannot create MDArray of dtype '#{@dtype}'"
  end
  
  params[1][:default_filter] = filter
  super(*params)
  
end

Instance Method Details

#readObject





65
66
67
# File 'lib/mdarray_reader.rb', line 65

def read
  to_mdarray(@dtype, super)
end

#to_mdarray(dtype, storage) ⇒ Object


Converts the data to an MDArray




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
# File 'lib/mdarray_reader.rb', line 73

def to_mdarray(dtype, storage)

  raise "Cannot convert deep map into MDArray" if (@deep_map == true)
  
  prod = nil
  shape = []
  vector = []
  
  columns = @column_mapping.mapping - [true, false, nil]
  header_size = columns.size

  if (@dimensions.nil?)
    shape = [storage.size, header_size]
    storage.each do |line|
      vector.concat(line.values)
    end
  else
    @dimensions.dimensions_names.each do |name|
      keys = @dimensions[name].labels.keys
      shape << keys.size
      prod = (prod.nil?)? keys : prod.product(keys)
    end
    
    shape << header_size
    
    prod.each do |k|
      row = (@dimensions.dimensions_names.size > 1)? storage[k.flatten.join(".")] : storage[k]
      vector.concat(((row.nil?)? ([Float::NAN] * header_size) : row.values))
    end
  end

  array = MDArray.build(@dtype, shape, vector)
  
end