-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathfabber_capi.cc
More file actions
623 lines (569 loc) · 18 KB
/
fabber_capi.cc
File metadata and controls
623 lines (569 loc) · 18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
/* fabber_capi.cc - Pure C API for fabber
Used by language bindings, e.g. Python interface
Martin Craig
Copyright (C) 2016 University of Oxford */
/* CCOPYRIGHT */
#include "fabber_capi.h"
#include "easylog.h"
#include "fwdmodel.h"
#include "inference.h"
#include "rundata_array.h"
#include "setup.h"
#include "armawrap/newmat.h"
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>
#include <stdio.h>
#include <string.h>
using NEWMAT::Matrix;
using namespace std;
static int fabber_err(int code, const char *msg, char *err_buf)
{
// Error buffer is optional
if (!err_buf)
return code;
if (!msg)
msg = "NULL message";
strncpy(err_buf, msg, FABBER_ERR_MAXC - 1);
err_buf[FABBER_ERR_MAXC - 1] = '\0';
return code;
}
void *fabber_new(char *err_buf)
{
try
{
FabberSetup::SetupDefaults();
FabberRunDataArray *rundata = new FabberRunDataArray(false);
return rundata;
}
catch (...)
{
fabber_err(FABBER_ERR_FATAL, "Failed to allocate memory for run data", err_buf);
return NULL;
}
}
int fabber_load_models(void *fab, const char *libpath, char *err_buf)
{
if (!fab)
return fabber_err(FABBER_ERR_FATAL, "Rundata is NULL", err_buf);
if (!libpath)
return fabber_err(FABBER_ERR_FATAL, "Library path is NULL", err_buf);
try
{
FwdModel::LoadFromDynamicLibrary(libpath);
return 0;
}
catch (exception &e)
{
return fabber_err(FABBER_ERR_FATAL, e.what(), err_buf);
}
}
int fabber_set_extent(
void *fab, unsigned int nx, unsigned int ny, unsigned int nz, const int *mask, char *err_buf)
{
if (!fab)
return fabber_err(FABBER_ERR_FATAL, "Rundata is NULL", err_buf);
if (!mask)
return fabber_err(FABBER_ERR_FATAL, "Mask is NULL", err_buf);
if ((nx <= 0) || (ny <= 0) || (nz <= 0))
return fabber_err(FABBER_ERR_FATAL, "Dimensions must be >0", err_buf);
try
{
FabberRunDataArray *rundata = (FabberRunDataArray *)fab;
rundata->SetExtent(nx, ny, nz, mask);
return 0;
}
catch (NEWMAT::Exception &e)
{
return fabber_err(FABBER_ERR_FATAL, e.what(), err_buf);
}
catch (exception &e)
{
return fabber_err(FABBER_ERR_FATAL, e.what(), err_buf);
}
catch (...)
{
return fabber_err(FABBER_ERR_FATAL, "Error setting extent", err_buf);
}
}
int fabber_set_opt(void *fab, const char *key, const char *value, char *err_buf)
{
if (!fab)
return fabber_err(FABBER_ERR_FATAL, "Rundata is NULL", err_buf);
if (!key || !value)
return fabber_err(FABBER_ERR_FATAL, "Option key or value is NULL", err_buf);
try
{
FabberRunDataArray *rundata = (FabberRunDataArray *)fab;
rundata->Set(key, value);
return 0;
}
catch (exception &e)
{
return fabber_err(FABBER_ERR_FATAL, e.what(), err_buf);
}
}
int fabber_set_data(
void *fab, const char *name, unsigned int data_size, const float *data, char *err_buf)
{
if (!fab)
return fabber_err(FABBER_ERR_FATAL, "Rundata is NULL", err_buf);
if (!data)
return fabber_err(FABBER_ERR_FATAL, "Data buffer is NULL", err_buf);
if (!name)
return fabber_err(FABBER_ERR_FATAL, "Data name is NULL", err_buf);
if (data_size <= 0)
return fabber_err(FABBER_ERR_FATAL, "Data size must be >0", err_buf);
FabberRunDataArray *rundata = (FabberRunDataArray *)fab;
EasyLog log;
rundata->SetLogger(&log); // Ignored but avoids it going to stdout
try
{
rundata->SetVoxelDataArray(name, data_size, data);
}
catch (exception &e)
{
return fabber_err(FABBER_ERR_FATAL, e.what(), err_buf);
}
catch (...)
{
return fabber_err(FABBER_ERR_FATAL, "Error setting data", err_buf);
}
return 0;
}
int fabber_get_data_size(void *fab, const char *name, char *err_buf)
{
if (!fab)
return fabber_err(FABBER_ERR_FATAL, "Rundata is NULL", err_buf);
if (!name)
return fabber_err(FABBER_ERR_FATAL, "Data name is NULL", err_buf);
FabberRunDataArray *rundata = (FabberRunDataArray *)fab;
EasyLog log;
rundata->SetLogger(&log); // Ignored but avoids it going to stdout
try
{
return rundata->GetVoxelDataSize(name);
}
catch (DataNotFound &e)
{
return fabber_err(-1, e.what(), err_buf);
}
catch (...)
{
return fabber_err(FABBER_ERR_FATAL, "Error getting data", err_buf);
}
}
int fabber_get_data(void *fab, const char *name, float *data_buf, char *err_buf)
{
if (!fab)
return fabber_err(FABBER_ERR_FATAL, "Rundata is NULL", err_buf);
if (!name)
return fabber_err(FABBER_ERR_FATAL, "Data name is NULL", err_buf);
if (!data_buf)
return fabber_err(FABBER_ERR_FATAL, "Data name is NULL", err_buf);
FabberRunDataArray *rundata = (FabberRunDataArray *)fab;
EasyLog log;
rundata->SetLogger(&log); // Ignored but avoids it going to stdout
try
{
rundata->GetVoxelDataArray(name, data_buf);
return 0;
}
catch (DataNotFound &e)
{
return fabber_err(-1, e.what(), err_buf);
}
catch (...)
{
return fabber_err(FABBER_ERR_FATAL, "Error getting data", err_buf);
}
}
int fabber_dorun(void *fab, unsigned int log_bufsize, char *log_buf, char *err_buf,
void (*progress_cb)(int, int))
{
EasyLog log;
if (!fab)
return fabber_err(FABBER_ERR_FATAL, "Rundata is NULL", err_buf);
if (!log_buf)
return fabber_err(FABBER_ERR_FATAL, "Log buffer is NULL", err_buf);
if (!err_buf)
return fabber_err(FABBER_ERR_FATAL, "Error buffer is NULL", err_buf);
if (log_bufsize > 0 && !log_buf)
return fabber_err(FABBER_ERR_FATAL, "Log buffer is NULL", err_buf);
int ret = 0;
FabberRunDataArray *rundata = (FabberRunDataArray *)fab;
rundata->SetLogger(&log);
stringstream logstr;
try
{
log.StartLog(logstr);
if (progress_cb)
{
CallbackProgressCheck prog(progress_cb);
rundata->Run(&prog);
}
else
{
rundata->Run();
}
log.ReissueWarnings();
}
catch (const FabberError &e)
{
log.ReissueWarnings();
log.LogStream() << e.what() << endl;
ret = fabber_err(FABBER_ERR_FATAL, e.what(), err_buf);
}
catch (NEWMAT::Exception &e)
{
log.ReissueWarnings();
log.LogStream() << "NEWMAT exception caught in fabber:\n " << e.what() << endl;
ret = fabber_err(FABBER_ERR_NEWMAT, e.what(), err_buf);
}
catch (const exception &e)
{
log.ReissueWarnings();
log.LogStream() << "STL exception caught in fabber:\n " << e.what() << endl;
ret = fabber_err(FABBER_ERR_FATAL, e.what(), err_buf);
}
catch (...)
{
log.ReissueWarnings();
log.LogStream() << "Some other exception caught in fabber!" << endl;
ret = fabber_err(FABBER_ERR_FATAL, "Unrecognized exception", err_buf);
}
log.StopLog();
strncpy(log_buf, logstr.str().c_str(), log_bufsize - 1);
log_buf[log_bufsize - 1] = '\0';
return ret;
}
void fabber_destroy(void *fab)
{
if (fab)
{
// Get rid of registered models etc
FabberSetup::Destroy();
FabberRunDataArray *rundata = (FabberRunDataArray *)fab;
delete rundata;
}
}
int fabber_get_options(void *fab, const char *key, const char *value, unsigned int out_bufsize,
char *out_buf, char *err_buf)
{
if (!fab)
return fabber_err(FABBER_ERR_FATAL, "Rundata is NULL", err_buf);
if (!out_buf)
return fabber_err(FABBER_ERR_FATAL, "Output buffer is NULL", err_buf);
if (key && !value)
return fabber_err(FABBER_ERR_FATAL, "Key specified but no value", err_buf);
vector<OptionSpec> options;
string desc;
try
{
if (!key || (strlen(key) == 0))
{
FabberRunDataArray::GetOptions(options);
}
else if (strcmp(key, "model") == 0)
{
std::auto_ptr<FwdModel> model(FwdModel::NewFromName(value));
desc = model->GetDescription();
model->GetOptions(options);
}
else if (strcmp(key, "method") == 0)
{
std::auto_ptr<InferenceTechnique> method(InferenceTechnique::NewFromName(value));
desc = method->GetDescription();
method->GetOptions(options);
}
// Remove newlines from the description so we can guarantee that the first line is the
// description
desc.erase(std::remove(desc.begin(), desc.end(), '\n'), desc.end());
stringstream out;
out << desc << endl;
vector<OptionSpec>::iterator iter;
for (iter = options.begin(); iter != options.end(); ++iter)
{
out << iter->name << "\t" << iter->description << "\t" << iter->type << "\t"
<< iter->optional << "\t" << iter->def << endl;
}
string outstr = out.str();
if (outstr.size() >= out_bufsize)
{
return fabber_err(-1, "Buffer too small", err_buf);
}
strncpy(out_buf, outstr.c_str(), outstr.size());
out_buf[outstr.size()] = '\0';
return 0;
}
catch (exception &e)
{
return fabber_err(FABBER_ERR_FATAL, e.what(), err_buf);
}
catch (...)
{
return fabber_err(FABBER_ERR_FATAL, "Error in get_options", err_buf);
}
}
int fabber_get_models(void *fab, unsigned int out_bufsize, char *out_buf, char *err_buf)
{
if (!fab)
return fabber_err(FABBER_ERR_FATAL, "Rundata is NULL", err_buf);
if (!out_buf)
return fabber_err(FABBER_ERR_FATAL, "Output buffer is NULL", err_buf);
try
{
vector<string> known = FwdModel::GetKnown();
stringstream out;
vector<string>::iterator iter;
for (iter = known.begin(); iter != known.end(); ++iter)
{
out << *iter << endl;
}
string outstr = out.str();
if (outstr.size() >= out_bufsize)
{
return fabber_err(-1, "Buffer too small", err_buf);
}
strncpy(out_buf, outstr.c_str(), outstr.size());
out_buf[outstr.size()] = '\0';
return 0;
}
catch (exception &e)
{
return fabber_err(FABBER_ERR_FATAL, e.what(), err_buf);
}
catch (...)
{
return fabber_err(FABBER_ERR_FATAL, "Error in get_models", err_buf);
}
}
int fabber_get_methods(void *fab, unsigned int out_bufsize, char *out_buf, char *err_buf)
{
if (!fab)
return fabber_err(FABBER_ERR_FATAL, "Rundata is NULL", err_buf);
if (!out_buf)
return fabber_err(FABBER_ERR_FATAL, "Output buffer is NULL", err_buf);
try
{
vector<string> known = InferenceTechnique::GetKnown();
stringstream out;
vector<string>::iterator iter;
for (iter = known.begin(); iter != known.end(); ++iter)
{
out << *iter << endl;
}
string outstr = out.str();
if (outstr.size() >= out_bufsize)
{
return fabber_err(-1, "Buffer too small", err_buf);
}
strncpy(out_buf, outstr.c_str(), outstr.size());
out_buf[outstr.size()] = '\0';
return 0;
}
catch (exception &e)
{
return fabber_err(FABBER_ERR_FATAL, e.what(), err_buf);
}
catch (...)
{
return fabber_err(FABBER_ERR_FATAL, "Error in get_methods", err_buf);
}
}
int fabber_get_model_params(void *fab, unsigned int out_bufsize, char *out_buf, char *err_buf)
{
if (!fab)
return fabber_err(FABBER_ERR_FATAL, "Rundata is NULL", err_buf);
if (!out_buf)
return fabber_err(FABBER_ERR_FATAL, "Output buffer is NULL", err_buf);
try
{
FabberRunDataArray *rundata = (FabberRunDataArray *)fab;
std::auto_ptr<FwdModel> model(FwdModel::NewFromName(rundata->GetString("model")));
EasyLog log;
model->SetLogger(&log); // We ignore the log but this stops it going to cerr
model->Initialize(*rundata);
vector<Parameter> params;
model->GetParameters(*rundata, params);
stringstream out;
vector<Parameter>::iterator iter;
for (iter = params.begin(); iter != params.end(); ++iter)
{
out << iter->name << endl;
}
string outstr = out.str();
if (outstr.size() >= out_bufsize)
{
return fabber_err(-1, "Buffer too small", err_buf);
}
strncpy(out_buf, outstr.c_str(), outstr.size());
out_buf[outstr.size()] = '\0';
return 0;
}
catch (exception &e)
{
return fabber_err(FABBER_ERR_FATAL, e.what(), err_buf);
}
catch (...)
{
return fabber_err(FABBER_ERR_FATAL, "Error in get_model_params", err_buf);
}
}
int fabber_get_model_param_descs(void *fab, unsigned int out_bufsize, char *out_buf, char *err_buf)
{
if (!fab)
return fabber_err(FABBER_ERR_FATAL, "Rundata is NULL", err_buf);
if (!out_buf)
return fabber_err(FABBER_ERR_FATAL, "Output buffer is NULL", err_buf);
try
{
FabberRunDataArray *rundata = (FabberRunDataArray *)fab;
std::auto_ptr<FwdModel> model(FwdModel::NewFromName(rundata->GetString("model")));
EasyLog log;
model->SetLogger(&log); // We ignore the log but this stops it going to cerr
model->Initialize(*rundata);
vector<Parameter> params;
model->GetParameters(*rundata, params);
stringstream out;
vector<Parameter>::iterator iter;
for (iter = params.begin(); iter != params.end(); ++iter)
{
out << iter->name << " " << iter->desc;
if (iter->units != "")
{
out << " (units: " << iter->units << ")";
}
out << endl;
}
string outstr = out.str();
if (outstr.size() >= out_bufsize)
{
return fabber_err(-1, "Buffer too small", err_buf);
}
strncpy(out_buf, outstr.c_str(), outstr.size());
out_buf[outstr.size()] = '\0';
return 0;
}
catch (exception &e)
{
return fabber_err(FABBER_ERR_FATAL, e.what(), err_buf);
}
catch (...)
{
return fabber_err(FABBER_ERR_FATAL, "Error in get_model_params", err_buf);
}
}
int fabber_get_model_outputs(void *fab, unsigned int out_bufsize, char *out_buf, char *err_buf)
{
if (!fab)
return fabber_err(FABBER_ERR_FATAL, "Rundata is NULL", err_buf);
if (!out_buf)
return fabber_err(FABBER_ERR_FATAL, "Output buffer is NULL", err_buf);
try
{
FabberRunDataArray *rundata = (FabberRunDataArray *)fab;
std::auto_ptr<FwdModel> model(FwdModel::NewFromName(rundata->GetString("model")));
EasyLog log;
model->SetLogger(&log); // We ignore the log but this stops it going to cerr
model->Initialize(*rundata);
vector<string> outputs;
model->GetOutputs(outputs);
stringstream out;
vector<string>::iterator iter;
for (iter = outputs.begin(); iter != outputs.end(); ++iter)
{
if (*iter != "")
out << *iter << endl;
}
string outstr = out.str();
if (outstr.size() >= out_bufsize)
{
return fabber_err(-1, "Buffer too small", err_buf);
}
strncpy(out_buf, outstr.c_str(), outstr.size());
out_buf[outstr.size()] = '\0';
return 0;
}
catch (exception &e)
{
return fabber_err(FABBER_ERR_FATAL, e.what(), err_buf);
}
catch (...)
{
return fabber_err(FABBER_ERR_FATAL, "Error in fabber_get_model_outputs", err_buf);
}
}
int fabber_model_evaluate(void *fab, unsigned int n_params, float *params, unsigned int n_ts,
float *indata, float *output, char *err_buf)
{
return fabber_model_evaluate_output(fab, n_params, params, n_ts, indata, "", output, err_buf);
}
int fabber_model_evaluate_output(void *fab, unsigned int n_params, float *params, unsigned int n_ts,
float *indata, const char *output_name, float *output, char *err_buf)
{
if (!fab)
return fabber_err(FABBER_ERR_FATAL, "Rundata is NULL", err_buf);
if (!params)
return fabber_err(FABBER_ERR_FATAL, "Params array is NULL", err_buf);
if (!output_name)
return fabber_err(FABBER_ERR_FATAL, "Output name is NULL", err_buf);
if (!output)
return fabber_err(FABBER_ERR_FATAL, "Output buffer is NULL", err_buf);
EasyLog log;
int ret = FABBER_ERR_FATAL;
stringstream logstr;
try
{
FabberRunDataArray *rundata = (FabberRunDataArray *)fab;
std::auto_ptr<FwdModel> model(FwdModel::NewFromName(rundata->GetString("model")));
log.StartLog(logstr);
model->SetLogger(&log);
model->Initialize(*rundata);
NEWMAT::ColumnVector p_vec(n_params);
NEWMAT::ColumnVector o_vec(n_ts);
NEWMAT::ColumnVector data_vec(n_ts);
NEWMAT::ColumnVector coords(3);
for (unsigned int i = 0; i < n_params; i++)
{
p_vec(i + 1) = params[i];
}
for (unsigned int i=0; i < n_ts; i++)
{
if (indata)
data_vec(i + 1) = indata[i];
else
data_vec(i + 1) = 0;
}
coords(1) = 1;
coords(2) = 1;
coords(3) = 1;
model->PassData(1, data_vec, coords);
model->EvaluateModel(p_vec, o_vec, output_name);
for (unsigned int i = 0; i < n_ts; i++)
{
// Model may not return the same number of timepoints as passed in!
if ((int)i < o_vec.Nrows())
output[i] = o_vec(i + 1);
else
output[i] = 0;
}
log.ReissueWarnings();
ret = 0;
}
catch (NEWMAT::Exception &e)
{
ret = fabber_err(FABBER_ERR_FATAL, e.what(), err_buf);
}
catch (exception &e)
{
ret = fabber_err(FABBER_ERR_FATAL, e.what(), err_buf);
}
catch (...)
{
ret = fabber_err(FABBER_ERR_FATAL, "Error evaluating model", err_buf);
}
log.StopLog();
return ret;
}