Class: Hdf5::H5Dataset

Inherits:
Object
  • Object
show all
Extended by:
FFI::Library
Defined in:
lib/hdf5.rb

Overview

Object wrapping an HDF5 Dataset, which contains a set of data, and information about the type of the data elements and the size and shape of the data array.

Defined Under Namespace

Classes: NotFound

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ H5Dataset

Create a new object. id is the id of the HDF5 dataset this wraps. Use H5Dataset.open to open a dataset



164
165
166
# File 'lib/hdf5.rb', line 164

def initialize(id)
  @id = id
end

Class Method Details

.open(location_id, name) ⇒ Object

Open the dataset. location_id is the id of the parent file or group. Returns and H5Dataset object

Raises:



157
158
159
160
161
# File 'lib/hdf5.rb', line 157

def self.open(location_id, name)
  id = basic_open(location_id, name, 0)
  raise NotFound.new("dataset #{name} not found") if id < 0
  return new(id)
end

Instance Method Details

#dataspaceObject



172
173
174
# File 'lib/hdf5.rb', line 172

def dataspace
  H5Dataspace.new(basic_get_space(@id))
end

#datatypeObject

Return an H5Datatype object containing information about the type of an individual member of the dataset



169
170
171
# File 'lib/hdf5.rb', line 169

def datatype
  H5Datatype.new(basic_get_type(@id))
end

#narray_allObject

Create an NArray of the appropriate size and read the entire content of the dataset into it. Will not work for complicated datatypes (basically only works for ints, floats and complexes, where a datatype composed of two floats is assumed to be a complex). There is scope in the future for writing custom closures for reading in more complex datatypes.



203
204
205
206
207
# File 'lib/hdf5.rb', line 203

def narray_all
  narr = NArray.send(narray_type, *dataspace.dims.reverse) # Note narray is fortran-style column major
  basic_read(@id, datatype.id, 0, 0, 0, narr.ffi_pointer)
  narr
end

#narray_simple_read(start_indexes, end_indexes) ⇒ Object

Create an NArray of the appropriate type and size and a subsection of the dataset into it. start_indexes and end_indexes should be arrays of size ndims. start_indexes should contain the (zero-based) offset of the start of the read, and end_indexes should contain the offset of the end of the read. Each element of end_indexes can either be a zero based positive offset, or a negative offset where -1 corresponds to the end of the dataset dimension. This function will not work for complicated datatypes (basically only works for ints, floats and complexes, where a datatype composed of two floats is assumed to be a complex). There is scope in the future for writing custom closures for reading in more complex datatypes. As an example, consider a two-dimensional 6x10 dataset.

dataset.narray_read([0,0], [-1,-1]) # would read the whole of the dataset
dataset.narray_read([0,0], [5,9]) # would read the whole of the dataset
dataset.narray_read([0,0], [2,-1]) # would read half the dataset
dataset.narray_read([0,0], [-4,-1]) # would read the same half of the dataset
dataset.narray_read([2,4], [2,4]) # would read one element of the dataset

Raises:

  • (ArgumentError)


225
226
227
228
229
230
231
232
233
234
235
# File 'lib/hdf5.rb', line 225

def narray_simple_read(start_indexes, end_indexes)
  nd = dataspace.ndims
  raise ArgumentError.new("start_indexes and end_indexes must be of size ndims") unless start_indexes.size == nd and end_indexes.size == nd
  szs = dataspace.dims
  counts = end_indexes.zip(start_indexes.zip(szs)).map{|ei, (si, sz)| ei < 0 ? ei + sz - si + 1 : ei - si + 1}
  dtspce = H5Dataspace.create_simple(counts)
  dtspce.offset_simple(start_indexes)
  narr = NArray.send(narray_type, *dtspce.dims.reverse) # Note narray is fortran-style column major
  basic_read(@id, datatype.id, 0, dtspce.id, 0, narr.ffi_pointer)
  narr
end

#narray_typeObject

Gives the narray type corresponding to the datatype of the dataset Raises an error for unsupported datatypes. datatypes (basically only works for ints, floats and complexes, where a datatype composed of two floats is assumed to be a complex).



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/hdf5.rb', line 179

def narray_type
  #cls = H5Types.h5t_class_t
  #p 'datatype', datatype.h5_class
  case h5c = datatype.h5_class
  when :h5t_integer
    :integer
  when :h5t_float
    :float
  when :h5t_compound
    if datatype.is_complex?
      :complex
    else
      raise "Unsupported datatype for narray: #{h5c}"
    end
  else
    raise "Unsupported datatype for narray: #{h5c}"
  end
end