From: Paul Brossier Date: Tue, 8 Jan 2019 15:10:43 +0000 (+0100) Subject: [file_hdf5] read data hdf5 files X-Git-Url: https://git.aubio.org/?a=commitdiff_plain;h=42dbcf4f55a89a78884c99f6e5ce076cdf71f343;p=aubio.git [file_hdf5] read data hdf5 files --- diff --git a/src/io/file_hdf5.c b/src/io/file_hdf5.c new file mode 100644 index 00000000..332b48ac --- /dev/null +++ b/src/io/file_hdf5.c @@ -0,0 +1,123 @@ +/* + Copyright (C) 2018 Paul Brossier + + This file is part of aubio. + + aubio is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + aubio is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with aubio. If not, see . + +*/ + +#include "aubio_priv.h" + +#ifdef HAVE_HDF5 + +#include "fmat.h" +#include "ai/tensor.h" +#include "file_hdf5.h" + +#include +#include + +struct _aubio_file_hdf5_t { + const char_t *path; + hid_t fid; + hid_t datatype; +}; + +aubio_file_hdf5_t *new_aubio_file_hdf5(const char_t *path) +{ + aubio_file_hdf5_t *f = AUBIO_NEW(aubio_file_hdf5_t); + + f->fid = H5Fopen(path, H5F_ACC_RDONLY, H5P_DEFAULT); + if (f->fid <= 0) goto failure; + + f->path = path; + return f; + +failure: + del_aubio_file_hdf5(f); + return NULL; +} + +uint_t aubio_file_hdf5_load_dataset_into_tensor (aubio_file_hdf5_t *f, + const char_t *key, aubio_tensor_t *tensor) { + uint_t i; + AUBIO_ASSERT(f && key && tensor); + // check file is open + if (!f->fid) + return AUBIO_FAIL; + // find key in file + hid_t data_id = H5Dopen(f->fid, key, H5P_DEFAULT); + if (data_id <= 0) { + AUBIO_ERR("file_hdf5: failed getting key %s in %s\n", key, f->path); + return AUBIO_FAIL; + } + // get dimensions + hsize_t shape[10]; + hid_t space = H5Dget_space(data_id); + int ndim = H5Sget_simple_extent_dims(space, shape, NULL); + if (ndim <= 0) { + AUBIO_ERR("file_hdf5: failed to get dims of %s in %s\n", key, f->path); + return AUBIO_FAIL; + } + + // check output tensor dimension matches + AUBIO_ASSERT(ndim == (sint_t)tensor->ndim); + for (i = 0; i < (uint_t)ndim; i++) { + //AUBIO_DBG("file_hdf5: found dim %d : %d %d (%s in %s)\n", i, dims[i], + // tensor->shape[i], key, f->path); + AUBIO_ASSERT(shape[i] == tensor->shape[i]); + } + + if (ndim != (sint_t)tensor->ndim) return AUBIO_FAIL; + for (i = 0; i < (uint_t)ndim; i++) { + if (shape[i] != tensor->shape[i]) return AUBIO_FAIL; + } + + // read data from hdf5 file into tensor buffer + smpl_t *buffer = tensor->buffer; + herr_t err = H5LTread_dataset_float(f->fid, key, buffer); + + if (err < 0) { + return AUBIO_FAIL; + } + + H5Dclose(data_id); + return AUBIO_OK; +} + +uint_t aubio_file_hdf5_load_dataset_into_matrix(aubio_file_hdf5_t *f, + const char_t *key, fmat_t *mat) { + aubio_tensor_t t; + if (aubio_fmat_as_tensor (mat, &t)) return AUBIO_FAIL; + return aubio_file_hdf5_load_dataset_into_tensor(f, key, &t); +} + + +uint_t aubio_file_hdf5_load_dataset_into_vector(aubio_file_hdf5_t *f, + const char_t *key, fvec_t *vec) { + aubio_tensor_t t; + if (aubio_fvec_as_tensor (vec, &t)) return AUBIO_FAIL; + return aubio_file_hdf5_load_dataset_into_tensor(f, key, &t); +} + +void del_aubio_file_hdf5(aubio_file_hdf5_t *f) +{ + AUBIO_ASSERT(f); + if (f->fid > 0) + H5Fclose(f->fid); + AUBIO_FREE(f); +} + +#endif /* HAVE_HDF5 */ diff --git a/src/io/file_hdf5.h b/src/io/file_hdf5.h new file mode 100644 index 00000000..333700a7 --- /dev/null +++ b/src/io/file_hdf5.h @@ -0,0 +1,39 @@ +/* + Copyright (C) 2018 Paul Brossier + + This file is part of aubio. + + aubio is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + aubio is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with aubio. If not, see . + +*/ + +#ifndef AUBIO_FILE_HDF5_H +#define AUBIO_FILE_HDF5_H + +typedef struct _aubio_file_hdf5_t aubio_file_hdf5_t; + +aubio_file_hdf5_t *new_aubio_file_hdf5(const char_t *path); + +void del_aubio_file_hdf5(aubio_file_hdf5_t *f); + +uint_t aubio_file_hdf5_load_dataset_into_tensor (aubio_file_hdf5_t *f, + const char_t *key, aubio_tensor_t *tensor); + +uint_t aubio_file_hdf5_load_dataset_into_matrix(aubio_file_hdf5_t *f, + const char_t *key, fmat_t *mat); + +uint_t aubio_file_hdf5_load_dataset_into_vector (aubio_file_hdf5_t *f, + const char_t *key, fvec_t *vec); + +#endif /* AUBIO_FILE_HDF5_H */