-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathNeuroBayesExpert.cc
1788 lines (1616 loc) · 49 KB
/
NeuroBayesExpert.cc
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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <cstdlib>
#include <cmath>
#include "NeuroBayesExpert.hh"
#include <iostream>
#include <string.h>
#include <stdio.h>
// these are the Fortran NeuroBayes functions
// since Fortran passes all arguments by
// reference, each argument has to be <var type>*
extern "C"
{
double nb_f3_e_ (common_t **, float *);
void nb_wrap_f3_e_ (common_t **, float *, double *result);
void nb_wrap_invqincl_ (common_t **, float *, float *tabxs,
int *nllevel, float *tdeltaval, float *tdeltafrac,
float *result);
void nb_wrap_rndincl_e_ (common_t **, float *, int *debug, float *tabxs,
int *nllevel, float *tdeltaval, float *tdeltafrac,
float *result);
void nb_invquant2_ (common_t **, float *t, float *tabg, float *tabxs,
float *result);
void nb_wrap_quantile_ (common_t **, float *, float *tabg, float *tabxs,
int *nllevel, float *tdeltaval, double *netout,
int *dbg, float *result);
void nb_wrap_tmean_e_ (common_t **, float *TABG, float *TABXS, float *TRIM,
int *nllevel, float *tdeltaval, double *netout,
int *dbg, float *result);
void nb_wrap_eval_bspline_e_ (common_t **, float *x, int *k, float *t,
int *nkn, float *c, float *der, float *der2,
float *der3, float *result);
void nb_wrap_transgle_ (common_t **, float *, float *, float *result);
void nb_wrap_rndm2_ (common_t **, int *, int *dbg, float *result);
void nb_wrap_conddensity_ (common_t **, float *, int *dbg, float *tabxs,
float *tabd, float *TT, int *NKNOT, float *CP,
int *nllevel, float *tdeltaval,
float *tdeltafrac, double *netout,
float *result);
void nb_defpolynomials_e_ (common_t **, int *, float *, int *, int *,
float *, float *, float *);
void nb_defexpertise_e_ (common_t **, int *NLAYER, int *NODES, int *ITER,
int *IPRUNE, float *W, float *TABX, float *ITABY,
float *AE, float *DIAG, float *CHEBY, float *THETA,
float *EXPERTISE, int *NumPreproVar, int *PreProc,
int *AutoVarSelect, int *ISigSort, float *SigFrac,
int *PreproFlag, float *PreproPar, int *Debug,
float *RsfTable, int *nMapKey, float *MapKeyValue,
float *MapKeyTrans, float *TABX2, float *RsfTable2,
int *nlevel, int *node1, int *lshape, int *llog,
int *ifixorder, int *ifixshape, int *nvar,
int *n_marg_dim, int *marg_varid,
float *marg_coeff, float *marg_coeff0,
int *nllevel, float *tdeltaval, float *tdeltafrac,
float *TABX3, float *RsfTable3, float *a_boost,
float *inv_sigma_boostvar, float *diag_boost,
float *mean_boostvar, int *ipos_add_vars);
void nb_defft_e_ (common_t **, float *, float *, float *, float *, int *,
float *TT, int *NKNOT, float *CP, int *iversiont);
void nb_prepro2_e_ (common_t **, float *IN, int *NEVTS1, int *NVARM1,
float *TABX, float *ITABY, int *MXNODE, float *SCRATCH,
double *CTH, double *STH, float *A, float *DIAG,
float *CHEBY, float *THETA, int *IFIXORDER,
int *PREPROC, int *Node1, int *LSHAPE, int *NEWEX,
int *ISigSort, int *IPRUNE, int *NumPreproVar,
int *PreproFlag, float *PreproPar, int *Debug,
float *RsfTable, int *nmapkey, float *mapkeyvalue,
float *mapkeytrans, int *doHisto, float *OUTLEVEL,
float *T, float *TABX2, float *RsfTable2,
int *IFixShape, int *NLEVEL, int *nllevel,
float *tdeltaval, float *tdeltafrac,
bool * is_preboost);
void nb_chouth_ (common_t **, int *, float *, float *, float *, float *,
int *, float *, int *, int *dbg);
void nb_choutc_ (common_t **, float *, float *, float *,
int *, float *, int *, int *nllevel, float *tdeltafrac);
void nb_forwe_e_ (common_t **, int *, float *,
double (*f) (common_t **, float *), float *, int *,
int *);
void nb_splinef2_e_ (common_t **, int *, double *, float *, float *, int *,
int *, float *, int *, int *, int *);
void nb_splinef2_when_needed_e_ (common_t **, double *, float *, int *,
int *, float *, int *, int *, int *,
bool *);
void nb_perfplot2_ (common_t **, int *, double *, int *, float *, int *);
void nb_ftxdef_ (common_t **, float *, float *, float *, float *, int *,
int *nllevel, double *netout, int *dbg);
void nb_transbacktail_ (common_t **, float *, float *, float *, int *dbg);
void nb_transback_ (common_t **, float *, float *, float *,
int *nlLevel, double *netout, int *dbg);
void nb_converttocarray_e_ (common_t **, const char *ExName, int);
void nb_readexpertise_e_ (common_t **, const char *ExName, float *Ex, int);
void nb_derivative_ (common_t **, float *t, float *deri, float *TABXS,
float *TT, int *NKNOT, float *CP, int *nllevel,
float *tdeltaval, float *tdeltafrac);
void nb_wrap_ftmean_ (common_t **, float *TABG, float *TABXS,
double (*f) (float *), int *nllevel, float *tdeltaval,
double *netout, int *dbg, float *result);
void nb_wrap_tomarginal_e_ (common_t **, float *X, float *TABX,
int *nMapKey, float *MapKeyValue,
int *PreproFlag, float *PreproPar, int *Debug,
int *n_marg_dim, int *marg_varid,
float *marg_coeff, float *marg_coeff0,
float *result);
void nb_wrap_decomposepreproflag_ (common_t **, int *PreproFlag,
int *PreproFlag1, int *PreproFlag10,
int *PreproFlag100, int *Debug,
int *result);
void nb_prepare_boostdiag_ (bool * is_preboost, bool * is_boost,
bool * is_net_plus_diag, int *ifixshape,
int *preproc, int *iterations, int *ioffset,
int *iversiont);
void nb_prepro2_boost_e_ (common_t **, float *IN, int *inum, float *scratch,
bool * is_boost, bool * is_preboost,
int *NumPreproVar_save, int *NumPreproVar,
int *preproc_save, int *preproc, int *NODE1,
int *node1_save, int *IFIXSHAPE, int *nlevel,
int *nllevel, float *outlevel,
float *inv_sigma_boostvar, float *a_boost,
float *diag_boost, int *NVar, int *nodes,
float *mean_boostvar, int *ISigSort, int *nMapKey,
float *MapKeyValue, int *ipos_add_vars,
int *iversiont, int *final_diagfit);
void nb_diag_boostnet_e_ (common_t **, float *IN, int *NEVTS1, float *A,
int *ioffset, int *nlevel, int *nllevel,
int *lshape, float *tabx3, float *rsftable3,
int *PREPROC, int *preproc_save, bool * is_boost,
int *node1, int *node1_save, int *NumPreproVar,
int *NumPreproVar_save, int *ifixshape,
int *debug, bool * is_preboost, double *NETOUT,
int *NVar, int *nodes, int *iversiont,
float *cshape, int *final_diagfit);
void nb_regul_inputs_ (float *IN, int *NVar, int *inum);
// Functions called from NeuroBayes libs
void nbbook1_ (int *, const char *, int *, float *, float *, float *, int);
void nbfill_e_ (common_t **, int *, float *, float *, float *);
int nb_def_debugexpert_ (common_t **, const int &);
void nbpak_e_ (common_t **, int *, float *);
void nb_histo_init_ ();
void nb_histo_save_ (const char *, int);
int nb_check_netout_ (double *netout, int *nlevel);
void nb_interpolate_tabg_ (double *netout, int *nlevel, float *tabg);
}
/// simple wrapper that uses default arguments for rand function, etc
static common_t *
nb_init_common_simple (int debug_lvl, ec_t ** ec1)
{
return nb_init_common (debug_lvl, ec1, rand_double1,
NULL, 0);
}
double
Expert::NB_F3_e (float a)
{
double result = 0.;
nb_wrap_f3_e_ (&com, &a, &result);
nb_cpp_handle_error (com);
return result;
}
float
Expert::NB_TRANSGLE (float &a, float *aa)
{
float result = 0.;
nb_wrap_transgle_ (&com, &a, aa, &result);
return result;
}
float
Expert::NB_RNDM2 (int &a, int dbg)
{
return rand_double ();
}
float
Expert::NB_CONDDENSITY (float &arg)
{
float result = 0.;
NB_SPLINEF2_WHEN_NEEDED_e ();
if (nb_cpp_handle_error (com))
return result;
nb_wrap_conddensity_ (&com, &arg, &(com->log->debug_lvl), TABXS, TABD, TT,
&NKNOT, CP, &nlLevel, &tDeltaVal[0], &tDeltaFrac[0],
&NETOUT[0], &result);
return result;
}
float
Expert::NB_BSKFUN_e (float &t, float &Der)
{
float result = 0.;
int order = 4;
float Der2, Der3;
nb_wrap_eval_bspline_e_ (&com, &t, &order, TT, &NKNOT, CP, &Der, &Der2,
&Der3, &result);
nb_cpp_handle_error (com);
return result;
}
void
Expert::NB_DefPolynomials_e ()
{
float XLEVEL = 0;
nb_cpp_handle_error (com);
nb_defpolynomials_e_ (&com, &NVar, &XLEVEL, &(com->log->debug_lvl), &NLEVEL,
OUTLEVEL, XMEAN, &T[0][0]);
}
void
Expert::NBBOOK1 (int a, const char *b, int c, float d, float e, float f,
int i)
{
nbbook1_ (&a, b, &c, &d, &e, &f, i);
}
void
Expert::NBFILL_e (int a, float &b, float c, float &d)
{
nbfill_e_ (&com, &a, &b, &c, &d);
nb_cpp_handle_error (com);
}
void
Expert::NB_DEFEXPERTISE_e ()
{
nb_defexpertise_e_ (&com, &NLAYER, NODES, &ITER, &IPRUNE, &Weights[0][0][0],
&TABX[0][0], ITABY, AA, DIAG, CHEBY, THETA, EXPERTISE,
&NumPreproVar, &PREPROC, &AutoVarSelect,
ISigSort, &SigFrac, PreproFlag, &PreproPar[0][0],
&(com->log->debug_lvl), &RsfTable[0][0], nMapKey,
&MapKeyValue[0][0], &MapKeyTrans[0][0], &TABX2[0][0],
&RsfTable2[0][0], &NLEVEL, &NODE1, &LSHAPE, &LLOG,
&IFIXORDER, &IFIXSHAPE, &NVar, &nMargDim, &MargVarid[0],
&MargCoeff[0][0], &MargCoeff0, &nlLevel, &tDeltaVal[0],
&tDeltaFrac[0], &TABX3[0][0], &RsfTable3[0][0],
&a_boost[0], &inv_sigma_boostvar[0], &diag_boost[0],
&mean_boostvar[0], &ipos_add_vars[0]);
nb_cpp_handle_error (com);
}
void
Expert::NB_DEFFT_e ()
{
nb_defft_e_ (&com, &TABX[0][0], TABXS, ITABY, TABF, &LSHAPE, TT, &NKNOT, CP,
&iversiont);
nb_cpp_handle_error (com);
}
void
Expert::NBPAK_e (int a, float *b)
{
nbpak_e_ (&com, &a, b);
nb_cpp_handle_error (com);
}
void
Expert::NB_PREPRO2_e ()
{
int nVar = NVar - 1;
int nEv = 1;
int newExpert = 1;
int doHisto = 0;
nb_prepro2_e_ (&com, &IN[0][0], &nEv, &nVar, &TABX[0][0], ITABY, &MXNODE,
SCRATCH, &CTH[0][0], &STH[0][0], AA, DIAG, CHEBY, THETA,
&IFIXORDER, &PREPROC, &NODE1, &LSHAPE, &newExpert, ISigSort,
&IPRUNE, &NumPreproVar, PreproFlag, &PreproPar[0][0],
&(com->log->debug_lvl), &RsfTable[0][0], nMapKey,
&MapKeyValue[0][0], &MapKeyTrans[0][0], &doHisto, OUTLEVEL,
&T[0][0], &TABX2[0][0], &RsfTable2[0][0], &IFIXSHAPE,
&NLEVEL, &nlLevel, &tDeltaVal[0], &tDeltaFrac[0],
&is_preboost);
nb_cpp_handle_error (com);
}
void
Expert::NB_CHOUTH (int &a, float *b, float *c, float *d, float *e,
int &f, float *g, int &h, int dbg)
{
nb_chouth_ (&com, &a, b, c, d, e, &f, g, &h, &dbg);
}
void
Expert::NB_CHOUTC (float *b, float *c, float *d, int &e, float *f, int &g)
{
nb_choutc_ (&com, b, c, d, &e, f, &g, &nlLevel, &tDeltaFrac[0]);
}
void
Expert::NB_FORWE_e (int *a, float *b, double (*f) (common_t **, float *),
float *d, int &e, int &g)
{
nb_forwe_e_ (&com, a, b, f, d, &e, &g);
nb_cpp_handle_error (com);
}
void
Expert::NB_SPLINEF2_e (int &histoID)
{
nb_splinef2_e_ (&com, &histoID, NETOUT, TABG, TABD, &MODEGS, &NGS, ®GS,
&NLEVEL, &(com->log->debug_lvl), &nlLevel);
nb_cpp_handle_error (com);
}
void
Expert::NB_SPLINEF2_WHEN_NEEDED_e ()
{
nb_splinef2_when_needed_e_ (&com, NETOUT, TABD, &MODEGS, &NGS, ®GS,
&NLEVEL, &(com->log->debug_lvl), &nlLevel,
&spline_performed);
nb_cpp_handle_error (com);
}
void
Expert::NB_PERFPLOT2 (int histoID)
{
nb_perfplot2_ (&com, &histoID, NETOUT, &NLEVEL, OUTLEVEL, &nlLevel);
}
void
Expert::NB_FTXDEF (int &mmax)
{
nb_ftxdef_ (&com, TABXS, TABF, TABD, TABL, &mmax,
&nlLevel, &NETOUT[0], &(com->log->debug_lvl));
}
void
Expert::NB_FTXDEF_e (float *array, int nBins)
{
float t, s, der;
float gs = 0;
float step = TABXS[NB_NVALUE - 1] - TABXS[0];
float Der = 0;
for (int ii = 0; ii < nBins; ++ii)
{
t = TABXS[0] + step * (ii + 0.5) / (float) nBins;
s = NB_TRANSGLE (t, TABXS);
// computation of TABF[ii]
NB_BSKFUN_e (t, Der);
if (nb_cpp_handle_error (com))
return;
der = -Der / 2.;
NB_TRANSBACK (s, gs);
array[ii] = gs * der;
if (nb_get_debug (&com) >= 2)
{
ss << "t = " << t << "\ts = " << s << "\tgs = " << gs << "\tder = "
<< der << "\tarray[" << ii << "] = " << array[ii] << std::endl;
nb_cpp_log (ss, 2, &com);
}
}
}
void
Expert::NB_TRANSBACKTAIL (float &randomN, float &result)
{
nb_transbacktail_ (&com, &randomN, TABXS, &result, &(com->log->debug_lvl));
}
void
Expert::NB_TRANSBACK (float &eq, float &result)
{
nb_transback_ (&com, &eq, TABD, &result,
&nlLevel, &NETOUT[0], &(com->log->debug_lvl));
}
float
Expert::NB_QUANTILE (float value)
{
float result = 0.;
nb_wrap_quantile_ (&com, &value, TABG, TABXS,
&nlLevel, &tDeltaVal[0], &NETOUT[0],
&(com->log->debug_lvl), &result);
return result;
}
float
Expert::NB_BINCLASS ()
{
return (float) NETOUT[0];
}
float
Expert::NB_INVQUANT (float value)
{
float result = 0.;
nb_invquant2_ (&com, &value, &TABG[0], &TABXS[0], &result);
return result;
}
float
Expert::NB_TMEAN (float value)
{
float result = -999;
nb_wrap_tmean_e_ (&com, TABG, TABXS, &value,
&nlLevel, &tDeltaVal[0], &NETOUT[0],
&(com->log->debug_lvl), &result);
nb_cpp_handle_error (com);
return result;
}
float
Expert::NB_INVQINCL (float argument)
{
float result = 0;
nb_wrap_invqincl_ (&com, &argument, TABXS,
&nlLevel, &tDeltaVal[0], &tDeltaFrac[0], &result);
return result;
}
float
Expert::NB_RNDINCL (float argument)
{
float result = 0;
nb_wrap_rndincl_e_ (&com, &argument, &(com->log->debug_lvl), TABXS,
&nlLevel, &tDeltaVal[0], &tDeltaFrac[0], &result);
nb_cpp_handle_error (com);
return result;
}
void
Expert::NB_DERIVATIVE (float t, float *der)
{
nb_derivative_ (&com, &t, der, TABXS, TT, &NKNOT, CP, &nlLevel,
&tDeltaVal[0], &tDeltaFrac[0]);
}
void
Expert::NB_READEXPERTISEC_e (const char *ExName, float *Ex)
{
std::string exName = ExName;
nb_readexpertise_e_ (&com, exName.c_str (), Ex, exName.size ());
nb_cpp_handle_error (com);
}
float
Expert::NB_FTMEAN (double (*f) (float *))
{
float result = 0.;
nb_wrap_ftmean_ (&com, TABG, TABXS, f,
&nlLevel, &tDeltaVal[0], &NETOUT[0],
&(com->log->debug_lvl), &result);
return result;
}
float
Expert::NB_TOMARGINAL_e (float *X)
{
float result = 0.;
nb_wrap_tomarginal_e_ (&com, X, TABX[0], nMapKey, MapKeyValue[0],
PreproFlag, PreproPar[0], &(com->log->debug_lvl),
&nMargDim, MargVarid, MargCoeff[0], &MargCoeff0,
&result);
nb_cpp_handle_error (com);
return result;
}
void
Expert::nb_prepare_boostdiag ()
{
nb_prepare_boostdiag_ (&is_preboost, &is_boost, &is_net_plus_diag,
&IFIXSHAPE, &PREPROC, &iterations, &ioffset,
&iversiont);
}
void
Expert::nb_prepro2_boost_e ()
{
int n_events = 1;
nb_prepro2_boost_e_ (&com, &IN[0][0], &n_events, SCRATCH, &is_boost,
&is_preboost, &NumPreproVar_save, &NumPreproVar,
&preproc_save, &PREPROC, &NODE1, &node1_save,
&IFIXSHAPE, &NLEVEL, &nlLevel, OUTLEVEL,
&inv_sigma_boostvar[0], a_boost, diag_boost, &NVar,
NODES, &mean_boostvar[0], ISigSort, nMapKey,
MapKeyValue[0], &ipos_add_vars[0], &iversiont,
&final_diagfit);
nb_cpp_handle_error (com);
}
void
Expert::nb_diag_boostnet_e ()
{
int n_events = 1;
nb_diag_boostnet_e_ (&com, &IN[0][0], &n_events, &A[0][0], &ioffset,
&NLEVEL, &nlLevel, &LSHAPE, &TABX3[0][0],
&RsfTable3[0][0], &PREPROC, &preproc_save, &is_boost,
&NODE1, &node1_save, &NumPreproVar, &NumPreproVar_save,
&IFIXSHAPE, &(com->log->debug_lvl), &is_preboost,
&NETOUT[0], &NVar, NODES, &iversiont, &CSHAPE[0],
&final_diagfit);
nb_cpp_handle_error (com);
}
Expert::Expert ()
{
std::cout << "NeuroBayes(R): wrong constructor called, abort" << std::endl;
}
bool
Expert::check_num_inputs (int num_inputs)
{
if (num_inputs > NODE1 - 1)
{
if (nb_get_debug (&com) >= -2)
{
ss << "ERROR: num_inputs = " << num_inputs
<< ", which is bigger than NODE1 - 1 = " << NODE1 -
1 << std::endl;
nb_cpp_log (ss, -2, &com);
}
return false;
}
return true;
}
Expert::Expert (float *myExpertise, int myDebug, bool writeout,
ec_t ** ec, log_func_t log_f, void *log_enclosed,
delete_enclosed_func_t log_delete_enclosed)
{
com = nb_init_common_simple (myDebug, ec);
if (nb_get_debug (&com) > -3 && log_f)
{
nb_register_logging (&com, log_f, log_enclosed, log_delete_enclosed);
}
cg_of_nb_cols = NULL;
n_nb_cols = -1;
tellinputs_mode = 0;
filename = NULL;
if (nb_get_debug (&com) >= 1)
{
ss << "NeuroBayes Expert -- constructor" << std::endl;
nb_cpp_log (ss, 1, &com);
}
writeoutdata = writeout;
EXPERTISE = new float[NB_NEXPERTISE];
if (myExpertise == NULL)
{
ss << "NeuroBayes Expert -- Expertise array is NULL" << std::endl;
char *msg = stream_c_str (ss);
if (nb_get_debug (&com) >= -2)
nb_c_log (msg, -2, &com);
nb_set_error (&com, invalid_arg_exc, msg);
free (msg);
nb_cpp_handle_error (com);
return;
}
int expSize = (int) myExpertise[19];
if (nb_get_debug (&com) >= 1)
{
ss << "read size " << expSize << " and max is " << NB_NEXPERTISE <<
std::endl;
nb_cpp_log (ss, 1, &com);
}
if (expSize < 1)
{
ss << "Expert constructor error: the array passed might be corrupted" <<
std::endl;
char *msg = stream_c_str (ss);
if (nb_get_debug (&com) >= -2)
nb_c_log (msg, -2, &com);
nb_set_error (&com, invalid_arg_exc, msg);
free (msg);
nb_cpp_handle_error (com);
return;
}
// fill the expertise array by copying the content of the passed array
// (this is to avoid problem with the destructor, which deletes EXPERTISE
// -> if the EXPERTISE is not initialized with new we end up trying to delete
// an object we do not own)
for (int k = 0; k < NB_NEXPERTISE; ++k)
if (k < expSize)
EXPERTISE[k] = myExpertise[k];
else
EXPERTISE[k] = 0;
// now call initialisation routine
if (nb_get_debug (&com) >= 1)
{
ss << " initialise NeuroBayes(R) Expert " << std::endl;
nb_cpp_log (ss, 1, &com);
}
initialise_e ();
if (nb_cpp_handle_error (com))
return;
if (nb_get_debug (&com) >= 1)
{
ss << "NeuroBayes Expert - constructor ends" << std::endl;
nb_cpp_log (ss, 1, &com);
}
}
Expert::Expert (const std::string ExpertiseName, int myDebug,
bool writeout, ec_t ** ec, log_func_t log_f,
void *log_enclosed,
delete_enclosed_func_t log_delete_enclosed)
{
init_expert (ExpertiseName, myDebug, writeout, ec,
log_f, log_enclosed, log_delete_enclosed);
}
Expert::Expert (const char *ExpertiseName, int myDebug,
bool writeout, ec_t ** ec, log_func_t log_f,
void *log_enclosed,
delete_enclosed_func_t log_delete_enclosed)
{
std::string Name = "";
if (ExpertiseName)
{
Name = std::string (ExpertiseName);
}
init_expert (Name, myDebug, writeout, ec,
log_f, log_enclosed, log_delete_enclosed);
}
void
Expert::init_expert (const std::string ExpertiseName, int myDebug,
bool writeout, ec_t ** ec, log_func_t log_f,
void *log_enclosed,
delete_enclosed_func_t log_delete_enclosed)
{
com = nb_init_common_simple (myDebug, ec);
if (nb_get_debug (&com) > -3 && log_f)
{
nb_register_logging (&com, log_f, log_enclosed, log_delete_enclosed);
}
cg_of_nb_cols = NULL;
n_nb_cols = -1;
filename = NULL;
is_valid = true;
tellinputs_mode = 0;
// create arrays
EXPERTISE = new float[NB_NEXPERTISE];
if (nb_cpp_handle_error (com))
return;
if (nb_get_debug (&com) >= 0)
{
ss << "NeuroBayes Expert -- constructor" << std::endl;
nb_cpp_log (ss, 0, &com);
}
writeoutdata = writeout;
// reading in Expertise
if (nb_get_debug (&com) >= 1)
{
ss << "NeuroBayes Expert: reading in Expertise" << std::endl;
nb_cpp_log (ss, 1, &com);
}
if (nb_get_debug (&com) >= 1)
{
ss << " now read expertise " << std::endl;
ss << " size of expertise " << NB_NEXPERTISE << std::endl;
nb_cpp_log (ss, 1, &com);
} // if debug
NB_READEXPERTISEC_e (ExpertiseName.c_str (), EXPERTISE);
if (nb_cpp_handle_error (com))
return;
// now call initialisation routine
if (nb_get_debug (&com) >= 1)
{
ss << " initialise NeuroBayes(R) Expert " << std::endl;
nb_cpp_log (ss, 1, &com);
}
initialise_e ();
if (nb_cpp_handle_error (com))
return;
if (nb_get_debug (&com) >= 1)
{
ss << "NeuroBayes Expert - constructor ends" << std::endl;
nb_cpp_log (ss, 1, &com);
}
}
void
Expert::initialise_e ()
{
if (nb_get_debug (&com) >= 1)
{
ss << "NeuroBayes Expert - initialise begins" << std::endl;
nb_cpp_log (ss, 1, &com);
}
InfoAndLicence ();
LDEFSPLINE = 0;
memset (XSAVE, 0, NB_MAXNODE * sizeof (float));
memset (&T[0][0], 0, NB_MAXNODE * (NB_LEVMAX + 1) * sizeof (float));
NumPreproVar = 0; //variable selection, 0 if not used
// there is only one Expertise per class instance of
// NeuroBayes Expert, i.e. the Expertise is always new
// in the constructor
NewEx = 1; //assume new expertise
NEWEVT = 0;
eventCounter = 0;
MODEGS = 0;
NGS = 0;
REGGS = 0.;
memset (&TABX[0][0], 0, NB_MAXNODE * (NB_NVALUE) * sizeof (float));
NLAYER = NB_MAXLAYER;
MXNODE = NB_MAXNODE;
// need this call here also if writeout is false
// because a histogram is booked inside NB_DEFFT
nb_histo_init_ ();
// decode expertise
if (nb_get_debug (&com) >= 2)
{
ss << "NeuroBayes Expert: now decode Expertise" << std::endl;
nb_cpp_log (ss, 2, &com);
}
NB_DEFEXPERTISE_e ();
if (nb_cpp_handle_error (com))
return;
iversiont = int (EXPERTISE[0]) + 20000000;
iterations = int (EXPERTISE[14]);
nb_prepare_boostdiag (); // check version and set flags
if (nb_get_debug (&com) >= 2)
{
PrintArrays ("After DefExpertise");
ss << "NeuroBayes Expert (C++): initialise orthogonal polynomials" <<
std::endl;
nb_cpp_log (ss, -2, &com);
}
NB_DefPolynomials_e ();
if (nb_cpp_handle_error (com))
return;
if (nb_get_debug (&com) >= 2)
{
ss << "NeuroBayes Expert (C++): orth. pol. initialised " << std::endl;
nb_cpp_log (ss, 2, &com);
}
if (nb_get_debug (&com) >= 1)
{
for (int ii = 0; ii < NB_MAXNODE; ii++)
{
ss << "Expert (C++): T(" << ii << ",ii):" << std::endl;
for (int j = 0; j < NB_LEVMAX + 1; j++)
ss << T[j][ii] << " ";
ss << std::endl;
}
nb_cpp_log (ss, 1, &com);
}
// adjustments for target delta functions
if (nlLevel > 0)
{
SigFrac = tDeltaFrac[nlLevel - 1];
for (int k = NLEVEL - 1; k >= 0; --k)
OUTLEVEL[nlLevel + k] = OUTLEVEL[k];
for (int k = 0; k < nlLevel - 1; ++k)
OUTLEVEL[k] = 0.5 * (tDeltaVal[k] + tDeltaVal[k + 1]);
OUTLEVEL[nlLevel - 1] = 0.5 * tDeltaVal[nlLevel - 1];
}
// define levels (based on number of nodes in output layer)
// for classification
if (IFIXSHAPE == 1)
{
switch (LSHAPE)
{
case 0:
XSHAPE[0] = -log (1.0 / SigFrac - 1.0);
break;
case 1:
for (int k = 0; k < NLEVEL; ++k)
XSHAPE[k] = log (1. / OUTLEVEL[k] - 1.0);
SigFrac = 1.;
break;
case 2:
SigFrac = tDeltaFrac[nlLevel - 1];
for (int k = 0; k < nlLevel; ++k)
XSHAPE[k] = -log (1. / tDeltaFrac[k] - 1.0);
for (int k = nlLevel; k < NLEVEL + nlLevel; ++k)
XSHAPE[k] = -log (1.0 / (SigFrac * (1 - OUTLEVEL[k])) - 1.0);
break;
}
if (nb_get_debug (&com) >= 1)
{
ss << "Expert: Define Levels:" << std::endl;
for (int i = 0; i < NLEVEL + nlLevel; ++i)
ss << "SigFrac = " << SigFrac << "\tOUTLEVEL[" << i << "] = " <<
OUTLEVEL[i] << "\tXSHAPE[" << i << "] = " << XSHAPE[i] <<
"\tF3(XSHAPE[" << i << "]) = " << NB_F3_e (XSHAPE[i]) << std::
endl;
if (nb_cpp_handle_error (com))
return;
nb_cpp_log (ss, 1, &com);
}
}
// new expertise, calc everthing
if (nb_get_debug (&com) >= 2)
{
ss << "NeuroBayes Expert: calc. incl. density" << std::endl;
nb_cpp_log (ss, 2, &com);
}
if (LSHAPE != 0)
{
NB_DEFFT_e ();
if (nb_cpp_handle_error (com))
return;
}
if (nb_get_debug (&com) >= 2)
{
ss << "NeuroBayes Expert: incl. density calculated" << std::endl;
nb_cpp_log (ss, 2, &com);
}
// initialize and fill the inverse array of ISigSort
for (int i = 0; i < NB_MAXNODE - 1; ++i)
inverseISigSort[i] = 0;
if (NumPreproVar > 0)
{
for (int i = 0; i < NODE1 - 1; ++i)
inverseISigSort[ISigSort[NODE1 - 2 - i] - 2] = i + 1;
}
else
{
// case in which there is no significance cut
for (int i = 0; i < NB_MAXNODE - 1; ++i)
inverseISigSort[i] = i + 1;
}
if (nb_get_debug (&com) >= 1)
{
for (int i = 0; i < NODE1 - 1; ++i)
{
if (inverseISigSort[i] != 0)
{
ss << "inverseISigSort[" << i << "] = " << inverseISigSort[i]
<< "\tprepro = " << PreproFlag[inverseISigSort[i]]
<< "\tTABX extremes = " << TABX[inverseISigSort[i]][0]
<< "\t" << TABX[inverseISigSort[i]][NB_NVALUE -
1] << std::endl;
nb_cpp_log (ss, 1, &com);
}
}
}
if (writeoutdata)
{
NBBOOK1 (498, "TABX ", 101, 0., 101., 0., 8);
NBPAK_e (498, &TABX[0][0]);
if (nb_cpp_handle_error (com))
return;
NBBOOK1 (499, "TABF ", 100, TABX[0][0], TABX[0][100], 0., 5);
NBPAK_e (499, TABF);
if (nb_cpp_handle_error (com))
return;
NBBOOK1 (987, "E likelihood sum ", 100, TABX[0][0], TABX[0][100], 0.0,
18);
NBBOOK1 (986, "s likelihood sum ", 101, 0., 1.00001, 0.0, 18);
//book histogrammes for purity/efficiency plots
for (int jj = 0; jj < NLEVEL + nlLevel; jj++)
{
NBBOOK1 (100 + jj + 1, "F(NETOUT) FOR BACKGROUND ", 100, -1., 1.,
0., 26);
NBBOOK1 (200 + jj + 1, "F(NETOUT) FOR SIGNAL ", 100, -1., 1., 0.,
22);
}
}
if (nb_get_debug (&com) >= 1)
{
ss << "-------------------------------------" << std::endl;
ss << "Expert: actual network topology:" << std::endl;
ss << "NODES[1] = " << NODES[0] << std::endl;
ss << "NODES[2] = " << NODES[1] << std::endl;
ss << "NODES[3] = " << NODES[2] << std::endl;
ss << "nominal node1= " << NODE1 << std::endl;
ss << "-------------------------------------" << std::endl;
ss << "NeuroBayes Expert: initialise ends" << std::endl;
nb_cpp_log (ss, 1, &com);
}
}
Expert::~Expert ()
{
// save histograms
if (writeoutdata)
{
if (HistoFileName.size () < 1)
HistoFileName = "expertHistos.hist";
nb_histo_save_ (HistoFileName.c_str (), (int) HistoFileName.size ());
}
delete[]EXPERTISE;
nb_delete_common (&com);
}
void
Expert::Print ()
{
if (nb_get_debug (&com) >= -1)
{
ss << "Greetings" << std::endl;
nb_cpp_log (ss, -1, &com);
}
}
float
Expert::nb_expert (ACTION key, double *input, float ARGUMENT)
{
float X[NB_MAXNODE];
memset (X, 0, NB_MAXNODE * sizeof (float)); //reset
for (int i = 0; i < NODE1 - 1; ++i)
X[i] = (float) input[i];
return nb_expert (key, X, ARGUMENT);
}
void
Expert::writeErrorMessage (int varIndex, float limit,
float current, int status, int prepro)
{
char help[100] = "";
sprintf (errorMessage,
"****************************************************\n");
strcat (errorMessage,
"** NeuroBayes Expert Warning (only last one shown)**\n");
strcat (errorMessage,
"****************************************************\n");
sprintf (help, "Variable: %3i with flag %i value", varIndex, prepro);
strcat (errorMessage, help);
if (status == 0)
strcat (errorMessage, " too low\nLowest");
else if (status == 1)
strcat (errorMessage, " too high\nHighest");
else
strcat (errorMessage, " unknown");
sprintf (help, " known input: %5.4f\n", limit);
strcat (errorMessage, help);
sprintf (help, "Current input : %5.4f\n", current);
strcat (errorMessage, help);
strcat (errorMessage,
"****************************************************\n");
}
void
Expert::checkInputRange_e (float *X)
{
// loop to check the values passed (if INF or NAN issue an error message and abort)
for (int ii = 0; ii < NODE1 - 1; ++ii)
{
if (isnan (X[ii]))
{
ss << ii << "\t" << NODE1 << std::endl;
ss << " Expert::nb_expert ERROR: NAN passed as input" << std::endl;
for (int kk = 0; kk < NODE1; kk++)
{
ss << " input variable " << kk << " = " << X[kk] << std::endl;
}
ss << "\n Please, correct the error" << std::endl;
char *msg = stream_c_str (ss);