helma / lazar-core

DEPRECATED (use the pure ruby implementation in conjunction with libfminer in opentox-algorithm and opentox-model), C++ implementation of various lazar algorithms

lazar-core / model.h
100644 624 lines (479 sloc) 19.381 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
624
/* Copyright (C) 2005 Christoph Helma <helma@in-silico.de>
 
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
 
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
 
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 
*/
#include <math.h>
#include "io.h"
#include "stats.h"
#include <sstream>
#include <iostream>
#include <iomanip>
#include "openbabel/obconversion.h"
#include <stdlib.h>
#include <stdio.h>
 
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
#include <gsl/gsl_blas.h>
#include <gsl/gsl_statistics.h>
#include <gsl/gsl_vector.h>
#include <gsl/gsl_matrix.h>
 
#include "utils.h"
#include "feature.h"
#include "lazmol.h"
 
#include <time.h>
 
#ifndef MODEL_H
#define MODEL_H
 
using namespace std;
using namespace OpenBabel;
 
float gauss(float sim, float sigma = 0.3);
 
template <typename MolType, typename FeatureType, typename ActivityType>
class MetaModel {
typedef vector<Feature<FeatureType> *> FeatVect;
typedef FeatMol<MolType,FeatureType,ActivityType> * MolRef;
typedef vector<FeatMol < MolType, ClassFeat, bool > * > ClassMolVect;
typedef vector<FeatMol < MolType, RegrFeat, float > * > RegrMolVect;
typedef vector<FeatMol<MolType,FeatureType,ActivityType>*> MolVect;
 
public:
Out* out;
 
    public:
        //MetaModel(Out* out): out(out) {};
void set_output(Out * newout) { out = newout; };
        virtual ~MetaModel() {};
        virtual void calculate_prediction(FeatMol < MolType, ClassFeat, bool >* test, ClassMolVect * neighbors, string act){};
        virtual void calculate_prediction(FeatMol < MolType, RegrFeat, float >* test, RegrMolVect * neighbors, string act){};
};
 
template <typename MolType, typename FeatureType, typename ActivityType>
class Model: public MetaModel<MolType,FeatureType,ActivityType> {
typedef vector<Feature<FeatureType> *> FeatVect;
typedef FeatMol<MolType,FeatureType,ActivityType> * MolRef;
typedef vector<FeatMol < MolType, ClassFeat, bool > * > ClassMolVect;
typedef vector<FeatMol < MolType, RegrFeat, float > * > RegrMolVect;
typedef vector<FeatMol<MolType,FeatureType,ActivityType>*> MolVect;
 
    private:
        vector<string> unknown_features;
 
    public:
        Model(Out* out) { this->set_output(out); };
        virtual ~Model() {};
        virtual void calculate_prediction(FeatMol<MolType,ClassFeat,bool>* t, ClassMolVect* neighbors, string act);
        virtual void calculate_prediction(FeatMol<MolType,RegrFeat,float>* test, RegrMolVect* neighbors, string act);
};
 
 
template <typename MolType, typename FeatureType, typename ActivityType>
class KernelModel: public MetaModel<MolType,FeatureType,ActivityType> {
typedef vector<Feature<FeatureType> *> FeatVect;
typedef FeatMol<MolType,FeatureType,ActivityType> * MolRef;
typedef vector<FeatMol < MolType, ClassFeat, bool > * > ClassMolVect;
typedef vector<FeatMol < MolType, RegrFeat, float > * > RegrMolVect;
typedef vector<FeatMol<MolType,FeatureType,ActivityType>*> MolVect;
 
    private:
        vector<string> unknown_features;
        ActivityType prediction;
        //Out* out;
 
    public:
        KernelModel(Out* out) { this->set_output(out); };
        virtual ~KernelModel() {};
        virtual void calculate_prediction(FeatMol<MolType,ClassFeat,bool>* t, ClassMolVect * neighbors, string act);
        virtual void calculate_prediction(FeatMol<MolType,RegrFeat,float>* test, RegrMolVect * neighbors, string act);
};
 
// Implementations
template <typename MolType, typename FeatureType, typename ActivityType>
void Model<MolType,FeatureType,ActivityType>::calculate_prediction(FeatMol<MolType,ClassFeat,bool>* test, ClassMolVect * neighbors, string act) {
    vector<Feature<ClassFeat> *> features = test->get_features();
    this->unknown_features = test->get_unknown();
 
float prediction = 0;
float sim;
float known_fraction = float(features.size()) / float(features.size() + unknown_features.size());
 
typename vector<FeatMol<MolType,ClassFeat,bool> *>::iterator cur_n;
vector<bool> activity;
vector<bool>::iterator a;
 
if (neighbors->size()>1) {
 
for (cur_n = neighbors->begin(); cur_n != neighbors->end(); cur_n++) {
 
// prediction weighted by fraction of known structure
sim = (*cur_n)->get_similarity()*known_fraction;
sim = gauss(sim);
vector<bool> activity = (*cur_n)->get_act(act);
 
for (a = activity.begin(); a != activity.end(); a++) {
 
if (*a)
prediction = prediction + sim;
else
prediction = prediction - sim;
 
}
}
 
prediction = prediction/neighbors->size();
*(this->out) << "known_fraction: " << known_fraction << "\n";
this->out->print();
 
if (prediction>0) {
*(this->out) << "prediction: 1\n";
*(this->out) << "confidence: " << prediction << "\n";
this->out->print();
}
else {
*(this->out) << "prediction: 0\n";
*(this->out) << "confidence: " << prediction << "\n";
this->out->print();
}
}
 
else {
*(this->out) << "prediction: \n";
*(this->out) << "confidence: \n";
this->out->print();
}
};
 
template <typename MolType, typename FeatureType, typename ActivityType>
void Model<MolType,FeatureType,ActivityType>::calculate_prediction(FeatMol<MolType,RegrFeat,float>* test, RegrMolVect * neighbors, string act) {
// data storage
    vector<Feature<RegrFeat>*> lrf;
vector<Feature<RegrFeat>*>* lr_features = &lrf;
multimap<float, RegrFeat*> msf;
multimap<float,FeatMol<MolType,RegrFeat,float>*> ssn;
multimap<float,FeatMol<MolType,RegrFeat,float>*>* sim_sorted_neighbors = &ssn;
 
// iterators
typename RegrMolVect::iterator cur_n;
typename FeatVect::iterator cur_feat;
typename multimap<float, RegrFeat*>::iterator sig_feat;
 
// linear regression
gsl_vector* x;
gsl_matrix* X;
gsl_matrix* cov;
gsl_vector* c;
gsl_multifit_linear_workspace* p_workspace;
gsl_vector* y;
gsl_vector* w;
 
// primitive data types
unsigned int no_r, no_c;
float sim;
double chisq, y_est, y_err;
 
if (neighbors->size()) {
 
// sort neighbors by similarity
sim_sorted_neighbors->clear();
for (cur_n = neighbors->begin(); cur_n != neighbors->end(); cur_n++) {
sim = (*cur_n)->get_similarity();
sim_sorted_neighbors->insert(pair<float, FeatMol<MolType,RegrFeat,float>*>(sim,*cur_n));
}
test->extract_neighbors(neighbors, sim_sorted_neighbors);
 
// calculate confidence
float confidence = test->calculate_confidence(neighbors, act);
 
// unite neighbor's with test's features
test->unite_features(lr_features, neighbors);
 
// determine matrix size
no_c = (unsigned int) neighbors->size();
 
if (lr_features->size() < no_c) {
no_c = lr_features->size();
}
 
no_r = neighbors->size();
 
if ((no_r >= no_c) && (confidence < 0.995) && (confidence > 0.0)) {
 
x = gsl_vector_calloc(1);
X = gsl_matrix_calloc(no_r,1);
 
gsl_vector** x_p = &x;
gsl_matrix** X_p = &X;
 
float qdist = 0.0;
float med_ndist = 0.0;
float std_ndist = 0.0;
float max_ndist = 0.0;
 
//bool tset_interpolates = build_descriptors_pca(lr_features, neighbors, no_c-1, X_p, x_p, act, &qdist, &med_ndist, &std_ndist, &max_ndist);
test->build_descriptors_pca(lr_features, neighbors, no_c-1, X_p, x_p, act, &qdist, &med_ndist, &std_ndist, &max_ndist);
 
// apply mahalanobis correction for confidence with weight 0.25
float norm_med_ndist = 0.0;
if (max_ndist > 0.0) norm_med_ndist = med_ndist / max_ndist;
float norm_std_ndist = 0.0;
if (max_ndist > 0.0) norm_std_ndist = std_ndist / max_ndist;
 
// 1. 0.5
float x = (norm_med_ndist + 0.5 * norm_std_ndist) / 1.5;
 
if (x == 0.0) x = 1.0;
// 1. 0.25
//confidence = (confidence + 0.25*x) / 1.25;
 
if (confidence < 0.0) confidence = 0.0;
if (confidence > 1.0) confidence = 1.0;
 
y = gsl_vector_calloc((*X_p)->size1);
w = gsl_vector_calloc((*X_p)->size1);
 
unsigned int rc = 1;
for (cur_n = neighbors->begin(); cur_n != neighbors->end(); cur_n++) {
test->set_y_w((*cur_n), y, w, act, rc);
rc++;
}
 
cov = gsl_matrix_calloc((*X_p)->size2, (*X_p)->size2);
c = gsl_vector_calloc((*X_p)->size2);
p_workspace = gsl_multifit_linear_alloc((*X_p)->size1, (*X_p)->size2);
 
// do regression
if ((*X_p)->size1 && (*X_p)->size2) {
y_est = 0.0; y_err = 0.0; chisq = 0.0;
 
// learn model and predict activity
 
for (unsigned int i=0; i<(*X_p)->size1; i++) {
for (unsigned int j=0; j<(*X_p)->size2; j++) {
}
}
for (unsigned int j=0; j<(*x_p)->size; j++) {
}
 
gsl_multifit_wlinear((*X_p), w, y, c, cov, &chisq, p_workspace);
gsl_multifit_linear_est((*x_p), c, cov, &y_est, &y_err);
 
// free learning matrix
gsl_matrix_free(*X_p);
 
// output from here
int df = no_r-1;
if (df>0) chisq = chisq / df;
 
*(this->out) << "med_ndist: " << norm_med_ndist << "\n";
                *(this->out) << "std_ndist: " << norm_std_ndist << "\n";
                *(this->out) << "prediction: " << y_est << "\n";
*(this->out) << "confidence: " << confidence << "\n";
}
 
else { // end if vector set
                *(this->out) << "prediction: \n";
                *(this->out) << "confidence: \n";
}
 
gsl_vector_free(y);
gsl_vector_free(*x_p);
gsl_multifit_linear_free(p_workspace);
gsl_vector_free(c);
gsl_matrix_free(cov);
 
} // end if at least one significant feature
 
else {
            *(this->out) << "prediction: \n";
            *(this->out) << "confidence: \n";
}
 
 
 
} // end if at least one neighbor
 
else {
        *(this->out) << "prediction: \n";
        *(this->out) << "confidence: \n";
}
 
};
 
template <typename MolType, typename FeatureType, typename ActivityType>
void KernelModel<MolType,FeatureType,ActivityType>::calculate_prediction(FeatMol<MolType,ClassFeat,bool>* test, ClassMolVect * neighbors, string act) {
    vector<Feature<ClassFeat> *> features = test->get_features();
    this->unknown_features = test->get_unknown();
 
float confidence = 0.0;
float sim = 0.0;
   
float known_fraction = float(features.size()) / float(features.size() + unknown_features.size());
 
typename vector<FeatMol<MolType,ClassFeat,bool> *>::iterator cur_n;
vector<bool> activity;
vector<bool>::iterator a;
 
if (neighbors->size()>1) {
 
// calculate confidence
for (cur_n = neighbors->begin(); cur_n != neighbors->end(); cur_n++) {
sim = (*cur_n)->get_similarity()*known_fraction;
sim = gauss(sim);
activity = (*cur_n)->get_act(act);
for (a = activity.begin(); a != activity.end(); a++) {
if (*a) confidence = confidence + sim;
else confidence = confidence - sim;
}
}
confidence = confidence/neighbors->size();
 
 
// calculate activities
gsl_vector* y = gsl_vector_calloc(neighbors->size());
SEXP yR; PROTECT(yR = allocVector(INTSXP, neighbors->size()));
unsigned int rc = 1;
 
for (cur_n = neighbors->begin(); cur_n != neighbors->end(); cur_n++) {
activity = (*cur_n)->get_act(act);
 
for (a = activity.begin(); a != activity.end(); a++) {
gsl_vector_set(y, (rc-1), (*a)); // set gsl vector
INTEGER(yR)[rc-1] = (*a);
}
 
rc++;
}
PROTECT(yR = R_exec("as.factor",yR));
//R_exec("print", yR);
 
gsl_vector* y_bar = gsl_vector_calloc(y->size);
for (unsigned int i=0; i<y->size; i++) gsl_vector_set(y_bar,i,(gsl_vector_get(y,0)-gsl_vector_get(y,i)));
 
if (gsl_vector_isnull(y_bar)) {
            if (gsl_vector_get(y,0) == 0) *(this->out) << "prediction: 0\n";
            else *(this->out) << "prediction: 1\n";
*(this->out) << "confidence: " << confidence<< "\n";
*(this->out) << "known_fraction: " << known_fraction <<"\n";
this->out->print();
            UNPROTECT(2);
}
 
else {
// calculate gram matrix
gsl_matrix* gram_matrix = gsl_matrix_calloc(neighbors->size(), neighbors->size());
test->calculate_gram_matrix(neighbors, gram_matrix, act);
 
// convert gram matrix to R kernelMatrix using R util function
SEXP mr;
SEXP* gramR = &mr;
matrix_gsl2R(gramR, gram_matrix);
PROTECT((*gramR) = R_exec("as.kernelMatrix", (*gramR)));
 
// learn kernel model
SEXP e;
SEXP fun;
PROTECT(fun = Rf_findFun(Rf_install("ksvm"), R_GlobalEnv));
if(fun == R_NilValue) {
fprintf(stderr, "No definition for function.\n");
UNPROTECT(1);
exit(1);
}
PROTECT(e = allocVector(LANGSXP,6));
SETCAR(e, fun);
SETCAR(CDR(e), (*gramR));
SETCAR(CDR(CDR(e)), yR);
SETCAR(CDR(CDR(CDR(e))), mkString("matrix"));
SET_TAG(CDR(CDR(CDR(e))), install("kernel"));
SETCAR(CDR(CDR(CDR(CDR(e)))), mkString("C-svc"));
SET_TAG(CDR(CDR(CDR(CDR(e)))), install("type"));
            SETCAR(CDR(CDR(CDR(CDR(CDR(e))))), mkString("1.0"));
            SET_TAG(CDR(CDR(CDR(CDR(CDR(e))))), install("C"));
 
 
SEXP regm;
PROTECT(regm = R_tryEval(e, R_GlobalEnv, NULL));
 
// extract Support Vector indices and create predictive gram matrix
// get indices of support vectors
SEXP svR;
PROTECT(svR = R_exec("SVindex", regm));
 
// how many sv's
SEXP lR;
PROTECT(lR = R_exec("length", svR));
 
gsl_matrix* pred_matrix = gsl_matrix_calloc(1, INTEGER(lR)[0]);
test->calculate_pred_matrix(neighbors, pred_matrix, svR);
 
// convert predictive gram matrix into correct format
SEXP pr;
SEXP* predR = &pr;
matrix_gsl2R(predR, pred_matrix);
SEXP nrR; PROTECT (nrR = allocVector(INTSXP, 1)); INTEGER(nrR)[0] = 1;
PROTECT((*predR) = R_exec3("matrix",(*predR), nrR)); // enforce row representation
PROTECT((*predR) = R_exec("as.kernelMatrix", (*predR)));
 
// predict(regm,pred)
SEXP pR;
 
PROTECT(pR = R_exec3("predict", regm, (*predR)));
PROTECT(pR = R_exec("as.integer", pR));
 
UNPROTECT(14);
 
gsl_matrix_free(pred_matrix);
            if ((INTEGER(pR)[0]-1) == 0) *(this->out) << "prediction: 0\n";
            else *(this->out) << "prediction: 1\n";
*(this->out) << "confidence: " << confidence << "\n";
*(this->out) << "known_fraction: " << known_fraction << "\n";
this->out->print();
}
 
gsl_vector_free(y);
gsl_vector_free(y_bar);
 
}
 
else {
*(this->out) << "prediction: \n";
*(this->out) << "confidence: \n";
*(this->out) << "known_fraction: \n";
this->out->print();
}
 
};
 
template <typename MolType, typename FeatureType, typename ActivityType>
void KernelModel<MolType,FeatureType,ActivityType>::calculate_prediction(FeatMol<MolType,RegrFeat,float>* test, RegrMolVect * neighbors, string act) {
 
// data storage
    vector<Feature<RegrFeat>*> lrf;
vector<Feature<RegrFeat>*>* lr_features = &lrf;
multimap<float, RegrFeat*> msf;
multimap<float,FeatMol<MolType,RegrFeat,float>*> ssn;
multimap<float,FeatMol<MolType,RegrFeat,float>*>* sim_sorted_neighbors = &ssn;
 
 
// iterators
typename RegrMolVect::iterator cur_n, cur_n2;
typename FeatVect::iterator cur_feat;
typename multimap<float, RegrFeat*>::iterator sig_feat;
gsl_vector* y;
 
// primitive data types
float confidence, sim;
 
if (neighbors->size()) {
 
// sort neighbors by similarity
sim_sorted_neighbors->clear();
for (cur_n = neighbors->begin(); cur_n != neighbors->end(); cur_n++) {
sim = (*cur_n)->get_similarity();
            sim_sorted_neighbors->insert(pair<float, FeatMol<MolType,RegrFeat,float>*>(sim,*cur_n));
}
test->extract_neighbors(neighbors, sim_sorted_neighbors);
 
// calculate confidence
confidence = test->calculate_confidence(neighbors, act);
 
// unite neighbor's with test's features
test->unite_features(lr_features, neighbors);
 
if (confidence < 0.995) {
 
y = gsl_vector_calloc(neighbors->size());
 
unsigned int rc = 1;
for (cur_n = neighbors->begin(); cur_n != neighbors->end(); cur_n++) {
test->set_y((*cur_n), y, act, rc);
rc++;
}
 
gsl_matrix* gram_matrix = gsl_matrix_calloc(neighbors->size(), neighbors->size());
test->calculate_gram_matrix(neighbors, gram_matrix, act);
 
// convert gram matrix to R kernelMatrix using R util function
SEXP mr;
SEXP* gramR = &mr;
matrix_gsl2R(gramR, gram_matrix);
PROTECT((*gramR) = R_exec("as.kernelMatrix", (*gramR)));
 
// convert activity values to R vector
SEXP vr;
SEXP* aR = &vr;
vector_gsl2R(aR,y);
PROTECT((*aR) = R_exec("as.vector", (*aR)));
 
// learn kernel model
SEXP e;
SEXP fun;
PROTECT(fun = Rf_findFun(Rf_install("ksvm"), R_GlobalEnv));
if(fun == R_NilValue) {
fprintf(stderr, "No definition for function.\n");
UNPROTECT(1);
exit(1);
}
                        PROTECT(e = allocVector(LANGSXP,6));
                        SETCAR(e, fun);
                        SETCAR(CDR(e), (*gramR));
                        SETCAR(CDR(CDR(e)), (*aR));
                        SETCAR(CDR(CDR(CDR(e))), mkString("matrix"));
                        SET_TAG(CDR(CDR(CDR(e))), install("kernel"));
                        SETCAR(CDR(CDR(CDR(CDR(e)))), mkString("nu-svr"));
                        SET_TAG(CDR(CDR(CDR(CDR(e)))), install("type"));
                        SETCAR(CDR(CDR(CDR(CDR(CDR(e))))), mkString("0.8"));
                        SET_TAG(CDR(CDR(CDR(CDR(CDR(e))))), install("nu"));
 
 
 
SEXP regm;
PROTECT(regm = R_tryEval(e, R_GlobalEnv, NULL));
 
// extract Support Vector indices and create predictive gram matrix
// get indices of support vectors
SEXP svR;
PROTECT(svR = R_exec("SVindex", regm));
 
// how many sv's
SEXP lR;
PROTECT(lR = R_exec("length", svR));
 
// extract predictive gram matrix
gsl_matrix* pred_matrix = gsl_matrix_calloc(1, INTEGER(lR)[0]);
test->calculate_pred_matrix(neighbors, pred_matrix, svR);
 
// convert predictive gram matrix into correct format
SEXP pr;
SEXP* predR = &pr;
matrix_gsl2R(predR, pred_matrix);
SEXP nrR; PROTECT (nrR = allocVector(INTSXP, 1)); INTEGER(nrR)[0] = 1;
PROTECT((*predR) = R_exec3("matrix",(*predR), nrR)); // enforce row representation
PROTECT((*predR) = R_exec("as.kernelMatrix", (*predR)));
 
// predict
SEXP pR;
PROTECT(pR = R_exec3("predict", regm, (*predR)));
prediction = REAL(pR)[0];
 
UNPROTECT(11);
 
gsl_matrix_free(gram_matrix);
gsl_matrix_free(pred_matrix);
gsl_vector_free(y);
 
*(this->out) << "prediction: " << prediction << "\n";
*(this->out) << "confidence: " << confidence << "\n";
this->out->print();
 
 
} // end if confidence >...
 
        else {
 
            *(this->out) << "prediction: \n";
            *(this->out) << "confidence: \n";
            this->out->print();
    
        }
 
 
} // end if at least one neighbor
 
    else {
    
        *(this->out) << "prediction: \n";
        *(this->out) << "confidence: \n";
        this->out->print();
 
    }
 
};
 
 
float gauss(float sim, float sigma) {
 
const float invc = 1.0;
float x = (invc - sim);
if (x > 1.0) x = 1.0;
if (x < 0.0) x = 0.0;
 
// gauss kernel
float g = 0.0;
g = exp(-(x*x)/(2*sigma*sigma));
return g;
};
 
#endif