Loading [MathJax]/extensions/TeX/AMSsymbols.js
LALInference 4.1.9.1-5e288d3
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
LALInferenceHDF5.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2016 John Veitch and Leo Singer
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with with program; see the file COPYING. If not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 * MA 02110-1301 USA
18 */
19
20#include <lal/LALInference.h>
21#include <lal/LALInferenceVCSInfo.h>
22#include <lal/H5FileIO.h>
23#include <lal/LALVCSInfoType.h>
24#include <lal/LALInferenceHDF5.h>
25#include <stdlib.h>
26#include <sys/stat.h>
27
28const char LALInferenceHDF5PosteriorSamplesDatasetName[] = "posterior_samples";
29const char LALInferenceHDF5NestedSamplesDatasetName[] = "nested_samples";
30
31
33{
34 struct stat st;
35 if( !stat(filename, &st) )
36 {
37 printf("File %s is size %jd bytes\n",filename,(intmax_t) st.st_size);
38 }
39 return(0);
40}
41
43{
44 struct stat st;
45 if( !stat(filename, &st) )
46 {
47 if (st.st_size!=0) return(1);
48 }
49 return(0);
50}
51
53 LALH5Generic gdataset, LALInferenceVariables *vars, char *name);
54
55
57 LALH5File *h5file, const char *codename, const char *runID)
58{
59 LALH5File *codeGroup = XLALH5GroupOpen(h5file, codename);
60 char *versionString = XLALVCSInfoString(lalInferenceVCSInfoList, 0, "");
61 XLALH5FileAddStringAttribute(codeGroup, "version", versionString );
62 XLALFree(versionString);
63
64 LALH5File *runGroup = XLALH5GroupOpen(codeGroup, runID);
65 XLALH5FileClose(codeGroup);
66 return(runGroup);
67}
68
69
71 LALH5Dataset *dataset, LALInferenceVariables ***varsArray, UINT4 *N)
72{
73 size_t type_size = XLALH5TableQueryRowSize(dataset);
74 size_t Nvary = XLALH5TableQueryNColumns(dataset);
75 LALH5Generic gdataset = {.dset = dataset};
76
77 int vary[Nvary];
78 char *column_names[Nvary];
79 size_t column_offsets[Nvary];
80 LALInferenceVariableType column_types[Nvary];
81 int ret;
82 (void) ret;
83
84 for (size_t i = 0; i < Nvary; i ++)
85 {
86 size_t column_name_len = XLALH5TableQueryColumnName(
87 NULL, 0, dataset, i);
88 column_names[i] = malloc(column_name_len + 1);
90 column_names[i], column_name_len + 1, dataset, i);
91 column_offsets[i] = XLALH5TableQueryColumnOffset(dataset, i);
92 LALTYPECODE column_type = XLALH5TableQueryColumnType(dataset, i);
93
94 switch (column_type)
95 {
96 case LAL_D_TYPE_CODE:
97 column_types[i] = LALINFERENCE_REAL8_t; break;
98 case LAL_S_TYPE_CODE:
99 column_types[i] = LALINFERENCE_REAL4_t; break;
100 case LAL_U4_TYPE_CODE:
101 column_types[i] = LALINFERENCE_UINT4_t; break;
102 case LAL_I4_TYPE_CODE:
103 column_types[i] = LALINFERENCE_INT4_t; break;
104 case LAL_Z_TYPE_CODE:
105 column_types[i] = LALINFERENCE_COMPLEX16_t; break;
106 case LAL_C_TYPE_CODE:
107 column_types[i] = LALINFERENCE_COMPLEX8_t; break;
108 default:
110 "%s: Unknown type code column_type=%i\n", __func__, column_type);
111 return(XLAL_FAILURE);
112
113 }
114 }
115
116 size_t nbytes = XLALH5DatasetQueryNBytes(dataset);
117 char *data = XLALMalloc(nbytes);
119 ret = XLALH5DatasetQueryData(data, dataset);
120 XLAL_CHECK_ABORT(ret == 0);
121
122 LALInferenceVariables **va = NULL;
123 UINT4 Nsamples = XLALH5DatasetQueryNPoints(dataset);
124
125 va = XLALCalloc(Nsamples, sizeof(LALInferenceVariables *));
126 for (size_t i = 0; i < Nsamples; i++)
127 va[i] = XLALCalloc(1, sizeof(LALInferenceVariables));
128
129 for (UINT4 i = 0; i < Nvary; i ++)
130 {
131 char pname[] = "FIELD_NNN_VARY";
132 snprintf(pname, sizeof(pname), "FIELD_%d_VARY", i);
133 INT4 value;
134 ret = XLALH5AttributeQueryScalarValue(&value, gdataset, pname);
135 XLAL_CHECK_ABORT(ret == 0);
136 vary[i] = value;
137 }
138
139 /* Read the group datasets in as arrays */
140 for (UINT4 i = 0; i < Nsamples; i++)
141 for (UINT4 j = 0; j < Nvary; j++)
143 va[i], column_names[j],
144 data + type_size * i + column_offsets[j],
145 column_types[j], vary[j]);
146 XLALFree(data);
147
148 for (size_t i = 0; i < Nvary; i++)
149 free(column_names[i]);
150
151 size_t Nfixed = XLALH5AttributeQueryN(gdataset);
152
153 for (size_t i = 0; i < Nfixed; i ++)
154 {
155 int len = XLALH5AttributeQueryName(NULL, 0, gdataset, i);
156 char pname[len + 1];
157 char value[16]; /* Big enough to hold largest supported LAL type */
158 XLALH5AttributeQueryName(pname, sizeof(pname), gdataset, i);
159
160 /* Skip the "vary" attribute as well as any attribute
161 * associated with the H5TB interface
162 * (https://www.hdfgroup.org/HDF5/doc/HL/H5TB_Spec.html). */
163 if (strcmp(pname, "CLASS") == 0 ||
164 strcmp(pname, "VERSION") == 0 ||
165 strcmp(pname, "TITLE") == 0 ||
166 strncmp(pname, "FIELD_", 6) == 0) continue;
167
168 LALTYPECODE laltype = XLALH5AttributeQueryScalarType(gdataset, pname);
169 LALInferenceVariableType lalinftype;
170
171 switch (laltype)
172 {
173 case LAL_D_TYPE_CODE:
174 lalinftype = LALINFERENCE_REAL8_t; break;
175 case LAL_S_TYPE_CODE:
176 lalinftype = LALINFERENCE_REAL4_t; break;
177 case LAL_U4_TYPE_CODE:
178 lalinftype = LALINFERENCE_UINT4_t; break;
179 case LAL_I4_TYPE_CODE:
180 lalinftype = LALINFERENCE_INT4_t; break;
181 case LAL_Z_TYPE_CODE:
182 lalinftype = LALINFERENCE_COMPLEX16_t; break;
183 case LAL_C_TYPE_CODE:
184 lalinftype = LALINFERENCE_COMPLEX8_t; break;
185 default:
187 "%s: Unknown type code laltype=%i\n", __func__, laltype);
188 continue;
189 }
190
191 XLALH5AttributeQueryScalarValue(&value, gdataset, pname);
192 for (UINT4 j = 0; j < Nsamples; j++)
194 va[j], pname, value, lalinftype, LALINFERENCE_PARAM_FIXED);
195 } /* End loop over fixed_params */
196
197
198 /* Construct the array of LALInferenceVariables */
199 *varsArray = va;
200 *N = Nsamples;
201 return(XLAL_SUCCESS);
202}
203
204
206 LALH5File *h5file, LALInferenceVariables *const *const varsArray, UINT4 N,
207 const char *TableName)
208{
209 /* Sanity check input */
210 if (!varsArray)
211 XLAL_ERROR(XLAL_EFAULT, "Received null varsArray pointer");
212 if (!h5file)
213 XLAL_ERROR(XLAL_EFAULT, "Received null h5file pointer");
214 if (N == 0)
215 return 0;
216
217 const char *column_names[varsArray[0]->dimension];
218 UINT4 Nvary = 0;
219 size_t type_size = 0;
220 size_t column_offsets[varsArray[0]->dimension];
221 size_t column_sizes[varsArray[0]->dimension];
222 LALTYPECODE column_types[varsArray[0]->dimension];
223 char *fixed_names[varsArray[0]->dimension];
224 int vary[varsArray[0]->dimension];
225 UINT4 Nfixed = 0;
226
227 /* Build a list of PARAM and FIELD elements */
228 for (LALInferenceVariableItem *varitem = varsArray[0]->head; varitem;
229 varitem = varitem->next)
230 {
231 switch(varitem->vary)
232 {
236 {
237 LALTYPECODE tp;
238 size_t sz;
239 switch (varitem->type)
240 {
242 tp = LAL_D_TYPE_CODE; sz = sizeof(REAL8); break;
244 tp = LAL_S_TYPE_CODE; sz = sizeof(REAL4); break;
246 tp = LAL_U4_TYPE_CODE; sz = sizeof(UINT4); break;
248 tp = LAL_I4_TYPE_CODE; sz = sizeof(INT4); break;
250 tp = LAL_C_TYPE_CODE; sz = sizeof(COMPLEX8); break;
252 tp = LAL_Z_TYPE_CODE; sz = sizeof(COMPLEX16); break;
253 default:
255 "LALInferenceType %i for parameter %s not "
256 "implemented for HDF5, ignoring\n",
257 varitem->type, varitem->name);
258 continue;
259 } /* End switch */
260 vary[Nvary] = varitem->vary;
261 column_types[Nvary] = tp;
262 column_sizes[Nvary] = sz;
263 column_offsets[Nvary] = type_size;
264 type_size += sz;
265 column_names[Nvary++] = varitem->name;
266 break;
267 }
269 fixed_names[Nfixed++] = varitem->name;
270 break;
271 default:
272 XLALPrintWarning("Unknown param vary type");
273 }
274 }
275
276 /* Gather together data in one big array */
277 char *data = XLALCalloc(N, type_size);
279 for (UINT4 i = 0; i < N; i++)
280 {
281 for (UINT4 j = 0; j < Nvary; j++)
282 {
283 void *var = LALInferenceGetVariable(varsArray[i], column_names[j]);
284 memcpy(
285 data + type_size * i + column_offsets[j], var, column_sizes[j]);
286 }
287 }
288
289 /* Create table */
290 LALH5Dataset *dataset = XLALH5TableAlloc(h5file, TableName, Nvary,
291 column_names, column_types, column_offsets, type_size);
292 XLAL_CHECK_ABORT(dataset);
293 int ret = XLALH5TableAppend(
294 dataset, column_offsets, column_sizes, N, type_size, data);
295 (void) ret;
296 XLAL_CHECK_ABORT(ret == 0);
297 XLALFree(data);
298
299 LALH5Generic gdataset = {.dset = dataset};
300 for (UINT4 i = 0; i < Nvary; i ++)
301 {
302 INT4 value = vary[i];
303 char pname[] = "FIELD_NNN_VARY";
304 snprintf(pname, sizeof(pname), "FIELD_%d_VARY", i);
306 gdataset, pname, &value, LAL_I4_TYPE_CODE);
307 XLAL_CHECK_ABORT(ret == 0);
308 }
309
310 /* Write attributes, if any */
311 for (UINT4 i = 0; i < Nfixed; i++)
313 gdataset, varsArray[0], fixed_names[i]);
314
315 XLALH5DatasetFree(dataset);
316 return XLAL_SUCCESS;
317}
318
319
321 LALH5Generic gdataset, LALInferenceVariables *vars, char *name)
322{
324 LALTYPECODE laltype;
325 switch(type)
326 {
328 laltype = LAL_U4_TYPE_CODE; break;
330 laltype = LAL_D_TYPE_CODE; break;
332 laltype = LAL_S_TYPE_CODE; break;
334 laltype = LAL_I4_TYPE_CODE; break;
335 default:
337 "LALInferenceType %i for parameter %s not "
338 "implemented for HDF5 attribute, ignoring", type, name);
339 return;
340 } /* End switch */
342 gdataset, name, LALInferenceGetVariable(vars,name), laltype);
343}
struct tagLALH5File LALH5File
struct tagLALH5Dataset LALH5Dataset
void(* lalAbortHook)(const char *,...)
int LALInferenceCheckNonEmptyFile(char *filename)
Returns 1 if a non-empty file exists, 0 otherwise.
int LALInferencePrintCheckpointFileInfo(char *filename)
Prints the size of the file.
int LALInferenceH5DatasetToVariablesArray(LALH5Dataset *dataset, LALInferenceVariables ***varsArray, UINT4 *N)
const char LALInferenceHDF5PosteriorSamplesDatasetName[]
int LALInferenceH5VariablesArrayToDataset(LALH5File *h5file, LALInferenceVariables *const *const varsArray, UINT4 N, const char *TableName)
LALH5File * LALInferenceH5CreateGroupStructure(LALH5File *h5file, const char *codename, const char *runID)
Create a HDF5 heirarchy in the given LALH5File reference /codename/runID/ Returns a LALH5File pointer...
static void LALInferenceH5VariableToAttribute(LALH5Generic gdataset, LALInferenceVariables *vars, char *name)
const char LALInferenceHDF5NestedSamplesDatasetName[]
int j
const LALVCSInfoList lalInferenceVCSInfoList
NULL-terminated list of VCS and build information for LALInference and its dependencies
const char *const name
const double sz
sigmaKerr data[0]
void XLALH5FileClose(LALH5File UNUSED *file)
LALTYPECODE XLALH5AttributeQueryScalarType(const LALH5Generic UNUSED object, const char UNUSED *key)
size_t XLALH5DatasetQueryNPoints(LALH5Dataset UNUSED *dset)
void XLALH5DatasetFree(LALH5Dataset UNUSED *dset)
int XLALH5AttributeQueryName(char UNUSED *name, size_t UNUSED size, const LALH5Generic UNUSED object, int UNUSED pos)
size_t XLALH5DatasetQueryNBytes(LALH5Dataset UNUSED *dset)
LALH5File * XLALH5GroupOpen(LALH5File UNUSED *file, const char UNUSED *name)
int XLALH5AttributeAddScalar(LALH5Generic UNUSED object, const char UNUSED *key, const void UNUSED *value, LALTYPECODE UNUSED dtype)
int XLALH5FileAddStringAttribute(LALH5File UNUSED *file, const char UNUSED *key, const char UNUSED *value)
int XLALH5AttributeQueryScalarValue(void UNUSED *value, const LALH5Generic UNUSED object, const char UNUSED *key)
int XLALH5DatasetQueryData(void UNUSED *data, LALH5Dataset UNUSED *dset)
size_t XLALH5AttributeQueryN(const LALH5Generic UNUSED object)
LALTYPECODE XLALH5TableQueryColumnType(const LALH5Dataset UNUSED *dset, int UNUSED pos)
size_t XLALH5TableQueryRowSize(const LALH5Dataset UNUSED *dset)
size_t XLALH5TableQueryNColumns(const LALH5Dataset UNUSED *dset)
int XLALH5TableQueryColumnName(char UNUSED *name, size_t UNUSED size, const LALH5Dataset UNUSED *dset, int UNUSED pos)
int XLALH5TableAppend(LALH5Dataset UNUSED *dset, const size_t UNUSED *offsets, const size_t UNUSED *colsz, size_t UNUSED nrows, size_t UNUSED rowsz, const void UNUSED *data)
size_t XLALH5TableQueryColumnOffset(const LALH5Dataset UNUSED *dset, int UNUSED pos)
LALH5Dataset * XLALH5TableAlloc(LALH5File UNUSED *file, const char UNUSED *name, size_t UNUSED ncols, const char UNUSED **cols, const LALTYPECODE UNUSED *types, const size_t UNUSED *offsets, size_t UNUSED rowsz)
LALTYPECODE
double complex COMPLEX16
double REAL8
uint32_t UINT4
float complex COMPLEX8
int32_t INT4
float REAL4
LAL_C_TYPE_CODE
LAL_Z_TYPE_CODE
LAL_S_TYPE_CODE
LAL_D_TYPE_CODE
LAL_I4_TYPE_CODE
LAL_U4_TYPE_CODE
LALInferenceVariableType LALInferenceGetVariableType(const LALInferenceVariables *vars, const char *name)
Get the LALInferenceVariableType of the parameter named name in vars.
Definition: LALInference.c:321
void LALInferenceAddVariable(LALInferenceVariables *vars, const char *name, const void *value, LALInferenceVariableType type, LALInferenceParamVaryType vary)
Add a variable named name to vars with initial value referenced by value.
Definition: LALInference.c:395
void * LALInferenceGetVariable(const LALInferenceVariables *vars, const char *name)
Return a pointer to the memory the variable vars is stored in specified by name User must cast this p...
Definition: LALInference.c:238
LALInferenceVariableType
An enumerated type for denoting the type of a variable.
Definition: LALInference.h:104
@ LALINFERENCE_PARAM_OUTPUT
A parameter that never changes, functions should respect this.
Definition: LALInference.h:131
@ LALINFERENCE_PARAM_FIXED
A parameter that is cyclic, such as an angle between 0 and 2pi.
Definition: LALInference.h:130
@ LALINFERENCE_PARAM_CIRCULAR
A parameter that simply has a maximum and a minimum.
Definition: LALInference.h:129
@ LALINFERENCE_PARAM_LINEAR
Definition: LALInference.h:128
@ LALINFERENCE_COMPLEX16_t
Definition: LALInference.h:111
@ LALINFERENCE_REAL8_t
Definition: LALInference.h:109
@ LALINFERENCE_REAL4_t
Definition: LALInference.h:108
@ LALINFERENCE_INT4_t
Definition: LALInference.h:105
@ LALINFERENCE_UINT4_t
Definition: LALInference.h:107
@ LALINFERENCE_COMPLEX8_t
Definition: LALInference.h:110
void * XLALMalloc(size_t n)
void * XLALCalloc(size_t m, size_t n)
void XLALFree(void *p)
char * XLALVCSInfoString(const LALVCSInfoList vcs_list, const int verbose, const char *prefix)
#define XLAL_ERROR(...)
#define XLAL_CHECK_ABORT(assertion)
int int XLALPrintWarning(const char *fmt,...) _LAL_GCC_PRINTF_FORMAT_(1
XLAL_SUCCESS
XLAL_EFAULT
XLAL_FAILURE
type
The LALInferenceVariableItem list node structure This should only be accessed using the accessor func...
Definition: LALInference.h:154
The LALInferenceVariables structure to contain a set of parameters Implemented as a linked list of LA...
Definition: LALInference.h:170
LALH5Dataset * dset