source: src/io/file_hdf5.c @ bf3ce2c

feature/crepe_org
Last change on this file since bf3ce2c was bf3ce2c, checked in by Paul Brossier <piem@piem.org>, 6 years ago

[file_hdf5] check if tensor exists before loading, double precision support, improve debug output

  • Property mode set to 100644
File size: 3.4 KB
Line 
1/*
2  Copyright (C) 2018 Paul Brossier <piem@aubio.org>
3
4  This file is part of aubio.
5
6  aubio is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10
11  aubio is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  GNU General Public License for more details.
15
16  You should have received a copy of the GNU General Public License
17  along with aubio.  If not, see <http://www.gnu.org/licenses/>.
18
19*/
20
21#include "aubio_priv.h"
22
23#ifdef HAVE_HDF5
24
25#include "fmat.h"
26#include "ai/tensor.h"
27#include "file_hdf5.h"
28
29#include <hdf5.h>
30#include <hdf5_hl.h>
31
32#if !HAVE_AUBIO_DOUBLE
33#define aubio_H5LTread_dataset_smpl H5LTread_dataset_float
34#else
35#define aubio_H5LTread_dataset_smpl H5LTread_dataset_double
36#endif
37
38struct _aubio_file_hdf5_t {
39  const char_t *path;
40  hid_t fid;
41  hid_t datatype;
42};
43
44aubio_file_hdf5_t *new_aubio_file_hdf5(const char_t *path)
45{
46  aubio_file_hdf5_t *f = AUBIO_NEW(aubio_file_hdf5_t);
47
48  f->fid = H5Fopen(path, H5F_ACC_RDONLY, H5P_DEFAULT);
49  if (f->fid <= 0) goto failure;
50
51  f->path = path;
52  return f;
53
54failure:
55  del_aubio_file_hdf5(f);
56  return NULL;
57}
58
59uint_t aubio_file_hdf5_load_dataset_into_tensor (aubio_file_hdf5_t *f,
60    const char_t *key, aubio_tensor_t *tensor)
61{
62  uint_t i;
63  AUBIO_ASSERT(f && key && tensor);
64  // check arguments
65  if (!f->fid || !key || !tensor)
66    return AUBIO_FAIL;
67  // find key in file
68  hid_t data_id = H5Dopen(f->fid, key, H5P_DEFAULT);
69  if (data_id <= 0) {
70    AUBIO_ERR("file_hdf5: failed getting key %s in %s\n", key, f->path);
71    return AUBIO_FAIL;
72  }
73  // get dimensions
74  hsize_t shape[10];
75  hid_t space = H5Dget_space(data_id);
76  int ndim = H5Sget_simple_extent_dims(space, shape, NULL);
77  if (ndim <= 0) {
78    AUBIO_ERR("file_hdf5: failed to get dims of %s in %s\n", key, f->path);
79    return AUBIO_FAIL;
80  }
81
82  // check output tensor dimension matches
83  AUBIO_ASSERT(ndim == (sint_t)tensor->ndim);
84  for (i = 0; i < (uint_t)ndim; i++) {
85    AUBIO_ASSERT(shape[i] == tensor->shape[i]);
86  }
87
88  if (ndim != (sint_t)tensor->ndim) return AUBIO_FAIL;
89  for (i = 0; i < (uint_t)ndim; i++) {
90    if (shape[i] != tensor->shape[i]) return AUBIO_FAIL;
91  }
92
93  // read data from hdf5 file into tensor buffer
94  smpl_t *buffer = tensor->buffer;
95  herr_t err = aubio_H5LTread_dataset_smpl(f->fid, key, buffer);
96
97  if (err < 0) {
98    return AUBIO_FAIL;
99  }
100
101  //AUBIO_DBG("file_hdf5: loaded : shape %s from key %s\n",
102  //    aubio_tensor_get_shape_string(tensor), key);
103
104  H5Dclose(data_id);
105  return AUBIO_OK;
106}
107
108uint_t aubio_file_hdf5_load_dataset_into_matrix(aubio_file_hdf5_t *f,
109    const char_t *key, fmat_t *mat) {
110  aubio_tensor_t t;
111  if (aubio_fmat_as_tensor (mat, &t)) return AUBIO_FAIL;
112  return aubio_file_hdf5_load_dataset_into_tensor(f, key, &t);
113}
114
115
116uint_t aubio_file_hdf5_load_dataset_into_vector(aubio_file_hdf5_t *f,
117    const char_t *key, fvec_t *vec) {
118  aubio_tensor_t t;
119  if (aubio_fvec_as_tensor (vec, &t)) return AUBIO_FAIL;
120  return aubio_file_hdf5_load_dataset_into_tensor(f, key, &t);
121}
122
123void del_aubio_file_hdf5(aubio_file_hdf5_t *f)
124{
125  AUBIO_ASSERT(f);
126  if (f->fid > 0)
127    H5Fclose(f->fid);
128  AUBIO_FREE(f);
129}
130
131#endif /* HAVE_HDF5 */
Note: See TracBrowser for help on using the repository browser.