Class: HDF5::DType
- Inherits:
-
Object
- Object
- HDF5::DType
- Defined in:
- lib/hdf5/dtype.rb
Constant Summary collapse
- TYPES =
{ int8: [Numo::Int8, :H5T_NATIVE_INT8_g, :H5T_STD_I8LE_g, :integer, 1], uint8: [Numo::UInt8, :H5T_NATIVE_UINT8_g, :H5T_STD_U8LE_g, :integer, 1], int16: [Numo::Int16, :H5T_NATIVE_INT16_g, :H5T_STD_I16LE_g, :integer, 2], uint16: [Numo::UInt16, :H5T_NATIVE_UINT16_g, :H5T_STD_U16LE_g, :integer, 2], int32: [Numo::Int32, :H5T_NATIVE_INT32_g, :H5T_STD_I32LE_g, :integer, 4], uint32: [Numo::UInt32, :H5T_NATIVE_UINT32_g, :H5T_STD_U32LE_g, :integer, 4], int64: [Numo::Int64, :H5T_NATIVE_INT64_g, :H5T_STD_I64LE_g, :integer, 8], uint64: [Numo::UInt64, :H5T_NATIVE_UINT64_g, :H5T_STD_U64LE_g, :integer, 8], float32: [Numo::SFloat, :H5T_NATIVE_FLOAT, :H5T_IEEE_F32LE_g, :float, 4], float64: [Numo::DFloat, :H5T_NATIVE_DOUBLE, :H5T_IEEE_F64LE_g, :float, 8], bool: [Numo::Bit, nil, nil, :bool, 1], complex64: [Numo::SComplex, nil, nil, :complex, 8], complex128: [Numo::DComplex, nil, nil, :complex, 16] }.freeze
Instance Attribute Summary collapse
-
#byteorder ⇒ Object
readonly
Returns the value of attribute byteorder.
-
#hdf5_class ⇒ Object
readonly
Returns the value of attribute hdf5_class.
-
#itemsize ⇒ Object
readonly
Returns the value of attribute itemsize.
-
#kind ⇒ Object
readonly
Returns the value of attribute kind.
-
#memory_type_name ⇒ Object
readonly
Returns the value of attribute memory_type_name.
-
#numo_class ⇒ Object
readonly
Returns the value of attribute numo_class.
-
#offset ⇒ Object
readonly
Returns the value of attribute offset.
-
#precision ⇒ Object
readonly
Returns the value of attribute precision.
-
#storage_type_name ⇒ Object
readonly
Returns the value of attribute storage_type_name.
Class Method Summary collapse
- .bool_type_id(native:) ⇒ Object
- .complex_type_id(size, native:) ⇒ Object
- .for_bool_hdf5(type_id, itemsize) ⇒ Object
- .for_complex_hdf5(type_id, itemsize) ⇒ Object
- .for_hdf5(type_id) ⇒ Object
- .for_numo(value) ⇒ Object
- .for_symbol(symbol) ⇒ Object
Instance Method Summary collapse
- #castable_to?(target, casting: :safe) ⇒ Boolean
-
#initialize(symbol, numo_class, memory_type_name, storage_type_name, kind, itemsize, byteorder: :little, precision: itemsize * 8, offset: 0, hdf5_class: nil) ⇒ DType
constructor
A new instance of DType.
- #memory_type_id ⇒ Object
- #storage_type_id ⇒ Object
- #to_sym ⇒ Object
- #unsigned? ⇒ Boolean
Constructor Details
#initialize(symbol, numo_class, memory_type_name, storage_type_name, kind, itemsize, byteorder: :little, precision: itemsize * 8, offset: 0, hdf5_class: nil) ⇒ DType
Returns a new instance of DType.
100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/hdf5/dtype.rb', line 100 def initialize(symbol, numo_class, memory_type_name, storage_type_name, kind, itemsize, byteorder: :little, precision: itemsize * 8, offset: 0, hdf5_class: nil) @symbol = symbol @numo_class = numo_class @memory_type_name = memory_type_name @storage_type_name = storage_type_name @kind = kind @itemsize = itemsize @byteorder = byteorder @precision = precision @offset = offset @hdf5_class = hdf5_class || (kind == :integer ? :H5T_INTEGER : :H5T_FLOAT) freeze end |
Instance Attribute Details
#byteorder ⇒ Object (readonly)
Returns the value of attribute byteorder.
19 20 21 |
# File 'lib/hdf5/dtype.rb', line 19 def byteorder @byteorder end |
#hdf5_class ⇒ Object (readonly)
Returns the value of attribute hdf5_class.
19 20 21 |
# File 'lib/hdf5/dtype.rb', line 19 def hdf5_class @hdf5_class end |
#itemsize ⇒ Object (readonly)
Returns the value of attribute itemsize.
19 20 21 |
# File 'lib/hdf5/dtype.rb', line 19 def itemsize @itemsize end |
#kind ⇒ Object (readonly)
Returns the value of attribute kind.
19 20 21 |
# File 'lib/hdf5/dtype.rb', line 19 def kind @kind end |
#memory_type_name ⇒ Object (readonly)
Returns the value of attribute memory_type_name.
19 20 21 |
# File 'lib/hdf5/dtype.rb', line 19 def memory_type_name @memory_type_name end |
#numo_class ⇒ Object (readonly)
Returns the value of attribute numo_class.
19 20 21 |
# File 'lib/hdf5/dtype.rb', line 19 def numo_class @numo_class end |
#offset ⇒ Object (readonly)
Returns the value of attribute offset.
19 20 21 |
# File 'lib/hdf5/dtype.rb', line 19 def offset @offset end |
#precision ⇒ Object (readonly)
Returns the value of attribute precision.
19 20 21 |
# File 'lib/hdf5/dtype.rb', line 19 def precision @precision end |
#storage_type_name ⇒ Object (readonly)
Returns the value of attribute storage_type_name.
19 20 21 |
# File 'lib/hdf5/dtype.rb', line 19 def storage_type_name @storage_type_name end |
Class Method Details
.bool_type_id(native:) ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/hdf5/dtype.rb', line 159 def bool_type_id(native:) @bool_type_ids ||= {} @bool_type_ids[native] ||= begin base_id = HDF5::FFI.public_send(native ? :H5T_NATIVE_INT8_g : :H5T_STD_I8LE_g) type_id = HDF5::FFI.H5Tenum_create(base_id) raise HDF5::Error, 'Failed to create bool datatype' if type_id < 0 false_value = ::FFI::MemoryPointer.new(:int8).tap { |pointer| pointer.write_int8(0) } true_value = ::FFI::MemoryPointer.new(:int8).tap { |pointer| pointer.write_int8(1) } if HDF5::FFI.H5Tenum_insert(type_id, 'FALSE', false_value) < 0 || HDF5::FFI.H5Tenum_insert(type_id, 'TRUE', true_value) < 0 HDF5::FFI.H5Tclose(type_id) raise HDF5::Error, 'Failed to define bool datatype' end type_id end end |
.complex_type_id(size, native:) ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/hdf5/dtype.rb', line 178 def complex_type_id(size, native:) @complex_type_ids ||= {} @complex_type_ids[[size, native]] ||= begin component_id = if native HDF5::FFI.public_send(size == 8 ? :H5T_NATIVE_FLOAT : :H5T_NATIVE_DOUBLE) else HDF5::FFI.public_send(size == 8 ? :H5T_IEEE_F32LE_g : :H5T_IEEE_F64LE_g) end type_id = HDF5::FFI.H5Tcreate(:H5T_COMPOUND, size) raise HDF5::Error, 'Failed to create complex datatype' if type_id < 0 if HDF5::FFI.H5Tinsert(type_id, 'r', 0, component_id) < 0 || HDF5::FFI.H5Tinsert(type_id, 'i', size / 2, component_id) < 0 HDF5::FFI.H5Tclose(type_id) raise HDF5::Error, 'Failed to define complex datatype' end type_id end end |
.for_bool_hdf5(type_id, itemsize) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/hdf5/dtype.rb', line 66 def self.for_bool_hdf5(type_id, itemsize) false_value = ::FFI::MemoryPointer.new(:int8) true_value = ::FFI::MemoryPointer.new(:int8) valid = itemsize == 1 && HDF5::FFI.H5Tget_nmembers(type_id) == 2 && HDF5::FFI.H5Tenum_valueof(type_id, 'FALSE', false_value) >= 0 && HDF5::FFI.H5Tenum_valueof(type_id, 'TRUE', true_value) >= 0 && false_value.read_int8.zero? && true_value.read_int8 == 1 raise UnsupportedTypeError, 'Unsupported HDF5 enum datatype' unless valid new(:bool, *TYPES.fetch(:bool), byteorder: :none, hdf5_class: :H5T_ENUM) end |
.for_complex_hdf5(type_id, itemsize) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/hdf5/dtype.rb', line 78 def self.for_complex_hdf5(type_id, itemsize) component_size = itemsize / 2 real_index = HDF5::FFI.H5Tget_member_index(type_id, 'r') imaginary_index = HDF5::FFI.H5Tget_member_index(type_id, 'i') valid = [8, 16].include?(itemsize) && real_index >= 0 && imaginary_index >= 0 && HDF5::FFI.H5Tget_nmembers(type_id) == 2 && HDF5::FFI.H5Tget_member_offset(type_id, real_index) == 0 && HDF5::FFI.H5Tget_member_offset(type_id, imaginary_index) == component_size [real_index, imaginary_index].each do |index| next unless index >= 0 member_type_id = HDF5::FFI.H5Tget_member_type(type_id, index) valid &&= member_type_id >= 0 && HDF5::FFI.H5Tget_class(member_type_id) == :H5T_FLOAT && HDF5::FFI.H5Tget_size(member_type_id) == component_size HDF5::FFI.H5Tclose(member_type_id) if member_type_id >= 0 end raise UnsupportedTypeError, 'Unsupported HDF5 compound datatype' unless valid symbol = itemsize == 8 ? :complex64 : :complex128 new(symbol, *TYPES.fetch(symbol), hdf5_class: :H5T_COMPOUND) end |
.for_hdf5(type_id) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/hdf5/dtype.rb', line 35 def self.for_hdf5(type_id) type_class = HDF5::FFI.H5Tget_class(type_id) itemsize = HDF5::FFI.H5Tget_size(type_id) return for_bool_hdf5(type_id, itemsize) if type_class == :H5T_ENUM return for_complex_hdf5(type_id, itemsize) if type_class == :H5T_COMPOUND symbol = case type_class when :H5T_INTEGER prefix = HDF5::FFI.H5Tget_sign(type_id) == :H5T_SGN_NONE ? 'uint' : 'int' "#{prefix}#{itemsize * 8}".to_sym when :H5T_FLOAT "float#{itemsize * 8}".to_sym else raise UnsupportedTypeError, "Unsupported HDF5 datatype: #{type_class}" end precision = HDF5::FFI.H5Tget_precision(type_id) offset = HDF5::FFI.H5Tget_offset(type_id) unless precision == itemsize * 8 raise UnsupportedTypeError, "Unsupported #{precision}-bit datatype in #{itemsize * 8}-bit storage" end raise UnsupportedTypeError, "Unsupported datatype bit offset: #{offset}" unless offset.zero? order = HDF5::FFI.H5Tget_order(type_id) byteorder = { H5T_ORDER_LE: :little, H5T_ORDER_BE: :big, H5T_ORDER_NONE: :none }.fetch(order) do raise UnsupportedTypeError, "Unsupported datatype byte order: #{order}" end new(symbol, *TYPES.fetch(symbol), byteorder:, precision:, offset:, hdf5_class: type_class) end |
Instance Method Details
#castable_to?(target, casting: :safe) ⇒ Boolean
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/hdf5/dtype.rb', line 133 def castable_to?(target, casting: :safe) raise ArgumentError, "Unsupported casting mode: #{casting.inspect}" unless %i[safe unsafe].include?(casting) return true if casting == :unsafe || to_sym == target.to_sym if kind == :integer && target.kind == :integer return itemsize <= target.itemsize if unsigned? == target.unsigned? return false unless unsigned? && !target.unsigned? return itemsize < target.itemsize end return itemsize <= target.itemsize if kind == :float && target.kind == :float if kind == :integer && target.kind == :float significant_bits = unsigned? ? itemsize * 8 : itemsize * 8 - 1 mantissa_bits = target.itemsize == 4 ? 24 : 53 return significant_bits <= mantissa_bits end false end |
#memory_type_id ⇒ Object
115 116 117 118 119 120 |
# File 'lib/hdf5/dtype.rb', line 115 def memory_type_id return self.class.bool_type_id(native: true) if kind == :bool return self.class.complex_type_id(itemsize, native: true) if kind == :complex HDF5::FFI.public_send(memory_type_name) end |
#storage_type_id ⇒ Object
122 123 124 125 126 127 |
# File 'lib/hdf5/dtype.rb', line 122 def storage_type_id return self.class.bool_type_id(native: false) if kind == :bool return self.class.complex_type_id(itemsize, native: false) if kind == :complex HDF5::FFI.public_send(storage_type_name) end |
#to_sym ⇒ Object
129 130 131 |
# File 'lib/hdf5/dtype.rb', line 129 def to_sym @symbol end |
#unsigned? ⇒ Boolean
154 155 156 |
# File 'lib/hdf5/dtype.rb', line 154 def unsigned? @symbol.to_s.start_with?('uint') end |