SHOGUN  v3.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
SGInterface.cpp
Go to the documentation of this file.
4 
5 #include <shogun/lib/config.h>
6 #include <shogun/lib/DataType.h>
7 #include <shogun/lib/SGNDArray.h>
8 #include <shogun/lib/common.h>
11 #include <shogun/lib/Hash.h>
12 #include <shogun/lib/Map.h>
13 #include <shogun/lib/Signal.h>
14 
16 #include <shogun/lib/external/pr_loqo.h>
31 
33 
34 #include <shogun/structure/Plif.h>
40 
41 #include <ctype.h>
42 
43 using namespace shogun;
44 
45 CSGInterface* interface=NULL;
47 
48 #if defined(HAVE_CMDLINE)
49 #define USAGE(method) "", ""
50 #define USAGE_I(method, in) "", " " in ""
51 #define USAGE_O(method, out) "" out " = ", ""
52 #define USAGE_IO(method, in, out) "" out " = ", " " in ""
53 #define USAGE_COMMA " "
54 #define USAGE_STR ""
55 #elif defined(HAVE_R)
56 #define USAGE(method) "sg('", "')"
57 #define USAGE_I(method, in) "sg('", "', " in ")"
58 #define USAGE_O(method, out) "[" out "] <- sg('", "')"
59 #define USAGE_IO(method, in, out) "[" out "] <- sg('", "', " in ")"
60 #define USAGE_COMMA ", "
61 #define USAGE_STR "'"
62 #else
63 #define USAGE(method) "sg('", "')"
64 #define USAGE_I(method, in) "sg('", "', " in ")"
65 #define USAGE_O(method, out) "[" out "]=sg('", "')"
66 #define USAGE_IO(method, in, out) "[" out "]=sg('", "', " in ")"
67 #define USAGE_COMMA ", "
68 #define USAGE_STR "'"
69 #endif
70 
71 CSGInterfaceMethod sg_methods[]=
72 {
73  { "Features", NULL, NULL, NULL },
74  {
75  N_PR_LOQO,
76  (&CSGInterface::cmd_pr_loqo),
78  "'Var1', Var1, 'Var2', Var2", "results")
79  },
80  {
82  (&CSGInterface::cmd_load_features),
84  "filename" USAGE_COMMA "feature_class" USAGE_COMMA "type" USAGE_COMMA "target[" USAGE_COMMA "size[" USAGE_COMMA "comp_features]]")
85  },
86  {
88  (&CSGInterface::cmd_save_features),
89  USAGE_I(N_SAVE_FEATURES, "filename" USAGE_COMMA "type" USAGE_COMMA "target")
90  },
91  {
93  (&CSGInterface::cmd_clean_features),
95  },
96  {
98  (&CSGInterface::cmd_get_features),
99  USAGE_IO(N_GET_FEATURES, USAGE_STR "TRAIN|TEST" USAGE_STR, "features")
100  },
101  {
103  (&CSGInterface::cmd_add_features),
105  USAGE_STR "TRAIN|TEST" USAGE_STR USAGE_COMMA "features[" USAGE_COMMA "DNABINFILE|<ALPHABET>]")
106  },
107  {
109  (&CSGInterface::cmd_add_multiple_features),
111  USAGE_STR "TRAIN|TEST" USAGE_STR USAGE_COMMA "repetitions" USAGE_COMMA "features[" USAGE_COMMA "DNABINFILE|<ALPHABET>]")
112  },
113  {
115  (&CSGInterface::cmd_add_dotfeatures),
117  USAGE_STR "TRAIN|TEST" USAGE_STR USAGE_COMMA "features[" USAGE_COMMA "DNABINFILE|<ALPHABET>]")
118  },
119  {
121  (&CSGInterface::cmd_set_features),
123  USAGE_STR "TRAIN|TEST" USAGE_STR
124  USAGE_COMMA "features["
125  USAGE_COMMA "DNABINFILE|<ALPHABET>]["
126  USAGE_COMMA "[from_position_list|slide_window]"
127  USAGE_COMMA "window size"
128  USAGE_COMMA "[position_list|shift]"
129  USAGE_COMMA "skip")
130  },
131  {
133  (&CSGInterface::cmd_set_reference_features),
135  },
136  {
138  (&CSGInterface::cmd_del_last_features),
140  },
141  {
142  N_CONVERT,
143  (&CSGInterface::cmd_convert),
144  USAGE_I(N_CONVERT, USAGE_STR "TRAIN|TEST" USAGE_STR
145  USAGE_COMMA "from_class"
146  USAGE_COMMA "from_type"
147  USAGE_COMMA "to_class"
148  USAGE_COMMA "to_type["
149  USAGE_COMMA "order"
150  USAGE_COMMA "start"
151  USAGE_COMMA "gap"
152  USAGE_COMMA "reversed]")
153  },
154  {
155  N_RESHAPE,
156  (&CSGInterface::cmd_reshape),
157  USAGE_I(N_RESHAPE, USAGE_STR "TRAIN|TEST"
158  USAGE_COMMA "num_feat"
159  USAGE_COMMA "num_vec")
160  },
161  {
163  (&CSGInterface::cmd_load_labels),
164  USAGE_I(N_LOAD_LABELS, "filename"
165  USAGE_COMMA USAGE_STR "TRAIN|TARGET" USAGE_STR)
166  },
167  {
168  N_SET_LABELS,
169  (&CSGInterface::cmd_set_labels),
171  USAGE_COMMA "labels")
172  },
173  {
174  N_GET_LABELS,
175  (&CSGInterface::cmd_get_labels),
176  USAGE_IO(N_GET_LABELS, USAGE_STR "TRAIN|TEST" USAGE_STR, "labels")
177  },
178 
179 
180  { "Kernel", NULL, NULL },
181  {
183  (&CSGInterface::cmd_set_kernel_normalization),
184  USAGE_I(N_SET_KERNEL_NORMALIZATION, "IDENTITY|AVGDIAG|SQRTDIAG|FIRSTELEMENT|VARIANCE|ZEROMEANCENTER"
185  USAGE_COMMA "size[" USAGE_COMMA "kernel-specific parameters]")
186  },
187  {
188  N_SET_KERNEL,
189  (&CSGInterface::cmd_set_kernel),
190  USAGE_I(N_SET_KERNEL, "type" USAGE_COMMA "size[" USAGE_COMMA "kernel-specific parameters]")
191  },
192  {
193  N_ADD_KERNEL,
194  (&CSGInterface::cmd_add_kernel),
195  USAGE_I(N_ADD_KERNEL, "weight" USAGE_COMMA "kernel-specific parameters")
196  },
197  {
199  (&CSGInterface::cmd_del_last_kernel),
201  },
202  {
204  (&CSGInterface::cmd_init_kernel),
206  },
207  {
209  (&CSGInterface::cmd_clean_kernel),
211  },
212  {
214  (&CSGInterface::cmd_save_kernel),
215  USAGE_I(N_SAVE_KERNEL, "filename" USAGE_COMMA USAGE_STR "TRAIN|TEST" USAGE_STR)
216  },
217  {
219  (&CSGInterface::cmd_get_kernel_matrix),
220  USAGE_IO(N_GET_KERNEL_MATRIX, "[" USAGE_STR "TRAIN|TEST" USAGE_STR, "K]")
221  },
222  {
224  (&CSGInterface::cmd_set_WD_position_weights),
226  },
227  {
229  (&CSGInterface::cmd_get_subkernel_weights),
231  },
232  {
234  (&CSGInterface::cmd_set_subkernel_weights),
236  },
237  {
239  (&CSGInterface::cmd_set_subkernel_weights_combined),
241  },
242  {
244  (&CSGInterface::cmd_get_dotfeature_weights_combined),
246  },
247  {
249  (&CSGInterface::cmd_set_dotfeature_weights_combined),
251  },
252  {
254  (&CSGInterface::cmd_set_last_subkernel_weights),
256  },
257  {
259  (&CSGInterface::cmd_get_WD_position_weights),
261  },
262  {
264  (&CSGInterface::cmd_get_last_subkernel_weights),
266  },
267  {
269  (&CSGInterface::cmd_compute_by_subkernels),
271  },
272  {
274  (&CSGInterface::cmd_init_kernel_optimization),
276  },
277  {
279  (&CSGInterface::cmd_get_kernel_optimization),
281  },
282  {
284  (&CSGInterface::cmd_delete_kernel_optimization),
286  },
287  {
289  (&CSGInterface::cmd_use_diagonal_speedup),
291  },
292  {
294  (&CSGInterface::cmd_set_kernel_optimization_type),
295  USAGE_I(N_SET_KERNEL_OPTIMIZATION_TYPE, USAGE_STR "FASTBUTMEMHUNGRY|SLOWBUTMEMEFFICIENT" USAGE_STR)
296  },
297  {
298  N_SET_SOLVER,
299  (&CSGInterface::cmd_set_solver),
300  USAGE_I(N_SET_SOLVER, USAGE_STR "AUTO|CPLEX|GLPK|INTERNAL" USAGE_STR)
301  },
302  {
304  (&CSGInterface::cmd_set_constraint_generator),
305  USAGE_I(N_SET_CONSTRAINT_GENERATOR, USAGE_STR "LIBSVM_ONECLASS|LIBSVM_MULTICLASS|LIBSVM"
306  "|SVMLIGHT|LIGHT|SVMLIGHT_ONECLASS|GPBTSVM|MPDSVM|GNPPSVM|GMNPSVM"
307  USAGE_STR)
308  },
309  {
311  (&CSGInterface::cmd_set_prior_probs),
312  USAGE_I(N_SET_PRIOR_PROBS, USAGE_STR "pos probs, neg_probs" USAGE_STR)
313  },
314  {
316  (&CSGInterface::cmd_set_prior_probs_from_labels),
318  },
319 
320 
321 
322  { "Distance", NULL, NULL },
323  {
325  (&CSGInterface::cmd_set_distance),
326  USAGE_I(N_SET_DISTANCE, "type" USAGE_COMMA "data type[" USAGE_COMMA "distance-specific parameters]")
327  },
328  {
330  (&CSGInterface::cmd_init_distance),
332  },
333  {
335  (&CSGInterface::cmd_get_distance_matrix),
337  },
338 
339 
340  { "Classifier", NULL, NULL },
341  {
342  N_CLASSIFY,
343  (&CSGInterface::cmd_classify),
344  USAGE_O(N_CLASSIFY, "result")
345  },
346  {
348  (&CSGInterface::cmd_classify),
349  USAGE_O(N_SVM_CLASSIFY, "result")
350  },
351  {
353  (&CSGInterface::cmd_classify_example),
354  USAGE_IO(N_CLASSIFY_EXAMPLE, "feature_vector_index", "result")
355  },
356  {
358  (&CSGInterface::cmd_classify_example),
359  USAGE_IO(N_SVM_CLASSIFY_EXAMPLE, "feature_vector_index", "result")
360  },
361  {
363  (&CSGInterface::cmd_get_classifier),
364  USAGE_IO(N_GET_CLASSIFIER, "[index in case of MultiClassSVM]", "bias" USAGE_COMMA "weights")
365  },
366  {
368  (&CSGInterface::cmd_get_classifier),
369  USAGE_O(N_GET_CLUSTERING, "radi" USAGE_COMMA "centers|merge_distances" USAGE_COMMA "pairs")
370  },
371  {
372  N_NEW_SVM,
373  (&CSGInterface::cmd_new_classifier),
374  USAGE_I(N_NEW_SVM, USAGE_STR "LIBSVM_ONECLASS|LIBSVM_MULTICLASS|LIBSVM"
375  "|SVMLIGHT|LIGHT|LIGHT_ONECLASS|SVMLIN|GPBTSVM|MPDSVM|GNPPSVM|GMNPSVM"
376  "|WDSVMOCAS|SVMOCAS|SVMSGD|SVMBMRM|SVMPERF"
377  "|KERNELPERCEPTRON|PERCEPTRON|LIBLINEAR_LR|LIBLINEAR_L2|LDA"
378  "|LPM|LPBOOST|KNN" USAGE_STR)
379  },
380  {
382  (&CSGInterface::cmd_new_classifier),
383  USAGE_I(N_NEW_CLASSIFIER, USAGE_STR "LIBSVM_ONECLASS|LIBSVM_MULTICLASS"
384  "|LIBSVM|SVMLIGHT|LIGHT|LIGHT_ONECLASS|SVMLIN|GPBTSVM|MPDSVM|GNPPSVM|GMNPSVM"
385  "|WDSVMOCAS|SVMOCAS|SVMSGD|SVMBMRM|SVMPERF"
386  "|KERNELPERCEPTRON|PERCEPTRON|LIBLINEAR_LR|LIBLINEAR_L2|LDA"
387  "|LPM|LPBOOST|KNN" USAGE_STR)
388  },
389  {
391  (&CSGInterface::cmd_new_classifier),
392  USAGE_I(N_NEW_REGRESSION, USAGE_STR "SVRLIGHT|LIBSVR|KRR" USAGE_STR)
393  },
394  {
396  (&CSGInterface::cmd_new_classifier),
397  USAGE_I(N_NEW_CLUSTERING, USAGE_STR "KMEANS|HIERARCHICAL" USAGE_STR)
398  },
399  {
401  (&CSGInterface::cmd_load_classifier),
402  USAGE_O(N_LOAD_CLASSIFIER, "filename" USAGE_COMMA "type")
403  },
404  {
406  (&CSGInterface::cmd_save_classifier),
407  USAGE_I(N_SAVE_CLASSIFIER, "filename")
408  },
409  {
411  (&CSGInterface::cmd_get_num_svms),
412  USAGE_O(N_GET_NUM_SVMS, "number of SVMs in MultiClassSVM")
413  },
414  {
415  N_GET_SVM,
416  (&CSGInterface::cmd_get_svm),
417  USAGE_IO(N_GET_SVM, "[index in case of MultiClassSVM]", "bias" USAGE_COMMA "alphas")
418  },
419  {
420  N_SET_SVM,
421  (&CSGInterface::cmd_set_svm),
422  USAGE_I(N_SET_SVM, "bias" USAGE_COMMA "alphas")
423  },
424  {
426  (&CSGInterface::cmd_set_linear_classifier),
428  },
429  {
431  (&CSGInterface::cmd_get_svm_objective),
432  USAGE_O(N_GET_SVM_OBJECTIVE, "objective")
433  },
434  {
436  (&CSGInterface::cmd_compute_svm_primal_objective),
438  },
439  {
441  (&CSGInterface::cmd_compute_svm_dual_objective),
443  },
444  {
446  (&CSGInterface::cmd_compute_svm_primal_objective),
448  },
449  {
451  (&CSGInterface::cmd_compute_mkl_dual_objective),
453  },
454  {
456  (&CSGInterface::cmd_compute_relative_mkl_duality_gap),
458  },
459  {
461  (&CSGInterface::cmd_compute_absolute_mkl_duality_gap),
463  },
464  {
466  (&CSGInterface::cmd_do_auc_maximization),
468  },
469  {
471  (&CSGInterface::cmd_set_perceptron_parameters),
472  USAGE_I(N_SET_PERCEPTRON_PARAMETERS, "learnrate" USAGE_COMMA "maxiter")
473  },
474  {
476  (&CSGInterface::cmd_train_classifier),
477  USAGE_I(N_TRAIN_CLASSIFIER, "[classifier-specific parameters]")
478  },
479  {
481  (&CSGInterface::cmd_train_classifier),
483  },
484  {
486  (&CSGInterface::cmd_train_classifier),
488  },
489  {
490  N_SVM_TRAIN,
491  (&CSGInterface::cmd_train_classifier),
492  USAGE_I(N_SVM_TRAIN, "[classifier-specific parameters]")
493  },
494  {
495  N_SVMQPSIZE,
496  (&CSGInterface::cmd_set_svm_qpsize),
497  USAGE_I(N_SVMQPSIZE, "size")
498  },
499  {
501  (&CSGInterface::cmd_set_svm_max_qpsize),
502  USAGE_I(N_SVMMAXQPSIZE, "size")
503  },
504  {
505  N_SVMBUFSIZE,
506  (&CSGInterface::cmd_set_svm_bufsize),
507  USAGE_I(N_SVMBUFSIZE, "size")
508  },
509  {
510  N_C,
511  (&CSGInterface::cmd_set_svm_C),
512  USAGE_I(N_C, "C1[" USAGE_COMMA "C2]")
513  },
514  {
516  (&CSGInterface::cmd_set_svm_epsilon),
517  USAGE_I(N_SVM_EPSILON, "epsilon")
518  },
519  {
521  (&CSGInterface::cmd_set_svr_tube_epsilon),
522  USAGE_I(N_SVR_TUBE_EPSILON, "tube_epsilon")
523  },
524  {
525  N_SVM_NU,
526  (&CSGInterface::cmd_set_svm_nu),
527  USAGE_I(N_SVM_NU, "nu")
528  },
529  {
531  (&CSGInterface::cmd_set_svm_mkl_parameters),
532  USAGE_I(N_MKL_PARAMETERS, "weight_epsilon" USAGE_COMMA "C_MKL [" USAGE_COMMA "mkl_norm ]")
533  },
534  {
535  N_ENT_LAMBDA,
536  (&CSGInterface::cmd_set_elasticnet_lambda),
537  USAGE_I(N_ENT_LAMBDA, "ent_lambda")
538  },
539  {
541  (&CSGInterface::cmd_set_mkl_block_norm),
542  USAGE_I(N_MKL_BLOCK_NORM, "mkl_block_norm")
543  },
544  {
546  (&CSGInterface::cmd_set_max_train_time),
547  USAGE_I(N_SVM_MAX_TRAIN_TIME, "max_train_time")
548  },
549  {
551  (&CSGInterface::cmd_set_svm_shrinking_enabled),
552  USAGE_I(N_USE_SHRINKING, "enable_shrinking")
553  },
554  {
556  (&CSGInterface::cmd_set_svm_batch_computation_enabled),
557  USAGE_I(N_USE_BATCH_COMPUTATION, "enable_batch_computation")
558  },
559  {
560  N_USE_LINADD,
561  (&CSGInterface::cmd_set_svm_linadd_enabled),
562  USAGE_I(N_USE_LINADD, "enable_linadd")
563  },
564  {
566  (&CSGInterface::cmd_set_svm_bias_enabled),
567  USAGE_I(N_SVM_USE_BIAS, "enable_bias")
568  },
569  {
571  (&CSGInterface::cmd_set_mkl_interleaved_enabled),
572  USAGE_I(N_MKL_USE_INTERLEAVED_OPTIMIZATION, "enable_interleaved_optimization")
573  },
574  {
575  N_KRR_TAU,
576  (&CSGInterface::cmd_set_krr_tau),
577  USAGE_I(N_KRR_TAU, "tau")
578  },
579 
580 
581  { "Preprocessors", NULL, NULL },
582  {
584  (&CSGInterface::cmd_add_preproc),
585  USAGE_I(N_ADD_PREPROC, "preproc[, preproc-specific parameters]")
586  },
587  {
589  (&CSGInterface::cmd_del_preproc),
591  },
592  {
594  (&CSGInterface::cmd_attach_preproc),
596  },
597  {
599  (&CSGInterface::cmd_clean_preproc),
601  },
602 
603  { "Converters", NULL, NULL },
604  {
606  (&CSGInterface::cmd_set_converter),
608  },
609  {
611  (&CSGInterface::cmd_apply_converter),
612  USAGE_O(N_APPLY_CONVERTER, "conv_features")
613  },
614  {
615  N_EMBED,
616  (&CSGInterface::cmd_embed),
617  USAGE_IO(N_EMBED,"target dim","embedding")
618  },
619 
620 
621  { "HMM", NULL, NULL },
622  {
623  N_NEW_HMM,
624  (&CSGInterface::cmd_new_hmm),
625  USAGE_I(N_NEW_HMM, "N" USAGE_COMMA "M")
626  },
627  {
628  N_LOAD_HMM,
629  (&CSGInterface::cmd_load_hmm),
630  USAGE_I(N_LOAD_HMM, "filename")
631  },
632  {
633  N_SAVE_HMM,
634  (&CSGInterface::cmd_save_hmm),
635  USAGE_I(N_SAVE_HMM, "filename[" USAGE_COMMA "save_binary]")
636  },
637  {
638  N_GET_HMM,
639  (&CSGInterface::cmd_get_hmm),
641  },
642  {
643  N_APPEND_HMM,
644  (&CSGInterface::cmd_append_hmm),
646  },
647  {
649  (&CSGInterface::cmd_append_model),
650  USAGE_I(N_APPEND_MODEL, USAGE_STR "filename" USAGE_STR "[" USAGE_COMMA "base1" USAGE_COMMA "base2]")
651  },
652  {
653  N_SET_HMM,
654  (&CSGInterface::cmd_set_hmm),
656  },
657  {
658  N_SET_HMM_AS,
659  (&CSGInterface::cmd_set_hmm_as),
660  USAGE_I(N_SET_HMM_AS, "POS|NEG|TEST")
661  },
662  {
663  N_CHOP,
664  (&CSGInterface::cmd_set_chop),
665  USAGE_I(N_CHOP, "chop")
666  },
667  {
668  N_PSEUDO,
669  (&CSGInterface::cmd_set_pseudo),
670  USAGE_I(N_PSEUDO, "pseudo")
671  },
672  {
674  (&CSGInterface::cmd_load_definitions),
675  USAGE_I(N_LOAD_DEFINITIONS, "filename" USAGE_COMMA "init")
676  },
677  {
679  (&CSGInterface::cmd_hmm_classify),
680  USAGE_O(N_HMM_CLASSIFY, "result")
681  },
682  {
684  (&CSGInterface::cmd_one_class_linear_hmm_classify),
686  },
687  {
689  (&CSGInterface::cmd_one_class_hmm_classify),
691  },
692  {
694  (&CSGInterface::cmd_one_class_hmm_classify_example),
695  USAGE_IO(N_ONE_CLASS_HMM_CLASSIFY_EXAMPLE, "feature_vector_index", "result")
696  },
697  {
699  (&CSGInterface::cmd_hmm_classify_example),
700  USAGE_IO(N_HMM_CLASSIFY_EXAMPLE, "feature_vector_index", "result")
701  },
702  {
703  N_OUTPUT_HMM,
704  (&CSGInterface::cmd_output_hmm),
706  },
707  {
709  (&CSGInterface::cmd_output_hmm_defined),
711  },
712  {
714  (&CSGInterface::cmd_hmm_likelihood),
715  USAGE_O(N_HMM_LIKELIHOOD, "likelihood")
716  },
717  {
718  N_LIKELIHOOD,
719  (&CSGInterface::cmd_likelihood),
721  },
722  {
724  (&CSGInterface::cmd_save_likelihood),
725  USAGE_I(N_SAVE_LIKELIHOOD, "filename[" USAGE_COMMA "save_binary]")
726  },
727  {
729  (&CSGInterface::cmd_get_viterbi_path),
730  USAGE_IO(N_GET_VITERBI_PATH, "dim", "path" USAGE_COMMA "likelihood")
731  },
732  {
734  (&CSGInterface::cmd_viterbi_train_defined),
736  },
737  {
739  (&CSGInterface::cmd_viterbi_train),
741  },
742  {
744  (&CSGInterface::cmd_baum_welch_train),
746  },
747  {
749  (&CSGInterface::cmd_baum_welch_train_defined),
751  },
752  {
754  (&CSGInterface::cmd_baum_welch_trans_train),
756  },
757  {
759  (&CSGInterface::cmd_linear_train),
761  },
762  {
763  N_SAVE_PATH,
764  (&CSGInterface::cmd_save_path),
765  USAGE_I(N_SAVE_PATH, "filename[" USAGE_COMMA "save_binary]")
766  },
767  {
769  (&CSGInterface::cmd_convergence_criteria),
770  USAGE_I(N_CONVERGENCE_CRITERIA, "num_iterations" USAGE_COMMA "epsilon")
771  },
772  {
773  N_NORMALIZE,
774  (&CSGInterface::cmd_normalize),
775  USAGE_I(N_NORMALIZE, "[keep_dead_states]")
776  },
777  {
778  N_ADD_STATES,
779  (&CSGInterface::cmd_add_states),
780  USAGE_I(N_ADD_STATES, "states" USAGE_COMMA "value")
781  },
782  {
784  (&CSGInterface::cmd_permutation_entropy),
785  USAGE_I(N_PERMUTATION_ENTROPY, "width" USAGE_COMMA "seqnum")
786  },
787  {
789  (&CSGInterface::cmd_relative_entropy),
790  USAGE_O(N_RELATIVE_ENTROPY, "result")
791  },
792  {
793  N_ENTROPY,
794  (&CSGInterface::cmd_entropy),
795  USAGE_O(N_ENTROPY, "result")
796  },
797  {
798  (char*) N_SET_FEATURE_MATRIX,
799  (&CSGInterface::cmd_set_feature_matrix),
800  (char*) USAGE_I(N_SET_FEATURE_MATRIX, "features")
801  },
802  {
804  (&CSGInterface::cmd_set_feature_matrix_sparse),
805  (char*) USAGE_I(N_SET_FEATURE_MATRIX_SPARSE, "sp1" USAGE_COMMA "sp2" )
806  },
807  {
809  (&CSGInterface::cmd_new_plugin_estimator),
810  USAGE_I(N_NEW_PLUGIN_ESTIMATOR, "pos_pseudo" USAGE_COMMA "neg_pseudo")
811  },
812  {
814  (&CSGInterface::cmd_train_estimator),
816  },
817  {
819  (&CSGInterface::cmd_plugin_estimate_classify_example),
820  USAGE_IO(N_PLUGIN_ESTIMATE_CLASSIFY_EXAMPLE, "feature_vector_index", "result")
821  },
822  {
824  (&CSGInterface::cmd_plugin_estimate_classify),
826  },
827  {
829  (&CSGInterface::cmd_set_plugin_estimate),
830  USAGE_I(N_SET_PLUGIN_ESTIMATE, "emission_probs" USAGE_COMMA "model_sizes")
831  },
832  {
834  (&CSGInterface::cmd_get_plugin_estimate),
835  USAGE_O(N_GET_PLUGIN_ESTIMATE, "emission_probs" USAGE_COMMA "model_sizes")
836  },
837  { "Signals", NULL, NULL },
838  {
840  (&CSGInterface::cmd_signals_set_model),
842  },
843  {
845  (&CSGInterface::cmd_signals_set_positions),
846  USAGE_I(N_SIGNALS_SET_POSITIONS, "positions")
847  },
848  {
850  (&CSGInterface::cmd_signals_set_labels),
851  USAGE_I(N_SIGNALS_SET_LABELS, "labels")
852  },
853  {
855  (&CSGInterface::cmd_signals_set_split),
856  USAGE_I(N_SIGNALS_SET_SPLIT, "split")
857  },
858  {
860  (&CSGInterface::cmd_signals_set_train_mask),
862  },
863  {
865  (&CSGInterface::cmd_signals_add_feature),
866  USAGE_I(N_SIGNALS_ADD_FEATURE, "feature")
867  },
868  {
870  (&CSGInterface::cmd_signals_add_kernel),
871  USAGE_I(N_SIGNALS_ADD_KERNEL, "kernelparam")
872  },
873  {
875  (&CSGInterface::cmd_signals_run),
876  USAGE_I(N_SIGNALS_RUN, "arg1")
877  },
878  { "Structure", NULL, NULL },
879  {
880  N_BEST_PATH,
881  (&CSGInterface::cmd_best_path),
882  USAGE_I(N_BEST_PATH, "from" USAGE_COMMA "to")
883  },
884  {
886  (&CSGInterface::cmd_best_path_2struct),
888  USAGE_COMMA "q"
889  USAGE_COMMA "cmd_trans"
890  USAGE_COMMA "seq"
891  USAGE_COMMA "pos"
892  USAGE_COMMA "genestr"
893  USAGE_COMMA "penalties"
894  USAGE_COMMA "penalty_info"
895  USAGE_COMMA "nbest"
896  USAGE_COMMA "content_weights"
897  USAGE_COMMA "segment_sum_weights",
898  "prob" USAGE_COMMA "path" USAGE_COMMA "pos")
899  },
900  {
901  (char*) N_SET_PLIF_STRUCT,
902  (&CSGInterface::cmd_set_plif_struct),
903  (char*) USAGE_I(N_SET_PLIF_STRUCT, "id"
904  USAGE_COMMA "name"
905  USAGE_COMMA "limits"
906  USAGE_COMMA "penalties"
907  USAGE_COMMA "transform"
908  USAGE_COMMA "min_value"
909  USAGE_COMMA "max_value"
910  USAGE_COMMA "use_cache"
911  USAGE_COMMA "use_svm")
912  },
913  {
914  (char*) N_GET_PLIF_STRUCT,
915  (&CSGInterface::cmd_get_plif_struct),
916  (char*) USAGE_O(N_GET_PLIF_STRUCT, "id"
917  USAGE_COMMA "name"
918  USAGE_COMMA "limits"
919  USAGE_COMMA "penalties"
920  USAGE_COMMA "transform"
921  USAGE_COMMA "min_value"
922  USAGE_COMMA "max_value"
923  USAGE_COMMA "use_cache"
924  USAGE_COMMA "use_svm")
925  },
926  {
927  (char*) N_PRECOMPUTE_SUBKERNELS,
928  (&CSGInterface::cmd_precompute_subkernels),
930  },
931  {
933  (&CSGInterface::cmd_precompute_content_svms),
934  (char*) USAGE_I(N_PRECOMPUTE_CONTENT_SVMS, "sequence"
935  USAGE_COMMA "position_list"
936  USAGE_COMMA "weights")
937  },
938  {
939  (char*) N_GET_LIN_FEAT,
940  (&CSGInterface::cmd_get_lin_feat),
941  (char*) USAGE_O(N_GET_LIN_FEAT, "lin_feat")
942  },
943  {
944  (char*) N_SET_LIN_FEAT,
945  (&CSGInterface::cmd_set_lin_feat),
946  (char*) USAGE_I(N_SET_LIN_FEAT, "lin_feat")
947  },
948  {
949  (char*) N_INIT_DYN_PROG,
950  (&CSGInterface::cmd_init_dyn_prog),
951  (char*) USAGE_I(N_INIT_DYN_PROG, "num_svms")
952  },
953  {
954  (char*) N_CLEAN_UP_DYN_PROG,
955  (&CSGInterface::cmd_clean_up_dyn_prog),
956  (char*) USAGE(N_CLEAN_UP_DYN_PROG)
957  },
958  {
959  (char*) N_INIT_INTRON_LIST,
960  (&CSGInterface::cmd_init_intron_list),
961  (char*) USAGE_I(N_INIT_INTRON_LIST, "start_positions"
962  USAGE_COMMA "end_positions"
963  USAGE_COMMA "quality")
964  },
965  {
967  (&CSGInterface::cmd_precompute_tiling_features),
968  (char*) USAGE_I(N_PRECOMPUTE_TILING_FEATURES, "intensities"
969  USAGE_COMMA "probe_pos"
970  USAGE_COMMA "tiling_plif_ids")
971  },
972  {
974  (&CSGInterface::cmd_long_transition_settings),
975  (char*) USAGE_I(N_LONG_TRANSITION_SETTINGS, "use_long_transitions"
976  USAGE_COMMA "threshold"
977  USAGE_COMMA "max_len")
978  },
979 
980  {
981  (char*) N_SET_MODEL,
982  (&CSGInterface::cmd_set_model),
983  (char*) USAGE_I(N_SET_MODEL, "content_weights"
984  USAGE_COMMA "transition_pointers"
985  USAGE_COMMA "use_orf"
986  USAGE_COMMA "mod_words")
987  },
988 
989  {
990  (char*) N_BEST_PATH_TRANS,
991  (&CSGInterface::cmd_best_path_trans),
993  USAGE_COMMA "q"
994  USAGE_COMMA "nbest"
995  USAGE_COMMA "seq_path"
996  USAGE_COMMA "a_trans"
997  USAGE_COMMA "segment_loss",
998  "prob" USAGE_COMMA "path" USAGE_COMMA "pos")
999  },
1000  {
1002  (&CSGInterface::cmd_best_path_trans_deriv),
1004  USAGE_COMMA "my_path"
1005  USAGE_COMMA "my_pos"
1006  USAGE_COMMA "p"
1007  USAGE_COMMA "q"
1008  USAGE_COMMA "cmd_trans"
1009  USAGE_COMMA "seq"
1010  USAGE_COMMA "pos"
1011  USAGE_COMMA "genestr"
1012  USAGE_COMMA "penalties"
1013  USAGE_COMMA "state_signals"
1014  USAGE_COMMA "penalty_info"
1015  USAGE_COMMA "dict_weights"
1016  USAGE_COMMA "mod_words ["
1017  USAGE_COMMA "segment_loss"
1018  USAGE_COMMA "segmend_ids_mask]", "p_deriv"
1019  USAGE_COMMA "q_deriv"
1020  USAGE_COMMA "cmd_deriv"
1021  USAGE_COMMA "penalties_deriv"
1022  USAGE_COMMA "my_scores"
1023  USAGE_COMMA "my_loss")
1024  },
1025 
1026  { "POIM", NULL, NULL },
1027  {
1029  (&CSGInterface::cmd_compute_POIM_WD),
1030  USAGE_IO(N_COMPUTE_POIM_WD, "max_order" USAGE_COMMA "distribution", "W")
1031  },
1032  {
1034  (&CSGInterface::cmd_get_SPEC_consensus),
1036  },
1037  {
1039  (&CSGInterface::cmd_get_SPEC_scoring),
1040  USAGE_IO(N_GET_SPEC_SCORING, "max_order", "W")
1041  },
1042  {
1044  (&CSGInterface::cmd_get_WD_consensus),
1046  },
1047  {
1049  (&CSGInterface::cmd_get_WD_scoring),
1050  USAGE_IO(N_GET_WD_SCORING, "max_order", "W")
1051  },
1052 
1053 
1054  { "Utility", NULL, NULL },
1055  {
1056  N_CRC,
1057  (&CSGInterface::cmd_crc),
1058  USAGE_IO(N_CRC, "string", "crc32")
1059  },
1060  {
1061  N_SYSTEM,
1062  (&CSGInterface::cmd_system),
1063  USAGE_I(N_SYSTEM, "system_command")
1064  },
1065  {
1066  N_EXIT,
1067  (&CSGInterface::cmd_exit),
1068  USAGE(N_EXIT)
1069  },
1070  {
1071  N_QUIT,
1072  (&CSGInterface::cmd_exit),
1073  USAGE(N_QUIT)
1074  },
1075  {
1076  N_EXEC,
1077  (&CSGInterface::cmd_exec),
1078  USAGE_I(N_EXEC, "filename")
1079  },
1080  {
1081  N_SET_OUTPUT,
1082  (&CSGInterface::cmd_set_output),
1083  USAGE_I(N_SET_OUTPUT, USAGE_STR "STDERR|STDOUT|filename" USAGE_STR)
1084  },
1085  {
1087  (&CSGInterface::cmd_set_threshold),
1088  USAGE_I(N_SET_THRESHOLD, "threshold")
1089  },
1090  {
1091  N_INIT_RANDOM,
1092  (&CSGInterface::cmd_init_random),
1093  USAGE_I(N_INIT_RANDOM, "value_to_initialize_RNG_with")
1094  },
1095  {
1096  N_THREADS,
1097  (&CSGInterface::cmd_set_num_threads),
1098  USAGE_I(N_THREADS, "num_threads")
1099  },
1100  {
1102  (&CSGInterface::cmd_translate_string),
1104  "string, order, start", "translation")
1105  },
1106  {
1107  N_CLEAR,
1108  (&CSGInterface::cmd_clear),
1109  USAGE(N_CLEAR)
1110  },
1111  {
1112  N_TIC,
1113  (&CSGInterface::cmd_tic),
1114  USAGE(N_TIC)
1115  },
1116  {
1117  N_TOC,
1118  (&CSGInterface::cmd_toc),
1119  USAGE(N_TOC)
1120  },
1121  {
1122  N_PRINT,
1123  (&CSGInterface::cmd_print),
1124  USAGE_I(N_PRINT, "msg")
1125  },
1126  {
1127  N_ECHO,
1128  (&CSGInterface::cmd_echo),
1129  USAGE_I(N_ECHO, "level")
1130  },
1131  {
1132  N_LOGLEVEL,
1133  (&CSGInterface::cmd_loglevel),
1134  USAGE_I(N_LOGLEVEL, USAGE_STR "ALL|DEBUG|INFO|NOTICE|WARN|ERROR|CRITICAL|ALERT|EMERGENCY" USAGE_STR)
1135  },
1136  {
1138  (&CSGInterface::cmd_syntax_highlight),
1140  },
1141  {
1142  N_PROGRESS,
1143  (&CSGInterface::cmd_progress),
1145  },
1146  {
1147  N_GET_VERSION,
1148  (&CSGInterface::cmd_get_version),
1149  USAGE_O(N_GET_VERSION, "version")
1150  },
1151  {
1152  N_HELP,
1153  (&CSGInterface::cmd_help),
1154  USAGE(N_HELP)
1155  },
1156  {
1157  N_WHOS,
1158  (&CSGInterface::cmd_whos),
1159  USAGE(N_WHOS)
1160  },
1161  {
1163  (&CSGInterface::cmd_send_command),
1164  NULL
1165  },
1166  {
1167  N_RUN_PYTHON,
1168  (&CSGInterface::cmd_run_python),
1170  "'Var1', Var1, 'Var2', Var2,..., python_function", "results")
1171  },
1172  {
1173  N_RUN_OCTAVE,
1174  (&CSGInterface::cmd_run_octave),
1176  "'Var1', Var1, 'Var2', Var2,..., octave_function", "results")
1177  },
1178  {
1179  N_RUN_R,
1180  (&CSGInterface::cmd_run_r),
1181  USAGE_IO(N_RUN_R,
1182  "'Var1', Var1, 'Var2', Var2,..., r_function", "results")
1183  },
1184  {NULL, NULL, NULL} /* Sentinel */
1185 };
1186 
1187 
1188 CSGInterface::CSGInterface(bool print_copyright)
1189 : CSGObject(),
1190  ui_classifier(new CGUIClassifier(this)),
1191  ui_distance(new CGUIDistance(this)),
1192  ui_features(new CGUIFeatures(this)),
1193  ui_hmm(new CGUIHMM(this)),
1194  ui_kernel(new CGUIKernel(this)),
1195  ui_labels(new CGUILabels(this)),
1196  ui_math(new CGUIMath(this)),
1197  ui_pluginestimate(new CGUIPluginEstimate(this)),
1198  ui_preproc(new CGUIPreprocessor(this)),
1199  ui_time(new CGUITime(this)),
1200  ui_structure(new CGUIStructure(this)),
1201  ui_converter(new CGUIConverter(this))/*,
1202 / ui_signals(new CGUISignals(this))*/
1203 {
1204  if (print_copyright)
1205  {
1206  version->print_version();
1207  SG_PRINT("( seeding random number generator with %u (seed size %d))\n",
1209 #ifdef USE_LOGCACHE
1210  SG_PRINT("initializing log-table (size=%i*%i*%i=%2.1fMB) ... ) ",
1211  CMath::get_log_range(),CMath::get_log_accuracy(),sizeof(float64_t),
1212  CMath::get_log_range()*CMath::get_log_accuracy()*sizeof(float64_t)/(1024.0*1024.0));
1213 #else
1214  SG_PRINT("determined range for x in log(1+exp(-x)) is:%d )\n", CMath::get_log_range())
1215 #endif
1216  }
1217 
1218  reset();
1219 }
1220 
1221 CSGInterface::~CSGInterface()
1222 {
1223  delete ui_classifier;
1224  delete ui_hmm;
1225  delete ui_pluginestimate;
1226  delete ui_kernel;
1227  delete ui_preproc;
1228  delete ui_features;
1229  delete ui_labels;
1230  delete ui_math;
1231  delete ui_structure;
1232  //delete ui_signals;
1233  delete ui_time;
1234  delete ui_distance;
1235  delete ui_converter;
1236 
1237  if (file_out)
1238  fclose(file_out);
1239 }
1240 
1241 void CSGInterface::reset()
1242 {
1243  m_lhs_counter=0;
1244  m_rhs_counter=0;
1245  m_nlhs=0;
1246  m_nrhs=0;
1247  m_legacy_strptr=NULL;
1248  file_out=NULL;
1249  echo=true;
1250 }
1251 
1252 void CSGInterface::translate_arg(CSGInterface* source, CSGInterface* target)
1253 {
1254  switch (source->get_argument_type())
1255  {
1256  case SCALAR_INT:
1257  target->set_int(source->get_int());
1258  break;
1259  case SCALAR_REAL:
1260  target->set_real(source->get_real());
1261  break;
1262  case SCALAR_BOOL:
1263  target->set_bool(source->get_bool());
1264  break;
1265  case VECTOR_BOOL:
1266  {
1267  bool* v=NULL;
1268  int32_t len=0;
1269  source->get_vector(v, len);
1270  target->set_vector(v, len);
1271  SG_FREE(v);
1272  break;
1273  }
1274  case VECTOR_BYTE:
1275  {
1276  uint8_t* v=NULL;
1277  int32_t len=0;
1278  source->get_vector(v, len);
1279  target->set_vector(v, len);
1280  SG_FREE(v);
1281  break;
1282  }
1283  case VECTOR_CHAR:
1284  {
1285  char* v=NULL;
1286  int32_t len=0;
1287  source->get_vector(v, len);
1288  target->set_vector(v, len);
1289  SG_FREE(v);
1290  break;
1291  }
1292  case VECTOR_INT:
1293  {
1294  int32_t* v=NULL;
1295  int32_t len=0;
1296  source->get_vector(v, len);
1297  target->set_vector(v, len);
1298  SG_FREE(v);
1299  break;
1300  }
1301  case VECTOR_REAL:
1302  {
1303  float64_t* v=NULL;
1304  int32_t len=0;
1305  source->get_vector(v, len);
1306  target->set_vector(v, len);
1307  SG_FREE(v);
1308  break;
1309  }
1310  case VECTOR_SHORTREAL:
1311  {
1312  float32_t* v=NULL;
1313  int32_t len=0;
1314  source->get_vector(v, len);
1315  target->set_vector(v, len);
1316  SG_FREE(v);
1317  break;
1318  }
1319  case VECTOR_SHORT:
1320  {
1321  int16_t* v=NULL;
1322  int32_t len=0;
1323  source->get_vector(v, len);
1324  target->set_vector(v, len);
1325  SG_FREE(v);
1326  break;
1327  }
1328  case VECTOR_WORD:
1329  {
1330  uint16_t* v=NULL;
1331  int32_t len=0;
1332  source->get_vector(v, len);
1333  target->set_vector(v, len);
1334  SG_FREE(v);
1335  break;
1336  }
1337 
1338  case STRING_BYTE:
1339  {
1340  int32_t num_str=0;
1341  int32_t max_str_len=0;
1342  SGString<uint8_t>* strs=NULL;
1343  source->get_string_list(strs, num_str, max_str_len);
1344  target->set_string_list(strs, num_str);
1345  SG_FREE(strs);
1346  break;
1347  }
1348  case STRING_CHAR:
1349  {
1350  int32_t num_str=0;
1351  int32_t max_str_len=0;
1352  SGString<char>* strs;
1353  source->get_string_list(strs, num_str,max_str_len);
1354  target->set_string_list(strs, num_str);
1355  SG_FREE(strs);
1356  break;
1357  }
1358  case STRING_INT:
1359  {
1360  int32_t num_str=0;
1361  int32_t max_str_len=0;
1362  SGString<int32_t>* strs;
1363  source->get_string_list(strs, num_str,max_str_len);
1364  target->set_string_list(strs, num_str);
1365  SG_FREE(strs);
1366  break;
1367  }
1368  case STRING_SHORT:
1369  {
1370  int32_t num_str=0;
1371  int32_t max_str_len=0;
1372  SGString<int16_t>* strs=NULL;
1373  source->get_string_list(strs, num_str, max_str_len);
1374  target->set_string_list(strs, num_str);
1375  SG_FREE(strs);
1376  break;
1377  }
1378  case STRING_WORD:
1379  {
1380  int32_t num_str=0;
1381  int32_t max_str_len=0;
1382  SGString<uint16_t>* strs=NULL;
1383  source->get_string_list(strs, num_str, max_str_len);
1384  target->set_string_list(strs, num_str);
1385  SG_FREE(strs);
1386  break;
1387  }
1388  case DENSE_INT:
1389  {
1390  int32_t num_feat=0;
1391  int32_t num_vec=0;
1392  int32_t* fmatrix=NULL;
1393  source->get_matrix(fmatrix, num_feat, num_vec);
1394  target->set_matrix(fmatrix, num_feat, num_vec);
1395  SG_FREE(fmatrix);
1396  break;
1397  }
1398  case DENSE_REAL:
1399  {
1400  int32_t num_feat=0;
1401  int32_t num_vec=0;
1402  float64_t* fmatrix=NULL;
1403  source->get_matrix(fmatrix, num_feat, num_vec);
1404  target->set_matrix(fmatrix, num_feat, num_vec);
1405  SG_FREE(fmatrix);
1406  break;
1407  }
1408  case DENSE_SHORT:
1409  {
1410  int32_t num_feat=0;
1411  int32_t num_vec=0;
1412  int16_t* fmatrix=NULL;
1413  source->get_matrix(fmatrix, num_feat, num_vec);
1414  target->set_matrix(fmatrix, num_feat, num_vec);
1415  SG_FREE(fmatrix);
1416  break;
1417  }
1418  case DENSE_SHORTREAL:
1419  {
1420  int32_t num_feat=0;
1421  int32_t num_vec=0;
1422  float32_t* fmatrix=NULL;
1423  source->get_matrix(fmatrix, num_feat, num_vec);
1424  target->set_matrix(fmatrix, num_feat, num_vec);
1425  SG_FREE(fmatrix);
1426  break;
1427  }
1428  case DENSE_WORD:
1429  {
1430  int32_t num_feat=0;
1431  int32_t num_vec=0;
1432  uint16_t* fmatrix=NULL;
1433  source->get_matrix(fmatrix, num_feat, num_vec);
1434  target->set_matrix(fmatrix, num_feat, num_vec);
1435  SG_FREE(fmatrix);
1436  break;
1437  }
1438  /*
1439  case NDARRAY_BYTE:
1440  {
1441  uint8_t* a=NULL;
1442  int32_t* dims=NULL;
1443  int32_t num_dims=0;
1444  source->get_ndarray(a, dims, num_dims);
1445  target->set_ndarray(a, dims, num_dims);
1446  SG_FREE(a);
1447  SG_FREE(dims);
1448  break;
1449  }
1450  case NDARRAY_CHAR:
1451  {
1452  char* a=NULL;
1453  int32_t* dims=NULL;
1454  int32_t num_dims=0;
1455  source->get_ndarray(a, dims, num_dims);
1456  target->set_ndarray(a, dims, num_dims);
1457  SG_FREE(a);
1458  SG_FREE(dims);
1459  break;
1460  }
1461  case NDARRAY_INT:
1462  {
1463  int32_t* a=NULL;
1464  int32_t* dims=NULL;
1465  int32_t num_dims=0;
1466  source->get_ndarray(a, dims, num_dims);
1467  target->set_ndarray(a, dims, num_dims);
1468  SG_FREE(a);
1469  SG_FREE(dims);
1470  break;
1471  }
1472  case NDARRAY_REAL:
1473  {
1474  float64_t* a=NULL;
1475  int32_t* dims=NULL;
1476  int32_t num_dims=0;
1477  source->get_ndarray(a, dims, num_dims);
1478  target->set_ndarray(a, dims, num_dims);
1479  SG_FREE(a);
1480  SG_FREE(dims);
1481  break;
1482  }
1483  case NDARRAY_SHORTREAL:
1484  {
1485  float32_t* a=NULL;
1486  int32_t* dims=NULL;
1487  int32_t num_dims=0;
1488  source->get_ndarray(a, dims, num_dims);
1489  target->set_ndarray(a, dims, num_dims);
1490  SG_FREE(a);
1491  SG_FREE(dims);
1492  break;
1493  }
1494  case NDARRAY_SHORT:
1495  {
1496  int16_t* a=NULL;
1497  int32_t* dims=NULL;
1498  int32_t num_dims=0;
1499  source->get_ndarray(a, dims, num_dims);
1500  target->set_ndarray(a, dims, num_dims);
1501  SG_FREE(a);
1502  SG_FREE(dims);
1503  break;
1504  }
1505  case NDARRAY_WORD:
1506  {
1507  uint16_t* a=NULL;
1508  int32_t* dims=NULL;
1509  int32_t num_dims=0;
1510  source->get_ndarray(a, dims, num_dims);
1511  target->set_ndarray(a, dims, num_dims);
1512  SG_FREE(a);
1513  SG_FREE(dims);
1514  break;
1515  }*/
1516  case SPARSE_REAL:
1517  {
1518  int32_t num_feat=0;
1519  int32_t num_vec=0;
1520  SGSparseVector<float64_t>* fmatrix=NULL;
1521  source->get_sparse_matrix(fmatrix, num_feat, num_vec);
1522  int64_t nnz=0;
1523  for (int32_t i=0; i<num_vec; i++)
1524  nnz+=fmatrix[i].num_feat_entries;
1525  target->set_sparse_matrix(fmatrix, num_feat, num_vec, nnz);
1526  SG_FREE(fmatrix);
1527  break;
1528  }
1529 
1530  default:
1531  SG_ERROR("unknown return type")
1532  break;
1533  }
1534 }
1535 
1537 // commands
1539 
1540 /* Features */
1541 
1542 bool CSGInterface::cmd_load_features()
1543 {
1544  if (m_nrhs<8 || !create_return_values(0))
1545  return false;
1546 
1547  int32_t len=0;
1548  char* filename=get_str_from_str_or_direct(len);
1549  char* fclass=get_str_from_str_or_direct(len);
1550  char* type=get_str_from_str_or_direct(len);
1551  char* target=get_str_from_str_or_direct(len);
1552  int32_t size=get_int_from_int_or_str();
1553  int32_t comp_features=get_int_from_int_or_str();
1554 
1555  bool success=ui_features->load(
1556  filename, fclass, type, target, size, comp_features);
1557 
1558  SG_FREE(filename);
1559  SG_FREE(fclass);
1560  SG_FREE(type);
1561  SG_FREE(target);
1562  return success;
1563 }
1564 
1565 bool CSGInterface::cmd_save_features()
1566 {
1567  if (m_nrhs<5 || !create_return_values(0))
1568  return false;
1569 
1570  int32_t len=0;
1571  char* filename=get_str_from_str_or_direct(len);
1572  char* type=get_str_from_str_or_direct(len);
1573  char* target=get_str_from_str_or_direct(len);
1574 
1575  bool success=ui_features->save(filename, type, target);
1576 
1577  SG_FREE(filename);
1578  SG_FREE(type);
1579  SG_FREE(target);
1580  return success;
1581 }
1582 
1583 bool CSGInterface::cmd_clean_features()
1584 {
1585  if (m_nrhs<2 || !create_return_values(0))
1586  return false;
1587 
1588  int32_t len=0;
1589  char* target=get_str_from_str_or_direct(len);
1590 
1591  bool success=ui_features->clean(target);
1592 
1593  SG_FREE(target);
1594  return success;
1595 }
1596 
1597 bool CSGInterface::cmd_get_features()
1598 {
1599  if (m_nrhs!=2 || !create_return_values(1))
1600  return false;
1601 
1602  int32_t tlen=0;
1603  char* target=get_string(tlen);
1604  CFeatures* feat=NULL;
1605 
1606  if (strmatch(target, "TRAIN"))
1607  feat=ui_features->get_train_features();
1608  else if (strmatch(target, "TEST"))
1609  feat=ui_features->get_test_features();
1610  else
1611  {
1612  SG_FREE(target);
1613  SG_ERROR("Unknown target, neither TRAIN nor TEST.\n")
1614  }
1615  SG_FREE(target);
1616 
1617  ASSERT(feat)
1618 
1619  switch (feat->get_feature_class())
1620  {
1621  case C_DENSE:
1622  {
1623  int32_t num_feat=0;
1624  int32_t num_vec=0;
1625 
1626  switch (feat->get_feature_type())
1627  {
1628  case F_BYTE:
1629  {
1630  uint8_t* fmatrix=((CDenseFeatures<uint8_t> *) feat)->get_feature_matrix(num_feat, num_vec);
1631  set_matrix(fmatrix, num_feat, num_vec);
1632  break;
1633  }
1634 
1635  case F_CHAR:
1636  {
1637  char* fmatrix=((CDenseFeatures<char> *) feat)->get_feature_matrix(num_feat, num_vec);
1638  set_matrix(fmatrix, num_feat, num_vec);
1639  break;
1640  }
1641 
1642  case F_DREAL:
1643  {
1644  float64_t* fmatrix=((CDenseFeatures<float64_t> *) feat)->get_feature_matrix(num_feat, num_vec);
1645  set_matrix(fmatrix, num_feat, num_vec);
1646  break;
1647  }
1648 
1649  case F_INT:
1650  {
1651  int32_t* fmatrix=((CDenseFeatures<int32_t> *) feat)->get_feature_matrix(num_feat, num_vec);
1652  set_matrix(fmatrix, num_feat, num_vec);
1653  break;
1654  }
1655 
1656  case F_SHORT:
1657  {
1658  int16_t* fmatrix=((CDenseFeatures<int16_t> *) feat)->get_feature_matrix(num_feat, num_vec);
1659  set_matrix(fmatrix, num_feat, num_vec);
1660  break;
1661  }
1662 
1663  case F_SHORTREAL:
1664  {
1665  float32_t* fmatrix=((CDenseFeatures<float32_t> *) feat)->get_feature_matrix(num_feat, num_vec);
1666  set_matrix(fmatrix, num_feat, num_vec);
1667  break;
1668  }
1669 
1670  case F_WORD:
1671  {
1672  uint16_t* fmatrix=((CDenseFeatures<uint16_t> *) feat)->get_feature_matrix(num_feat, num_vec);
1673  set_matrix(fmatrix, num_feat, num_vec);
1674  break;
1675  }
1676 
1677  default:
1679  }
1680  break;
1681  }
1682 
1683  case C_SPARSE:
1684  {
1685  switch (feat->get_feature_type())
1686  {
1687  case F_DREAL:
1688  {
1689  int64_t nnz=((CSparseFeatures<float64_t>*) feat)->
1690  get_num_nonzero_entries();
1691  SGSparseMatrix<float64_t> fmatrix = ((CSparseFeatures<float64_t>*) feat)->get_sparse_feature_matrix();
1692  SG_INFO("sparse matrix has %d feats, %d vecs and %d nnz elemements\n", fmatrix.num_features, fmatrix.num_vectors, nnz)
1693 
1694  set_sparse_matrix(fmatrix.sparse_matrix, fmatrix.num_features, fmatrix.num_vectors, nnz);
1695  break;
1696  }
1697 
1698  default:
1700  }
1701  break;
1702  }
1703 
1704  case C_STRING:
1705  {
1706  int32_t num_str=0;
1707  int32_t max_str_len=0;
1708  switch (feat->get_feature_type())
1709  {
1710  case F_BYTE:
1711  {
1712  SGString<uint8_t>* fmatrix=((CStringFeatures<uint8_t>*) feat)->get_features(num_str, max_str_len);
1713  set_string_list(fmatrix, num_str);
1714  break;
1715  }
1716 
1717  case F_CHAR:
1718  {
1719  SGString<char>* fmatrix=((CStringFeatures<char>*) feat)->get_features(num_str, max_str_len);
1720  set_string_list(fmatrix, num_str);
1721  break;
1722  }
1723 
1724  case F_WORD:
1725  {
1726  SGString<uint16_t>* fmatrix=((CStringFeatures<uint16_t>*) feat)->get_features(num_str, max_str_len);
1727  set_string_list(fmatrix, num_str);
1728  break;
1729  }
1730 
1731  default:
1733  }
1734  break;
1735  }
1736 
1737  case C_WD:
1738  case C_WEIGHTEDSPEC:
1739  case C_SPEC:
1740  case C_COMBINED_DOT:
1741  case C_POLY:
1742  {
1743 
1744  SGMatrix<float64_t> fmatrix = ((CDotFeatures*) feat)->get_computed_dot_feature_matrix();
1745  set_matrix(fmatrix.matrix, fmatrix.num_cols, fmatrix.num_rows);
1746  break;
1747  }
1748 
1749  default:
1751  }
1752 
1753  return true;
1754 }
1755 
1756 bool CSGInterface::cmd_add_features()
1757 {
1758  if (m_nrhs<3 || !create_return_values(0))
1759  return false;
1760 
1761  return do_set_features(true, false);
1762 }
1763 
1764 bool CSGInterface::cmd_add_multiple_features()
1765 {
1766  if ((m_nrhs!=4 && m_nrhs<5) || !create_return_values(0))
1767  return false;
1768 
1769  int32_t repetitions=get_int();
1770 
1771  ASSERT(repetitions>=1)
1772 
1773  return do_set_features(true, false, repetitions);
1774 }
1775 
1776 bool CSGInterface::cmd_add_dotfeatures()
1777 {
1778  if (m_nrhs<3 || !create_return_values(0))
1779  return false;
1780 
1781  return do_set_features(true, true);
1782 }
1783 
1784 bool CSGInterface::cmd_set_features()
1785 {
1786  if (m_nrhs<3 || !create_return_values(0))
1787  return false;
1788 
1789  return do_set_features(false, false);
1790 }
1791 
1792 bool CSGInterface::do_set_features(bool add, bool check_dot, int32_t repetitions)
1793 {
1794  int32_t tlen=0;
1795  char* target=get_string(tlen);
1796  if (!strmatch(target, "TRAIN") && !strmatch(target, "TEST"))
1797  {
1798  SG_FREE(target);
1799  SG_ERROR("Unknown target, neither TRAIN nor TEST.\n")
1800  }
1801 
1802  CFeatures* feat=NULL;
1803  int32_t num_feat=0;
1804  int32_t num_vec=0;
1805 
1806  switch (get_argument_type())
1807  {
1808  case SPARSE_REAL:
1809  {
1810  SGSparseVector<float64_t>* fmatrix=NULL;
1811  get_sparse_matrix(fmatrix, num_feat, num_vec);
1812 
1813  feat=new CSparseFeatures<float64_t>(SGSparseMatrix<float64_t>(fmatrix, num_feat, num_vec));
1814  break;
1815  }
1816 
1817  case DENSE_REAL:
1818  {
1819  float64_t* fmatrix=NULL;
1820  get_matrix(fmatrix, num_feat, num_vec);
1821 
1822  feat=new CDenseFeatures<float64_t>(0);
1823  ((CDenseFeatures<float64_t>*) feat)->
1824  set_feature_matrix(SGMatrix<float64_t>(fmatrix, num_feat, num_vec));
1825 
1826  if (m_nrhs==6)
1827  feat = create_custom_real_features((CDenseFeatures<float64_t>*) feat);
1828 
1829  break;
1830  }
1831 
1832  case DENSE_INT:
1833  {
1834  int32_t* fmatrix=NULL;
1835  get_matrix(fmatrix, num_feat, num_vec);
1836 
1837  feat=new CDenseFeatures<int32_t>(0);
1838  ((CDenseFeatures<int32_t>*) feat)->
1839  set_feature_matrix(SGMatrix<int32_t>(fmatrix, num_feat, num_vec));
1840  break;
1841  }
1842 
1843  case DENSE_SHORT:
1844  {
1845  int16_t* fmatrix=NULL;
1846  get_matrix(fmatrix, num_feat, num_vec);
1847 
1848  feat=new CDenseFeatures<int16_t>(0);
1849  ((CDenseFeatures<int16_t>*) feat)->
1850  set_feature_matrix(SGMatrix<int16_t>(fmatrix, num_feat, num_vec));
1851  break;
1852  }
1853 
1854  case DENSE_WORD:
1855  {
1856  uint16_t* fmatrix=NULL;
1857  get_matrix(fmatrix, num_feat, num_vec);
1858 
1859  feat=new CDenseFeatures<uint16_t>(0);
1860  ((CDenseFeatures<uint16_t>*) feat)->
1861  set_feature_matrix(SGMatrix<uint16_t>(fmatrix, num_feat, num_vec));
1862  break;
1863  }
1864 
1865  case DENSE_SHORTREAL:
1866  {
1867  float32_t* fmatrix=NULL;
1868  get_matrix(fmatrix, num_feat, num_vec);
1869 
1870  feat=new CDenseFeatures<float32_t>(0);
1871  ((CDenseFeatures<float32_t>*) feat)->
1872  set_feature_matrix(SGMatrix<float32_t>(fmatrix, num_feat, num_vec));
1873  break;
1874  }
1875 
1876  case STRING_CHAR:
1877  {
1878  if (m_nrhs<4)
1879  SG_ERROR("Please specify alphabet!\n")
1880 
1881  int32_t num_str=0;
1882  int32_t max_str_len=0;
1883  SGString<char>* fmatrix=NULL;
1884  get_string_list(fmatrix, num_str, max_str_len);
1885 
1886  int32_t alphabet_len=0;
1887  char* alphabet_str=get_string(alphabet_len);
1888  ASSERT(alphabet_str)
1889 
1890  if (strmatch(alphabet_str, "DNABINFILE"))
1891  {
1892  SG_FREE(alphabet_str);
1893 
1894  ASSERT(fmatrix[0].string)
1895  feat=new CStringFeatures<uint8_t>(DNA);
1896 
1897  try
1898  {
1899  ((CStringFeatures<uint8_t>*) feat)->load_ascii_file(fmatrix[0].string);
1900  }
1901  catch (...)
1902  {
1903  SG_UNREF(feat);
1904  SG_ERROR("Couldn't load DNA features from file.\n")
1905  }
1906  feat=create_custom_string_features((CStringFeatures<uint8_t>*) feat);
1907  break;
1908  }
1909  else
1910  {
1911  bool convert_to_word=false;
1912  bool convert_to_ulong=false;
1913  CAlphabet* alphabet=NULL;
1914  if (strmatch(alphabet_str, "DNAWORD"))
1915  {
1916  alphabet=new CAlphabet(DNA);
1917  convert_to_word=true;
1918  }
1919  else if (strmatch(alphabet_str, "DNAULONG"))
1920  {
1921  alphabet=new CAlphabet(DNA);
1922  convert_to_ulong=true;
1923  }
1924  else
1925  alphabet=new CAlphabet(alphabet_str, alphabet_len);
1926 
1927  SG_REF(alphabet);
1928  SG_FREE(alphabet_str);
1929 
1930  feat=new CStringFeatures<char>(alphabet);
1931 
1932  if (!((CStringFeatures<char>*) feat)->set_features(fmatrix, num_str, max_str_len))
1933  {
1934  SG_UNREF(alphabet);
1935  SG_UNREF(feat);
1936  SG_ERROR("Couldnt set byte string features.\n")
1937  }
1938 
1939  SG_UNREF(alphabet);
1940 
1941  if (convert_to_word || convert_to_ulong)
1942  convert_to_bitembedding(feat, convert_to_word, convert_to_ulong);
1943  }
1944 
1945  obtain_from_single_string(feat);
1946  break;
1947  }
1948 
1949  case STRING_BYTE:
1950  {
1951  if (m_nrhs<4)
1952  SG_ERROR("Please specify alphabet!\n")
1953 
1954  int32_t num_str=0;
1955  int32_t max_str_len=0;
1956  SGString<uint8_t>* fmatrix=NULL;
1957  get_string_list(fmatrix, num_str, max_str_len);
1958 
1959  int32_t alphabet_len=0;
1960  char* alphabet_str=get_string(alphabet_len);
1961  ASSERT(alphabet_str)
1962  CAlphabet* alphabet=NULL;
1963  alphabet=new CAlphabet(alphabet_str, alphabet_len);
1964  SG_FREE(alphabet_str);
1965 
1966  feat=new CStringFeatures<uint8_t>(alphabet);
1967  if (!((CStringFeatures<uint8_t>*) feat)->set_features(fmatrix, num_str, max_str_len))
1968  {
1969  SG_UNREF(alphabet);
1970  SG_UNREF(feat);
1971  SG_ERROR("Couldnt set byte string features.\n")
1972  }
1973  feat=create_custom_string_features((CStringFeatures<uint8_t>*) feat);
1974  break;
1975  }
1976 
1977  default:
1978  SG_ERROR("Wrong argument type %d.\n", get_argument_type())
1979  }
1980 
1981  if (check_dot && !feat->has_property(FP_DOT))
1982  {
1983  SG_UNREF(feat);
1984  SG_ERROR("Feature type not supported by DOT Features\n")
1985  }
1986 
1987  if (strmatch(target, "TRAIN"))
1988  {
1989  if (!add)
1990  ui_features->set_train_features(feat);
1991  else if (check_dot)
1992  {
1993  for (int32_t i=0; i<repetitions; i++)
1994  ui_features->add_train_dotfeatures((CDotFeatures*) feat);
1995  }
1996  else
1997  {
1998  for (int32_t i=0; i<repetitions; i++)
1999  ui_features->add_train_features(feat);
2000  }
2001  }
2002  else
2003  {
2004  if (!add)
2005  ui_features->set_test_features(feat);
2006  else if (check_dot)
2007  {
2008  for (int32_t i=0; i<repetitions; i++)
2009  ui_features->add_test_dotfeatures((CDotFeatures*) feat);
2010  }
2011  else
2012  {
2013  for (int32_t i=0; i<repetitions; i++)
2014  ui_features->add_test_features(feat);
2015  }
2016  }
2017 
2018  SG_FREE(target);
2019 
2020  return true;
2021 }
2022 
2023 bool CSGInterface::cmd_set_reference_features()
2024 {
2025  if (m_nrhs<3 || !create_return_values(0))
2026  return false;
2027 
2028  int32_t len=0;
2029  char* target=get_str_from_str_or_direct(len);
2030 
2031  bool success=ui_features->set_reference_features(target);
2032 
2033  SG_FREE(target);
2034  return success;
2035 }
2036 
2037 bool CSGInterface::cmd_del_last_features()
2038 {
2039  if (m_nrhs<2 || !create_return_values(0))
2040  return false;
2041 
2042  int32_t len=0;
2043  char* target=get_str_from_str_or_direct(len);
2044  bool success=ui_features->del_last_feature_obj(target);
2045 
2046  SG_FREE(target);
2047  return success;
2048 }
2049 
2050 bool CSGInterface::cmd_convert()
2051 {
2052  if (m_nrhs<5 || !create_return_values(0))
2053  return false;
2054 
2055  int32_t len=0;
2056  char* target=get_str_from_str_or_direct(len);
2057  CFeatures* features=ui_features->get_convert_features(target);
2058  if (!features)
2059  {
2060  SG_FREE(target);
2061  SG_ERROR("No \"%s\" features available.\n", target)
2062  }
2063 
2064  char* from_class=get_str_from_str_or_direct(len);
2065  char* from_type=get_str_from_str_or_direct(len);
2066  char* to_class=get_str_from_str_or_direct(len);
2067  char* to_type=get_str_from_str_or_direct(len);
2068 
2069  CFeatures* result=NULL;
2070  if (strmatch(from_class, "SIMPLE"))
2071  {
2072  if (strmatch(from_type, "REAL"))
2073  {
2074  if (strmatch(to_class, "SPARSE") &&
2075  strmatch(to_type, "REAL"))
2076  {
2077  result=ui_features->convert_simple_real_to_sparse_real(
2078  ((CDenseFeatures<float64_t>*) features));
2079  }
2080  else
2082  } // from_type REAL
2083 
2084  else if (strmatch(from_type, "CHAR"))
2085  {
2086  if (strmatch(to_class, "STRING") &&
2087  strmatch(to_type, "CHAR"))
2088  {
2089  result=ui_features->convert_simple_char_to_string_char(
2090  ((CDenseFeatures<char>*) features));
2091  }
2092  else if (strmatch(to_class, "SIMPLE"))
2093  {
2094  if (strmatch(to_type, "ALIGN") && m_nrhs==8)
2095  {
2096  float64_t gap_cost=get_real_from_real_or_str();
2097  result=ui_features->convert_simple_char_to_simple_align(
2098  (CDenseFeatures<char>*) features, gap_cost);
2099  }
2100  else
2102  }
2103  else
2105  } // from_type CHAR
2106 
2107  else if (strmatch(from_type, "WORD"))
2108  {
2109  if (strmatch(to_class, "SIMPLE") &&
2110  strmatch(to_type, "SALZBERG"))
2111  {
2112  result=ui_features->convert_simple_word_to_simple_salzberg(
2113  (CDenseFeatures<uint16_t>*) features);
2114  }
2115  else
2117  } // from_type WORD
2118 
2119  else
2121  } // from_class SIMPLE
2122 
2123  else if (strmatch(from_class, "SPARSE"))
2124  {
2125  if (strmatch(from_type, "REAL"))
2126  {
2127  if (strmatch(to_class, "SIMPLE") &&
2128  strmatch(to_type, "REAL"))
2129  {
2130  result=ui_features->convert_sparse_real_to_simple_real(
2131  (CSparseFeatures<float64_t>*) features);
2132  }
2133  else
2135  } // from_type REAL
2136  else
2138  } // from_class SPARSE
2139 
2140  else if (strmatch(from_class, "STRING"))
2141  {
2142  if (strmatch(from_type, "CHAR"))
2143  {
2144  if (strmatch(to_class, "STRING"))
2145  {
2146  int32_t order=1;
2147  int32_t start=0;
2148  int32_t gap=0;
2149  char rev='f';
2150 
2151  if (m_nrhs>6)
2152  {
2153  order=get_int_from_int_or_str();
2154 
2155  if (m_nrhs>7)
2156  {
2157  start=get_int_from_int_or_str();
2158 
2159  if (m_nrhs>8)
2160  {
2161  gap=get_int_from_int_or_str();
2162 
2163  if (m_nrhs>9)
2164  {
2165  char* rev_str=get_str_from_str_or_direct(len);
2166  if (rev_str)
2167  rev=rev_str[0];
2168 
2169  SG_FREE(rev_str);
2170  }
2171  }
2172  }
2173  }
2174 
2175  if (strmatch(to_type, "BYTE"))
2176  {
2177  result=ui_features->convert_string_char_to_string_generic<char,uint8_t>(
2178  (CStringFeatures<char>*) features, order, start,
2179  gap, rev);
2180  }
2181  else if (strmatch(to_type, "WORD"))
2182  {
2183  result=ui_features->convert_string_char_to_string_generic<char,uint16_t>(
2184  (CStringFeatures<char>*) features, order, start,
2185  gap, rev);
2186  }
2187  else if (strmatch(to_type, "ULONG"))
2188  {
2189  result=ui_features->convert_string_char_to_string_generic<char,uint64_t>(
2190  (CStringFeatures<char>*) features, order, start,
2191  gap, rev);
2192  }
2193  else
2195  }
2196  else
2198  } // from_type CHAR
2199 
2200  else if (strmatch(from_type, "BYTE"))
2201  {
2202  if (strmatch(to_class, "STRING"))
2203  {
2204  int32_t order=1;
2205  int32_t start=0;
2206  int32_t gap=0;
2207  char rev='f';
2208 
2209  if (m_nrhs>6)
2210  {
2211  order=get_int_from_int_or_str();
2212 
2213  if (m_nrhs>7)
2214  {
2215  start=get_int_from_int_or_str();
2216 
2217  if (m_nrhs>8)
2218  {
2219  gap=get_int_from_int_or_str();
2220 
2221  if (m_nrhs>9)
2222  {
2223  char* rev_str=get_str_from_str_or_direct(len);
2224  if (rev_str)
2225  rev=rev_str[0];
2226 
2227  SG_FREE(rev_str);
2228  }
2229  }
2230  }
2231  }
2232 
2233  if (strmatch(to_type, "WORD"))
2234  {
2235  result=ui_features->convert_string_char_to_string_generic<uint8_t,uint16_t>(
2236  (CStringFeatures<uint8_t>*) features, order, start,
2237  gap, rev);
2238  }
2239  else if (strmatch(to_type, "ULONG"))
2240  {
2241  result=ui_features->convert_string_char_to_string_generic<uint8_t,uint64_t>(
2242  (CStringFeatures<uint8_t>*) features, order, start,
2243  gap, rev);
2244  }
2245  else
2247  }
2248  else
2250  } // from_type uint8_t
2251 
2252  else if (strmatch(from_type, "WORD"))
2253  {
2254  if (strmatch(to_class, "SIMPLE") && strmatch(to_type, "TOP"))
2255  {
2256  result=ui_features->convert_string_word_to_simple_top(
2257  (CStringFeatures<uint16_t>*) features);
2258  }
2259  else if (strmatch(to_class, "SPEC") && strmatch(to_type, "WORD") && m_nrhs==7)
2260  {
2261  bool use_norm=get_bool();
2262  result=ui_features->convert_string_byte_to_spec_word((CStringFeatures<uint16_t>*) features, use_norm);
2263 
2264  }
2265  else
2267  } // from_type WORD
2268 
2269  else if (strmatch(to_class, "SIMPLE") && strmatch(to_type, "FK"))
2270  {
2271  result=ui_features->convert_string_word_to_simple_fk(
2272  (CStringFeatures<uint16_t>*) features);
2273  } // to_type FK
2274 
2275  else
2277 
2278  } // from_class STRING
2279 
2280  if (result && ui_features->set_convert_features(result, target))
2281  SG_INFO("Conversion was successful.\n")
2282  else
2283  SG_ERROR("Conversion failed.\n")
2284 
2285  SG_FREE(target);
2286  SG_FREE(from_class);
2287  SG_FREE(from_type);
2288  SG_FREE(to_class);
2289  SG_FREE(to_type);
2290  return (result!=NULL);
2291 }
2292 
2293 void CSGInterface::convert_to_bitembedding(CFeatures* &features, bool convert_to_word, bool convert_to_ulong)
2294 {
2295  int32_t order=1;
2296  int32_t start=0;
2297  int32_t gap=0;
2298  char rev='f';
2299 
2300  if (m_nrhs<5)
2301  return;
2302 
2303  order=get_int();
2304  // remove arg, for parameters to come
2305  m_nrhs--;
2306 
2307  if (convert_to_word)
2308  {
2309  SG_INFO("Converting into word-bitembedding\n")
2310  features=ui_features->convert_string_char_to_string_generic<char,uint16_t>(
2311  (CStringFeatures<char>*) features, order, start, gap, rev);
2312  }
2313 
2314  if (convert_to_ulong)
2315  {
2316  SG_INFO("Converting into ulong-bitembedding\n")
2317  features=ui_features->convert_string_char_to_string_generic<char,uint64_t>(
2318  (CStringFeatures<char>*) features, order, start, gap, rev);
2319  }
2320 }
2321 
2322 void CSGInterface::obtain_from_single_string(CFeatures* features)
2323 {
2324  if (m_nrhs<5)
2325  return;
2326 
2327  int32_t len=0;
2328  char* str=get_string(len);
2329  ASSERT(str)
2330 
2331  if (strmatch(str, "from_position_list"))
2332  {
2333  obtain_from_position_list(features);
2334  }
2335  else if (strmatch(str, "slide_window"))
2336  {
2337  obtain_by_sliding_window(features);
2338  }
2339  else
2340  SG_SERROR("Unknown conversion\n")
2341 }
2342 
2343 bool CSGInterface::obtain_from_position_list(CFeatures* features)
2344 {
2345  int32_t winsize=get_int();
2346 
2347  int32_t* shifts=NULL;
2348  int32_t num_shift=0;
2349  get_vector(shifts, num_shift);
2350 
2351  int32_t skip=0;
2352  if (m_nrhs==8)
2353  skip=get_int();
2354 
2355  SG_DEBUG("winsize: %d num_shifts: %d skip: %d\n", winsize, num_shift, skip)
2356 
2357  CDynamicArray<int32_t> positions(num_shift+1);
2358 
2359  for (int32_t i=0; i<num_shift; i++)
2360  positions.set_element(shifts[i], i);
2361 
2362  if (features->get_feature_class()!=C_STRING)
2363  SG_ERROR("No string features.\n")
2364 
2365  bool success=false;
2366  switch (features->get_feature_type())
2367  {
2368  case F_CHAR:
2369  {
2370  success=(((CStringFeatures<char>*) features)->
2371  obtain_by_position_list(winsize, &positions, skip)>0);
2372  break;
2373  }
2374  case F_BYTE:
2375  {
2376  success=(((CStringFeatures<uint8_t>*) features)->
2377  obtain_by_position_list(winsize, &positions, skip)>0);
2378  break;
2379  }
2380  case F_WORD:
2381  {
2382  success=(((CStringFeatures<uint16_t>*) features)->
2383  obtain_by_position_list(winsize, &positions, skip)>0);
2384  break;
2385  }
2386  case F_ULONG:
2387  {
2388  success=(((CStringFeatures<uint64_t>*) features)->
2389  obtain_by_position_list(winsize, &positions, skip)>0);
2390  break;
2391  }
2392  default:
2393  SG_ERROR("Unsupported string features type.\n")
2394  }
2395 
2396  return success;
2397 }
2398 
2399 bool CSGInterface::obtain_by_sliding_window(CFeatures* features)
2400 {
2401  int32_t winsize=get_int();
2402  int32_t shift=get_int();
2403  int32_t skip=0;
2404 
2405  if (m_nrhs==8)
2406  skip=get_int();
2407 
2408  bool success=false;
2409 
2410  ASSERT(features)
2411  ASSERT(((CFeatures*) features)->get_feature_class()==C_STRING)
2412 
2413  switch (features->get_feature_type())
2414  {
2415  case F_CHAR:
2416  return ( ((CStringFeatures<char>*) features)->obtain_by_sliding_window(winsize, shift, skip)>0);
2417  case F_BYTE:
2418  return ( ((CStringFeatures<uint8_t>*) features)->obtain_by_sliding_window(winsize, shift, skip)>0);
2419  case F_WORD:
2420  return ( ((CStringFeatures<uint16_t>*) features)->obtain_by_sliding_window(winsize, shift, skip)>0);
2421  case F_ULONG:
2422  return ( ((CStringFeatures<uint64_t>*) features)->obtain_by_sliding_window(winsize, shift, skip)>0);
2423  default:
2424  SG_SERROR("Unsupported string features type.\n")
2425  return false;
2426  }
2427 
2428  return success;
2429 }
2430 
2431 bool CSGInterface::cmd_reshape()
2432 {
2433  if (m_nrhs<4 || !create_return_values(0))
2434  return false;
2435 
2436  int32_t len=0;
2437  char* target=get_str_from_str_or_direct(len);
2438  int32_t num_feat=get_int_from_int_or_str();
2439  int32_t num_vec=get_int_from_int_or_str();
2440 
2441  bool success=ui_features->reshape(target, num_feat, num_vec);
2442 
2443  SG_FREE(target);
2444  return success;
2445 }
2446 
2447 bool CSGInterface::cmd_load_labels()
2448 {
2449  if (m_nrhs<4 || !create_return_values(0))
2450  return false;
2451 
2452  int32_t len=0;
2453  char* filename=get_str_from_str_or_direct(len);
2454  char* target=get_str_from_str_or_direct(len);
2455 
2456  bool success=ui_labels->load(filename, target);
2457 
2458  SG_FREE(filename);
2459  SG_FREE(target);
2460  return success;
2461 }
2462 
2463 bool CSGInterface::cmd_set_labels()
2464 {
2465  if (m_nrhs!=3 || !create_return_values(0))
2466  return false;
2467 
2468  int32_t tlen=0;
2469  char* target=get_string(tlen);
2470  if (!strmatch(target, "TRAIN") && !strmatch(target, "TEST"))
2471  {
2472  SG_FREE(target);
2473  SG_ERROR("Unknown target, neither TRAIN nor TEST.\n")
2474  }
2475 
2476  float64_t* lab=NULL;
2477  int32_t len=0;
2478  get_vector(lab, len);
2479 
2480  CLabels* labels=ui_labels->infer_labels(lab, len);
2481 
2482  SG_INFO("num labels: %d\n", labels->get_num_labels())
2483 
2484  if (strmatch(target, "TRAIN"))
2485  ui_labels->set_train_labels(labels);
2486  else if (strmatch(target, "TEST"))
2487  ui_labels->set_test_labels(labels);
2488  else
2489  {
2490  SG_FREE(target);
2491  SG_ERROR("Unknown target, neither TRAIN nor TEST.\n")
2492  }
2493  SG_FREE(target);
2494 
2495  return true;
2496 }
2497 
2498 bool CSGInterface::cmd_get_labels()
2499 {
2500  if (m_nrhs!=2 || !create_return_values(1))
2501  return false;
2502 
2503  int32_t tlen=0;
2504  char* target=get_string(tlen);
2505  CLabels* labels=NULL;
2506 
2507  if (strmatch(target, "TRAIN"))
2508  labels=ui_labels->get_train_labels();
2509  else if (strmatch(target, "TEST"))
2510  labels=ui_labels->get_test_labels();
2511  else
2512  {
2513  SG_FREE(target);
2514  SG_ERROR("Unknown target, neither TRAIN nor TEST.\n")
2515  }
2516  SG_FREE(target);
2517 
2518  if (!labels)
2519  SG_ERROR("No labels.\n")
2520 
2521  //FIXME
2522  SGVector<float64_t> lab=((CBinaryLabels*) labels)->get_labels();
2523 
2524  set_vector(lab.vector, lab.vlen);
2525  return true;
2526 }
2527 
2528 
2531 bool CSGInterface::cmd_set_kernel_normalization()
2532 {
2533  if (m_nrhs<2 || !create_return_values(0))
2534  return false;
2535 
2536  int32_t len=0;
2537  char* normalization=get_string(len);
2538 
2539  float64_t c=0;
2540  float64_t r=0;
2541 
2542  if (m_nrhs>=3)
2543  c=get_real();
2544  if (m_nrhs>=4)
2545  r=get_real();
2546 
2547  bool success=ui_kernel->set_normalization(normalization, c, r);
2548 
2549  SG_FREE(normalization);
2550  return success;
2551 }
2552 
2553 bool CSGInterface::cmd_set_kernel()
2554 {
2555  if (m_nrhs<2 || !create_return_values(0))
2556  return false;
2557 
2558  SG_DEBUG("SGInterface: set_kernel\n")
2559  CKernel* kernel=create_kernel();
2560  return ui_kernel->set_kernel(kernel);
2561 }
2562 
2563 bool CSGInterface::cmd_add_kernel()
2564 {
2565  if (m_nrhs<3 || !create_return_values(0))
2566  return false;
2567 
2568  float64_t weight=get_real_from_real_or_str();
2569  // adjust m_nrhs to play well with checks in create_kernel
2570  m_nrhs--;
2571  CKernel* kernel=create_kernel();
2572 
2573  SG_DEBUG("SGInterface: add_kernel\n")
2574  return ui_kernel->add_kernel(kernel, weight);
2575 }
2576 
2577 bool CSGInterface::cmd_del_last_kernel()
2578 {
2579  if (m_nrhs<1 || !create_return_values(0))
2580  return false;
2581 
2582  return ui_kernel->del_last_kernel();
2583 }
2584 
2585 CKernel* CSGInterface::create_kernel()
2586 {
2587  CKernel* kernel=NULL;
2588  int32_t len=0;
2589  char* type=get_str_from_str_or_direct(len);
2590 
2591  SG_DEBUG("set_kernel with type: %s\n", type)
2592 
2593  if (strmatch(type, "COMBINED"))
2594  {
2595  if (m_nrhs<3)
2596  return NULL;
2597 
2598  int32_t size=get_int_from_int_or_str();
2599  bool append_subkernel_weights=false;
2600  if (m_nrhs>3)
2601  append_subkernel_weights=get_bool_from_bool_or_str();
2602 
2603  kernel=ui_kernel->create_combined(size, append_subkernel_weights);
2604  }
2605  else if (strmatch(type, "DISTANCE"))
2606  {
2607  if (m_nrhs<3)
2608  return NULL;
2609 
2610  int32_t size=get_int_from_int_or_str();
2611  float64_t width=1;
2612  if (m_nrhs>3)
2613  width=get_real_from_real_or_str();
2614 
2615  kernel=ui_kernel->create_distance(size, width);
2616  }
2617  else if (strmatch(type, "WAVELET"))
2618  {
2619 
2620  if (m_nrhs<4)
2621  return NULL;
2622 
2623  char* dtype=get_str_from_str_or_direct(len);
2624  if (strmatch(dtype, "REAL"))
2625  {
2626  int32_t size=get_int_from_int_or_str();
2627  float64_t Wdilation=5.0;
2628  float64_t Wtranslation=2.0;
2629 
2630  if (m_nrhs>4)
2631  {
2632  Wdilation=get_real_from_real_or_str();
2633 
2634  if (m_nrhs>5)
2635  Wtranslation=get_real_from_real_or_str();
2636  }
2637 
2638  kernel=ui_kernel->create_sigmoid(size, Wdilation, Wtranslation);
2639  }
2640 
2641  SG_FREE(dtype);
2642  }
2643  else if (strmatch(type, "LINEAR"))
2644  {
2645  if (m_nrhs<4)
2646  return NULL;
2647  if (m_nrhs>5)
2648  return NULL;
2649 
2650  char* dtype=get_str_from_str_or_direct(len);
2651  int32_t size=get_int_from_int_or_str();
2652  float64_t scale=-1;
2653  if (m_nrhs==5)
2654  scale=get_real_from_real_or_str();
2655 
2656  if (strmatch(dtype, "BYTE"))
2657  kernel=ui_kernel->create_linearbyte(size, scale);
2658  else if (strmatch(dtype, "WORD"))
2659  kernel=ui_kernel->create_linearword(size, scale);
2660  else if (strmatch(dtype, "CHAR"))
2661  kernel=ui_kernel->create_linearstring(size, scale);
2662  else if (strmatch(dtype, "REAL"))
2663  kernel=ui_kernel->create_linear(size, scale);
2664  else if (strmatch(dtype, "SPARSEREAL"))
2665  kernel=ui_kernel->create_sparselinear(size, scale);
2666 
2667  SG_FREE(dtype);
2668  }
2669  else if (strmatch(type, "HISTOGRAM"))
2670  {
2671  if (m_nrhs<4)
2672  return NULL;
2673 
2674  char* dtype=get_str_from_str_or_direct(len);
2675  if (strmatch(dtype, "WORD"))
2676  {
2677  int32_t size=get_int_from_int_or_str();
2678  kernel=ui_kernel->create_histogramword(size);
2679  }
2680 
2681  SG_FREE(dtype);
2682  }
2683  else if (strmatch(type, "SALZBERG"))
2684  {
2685  if (m_nrhs<4)
2686  return NULL;
2687 
2688  char* dtype=get_str_from_str_or_direct(len);
2689  if (strmatch(dtype, "WORD"))
2690  {
2691  int32_t size=get_int_from_int_or_str();
2692  kernel=ui_kernel->create_salzbergword(size);
2693  }
2694 
2695  SG_FREE(dtype);
2696  }
2697  else if (strmatch(type, "POLYMATCH"))
2698  {
2699  if (m_nrhs<4)
2700  return NULL;
2701 
2702  char* dtype=get_str_from_str_or_direct(len);
2703  int32_t size=get_int_from_int_or_str();
2704  int32_t degree=3;
2705  bool inhomogene=false;
2706  bool normalize=true;
2707 
2708  if (m_nrhs>4)
2709  {
2710  degree=get_int_from_int_or_str();
2711  if (m_nrhs>5)
2712  {
2713  inhomogene=get_bool_from_bool_or_str();
2714  if (m_nrhs>6)
2715  normalize=get_bool_from_bool_or_str();
2716  }
2717  }
2718 
2719  if (strmatch(dtype, "CHAR"))
2720  {
2721  kernel=ui_kernel->create_polymatchstring(
2722  size, degree, inhomogene, normalize);
2723  }
2724  else if (strmatch(dtype, "WORD"))
2725  {
2726  kernel=ui_kernel->create_polymatchwordstring(
2727  size, degree, inhomogene, normalize);
2728  }
2729 
2730  SG_FREE(dtype);
2731  }
2732  else if (strmatch(type, "MATCH"))
2733  {
2734  if (m_nrhs<4)
2735  return NULL;
2736 
2737  char* dtype=get_str_from_str_or_direct(len);
2738  if (strmatch(dtype, "WORD"))
2739  {
2740  int32_t size=get_int_from_int_or_str();
2741  int32_t d=3;
2742  bool normalize=true;
2743 
2744  if (m_nrhs>4)
2745  d=get_int_from_int_or_str();
2746  if (m_nrhs>5)
2747  normalize=get_bool_from_bool_or_str();
2748 
2749  kernel=ui_kernel->create_matchwordstring(size, d, normalize);
2750  }
2751 
2752  SG_FREE(dtype);
2753  }
2754  else if (strmatch(type, "WEIGHTEDCOMMSTRING") || strmatch(type, "COMMSTRING"))
2755  {
2756  char* dtype=get_str_from_str_or_direct(len);
2757  int32_t size=get_int_from_int_or_str();
2758  bool use_sign=false;
2759  char* norm_str=NULL;
2760 
2761  if (m_nrhs>4)
2762  {
2763  use_sign=get_bool_from_bool_or_str();
2764 
2765  if (m_nrhs>5)
2766  norm_str=get_str_from_str_or_direct(len);
2767  }
2768 
2769  if (strmatch(dtype, "WORD"))
2770  {
2771  if (strmatch(type, "WEIGHTEDCOMMSTRING"))
2772  {
2773  kernel=ui_kernel->create_commstring(
2774  size, use_sign, norm_str, K_WEIGHTEDCOMMWORDSTRING);
2775  }
2776  else if (strmatch(type, "COMMSTRING"))
2777  {
2778  kernel=ui_kernel->create_commstring(
2779  size, use_sign, norm_str, K_COMMWORDSTRING);
2780  }
2781  }
2782  else if (strmatch(dtype, "ULONG"))
2783  {
2784  kernel=ui_kernel->create_commstring(
2785  size, use_sign, norm_str, K_COMMULONGSTRING);
2786  }
2787 
2788  SG_FREE(dtype);
2789  SG_FREE(norm_str);
2790  }
2791  else if (strmatch(type, "CHI2"))
2792  {
2793  if (m_nrhs<4)
2794  return NULL;
2795 
2796  char* dtype=get_str_from_str_or_direct(len);
2797  if (strmatch(dtype, "REAL"))
2798  {
2799  int32_t size=get_int_from_int_or_str();
2800  float64_t width=1;
2801 
2802  if (m_nrhs>4)
2803  width=get_real_from_real_or_str();
2804 
2805  kernel=ui_kernel->create_chi2(size, width);
2806  }
2807 
2808  SG_FREE(dtype);
2809  }
2810  else if (strmatch(type, "FIXEDDEGREE"))
2811  {
2812  if (m_nrhs<4)
2813  return NULL;
2814 
2815  char* dtype=get_str_from_str_or_direct(len);
2816  if (strmatch(dtype, "CHAR"))
2817  {
2818  int32_t size=get_int_from_int_or_str();
2819  int32_t d=3;
2820  if (m_nrhs>4)
2821  d=get_int_from_int_or_str();
2822 
2823  kernel=ui_kernel->create_fixeddegreestring(size, d);
2824  }
2825 
2826  SG_FREE(dtype);
2827  }
2828  else if (strmatch(type, "LOCALALIGNMENT"))
2829  {
2830  if (m_nrhs<4)
2831  return NULL;
2832 
2833  char* dtype=get_str_from_str_or_direct(len);
2834  if (strmatch(dtype, "CHAR"))
2835  {
2836  int32_t size=get_int_from_int_or_str();
2837 
2838  kernel=ui_kernel->create_localalignmentstring(size);
2839  }
2840 
2841  SG_FREE(dtype);
2842  }
2843  else if (strmatch(type, "OLIGO"))
2844  {
2845  if (m_nrhs<6)
2846  return NULL;
2847 
2848  char* dtype=get_str_from_str_or_direct(len);
2849  if (strmatch(dtype, "CHAR"))
2850  {
2851  int32_t size=get_int_from_int_or_str();
2852  int32_t k=get_int_from_int_or_str();
2853  float64_t w=get_real_from_real_or_str();
2854 
2855  kernel=ui_kernel->create_oligo(size, k, w);
2856  }
2857 
2858  SG_FREE(dtype);
2859  }
2860  else if (strmatch(type, "WEIGHTEDDEGREEPOS2") ||
2861  strmatch(type, "WEIGHTEDDEGREEPOS2_NONORM"))
2862  {
2863  if (m_nrhs<7)
2864  return NULL;
2865 
2866  char* dtype=get_str_from_str_or_direct(len);
2867  if (strmatch(dtype, "CHAR") || strmatch(dtype, "STRING"))
2868  {
2869  int32_t size=get_int_from_int_or_str();
2870  int32_t order=get_int_from_int_or_str();
2871  int32_t max_mismatch=get_int_from_int_or_str();
2872  int32_t length=get_int_from_int_or_str();
2873  int32_t* shifts=NULL;
2874  int32_t l=0;
2875  get_vector_from_int_vector_or_str(shifts, l);
2876 
2877  ASSERT(l==length)
2878 
2879  bool use_normalization=true;
2880  if (strmatch(type, "WEIGHTEDDEGREEPOS2_NONORM"))
2881  use_normalization=false;
2882 
2883  kernel=ui_kernel->create_weighteddegreepositionstring2(
2884  size, order, max_mismatch, shifts, length,
2885  use_normalization);
2886 
2887  SG_FREE(shifts);
2888  }
2889 
2890  SG_FREE(dtype);
2891  }
2892  else if (strmatch(type, "WEIGHTEDDEGREEPOS3"))
2893  {
2894  if (m_nrhs<7)
2895  return NULL;
2896 
2897  char* dtype=get_str_from_str_or_direct(len);
2898  if (strmatch(dtype, "CHAR") || strmatch(dtype, "STRING"))
2899  {
2900  int32_t size=get_int_from_int_or_str();
2901  int32_t order=get_int_from_int_or_str();
2902  int32_t max_mismatch=get_int_from_int_or_str();
2903  int32_t length=get_int_from_int_or_str();
2904  int32_t mkl_stepsize=get_int_from_int_or_str();
2905  int32_t* shifts=NULL;
2906  int32_t l=0;
2907  get_vector_from_int_vector_or_str(shifts, l);
2908  ASSERT(l==length)
2909 
2910  float64_t* position_weights=NULL;
2911  if (m_nrhs>9+length)
2912  {
2913  get_vector_from_real_vector_or_str(
2914  position_weights, length);
2915  }
2916 
2917  kernel=ui_kernel->create_weighteddegreepositionstring3(
2918  size, order, max_mismatch, shifts, length,
2919  mkl_stepsize, position_weights);
2920 
2921  SG_FREE(position_weights);
2922  SG_FREE(shifts);
2923  }
2924 
2925  SG_FREE(dtype);
2926  }
2927  else if (strmatch(type, "WEIGHTEDDEGREEPOS"))
2928  {
2929  if (m_nrhs<4)
2930  return NULL;
2931 
2932  char* dtype=get_str_from_str_or_direct(len);
2933  if (strmatch(dtype, "CHAR") || strmatch(dtype, "STRING"))
2934  {
2935  int32_t size=get_int_from_int_or_str();
2936  int32_t order=3;
2937  int32_t max_mismatch=0;
2938  int32_t length=0;
2939  int32_t center=0;
2940  float64_t step=1;
2941 
2942  if (m_nrhs>4)
2943  {
2944  order=get_int_from_int_or_str();
2945 
2946  if (m_nrhs>5)
2947  {
2948  max_mismatch=get_int_from_int_or_str();
2949 
2950  if (m_nrhs>6)
2951  {
2952  length=get_int_from_int_or_str();
2953 
2954  if (m_nrhs>7)
2955  {
2956  center=get_int_from_int_or_str();
2957 
2958  if (m_nrhs>8)
2959  step=get_real_from_real_or_str();
2960  }
2961  }
2962  }
2963  }
2964 
2965  kernel=ui_kernel->create_weighteddegreepositionstring(
2966  size, order, max_mismatch, length, center, step);
2967  }
2968 
2969  SG_FREE(dtype);
2970  }
2971  else if (strmatch(type, "WEIGHTEDDEGREE"))
2972  {
2973  if (m_nrhs<4)
2974  return NULL;
2975 
2976  char* dtype=get_str_from_str_or_direct(len);
2977  if (strmatch(dtype, "CHAR") || strmatch(dtype, "STRING"))
2978  {
2979  int32_t size=get_int_from_int_or_str();
2980  int32_t order=3;
2981  int32_t max_mismatch=0;
2982  bool use_normalization=true;
2983  int32_t mkl_stepsize=1;
2984  bool block_computation=true;
2985  int32_t single_degree=-1;
2986 
2987  if (m_nrhs>4)
2988  {
2989  order=get_int_from_int_or_str();
2990 
2991  if (m_nrhs>5)
2992  {
2993  max_mismatch=get_int_from_int_or_str();
2994 
2995  if (m_nrhs>6)
2996  {
2997  use_normalization=get_bool_from_bool_or_str();
2998 
2999  if (m_nrhs>7)
3000  {
3001  mkl_stepsize=get_int_from_int_or_str();
3002 
3003  if (m_nrhs>8)
3004  {
3005  block_computation=get_int_from_int_or_str();
3006 
3007  if (m_nrhs>9)
3008  single_degree=get_int_from_int_or_str();
3009  }
3010  }
3011  }
3012  }
3013  }
3014 
3015  kernel=ui_kernel->create_weighteddegreestring(
3016  size, order, max_mismatch, use_normalization,
3017  mkl_stepsize, block_computation, single_degree);
3018  }
3019 
3020  SG_FREE(dtype);
3021  }
3022  else if (strmatch(type, "WEIGHTEDDEGREERBF"))
3023  {
3024  if (m_nrhs<5)
3025  return NULL;
3026 
3027  char* dtype=get_str_from_str_or_direct(len);
3028  int32_t size=get_int_from_int_or_str();
3029  int32_t nof_properties=get_int_from_int_or_str();
3030  int32_t degree=1;
3031  float64_t width=1;
3032  if (m_nrhs>5)
3033  {
3034  degree=get_int_from_int_or_str();
3035  if (m_nrhs>6)
3036  {
3037  width=get_real_from_real_or_str();
3038  }
3039 
3040  }
3041  //if (strmatch(dtype, "REAL"))
3042 
3043  kernel=ui_kernel->create_weighteddegreerbf(size, degree, nof_properties, width);
3044 
3045  SG_FREE(dtype);
3046 
3047  }
3048  else if (strmatch(type, "SPECTRUMMISMATCHRBF"))
3049  {
3050  if (m_nrhs<7)
3051  return NULL;
3052 
3053  char* dtype=get_str_from_str_or_direct(len);
3054  if (strmatch(dtype, "CHAR") || strmatch(dtype, "STRING"))
3055  {
3056  int32_t size=get_int_from_int_or_str();
3057  int32_t degree=get_int_from_int_or_str();
3058  int32_t max_mismatch=get_int_from_int_or_str();
3059  float64_t width=get_real_from_real_or_str();
3060  float64_t* AA_matrix = NULL;
3061 
3062  //int32_t length=128*128;
3063  //get_vector_from_real_vector_or_str(AA_matrix, length);
3064  float64_t* helper_matrix=NULL;
3065  int32_t N=0;
3066  int32_t M=0;
3067  get_matrix(helper_matrix, N, M);
3068 
3069  if (N == 128 && M == 128)
3070  {
3071  AA_matrix=SG_MALLOC(float64_t, N*M);
3072  memcpy(AA_matrix, helper_matrix, N*M*sizeof(float64_t)) ;
3073  kernel=ui_kernel->create_spectrummismatchrbf(size, AA_matrix, 128, 128, max_mismatch, degree, width);
3074  }
3075  else
3076  {
3077  SG_ERROR("Matrix size %d %d\n", N, M)
3078  }
3079  }
3080  SG_FREE(dtype);
3081 
3082  }
3083 
3084  else if (strmatch(type, "SLIK") || strmatch(type, "LIK"))
3085  {
3086  if (m_nrhs<4)
3087  return NULL;
3088 
3089  char* dtype=get_str_from_str_or_direct(len);
3090  if (strmatch(dtype, "CHAR"))
3091  {
3092  int32_t size=get_int_from_int_or_str();
3093  int32_t length=3;
3094  int32_t inner_degree=3;
3095  int32_t outer_degree=1;
3096 
3097  if (m_nrhs>4)
3098  {
3099  length=get_int_from_int_or_str();
3100 
3101  if (m_nrhs>5)
3102  {
3103  inner_degree=get_int_from_int_or_str();
3104 
3105  if (m_nrhs>6)
3106  outer_degree=get_int_from_int_or_str();
3107  }
3108  }
3109 
3110  if (strmatch(type, "SLIK"))
3111  {
3112  kernel=ui_kernel->create_localityimprovedstring(
3113  size, length, inner_degree, outer_degree,
3115  }
3116  else
3117  {
3118  kernel=ui_kernel->create_localityimprovedstring(
3119  size, length, inner_degree, outer_degree,
3121  }
3122  }
3123 
3124  SG_FREE(dtype);
3125  }
3126  else if (strmatch(type, "POLY"))
3127  {
3128  if (m_nrhs<4)
3129  return NULL;
3130 
3131  char* dtype=get_str_from_str_or_direct(len);
3132  int32_t size=get_int_from_int_or_str();
3133  int32_t degree=2;
3134  bool inhomogene=false;
3135  bool normalize=true;
3136 
3137  if (m_nrhs>4)
3138  {
3139  degree=get_int_from_int_or_str();
3140 
3141  if (m_nrhs>5)
3142  {
3143  inhomogene=get_bool_from_bool_or_str();
3144 
3145  if (m_nrhs>6)
3146  normalize=get_bool_from_bool_or_str();
3147  }
3148  }
3149 
3150  if (strmatch(dtype, "REAL"))
3151  {
3152  kernel=ui_kernel->create_poly(
3153  size, degree, inhomogene, normalize);
3154  }
3155  else if (strmatch(dtype, "SPARSEREAL"))
3156  {
3157  kernel=ui_kernel->create_sparsepoly(
3158  size, degree, inhomogene, normalize);
3159  }
3160 
3161  SG_FREE(dtype);
3162  }
3163  else if (strmatch(type, "SIGMOID"))
3164  {
3165  if (m_nrhs<4)
3166  return NULL;
3167 
3168  char* dtype=get_str_from_str_or_direct(len);
3169  if (strmatch(dtype, "REAL"))
3170  {
3171  int32_t size=get_int_from_int_or_str();
3172  float64_t gamma=0.01;
3173  float64_t coef0=0;
3174 
3175  if (m_nrhs>4)
3176  {
3177  gamma=get_real_from_real_or_str();
3178 
3179  if (m_nrhs>5)
3180  coef0=get_real_from_real_or_str();
3181  }
3182 
3183  kernel=ui_kernel->create_sigmoid(size, gamma, coef0);
3184  }
3185 
3186  SG_FREE(dtype);
3187  }
3188  else if (strmatch(type, "GAUSSIAN")) // RBF
3189  {
3190  if (m_nrhs<4)
3191  return NULL;
3192 
3193  char* dtype=get_str_from_str_or_direct(len);
3194  int32_t size=get_int_from_int_or_str();
3195  float64_t width=1;
3196  if (m_nrhs>4)
3197  width=get_real_from_real_or_str();
3198 
3199  if (strmatch(dtype, "REAL"))
3200  kernel=ui_kernel->create_gaussian(size, width);
3201  else if (strmatch(dtype, "SPARSEREAL"))
3202  kernel=ui_kernel->create_sparsegaussian(size, width);
3203 
3204  SG_FREE(dtype);
3205  }
3206  else if (strmatch(type, "GAUSSIANSHIFT")) // RBF
3207  {
3208  if (m_nrhs<7)
3209  return NULL;
3210 
3211  char* dtype=get_str_from_str_or_direct(len);
3212  if (strmatch(dtype, "REAL"))
3213  {
3214  int32_t size=get_int_from_int_or_str();
3215  float64_t width=get_real_from_real_or_str();
3216  int32_t max_shift=get_int_from_int_or_str();
3217  int32_t shift_step=get_int_from_int_or_str();
3218 
3219  kernel=ui_kernel->create_gaussianshift(
3220  size, width, max_shift, shift_step);
3221  }
3222 
3223  SG_FREE(dtype);
3224  }
3225  else if (strmatch(type, "CUSTOM"))
3226  {
3227  if (m_nrhs!=4 || !create_return_values(0))
3228  return NULL;
3229 
3230  float64_t* kmatrix=NULL;
3231  int32_t num_feat=0;
3232  int32_t num_vec=0;
3233  get_matrix(kmatrix, num_feat, num_vec);
3234 
3235  int32_t tlen=0;
3236  char* ktype=get_string(tlen);
3237 
3238  if (!strmatch(ktype, "DIAG") &&
3239  !strmatch(ktype, "FULL") &&
3240  !strmatch(ktype, "FULL2DIAG"))
3241  {
3242  SG_FREE(ktype);
3243  SG_ERROR("Undefined type, not DIAG, FULL or FULL2DIAG.\n")
3244  }
3245 
3246  bool source_is_diag=false;
3247  bool dest_is_diag=false;
3248 
3249  if (strmatch(ktype, "FULL2DIAG"))
3250  dest_is_diag=true;
3251  else if (strmatch(ktype, "DIAG"))
3252  {
3253  source_is_diag=true;
3254  dest_is_diag=true;
3255  }
3256 
3257  kernel=ui_kernel->create_custom(kmatrix, num_feat, num_vec,
3258  source_is_diag, dest_is_diag);
3259  }
3260  else if (strmatch(type, "CONST"))
3261  {
3262  if (m_nrhs<4)
3263  return NULL;
3264 
3265  char* dtype=get_str_from_str_or_direct(len);
3266  if (strmatch(dtype, "REAL"))
3267  {
3268  int32_t size=get_int_from_int_or_str();
3269  float64_t c=1;
3270  if (m_nrhs>4)
3271  c=get_real_from_real_or_str();
3272 
3273  kernel=ui_kernel->create_const(size, c);
3274  }
3275 
3276  SG_FREE(dtype);
3277  }
3278  else if (strmatch(type, "DIAG"))
3279  {
3280  if (m_nrhs<4)
3281  return NULL;
3282 
3283  char* dtype=get_str_from_str_or_direct(len);
3284  if (strmatch(dtype, "REAL"))
3285  {
3286  int32_t size=get_int_from_int_or_str();
3287  float64_t diag=1;
3288  if (m_nrhs>4)
3289  diag=get_real_from_real_or_str();
3290 
3291  kernel=ui_kernel->create_diag(size, diag);
3292  }
3293 
3294  SG_FREE(dtype);
3295  }
3296 
3297  else if (strmatch(type, "TPPK"))
3298  {
3299  if (m_nrhs!=5)
3300  return NULL;
3301 
3302  char* dtype=get_str_from_str_or_direct(len);
3303  if (strmatch(dtype, "INT"))
3304  {
3305  int32_t size=get_int_from_int_or_str();
3306  float64_t* km=NULL;
3307  int32_t rows=0;
3308  int32_t cols=0;
3309  get_matrix(km, rows, cols);
3310  kernel=ui_kernel->create_tppk(size, km, rows, cols);
3311  }
3312 
3313  SG_FREE(dtype);
3314  }
3315  else
3317 
3318  SG_FREE(type);
3319  SG_DEBUG("created kernel: %p\n", kernel)
3320  return kernel;
3321 }
3322 
3323 
3324 CFeatures* CSGInterface::create_custom_string_features(CStringFeatures<uint8_t>* orig_feat)
3325 {
3326  CFeatures* feat=orig_feat;
3327 
3328  if (m_nrhs>4)
3329  {
3330  int32_t start=-1;
3331  int32_t order=0;
3332  int32_t from_order=0;
3333  bool normalize=true;
3334 
3335  int32_t feature_class_len=0;
3336  char* feature_class_str=get_string(feature_class_len);
3337  ASSERT(feature_class_str)
3338  CAlphabet* alphabet=NULL;
3339  if (strmatch(feature_class_str, "WD"))
3340  {
3341  if (m_nrhs!=7)
3342  SG_ERROR("Please specify alphabet, WD, order, from_order\n")
3343 
3344  alphabet=new CAlphabet(RAWDNA);
3345  order=get_int();
3346  from_order=get_int();
3347  feat = new CWDFeatures((CStringFeatures<uint8_t>*) feat, order, from_order);
3348  }
3349  else if (strmatch(feature_class_str, "WSPEC"))
3350  {
3351  if (m_nrhs!=8)
3352  SG_ERROR("Please specify alphabet, order, WSPEC, start, normalize\n")
3353 
3354  alphabet=new CAlphabet(RAWDNA);
3355  order=get_int();
3356  start=get_int();
3357  normalize=get_bool();
3359  sf->obtain_from_char_features((CStringFeatures<uint8_t>*) feat, start, order, 0, normalize);
3360  sf->add_preprocessor(new CSortWordString());
3361  sf->apply_preprocessor();
3362  SG_UNREF(feat);
3363  feat = new CImplicitWeightedSpecFeatures(sf, normalize);
3364  }
3365  SG_FREE(feature_class_str);
3366 
3367  SG_UNREF(alphabet);
3368  }
3369 
3370  return feat;
3371 }
3372 
3373 CFeatures* CSGInterface::create_custom_real_features(CDenseFeatures<float64_t>* orig_feat)
3374 {
3375  CFeatures* feat=orig_feat;
3376 
3377  if (m_nrhs==6)
3378  {
3379  int32_t degree=0;
3380  int32_t feature_class_len=0;
3381  bool normalize;
3382  char* feature_class_str=get_string(feature_class_len);
3383  ASSERT(feature_class_str)
3384  if (strmatch(feature_class_str, "POLY"))
3385  {
3386  //if (m_nrhs!=7)
3387  // SG_ERROR("Please specify POLY, degree\n")
3388 
3389  degree=get_int();
3390  normalize = get_bool();
3391  feat = new CPolyFeatures((CDenseFeatures<float64_t>*) feat, degree, normalize);
3392 
3393  }
3394  else
3395  SG_ERROR("Unknown feature class: %s\n", feature_class_str)
3396 
3397  SG_FREE(feature_class_str);
3398  }
3399 
3400  return feat;
3401 }
3402 
3403 bool CSGInterface::cmd_init_kernel()
3404 {
3406  return true;
3407 }
3408 
3409 bool CSGInterface::cmd_clean_kernel()
3410 {
3411  if (m_nrhs<1 || !create_return_values(0))
3412  return false;
3413 
3414  return ui_kernel->clean_kernel();
3415 }
3416 
3417 bool CSGInterface::cmd_save_kernel()
3418 {
3419  if (m_nrhs<2 || !create_return_values(0))
3420  return false;
3421 
3422  int32_t len=0;
3423  char* filename=get_str_from_str_or_direct(len);
3424 
3425  bool success=ui_kernel->save_kernel(filename);
3426 
3427  SG_FREE(filename);
3428  return success;
3429 }
3430 
3431 bool CSGInterface::cmd_get_kernel_matrix()
3432 {
3433  if (m_nrhs>2 || !create_return_values(1))
3434  return false;
3435 
3436  int32_t len=0;
3437  char* target=NULL;
3438 
3439  if (m_nrhs==2)
3440  target=get_string(len);
3441  bool success=ui_kernel->init_kernel(target);
3442 
3443  if (success)
3444  {
3445  CKernel* kernel=ui_kernel->get_kernel();
3446  if (!kernel || !kernel->has_features())
3447  SG_ERROR("No kernel defined or not initialized.\n")
3448 
3450  set_matrix(km.matrix, km.num_rows, km.num_cols);
3451  }
3452 
3453  SG_FREE(target);
3454 
3455  return success;
3456 }
3457 
3458 bool CSGInterface::cmd_set_WD_position_weights()
3459 {
3460  if (m_nrhs<2 || m_nrhs>3 || !create_return_values(0))
3461  return false;
3462 
3463  CKernel* kernel=ui_kernel->get_kernel();
3464  if (!kernel)
3465  SG_ERROR("No kernel.\n")
3466 
3467  if (kernel->get_kernel_type()==K_COMBINED)
3468  {
3469  kernel=((CCombinedKernel*) kernel)->get_last_kernel();
3470  if (!kernel)
3471  SG_ERROR("No last kernel.\n")
3472 
3473  EKernelType ktype=kernel->get_kernel_type();
3474  if (ktype!=K_WEIGHTEDDEGREE && ktype!=K_WEIGHTEDDEGREEPOS)
3475  SG_ERROR("Unsupported kernel.\n")
3476  }
3477 
3478  bool success=false;
3479  float64_t* weights=NULL;
3480  int32_t dim=0;
3481  int32_t len=0;
3482  get_matrix(weights, dim, len);
3483 
3484  if (kernel->get_kernel_type()==K_WEIGHTEDDEGREE)
3485  {
3487  (CWeightedDegreeStringKernel*) kernel;
3488 
3489  if (dim!=1 && len>0)
3490  SG_ERROR("Dimension mismatch (should be 1 x seq_length or 0x0\n")
3491 
3492  ui_kernel->init_kernel("TRAIN");
3493  success=k->set_position_weights(weights, len);
3494  }
3495  else
3496  {
3499  char* target=NULL;
3500  bool is_train=true;
3501 
3502  if (m_nrhs==3)
3503  {
3504  int32_t tlen=0;
3505  target=get_string(tlen);
3506  if (!target)
3507  {
3508  SG_FREE(weights);
3509  SG_ERROR("Couldn't find second argument to method.\n")
3510  }
3511 
3512  if (!strmatch(target, "TRAIN") && !strmatch(target, "TEST"))
3513  {
3514  SG_FREE(target);
3515  SG_ERROR("Second argument none of TRAIN or TEST.\n")
3516  }
3517 
3518  if (strmatch(target, "TEST"))
3519  is_train=false;
3520  }
3521 
3522  if (dim!=1 && len>0)
3523  {
3524  SG_FREE(target);
3525  SG_ERROR("Dimension mismatch (should be 1 x seq_length or 0x0\n")
3526  }
3527 
3528  if (dim==0 && len==0)
3529  {
3530  if (create_return_values(3))
3531  {
3532  if (is_train)
3533  success=k->delete_position_weights_lhs();
3534  else
3535  success=k->delete_position_weights_rhs();
3536  }
3537  else
3538  success=k->delete_position_weights();
3539  }
3540  else
3541  {
3542  if (create_return_values(3))
3543  {
3544  if (is_train)
3545  success=k->set_position_weights_lhs(weights, dim, len);
3546  else
3547  success=k->set_position_weights_rhs(weights, dim, len);
3548  }
3549  else
3550  {
3551  ui_kernel->init_kernel("TRAIN");
3552  k->set_position_weights(SGVector<float64_t>(weights, len));
3553  success=true;
3554  }
3555  }
3556 
3557  SG_FREE(target);
3558  }
3559 
3560  return success;
3561 }
3562 
3563 bool CSGInterface::cmd_get_subkernel_weights()
3564 {
3565  if (m_nrhs!=1 || !create_return_values(1))
3566  return false;
3567 
3568  CKernel *kernel=ui_kernel->get_kernel();
3569  if (!kernel)
3570  SG_ERROR("Invalid kernel.\n")
3571 
3572  EKernelType ktype=kernel->get_kernel_type();
3573  const float64_t* weights=NULL;
3574 
3575  if (ktype==K_COMBINED)
3576  {
3577  int32_t num_weights=-1;
3578  weights=((CCombinedKernel *) kernel)->get_subkernel_weights(num_weights);
3579 
3580  // matrices of shape 1 x num_weight are returned
3581  set_matrix(weights, 1, num_weights);
3582  return true;
3583  }
3584 
3585  int32_t degree=-1;
3586  int32_t length=-1;
3587 
3588  if (ktype==K_WEIGHTEDDEGREE)
3589  {
3590  weights=((CWeightedDegreeStringKernel *) kernel)->
3591  get_degree_weights(degree, length);
3592  }
3593  else if (ktype==K_WEIGHTEDDEGREEPOS)
3594  {
3595  weights=((CWeightedDegreePositionStringKernel *) kernel)->
3596  get_degree_weights(degree, length);
3597  }
3598  else
3599  SG_ERROR("Setting subkernel weights not supported on this kernel.\n")
3600 
3601  if (length==0)
3602  length=1;
3603 
3604  set_matrix(weights, degree, length);
3605  return true;
3606 }
3607 
3608 bool CSGInterface::cmd_set_subkernel_weights()
3609 {
3610  if (m_nrhs!=2 || !create_return_values(0))
3611  return false;
3612 
3613  CKernel* kernel=ui_kernel->get_kernel();
3614  if (!kernel)
3615  SG_ERROR("No kernel.\n")
3616 
3617  bool success=false;
3618  float64_t* weights=NULL;
3619  int32_t dim=0;
3620  int32_t len=0;
3621  get_matrix(weights, dim, len);
3622 
3623  EKernelType ktype=kernel->get_kernel_type();
3624  if (ktype==K_WEIGHTEDDEGREE)
3625  {
3627  (CWeightedDegreeStringKernel*) kernel;
3628  int32_t degree=k->get_degree();
3629  if (dim!=degree || len<1)
3630  SG_ERROR("WD: Dimension mismatch (should be (seq_length | 1) x degree) got (%d x %d)\n", len, degree)
3631 
3632  if (len==1)
3633  len=0;
3634 
3635  success=k->set_weights(SGMatrix<float64_t>(weights, dim, len));
3636  }
3637  else if (ktype==K_WEIGHTEDDEGREEPOS)
3638  {
3641  int32_t degree=k->get_degree();
3642  if (dim!=degree || len<1)
3643  SG_ERROR("WDPos: Dimension mismatch (should be (seq_length | 1) x degree) got (%d x %d)\n", len, degree)
3644 
3645  if (len==1)
3646  len=0;
3647 
3648  success=k->set_weights(SGMatrix<float64_t>(weights, dim, len));
3649  }
3650  else // all other kernels
3651  {
3652  int32_t num_subkernels=kernel->get_num_subkernels();
3653  if (dim!=1 || len!=num_subkernels)
3654  SG_ERROR("All: Dimension mismatch (should be 1 x num_subkernels)\n")
3655 
3656  kernel->set_subkernel_weights(SGVector<float64_t>(weights, len));
3657  success=true;
3658  }
3659 
3660  return success;
3661 }
3662 
3663 bool CSGInterface::cmd_set_subkernel_weights_combined()
3664 {
3665  if (m_nrhs!=3 || !create_return_values(0))
3666  return false;
3667 
3668  CKernel* kernel=ui_kernel->get_kernel();
3669  if (!kernel)
3670  SG_ERROR("No kernel.\n")
3671  if (kernel->get_kernel_type()!=K_COMBINED)
3672  SG_ERROR("Only works for combined kernels.\n")
3673 
3674  bool success=false;
3675  float64_t* weights=NULL;
3676  int32_t dim=0;
3677  int32_t len=0;
3678  get_matrix(weights, dim, len);
3679 
3680  int32_t idx=get_int();
3681  SG_DEBUG("using kernel_idx=%i\n", idx)
3682 
3683  kernel=((CCombinedKernel*) kernel)->get_kernel(idx);
3684  if (!kernel)
3685  SG_ERROR("No subkernel at idx %d.\n", idx)
3686 
3687  EKernelType ktype=kernel->get_kernel_type();
3688  if (ktype==K_WEIGHTEDDEGREE)
3689  {
3691  (CWeightedDegreeStringKernel*) kernel;
3692  int32_t degree=k->get_degree();
3693  if (dim!=degree || len<1)
3694  SG_ERROR("Dimension mismatch (should be de(seq_length | 1) x degree)\n")
3695 
3696  if (len==1)
3697  len=0;
3698 
3699  success=k->set_weights(SGMatrix<float64_t>(weights, dim, len));
3700  }
3701  else if (ktype==K_WEIGHTEDDEGREEPOS)
3702  {
3705  int32_t degree=k->get_degree();
3706  if (dim!=degree || len<1)
3707  SG_ERROR("Dimension mismatch (should be de(seq_length | 1) x degree)\n")
3708 
3709  if (len==1)
3710  len=0;
3711 
3712  success=k->set_weights(SGMatrix<float64_t>(weights, dim, len));
3713  }
3714  else // all other kernels
3715  {
3716  int32_t num_subkernels=kernel->get_num_subkernels();
3717  if (dim!=1 || len!=num_subkernels)
3718  SG_ERROR("Dimension mismatch (should be 1 x num_subkernels)\n")
3719 
3720  kernel->set_subkernel_weights(SGVector<float64_t>(weights, len));
3721  success=true;
3722  }
3723 
3724  return success;
3725 }
3726 
3727 bool CSGInterface::cmd_get_dotfeature_weights_combined()
3728 {
3729  if (m_nrhs!=2 || !create_return_values(1))
3730  return false;
3731 
3732  int32_t tlen=0;
3733  char* target=get_string(tlen);
3734  CFeatures* features=NULL;
3735 
3736  if (strmatch(target, "TRAIN"))
3737  features=ui_features->get_train_features();
3738  else if (strmatch(target, "TEST"))
3739  features=ui_features->get_test_features();
3740  else
3741  {
3742  SG_FREE(target);
3743  SG_ERROR("Unknown target, neither TRAIN nor TEST.\n")
3744  }
3745  SG_FREE(target);
3746 
3747  if (!features)
3748  SG_ERROR("No features.\n")
3749  if (features->get_feature_class()!=C_COMBINED_DOT)
3750  SG_ERROR("Only works for combined dot features.\n")
3751 
3752  SGVector<float64_t> weights = ((CCombinedDotFeatures*) features)->get_subfeature_weights();
3753  set_vector(weights.vector, weights.vlen);
3754 
3755  return true;
3756 }
3757 
3758 bool CSGInterface::cmd_set_dotfeature_weights_combined()
3759 {
3760  if (m_nrhs!=3 || !create_return_values(0))
3761  return false;
3762 
3763  int32_t tlen=0;
3764  char* target=get_string(tlen);
3765  CFeatures* features=NULL;
3766 
3767  if (strmatch(target, "TRAIN"))
3768  features=ui_features->get_train_features();
3769  else if (strmatch(target, "TEST"))
3770  features=ui_features->get_test_features();
3771  else
3772  {
3773  SG_FREE(target);
3774  SG_ERROR("Unknown target, neither TRAIN nor TEST.\n")
3775  }
3776  SG_FREE(target);
3777 
3778  if (!features)
3779  SG_ERROR("No features.\n")
3780  if (features->get_feature_class()!=C_COMBINED_DOT)
3781  SG_ERROR("Only works for combined dot features.\n")
3782 
3783  float64_t* weights=NULL;
3784  int32_t dim=0;
3785  int32_t len=0;
3786  get_matrix(weights, dim, len);
3787 
3788  ((CCombinedDotFeatures*) features)->set_subfeature_weights(SGVector<float64_t>(weights, len));
3789 
3790  return true;
3791 }
3792 
3793 bool CSGInterface::cmd_set_last_subkernel_weights()
3794 {
3795  if (m_nrhs!=2 || !create_return_values(0))
3796  return false;
3797 
3798  CKernel* kernel=ui_kernel->get_kernel();
3799  if (!kernel)
3800  SG_ERROR("No kernel.\n")
3801  if (kernel->get_kernel_type()!=K_COMBINED)
3802  SG_ERROR("Only works for Combined kernels.\n")
3803 
3804  kernel=((CCombinedKernel*) kernel)->get_last_kernel();
3805  if (!kernel)
3806  SG_ERROR("No last kernel.\n")
3807 
3808  bool success=false;
3809  float64_t* weights=NULL;
3810  int32_t dim=0;
3811  int32_t len=0;
3812  get_matrix(weights, dim, len);
3813 
3814  EKernelType ktype=kernel->get_kernel_type();
3815  if (ktype==K_WEIGHTEDDEGREE)
3816  {
3818  if (dim!=k->get_degree() || len<1)
3819  SG_ERROR("Dimension mismatch (should be de(seq_length | 1) x degree)\n")
3820 
3821  if (len==1)
3822  len=0;
3823 
3824  success=k->set_weights(SGMatrix<float64_t>(weights, dim, len));
3825  }
3826  else if (ktype==K_WEIGHTEDDEGREEPOS)
3827  {
3830  if (dim!=k->get_degree() || len<1)
3831  SG_ERROR("Dimension mismatch (should be de(seq_length | 1) x degree)\n")
3832 
3833  if (len==1)
3834  len=0;
3835 
3836  success=k->set_weights(SGMatrix<float64_t>(weights, dim, len));
3837  }
3838  else // all other kernels
3839  {
3840  int32_t num_subkernels=kernel->get_num_subkernels();
3841  if (dim!=1 || len!=num_subkernels)
3842  SG_ERROR("Dimension mismatch (should be 1 x num_subkernels)\n")
3843 
3844  kernel->set_subkernel_weights(SGVector<float64_t>(weights, len));
3845  success=true;
3846  }
3847 
3848  return success;
3849 }
3850 
3851 bool CSGInterface::cmd_get_WD_position_weights()
3852 {
3853  if (m_nrhs!=1 || !create_return_values(1))
3854  return false;
3855 
3856  CKernel* kernel=ui_kernel->get_kernel();
3857  if (!kernel)
3858  SG_ERROR("No kernel.\n")
3859 
3860  if (kernel->get_kernel_type()==K_COMBINED)
3861  {
3862  kernel=((CCombinedKernel*) kernel)->get_last_kernel();
3863  if (!kernel)
3864  SG_ERROR("Couldn't find last kernel.\n")
3865 
3866  EKernelType ktype=kernel->get_kernel_type();
3867  if (ktype!=K_WEIGHTEDDEGREE && ktype!=K_WEIGHTEDDEGREEPOS)
3868  SG_ERROR("Wrong subkernel type.\n")
3869  }
3870 
3871  int32_t len=0;
3872  const float64_t* position_weights;
3873 
3874  if (kernel->get_kernel_type()==K_WEIGHTEDDEGREE)
3875  position_weights=((CWeightedDegreeStringKernel*) kernel)->get_position_weights(len);
3876  else
3877  position_weights=((CWeightedDegreePositionStringKernel*) kernel)->get_position_weights(len);
3878 
3879  if (position_weights==NULL)
3880  set_vector(position_weights, 0);
3881  else
3882  set_vector(position_weights, len);
3883 
3884  return true;
3885 }
3886 
3887 bool CSGInterface::cmd_get_last_subkernel_weights()
3888 {
3889  if (m_nrhs!=1 || !create_return_values(1))
3890  return false;
3891 
3892  CKernel* kernel=ui_kernel->get_kernel();
3893  EKernelType ktype=kernel->get_kernel_type();
3894  if (!kernel)
3895  SG_ERROR("No kernel.\n")
3896  if (ktype!=K_COMBINED)
3897  SG_ERROR("Only works for Combined kernels.\n")
3898 
3899  kernel=((CCombinedKernel*) kernel)->get_last_kernel();
3900  if (!kernel)
3901  SG_ERROR("Couldn't find last kernel.\n")
3902 
3903  int32_t degree=0;
3904  int32_t len=0;
3905 
3906  if (ktype==K_COMBINED)
3907  {
3908  int32_t num_weights=0;
3909  const float64_t* weights=
3910  ((CCombinedKernel*) kernel)->get_subkernel_weights(num_weights);
3911 
3912  set_vector(weights, num_weights);
3913  return true;
3914  }
3915 
3916  float64_t* weights=NULL;
3917  if (ktype==K_WEIGHTEDDEGREE)
3918  weights=((CWeightedDegreeStringKernel*) kernel)->
3919  get_degree_weights(degree, len);
3920  else if (ktype==K_WEIGHTEDDEGREEPOS)
3921  weights=((CWeightedDegreePositionStringKernel*) kernel)->
3922  get_degree_weights(degree, len);
3923  else
3924  SG_ERROR("Only works for Weighted Degree (Position) kernels.\n")
3925 
3926  if (len==0)
3927  len=1;
3928 
3929  set_matrix(weights, degree, len);
3930 
3931  return true;
3932 }
3933 
3934 bool CSGInterface::cmd_compute_by_subkernels()
3935 {
3936  if (m_nrhs!=1 || !create_return_values(1))
3937  return false;
3938 
3939  CKernel* kernel=ui_kernel->get_kernel();
3940  if (!kernel)
3941  SG_ERROR("No kernel.\n")
3942  if (!kernel->get_rhs())
3943  SG_ERROR("No rhs.\n")
3944 
3945  int32_t num_vec=kernel->get_rhs()->get_num_vectors();
3946  int32_t degree=0;
3947  int32_t len=0;
3948  EKernelType ktype=kernel->get_kernel_type();
3949 
3950  // it would be nice to have a common base class for the WD kernels
3951  if (ktype==K_WEIGHTEDDEGREE)
3952  {
3954  k->get_degree_weights(degree, len);
3955  if (!k->is_tree_initialized())
3956  SG_ERROR("Kernel optimization not initialized.\n")
3957  }
3958  else if (ktype==K_WEIGHTEDDEGREEPOS)
3959  {
3962  k->get_degree_weights(degree, len);
3963  if (!k->is_tree_initialized())
3964  SG_ERROR("Kernel optimization not initialized.\n")
3965  }
3966  else
3967  SG_ERROR("Only works for Weighted Degree (Position) kernels.\n")
3968 
3969  if (len==0)
3970  len=1;
3971 
3972  int32_t num_feat=degree*len;
3973  int32_t num=num_feat*num_vec;
3974  float64_t* result=SG_MALLOC(float64_t, num);
3975 
3976  for (int32_t i=0; i<num; i++)
3977  result[i]=0;
3978 
3979  if (ktype==K_WEIGHTEDDEGREE)
3980  {
3982  for (int32_t i=0; i<num_vec; i++)
3983  k->compute_by_tree(i, &result[i*num_feat]);
3984  }
3985  else
3986  {
3989  for (int32_t i=0; i<num_vec; i++)
3990  k->compute_by_tree(i, &result[i*num_feat]);
3991  }
3992 
3993  set_matrix(result, num_feat, num_vec);
3994  SG_FREE(result);
3995 
3996  return true;
3997 }
3998 
3999 bool CSGInterface::cmd_init_kernel_optimization()
4000 {
4001  if (m_nrhs<1 || !create_return_values(0))
4002  return false;
4003 
4004  return ui_kernel->init_kernel_optimization();
4005 }
4006 
4007 bool CSGInterface::cmd_get_kernel_optimization()
4008 {
4009  if (m_nrhs<1 || !create_return_values(1))
4010  return false;
4011 
4012  CKernel* kernel=ui_kernel->get_kernel();
4013  if (!kernel)
4014  SG_ERROR("No kernel defined.\n")
4015 
4016  switch (kernel->get_kernel_type())
4017  {
4018  case K_WEIGHTEDDEGREEPOS:
4019  {
4020  if (m_nrhs!=2)
4021  SG_ERROR("parameter missing\n")
4022 
4023  int32_t max_order=get_int();
4024  if ((max_order<1) || (max_order>12))
4025  {
4026  SG_WARNING("max_order out of range 1..12 (%d). setting to 1\n", max_order)
4027  max_order=1;
4028  }
4029 
4031  CSVM* svm=(CSVM*) ui_classifier->get_classifier();
4032  if (!svm)
4033  SG_ERROR("No SVM defined.\n")
4034 
4035  int32_t num_suppvec=svm->get_num_support_vectors();
4036  int32_t* sv_idx=SG_MALLOC(int32_t, num_suppvec);
4037  float64_t* sv_weight=SG_MALLOC(float64_t, num_suppvec);
4038  int32_t num_feat=0;
4039  int32_t num_sym=0;
4040 
4041  for (int32_t i=0; i<num_suppvec; i++)
4042  {
4043  sv_idx[i]=svm->get_support_vector(i);
4044  sv_weight[i]=svm->get_alpha(i);
4045  }
4046 
4047  float64_t* position_weights=k->extract_w(max_order, num_feat,
4048  num_sym, NULL, num_suppvec, sv_idx, sv_weight);
4049  SG_FREE(sv_idx);
4050  SG_FREE(sv_weight);
4051 
4052  set_matrix(position_weights, num_sym, num_feat);
4053  SG_FREE(position_weights);
4054 
4055  return true;
4056  }
4057 
4058  case K_COMMWORDSTRING:
4060  {
4062  int32_t len=0;
4063  float64_t* weights;
4064  k->get_dictionary(len, weights);
4065 
4066  set_vector(weights, len);
4067  return true;
4068  }
4069  case K_LINEAR:
4070  {
4071  CLinearKernel* k=(CLinearKernel*) kernel;
4072  SGVector<float64_t> weights=k->get_w();
4073 
4074  set_vector(weights.vector, weights.size());
4075  return true;
4076  }
4077  default:
4078  SG_ERROR("Unsupported kernel %s.\n", kernel->get_name())
4079  }
4080 
4081  return true;
4082 }
4083 
4084 bool CSGInterface::cmd_delete_kernel_optimization()
4085 {
4086  if (m_nrhs<1 || !create_return_values(0))
4087  return false;
4088 
4089  return ui_kernel->delete_kernel_optimization();
4090 }
4091 
4092 bool CSGInterface::cmd_use_diagonal_speedup()
4093 {
4094  if (m_nrhs<2 || !create_return_values(0))
4095  return false;
4096 
4097  bool speedup=get_bool();
4098 
4099  CKernel* kernel=ui_kernel->get_kernel();
4100  if (!kernel)
4101  SG_ERROR("No kernel defined.\n")
4102 
4103  if (kernel->get_kernel_type()==K_COMBINED)
4104  {
4105  SG_DEBUG("Identified combined kernel.\n")
4106  kernel=((CCombinedKernel*) kernel)->get_last_kernel();
4107  if (!kernel)
4108  SG_ERROR("No last kernel defined.\n")
4109  }
4110 
4111  if (kernel->get_kernel_type()!=K_COMMWORDSTRING)
4112  SG_ERROR("Currently only commwordstring kernel supports diagonal speedup\n")
4113 
4114  ((CCommWordStringKernel*) kernel)->set_use_dict_diagonal_optimization(speedup);
4115 
4116  SG_INFO("Diagonal speedup %s.\n", speedup ? "enabled" : "disabled")
4117 
4118  return true;
4119 }
4120 
4121 bool CSGInterface::cmd_set_kernel_optimization_type()
4122 {
4123  if (m_nrhs<2 || !create_return_values(0))
4124  return false;
4125 
4126  int32_t len=0;
4127  char* opt_type=get_str_from_str_or_direct(len);
4128 
4129  bool success=ui_kernel->set_optimization_type(opt_type);
4130 
4131  SG_FREE(opt_type);
4132  return success;
4133 }
4134 
4135 bool CSGInterface::cmd_set_solver()
4136 {
4137  if (m_nrhs<2 || !create_return_values(0))
4138  return false;
4139 
4140  int32_t len=0;
4141  char* solver=get_str_from_str_or_direct(len);
4142 
4143  bool success=ui_classifier->set_solver(solver);
4144 
4145  SG_FREE(solver);
4146  return success;
4147 }
4148 
4149 bool CSGInterface::cmd_set_constraint_generator()
4150 {
4151  if (m_nrhs<2 || !create_return_values(0))
4152  return false;
4153 
4154  int32_t len=0;
4155  char* cg=get_str_from_str_or_direct(len);
4156 
4157  bool success=ui_classifier->set_constraint_generator(cg);
4158 
4159  SG_FREE(cg);
4160  return success;
4161 }
4162 
4163 bool CSGInterface::cmd_set_prior_probs()
4164 {
4165  if (m_nrhs<3 || !create_return_values(0))
4166  return false;
4167 
4168  CSalzbergWordStringKernel* kernel=
4169  (CSalzbergWordStringKernel*) ui_kernel->get_kernel();
4170  if (kernel->get_kernel_type()!=K_SALZBERG)
4171  SG_ERROR("SalzbergWordStringKernel required for setting prior probs!\n")
4172 
4173  float64_t pos_probs=get_real_from_real_or_str();
4174  float64_t neg_probs=get_real_from_real_or_str();
4175 
4176  kernel->set_prior_probs(pos_probs, neg_probs);
4177 
4178  return true;
4179 }
4180 
4181 bool CSGInterface::cmd_set_prior_probs_from_labels()
4182 {
4183  if (m_nrhs<2 || !create_return_values(0))
4184  return false;
4185 
4186  CSalzbergWordStringKernel* kernel=
4187  (CSalzbergWordStringKernel*) ui_kernel->get_kernel();
4188  if (kernel->get_kernel_type()!=K_SALZBERG)
4189  SG_ERROR("SalzbergWordStringKernel required for setting prior probs!\n")
4190 
4191  float64_t* lab=NULL;
4192  int32_t len=0;
4193  get_vector(lab, len);
4194 
4195  CBinaryLabels* labels=new CBinaryLabels(len);
4196  for (int32_t i=0; i<len; i++)
4197  {
4198  if (!labels->set_label(i, lab[i]))
4199  SG_ERROR("Couldn't set label %d (of %d): %f.\n", i, len, lab[i])
4200  }
4201  SG_FREE(lab);
4202 
4203  kernel->set_prior_probs_from_labels(labels);
4204 
4205  SG_UNREF(labels);
4206  return true;
4207 }
4208 
4209 
4210 
4211 
4214 bool CSGInterface::cmd_set_distance()
4215 {
4216  if (m_nrhs<3 || !create_return_values(0))
4217  return false;
4218 
4219  CDistance* distance=NULL;
4220  int32_t len=0;
4221  char* type=get_str_from_str_or_direct(len);
4222  char* dtype=get_str_from_str_or_direct(len);
4223 
4224  if (strmatch(type, "MINKOWSKI") && m_nrhs==4)
4225  {
4226  float64_t k=get_real_from_real_or_str();
4227  distance=ui_distance->create_minkowski(k);
4228  }
4229  else if (strmatch(type, "MANHATTAN"))
4230  {
4231  if (strmatch(dtype, "REAL"))
4232  distance=ui_distance->create_generic(D_MANHATTAN);
4233  else if (strmatch(dtype, "WORD"))
4234  distance=ui_distance->create_generic(D_MANHATTANWORD);
4235  }
4236  else if (strmatch(type, "HAMMING") && strmatch(dtype, "WORD"))
4237  {
4238  bool use_sign=false;
4239  if (m_nrhs==4)
4240  use_sign=get_bool_from_bool_or_str(); // optional
4241 
4242  distance=ui_distance->create_hammingword(use_sign);
4243  }
4244  else if (strmatch(type, "CANBERRA"))
4245  {
4246  if (strmatch(dtype, "REAL"))
4247  distance=ui_distance->create_generic(D_CANBERRA);
4248  else if (strmatch(dtype, "WORD"))
4249  distance=ui_distance->create_generic(D_CANBERRAWORD);
4250  }
4251  else if (strmatch(type, "CHEBYSHEW") && strmatch(dtype, "REAL"))
4252  {
4253  distance=ui_distance->create_generic(D_CHEBYSHEW);
4254  }
4255  else if (strmatch(type, "GEODESIC") && strmatch(dtype, "REAL"))
4256  {
4257  distance=ui_distance->create_generic(D_GEODESIC);
4258  }
4259  else if (strmatch(type, "JENSEN") && strmatch(dtype, "REAL"))
4260  {
4261  distance=ui_distance->create_generic(D_JENSEN);
4262  }
4263  else if (strmatch(type, "CHISQUARE") && strmatch(dtype, "REAL"))
4264  {
4265  distance=ui_distance->create_generic(D_CHISQUARE);
4266  }
4267  else if (strmatch(type, "TANIMOTO") && strmatch(dtype, "REAL"))
4268  {
4269  distance=ui_distance->create_generic(D_TANIMOTO);
4270  }
4271  else if (strmatch(type, "COSINE") && strmatch(dtype, "REAL"))
4272  {
4273  distance=ui_distance->create_generic(D_COSINE);
4274  }
4275  else if (strmatch(type, "BRAYCURTIS") && strmatch(dtype, "REAL"))
4276  {
4277  distance=ui_distance->create_generic(D_BRAYCURTIS);
4278  }
4279  else if (strmatch(type, "EUCLIDEAN"))
4280  {
4281  if (strmatch(dtype, "REAL"))
4282  distance=ui_distance->create_generic(D_EUCLIDEAN);
4283  else if (strmatch(dtype, "SPARSEREAL"))
4284  distance=ui_distance->create_generic(D_SPARSEEUCLIDEAN);
4285  }
4286  else
4288 
4289  SG_FREE(type);
4290  SG_FREE(dtype);
4291  return ui_distance->set_distance(distance);
4292 }
4293 
4294 bool CSGInterface::cmd_init_distance()
4295 {
4297  return true;
4298 }
4299 
4300 bool CSGInterface::cmd_get_distance_matrix()
4301 {
4302  if (m_nrhs!=2 || !create_return_values(1))
4303  return false;
4304 
4305  int32_t len=0;
4306  char* target=get_string(len);
4307 
4308  bool success=ui_distance->init_distance(target);
4309 
4310  if (success)
4311  {
4312  CDistance* distance=ui_distance->get_distance();
4313  if (!distance || !distance->has_features())
4314  SG_ERROR("No distance defined or not initialized.\n")
4315 
4316  SGMatrix<float64_t> dmatrix=distance->get_distance_matrix();
4317  set_matrix(dmatrix.matrix, dmatrix.num_rows, dmatrix.num_cols);
4318  }
4319 
4320  return success;
4321 }
4322 
4323 
4324 /* POIM */
4325 
4326 bool CSGInterface::cmd_get_SPEC_consensus()
4327 {
4328  if (m_nrhs!=1 || !create_return_values(1))
4329  return false;
4330 
4331  CKernel* kernel=ui_kernel->get_kernel();
4332  if (!kernel)
4333  SG_ERROR("No kernel.\n")
4334  if (kernel->get_kernel_type()!=K_COMMWORDSTRING)
4335  SG_ERROR("Only works for CommWordString kernels.\n")
4336 
4337  CSVM* svm=(CSVM*) ui_classifier->get_classifier();
4338  ASSERT(svm)
4339  int32_t num_suppvec=svm->get_num_support_vectors();
4340  int32_t* sv_idx=SG_MALLOC(int32_t, num_suppvec);
4341  float64_t* sv_weight=SG_MALLOC(float64_t, num_suppvec);
4342  int32_t num_feat=0;
4343 
4344  for (int32_t i=0; i<num_suppvec; i++)
4345  {
4346  sv_idx[i]=svm->get_support_vector(i);
4347  sv_weight[i]=svm->get_alpha(i);
4348  }
4349 
4350  char* consensus=((CCommWordStringKernel*) kernel)->compute_consensus(
4351  num_feat, num_suppvec, sv_idx, sv_weight);
4352  SG_FREE(sv_idx);
4353  SG_FREE(sv_weight);
4354 
4355  set_vector(consensus, num_feat);
4356  SG_FREE(consensus);
4357 
4358  return true;
4359 }
4360 
4361 bool CSGInterface::cmd_get_SPEC_scoring()
4362 {
4363  if (m_nrhs!=2 || !create_return_values(1))
4364  return false;
4365 
4366  int32_t max_order=get_int();
4367  CKernel* kernel=ui_kernel->get_kernel();
4368  if (!kernel)
4369  SG_ERROR("No kernel.\n")
4370 
4371  EKernelType ktype=kernel->get_kernel_type();
4372  if (ktype!=K_COMMWORDSTRING && ktype!=K_WEIGHTEDCOMMWORDSTRING)
4373  SG_ERROR("Only works for (Weighted) CommWordString kernels.\n")
4374 
4375  CSVM* svm=(CSVM*) ui_classifier->get_classifier();
4376  ASSERT(svm)
4377  int32_t num_suppvec=svm->get_num_support_vectors();
4378  int32_t* sv_idx=SG_MALLOC(int32_t, num_suppvec);
4379  float64_t* sv_weight=SG_MALLOC(float64_t, num_suppvec);
4380  int32_t num_feat=0;
4381  int32_t num_sym=0;
4382 
4383  for (int32_t i=0; i<num_suppvec; i++)
4384  {
4385  sv_idx[i]=svm->get_support_vector(i);
4386  sv_weight[i]=svm->get_alpha(i);
4387  }
4388 
4389  if ((max_order<1) || (max_order>8))
4390  {
4391  SG_WARNING("max_order out of range 1..8 (%d). setting to 1\n", max_order)
4392  max_order=1;
4393  }
4394 
4395  float64_t* position_weights=NULL;
4396  if (ktype==K_COMMWORDSTRING)
4397  position_weights=((CCommWordStringKernel*) kernel)->compute_scoring(
4398  max_order, num_feat, num_sym, NULL,
4399  num_suppvec, sv_idx, sv_weight);
4400  else
4401  position_weights=((CWeightedCommWordStringKernel*) kernel)->compute_scoring(
4402  max_order, num_feat, num_sym, NULL,
4403  num_suppvec, sv_idx, sv_weight);
4404  SG_FREE(sv_idx);
4405  SG_FREE(sv_weight);
4406 
4407  set_matrix(position_weights, num_sym, num_feat);
4408  SG_FREE(position_weights);
4409 
4410  return true;
4411 }
4412 
4413 bool CSGInterface::cmd_get_WD_consensus()
4414 {
4415  if (m_nrhs!=1 || !create_return_values(1))
4416  return false;
4417 
4418  CKernel* kernel=ui_kernel->get_kernel();
4419  if (!kernel)
4420  SG_ERROR("No kernel.\n")
4421  if (kernel->get_kernel_type()!=K_WEIGHTEDDEGREEPOS)
4422  SG_ERROR("Only works for Weighted Degree Position kernels.\n")
4423 
4424  CSVM* svm=(CSVM*) ui_classifier->get_classifier();
4425  ASSERT(svm)
4426  int32_t num_suppvec=svm->get_num_support_vectors();
4427  int32_t* sv_idx=SG_MALLOC(int32_t, num_suppvec);
4428  float64_t* sv_weight=SG_MALLOC(float64_t, num_suppvec);
4429  int32_t num_feat=0;
4430 
4431  for (int32_t i=0; i<num_suppvec; i++)
4432  {
4433  sv_idx[i]=svm->get_support_vector(i);
4434  sv_weight[i]=svm->get_alpha(i);
4435  }
4436 
4437  char* consensus=((CWeightedDegreePositionStringKernel*) kernel)->compute_consensus(
4438  num_feat, num_suppvec, sv_idx, sv_weight);
4439  SG_FREE(sv_idx);
4440  SG_FREE(sv_weight);
4441 
4442  set_vector(consensus, num_feat);
4443  SG_FREE(consensus);
4444 
4445  return true;
4446 }
4447 
4448 bool CSGInterface::cmd_compute_POIM_WD()
4449 {
4450  if (m_nrhs!=3 || !create_return_values(1))
4451  return false;
4452 
4453  int32_t max_order=get_int();
4454  float64_t* distribution=NULL;
4455  int32_t num_dfeat=0;
4456  int32_t num_dvec=0;
4457  get_matrix(distribution, num_dfeat, num_dvec);
4458 
4459  if (!distribution)
4460  SG_ERROR("Wrong distribution.\n")
4461 
4462  CKernel* kernel=ui_kernel->get_kernel();
4463  if (!kernel)
4464  SG_ERROR("No Kernel.\n")
4465  if (kernel->get_kernel_type()!=K_WEIGHTEDDEGREEPOS)
4466  SG_ERROR("Only works for Weighted Degree Position kernels.\n")
4467 
4468  int32_t seqlen=0;
4469  int32_t num_sym=0;
4471  (((CWeightedDegreePositionStringKernel*) kernel)->get_lhs());
4472  ASSERT(sfeat)
4473  seqlen=sfeat->get_max_vector_length();
4474  num_sym=(int32_t) sfeat->get_num_symbols();
4475 
4476  if (num_dvec!=seqlen || num_dfeat!=num_sym)
4477  {
4478  SG_ERROR("distribution should have (seqlen x num_sym) elements"
4479  "(seqlen: %d vs. %d symbols: %d vs. %d)\n", seqlen,
4480  num_dvec, num_sym, num_dfeat);
4481  }
4482 
4483  CSVM* svm=(CSVM*) ui_classifier->get_classifier();
4484  ASSERT(svm)
4485  int32_t num_suppvec=svm->get_num_support_vectors();
4486  int32_t* sv_idx=SG_MALLOC(int32_t, num_suppvec);
4487  float64_t* sv_weight=SG_MALLOC(float64_t, num_suppvec);
4488 
4489  for (int32_t i=0; i<num_suppvec; i++)
4490  {
4491  sv_idx[i]=svm->get_support_vector(i);
4492  sv_weight[i]=svm->get_alpha(i);
4493  }
4494 
4495  /*
4496  if ((max_order < 1) || (max_order > 12))
4497  {
4498  SG_WARNING("max_order out of range 1..12 (%d). setting to 1.\n", max_order)
4499  max_order=1;
4500  }
4501  */
4502 
4503  float64_t* position_weights;
4504  position_weights=((CWeightedDegreePositionStringKernel*) kernel)->compute_POIM(
4505  max_order, seqlen, num_sym, NULL,
4506  num_suppvec, sv_idx, sv_weight, distribution);
4507  SG_FREE(sv_idx);
4508  SG_FREE(sv_weight);
4509 
4510  set_matrix(position_weights, num_sym, seqlen);
4511  SG_FREE(position_weights);
4512 
4513  return true;
4514  }
4515 
4516  bool CSGInterface::cmd_get_WD_scoring()
4517  {
4518  if (m_nrhs!=2 || !create_return_values(1))
4519  return false;
4520 
4521  int32_t max_order=get_int();
4522 
4523  CKernel* kernel=ui_kernel->get_kernel();
4524  if (!kernel)
4525  SG_ERROR("No kernel.\n")
4526  if (kernel->get_kernel_type()!=K_WEIGHTEDDEGREEPOS)
4527  SG_ERROR("Only works for Weighted Degree Position kernels.\n")
4528 
4529  CSVM* svm=(CSVM*) ui_classifier->get_classifier();
4530  ASSERT(svm)
4531  int32_t num_suppvec=svm->get_num_support_vectors();
4532  int32_t* sv_idx=SG_MALLOC(int32_t, num_suppvec);
4533  float64_t* sv_weight=SG_MALLOC(float64_t, num_suppvec);
4534  int32_t num_feat=0;
4535  int32_t num_sym=0;
4536 
4537  for (int32_t i=0; i<num_suppvec; i++)
4538  {
4539  sv_idx[i]=svm->get_support_vector(i);
4540  sv_weight[i]=svm->get_alpha(i);
4541  }
4542 
4543  if ((max_order<1) || (max_order>12))
4544  {
4545  SG_WARNING("max_order out of range 1..12 (%d). setting to 1\n", max_order)
4546  max_order=1;
4547  }
4548 
4549  float64_t* position_weights=
4550  ((CWeightedDegreePositionStringKernel*) kernel)->compute_scoring(
4551  max_order, num_feat, num_sym, NULL, num_suppvec, sv_idx, sv_weight);
4552  SG_FREE(sv_idx);
4553  SG_FREE(sv_weight);
4554 
4555  set_matrix(position_weights, num_sym, num_feat);
4556  SG_FREE(position_weights);
4557 
4558  return true;
4559 }
4560 
4561 
4562 /* Classifier */
4563 
4564 bool CSGInterface::cmd_classify()
4565 {
4566  if (m_nrhs!=1 || !create_return_values(1))
4567  return false;
4568 
4569  if (!ui_kernel->get_kernel() ||
4570  ui_kernel->get_kernel()->get_kernel_type()!=K_CUSTOM)
4571  {
4572  CFeatures* feat=ui_features->get_test_features();
4573  if (!feat)
4574  SG_ERROR("No features found.\n")
4575  }
4576 
4577  CLabels* labels=ui_classifier->classify();
4578  if (!labels)
4579  SG_ERROR("Classify failed\n")
4580 
4581  int32_t num_vec=labels->get_num_labels();
4582  float64_t* result=SG_MALLOC(float64_t, num_vec);
4583  for (int32_t i=0; i<num_vec; i++)
4584  {
4585  float64_t value = 0;
4586  switch (labels->get_label_type())
4587  {
4588  case LT_REGRESSION:
4589  value = ((CRegressionLabels*) labels)->get_label(i);
4590  break;
4591  case LT_BINARY:
4592  value = ((CBinaryLabels*) labels)->get_value(i);
4593  break;
4594  case LT_MULTICLASS:
4595  value = ((CMulticlassLabels*) labels)->get_label(i);
4596  break;
4597  default:
4599  break;
4600  }
4601  result[i]=value;
4602  }
4603  SG_UNREF(labels);
4604 
4605  set_vector(result, num_vec);
4606  SG_FREE(result);
4607 
4608  return true;
4609 }
4610 
4611 bool CSGInterface::cmd_classify_example()
4612 {
4613  if (m_nrhs!=2 || !create_return_values(1))
4614  return false;
4615 
4616  int32_t idx=get_int();
4617  float64_t result=0;
4618 
4619  if (!ui_classifier->classify_example(idx, result))
4620  SG_ERROR("Classify_example failed.\n")
4621 
4622  set_real(result);
4623 
4624  return true;
4625 }
4626 
4627 bool CSGInterface::cmd_get_classifier()
4628 {
4629  if (m_nrhs<1 || m_nrhs>2 || !create_return_values(2))
4630  return false;
4631 
4632  int32_t idx=-1;
4633  if (m_nrhs==2)
4634  idx=get_int();
4635 
4636  float64_t* bias=NULL;
4637  float64_t* weights=NULL;
4638  int32_t rows=0;
4639  int32_t cols=0;
4640  int32_t brows=0;
4641  int32_t bcols=0;
4642 
4643  if (!ui_classifier->get_trained_classifier(
4644  weights, rows, cols, bias, brows, bcols, idx))
4645  return false;
4646 
4647  //SG_PRINT("brows %d, bcols %d\n", brows, bcols)
4648  //CMath::display_matrix(bias, brows, bcols);
4649  set_matrix(bias, brows, bcols);
4650  SG_FREE(bias);
4651 
4652  //SG_PRINT("rows %d, cols %d\n", rows, cols)
4653  //CMath::display_matrix(weights, rows, cols);
4654  set_matrix(weights, rows, cols);
4655  SG_FREE(weights);
4656 
4657  return true;
4658 }
4659 
4660 bool CSGInterface::cmd_new_classifier()
4661 {
4662  if (m_nrhs<2 || !create_return_values(0))
4663  return false;
4664 
4665  int32_t len=0;
4666  char* name=get_str_from_str_or_direct(len);
4667  int32_t d=6;
4668  int32_t from_d=40;
4669 
4670  if (m_nrhs>2)
4671  {
4672  d=get_int_from_int_or_str();
4673 
4674  if (m_nrhs>3)
4675  from_d=get_int_from_int_or_str();
4676  }
4677 
4678  bool success=ui_classifier->new_classifier(name, d, from_d);
4679 
4680  SG_FREE(name);
4681  return success;
4682 }
4683 
4684 bool CSGInterface::cmd_save_classifier()
4685 {
4686  if (m_nrhs<2 || !create_return_values(0))
4687  return false;
4688 
4689  int32_t len=0;
4690  char* filename=get_str_from_str_or_direct(len);
4691 
4692  bool success=ui_classifier->save(filename);
4693 
4694  SG_FREE(filename);
4695  return success;
4696 }
4697 
4698 bool CSGInterface::cmd_load_classifier()
4699 {
4700  if (m_nrhs<3 || !create_return_values(0))
4701  return false;
4702 
4703  int32_t len=0;
4704  char* filename=get_str_from_str_or_direct(len);
4705  char* type=get_str_from_str_or_direct(len);
4706 
4707  bool success=ui_classifier->load(filename, type);
4708 
4709  if (dynamic_cast<CKernelMachine*>(ui_classifier->get_classifier()))
4710  {
4711  CKernelMachine* kernel_machine = dynamic_cast<CKernelMachine*>(ui_classifier->get_classifier());
4712  ui_features->set_train_features(kernel_machine->get_kernel()->get_lhs());
4713  ui_features->set_test_features(kernel_machine->get_kernel()->get_rhs());
4714  ui_kernel->set_kernel(kernel_machine->get_kernel());
4715  }
4716 
4717  SG_FREE(filename);
4718  SG_FREE(type);
4719  return success;
4720 }
4721 
4722 bool CSGInterface::cmd_get_num_svms()
4723 {
4724  if (m_nrhs!=1 || !create_return_values(1))
4725  return false;
4726 
4727  set_int(ui_classifier->get_num_svms());
4728 
4729  return true;
4730 }
4731 
4732 
4733 bool CSGInterface::cmd_get_svm()
4734 {
4735  return cmd_get_classifier();
4736 }
4737 
4738 bool CSGInterface::cmd_set_svm()
4739 {
4740  if (m_nrhs!=3 || !create_return_values(0))
4741  return false;
4742 
4743  float64_t bias=get_real();
4744 
4745  float64_t* alphas=NULL;
4746  int32_t num_feat_alphas=0;
4747  int32_t num_vec_alphas=0;
4748  get_matrix(alphas, num_feat_alphas, num_vec_alphas);
4749 
4750  if (!alphas)
4751  SG_ERROR("No proper alphas given.\n")
4752  if (num_vec_alphas!=2)
4753  SG_ERROR("Not 2 vectors in alphas.\n")
4754 
4755  CSVM* svm=(CSVM*) ui_classifier->get_classifier();
4756  if (!svm)
4757  SG_ERROR("No SVM object available.\n")
4758 
4759  svm->create_new_model(num_feat_alphas);
4760  svm->set_bias(bias);
4761 
4762  int32_t num_support_vectors=svm->get_num_support_vectors();
4763  for (int32_t i=0; i<num_support_vectors; i++)
4764  {
4765  svm->set_alpha(i, alphas[i]);
4766  svm->set_support_vector(i, (int32_t) alphas[i+num_support_vectors]);
4767  }
4768  SG_FREE(alphas);
4769 
4770  return true;
4771 }
4772 
4773 bool CSGInterface::cmd_set_linear_classifier()
4774 {
4775  if (m_nrhs!=3 || !create_return_values(0))
4776  return false;
4777 
4778  float64_t bias=get_real();
4779 
4780  float64_t* w=NULL;
4781  int32_t len=0;
4782  get_vector(w, len);
4783 
4784  if (!len)
4785  SG_ERROR("No proper weight vector given.\n")
4786 
4787  CLinearMachine* c=(CLinearMachine*) ui_classifier->get_classifier();
4788  if (!c)
4789  SG_ERROR("No Linear Classifier object available.\n")
4790 
4791  c->set_w(SGVector<float64_t>(w, len));
4792  c->set_bias(bias);
4793  return true;
4794 }
4795 
4796 bool CSGInterface::cmd_get_svm_objective()
4797 {
4798  if (m_nrhs!=1 || !create_return_values(1))
4799  return false;
4800 
4801  CSVM* svm=(CSVM*) ui_classifier->get_classifier();
4802  if (!svm)
4803  SG_ERROR("No SVM set.\n")
4804 
4805  set_real(svm->get_objective());
4806 
4807  return true;
4808 }
4809 
4810 bool CSGInterface::cmd_compute_svm_primal_objective()
4811 {
4812  return do_compute_objective(SVM_PRIMAL);
4813 }
4814 
4815 bool CSGInterface::cmd_compute_svm_dual_objective()
4816 {
4817  return do_compute_objective(SVM_DUAL);
4818 }
4819 
4820 bool CSGInterface::cmd_compute_mkl_dual_objective()
4821 {
4822  return do_compute_objective(MKL_DUAL);
4823 }
4824 
4825 bool CSGInterface::cmd_compute_relative_mkl_duality_gap()
4826 {
4827  return do_compute_objective(MKL_RELATIVE_DUALITY_GAP);
4828 }
4829 
4830 bool CSGInterface::cmd_compute_absolute_mkl_duality_gap()
4831 {
4832  return do_compute_objective(MKL_ABSOLUTE_DUALITY_GAP);
4833 }
4834 
4835 bool CSGInterface::do_compute_objective(E_WHICH_OBJ obj)
4836 {
4837  if (m_nrhs!=1 || !create_return_values(1))
4838  return false;
4839 
4840  float64_t result=23.5;
4841 
4842  CSVM* svm=(CSVM*) ui_classifier->get_classifier();
4843  if (!svm)
4844  SG_ERROR("No SVM set.\n")
4845 
4846  CLabels* trainlabels=NULL;
4847  trainlabels=ui_labels->get_train_labels();
4848 
4849  if (!trainlabels)
4850  SG_ERROR("No trainlabels available.\n")
4851 
4852  CKernel* kernel=ui_kernel->get_kernel();
4853  if (!kernel)
4854  SG_ERROR("No kernel available.\n")
4855 
4856  if (!ui_kernel->is_initialized() || !kernel->has_features())
4857  SG_ERROR("Kernel not initialized.\n")
4858 
4859  ((CKernelMachine*) svm)->set_labels(trainlabels);
4860  ((CKernelMachine*) svm)->set_kernel(kernel);
4861 
4862 
4863  switch (obj)
4864  {
4865  case SVM_PRIMAL:
4866  result=svm->compute_svm_primal_objective();
4867  break;
4868  case SVM_DUAL:
4869  result=svm->compute_svm_dual_objective();
4870  break;
4871  case MKL_PRIMAL:
4873  result=((CMKL*) svm)->compute_mkl_primal_objective();
4874  break;
4875  case MKL_DUAL:
4877  result=((CMKL*) svm)->compute_mkl_dual_objective();
4878  break;
4879  case MKL_RELATIVE_DUALITY_GAP:
4880  {
4882  float64_t primal=((CMKL*) svm)->compute_mkl_dual_objective();
4883  float64_t dual=((CMKL*) svm)->compute_mkl_primal_objective();
4884  result=(primal-dual)/dual;
4885  }
4886  break;
4887  case MKL_ABSOLUTE_DUALITY_GAP:
4888  {
4890  float64_t primal=((CMKL*) svm)->compute_mkl_dual_objective();
4891  float64_t dual=((CMKL*) svm)->compute_mkl_primal_objective();
4892  result=dual-primal;
4893  }
4894  break;
4895  default:
4896  SG_SERROR("Error calling do_compute_objective\n")
4897  return false;
4898  };
4899 
4900  set_real(result);
4901  return true;
4902 }
4903 
4904 bool CSGInterface::cmd_train_classifier()
4905 {
4906  if (m_nrhs<1 || !create_return_values(0))
4907  return false;
4908 
4909  CMachine* classifier=ui_classifier->get_classifier();
4910  if (!classifier)
4911  SG_ERROR("No classifier available.\n")
4912 
4913  EMachineType type=classifier->get_classifier_type();
4914  switch (type)
4915  {
4916  case CT_LIGHT:
4917  case CT_LIGHTONECLASS:
4918  case CT_LIBSVM:
4919  case CT_SCATTERSVM:
4920  case CT_MPD:
4921  case CT_GPBT:
4922  case CT_CPLEXSVM:
4923  case CT_GMNPSVM:
4924  case CT_GNPPSVM:
4925  case CT_KERNELPERCEPTRON:
4926  case CT_LIBSVR:
4927  case CT_LIBSVMMULTICLASS:
4928  case CT_LIBSVMONECLASS:
4929  case CT_SVRLIGHT:
4930  case CT_LARANK:
4931  return ui_classifier->train_svm();
4932  case CT_MKLMULTICLASS:
4933  return ui_classifier->train_mkl_multiclass();
4934  case CT_MKLCLASSIFICATION:
4935  case CT_MKLREGRESSION:
4936  case CT_MKLONECLASS:
4937  return ui_classifier->train_mkl();
4938 
4940  return ui_classifier->train_krr();
4941 
4942  case CT_KNN:
4943  {
4944  if (m_nrhs<2)
4945  return false;
4946 
4947  int32_t k=get_int_from_int_or_str();
4948 
4949  return ui_classifier->train_knn(k);
4950  }
4951 
4952  case CT_KMEANS:
4953  {
4954  if (m_nrhs<3)
4955  return false;
4956 
4957  int32_t k=get_int_from_int_or_str();
4958  int32_t max_iter=get_int_from_int_or_str();
4959 
4960  return ui_classifier->train_clustering(k, max_iter);
4961  }
4962 
4963  case CT_HIERARCHICAL:
4964  {
4965  if (m_nrhs<2)
4966  return false;
4967 
4968  int32_t merges=get_int_from_int_or_str();
4969 
4970  return ui_classifier->train_clustering(merges);
4971  }
4972 
4973  case CT_LDA:
4974  {
4975  float64_t gamma=0;
4976  if (m_nrhs==2)
4977  gamma=get_real_from_real_or_str();
4978 
4979  return ui_classifier->train_linear(gamma);
4980  }
4981 
4982  case CT_PERCEPTRON:
4983  case CT_SVMLIN:
4984  case CT_SVMPERF:
4985  case CT_SVMOCAS:
4986  case CT_SVMSGD:
4987  case CT_LPM:
4988  case CT_LPBOOST:
4989  case CT_LIBLINEAR:
4990  return ui_classifier->train_linear();
4991 
4992  case CT_WDSVMOCAS:
4993  return ui_classifier->train_wdocas();
4994 
4995  default:
4996  SG_ERROR("Unknown classifier type %d.\n", type)
4997  }
4998 
4999  return false;
5000 }
5001 
5002 bool CSGInterface::cmd_do_auc_maximization()
5003 {
5004  if (m_nrhs!=2 || !create_return_values(0))
5005  return false;
5006 
5007  bool do_auc=get_bool_from_bool_or_str();
5008 
5009  return ui_classifier->set_do_auc_maximization(do_auc);
5010 }
5011 
5012 bool CSGInterface::cmd_set_perceptron_parameters()
5013 {
5014  if (m_nrhs!=3 || !create_return_values(0))
5015  return false;
5016 
5017  float64_t lernrate=get_real_from_real_or_str();
5018  int32_t maxiter=get_int_from_int_or_str();
5019 
5020  return ui_classifier->set_perceptron_parameters(lernrate, maxiter);
5021 }
5022 
5023 bool CSGInterface::cmd_set_svm_qpsize()
5024 {
5025  if (m_nrhs!=2 || !create_return_values(0))
5026  return false;
5027 
5028  int32_t qpsize=get_int_from_int_or_str();
5029 
5030  return ui_classifier->set_svm_qpsize(qpsize);
5031 }
5032 
5033 bool CSGInterface::cmd_set_svm_max_qpsize()
5034 {
5035  if (m_nrhs!=2 || !create_return_values(0))
5036  return false;
5037 
5038  int32_t max_qpsize=get_int_from_int_or_str();
5039 
5040  return ui_classifier->set_svm_max_qpsize(max_qpsize);
5041 }
5042 
5043 bool CSGInterface::cmd_set_svm_bufsize()
5044 {
5045  if (m_nrhs!=2 || !create_return_values(0))
5046  return false;
5047 
5048  int32_t bufsize=get_int_from_int_or_str();
5049 
5050  return ui_classifier->set_svm_bufsize(bufsize);
5051 }
5052 
5053 bool CSGInterface::cmd_set_svm_C()
5054 {
5055  if (m_nrhs<2 || !create_return_values(0))
5056  return false;
5057 
5058  float64_t C1=get_real_from_real_or_str();
5059  float64_t C2=C1;
5060 
5061  if (m_nrhs==3)
5062  C2=get_real_from_real_or_str();
5063 
5064  return ui_classifier->set_svm_C(C1, C2);
5065 }
5066 
5067 bool CSGInterface::cmd_set_svm_epsilon()
5068 {
5069  if (m_nrhs!=2 || !create_return_values(0))
5070  return false;
5071 
5072  float64_t epsilon=get_real_from_real_or_str();
5073 
5074  return ui_classifier->set_svm_epsilon(epsilon);
5075 }
5076 
5077 bool CSGInterface::cmd_set_svr_tube_epsilon()
5078 {
5079  if (m_nrhs!=2 || !create_return_values(0))
5080  return false;
5081 
5082  float64_t tube_epsilon=get_real_from_real_or_str();
5083 
5084  return ui_classifier->set_svr_tube_epsilon(tube_epsilon);
5085 }
5086 
5087 bool CSGInterface::cmd_set_svm_nu()
5088 {
5089  if (m_nrhs!=2 || !create_return_values(0))
5090  return false;
5091 
5092  float64_t nu=get_real_from_real_or_str();
5093 
5094  return ui_classifier->set_svm_nu(nu);
5095 }
5096 
5097 bool CSGInterface::cmd_set_svm_mkl_parameters()
5098 {
5099  if (m_nrhs<3 || m_nrhs>4 || !create_return_values(0))
5100  return false;
5101 
5102  float64_t weight_epsilon=get_real_from_real_or_str();
5103  float64_t C_mkl=get_real_from_real_or_str();
5104  float64_t mkl_norm=1.0;
5105 
5106  if (m_nrhs==4)
5107  mkl_norm=get_real_from_real_or_str();
5108 
5109  return ui_classifier->set_svm_mkl_parameters(weight_epsilon, C_mkl, mkl_norm);
5110 }
5111 
5112 bool CSGInterface::cmd_set_elasticnet_lambda()
5113 {
5114  if (m_nrhs!=2 || !create_return_values(0))
5115  return false;
5116  float64_t lambda=get_real_from_real_or_str();
5117  return ui_classifier->set_elasticnet_lambda(lambda);
5118 }
5119 
5120 bool CSGInterface::cmd_set_mkl_block_norm()
5121 {
5122  if (m_nrhs!=2 || !create_return_values(0))
5123  return false;
5124  float64_t bnorm=get_real_from_real_or_str();
5125  return ui_classifier->set_mkl_block_norm(bnorm);
5126 }
5127 
5128 
5129 bool CSGInterface::cmd_set_max_train_time()
5130 {
5131  if (m_nrhs!=2 || !create_return_values(0))
5132  return false;
5133 
5134  float64_t max_train_time=get_real_from_real_or_str();
5135 
5136  return ui_classifier->set_max_train_time(max_train_time);
5137 }
5138 
5139 bool CSGInterface::cmd_set_svm_shrinking_enabled()
5140 {
5141  if (m_nrhs!=2 || !create_return_values(0))
5142  return false;
5143 
5144  bool shrinking_enabled=get_bool_from_bool_or_str();
5145 
5146  return ui_classifier->set_svm_shrinking_enabled(shrinking_enabled);
5147 }
5148 
5149 bool CSGInterface::cmd_set_svm_batch_computation_enabled()
5150 {
5151  if (m_nrhs!=2 || !create_return_values(0))
5152  return false;
5153 
5154  bool batch_computation_enabled=get_bool_from_bool_or_str();
5155 
5156  return ui_classifier->set_svm_batch_computation_enabled(
5157  batch_computation_enabled);
5158 }
5159 
5160 bool CSGInterface::cmd_set_svm_linadd_enabled()
5161 {
5162  if (m_nrhs!=2 || !create_return_values(0))
5163  return false;
5164 
5165  bool linadd_enabled=get_bool_from_bool_or_str();
5166 
5167  return ui_classifier->set_svm_linadd_enabled(linadd_enabled);
5168 }
5169 
5170 bool CSGInterface::cmd_set_svm_bias_enabled()
5171 {
5172  if (m_nrhs!=2 || !create_return_values(0))
5173  return false;
5174 
5175  bool bias_enabled=get_bool_from_bool_or_str();
5176 
5177  return ui_classifier->set_svm_bias_enabled(bias_enabled);
5178 }
5179 
5180 bool CSGInterface::cmd_set_mkl_interleaved_enabled()
5181 {
5182  if (m_nrhs!=2 || !create_return_values(0))
5183  return false;
5184 
5185  bool interleaved_enabled=get_bool_from_bool_or_str();
5186 
5187  return ui_classifier->set_mkl_interleaved_enabled(interleaved_enabled);
5188 }
5189 
5190 bool CSGInterface::cmd_set_krr_tau()
5191 {
5192  if (m_nrhs!=2 || !create_return_values(0))
5193  return false;
5194 
5195  float64_t tau=get_real_from_real_or_str();
5196 
5197  return ui_classifier->set_krr_tau(tau);
5198 }
5199 
5200 
5201 /* Preproc */
5202 
5203 bool CSGInterface::cmd_add_preproc()
5204 {
5205  if (m_nrhs<2 || !create_return_values(0))
5206  return false;
5207 
5208  int32_t len=0;
5209  char* type=get_str_from_str_or_direct(len);
5210  CPreprocessor* preproc=NULL;
5211 
5212  if (strmatch(type, "NORMONE"))
5213  preproc=ui_preproc->create_generic(P_NORMONE);
5214  else if (strmatch(type, "LOGPLUSONE"))
5215  preproc=ui_preproc->create_generic(P_LOGPLUSONE);
5216  else if (strmatch(type, "SORTWORDSTRING"))
5217  preproc=ui_preproc->create_generic(P_SORTWORDSTRING);
5218  else if (strmatch(type, "SORTULONGSTRING"))
5219  preproc=ui_preproc->create_generic(P_SORTULONGSTRING);
5220  else if (strmatch(type, "DECOMPRESSCHARSTRING"))
5221  preproc=ui_preproc->create_generic(P_DECOMPRESSCHARSTRING);
5222  else if (strmatch(type, "SORTWORD"))
5223  preproc=ui_preproc->create_generic(P_SORTWORD);
5224 
5225  else if (strmatch(type, "PRUNEVARSUBMEAN"))
5226  {
5227  bool divide_by_std=false;
5228  if (m_nrhs==3)
5229  divide_by_std=get_bool_from_bool_or_str();
5230 
5231  preproc=ui_preproc->create_prunevarsubmean(divide_by_std);
5232  }
5233 
5234 #ifdef HAVE_LAPACK
5235  else if (strmatch(type, "PCA") && m_nrhs==4)
5236  {
5237  bool do_whitening=get_bool_from_bool_or_str();
5238  float64_t threshold=get_real_from_real_or_str();
5239 
5240  preproc=ui_preproc->create_pca(do_whitening, threshold);
5241  }
5242 #endif
5243 
5244  else
5246 
5247  SG_FREE(type);
5248  return ui_preproc->add_preproc(preproc);
5249 }
5250 
5251 bool CSGInterface::cmd_del_preproc()
5252 {
5253  if (m_nrhs!=1 || !create_return_values(0))
5254  return false;
5255 
5256  return ui_preproc->del_preproc();
5257 }
5258 
5259 bool CSGInterface::cmd_attach_preproc()
5260 {
5261  if (m_nrhs<2 || !create_return_values(0))
5262  return false;
5263 
5264  int32_t len=0;
5265  char* target=get_str_from_str_or_direct(len);
5266 
5267  bool do_force=false;
5268  if (m_nrhs==3)
5269  do_force=get_bool_from_bool_or_str();
5270 
5271  bool success=ui_preproc->attach_preproc(target, do_force);
5272 
5273  SG_FREE(target);
5274  return success;
5275 }
5276 
5277 bool CSGInterface::cmd_clean_preproc()
5278 {
5279  if (m_nrhs!=1 || !create_return_values(0))
5280  return false;
5281 
5282  return ui_preproc->clean_preproc();
5283 }
5284 
5285 /* Converter */
5286 
5287 bool CSGInterface::cmd_set_converter()
5288 {
5289  int32_t len=0;
5290  char* type=get_str_from_str_or_direct(len);
5291 
5292  if (strmatch(type, "lle"))
5293  {
5294  int32_t k = get_int_from_int_or_str();
5295  ui_converter->create_locallylinearembedding(k);
5296  return true;
5297  }
5298  if (strmatch(type, "npe"))
5299  {
5300  int32_t k = get_int_from_int_or_str();
5301  ui_converter->create_neighborhoodpreservingembedding(k);
5302  return true;
5303  }
5304  if (strmatch(type, "ltsa"))
5305  {
5306  int32_t k = get_int_from_int_or_str();
5307  ui_converter->create_localtangentspacealignment(k);
5308  return true;
5309  }
5310  if (strmatch(type, "lltsa"))
5311  {
5312  int32_t k = get_int_from_int_or_str();
5313  ui_converter->create_linearlocaltangentspacealignment(k);
5314  return true;
5315  }
5316  if (strmatch(type, "hlle"))
5317  {
5318  int32_t k = get_int_from_int_or_str();
5319  ui_converter->create_hessianlocallylinearembedding(k);
5320  return true;
5321  }
5322  if (strmatch(type, "laplacian_eigenmaps"))
5323  {
5324  int32_t k = get_int_from_int_or_str();
5325  int32_t width = get_real_from_real_or_str();
5326  ui_converter->create_laplacianeigenmaps(k,width);
5327  return true;
5328  }
5329  if (strmatch(type, "lpp"))
5330  {
5331  int32_t k = get_int_from_int_or_str();
5332  int32_t width = get_real_from_real_or_str();
5333  ui_converter->create_localitypreservingprojections(k,width);
5334  return true;
5335  }
5336  if (strmatch(type, "diffusion_maps"))
5337  {
5338  int32_t t = get_int_from_int_or_str();
5339  int32_t width = get_real_from_real_or_str();
5340  ui_converter->create_diffusionmaps(t,width);
5341  return true;
5342  }
5343  if (strmatch(type, "isomap"))
5344  {
5345  int32_t k = get_int_from_int_or_str();
5346  ui_converter->create_isomap(k);
5347  return true;
5348  }
5349  if (strmatch(type, "mds"))
5350  {
5351  ui_converter->create_multidimensionalscaling();
5352  return true;
5353  }
5354  if (strmatch(type, "jade"))
5355  {
5356  ui_converter->create_jade();
5357  return true;
5358  }
5359  return false;
5360 }
5361 
5362 bool CSGInterface::cmd_apply_converter()
5363 {
5364  if (m_nrhs!=1 || !create_return_values(1))
5365  return false;
5366 
5367  CDenseFeatures<float64_t>* conv_features = ui_converter->apply();
5368  SGMatrix<float64_t> converted_mat = conv_features->get_feature_matrix();
5369  set_matrix(converted_mat.matrix,converted_mat.num_rows,converted_mat.num_cols);
5370  return true;
5371 }
5372 
5373 bool CSGInterface::cmd_embed()
5374 {
5375  int32_t target_dim = get_int_from_int_or_str();
5376 
5377  if (m_nrhs!=1 || !create_return_values(1))
5378  return false;
5379 
5380  CDenseFeatures<float64_t>* embedding = ui_converter->embed(target_dim);
5381  SGMatrix<float64_t> embedding_matrix = embedding->get_feature_matrix();
5382  set_matrix(embedding_matrix.matrix,embedding_matrix.num_cols,embedding_matrix.num_rows);
5383  return true;
5384 }
5385 
5386 /* HMM */
5387 
5388 bool CSGInterface::cmd_new_plugin_estimator()
5389 {
5390  if (m_nrhs<2 || !create_return_values(0))
5391  return false;
5392 
5393  float64_t pos_pseudo=get_real_from_real_or_str();
5394  float64_t neg_pseudo=get_real_from_real_or_str();
5395 
5396  return ui_pluginestimate->new_estimator(pos_pseudo, neg_pseudo);
5397 }
5398 
5399 bool CSGInterface::cmd_train_estimator()
5400 {
5401  if (m_nrhs!=1 || !create_return_values(0))
5402  return false;
5403 
5404  return ui_pluginestimate->train();
5405 }
5406 
5407 bool CSGInterface::cmd_plugin_estimate_classify_example()
5408 {
5409  if (m_nrhs!=2 || !create_return_values(1))
5410  return false;
5411 
5412  int32_t idx=get_int();
5413  float64_t result=ui_pluginestimate->apply_one(idx);
5414 
5415  set_vector(&result, 1);
5416  return true;
5417 }
5418 
5419 bool CSGInterface::cmd_plugin_estimate_classify()
5420 {
5421  if (m_nrhs!=1 || !create_return_values(1))
5422  return false;
5423 
5424  CFeatures* feat=ui_features->get_test_features();
5425  if (!feat)
5426  SG_ERROR("No features found.\n")
5427 
5428  int32_t num_vec=feat->get_num_vectors();
5429  float64_t* result=SG_MALLOC(float64_t, num_vec);
5430  CLabels* labels=ui_pluginestimate->apply();
5431  for (int32_t i=0; i<num_vec; i++)
5432  result[i]=((CRegressionLabels*) labels)->get_label(i);
5433  SG_UNREF(labels);
5434 
5435  set_vector(result, num_vec);
5436  SG_FREE(result);
5437 
5438  return true;
5439 }
5440 
5441 bool CSGInterface::cmd_set_plugin_estimate()
5442 {
5443  if (m_nrhs!=3 || !create_return_values(0))
5444  return false;
5445 
5446  float64_t* emission_probs=NULL;
5447  int32_t num_probs=0;
5448  int32_t num_vec=0;
5449  get_matrix(emission_probs, num_probs, num_vec);
5450 
5451  if (num_vec!=2)
5452  SG_ERROR("Need at least 1 set of positive and 1 set of negative params.\n")
5453 
5454  float64_t* pos_params=emission_probs;
5455  float64_t* neg_params=&(emission_probs[num_probs]);
5456 
5457  float64_t* model_sizes=NULL;
5458  int32_t len=0;
5459  get_vector(model_sizes, len);
5460 
5461  int32_t seq_length=(int32_t) model_sizes[0];
5462  int32_t num_symbols=(int32_t) model_sizes[1];
5463  if (num_probs!=seq_length*num_symbols)
5464  SG_ERROR("Mismatch in number of emission probs and sequence length * number of symbols.\n")
5465 
5466  ui_pluginestimate->get_estimator()->set_model_params(
5467  pos_params, neg_params, seq_length, num_symbols);
5468 
5469  return true;
5470 }
5471 
5472 bool CSGInterface::cmd_get_plugin_estimate()
5473 {
5474  if (m_nrhs!=1 || !create_return_values(2))
5475  return false;
5476 
5477  float64_t* pos_params=NULL;
5478  float64_t* neg_params=NULL;
5479  int32_t num_params=0;
5480  int32_t seq_length=0;
5481  int32_t num_symbols=0;
5482 
5483  if (!ui_pluginestimate->get_estimator()->get_model_params(
5484  pos_params, neg_params, seq_length, num_symbols))
5485  return false;
5486 
5487  num_params=seq_length*num_symbols;
5488 
5489  float64_t* result=SG_MALLOC(float64_t, num_params*2);
5490  for (int32_t i=0; i<num_params; i++)
5491  result[i]=pos_params[i];
5492  for (int32_t i=0; i<num_params; i++)
5493  result[i+num_params]=neg_params[i];
5494 
5495  set_matrix(result, num_params, 2);
5496  SG_FREE(result);
5497 
5498  float64_t model_sizes[2];
5499  model_sizes[0]=(float64_t) seq_length;
5500  model_sizes[1]=(float64_t) num_symbols;
5501  set_vector(model_sizes, 2);
5502 
5503  return true;
5504 }
5505 
5506 bool CSGInterface::cmd_convergence_criteria()
5507 {
5508  if (m_nrhs<3 || !create_return_values(0))
5509  return false;
5510 
5511  int32_t num_iterations=get_int_from_int_or_str();
5512  float64_t epsilon=get_real_from_real_or_str();
5513 
5514  return ui_hmm->convergence_criteria(num_iterations, epsilon);
5515 }
5516 
5517 bool CSGInterface::cmd_normalize()
5518 {
5519  if (m_nrhs<2 || !create_return_values(0))
5520  return false;
5521 
5522  bool keep_dead_states=get_bool_from_bool_or_str();
5523 
5524  return ui_hmm->normalize(keep_dead_states);
5525 }
5526 
5527 bool CSGInterface::cmd_add_states()
5528 {
5529  if (m_nrhs<3 || !create_return_values(0))
5530  return false;
5531 
5532  int32_t num_states=get_int_from_int_or_str();
5533  float64_t value=get_real_from_real_or_str();
5534 
5535  return ui_hmm->add_states(num_states, value);
5536 }
5537 
5538 bool CSGInterface::cmd_permutation_entropy()
5539 {
5540  if (m_nrhs<3 || !create_return_values(0))
5541  return false;
5542 
5543  int32_t width=get_int_from_int_or_str();
5544  int32_t seq_num=get_int_from_int_or_str();
5545 
5546  return ui_hmm->permutation_entropy(width, seq_num);
5547 }
5548 
5549 bool CSGInterface::cmd_relative_entropy()
5550 {
5551  if (m_nrhs!=1 || !create_return_values(1))
5552  return false;
5553 
5554  float64_t* entropy=NULL;
5555  int32_t len=0;
5556  bool success=ui_hmm->relative_entropy(entropy, len);
5557  if (!success)
5558  return false;
5559 
5560  set_vector(entropy, len);
5561 
5562  SG_FREE(entropy);
5563  return true;
5564 }
5565 
5566 bool CSGInterface::cmd_entropy()
5567 {
5568  if (m_nrhs!=1 || !create_return_values(1))
5569  return false;
5570 
5571  float64_t* entropy=NULL;
5572  int32_t len=0;
5573  bool success=ui_hmm->entropy(entropy, len);
5574  if (!success)
5575  return false;
5576 
5577  set_vector(entropy, len);
5578 
5579  SG_FREE(entropy);
5580  return true;
5581 }
5582 
5583 bool CSGInterface::cmd_hmm_classify()
5584 {
5585  return do_hmm_classify(false, false);
5586 }
5587 
5588 bool CSGInterface::cmd_one_class_hmm_classify()
5589 {
5590  return do_hmm_classify(false, true);
5591 }
5592 
5593 bool CSGInterface::cmd_one_class_linear_hmm_classify()
5594 {
5595  return do_hmm_classify(true, true);
5596 }
5597 
5598 bool CSGInterface::do_hmm_classify(bool linear, bool one_class)
5599 {
5600  if (m_nrhs>1 || !create_return_values(1))
5601  return false;
5602 
5603  CFeatures* feat=ui_features->get_test_features();
5604  if (!feat)
5605  return false;
5606 
5607  int32_t num_vec=feat->get_num_vectors();
5608  CRegressionLabels* labels=NULL;
5609 
5610  if (linear) // must be one_class as well
5611  {
5612  labels=ui_hmm->linear_one_class_classify();
5613  }
5614  else
5615  {
5616  if (one_class)
5617  labels=ui_hmm->one_class_classify();
5618  else
5619  labels=ui_hmm->classify();
5620  }
5621  if (!labels)
5622  return false;
5623 
5624  float64_t* result=SG_MALLOC(float64_t, num_vec);
5625  for (int32_t i=0; i<num_vec; i++)
5626  result[i]=labels->get_label(i);
5627  SG_UNREF(labels);
5628 
5629  set_vector(result, num_vec);
5630  SG_FREE(result);
5631 
5632  return true;
5633 }
5634 
5635 bool CSGInterface::cmd_one_class_hmm_classify_example()
5636 {
5637  return do_hmm_classify_example(true);
5638 }
5639 
5640 bool CSGInterface::cmd_hmm_classify_example()
5641 {
5642  return do_hmm_classify_example(false);
5643 }
5644 
5645 bool CSGInterface::do_hmm_classify_example(bool one_class)
5646 {
5647  if (m_nrhs!=2 || !create_return_values(1))
5648  return false;
5649 
5650  int32_t idx=get_int();
5651  float64_t result=0;
5652 
5653  if (one_class)
5654  result=ui_hmm->one_class_classify_example(idx);
5655  else
5656  result=ui_hmm->classify_example(idx);
5657 
5658  set_real(result);
5659 
5660  return true;
5661 }
5662 
5663 bool CSGInterface::cmd_output_hmm()
5664 {
5665  if (m_nrhs!=1 || !create_return_values(0))
5666  return false;
5667 
5668  return ui_hmm->output_hmm();
5669 }
5670 
5671 bool CSGInterface::cmd_output_hmm_defined()
5672 {
5673  if (m_nrhs!=1 || !create_return_values(0))
5674  return false;
5675 
5676  return ui_hmm->output_hmm_defined();
5677 }
5678 
5679 bool CSGInterface::cmd_hmm_likelihood()
5680 {
5681  if (m_nrhs!=1 || !create_return_values(1))
5682  return false;
5683 
5684  CHMM* h=ui_hmm->get_current();
5685  if (!h)
5686  SG_ERROR("No HMM.\n")
5687 
5688  float64_t likelihood=h->model_probability();
5689  set_real(likelihood);
5690 
5691  return true;
5692 }
5693 
5694 bool CSGInterface::cmd_likelihood()
5695 {
5696  if (m_nrhs!=1 || !create_return_values(0))
5697  return false;
5698 
5699  return ui_hmm->likelihood();
5700 }
5701 
5702 bool CSGInterface::cmd_save_likelihood()
5703 {
5704  if (m_nrhs<2 || !create_return_values(0))
5705  return false;
5706 
5707  int32_t len=0;
5708  char* filename=get_str_from_str_or_direct(len);
5709 
5710  bool is_binary=false;
5711  if (m_nrhs==3)
5712  is_binary=get_bool_from_bool_or_str();
5713 
5714  bool success=ui_hmm->save_likelihood(filename, is_binary);
5715 
5716  SG_FREE(filename);
5717  return success;
5718 }
5719 
5720 bool CSGInterface::cmd_get_viterbi_path()
5721 {
5722  if (m_nrhs!=2 || !create_return_values(2))
5723  return false;
5724 
5725  int32_t dim=get_int();
5726  SG_DEBUG("dim: %f\n", dim)
5727 
5728  CHMM* h=ui_hmm->get_current();
5729  if (!h)
5730  return false;
5731 
5732  CFeatures* feat=ui_features->get_test_features();
5733  if (!feat || (feat->get_feature_class()!=C_STRING) ||
5734  (feat->get_feature_type()!=F_WORD))
5735  return false;
5736 
5738 
5739  int32_t num_feat=0;
5740  bool free_vec;
5741  uint16_t* vec=((CStringFeatures<uint16_t>*) feat)->get_feature_vector(dim, num_feat, free_vec);
5742  if (!vec || num_feat<=0)
5743  {
5744  ((CStringFeatures<uint16_t>*) feat)->free_feature_vector(vec, dim, free_vec);
5745  return false;
5746  }
5747  ((CStringFeatures<uint16_t>*) feat)->free_feature_vector(vec, dim, free_vec);
5748 
5749  SG_DEBUG("computing viterbi path for vector %d (length %d)\n", dim, num_feat)
5750  float64_t likelihood=0;
5751  T_STATES* path=h->get_path(dim, likelihood);
5752 
5753  set_vector(path, num_feat);
5754  SG_FREE(path);
5755  set_real(likelihood);
5756 
5757  return true;
5758 }
5759 
5760 bool CSGInterface::cmd_viterbi_train()
5761 {
5762  if (m_nrhs!=1 || !create_return_values(0))
5763  return false;
5764 
5765  return ui_hmm->viterbi_train();
5766 }
5767 
5768 bool CSGInterface::cmd_viterbi_train_defined()
5769 {
5770  if (m_nrhs!=1 || !create_return_values(0))
5771  return false;
5772 
5773  return ui_hmm->viterbi_train_defined();
5774 }
5775 
5776 bool CSGInterface::cmd_baum_welch_train()
5777 {
5778  if (m_nrhs!=1 || !create_return_values(0))
5779  return false;
5780 
5781  return ui_hmm->baum_welch_train();
5782 }
5783 
5784 bool CSGInterface::cmd_baum_welch_train_defined()
5785 {
5786  if (m_nrhs!=1 || !create_return_values(0))
5787  return false;
5788 
5789  return ui_hmm->baum_welch_train_defined();
5790 }
5791 
5792 
5793 bool CSGInterface::cmd_baum_welch_trans_train()
5794 {
5795  if (m_nrhs!=1 || !create_return_values(0))
5796  return false;
5797 
5798  return ui_hmm->baum_welch_trans_train();
5799 }
5800 
5801 bool CSGInterface::cmd_linear_train()
5802 {
5803  if (m_nrhs<1 || !create_return_values(0))
5804  return false;
5805 
5806  if (m_nrhs==2)
5807  {
5808  int32_t len=0;
5809  char* align=get_str_from_str_or_direct(len);
5810 
5811  bool success=ui_hmm->linear_train(align[0]);
5812 
5813  SG_FREE(align);
5814  return success;
5815  }
5816  else
5817  return ui_hmm->linear_train();
5818 }
5819 
5820 bool CSGInterface::cmd_save_path()
5821 {
5822  if (m_nrhs<2 || !create_return_values(0))
5823  return false;
5824 
5825  int32_t len=0;
5826  char* filename=get_str_from_str_or_direct(len);
5827 
5828  bool is_binary=false;
5829  if (m_nrhs==3)
5830  is_binary=get_bool_from_bool_or_str();
5831 
5832  bool success=ui_hmm->save_path(filename, is_binary);
5833 
5834  SG_FREE(filename);
5835  return success;
5836 }
5837 
5838 bool CSGInterface::cmd_append_hmm()
5839 {
5840  if (m_nrhs!=5 || !create_return_values(0))
5841  return false;
5842 
5843  CHMM* old_h=ui_hmm->get_current();
5844  if (!old_h)
5845  SG_ERROR("No current HMM set.\n")
5846 
5847  float64_t* p=NULL;
5848  int32_t N_p=0;
5849  get_vector(p, N_p);
5850 
5851  float64_t* q=NULL;
5852  int32_t N_q=0;
5853  get_vector(q, N_q);
5854 
5855  float64_t* a=NULL;
5856  int32_t M_a=0;
5857  int32_t N_a=0;
5858  get_matrix(a, M_a, N_a);
5859  int32_t N=N_a;
5860 
5861  float64_t* b=NULL;
5862  int32_t M_b=0;
5863  int32_t N_b=0;
5864  get_matrix(b, M_b, N_b);
5865  int32_t M=N_b;
5866 
5867  if (N_p!=N || N_q!=N || N_a!=N || M_a!=N || N_b!=M || M_b!=N)
5868  {
5869  SG_ERROR("Model matrices not matching in size.\n"
5870  "p:(%d) q:(%d) a:(%d,%d) b(%d,%d)\n",
5871  N_p, N_q, N_a, M_a, N_b, M_b);
5872  }
5873 
5874  CHMM* h=new CHMM(N, M, NULL, ui_hmm->get_pseudo());
5875  int32_t i,j;
5876 
5877  for (i=0; i<N; i++)
5878  {
5879  h->set_p(i, p[i]);
5880  h->set_q(i, q[i]);
5881  }
5882 
5883  for (i=0; i<N; i++)
5884  for (j=0; j<N; j++)
5885  h->set_a(i,j, a[i+j*N]);
5886 
5887  for (i=0; i<N; i++)
5888  for (j=0; j<M; j++)
5889  h->set_b(i,j, b[i+j*N]);
5890 
5891  old_h->append_model(h);
5892  SG_UNREF(h);
5893 
5894  return true;
5895 }
5896 
5897 bool CSGInterface::cmd_append_model()
5898 {
5899  if (m_nrhs<2 || !create_return_values(0))
5900  return false;
5901  if (m_nrhs>2 && m_nrhs!=4)
5902  return false;
5903 
5904  int32_t len=0;
5905  char* filename=get_str_from_str_or_direct(len);
5906  int32_t base1=-1;
5907  int32_t base2=-1;
5908  if (m_nrhs>2)
5909  {
5910  base1=get_int_from_int_or_str();
5911  base2=get_int_from_int_or_str();
5912  }
5913 
5914  bool success=ui_hmm->append_model(filename, base1, base2);
5915 
5916  SG_FREE(filename);
5917  return success;
5918 }
5919 
5920 bool CSGInterface::cmd_new_hmm()
5921 {
5922  if (m_nrhs!=3 || !create_return_values(0))
5923  return false;
5924 
5925  int32_t n=get_int_from_int_or_str();
5926  int32_t m=get_int_from_int_or_str();
5927 
5928  return ui_hmm->new_hmm(n, m);
5929 }
5930 
5931 bool CSGInterface::cmd_load_hmm()
5932 {
5933  if (m_nrhs!=2 || !create_return_values(0))
5934  return false;
5935 
5936  int32_t len=0;
5937  char* filename=get_str_from_str_or_direct(len);
5938 
5939  bool success=ui_hmm->load(filename);
5940 
5941  SG_FREE(filename);
5942  return success;
5943 }
5944 
5945 bool CSGInterface::cmd_save_hmm()
5946 {
5947  if (m_nrhs<2 || !create_return_values(0))
5948  return false;
5949 
5950  int32_t len=0;
5951  char* filename=get_str_from_str_or_direct(len);
5952 
5953  bool is_binary=false;
5954  if (m_nrhs==3)
5955  is_binary=get_bool_from_bool_or_str();
5956 
5957  bool success=ui_hmm->save(filename, is_binary);
5958 
5959  SG_FREE(filename);
5960  return success;
5961 }
5962 
5963 bool CSGInterface::cmd_set_hmm()
5964 {
5965  if (m_nrhs!=5 || !create_return_values(0))
5966  return false;
5967 
5968  float64_t* p=NULL;
5969  int32_t N_p=0;
5970  get_vector(p, N_p);
5971 
5972  float64_t* q=NULL;
5973  int32_t N_q=0;
5974  get_vector(q, N_q);
5975 
5976  float64_t* a=NULL;
5977  int32_t M_a=0;
5978  int32_t N_a=0;
5979  get_matrix(a, M_a, N_a);
5980  int32_t N=N_a;
5981 
5982  float64_t* b=NULL;
5983  int32_t M_b=0;
5984  int32_t N_b=0;
5985  get_matrix(b, M_b, N_b);
5986  int32_t M=N_b;
5987 
5988  if (N_p!=N || N_q!=N || N_a!=N || M_a!=N || N_b!=M || M_b!=N)
5989  {
5990  SG_ERROR("Model matrices not matching in size.\n"
5991  "p:(%d) q:(%d) a:(%d,%d) b(%d,%d)\n",
5992  N_p, N_q, N_a, M_a, N_b, M_b);
5993  }
5994 
5995  CHMM* current=ui_hmm->get_current();
5996  if (!current)
5997  SG_ERROR("Need a previously created HMM.\n")
5998 
5999  int32_t i,j;
6000 
6001  for (i=0; i<N; i++)
6002  {
6003  current->set_p(i, p[i]);
6004  current->set_q(i, q[i]);
6005  }
6006 
6007  for (i=0; i<N; i++)
6008  for (j=0; j<N; j++)
6009  current->set_a(i,j, a[i+j*N]);
6010 
6011  for (i=0; i<N; i++)
6012  for (j=0; j<M; j++)
6013  current->set_b(i,j, b[i+j*N]);
6014 
6015  CStringFeatures<uint16_t>* sf = ((CStringFeatures<uint16_t>*) (ui_features->get_train_features()));
6016  current->set_observations(sf);
6017 
6018  return true;
6019 }
6020 
6021 bool CSGInterface::cmd_set_hmm_as()
6022 {
6023  if (m_nrhs!=2 || !create_return_values(0))
6024  return false;
6025 
6026  int32_t len=0;
6027  char* target=get_str_from_str_or_direct(len);
6028 
6029  bool success=ui_hmm->set_hmm_as(target);
6030 
6031  SG_FREE(target);
6032  return success;
6033 }
6034 
6035 bool CSGInterface::cmd_set_chop()
6036 {
6037  if (m_nrhs!=2 || !create_return_values(0))
6038  return false;
6039 
6040  float64_t value=get_real_from_real_or_str();
6041  return ui_hmm->chop(value);
6042 }
6043 
6044 bool CSGInterface::cmd_set_pseudo()
6045 {
6046  if (m_nrhs!=2 || !create_return_values(0))
6047  return false;
6048 
6049  float64_t value=get_real_from_real_or_str();
6050  return ui_hmm->set_pseudo(value);
6051 }
6052 
6053 bool CSGInterface::cmd_load_definitions()
6054 {
6055  if (m_nrhs<2 || !create_return_values(0))
6056  return false;
6057 
6058  int32_t len=0;
6059  char* filename=get_str_from_str_or_direct(len);
6060 
6061  bool do_init=false;
6062  if (m_nrhs==3)
6063  do_init=get_bool_from_bool_or_str();
6064 
6065  bool success=ui_hmm->load_definitions(filename, do_init);
6066 
6067  SG_FREE(filename);
6068  return success;
6069 }
6070 
6071 bool CSGInterface::cmd_get_hmm()
6072 {
6073  if (m_nrhs!=1 || !create_return_values(4))
6074  return false;
6075 
6076  CHMM* h=ui_hmm->get_current();
6077  if (!h)
6078  return false;
6079 
6080  int32_t N=h->get_N();
6081  int32_t M=h->get_M();
6082  int32_t i=0;
6083  int32_t j=0;
6084  float64_t* p=SG_MALLOC(float64_t, N);
6085  float64_t* q=SG_MALLOC(float64_t, N);
6086 
6087  for (i=0; i<N; i++)
6088  {
6089  p[i]=h->get_p(i);
6090  q[i]=h->get_q(i);
6091  }
6092 
6093  set_vector(p, N);
6094  SG_FREE(p);
6095  set_vector(q, N);
6096  SG_FREE(q);
6097 
6098  float64_t* a=SG_MALLOC(float64_t, N*N);
6099  for (i=0; i<N; i++)
6100  for (j=0; j<N; j++)
6101  a[i+j*N]=h->get_a(i, j);
6102  set_matrix(a, N, N);
6103  SG_FREE(a);
6104 
6105  float64_t* b=SG_MALLOC(float64_t, N*M);
6106  for (i=0; i<N; i++)
6107  for (j=0; j<M; j++)
6108  b[i+j*N]=h->get_b(i, j);
6109  set_matrix(b, N, M);
6110  SG_FREE(b);
6111 
6112  return true;
6113 }
6114 
6115 bool CSGInterface::cmd_best_path()
6116 {
6117  if (m_nrhs!=3 || !create_return_values(0))
6118  return false;
6119 
6120  int32_t from=get_int_from_int_or_str();
6121  int32_t to=get_int_from_int_or_str();
6122 
6123  return ui_hmm->best_path(from, to);
6124 }
6125 
6126 bool CSGInterface::cmd_best_path_2struct()
6127 {
6128  if (m_nrhs!=12 || !create_return_values(3))
6129  return false;
6130 
6131  SG_ERROR("Sorry, this parameter list is awful!\n")
6132 
6133  return true;
6134 }
6135 
6136 void CSGInterface::get_vector(bool*& vector, int32_t& len)
6137 {
6138  int32_t* int_vector;
6139  get_vector(int_vector, len);
6140 
6141  ASSERT(len>0)
6142  vector= SG_MALLOC(bool, len);
6143 
6144  for (int32_t i=0; i<len; i++)
6145  vector[i]= (int_vector[i]!=0);
6146 
6147  SG_FREE(int_vector);
6148 }
6149 
6150 void CSGInterface::set_vector(const bool* vector, int32_t len)
6151 {
6152  int32_t* int_vector = SG_MALLOC(int32_t, len);
6153  for (int32_t i=0;i<len;i++)
6154  {
6155  if (vector[i])
6156  int_vector[i]=1;
6157  else
6158  int_vector[i]=0;
6159  }
6160  set_vector(int_vector,len);
6161  SG_FREE(int_vector);
6162 }
6163 
6164 bool CSGInterface::cmd_set_plif_struct()
6165 {
6166  // ARG 2
6167  int32_t Nid=0;
6168  int32_t* ids;
6169  get_vector(ids,Nid);
6170 
6171  // ARG 3
6172  int32_t Nname=0;
6173  int32_t Mname=0;
6174  SGString<char>* names;
6175  get_string_list(names, Nname,Mname);
6176 
6177  // ARG 4
6178  int32_t Nlimits=0;
6179  int32_t Mlimits=0;
6180  float64_t* all_limits;
6181  get_matrix(all_limits, Mlimits, Nlimits);
6182 
6183  // ARG 5
6184  int32_t Npenalties=0;
6185  int32_t Mpenalties=0;
6186  float64_t* all_penalties;
6187  get_matrix(all_penalties, Mpenalties, Npenalties);
6188 
6189  // ARG 6
6190  int32_t Ntransform=0;
6191  int32_t Mtransform=0;
6192  SGString<char>* all_transform;
6193  get_string_list(all_transform, Ntransform, Mtransform);
6194 
6195  // ARG 7
6196  int32_t Nmin=0;
6197  float64_t* min_values;
6198  get_vector(min_values,Nmin);
6199 
6200  // ARG 8
6201  int32_t Nmax=0;
6202  float64_t* max_values;
6203  get_vector(max_values,Nmax);
6204 
6205  // ARG 9
6206  int32_t Ncache=0;
6207  bool* all_use_cache;
6208  get_vector(all_use_cache,Ncache);
6209 
6210  // ARG 10
6211  int32_t Nsvm=0;
6212  int32_t* all_use_svm;
6213  get_vector(all_use_svm,Nsvm);
6214 
6215  // ARG 11
6216  int32_t Ncalc=0;
6217  bool* all_do_calc;
6218  get_vector(all_do_calc,Ncalc);
6219 
6220  if (Ncalc!=Nsvm)
6221  SG_ERROR("Ncalc!=Nsvm, Ncalc:%i, Nsvm:%i\n",Ncalc,Nsvm)
6222  if (Ncalc!=Ncache)
6223  SG_ERROR("Ncalc!=Ncache, Ncalc:%i, Ncache:%i\n",Ncalc,Ncache)
6224  if (Ncalc!=Ntransform)
6225  SG_ERROR("Ncalc!=Ntransform, Ncalc:%i, Ntransform:%i\n",Ncalc,Ntransform)
6226  if (Ncalc!=Nmin)
6227  SG_ERROR("Ncalc!=Nmin, Ncalc:%i, Nmin:%i\n",Ncalc,Nmin)
6228  if (Ncalc!=Nmax)
6229  SG_ERROR("Ncalc!=Nmax, Ncalc:%i, Nmax:%i\n",Ncalc,Nmax)
6230  if (Ncalc!=Npenalties)
6231  SG_ERROR("Ncalc!=Npenalties, Ncalc:%i, Npenalties:%i\n",Ncalc,Npenalties)
6232  if (Ncalc!=Nlimits)
6233  SG_ERROR("Ncalc!=Nlimits, Ncalc:%i, Nlimits:%i\n",Ncalc,Nlimits)
6234  if (Ncalc!=Nname)
6235  SG_ERROR("Ncalc!=Nname, Ncalc:%i, Nname:%i\n",Ncalc,Nname)
6236  if (Ncalc!=Nid)
6237  SG_ERROR("Ncalc!=Nid, Ncalc:%i, Nid:%i\n",Ncalc,Nid)
6238  if (Mlimits!=Mpenalties)
6239  SG_ERROR("Mlimits!=Mpenalties, Mlimits:%i, Mpenalties:%i\n",Mlimits,Mpenalties)
6240 
6241  int32_t N = Ncalc;
6242  int32_t M = Mlimits;
6243  CPlifMatrix* pm=ui_structure->get_plif_matrix();
6244  pm->create_plifs(N, M);
6245  pm->set_plif_ids(SGVector<int32_t>(ids, N));
6246  pm->set_plif_min_values(SGVector<float64_t>(min_values, N));
6247  pm->set_plif_max_values(SGVector<float64_t>(max_values, N));
6248  pm->set_plif_use_cache(SGVector<bool>(all_use_cache, N));
6249  pm->set_plif_use_svm(SGVector<int32_t>(all_use_svm, N));
6250  pm->set_plif_limits(SGMatrix<float64_t>(all_limits, N, M));
6251  pm->set_plif_penalties(SGMatrix<float64_t>(all_penalties, N, M));
6252  pm->set_plif_names(names, N);
6253  pm->set_plif_transform_type(all_transform, N);
6254 
6255  SG_FREE(names);
6256  SG_FREE(all_transform);
6257  SG_FREE(all_do_calc);
6258 
6259  return true;
6260 }
6261 
6262 bool CSGInterface::cmd_get_plif_struct()
6263 {
6264  CPlifMatrix* pm=ui_structure->get_plif_matrix();
6265  CPlif** PEN = pm->get_PEN();
6266  int32_t N = pm->get_num_plifs();
6267  int32_t M = pm->get_num_limits();
6268 
6269 
6270  int32_t* ids = SG_MALLOC(int32_t, N);
6271  float64_t* max_values = SG_MALLOC(float64_t, N);
6272  float64_t* min_values = SG_MALLOC(float64_t, N);
6273  SGString<char>* names = SG_MALLOC(SGString<char>, N);
6274  SGString<char>* all_transform = SG_MALLOC(SGString<char>, N);
6275  float64_t* all_limits = SG_MALLOC(float64_t, N*M);
6276  float64_t* all_penalties = SG_MALLOC(float64_t, N*M);
6277  bool* all_use_cache = SG_MALLOC(bool, N);
6278  int32_t* all_use_svm = SG_MALLOC(int32_t, N);
6279  bool* all_do_calc = SG_MALLOC(bool, N);
6280  for (int32_t i=0;i<N;i++)
6281  {
6282  ids[i]=PEN[i]->get_id();
6283  names[i].string = PEN[i]->get_plif_name();
6284  names[i].slen = strlen(PEN[i]->get_plif_name());
6285  SGVector<float64_t> limits = PEN[i]->get_plif_limits();
6286  SGVector<float64_t> penalties = PEN[i]->get_plif_penalties();
6287  for (int32_t j=0;j<M;j++)
6288  {
6289  all_limits[i*M+j]=limits[j];
6290  all_penalties[i*M+j]=penalties[j];
6291  }
6292  all_transform[i].string = (char*) PEN[i]->get_transform_type();
6293  all_transform[i].slen = strlen(PEN[i]->get_transform_type());
6294  min_values[i]=PEN[i]->get_min_value();
6295  max_values[i]=PEN[i]->get_max_value();
6296  all_use_cache[i]=PEN[i]->get_use_cache();
6297  all_use_svm[i]=PEN[i]->get_use_svm();
6298  all_do_calc[i]=PEN[i]->get_do_calc();
6299 
6300  }
6301  set_vector(ids,N);
6302  set_string_list(names, N);
6303  set_matrix(all_limits, M, N);
6304  set_matrix(all_penalties, M, N);
6305  set_string_list(all_transform, N);
6306  set_vector(min_values,N);
6307  set_vector(max_values,N);
6308  set_vector(all_use_cache,N);
6309  set_vector(all_use_svm,N);
6310  set_vector(all_do_calc,N);
6311 
6312  SG_FREE(ids);
6313  SG_FREE(max_values);
6314  SG_FREE(min_values);
6315  SG_FREE(names);
6316  SG_FREE(all_transform);
6317  SG_FREE(all_limits);
6318  SG_FREE(all_penalties);
6319  SG_FREE(all_use_cache);
6320  SG_FREE(all_use_svm);
6321  SG_FREE(all_do_calc);
6322 
6323  return true;
6324 }
6325 /*bool CSGInterface::cmd_signals_set_model()
6326 {
6327  // ARG 1
6328  int32_t len=0;
6329  char* filename;
6330  filename = get_string(len);
6331 
6332  CTrainPredMaster* tpm = new CTrainPredMaster(ui_kernel);
6333 
6334  tpm->read_models_from_file(filename);
6335 
6336  return true;
6337  }*/
6338 bool CSGInterface::cmd_signals_set_positions()
6339 {
6340  return true;
6341 }
6342 bool CSGInterface::cmd_signals_set_labels()
6343 {
6344  return true;
6345 }
6346 bool CSGInterface::cmd_signals_set_split()
6347 {
6348  return true;
6349 }
6350 bool CSGInterface::cmd_signals_set_train_mask()
6351 {
6352  return true;
6353 }
6354 bool CSGInterface::cmd_signals_add_feature()
6355 {
6356  return true;
6357 }
6358 bool CSGInterface::cmd_signals_add_kernel()
6359 {
6360  return true;
6361 }
6362 bool CSGInterface::cmd_signals_run()
6363 {
6364  return true;
6365 }
6366 
6367 bool CSGInterface::cmd_init_dyn_prog()
6368 {
6369  //ARG 1
6370  int32_t num_svms=get_int();
6371 
6372  CDynProg* h=new CDynProg(num_svms);
6373  ui_structure->set_dyn_prog(h);
6374  return true;
6375 }
6376 
6377 bool CSGInterface::cmd_clean_up_dyn_prog()
6378 {
6379  return ui_structure->cleanup();
6380 }
6381 
6382 bool CSGInterface::cmd_set_model()
6383 {
6384 
6385  CPlifMatrix* pm=ui_structure->get_plif_matrix();
6386 
6387  CDynProg* h = ui_structure->get_dyn_prog();
6388  int32_t num_svms = h->get_num_svms();
6389  //CDynProg* h=new CDynProg(Nweights/* = num_svms */);
6390 
6391  //ARG 1
6392  // transition pointers
6393  // link transitions to length, content, frame (and tiling)
6394  // plifs (#states x #states x 3 or 4)
6395  int32_t numDim=0;
6396  int32_t* Dim=0;
6397  float64_t* penalties_array=NULL;
6398  get_ndarray(penalties_array,Dim,numDim);
6399  ASSERT(numDim==3)
6400  ASSERT(Dim[0]==Dim[1])
6401 
6402  if (!pm->compute_plif_matrix(SGNDArray<float64_t>(penalties_array, Dim, numDim, false)))
6403  SG_ERROR("error computing plif matrix\n")
6404  ui_structure->set_num_states(Dim[0]);
6405  SG_FREE(penalties_array);
6406 
6407  // ARG 2
6408  // bool-> determines if orf information should be used
6409  bool use_orf = get_bool();
6410  ui_structure->set_use_orf(use_orf);
6411 
6412  // ARG 3
6413  // determines for which contents which orf should be used (#contents x 2)
6414  int32_t Nmod=0;
6415  int32_t Mmod=0;
6416  int32_t* mod_words;
6417  get_matrix(mod_words, Nmod,Mmod);
6418  if (Nmod != num_svms)
6419  SG_ERROR("should be equal: Nmod: %i, num_svms: %i\n",Nmod,num_svms)
6420  ASSERT(Mmod == 2)
6421  h->init_mod_words_array(SGMatrix<int32_t>(mod_words, Nmod, Mmod));
6422 
6423  // ARG 4
6424  // links: states -> signal plifs (#states x 2)
6425  int32_t num_states=0;
6426  int32_t feat_dim3=0;
6427  int32_t* state_signals;
6428  get_matrix(state_signals,num_states,feat_dim3);
6429  ASSERT(num_states==Dim[0])
6430  pm->compute_signal_plifs(SGMatrix<int32_t>(state_signals, feat_dim3, num_states));
6431 
6432 
6433  // ARG 5
6434  // ORF info (#states x 2)
6435  int32_t Norf=0;
6436  int32_t Morf=0;
6437  int32_t* orf_info;
6438  get_matrix(orf_info,Norf,Morf);
6439  ASSERT(Norf==num_states)
6440  ASSERT(Morf==2)
6441 
6442  ui_structure->set_orf_info(orf_info, Norf, Morf);
6443  h->set_orf_info(SGMatrix<int32_t>(orf_info, Norf, Morf));
6444 
6445  h->set_num_states(num_states) ;
6446 
6447  return true;
6448 }
6449 
6450 bool CSGInterface::cmd_precompute_content_svms()
6451 {
6452 
6453  // ARG 1
6454  int32_t seq_len=0;
6455  char* seq;
6456  seq = get_string(seq_len);
6457 
6458  // ARG 2
6459  // all feature positions
6460  int32_t Npos=0;
6461  int32_t* all_pos;
6462  get_vector(all_pos, Npos);
6463 
6464  //ARG 3
6465  // content svm weights
6466  int32_t Nweights=0;
6467  int32_t num_svms=0;
6468  float64_t* weights;
6469  get_matrix(weights, Nweights, num_svms);
6470  if (Nweights!=5440)
6471  SG_PRINT("Dimension mismatch: got %i, expect %i\n", Nweights, 5440)
6472  ui_structure->set_content_svm_weights(weights, Nweights, num_svms);
6473 
6474  CDynProg* h = ui_structure->get_dyn_prog();
6475  if (!h)
6476  SG_ERROR("no DynProg object found, use init_dyn_prog first\n")
6477 
6478 
6479  //float64_t* weights = ui_structure->get_content_svm_weights();
6480  //int32_t Mweights = h->get_num_svms();
6481  //int32_t Nweights = ui_structure->get_num_svm_weights();
6482  h->set_pos(SGVector<int32_t>(all_pos, Npos));
6483  h->set_gene_string(SGVector<char>(seq, seq_len));
6484  h->create_word_string();
6486  h->init_content_svm_value_array(num_svms);
6487  h->set_dict_weights(SGMatrix<float64_t>(weights, Nweights, num_svms));
6489  SG_DEBUG("precompute_content_svms done\n")
6490  return true;
6491 }
6492 
6493 bool CSGInterface::cmd_get_lin_feat()
6494 {
6495  CDynProg* h = ui_structure->get_dyn_prog();
6496  if (!h)
6497  SG_ERROR("no DynProg object found, use set_model first\n")
6498 
6499 
6500  int32_t dim1, dim2 = 0;
6501  float64_t* lin_feat = h->get_lin_feat(dim1, dim2);
6502 
6503  set_matrix(lin_feat, dim1, dim2);
6504 
6505  return true;
6506 }
6507 bool CSGInterface::cmd_set_lin_feat()
6508 {
6509  // ARG 1
6510  int32_t Nseq=0;
6511  char* seq;
6512  seq = get_string(Nseq);
6513 
6514  // ARG 2
6515  // all feature positions
6516  int32_t Npos=0;
6517  int32_t* all_pos;
6518  get_vector(all_pos, Npos);
6519 
6520  //ARG 3
6521  //
6522  int32_t num_svms, seq_len;
6523  float64_t* lin_feat=NULL;
6524  get_matrix(lin_feat, num_svms, seq_len);
6525 
6526  if (Npos!=seq_len)
6527  {
6528  SG_ERROR("Dimension mismatch: got %i positions and (%ix%i) values\n", Npos, num_svms, seq_len)
6529 
6530  SG_FREE(lin_feat);
6531  SG_FREE(seq);
6532  SG_FREE(all_pos);
6533 
6534  return false ;
6535  }
6536 
6537  CDynProg* h = ui_structure->get_dyn_prog();
6538  if (!h)
6539  SG_ERROR("no DynProg object found, use set_model first\n")
6540 
6541  h->set_pos(SGVector<int32_t>(all_pos, Npos));
6542  h->set_gene_string(SGVector<char>(seq, Nseq));
6544  h->init_content_svm_value_array(num_svms);
6545  h->set_lin_feat(lin_feat, num_svms, seq_len);
6546 
6547  SG_FREE(lin_feat);
6548 
6549  return true;
6550 }
6551 bool CSGInterface::cmd_long_transition_settings()
6552 {
6553  bool use_long_transitions = get_bool();
6554  int32_t threshold = get_int();
6555  int32_t max_len = get_int();
6556 
6557  CDynProg* h = ui_structure->get_dyn_prog();
6558  if (!h)
6559  SG_ERROR("no DynProg object found, use set_model first\n")
6560 
6561  h->long_transition_settings(use_long_transitions, threshold, max_len);
6562 
6563  return true;
6564 }
6565 bool CSGInterface::cmd_set_feature_matrix()
6566 {
6567  int32_t num_states = ui_structure->get_num_states();
6568 
6569  //ARG 1
6570  // feature matrix (#states x #feature_positions x max_num_signals)
6571  int32_t* Dims=0;
6572  int32_t numDims=0;
6573  float64_t* features = NULL;
6574  get_ndarray(features, Dims, numDims);
6575 
6576  if (numDims!=3)
6577  SG_ERROR("expected a 3 dimensional array, got %i dimensions\n", numDims)
6578  if (Dims[0]!=num_states)
6579  SG_ERROR("number of rows (%i) not equal number of states (%i)\n",Dims[0], num_states)
6580  ASSERT(ui_structure->set_feature_matrix(features, Dims))
6581 
6582  ASSERT(ui_structure->set_feature_dims(Dims))
6583 
6584  SG_FREE(features);
6585  SG_FREE(Dims);
6586 
6587  return true;
6588 }
6589 bool CSGInterface::cmd_set_feature_matrix_sparse()
6590 {
6591  int32_t num_pos = ui_structure->get_num_positions();
6592  int32_t num_states = ui_structure->get_num_states();
6593 
6594  //ARG 1
6595  // feature matrix (#states x #feature_positions x max_num_signals)
6596  int32_t dim11, dim12 ;
6597  SGSparseVector<float64_t> *features1=NULL ;
6598  get_sparse_matrix(features1, dim11, dim12);
6599 
6600  int32_t dim21, dim22 ;
6601  SGSparseVector<float64_t> *features2=NULL ;
6602  get_sparse_matrix(features2, dim21, dim22);
6603 
6604  ASSERT(dim11==dim21)
6605  ASSERT(dim12==dim22)
6606 
6607  int32_t *Dims = SG_MALLOC(int32_t, 3);
6608  Dims[0]=dim11 ;
6609  Dims[1]=dim12 ;
6610  Dims[2]=2 ;
6611 
6612  ASSERT(Dims[0]==num_states)
6613  ASSERT(Dims[1]==num_pos)
6614 
6615  ASSERT(ui_structure->set_feature_matrix_sparse(features1, features2, Dims))
6616  ASSERT(ui_structure->set_feature_dims(Dims))
6617 
6618  SG_FREE(features1);
6619  SG_FREE(features2);
6620  SG_FREE(Dims);
6621 
6622  return true;
6623 }
6624 bool CSGInterface::cmd_init_intron_list()
6625 {
6626  //ARG1 start_positions
6627  int32_t Nstart_positions;
6628  int32_t* start_positions;
6629  get_vector(start_positions, Nstart_positions);
6630  //SG_PRINT("Nstart_positions:%i\n",Nstart_positions)
6631 
6632  //ARG2 end_positions
6633  int32_t Nend_positions;
6634  int32_t* end_positions;
6635  get_vector(end_positions, Nend_positions);
6636  //SG_PRINT("Nend_positions:%i\n",Nend_positions)
6637 
6638  //ARG3 quality
6639  int32_t Nquality;
6640  int32_t* quality;
6641  get_vector(quality, Nquality);
6642  //SG_PRINT("Nquality:%i\n",Nquality)
6643 
6644  //ARG4 all candidate positions
6645  int32_t Nall_pos;
6646  int32_t* all_pos;
6647  get_vector(all_pos, Nall_pos);
6648  //SG_PRINT("Nall_pos:%i\n",Nall_pos)
6649 
6650  ASSERT(Nquality==Nend_positions)
6651  ASSERT(Nend_positions==Nstart_positions)
6652 
6653  CIntronList* intron_list = new CIntronList();
6654 
6655  intron_list->init_list(all_pos, Nall_pos);
6656 
6657  intron_list->read_introns(start_positions, end_positions, quality, Nstart_positions);
6658 
6659  SG_FREE(start_positions);
6660  SG_FREE(end_positions);
6661  SG_FREE(quality);
6662  SG_FREE(all_pos);
6663 
6664  //int32_t test;
6665  //int32_t testq;
6666  //intron_list->get_coverage(&test, &testq, 15 ,16);
6667 
6668  //SG_PRINT("coverage: %i, quality: %i\n",test, testq)
6669 
6670  CDynProg* h = ui_structure->get_dyn_prog();
6671  if (!h)
6672  SG_ERROR("no DynProg object found, use set_model first\n")
6673 
6674  h->set_intron_list(intron_list, 2);
6675 
6676  return true;
6677 }
6678 bool CSGInterface::cmd_precompute_tiling_features()
6679 {
6680  CPlifMatrix* pm=ui_structure->get_plif_matrix();
6681  CPlif** PEN = pm->get_PEN();
6682  CDynProg* h = ui_structure->get_dyn_prog();
6683 
6684  int32_t Nintensities=0;
6685  float64_t* intensities;
6686  get_vector(intensities, Nintensities);
6687 
6688  int32_t Nprobe_pos=0;
6689  int32_t* probe_pos;
6690  get_vector(probe_pos, Nprobe_pos);
6691  ASSERT(Nprobe_pos==Nintensities)
6692 
6693  int32_t Ntiling_plif_ids=0;
6694  int32_t* tiling_plif_ids;
6695  get_vector(tiling_plif_ids, Ntiling_plif_ids);
6696 
6697  h->init_tiling_data(probe_pos,intensities, Nprobe_pos);
6698  h->precompute_tiling_plifs(PEN, tiling_plif_ids, Ntiling_plif_ids);
6699  return true;
6700 }
6701 
6702 bool CSGInterface::cmd_best_path_trans()
6703 {
6704  CDynProg* h = ui_structure->get_dyn_prog();
6705 
6706  CSegmentLoss* seg_loss_obj = h->get_segment_loss_object();
6707 
6708  CPlifMatrix* pm=ui_structure->get_plif_matrix();
6709 
6710  int32_t num_states = h->get_num_states();
6711  int32_t* feat_dims = ui_structure->get_feature_dims();
6712  float64_t* features = (ui_structure->get_feature_matrix(false));
6713  CSparseFeatures<float64_t>* features_sparse1 = (ui_structure->get_feature_matrix_sparse(0));
6714  CSparseFeatures<float64_t>* features_sparse2 = (ui_structure->get_feature_matrix_sparse(1));
6715  int32_t* orf_info = ui_structure->get_orf_info();
6716  bool use_orf = ui_structure->get_use_orf();
6717  int32_t Nplif = pm->get_num_plifs();
6718 
6719  // ARG 1
6720  // transitions from initial state (#states x 1)
6721  int32_t Np=0;
6722  float64_t* p;
6723  get_vector(p, Np);
6724  if (Np!=num_states)
6725  SG_ERROR("# transitions from initial state (%i) does not match # states (%i)\n", Np, num_states)
6726 
6727  // ARG 2
6728  // transitions to end state (#states x 1)
6729  int32_t Nq=0;
6730  float64_t* q;
6731  get_vector(q, Nq);
6732  if (Nq!=num_states)
6733  SG_ERROR("# transitions to end state (%i) does not match # states (%i)\n", Nq, num_states)
6734 
6735  // ARG 3
6736  // number of best paths
6737  int32_t Nnbest=0;
6738  int32_t* all_nbest;
6739  get_vector(all_nbest, Nnbest);
6740  int32_t nbest;
6741  int32_t nother = 0;
6742  if (Nnbest==2)
6743  {
6744  nbest =all_nbest[0];
6745  nother=all_nbest[1];
6746  }
6747  else
6748  nbest =all_nbest[0];
6749  SG_FREE(all_nbest);
6750 
6751  // ARG 4
6752  // segment path (2 x #feature_positions)
6753  // masking/weighting of loss for specific
6754  // regions of the true path
6755  int32_t Nseg_path=0;
6756  int32_t Mseg_path=0;
6757  float64_t* seg_path;
6758  get_matrix(seg_path, Nseg_path, Mseg_path);
6759 
6760  // ARG 5
6761  // links for transitions (#transitions x 4)
6762  int32_t Na_trans=0;
6763  int32_t num_a_trans=0;
6764  float64_t* a_trans;
6765  get_matrix(a_trans, num_a_trans, Na_trans);
6766 
6767  // ARG 6
6768  // loss matrix (#segment x 2*#segments)
6769  // one (#segment x #segments)-matrix for segment loss
6770  // and one for nucleotide loss
6771  int32_t Nloss=0;
6772  int32_t Mloss=0;
6773  float64_t* loss;
6774  get_matrix(loss, Nloss,Mloss);
6775 
6776  int32_t M = h->get_num_positions();
6777 
6779  // check input
6781  ASSERT(num_states==Nq)
6782 
6783  CPlif** PEN=pm->get_PEN();
6784  ASSERT(PEN)
6785 
6786  h->set_p_vector(SGVector<float64_t>(p, num_states));
6787  h->set_q_vector(SGVector<float64_t>(q, num_states));
6788 
6789  if (seg_path!=NULL)
6790  {
6791  h->set_a_trans_matrix(SGMatrix<float64_t>(a_trans, num_a_trans, Na_trans)) ;
6792  }
6793  else
6794  {
6795  h->set_a_trans_matrix(SGMatrix<float64_t>(a_trans, num_a_trans, 3)) ; // segment_id = 0
6796  }
6797 
6798  if (!h->check_svm_arrays())
6799  {
6800  SG_ERROR("svm arrays inconsistent\n")
6801  CPlif::delete_penalty_struct(PEN, Nplif) ;
6802  return false ;
6803  }
6804 
6805  SG_DEBUG("best_path_trans: M: %i, Mseg_path: %i\n", M, Mseg_path)
6806 
6807  h->set_observation_matrix(SGNDArray<float64_t>(features, feat_dims, 3, false));
6808 
6809  if (seg_path!=NULL)
6810  {
6811  h->best_path_set_segment_loss(SGMatrix<float64_t>(loss, Nloss, Mloss, false)) ;
6812  seg_loss_obj->set_segment_loss(loss, Nloss, Mloss);
6813  }
6814  else
6815  {
6816  float64_t zero2[2] = {0.0, 0.0} ;
6818  seg_loss_obj->set_segment_loss(zero2, 2, 1);
6819  }
6820  h->set_content_type_array(SGMatrix<float64_t>(seg_path,Nseg_path,Mseg_path));
6821 
6822  bool segment_loss_non_zero=false;
6823  for (int32_t i=0; i<Nloss*Mloss; i++)
6824  {
6825  if (loss[i]>1e-3)
6826  segment_loss_non_zero=true;
6827  }
6828 
6829  SG_FREE(loss);
6830  loss=NULL;
6831 
6832  h->set_orf_info(SGMatrix<int32_t>(orf_info, num_states, 2));
6833  h->set_sparse_features(features_sparse1, features_sparse2);
6834  h->set_plif_matrices(pm);
6835 
6836  if (segment_loss_non_zero)
6837  {
6838  SG_DEBUG("Using version with segment_loss\n")
6839  if (nbest==1)
6840  h->compute_nbest_paths(feat_dims[2], use_orf, 1,true,false);
6841  else
6842  h->compute_nbest_paths(feat_dims[2], use_orf, 2,true,false);
6843  }
6844  else
6845  {
6846  SG_DEBUG("Using version without segment_loss\n")
6847  if (nbest==1)
6848  h->compute_nbest_paths(feat_dims[2], use_orf, 1,false,false);
6849  else
6850  h->compute_nbest_paths(feat_dims[2], use_orf, 2,false,false);
6851  }
6852 
6853  SGVector<float64_t> p_prob=h->get_scores();
6854 
6855  SGMatrix<int32_t> states=h->get_states();
6856 
6857  SGMatrix<int32_t> my_pos=h->get_positions();
6858 
6859  // transcribe result
6860  float64_t* d_my_path= SG_MALLOC(float64_t, (nbest+nother)*M);
6861  float64_t* d_my_pos= SG_MALLOC(float64_t, (nbest+nother)*M);
6862 
6863  for (int32_t k=0; k<(nbest+nother); k++)
6864  {
6865  for (int32_t i=0; i<M; i++)
6866  {
6867  d_my_path[i*(nbest+nother)+k] = states.matrix[i+k*M] ;
6868  d_my_pos[i*(nbest+nother)+k] = my_pos.matrix[i+k*M] ;
6869  }
6870  }
6871  set_vector(p_prob.vector,nbest+nother);
6872  set_vector(d_my_path, (nbest+nother)*M);
6873  set_vector(d_my_pos, (nbest+nother)*M);
6874 
6875  SG_FREE(d_my_path);
6876  SG_FREE(d_my_pos);
6877 
6878  return true;
6879 
6880 }
6881 
6882 bool CSGInterface::cmd_best_path_trans_deriv()
6883 {
6884  int32_t num_states = ui_structure->get_num_states();
6885  int32_t* feat_dims = ui_structure->get_feature_dims();
6886  float64_t* features = (ui_structure->get_feature_matrix(false));
6887 
6888  CPlifMatrix* pm=ui_structure->get_plif_matrix();
6889  int32_t Nplif = pm->get_num_plifs();
6890  CPlif** PEN = pm->get_PEN();
6891 
6892  // ARG 1
6893  // transitions from initial state (#states x 1)
6894  int32_t Np=0;
6895  float64_t* p=NULL;
6896  get_vector(p, Np);
6897  if (Np!=num_states)
6898  SG_ERROR("Np!=num_states; Np:%i num_states:%i",Np,num_states)
6899 
6900  // ARG 2
6901  // transitions to end state (#states x 1)
6902  int32_t Nq=0;
6903  float64_t* q=NULL;
6904  get_vector(q, Nq);
6905  if (Nq!=num_states)
6906  SG_ERROR("Nq!=num_states; Nq:%i num_states:%i",Nq,num_states)
6907 
6908 
6909  // ARG 3
6910  // segment path (2 x #feature_positions)
6911  // masking/weighting of loss for specific
6912  // regions of the true path
6913  int32_t Nseg_path=0;
6914  int32_t Mseg_path=0;
6915  float64_t* seg_path;
6916  get_matrix(seg_path,Nseg_path,Mseg_path);
6917 
6918  // ARG 4
6919  // links for transitions (#transitions x 4)
6920  int32_t Na_trans=0;
6921  int32_t num_a_trans=0;
6922  float64_t* a_trans=NULL;
6923  get_matrix(a_trans, num_a_trans, Na_trans);
6924 
6925  // ARG 5
6926  // loss matrix (#segment x 2*#segments)
6927  // one (#segment x #segments)-matrix for segment loss
6928  // and one for nucleotide loss
6929  int32_t Nloss=0;
6930  int32_t Mloss=0;
6931  float64_t* loss=NULL;
6932  get_matrix(loss, Nloss,Mloss);
6933 
6934  // ARG 6
6935  // path to calc derivative for
6936  int32_t Nmystate_seq=0;
6937  int32_t* mystate_seq=NULL;
6938  get_vector(mystate_seq, Nmystate_seq);
6939 
6940  // ARG 7
6941  // positions of the path
6942  int32_t Nmypos_seq=0;
6943  int32_t* mypos_seq=NULL;
6944  get_vector(mypos_seq, Nmypos_seq);
6945 
6946 
6947  //a => a_trans
6948 
6949  int32_t max_plif_id = 0 ;
6950  int32_t max_plif_len = 1 ;
6951  for (int32_t i=0; i<Nplif; i++)
6952  {
6953  if (i>0 && PEN[i]->get_id()!=i)
6954  SG_ERROR("PEN[i]->get_id()!=i; PEN[%i]->get_id():%i ,\n",i, PEN[i]->get_id())
6955  if (i>max_plif_id)
6956  max_plif_id=i ;
6957  if (PEN[i]->get_plif_len()>max_plif_len)
6958  max_plif_len=PEN[i]->get_plif_len() ;
6959  } ;
6960 
6961 
6962  CDynProg* h = ui_structure->get_dyn_prog();
6963  CSegmentLoss* seg_loss_obj = h->get_segment_loss_object();
6964  h->set_num_states(num_states) ;
6965  h->set_p_vector(SGVector<float64_t>(p, num_states)) ;
6966  h->set_q_vector(SGVector<float64_t>(q, num_states)) ;
6967 
6968  if (seg_path!=NULL)
6969  h->set_a_trans_matrix(SGMatrix<float64_t>(a_trans, num_a_trans, Na_trans)) ;
6970  else
6971  h->set_a_trans_matrix(SGMatrix<float64_t>(a_trans, num_a_trans, 3)) ;
6972 
6973  if (!h->check_svm_arrays())
6974  SG_ERROR("svm arrays inconsistent\n")
6975 
6976  int32_t *my_path = SG_MALLOC(int32_t, Nmypos_seq+1);
6977  memset(my_path, -1, Nmypos_seq*sizeof(int32_t)) ;
6978  int32_t *my_pos = SG_MALLOC(int32_t, Nmypos_seq+1);
6979  memset(my_pos, -1, Nmypos_seq*sizeof(int32_t)) ;
6980 
6981  h->set_observation_matrix(SGNDArray<float64_t>(features, feat_dims, 3));
6982  for (int32_t i=0; i<Nmypos_seq; i++)
6983  {
6984  my_path[i] = mystate_seq[i] ;
6985  my_pos[i] = mypos_seq[i] ;
6986  }
6987 
6988  if (seg_path!=NULL)
6989  {
6990  h->best_path_set_segment_loss(SGMatrix<float64_t>(loss, Nloss, Mloss)) ;
6991  seg_loss_obj->set_segment_loss(loss, Nloss, Mloss);
6992  }
6993  else
6994  {
6995  float64_t zero2[2] = {0.0, 0.0} ;
6996  h->best_path_set_segment_loss(SGMatrix<float64_t>(zero2, 2, 1, false)) ;
6997  seg_loss_obj->set_segment_loss(zero2, 2, 1);
6998  }
6999  h->set_content_type_array(SGMatrix<float64_t>(seg_path,Nseg_path,Mseg_path));
7000 
7001  float64_t* p_Plif_deriv = SG_MALLOC(float64_t, (max_plif_id+1)*max_plif_len);
7002  CDynamicArray <float64_t> a_Plif_deriv(p_Plif_deriv, max_plif_id+1, max_plif_len, false, false) ; // 2d
7003 
7004  float64_t* p_A_deriv = SG_MALLOC(float64_t, num_states*num_states);
7005  float64_t* p_p_deriv = SG_MALLOC(float64_t, num_states);
7006  float64_t* p_q_deriv = SG_MALLOC(float64_t, num_states);
7007 
7008  h->set_plif_matrices(pm);
7009  h->best_path_trans_deriv(my_path, my_pos, Nmypos_seq, features, feat_dims[2]);
7010 
7011  float64_t* p_my_scores;
7012  int32_t n_scores;
7013  h->get_path_scores(&p_my_scores, &n_scores);
7014 
7015  float64_t* p_my_losses;
7016  int32_t n_losses;
7017  h->get_path_losses(&p_my_losses, &n_losses);
7018 
7019  for (int32_t i=0; i<num_states; i++)
7020  {
7021  for (int32_t j=0; j<num_states; j++)
7022  p_A_deriv[i+j*num_states] = h->get_a_deriv(i, j) ;
7023 
7024  p_p_deriv[i]=h->get_p_deriv(i) ;
7025  p_q_deriv[i]=h->get_q_deriv(i) ;
7026  }
7027 
7028  for (int32_t id=0; id<=max_plif_id; id++)
7029  {
7030  int32_t len=0 ;
7031  const float64_t * deriv = PEN[id]->get_cum_derivative(len) ;
7032  ASSERT(len<=max_plif_len)
7033  for (int32_t j=0; j<max_plif_len; j++)
7034  a_Plif_deriv.element(id, j)= deriv[j] ;
7035  }
7036 
7037  set_vector(p_p_deriv, num_states);
7038  set_vector(p_q_deriv, num_states);
7039  set_matrix(p_A_deriv, num_states, num_states);
7040  set_matrix(p_Plif_deriv, (max_plif_id+1), max_plif_len);
7041  set_vector(p_my_scores, Nmypos_seq);
7042  set_vector(p_my_losses, Nmypos_seq);
7043 
7044  SG_FREE(p_A_deriv);
7045  SG_FREE(p_p_deriv);
7046  SG_FREE(p_q_deriv);
7047  SG_FREE(p_Plif_deriv);
7048  free(p_my_scores);
7049  free(p_my_losses);
7050 
7051  SG_FREE(my_path);
7052  SG_FREE(my_pos);
7053 
7054  SG_FREE(p);
7055  SG_FREE(q);
7056  SG_FREE(a_trans);
7057  SG_FREE(loss);
7058  SG_FREE(mystate_seq);
7059  SG_FREE(mypos_seq);
7060 
7061  return true ;
7062 }
7063 
7064 bool CSGInterface::cmd_precompute_subkernels()
7065 {
7066  if (m_nrhs!=1 || !create_return_values(0))
7067  return false;
7068 
7069  return ui_kernel->precompute_subkernels();
7070 }
7071 bool CSGInterface::cmd_crc()
7072 {
7073  if (m_nrhs!=2 || !create_return_values(1))
7074  return false;
7075 
7076  int32_t slen=0;
7077  char* string=get_string(slen);
7078  ASSERT(string)
7079  uint8_t* bstring=SG_MALLOC(uint8_t, slen);
7080 
7081  for (int32_t i=0; i<slen; i++)
7082  bstring[i]=string[i];
7083  SG_FREE(string);
7084 
7085  int32_t val=CHash::crc32(bstring, slen);
7086  SG_FREE(bstring);
7087  set_int(val);
7088 
7089  return true;
7090 }
7091 
7092 bool CSGInterface::cmd_system()
7093 {
7094  if (m_nrhs<2 || !create_return_values(0))
7095  return false;
7096 
7097  const int32_t MAX_LEN=10000;
7098  int32_t len=0;
7099  int32_t command_len=0;
7100  char* command=SG_MALLOC(char, MAX_LEN+1);
7101  memset(command, 0, sizeof(char)*MAX_LEN+1);
7102  char* cmd=get_str_from_str_or_direct(len);
7103  strncat(command, cmd, MAX_LEN);
7104  command_len+=len;
7105  SG_FREE(cmd);
7106 
7107  while (m_rhs_counter<m_nrhs)
7108  {
7109  char* arg=get_str_from_str_or_direct(len);
7110  command_len += len + 1;
7111 
7112  if (command_len >= MAX_LEN)
7113  {
7114  SG_FREE(arg);
7115  return false;
7116  }
7117  strcat(command, " ");
7118  strcat(command, arg);
7119  SG_FREE(arg);
7120  }
7121 
7122  int status=system(command);
7123  SG_FREE(command);
7124  return (status==0);
7125 }
7126 
7127 bool CSGInterface::cmd_exit()
7128 {
7129  exit(0);
7130  return true; //never reached but necessary to keep sun compiler happy
7131 }
7132 
7133 bool CSGInterface::cmd_exec()
7134 {
7135  if (m_nrhs<2 || !create_return_values(0))
7136  return false;
7137 
7138  int32_t len=0;
7139  char* filename=get_str_from_str_or_direct(len);
7140  FILE* file=fopen(filename, "r");
7141  if (!file)
7142  {
7143  SG_FREE(filename);
7144  SG_ERROR("Error opening file: %s.\n", filename)
7145  }
7146 
7147  while (!feof(file))
7148  {
7149  // FIXME: interpret lines as input
7150  break;
7151  }
7152 
7153  fclose(file);
7154  return true;
7155 }
7156 
7157 bool CSGInterface::cmd_set_output()
7158 {
7159  if (m_nrhs<2 || !create_return_values(0))
7160  return false;
7161 
7162  int32_t len=0;
7163  char* filename=get_str_from_str_or_direct(len);
7164 
7165  if (file_out)
7166  fclose(file_out);
7167  file_out=NULL;
7168 
7169  SG_INFO("Setting output file to: %s.\n", filename)
7170 
7171  if (strmatch(filename, "STDERR"))
7172  io->set_target(stderr);
7173  else if (strmatch(filename, "STDOUT"))
7174  io->set_target(stdout);
7175  else
7176  {
7177  file_out=fopen(filename, "w");
7178  if (!file_out)
7179  SG_ERROR("Error opening output file %s.\n", filename)
7180  io->set_target(file_out);
7181  }
7182 
7183  return true;
7184 }
7185 
7186 bool CSGInterface::cmd_set_threshold()
7187 {
7188  if (m_nrhs!=2 || !create_return_values(0))
7189  return false;
7190 
7191  float64_t value=get_real_from_real_or_str();
7192 
7193  ui_math->set_threshold(value);
7194  return true;
7195 }
7196 
7197 bool CSGInterface::cmd_init_random()
7198 {
7199  if (m_nrhs!=2 || !create_return_values(0))
7200  return false;
7201 
7202  uint32_t initseed=(uint32_t) get_int_from_int_or_str();
7203  ui_math->init_random(initseed);
7204 
7205  return true;
7206 }
7207 
7208 bool CSGInterface::cmd_set_num_threads()
7209 {
7210  if (m_nrhs!=2 || !create_return_values(0))
7211  return false;
7212 
7213  int32_t num_threads=get_int_from_int_or_str();
7214 
7215  parallel->set_num_threads(num_threads);
7216  SG_INFO("Set number of threads to %d.\n", num_threads)
7217 
7218  return true;
7219 }
7220 
7221 bool CSGInterface::cmd_translate_string()
7222 {
7223  if (m_nrhs!=4 || !create_return_values(1))
7224  return false;
7225 
7226  float64_t* string=NULL;
7227  int32_t len;
7228  get_vector(string, len);
7229 
7230  int32_t order=get_int();
7231  int32_t start=get_int();
7232 
7233  const int32_t max_val=2; /* DNA->2bits */
7234  int32_t i,j;
7235  uint16_t* obs=SG_MALLOC(uint16_t, len);
7236 
7237  for (i=0; i<len; i++)
7238  {
7239  switch ((char) string[i])
7240  {
7241  case 'A': obs[i]=0; break;
7242  case 'C': obs[i]=1; break;
7243  case 'G': obs[i]=2; break;
7244  case 'T': obs[i]=3; break;
7245  case 'a': obs[i]=0; break;
7246  case 'c': obs[i]=1; break;
7247  case 'g': obs[i]=2; break;
7248  case 't': obs[i]=3; break;
7249  default: SG_ERROR("Wrong letter in string.\n")
7250  }
7251  }
7252 
7253  //convert interval of size T
7254  for (i=len-1; i>=order-1; i--)
7255  {
7256  uint16_t value=0;
7257  for (j=i; j>=i-order+1; j--)
7258  value=(value>>max_val) | ((obs[j])<<(max_val*(order-1)));
7259 
7260  obs[i]=(uint16_t) value;
7261  }
7262 
7263  for (i=order-2;i>=0;i--)
7264  {
7265  uint16_t value=0;
7266  for (j=i; j>=i-order+1; j--)
7267  {
7268  value= (value >> max_val);
7269  if (j>=0)
7270  value|=(obs[j]) << (max_val * (order-1));
7271  }
7272  obs[i]=value;
7273  }
7274 
7275  float64_t* real_obs=SG_MALLOC(float64_t, len);
7276  for (i=start; i<len; i++)
7277  real_obs[i-start]=(float64_t) obs[i];
7278  SG_FREE(obs);
7279 
7280  set_vector(real_obs, len);
7281  SG_FREE(real_obs);
7282 
7283  return true;
7284 }
7285 
7286 bool CSGInterface::cmd_clear()
7287 {
7288  // reset guilib
7289  SG_UNREF(ui_classifier);
7290  ui_classifier=new CGUIClassifier(this);
7291  SG_UNREF(ui_distance);
7292  ui_distance=new CGUIDistance(this);
7293  SG_UNREF(ui_features);
7294  ui_features=new CGUIFeatures(this);
7295  SG_UNREF(ui_hmm);
7296  ui_hmm=new CGUIHMM(this);
7297  SG_UNREF(ui_kernel);
7298  ui_kernel=new CGUIKernel(this);
7299  SG_UNREF(ui_labels);
7300  ui_labels=new CGUILabels(this);
7301  SG_UNREF(ui_math);
7302  ui_math=new CGUIMath(this);
7303  SG_UNREF(ui_pluginestimate);
7304  ui_pluginestimate=new CGUIPluginEstimate(this);
7305  SG_UNREF(ui_preproc);
7306  ui_preproc=new CGUIPreprocessor(this);
7307  SG_UNREF(ui_time);
7308  ui_time=new CGUITime(this);
7309 
7310  return true;
7311 }
7312 
7313 bool CSGInterface::cmd_tic()
7314 {
7315  ui_time->start();
7316  return true;
7317 }
7318 
7319 bool CSGInterface::cmd_toc()
7320 {
7321  ui_time->stop();
7322  return true;
7323 }
7324 
7325 bool CSGInterface::cmd_print()
7326 {
7327  if (m_nrhs<2 || !create_return_values(0))
7328  return false;
7329 
7330  int32_t len=0;
7331  char* msg=get_str_from_str_or_direct(len);
7332 
7333  SG_PRINT("%s\n", msg)
7334 
7335  SG_FREE(msg);
7336  return true;
7337 }
7338 
7339 bool CSGInterface::cmd_echo()
7340 {
7341  if (m_nrhs<2 || !create_return_values(0))
7342  return false;
7343 
7344  int32_t len=0;
7345  char* level=get_str_from_str_or_direct(len);
7346 
7347  if (strmatch(level, "OFF"))
7348  {
7349  echo=false;
7350  SG_INFO("Echo is off.\n")
7351  }
7352  else
7353  {
7354  echo=true;
7355  SG_INFO("Echo is on.\n")
7356  }
7357 
7358  SG_FREE(level);
7359  return true;
7360 }
7361 
7362 bool CSGInterface::cmd_loglevel()
7363 {
7364  if (m_nrhs<2 || !create_return_values(0))
7365  return false;
7366 
7367  int32_t len=0;
7368  char* level=get_str_from_str_or_direct(len);
7369 
7370  if (strmatch(level, "ALL") || strmatch(level, "GCDEBUG"))
7371  io->set_loglevel(MSG_GCDEBUG);
7372  else if (strmatch(level, "DEBUG"))
7373  io->set_loglevel(MSG_DEBUG);
7374  else if (strmatch(level, "INFO"))
7375  io->set_loglevel(MSG_INFO);
7376  else if (strmatch(level, "NOTICE"))
7377  io->set_loglevel(MSG_NOTICE);
7378  else if (strmatch(level, "WARN"))
7379  io->set_loglevel(MSG_WARN);
7380  else if (strmatch(level, "ERROR"))
7381  io->set_loglevel(MSG_ERROR);
7382  else if (strmatch(level, "CRITICAL"))
7383  io->set_loglevel(MSG_CRITICAL);
7384  else if (strmatch(level, "ALERT"))
7385  io->set_loglevel(MSG_ALERT);
7386  else if (strmatch(level, "EMERGENCY"))
7387  io->set_loglevel(MSG_EMERGENCY);
7388  else
7389  SG_ERROR("Unknown loglevel '%s'.\n", level)
7390 
7391  SG_INFO("Loglevel set to %s.\n", level)
7392 
7393  SG_FREE(level);
7394  return true;
7395 }
7396 
7397 bool CSGInterface::cmd_syntax_highlight()
7398 {
7399  if (m_nrhs<2 || !create_return_values(0))
7400  return false;
7401 
7402  int32_t len=0;
7403  char* hili=get_str_from_str_or_direct(len);
7404 
7405  if (strmatch(hili, "ON"))
7406  {
7407  hilight.set_ansi_syntax_hilighting();
7408  io->enable_syntax_highlighting();
7409  }
7410  else if (strmatch(hili, "OFF"))
7411  {
7412  hilight.disable_syntax_hilighting();
7413  io->disable_syntax_highlighting();
7414  }
7415  else
7416  SG_ERROR("arguments to " N_SYNTAX_HIGHLIGHT " are ON|OFF - found '%s'.\n", hili)
7417 
7418  SG_INFO("Syntax hilighting set to %s.\n", hili)
7419 
7420  SG_FREE(hili);
7421  return true;
7422 }
7423 
7424 bool CSGInterface::cmd_progress()
7425 {
7426  if (m_nrhs<2 || !create_return_values(0))
7427  return false;
7428 
7429  int32_t len=0;
7430  char* progress=get_str_from_str_or_direct(len);
7431 
7432  if (strmatch(progress, "ON"))
7433  io->enable_progress();
7434  else if (strmatch(progress, "OFF"))
7435  io->disable_progress();
7436  else
7437  SG_ERROR("arguments to progress are ON|OFF - found '%s'.\n", progress)
7438 
7439  SG_INFO("Progress set to %s.\n", progress)
7440 
7441  SG_FREE(progress);
7442  return true;
7443 }
7444 
7445 bool CSGInterface::cmd_get_version()
7446 {
7447  if (m_nrhs!=1 || !create_return_values(1))
7448  return false;
7449 
7450  set_int(version->get_version_revision());
7451 
7452  return true;
7453 }
7454 
7455 bool CSGInterface::cmd_help()
7456 {
7457  if ((m_nrhs!=1 && m_nrhs!=2) || !create_return_values(0))
7458  return false;
7459 
7460  int32_t i=0;
7461 
7462  SG_PRINT("\n")
7463  if (m_nrhs==1) // unspecified help
7464  {
7465  SG_PRINT("Help is available for the following topics.\n"
7466  "-------------------------------------------\n\n");
7467  while (sg_methods[i].command)
7468  {
7469  bool is_group_item=false;
7470  if (!sg_methods[i].method && !sg_methods[i].usage_prefix)
7471  is_group_item=true;
7472 
7473  if (is_group_item)
7474  {
7475  SG_PRINT("%s%s%s\n",
7476  hilight.get_command_prefix(),
7477  sg_methods[i].command,
7478  hilight.get_command_suffix());
7479  }
7480 
7481  i++;
7482  }
7483  SG_PRINT("\nUse sg('%shelp%s', '%s<topic>%s')"
7484  " to see the list of commands in this group, e.g.\n\n"
7485  "\tsg('%shelp%s', '%sFeatures%s')\n\n"
7486  "to see the list of commands for the 'Features' group.\n"
7487  "\nOr use sg('%shelp%s', '%sall%s')"
7488  " to see a brief listing of all commands.\n\nTo disable syntax"
7489  " highlighting (useful e.g. in the matlab GUI) use\n\n"
7490  "\tsg('syntax_highlight','OFF')\n",
7491  hilight.get_command_prefix(), hilight.get_command_suffix(),
7492  hilight.get_command_prefix(), hilight.get_command_suffix(),
7493  hilight.get_command_prefix(), hilight.get_command_suffix(),
7494  hilight.get_command_prefix(), hilight.get_command_suffix(),
7495  hilight.get_command_prefix(), hilight.get_command_suffix(),
7496  hilight.get_command_prefix(), hilight.get_command_suffix());
7497  }
7498  else // m_nrhs == 2 -> all commands, single command or group help
7499  {
7500  bool found=false;
7501  bool in_group=false;
7502  int32_t clen=0;
7503  char* command=get_string(clen);
7504 
7505  if (strmatch("doxygen", command) || strmatch("DOXYGEN", command))
7506  {
7507  found=true;
7508  while (sg_methods[i].command)
7509  {
7510  if (sg_methods[i].usage_prefix) // display group item
7511  {
7512  SG_PRINT("\\arg \\b %s \\verbatim %s%s%s \\endverbatim\n",
7513  sg_methods[i].command,
7514  sg_methods[i].usage_prefix,
7515  sg_methods[i].command,
7516  sg_methods[i].usage_suffix);
7517  }
7518  else if (!sg_methods[i].method) // display group
7519  {
7520  SG_PRINT("\n\\section %s_sec %s\n",
7521  sg_methods[i].command, sg_methods[i].command);
7522  }
7523  i++;
7524  }
7525  }
7526  if (strmatch("all", command) || strmatch("ALL", command))
7527  {
7528  found=true;
7529  while (sg_methods[i].command)
7530  {
7531  if (sg_methods[i].usage_prefix) // display group item
7532  {
7533  SG_PRINT("\t%s%s%s%s%s\n", sg_methods[i].usage_prefix,
7534  hilight.get_command_prefix(),
7535  sg_methods[i].command,
7536  hilight.get_command_suffix(),
7537  sg_methods[i].usage_suffix);
7538  }
7539  else if (!sg_methods[i].method) // display group
7540  {
7541  SG_PRINT("\nCommands in group %s%s%s\n",
7542  hilight.get_command_prefix(),
7543  sg_methods[i].command,
7544  hilight.get_command_suffix());
7545  }
7546  i++;
7547  }
7548  }
7549  else
7550  {
7551  while (sg_methods[i].command)
7552  {
7553  if (in_group)
7554  {
7555  if (sg_methods[i].usage_prefix) // display group item
7556  SG_PRINT("\t%s%s%s\n",
7557  hilight.get_command_prefix(),
7558  sg_methods[i].command,
7559  hilight.get_command_suffix())
7560  else // next group reached -> end
7561  break;
7562  }
7563  else
7564  {
7565  found=strmatch(sg_methods[i].command, command);
7566  if (found)
7567  {
7568  if (sg_methods[i].usage_prefix) // found item
7569  {
7570  SG_PRINT("Usage for %s%s%s\n\n\t%s%s%s%s%s\n",
7571  hilight.get_command_prefix(),
7572  sg_methods[i].command,
7573  hilight.get_command_suffix(),
7574  sg_methods[i].usage_prefix,
7575  hilight.get_command_prefix(),
7576  sg_methods[i].command,
7577  hilight.get_command_suffix(),
7578  sg_methods[i].usage_suffix)
7579  break;
7580  }
7581  else // found group item
7582  {
7583  SG_PRINT("Commands in group %s%s%s\n\n",
7584  hilight.get_command_prefix(),
7585  sg_methods[i].command,
7586  hilight.get_command_suffix())
7587  in_group=true;
7588  }
7589  }
7590  }
7591 
7592  i++;
7593  }
7594  }
7595 
7596  if (!found)
7597  SG_PRINT("Could not find help for command %s.\n", command)
7598  else if (in_group)
7599  {
7600  SG_PRINT("\n\nUse sg('%shelp%s', '%s<command>%s')"
7601  " to see the usage pattern of a single command, e.g.\n\n"
7602  "\tsg('%shelp%s', '%sclassify%s')\n\n"
7603  " to see the usage pattern of the command 'classify'.\n",
7604  hilight.get_command_prefix(), hilight.get_command_suffix(),
7605  hilight.get_command_prefix(), hilight.get_command_suffix(),
7606  hilight.get_command_prefix(), hilight.get_command_suffix(),
7607  hilight.get_command_prefix(), hilight.get_command_suffix());
7608  }
7609 
7610  SG_FREE(command);
7611  }
7612 
7613 
7614  SG_PRINT("\n")
7615 
7616  return true;
7617 }
7618 #ifdef TRACE_MEMORY_ALLOCS
7619  extern CMap<void*, MemoryBlock>* sg_mallocs;
7620 #endif
7621 
7622 bool CSGInterface::cmd_whos()
7623 {
7624  if ((m_nrhs!=1) || !create_return_values(0))
7625  return false;
7626 
7627 #ifdef TRACE_MEMORY_ALLOCS
7628  SG_PRINT("Blocks allocated by shogun\n")
7629  list_memory_allocs();
7630  SG_PRINT("\n")
7631  return true;
7632 #else
7633  SG_PRINT("Requires shogun to be compiled with --enable-trace-mallocs\n")
7634  return false;
7635 #endif
7636 }
7637 
7638 bool CSGInterface::cmd_send_command()
7639 {
7641 
7642  int32_t len=0;
7643  char* arg=get_string(len);
7644  //SG_DEBUG("legacy: arg == %s\n", arg)
7645  m_legacy_strptr=arg;
7646 
7647  char* command=get_str_from_str(len);
7648  int32_t i=0;
7649  bool success=false;
7650 
7651  while (sg_methods[i].command)
7652  {
7653  if (strmatch(command, sg_methods[i].command))
7654  {
7655  SG_DEBUG("legacy: found command %s\n", sg_methods[i].command)
7656  // fix-up m_nrhs; +1 to include command
7657  m_nrhs=get_num_args_in_str()+1;
7658 
7659  if (!(interface->*(sg_methods[i].method))())
7660  {
7661  SG_ERROR("Usage: %s%s%s\n\n\t%s%s%s%s%s\n",
7662  hilight.get_command_prefix(),
7663  sg_methods[i].command,
7664  hilight.get_command_suffix(),
7665  sg_methods[i].usage_prefix,
7666  hilight.get_command_prefix(),
7667  sg_methods[i].command,
7668  hilight.get_command_suffix(),
7669  sg_methods[i].usage_suffix);
7670  }
7671  else
7672  {
7673  success=true;
7674  break;
7675  }
7676  }
7677 
7678  i++;
7679  }
7680 
7681  if (!success)
7682  SG_ERROR("Non-supported legacy command %s.\n", command)
7683 
7684  SG_FREE(command);
7685  SG_FREE(arg);
7686  return success;
7687 }
7688 
7689 bool CSGInterface::cmd_run_python()
7690 {
7691  SG_ERROR("Only available in the elwms interface\n")
7692  return false;
7693 }
7694 
7695 bool CSGInterface::cmd_run_octave()
7696 {
7697  SG_ERROR("Only available in the elwms interface\n")
7698  return false;
7699 }
7700 
7701 bool CSGInterface::cmd_run_r()
7702 {
7703  SG_ERROR("Only available in the elwms interface\n")
7704  return false;
7705 }
7706 
7707 bool CSGInterface::cmd_pr_loqo()
7708 {
7709  if (m_nrhs!=7 || !create_return_values(2))
7710  return false;
7711 
7712  float64_t* c=NULL;
7713  int32_t lenc=0;
7714  get_vector(c, lenc);
7715 
7716  int32_t n = lenc;
7717 
7718  float64_t* H=NULL;
7719  int32_t nH=0;
7720  int32_t mH=0;
7721  get_matrix(H, nH, mH);
7722  ASSERT(nH==n && mH==n)
7723 
7724  float64_t* A=NULL;
7725  int32_t nA=0;
7726  int32_t mA=0;
7727  get_matrix(A, nA, mA);
7728  ASSERT(mA==n)
7729  int32_t m=nA;
7730 
7731  float64_t* b=NULL;
7732  int32_t lenb=0;
7733  get_vector(b, lenb);
7734  ASSERT(lenb==m)
7735 
7736  float64_t* l=NULL;
7737  int32_t lenl=0;
7738  get_vector(l, lenl);
7739  ASSERT(lenl==n)
7740 
7741  float64_t* u=NULL;
7742  int32_t lenu=0;
7743  get_vector(u, lenu);
7744  ASSERT(lenu==n)
7745 
7746  float64_t* x=SG_MALLOC(float64_t, 3*n);
7747  SGVector<float64_t>::fill_vector(x, 3*n, 0.0);
7748 
7749  float64_t* y=SG_MALLOC(float64_t, m+2*n);
7750  SGVector<float64_t>::fill_vector(y, m+2*n, 0.0);
7751 
7752  pr_loqo(n,m, c, H, A, b, l, u, x, y, 0, 5, 50, 0.05, 100, 0);
7753 
7754  set_vector(x, n);
7755  set_vector(y, m);
7756 
7757  SG_FREE(c);
7758  SG_FREE(H);
7759  SG_FREE(A);
7760  SG_FREE(b);
7761  SG_FREE(l);
7762  SG_FREE(u);
7763  SG_FREE(x);
7764  SG_FREE(y);
7765  return true;
7766 }
7767 
7768 void CSGInterface::print_prompt()
7769 {
7770  SG_PRINT("%sshogun%s >> ",
7771  hilight.get_prompt_prefix(),
7772  hilight.get_prompt_suffix());
7773 }
7774 
7776 // legacy-related methods
7778 
7779 char* CSGInterface::get_str_from_str_or_direct(int32_t& len)
7780 {
7781  if (m_legacy_strptr)
7782  return get_str_from_str(len);
7783  else
7784  return get_string(len);
7785 }
7786 
7787 int32_t CSGInterface::get_int_from_int_or_str()
7788 {
7789  if (m_legacy_strptr)
7790  {
7791  int32_t len=0;
7792  char* str=get_str_from_str(len);
7793  int32_t val=strtol(str, NULL, 10);
7794 
7795  SG_FREE(str);
7796  return val;
7797  }
7798  else
7799  return get_int();
7800 }
7801 
7802 float64_t CSGInterface::get_real_from_real_or_str()
7803 {
7804  if (m_legacy_strptr)
7805  {
7806  int32_t len=0;
7807  char* str=get_str_from_str(len);
7808  float64_t val=strtod(str, NULL);
7809 
7810  SG_FREE(str);
7811  return val;
7812  }
7813  else
7814  return get_real();
7815 }
7816 
7817 bool CSGInterface::get_bool_from_bool_or_str()
7818 {
7819  if (m_legacy_strptr)
7820  {
7821  int32_t len=0;
7822  char* str=get_str_from_str(len);
7823  bool val=strtol(str, NULL, 10)!=0;
7824 
7825  SG_FREE(str);
7826  return val;
7827  }
7828  else
7829  return get_bool();
7830 }
7831 
7832 void CSGInterface::get_vector_from_int_vector_or_str(int32_t*& vector, int32_t& len)
7833 {
7834  if (m_legacy_strptr)
7835  {
7836  len=get_vector_len_from_str(len);
7837  if (len==0)
7838  {
7839  vector=NULL;
7840  return;
7841  }
7842 
7843  vector=SG_MALLOC(int32_t, len);
7844  char* str=NULL;
7845  int32_t slen=0;
7846  for (int32_t i=0; i<len; i++)
7847  {
7848  str=get_str_from_str(slen);
7849  vector[i]=strtol(str, NULL, 10);
7850  //SG_DEBUG("vec[%d]: %d\n", i, vector[i])
7851  SG_FREE(str);
7852  }
7853  }
7854  else
7855  get_vector(vector, len);
7856 }
7857 
7858 void CSGInterface::get_vector_from_real_vector_or_str(
7859  float64_t*& vector, int32_t& len)
7860 {
7861  if (m_legacy_strptr)
7862  {
7863  len=get_vector_len_from_str(len);
7864  if (len==0)
7865  {
7866  vector=NULL;
7867  return;
7868  }
7869 
7870  vector=SG_MALLOC(float64_t, len);
7871  char* str=NULL;
7872  int32_t slen=0;
7873  for (int32_t i=0; i<len; i++)
7874  {
7875  str=get_str_from_str(slen);
7876  vector[i]=strtod(str, NULL);
7877  //SG_DEBUG("vec[%d]: %f\n", i, vector[i])
7878  SG_FREE(str);
7879  }
7880  }
7881  else
7882  get_vector(vector, len);
7883 }
7884 
7885 int32_t CSGInterface::get_vector_len_from_str(int32_t expected_len)
7886 {
7887  int32_t num_args=get_num_args_in_str();
7888 
7889  if (expected_len==0 || num_args==expected_len)
7890  return num_args;
7891  else if (num_args==2*expected_len)
7892  {
7893  // special case for position_weights; a bit shaky...
7894  return expected_len;
7895  }
7896  else
7897  SG_ERROR("Expected vector length %d does not match actual length %d.\n", expected_len, num_args)
7898 
7899  return 0;
7900 }
7901 
7902 char* CSGInterface::get_str_from_str(int32_t& len)
7903 {
7904  if (!m_legacy_strptr)
7905  return NULL;
7906 
7907  int32_t i=0;
7908  while (m_legacy_strptr[i]!='\0' && !isspace(m_legacy_strptr[i]))
7909  i++;
7910 
7911  len=i;
7912  char* str=SG_MALLOC(char, len+1);
7913  for (i=0; i<len; i++)
7914  str[i]=m_legacy_strptr[i];
7915  str[len]='\0';
7916 
7917  // move legacy strptr
7918  if (m_legacy_strptr[len]=='\0')
7919  m_legacy_strptr=NULL;
7920  else
7921  {
7922  m_legacy_strptr=m_legacy_strptr+len;
7923  m_legacy_strptr=SGIO::skip_spaces(m_legacy_strptr);
7924  }
7925 
7926  return str;
7927 }
7928 
7929 int32_t CSGInterface::get_num_args_in_str()
7930 {
7931  if (!m_legacy_strptr)
7932  return 0;
7933 
7934  int32_t count=0;
7935  int32_t i=0;
7936  bool in_arg=false;
7937  while (m_legacy_strptr[i]!='\0')
7938  {
7939  if (!isspace(m_legacy_strptr[i]) && !in_arg)
7940  {
7941  count++;
7942  in_arg=true;
7943  }
7944  else if (isspace(m_legacy_strptr[i]) && in_arg)
7945  in_arg=false;
7946 
7947  i++;
7948  }
7949 
7950  return count;
7951 }
7952 
7954 // handler
7956 
7957 bool CSGInterface::handle()
7958 {
7959  int32_t len=0;
7960  bool success=false;
7961 
7962 #ifndef WIN32
7964 #endif
7965 
7966  char* command=NULL;
7967  command=interface->get_command(len);
7968 
7969  SG_DEBUG("command: %s, nrhs %d\n", command, m_nrhs)
7970  int32_t i=0;
7971  while (sg_methods[i].command)
7972  {
7973  if (strmatch(command, sg_methods[i].command))
7974  {
7975  SG_DEBUG("found command %s%s%s\n",
7976  hilight.get_command_prefix(),
7977  sg_methods[i].command,
7978  hilight.get_command_suffix());
7979 
7980  if (!(interface->*(sg_methods[i].method))())
7981  {
7982  if (sg_methods[i].usage_prefix)
7983  {
7984  SG_ERROR("Usage: %s%s%s\n\n\t%s%s%s%s%s\n",
7985  hilight.get_command_prefix(),
7986  sg_methods[i].command,
7987  hilight.get_command_suffix(),
7988  sg_methods[i].usage_prefix,
7989  hilight.get_command_prefix(),
7990  sg_methods[i].command,
7991  hilight.get_command_suffix(),
7992  sg_methods[i].usage_suffix);
7993  }
7994  else
7995  SG_ERROR("Non-supported command %s%s%s.\n",
7996  hilight.get_command_prefix(),
7997  sg_methods[i].command,
7998  hilight.get_command_suffix());
7999  }
8000  else
8001  {
8002  success=true;
8003  break;
8004  }
8005  }
8006  i++;
8007  }
8008 
8009 #ifndef WIN32
8011 #endif
8012 
8013  if (!success)
8014  SG_ERROR("Unknown command %s%s%s.\n",
8015  hilight.get_command_prefix(),
8016  command,
8017  hilight.get_command_suffix());
8018 
8019  SG_FREE(command);
8020  return success;
8021 }
#define N_HELP
Definition: GUICommands.h:214
virtual const char * get_name() const =0
float distance(CJLCoverTreePoint p1, CJLCoverTreePoint p2, float64_t upper_bound)
#define N_GET_SVM_OBJECTIVE
Definition: GUICommands.h:27
#define N_COMPUTE_MKL_PRIMAL_OBJECTIVE
Definition: GUICommands.h:30
virtual int32_t get_max_vector_length()
#define N_NEW_CLASSIFIER
Definition: GUICommands.h:119
UI labels.
Definition: GUILabels.h:24
#define N_COMPUTE_BY_SUBKERNELS
Definition: GUICommands.h:39
#define N_RELATIVE_ENTROPY
Definition: GUICommands.h:144
#define N_CLASSIFY
Definition: GUICommands.h:107
EMachineType
Definition: Machine.h:33
#define N_QUIT
Definition: GUICommands.h:210
Computes the standard linear kernel on CDotFeatures.
Definition: LinearKernel.h:33
#define SG_INFO(...)
Definition: SGIO.h:120
#define N_GET_WD_SCORING
Definition: GUICommands.h:50
static bool set_handler()
Definition: Signal.cpp:66
void best_path_trans_deriv(int32_t *my_state_seq, int32_t *my_pos_seq, int32_t my_seq_len, const float64_t *seq_array, int32_t max_num_signals)
Definition: DynProg.cpp:2051
#define N_EMBED
Definition: GUICommands.h:236
DNA - letters A,C,G,T.
Definition: Alphabet.h:23
#define N_SET_CONSTRAINT_GENERATOR
Definition: GUICommands.h:218
static void fill_vector(T *vec, int32_t len, T value)
Definition: SGVector.cpp:271
#define N_ADD_DOTFEATURES
Definition: GUICommands.h:97
#define N_VITERBI_TRAIN
Definition: GUICommands.h:169
#define N_INIT_KERNEL_OPTIMIZATION
Definition: GUICommands.h:165
virtual ELabelType get_label_type() const =0
#define N_HMM_CLASSIFY_EXAMPLE
Definition: GUICommands.h:106
CSGInterface * interface
Definition: SGInterface.cpp:45
binary labels +1/-1
Definition: LabelTypes.h:14
#define N_NEW_HMM
Definition: GUICommands.h:117
static float64_t * H
Definition: libbmrm.cpp:27
#define N_ENT_LAMBDA
Definition: GUICommands.h:187
#define N_SVM_MAX_TRAIN_TIME
Definition: GUICommands.h:190
Real Labels are real-valued labels.
#define N_SIGNALS_ADD_FEATURE
Definition: GUICommands.h:67
UI kernel.
Definition: GUIKernel.h:24
The class DenseFeatures implements dense feature matrices.
Definition: LDA.h:24
EKernelType
Definition: Kernel.h:51
static char * skip_spaces(char *str)
Definition: SGIO.cpp:257
virtual bool has_features()
Definition: Distance.h:300
#define N_LOAD_FEATURES
Definition: GUICommands.h:136
void set_lin_feat(float64_t *p_lin_feat, int32_t p_num_svms, int32_t p_seq_len)
Definition: DynProg.h:425
#define N_SIGNALS_SET_MODEL
Definition: GUICommands.h:62
void set_ansi_syntax_hilighting()
Class Distance, a base class for all the distances used in the Shogun toolbox.
Definition: Distance.h:80
#define N_INIT_DYN_PROG
Definition: GUICommands.h:79
#define N_ADD_MULTIPLE_FEATURES
Definition: GUICommands.h:96
#define N_RESHAPE
Definition: GUICommands.h:139
float64_t compute_svm_primal_objective()
Definition: SVM.cpp:267
#define N_SET_PERCEPTRON_PARAMETERS
Definition: GUICommands.h:127
int32_t get_M() const
access function for number of observations M
Definition: HMM.h:986
RAWDNA - letters 0,1,2,3.
Definition: Alphabet.h:26
void set_dict_weights(SGMatrix< float64_t > dictionary_weights)
Definition: DynProg.cpp:778
virtual void set_w(const SGVector< float64_t > src_w)
#define N_CLEAN_KERNEL
Definition: GUICommands.h:151
#define N_SET_FEATURE_MATRIX_SPARSE
Definition: GUICommands.h:85
void set_observations(CStringFeatures< uint16_t > *obs, CHMM *hmm=NULL)
Definition: HMM.cpp:5309
#define N_INIT_RANDOM
Definition: GUICommands.h:220
#define N_HMM_LIKELIHOOD
Definition: GUICommands.h:37
void set_plif_matrices(CPlifMatrix *pm)
Definition: DynProg.cpp:745
The class Labels models labels, i.e. class assignments of objects.
Definition: Labels.h:35
#define N_GET_LIN_FEAT
Definition: GUICommands.h:76
void create_word_string()
Definition: DynProg.cpp:360
#define N_ADD_STATES
Definition: GUICommands.h:196
#define N_LINEAR_TRAIN
Definition: GUICommands.h:171
#define N_SAVE_HMM
Definition: GUICommands.h:132
virtual int32_t get_num_labels() const =0
void set_plif_transform_type(SGString< char > *transform_type, int32_t num_values, int32_t maxlen=0)
Definition: PlifMatrix.cpp:157
real valued labels (e.g. for regression, classifier outputs)
Definition: LabelTypes.h:18
#define N_TIC
Definition: GUICommands.h:227
void set_observation_matrix(SGNDArray< float64_t > seq)
Definition: DynProg.cpp:656
float64_t get_a_deriv(T_STATES line_, T_STATES column) const
Definition: DynProg.h:456
multi-class labels 0,1,...
Definition: LabelTypes.h:16
#define N_LIKELIHOOD
Definition: GUICommands.h:202
char * get_plif_name() const
Definition: Plif.cpp:103
static uint32_t crc32(uint8_t *data, int32_t len)
Definition: Hash.cpp:37
UI HMM (Hidden Markov Model)
Definition: GUIHMM.h:26
SGMatrix< ST > get_feature_matrix()
void set_gene_string(SGVector< char > genestr)
Definition: DynProg.cpp:754
void set_orf_info(SGMatrix< int32_t > orf_info)
Definition: DynProg.cpp:722
void get_path_scores(float64_t **my_scores, int32_t *seq_len)
Definition: DynProg.cpp:862
#define SG_UNREF(x)
Definition: SGRefObject.h:35
bool check_svm_arrays()
Definition: DynProg.cpp:600
CPlifBase ** get_plif_matrix()
Definition: PlifMatrix.h:52
#define N_TRAIN_REGRESSION
Definition: GUICommands.h:125
#define N_GET_FEATURES
Definition: GUICommands.h:54
#define N_SYSTEM
Definition: GUICommands.h:216
#define N_COMPUTE_RELATIVE_MKL_DUALITY_GAP
Definition: GUICommands.h:32
static uint32_t get_seed()
returns number generator seed
Definition: Math.h:1212
#define N_TRAIN_CLASSIFIER
Definition: GUICommands.h:124
#define N_BEST_PATH
Definition: GUICommands.h:168
#define N_OUTPUT_HMM_DEFINED
Definition: GUICommands.h:209
SGVector< float64_t > get_scores()
Definition: DynProg.cpp:834
#define N_SET_PLIF_STRUCT
Definition: GUICommands.h:72
virtual int32_t get_num_vectors() const =0
#define N_SET_HMM
Definition: GUICommands.h:59
#define N_GET_WD_POS_WEIGHTS
Definition: GUICommands.h:48
#define N_SIGNALS_RUN
Definition: GUICommands.h:69
CFeatures * get_rhs()
Definition: Kernel.h:349
#define SG_ERROR(...)
Definition: SGIO.h:131
#define N_SET_PRIOR_PROBS_FROM_LABELS
Definition: GUICommands.h:157
#define N_DEL_LAST_KERNEL
Definition: GUICommands.h:150
#define SG_NOTIMPLEMENTED
Definition: SGIO.h:141
#define N_LOAD_HMM
Definition: GUICommands.h:131
UI time.
Definition: GUITime.h:23
The class Alphabet implements an alphabet and alphabet utility functions.
Definition: Alphabet.h:88
#define N_TRANSLATE_STRING
Definition: GUICommands.h:20
#define N_GET_SPEC_CONSENSUS
Definition: GUICommands.h:53
#define N_SET_SUBKERNEL_WEIGHTS_COMBINED
Definition: GUICommands.h:41
int32_t get_num_svms()
Definition: DynProg.cpp:190
index_t num_cols
Definition: SGMatrix.h:303
#define N_ONE_CLASS_LINEAR_HMM_CLASSIFY
Definition: GUICommands.h:103
#define RNG_SEED_SIZE
Definition: Math.h:70
#define N_COMPUTE_ABSOLUTE_MKL_DUALITY_GAP
Definition: GUICommands.h:33
float64_t get_b(T_STATES line_, uint16_t column) const
Definition: HMM.h:1159
static uint32_t get_log_range()
returns range of logtable
Definition: Math.h:1218
const char * get_command_suffix()
#define N_NEW_CLUSTERING
Definition: GUICommands.h:122
int32_t get_plif_len()
Definition: Plif.h:396
class IntronList
Definition: SegmentLoss.h:22
#define N_SIGNALS_SET_SPLIT
Definition: GUICommands.h:65
The SalzbergWordString kernel implements the Salzberg kernel.
#define N_RUN_R
Definition: GUICommands.h:232
A generic KernelMachine interface.
Definition: KernelMachine.h:50
#define N_C
Definition: GUICommands.h:177
CSyntaxHighLight hilight
Definition: SGInterface.cpp:46
bool set_position_weights(float64_t *pws, int32_t len)
int32_t get_num_limits()
Definition: PlifMatrix.h:78
float64_t get_label(int32_t idx)
void set_pos(SGVector< int32_t > pos)
Definition: DynProg.cpp:716
void set_plif_use_cache(SGVector< bool > use_cache)
Definition: PlifMatrix.cpp:80
#define N_VITERBI_TRAIN_DEFINED
Definition: GUICommands.h:170
#define N_DO_AUC_MAXIMIZATION
Definition: GUICommands.h:195
#define N_SET_OUTPUT
Definition: GUICommands.h:225
int32_t get_num_positions()
Definition: DynProg.cpp:678
Features that compute the Weighted Degreee Kernel feature space explicitly.
Definition: WDFeatures.h:28
Features that support dot products among other operations.
Definition: DotFeatures.h:41
#define N_USE_BATCH_COMPUTATION
Definition: GUICommands.h:203
#define N_PROGRESS
Definition: GUICommands.h:179
#define N_PLUGIN_ESTIMATE_CLASSIFY_EXAMPLE
Definition: GUICommands.h:114
SGMatrix< float64_t > get_kernel_matrix()
Definition: Kernel.h:211
#define N_CLEAR
Definition: GUICommands.h:172
The WeightedCommWordString kernel may be used to compute the weighted spectrum kernel (i...
#define N_TOC
Definition: GUICommands.h:228
#define N_GET_CLASSIFIER
Definition: GUICommands.h:25
class Plif
Definition: Plif.h:38
index_t num_rows
Definition: SGMatrix.h:301
#define N_GET_DISTANCE_MATRIX
Definition: GUICommands.h:36
A generic learning machine interface.
Definition: Machine.h:138
UI classifier.
Definition: GUIClassifier.h:24
#define N_GET_PLUGIN_ESTIMATE
Definition: GUICommands.h:111
#define N_PR_LOQO
Definition: GUICommands.h:233
bool set_label(int32_t idx, float64_t label)
#define N_SEND_COMMAND
Definition: GUICommands.h:18
#define N_SET_MODEL
Definition: GUICommands.h:83
#define N_CONVERGENCE_CRITERIA
Definition: GUICommands.h:174
#define N_INIT_INTRON_LIST
Definition: GUICommands.h:77
Features that compute the Weighted Spectrum Kernel feature space explicitly.
#define N_ECHO
Definition: GUICommands.h:181
#define N_LOAD_CLASSIFIER
Definition: GUICommands.h:133
#define N_KRR_TAU
Definition: GUICommands.h:229
void precompute_content_values()
Definition: DynProg.cpp:391
#define USAGE_IO(method, in, out)
Definition: SGInterface.cpp:66
Multiclass Labels for multi-class classification.
class IntronList
Definition: IntronList.h:20
static const float64_t epsilon
Definition: libbmrm.cpp:25
#define N_SAVE_CLASSIFIER
Definition: GUICommands.h:134
void init_list(int32_t *all_pos, int32_t len)
Definition: IntronList.cpp:31
#define N_SET_PLUGIN_ESTIMATE
Definition: GUICommands.h:112
#define N_PRECOMPUTE_SUBKERNELS
Definition: GUICommands.h:74
const char * get_command_prefix()
void load_ascii_file(char *fname, bool remap_to_bin=true, EAlphabet ascii_alphabet=DNA, EAlphabet binary_alphabet=RAWDNA)
CSGInterfaceMethod sg_methods[]
Definition: SGInterface.cpp:71
#define N_NEW_SVM
Definition: GUICommands.h:118
#define N_GET_WD_CONSENSUS
Definition: GUICommands.h:51
#define N_SAVE_FEATURES
Definition: GUICommands.h:137
void init_mod_words_array(SGMatrix< int32_t > p_mod_words_array)
Definition: DynProg.cpp:578
#define N_NEW_REGRESSION
Definition: GUICommands.h:121
#define N_BAUM_WELCH_TRAIN
Definition: GUICommands.h:199
#define USAGE_I(method, in)
Definition: SGInterface.cpp:64
#define N_MKL_USE_INTERLEAVED_OPTIMIZATION
Definition: GUICommands.h:194
#define N_APPLY_CONVERTER
Definition: GUICommands.h:235
float64_t model_probability(int32_t dimension=-1)
inline proxy for model probability.
Definition: HMM.h:576
int32_t size() const
Definition: SGVector.h:54
#define N_SAVE_PATH
Definition: GUICommands.h:166
float64_t compute_svm_dual_objective()
Definition: SVM.cpp:242
virtual bool apply_preprocessor(bool force_preprocessing=false)
index_t vlen
Definition: SGVector.h:706
#define SG_PRINT(...)
Definition: SGIO.h:139
#define USAGE_COMMA
Definition: SGInterface.cpp:67
#define N_MKL_BLOCK_NORM
Definition: GUICommands.h:188
bool compute_plif_matrix(SGNDArray< float64_t > penalties_array)
Definition: PlifMatrix.cpp:180
void set_intron_list(CIntronList *intron_list, int32_t num_plifs)
Definition: DynProg.cpp:2534
#define ASSERT(x)
Definition: SGIO.h:203
virtual float64_t get_max_value() const
Definition: Plif.h:339
index_t num_features
total number of features
#define USAGE(method)
Definition: SGInterface.cpp:63
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:102
#define N_COMPUTE_MKL_DUAL_OBJECTIVE
Definition: GUICommands.h:31
int32_t get_use_cache()
Definition: Plif.h:225
bool set_element(T e, int32_t idx1, int32_t idx2=0, int32_t idx3=0)
Definition: DynamicArray.h:304
#define N_MKL_PARAMETERS
Definition: GUICommands.h:186
void set_bias(float64_t bias)
#define N_SVM_CLASSIFY
Definition: GUICommands.h:109
void set_q_vector(SGVector< float64_t > q)
Definition: DynProg.cpp:449
#define N_SIGNALS_SET_POSITIONS
Definition: GUICommands.h:63
#define N_EXIT
Definition: GUICommands.h:212
#define USAGE_O(method, out)
Definition: SGInterface.cpp:65
#define N_SET_CONVERTER
Definition: GUICommands.h:234
#define N_CHOP
Definition: GUICommands.h:173
The CommWordString kernel may be used to compute the spectrum kernel from strings that have been mapp...
#define N_INIT_KERNEL
Definition: GUICommands.h:162
const float64_t * get_cum_derivative(int32_t &p_len) const
Definition: Plif.h:113
#define N_SIGNALS_ADD_KERNEL
Definition: GUICommands.h:68
#define N_GET_KERNEL_MATRIX
Definition: GUICommands.h:35
#define N_TRAIN_CLUSTERING
Definition: GUICommands.h:126
#define N_SET_DOTFEATURE_WEIGHTS_COMBINED
Definition: GUICommands.h:43
#define N_COMPUTE_SVM_PRIMAL_OBJECTIVE
Definition: GUICommands.h:28
void set_plif_penalties(SGMatrix< float64_t > penalties)
Definition: PlifMatrix.cpp:123
void precompute_stop_codons()
Definition: DynProg.cpp:195
static bool unset_handler()
Definition: Signal.cpp:104
#define N_SVM_CLASSIFY_EXAMPLE
Definition: GUICommands.h:110
void set_p(T_STATES offset, float64_t value)
Definition: HMM.h:1005
void set_plif_limits(SGMatrix< float64_t > limits)
Definition: PlifMatrix.cpp:104
double float64_t
Definition: common.h:48
#define N_GET_LAST_SUBKERNEL_WEIGHTS
Definition: GUICommands.h:47
bool set_alpha(int32_t idx, float64_t val)
#define N_GET_LABELS
Definition: GUICommands.h:55
SGMatrix< int32_t > get_positions()
Definition: DynProg.cpp:852
SGVector< float64_t > get_w() const
Definition: LinearKernel.h:119
#define N_USE_SHRINKING
Definition: GUICommands.h:206
#define N_SIGNALS_SET_TRAIN_MASK
Definition: GUICommands.h:66
#define N_THREADS
Definition: GUICommands.h:185
#define N_EXEC
Definition: GUICommands.h:211
#define N_SAVE_KERNEL
Definition: GUICommands.h:141
#define SG_REF(x)
Definition: SGRefObject.h:34
#define N_CONVERT
Definition: GUICommands.h:176
#define N_LOGLEVEL
Definition: GUICommands.h:178
#define N_GET_DOTFEATURE_WEIGHTS_COMBINED
Definition: GUICommands.h:42
#define N_CLEAN_FEATURES
Definition: GUICommands.h:138
#define N_SVM_NU
Definition: GUICommands.h:192
UI math.
Definition: GUIMath.h:22
#define N_RUN_OCTAVE
Definition: GUICommands.h:231
float64_t get_q(T_STATES offset) const
Definition: HMM.h:1090
virtual void set_position_weights(SGVector< float64_t > pws)
#define N_SVM_TRAIN
Definition: GUICommands.h:221
#define N_SET_WD_POS_WEIGHTS
Definition: GUICommands.h:45
float64_t get_alpha(int32_t idx)
SGMatrix< int32_t > get_states()
Definition: DynProg.cpp:842
void compute_by_tree(int32_t idx, float64_t *LevelContrib)
#define N_GET_CLUSTERING
Definition: GUICommands.h:26
#define N_APPEND_HMM
Definition: GUICommands.h:197
The Weighted Degree String kernel.
float64_t * get_degree_weights(int32_t &d, int32_t &len)
#define N_SET_LIN_FEAT
Definition: GUICommands.h:78
void best_path_set_segment_loss(SGMatrix< float64_t > segment_loss)
Definition: DynProg.cpp:797
#define N_GET_KERNEL_OPTIMIZATION
Definition: GUICommands.h:38
#define N_BEST_PATH_2STRUCT
Definition: GUICommands.h:86
void set_content_type_array(SGMatrix< float64_t > seg_path)
Definition: DynProg.cpp:683
int32_t get_use_svm() const
Definition: Plif.h:189
virtual EFeatureClass get_feature_class() const =0
The Combined kernel is used to combine a number of kernels into a single CombinedKernel object by lin...
#define N_SET_LINEAR_CLASSIFIER
Definition: GUICommands.h:90
UI structure.
Definition: GUIStructure.h:28
void set_q(T_STATES offset, float64_t value)
Definition: HMM.h:992
SGSparseVector< T > * sparse_matrix
array of sparse vectors of size num_vectors
void precompute_tiling_plifs(CPlif **PEN, const int32_t *tiling_plif_ids, const int32_t num_tiling_plifs)
Definition: DynProg.cpp:311
bool set_support_vector(int32_t idx, int32_t val)
void set_plif_min_values(SGVector< float64_t > min_values)
Definition: PlifMatrix.cpp:56
float64_t get_q_deriv(T_STATES offset) const
Definition: DynProg.h:376
Class LinearMachine is a generic interface for all kinds of linear machines like classifiers.
Definition: LinearMachine.h:61
Multiple Kernel Learning.
Definition: MKL.h:95
float64_t * get_lin_feat(int32_t &dim1, int32_t &dim2)
Definition: DynProg.h:412
#define N_SVM_EPSILON
Definition: GUICommands.h:189
#define N_BEST_PATH_TRANS_DERIV
Definition: GUICommands.h:70
virtual EMachineType get_classifier_type()
Definition: Machine.cpp:102
#define N_GET_HMM
Definition: GUICommands.h:21
#define N_SET_FEATURE_MATRIX
Definition: GUICommands.h:84
#define N_OUTPUT_HMM
Definition: GUICommands.h:208
#define N_HMM_CLASSIFY
Definition: GUICommands.h:104
#define N_CLEAN_UP_DYN_PROG
Definition: GUICommands.h:80
#define N_CRC
Definition: GUICommands.h:19
int32_t get_support_vector(int32_t idx)
T_STATES * get_path(int32_t dim, float64_t &prob)
Definition: HMM.cpp:4073
const char * get_prompt_suffix()
void set_a_trans_matrix(SGMatrix< float64_t > a_trans)
Definition: DynProg.cpp:477
virtual void set_subkernel_weights(SGVector< float64_t > weights)
Definition: Kernel.cpp:385
#define N_GET_VITERBI_PATH
Definition: GUICommands.h:22
#define N_GET_VERSION
Definition: GUICommands.h:56
float float32_t
Definition: common.h:47
virtual void add_preprocessor(CPreprocessor *p)
Definition: Features.cpp:82
#define N_SET_KERNEL_OPTIMIZATION_TYPE
Definition: GUICommands.h:155
void set_plif_ids(SGVector< int32_t > ids)
Definition: PlifMatrix.cpp:41
bool append_model(CHMM *append_model, float64_t *cur_out, float64_t *app_out)
Definition: HMM.cpp:4954
virtual float64_t get_min_value() const
Definition: Plif.h:358
void set_plif_use_svm(SGVector< int32_t > use_svm)
Definition: PlifMatrix.cpp:92
#define N_GET_PLIF_STRUCT
Definition: GUICommands.h:73
#define N_SYNTAX_HIGHLIGHT
Definition: GUICommands.h:213
UI converter.
Definition: GUIConverter.h:24
#define N_SET_SVM
Definition: GUICommands.h:89
uint8_t T_STATES
Definition: HMM.h:64
#define N_SET_SOLVER
Definition: GUICommands.h:217
void get_dictionary(int32_t &dsize, float64_t *&dweights)
#define N_SET_KERNEL
Definition: GUICommands.h:147
#define N_SET_LABELS
Definition: GUICommands.h:99
int32_t get_num_plifs()
Definition: PlifMatrix.h:72
SGVector< float64_t > get_plif_limits()
Definition: Plif.h:311
float64_t get_p_deriv(T_STATES offset) const
Definition: DynProg.h:396
bool set_position_weights_lhs(float64_t *pws, int32_t len, int32_t num)
#define N_CLASSIFY_EXAMPLE
Definition: GUICommands.h:108
#define SG_DEBUG(...)
Definition: SGIO.h:109
float64_t get_a(T_STATES line_, T_STATES column) const
Definition: HMM.h:1131
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:16
SGVector< float64_t > get_plif_penalties()
Definition: Plif.h:320
#define N_DELETE_KERNEL_OPTIMIZATION
Definition: GUICommands.h:164
#define N_PERMUTATION_ENTROPY
Definition: GUICommands.h:146
#define N_PSEUDO
Definition: GUICommands.h:175
#define N_PRECOMPUTE_CONTENT_SVMS
Definition: GUICommands.h:75
void disable_syntax_hilighting()
void set_prior_probs(float64_t pos_prior_, float64_t neg_prior_)
#define N_GET_SVM
Definition: GUICommands.h:24
#define N_COMPUTE_SVM_DUAL_OBJECTIVE
Definition: GUICommands.h:29
virtual EKernelType get_kernel_type()=0
#define N_LONG_TRANSITION_SETTINGS
Definition: GUICommands.h:82
float64_t get_p(T_STATES offset) const
Definition: HMM.h:1103
void set_plif_max_values(SGVector< float64_t > max_values)
Definition: PlifMatrix.cpp:68
int32_t get_num_states()
Definition: DynProg.cpp:233
#define N_ATTACH_PREPROC
Definition: GUICommands.h:158
#define N_LOAD_DEFINITIONS
Definition: GUICommands.h:140
#define N_SET_REF_FEAT
Definition: GUICommands.h:226
The class Features is the base class of all feature objects.
Definition: Features.h:62
#define N_NEW_PLUGIN_ESTIMATOR
Definition: GUICommands.h:120
#define SG_SERROR(...)
Definition: SGIO.h:181
#define N_ADD_KERNEL
Definition: GUICommands.h:149
#define N_TRAIN_ESTIMATOR
Definition: GUICommands.h:123
#define N_GET_SUBKERNEL_WEIGHTS
Definition: GUICommands.h:46
#define N_NORMALIZE
Definition: GUICommands.h:143
#define N_GET_SPEC_SCORING
Definition: GUICommands.h:52
Class Preprocessor defines a preprocessor interface.
Definition: Preprocessor.h:69
#define N_SVR_TUBE_EPSILON
Definition: GUICommands.h:191
#define SG_DEPRECATED
Definition: SGIO.h:142
Dynamic Programming Class.
Definition: DynProg.h:76
#define N_INIT_DISTANCE
Definition: GUICommands.h:163
#define N_ONE_CLASS_HMM_CLASSIFY_EXAMPLE
Definition: GUICommands.h:105
#define N_BEST_PATH_TRANS
Definition: GUICommands.h:71
Features that allow stacking of a number of DotFeatures.
#define N_USE_DIAGONAL_SPEEDUP
Definition: GUICommands.h:204
#define N_SET_FEATURES
Definition: GUICommands.h:94
index_t slen
Definition: SGString.h:79
virtual int32_t get_num_subkernels()
Definition: Kernel.cpp:361
void set_p_vector(SGVector< float64_t > p)
Definition: DynProg.cpp:441
#define N_ONE_CLASS_HMM_CLASSIFY
Definition: GUICommands.h:102
A generic Support Vector Machine Interface.
Definition: SVM.h:47
UI features.
Definition: GUIFeatures.h:36
bool get_do_calc()
Definition: Plif.cpp:398
void set_plif_names(SGString< char > *names, int32_t num_values, int32_t maxlen=0)
Definition: PlifMatrix.cpp:143
void get_path_losses(float64_t **my_losses, int32_t *seq_len)
Definition: DynProg.cpp:876
#define N_SVM_USE_BIAS
Definition: GUICommands.h:193
const T & element(int32_t idx1, int32_t idx2=0, int32_t idx3=0) const
Definition: DynamicArray.h:222
void set_b(T_STATES line_, uint16_t column, float64_t value)
Definition: HMM.h:1061
#define N_COMPUTE_POIM_WD
Definition: GUICommands.h:49
The Kernel base class.
Definition: Kernel.h:150
Binary Labels for binary classification.
Definition: BinaryLabels.h:36
void init_tiling_data(int32_t *probe_pos, float64_t *intensities, const int32_t num_probes)
Definition: DynProg.cpp:238
#define N_LOAD_LABELS
Definition: GUICommands.h:135
bool compute_signal_plifs(SGMatrix< int32_t > state_signals)
Definition: PlifMatrix.cpp:243
#define N_RUN_PYTHON
Definition: GUICommands.h:230
bool set_position_weights_rhs(float64_t *pws, int32_t len, int32_t num)
virtual void set_bias(float64_t b)
#define N_SAVE_LIKELIHOOD
Definition: GUICommands.h:167
#define N_DEL_LAST_FEATURES
Definition: GUICommands.h:98
SGMatrix< float64_t > get_distance_matrix()
Definition: Distance.h:126
#define N_PLUGIN_ESTIMATE_CLASSIFY
Definition: GUICommands.h:113
#define N_CLEAN_PREPROC
Definition: GUICommands.h:161
#define N_SET_THRESHOLD
Definition: GUICommands.h:219
#define N_SVMMAXQPSIZE
Definition: GUICommands.h:184
void set_kernel(CKernel *k)
implement DotFeatures for the polynomial kernel
Definition: PolyFeatures.h:25
#define N_BAUM_WELCH_TRANS_TRAIN
Definition: GUICommands.h:200
#define N_SET_HMM_AS
Definition: GUICommands.h:142
CPlif ** get_PEN()
Definition: PlifMatrix.h:46
#define N_USE_LINADD
Definition: GUICommands.h:207
#define SG_WARNING(...)
Definition: SGIO.h:130
The Weighted Degree Position String kernel (Weighted Degree kernel with shifts).
const char * get_prompt_prefix()
#define N_PRECOMPUTE_TILING_FEATURES
Definition: GUICommands.h:81
#define USAGE_STR
Definition: SGInterface.cpp:68
#define N_BAUM_WELCH_TRAIN_DEFINED
Definition: GUICommands.h:201
#define N_SET_PRIOR_PROBS
Definition: GUICommands.h:156
Preprocessor SortWordString, sorts the indivual strings in ascending order.
int32_t get_id() const
Definition: Plif.h:161
UI distance.
Definition: GUIDistance.h:25
#define N_SET_LAST_SUBKERNEL_WEIGHTS
Definition: GUICommands.h:44
float64_t * extract_w(int32_t max_degree, int32_t &num_feat, int32_t &num_sym, float64_t *w_result, int32_t num_suppvec, int32_t *IDX, float64_t *alphas)
void set_num_states(int32_t N)
Definition: DynProg.cpp:218
bool has_property(EFeatureProperty p) const
Definition: Features.cpp:292
void read_introns(int32_t *start_pos, int32_t *end_pos, int32_t *quality, int32_t len)
Definition: IntronList.cpp:51
void long_transition_settings(bool use_long_transitions, int32_t threshold, int32_t max_len)
Definition: DynProg.h:480
virtual bool has_features()
Definition: Kernel.h:373
#define N_ADD_PREPROC
Definition: GUICommands.h:159
CSegmentLoss * get_segment_loss_object()
Definition: DynProg.h:469
bool obtain_from_char_features(CStringFeatures< CT > *sf, int32_t start, int32_t p_order, int32_t gap, bool rev)
void set_a(T_STATES line_, T_STATES column, float64_t value)
Definition: HMM.h:1033
#define N_DEL_PREPROC
Definition: GUICommands.h:160
the syntax highlight
index_t num_vectors
total number of vectors
#define N_SET_SUBKERNEL_WEIGHTS
Definition: GUICommands.h:40
#define N_SVMQPSIZE
Definition: GUICommands.h:182
Hidden Markov Model.
Definition: HMM.h:371
bool set_weights(SGMatrix< float64_t > new_weights)
#define N_ENTROPY
Definition: GUICommands.h:145
static void delete_penalty_struct(CPlif **PEN, int32_t P)
Definition: Plif.cpp:115
void set_segment_loss(float64_t *segment_loss, int32_t m, int32_t n)
Definition: SegmentLoss.cpp:26
float64_t get_objective()
Definition: SVM.h:216
void init_content_svm_value_array(const int32_t p_num_svms)
Definition: DynProg.cpp:268
void compute_nbest_paths(int32_t max_num_signals, bool use_orf, int16_t nbest, bool with_loss, bool with_multiple_sequences)
Definition: DynProg.cpp:943
T_STATES get_N() const
access function for number of states N
Definition: HMM.h:983
virtual EFeatureType get_feature_type() const =0
void create_plifs(int32_t num_plifs, int32_t num_limits)
Definition: PlifMatrix.cpp:27
#define N_SVMBUFSIZE
Definition: GUICommands.h:183
store plif arrays for all transitions in the model
Definition: PlifMatrix.h:29
#define N_SIGNALS_SET_LABELS
Definition: GUICommands.h:64
bool create_new_model(int32_t num)
#define N_SET_KERNEL_NORMALIZATION
Definition: GUICommands.h:93
#define N_SET_DISTANCE
Definition: GUICommands.h:148
#define N_WHOS
Definition: GUICommands.h:215
#define N_GET_NUM_SVMS
Definition: GUICommands.h:23
void set_sparse_features(CSparseFeatures< float64_t > *seq_sparse1, CSparseFeatures< float64_t > *seq_sparse2)
Definition: DynProg.cpp:731
CFeatures * get_lhs()
Definition: Kernel.h:343
the class CMap, a map based on the hash-table. w: http://en.wikipedia.org/wiki/Hash_table ...
Definition: SGObject.h:40
#define N_PRINT
Definition: GUICommands.h:180
#define N_APPEND_MODEL
Definition: GUICommands.h:198
#define N_ADD_FEATURES
Definition: GUICommands.h:95

SHOGUN Machine Learning Toolbox - Documentation