Class: HDF5::AttributeManager

Inherits:
Object
  • Object
show all
Defined in:
lib/hdf5/attribute.rb

Instance Method Summary collapse

Constructor Details

#initialize(dataset_id, context = nil) ⇒ AttributeManager

Returns a new instance of AttributeManager.



92
93
94
95
# File 'lib/hdf5/attribute.rb', line 92

def initialize(dataset_id, context = nil)
  @dataset_id = dataset_id
  @context = context
end

Instance Method Details

#[](attr_name) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/hdf5/attribute.rb', line 97

def [](attr_name)
  @context&.ensure_open!(@dataset_id)
  attr = Attribute.new(@dataset_id, attr_name, @context)
  attr.read
ensure
  Native.close_object(attr)
end

#[]=(attr_name, value) ⇒ Object



105
106
107
# File 'lib/hdf5/attribute.rb', line 105

def []=(attr_name, value)
  write(attr_name, value)
end

#create(attr_name, value, dtype: nil, casting: :safe) ⇒ Object

Raises:



140
141
142
143
144
# File 'lib/hdf5/attribute.rb', line 140

def create(attr_name, value, dtype: nil, casting: :safe)
  raise HDF5::Error, "Attribute already exists: #{attr_name}" if key?(attr_name)

  write(attr_name, value, dtype:, casting:)
end

#delete(attr_name) ⇒ Object

Raises:



132
133
134
135
136
137
138
# File 'lib/hdf5/attribute.rb', line 132

def delete(attr_name)
  @context&.ensure_open!(@dataset_id)
  status = HDF5::FFI.H5Adelete(@dataset_id, attr_name)
  raise NativeError, "Failed to delete attribute: #{attr_name}" if status < 0

  self
end

#key?(attr_name) ⇒ Boolean

Returns:

  • (Boolean)

Raises:



124
125
126
127
128
129
130
# File 'lib/hdf5/attribute.rb', line 124

def key?(attr_name)
  @context&.ensure_open!(@dataset_id)
  exists = HDF5::FFI.H5Aexists(@dataset_id, attr_name)
  raise NativeError, "Failed to check attribute existence: #{attr_name}" if exists.negative?

  exists.positive?
end

#keysObject

Raises:



109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/hdf5/attribute.rb', line 109

def keys
  @context&.ensure_open!(@dataset_id)
  names = []
  index = ::FFI::MemoryPointer.new(:ulong_long)
  index.write_ulong_long(0)
  callback = ::FFI::Function.new(:int, %i[int64_t string pointer pointer]) do |_, name, _, _|
    names << name
    0
  end
  status = HDF5::FFI.H5Aiterate2(@dataset_id, :H5_INDEX_NAME, :H5_ITER_NATIVE, index, callback, nil)
  raise NativeError, 'Failed to iterate over attributes' if status < 0

  names
end

#modify(attr_name, value, casting: :safe) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/hdf5/attribute.rb', line 146

def modify(attr_name, value, casting: :safe)
  @context&.ensure_open!(@dataset_id)
  DataHelpers.validate_casting!(casting)
  raise HDF5::Error, "Attribute not found: #{attr_name}" unless key?(attr_name)

  attr_id = HDF5::FFI.H5Aopen(@dataset_id, attr_name, HDF5::DEFAULT_PROPERTY_LIST)
  raise NativeError, "Failed to open attribute: #{attr_name}" if attr_id < 0

  type_id = HDF5::FFI.H5Aget_type(attr_id)
  space_id = HDF5::FFI.H5Aget_space(attr_id)
  raise NativeError, "Failed to inspect attribute: #{attr_name}" if type_id < 0 || space_id < 0

  if Native.extent_type(space_id) == :H5S_NULL
    unless value.is_a?(HDF5::Empty) && value.dtype.to_sym == DType.for_hdf5(type_id).to_sym
      raise ShapeError, 'Cannot assign a value to a Null attribute'
    end
    return value
  end

  if Native.datatype_class(type_id) == :H5T_STRING
    unless HDF5::StringCodec.variable?(type_id)
      raise UnsupportedTypeError, 'Fixed-length string attributes are not yet supported'
    end

    encoding = HDF5::StringCodec.encoding_for(type_id)
    string_values, string_shape = HDF5::StringCodec.normalize_data(value, encoding:)
    unless string_shape == attribute_shape(space_id)
      raise ShapeError,
            'Attribute shape must not change when modifying'
    end

    buffer, _string_pointers = HDF5::StringCodec.buffer_for_values(string_values, encoding:)
    status = HDF5::FFI.H5Awrite(attr_id, type_id, buffer)
  else
    dtype_object = DType.for_hdf5(type_id)
    values = HDF5::DataHelpers.normalize_data(value, label: 'Attribute data', dtype: dtype_object, casting:, convert: false)
    expected_shape = attribute_shape(space_id)
    raise ShapeError, 'Attribute shape must not change when modifying' unless values.shape == expected_shape

    status = HDF5::FFI.H5Awrite(attr_id, DType.for_numo(values).memory_type_id, HDF5::DataHelpers.buffer_for(values))
  end
  raise NativeError, "Failed to modify attribute: #{attr_name}" if status < 0

  value
ensure
  Native.close([:H5Sclose, space_id], [:H5Tclose, type_id], [:H5Aclose, attr_id])
end

#write(attr_name, value, dtype: nil, casting: :safe) ⇒ Object



194
195
196
197
198
199
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
# File 'lib/hdf5/attribute.rb', line 194

def write(attr_name, value, dtype: nil, casting: :safe)
  @context&.ensure_open!(@dataset_id)
  DataHelpers.validate_casting!(casting)

  empty_data = value.is_a?(HDF5::Empty)
  explicit_dtype = DType.for_symbol(dtype) if dtype
  if empty_data && explicit_dtype && explicit_dtype.to_sym != value.dtype.to_sym
    raise ConversionError, 'dtype must match the Null attribute dtype'
  end
  inferred_string = HDF5::StringCodec.string_data?(value)
  if inferred_string && explicit_dtype && explicit_dtype.kind != :string
    raise ConversionError, 'Cannot create a numeric attribute from string data'
  end
  string_type = inferred_string || explicit_dtype&.kind == :string || empty_data && value.dtype.kind == :string
  string_data = string_type && !empty_data
  string_values, string_shape = HDF5::StringCodec.normalize_data(value) if string_data
  values = HDF5::DataHelpers.normalize_data(value, label: 'Attribute data', dtype: explicit_dtype, casting:) unless string_type || empty_data
  dtype_object = empty_data ? value.dtype : (explicit_dtype || DType.for_numo(values)) unless string_type
  type_id = string_type ? HDF5::StringCodec.datatype_id : dtype_object.storage_type_id

  exists = HDF5::FFI.H5Aexists(@dataset_id, attr_name)
  raise NativeError, "Failed to check attribute existence: #{attr_name}" if exists.negative?

  # Write a replacement completely before changing the existing attribute.
  written_name = exists.positive? ? temporary_attribute_name : attr_name

  dataspace_id = create_dataspace(empty_data ? nil : (string_data ? string_shape : values.shape))
  raise NativeError, 'Failed to create attribute dataspace' if dataspace_id < 0

  attr_id = HDF5::FFI.H5Acreate2(
    @dataset_id,
    written_name,
    type_id,
    dataspace_id,
    HDF5::DEFAULT_PROPERTY_LIST,
    HDF5::DEFAULT_PROPERTY_LIST
  )
  raise NativeError, "Failed to create attribute: #{attr_name}" if attr_id < 0

  created = true

  unless empty_data || string_data && string_values.empty?
    buffer, _string_pointers = if string_data
                                 HDF5::StringCodec.buffer_for_values(string_values)
                               else
                                 [HDF5::DataHelpers.buffer_for(values), nil]
                               end
    memory_type_id = string_data ? type_id : dtype_object.memory_type_id
    status = HDF5::FFI.H5Awrite(attr_id, memory_type_id, buffer)
    raise NativeError, "Failed to write attribute: #{attr_name}" if status < 0
  end

  replace_attribute(attr_name, written_name) if exists.positive?
  initialized = true

  value
ensure
  begin
    if created && !initialized && HDF5::FFI.H5Aexists(@dataset_id, written_name).positive?
      HDF5::FFI.H5Adelete(@dataset_id, written_name)
    end
  ensure
    Native.close([:H5Aclose, attr_id], [:H5Sclose, dataspace_id], [:H5Tclose, string_type ? type_id : nil])
  end
end