source: src/io/file_hdf5.c @ 5154024

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

[file_hdf5] read data hdf5 files

  • Property mode set to 100644
File size: 3.2 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
32struct _aubio_file_hdf5_t {
33  const char_t *path;
34  hid_t fid;
35  hid_t datatype;
36};
37
38aubio_file_hdf5_t *new_aubio_file_hdf5(const char_t *path)
39{
40  aubio_file_hdf5_t *f = AUBIO_NEW(aubio_file_hdf5_t);
41
42  f->fid = H5Fopen(path, H5F_ACC_RDONLY, H5P_DEFAULT);
43  if (f->fid <= 0) goto failure;
44
45  f->path = path;
46  return f;
47
48failure:
49  del_aubio_file_hdf5(f);
50  return NULL;
51}
52
53uint_t aubio_file_hdf5_load_dataset_into_tensor (aubio_file_hdf5_t *f,
54    const char_t *key, aubio_tensor_t *tensor) {
55  uint_t i;
56  AUBIO_ASSERT(f && key && tensor);
57  // check file is open
58  if (!f->fid)
59    return AUBIO_FAIL;
60  // find key in file
61  hid_t data_id = H5Dopen(f->fid, key, H5P_DEFAULT);
62  if (data_id <= 0) {
63    AUBIO_ERR("file_hdf5: failed getting key %s in %s\n", key, f->path);
64    return AUBIO_FAIL;
65  }
66  // get dimensions
67  hsize_t shape[10];
68  hid_t space = H5Dget_space(data_id);
69  int ndim = H5Sget_simple_extent_dims(space, shape, NULL);
70  if (ndim <= 0) {
71    AUBIO_ERR("file_hdf5: failed to get dims of %s in %s\n", key, f->path);
72    return AUBIO_FAIL;
73  }
74
75  // check output tensor dimension matches
76  AUBIO_ASSERT(ndim == (sint_t)tensor->ndim);
77  for (i = 0; i < (uint_t)ndim; i++) {
78    //AUBIO_DBG("file_hdf5: found dim %d : %d %d (%s in %s)\n", i, dims[i],
79    //    tensor->shape[i], key, f->path);
80    AUBIO_ASSERT(shape[i] == tensor->shape[i]);
81  }
82
83  if (ndim != (sint_t)tensor->ndim) return AUBIO_FAIL;
84  for (i = 0; i < (uint_t)ndim; i++) {
85    if (shape[i] != tensor->shape[i]) return AUBIO_FAIL;
86  }
87
88  // read data from hdf5 file into tensor buffer
89  smpl_t *buffer = tensor->buffer;
90  herr_t err = H5LTread_dataset_float(f->fid, key, buffer);
91
92  if (err < 0) {
93    return AUBIO_FAIL;
94  }
95
96  H5Dclose(data_id);
97  return AUBIO_OK;
98}
99
100uint_t aubio_file_hdf5_load_dataset_into_matrix(aubio_file_hdf5_t *f,
101    const char_t *key, fmat_t *mat) {
102  aubio_tensor_t t;
103  if (aubio_fmat_as_tensor (mat, &t)) return AUBIO_FAIL;
104  return aubio_file_hdf5_load_dataset_into_tensor(f, key, &t);
105}
106
107
108uint_t aubio_file_hdf5_load_dataset_into_vector(aubio_file_hdf5_t *f,
109    const char_t *key, fvec_t *vec) {
110  aubio_tensor_t t;
111  if (aubio_fvec_as_tensor (vec, &t)) return AUBIO_FAIL;
112  return aubio_file_hdf5_load_dataset_into_tensor(f, key, &t);
113}
114
115void del_aubio_file_hdf5(aubio_file_hdf5_t *f)
116{
117  AUBIO_ASSERT(f);
118  if (f->fid > 0)
119    H5Fclose(f->fid);
120  AUBIO_FREE(f);
121}
122
123#endif /* HAVE_HDF5 */
Note: See TracBrowser for help on using the repository browser.