python - Convert C++ datastructure into intelligible HDF5 dataset (Vector of Pairs) -


i have following datastructure in c++:

std::vector<std::pair<boost::geometry::model::point<double, 3, boost::geometry::cs::cartesian>, std::vector<double>>> 

in english, represents bunch of spacial locations, each of coupled metadata vector of same length.

i need constructing elegant representation of datastructure hdf5 dataset.

my first attempt use (in python, mockup)

import h5py f = h5py.file('foo.h5', 'w') f.create_dataset('locations_and_metadata', (num_locations, metadata_len + 3)) 

and interpret first 3 elements in dataset coordinates, ugly , nonintuitive. in particular, can't add units attribute spacial location, decreases 'self-describing' nature of hdf5 file.

the solution compound datatypes, see here example.

to solve particular problem, used:

#!/usr/bin/env python3 import h5py import numpy  output = h5py.file('foo.h5', 'w')  len = 50  x0 = 1.23 y0 = 2.25 z0 = 7.84  a0 = numpy.random.uniform(size=len)  x1 = 2.34; y1 = 4.38; z1 = 7.25;  a1 = numpy.random.uniform(size=len)  comp_type = numpy.dtype([('x', numpy.float64), ('y', numpy.float64), ('z', numpy.float64), ('trace', (numpy.dtype('<d'), len))]) g = output.create_dataset('my_type', (2,), comp_type) data = numpy.array([(x0, y0, z0, a0), (x1, y1, z1, a1)], dtype=comp_type) g[...] = data  output.close() 

the code in c++ bit more complicated; had create contiguous array of doubles (as std::pair not guaranteed contiguous in ram), looking @ documentation h5::comptype should need go. c++ example here.


Comments