Module: HDF5::DataHelpers

Defined in:
lib/hdf5/data_helpers.rb

Class Method Summary collapse

Class Method Details

.array_shape(value) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/hdf5/data_helpers.rb', line 84

def array_shape(value)
  return [] unless value.is_a?(Array)
  return [0] if value.empty?

  unless value.first.is_a?(Array)
    raise ShapeError, 'Data must be rectangular' if value.any? { |item| item.is_a?(Array) }

    return [value.length]
  end

  child_shape = array_shape(value.first)
  index = 1
  while index < value.length
    item = value[index]
    unless item.is_a?(Array) && array_shape(item) == child_shape
      raise ShapeError, 'Data must be rectangular'
    end
    index += 1
  end

  [value.length, *child_shape]
end

.buffer_for(narray) ⇒ Object

Raises:



162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/hdf5/data_helpers.rb', line 162

def buffer_for(narray)
  dtype = DType.for_numo(narray)
  binary = if dtype.kind == :bool
             narray.to_a.flatten.map { |value| value.zero? ? 0 : 1 }.pack('C*')
           else
             narray.to_binary
           end
  expected_bytes = narray.size * dtype.itemsize
  raise HDF5::Error, 'Numo binary representation has an unexpected size' unless binary.bytesize == expected_bytes

  ::FFI::MemoryPointer.new(:char, expected_bytes).tap { |buffer| buffer.put_bytes(0, binary) }
end

.from_binary(dtype, binary, shape) ⇒ Object



175
176
177
178
179
180
181
182
# File 'lib/hdf5/data_helpers.rb', line 175

def from_binary(dtype, binary, shape)
  return dtype.numo_class.from_binary(binary, shape) unless dtype.kind == :bool

  bytes = binary.unpack('C*')
  return Numo::Bit.new.store(bytes.first) if shape.empty?

  Numo::UInt8.cast(bytes).reshape(*shape).ne(0)
end

.inferred_dtype(values, label:) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/hdf5/data_helpers.rb', line 58

def inferred_dtype(values, label:)
  kind = nil
  minimum = maximum = 0
  values.each do |value|
    if value.is_a?(Integer)
      current = :int64
      minimum = value if value < minimum
      maximum = value if value > maximum
    elsif value.is_a?(Complex)
      current = :complex128
    elsif value.is_a?(Numeric)
      current = :float64
    elsif value.equal?(true) || value.equal?(false)
      current = :bool
    else
      raise HDF5::Error, "Only numeric #{label.downcase} is supported"
    end
    if kind && (kind == :bool) != (current == :bool)
      raise HDF5::Error, "Only numeric #{label.downcase} is supported"
    end
    kind = current if kind.nil? || current == :complex128 || current == :float64 && kind == :int64
  end
  kind = :uint64 if kind == :int64 && minimum >= 0 && maximum >= (1 << 63)
  DType.for_symbol(kind)
end

.normalize_booleans(value) ⇒ Object



184
185
186
187
188
# File 'lib/hdf5/data_helpers.rb', line 184

def normalize_booleans(value)
  return value.map { |item| normalize_booleans(item) } if value.is_a?(Array)

  value ? 1 : 0
end

.normalize_data(data, label: 'Data', dtype: nil, casting: :safe, convert: true) ⇒ Object

Raises:

  • (ArgumentError)


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/hdf5/data_helpers.rb', line 5

def normalize_data(data, label: 'Data', dtype: nil, casting: :safe, convert: true)
  raise ArgumentError, "Unsupported casting mode: #{casting.inspect}" unless i[safe unsafe].include?(casting)

  if data.is_a?(Numo::NArray)
    source = DType.for_numo(data)
    return data unless dtype
    unless source.castable_to?(dtype, casting:)
      raise ConversionError, "Cannot safely cast #{source.to_sym} to #{dtype.to_sym}"
    end
    return data if source.to_sym == dtype.to_sym
    # HDF5 converts numeric widths and matching complex compounds directly,
    # but cannot convert a real numeric datatype to a complex compound.
    native_conversion = dtype.kind != :complex || source.kind == :complex
    return data if !convert && casting == :safe && native_conversion

    return dtype.numo_class.cast(data)
  end

  # Homogeneous flat Arrays need no recursive shape traversal or per-value
  # conversion checks: integer extrema cover the range; Ruby Float is float64.
  if dtype.nil? && data.is_a?(Array) && !data.empty?
    homogeneous = normalize_homogeneous_array(data, data, casting:)
    return homogeneous if homogeneous
  end

  shape = array_shape(data)
  values = data.is_a?(Array) ? (shape.length <= 1 ? data : data.flatten) : [data]
  raise HDF5::Error, "#{label} must not be empty without an explicit dtype" if values.empty? && !dtype
  if dtype.nil? && shape.length > 1
    homogeneous = normalize_homogeneous_array(data, values, casting:)
    return homogeneous if homogeneous
  end

  dtype ||= inferred_dtype(values, label:)
  validate_values(values, dtype, casting:)
  normalized = dtype.kind == :bool ? normalize_booleans(data) : data
  return dtype.numo_class.new(*shape) if values.empty?
  return Numo::Bit.new.store(normalized) if dtype.kind == :bool && !data.is_a?(Array)

  dtype.numo_class.cast(normalized)
end

.normalize_homogeneous_array(data, values, casting:) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/hdf5/data_helpers.rb', line 47

def normalize_homogeneous_array(data, values, casting:)
  if values.all? { |value| value.is_a?(Integer) }
    minimum, maximum = values.minmax
    dtype = DType.for_symbol(minimum >= 0 && maximum >= (1 << 63) ? :uint64 : :int64)
    validate_values([minimum, maximum], dtype, casting:)
    dtype.numo_class.cast(data)
  elsif values.all? { |value| value.is_a?(Float) }
    Numo::DFloat.cast(data)
  end
end

.scalar?(value) ⇒ Boolean

Returns:

  • (Boolean)


107
108
109
110
# File 'lib/hdf5/data_helpers.rb', line 107

def scalar?(value)
  value.is_a?(Numeric) || value.equal?(true) || value.equal?(false) ||
    value.is_a?(Numo::NArray) && value.shape.empty?
end

.validate_component(component, size, dtype) ⇒ Object

Raises:



148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/hdf5/data_helpers.rb', line 148

def validate_component(component, size, dtype)
  return if size == 8 && component.is_a?(Float)

  raise ConversionError, "Cannot safely cast complex data to #{dtype.to_sym}" if component.is_a?(Complex)

  converted = component.to_f
  converted = [converted].pack('f').unpack1('f') if size == 4
  return if component.is_a?(Float) && !component.finite? && !converted.finite?
  return if component.is_a?(Float) && component == converted
  return if converted.finite? && converted.to_r == component.to_r

  raise ConversionError, "Value #{component.inspect} cannot safely be represented as #{dtype.to_sym}"
end

.validate_values(values, dtype, casting:) ⇒ Object



112
113
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
# File 'lib/hdf5/data_helpers.rb', line 112

def validate_values(values, dtype, casting:)
  if dtype.kind == :bool
    unless values.all? { |value| value.equal?(true) || value.equal?(false) }
      raise ConversionError, 'Bool data must contain true or false'
    end
    return
  end
  if casting == :unsafe
    raise ConversionError, 'Data must contain numeric values' unless values.all? { |value| value.is_a?(Numeric) }
    return
  end

  case dtype.kind
  when :integer
    bits = dtype.itemsize * 8
    minimum = dtype.unsigned? ? 0 : -(1 << (bits - 1))
    maximum = dtype.unsigned? ? (1 << bits) - 1 : (1 << (bits - 1)) - 1
    values.each do |value|
      unless value.is_a?(Integer) && value.between?(minimum, maximum)
        raise ConversionError, "Value #{value.inspect} cannot safely be represented as #{dtype.to_sym}"
      end
    end
  when :float, :complex
    size = dtype.kind == :complex ? dtype.itemsize / 2 : dtype.itemsize
    values.each do |value|
      raise ConversionError, 'Data must contain numeric values' unless value.is_a?(Numeric)
      if dtype.kind == :complex
        validate_component(value.real, size, dtype)
        validate_component(value.imag, size, dtype)
      else
        validate_component(value, size, dtype)
      end
    end
  end
end