SHOGUN  v1.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
SGInterface.cpp
Go to the documentation of this file.
00001 #include <shogun/ui/GUICommands.h>
00002 #include <shogun/ui/SGInterface.h>
00003 #include <shogun/ui/SyntaxHighLight.h>
00004 
00005 #include <shogun/lib/config.h>
00006 #include <shogun/lib/memory.h>
00007 #include <shogun/lib/ShogunException.h>
00008 #include <shogun/mathematics/Math.h>
00009 #include <shogun/lib/Hash.h>
00010 #include <shogun/lib/Set.h>
00011 #include <shogun/lib/Signal.h>
00012 
00013 #include <shogun/classifier/svm/SVM.h>
00014 #include <shogun/classifier/svm/pr_loqo.h>
00015 #include <shogun/machine/LinearMachine.h>
00016 #include <shogun/classifier/mkl/MKL.h>
00017 #include <shogun/kernel/WeightedDegreePositionStringKernel.h>
00018 #include <shogun/kernel/WeightedDegreeStringKernel.h>
00019 #include <shogun/kernel/CommWordStringKernel.h>
00020 #include <shogun/kernel/WeightedCommWordStringKernel.h>
00021 #include <shogun/kernel/LinearKernel.h>
00022 #include <shogun/kernel/CombinedKernel.h>
00023 #include <shogun/kernel/CustomKernel.h>
00024 #include <shogun/kernel/SalzbergWordStringKernel.h>
00025 #include <shogun/kernel/WaveletKernel.h>
00026 #include <shogun/features/SimpleFeatures.h>
00027 #include <shogun/features/PolyFeatures.h>
00028 #include <shogun/preprocessor/SortWordString.h>
00029 
00030 #include <shogun/structure/Plif.h>
00031 #include <shogun/structure/PlifArray.h>
00032 #include <shogun/structure/PlifBase.h>
00033 #include <shogun/structure/DynProg.h>
00034 #include <shogun/structure/IntronList.h>
00035 #include <shogun/structure/SegmentLoss.h>
00036 
00037 #include <ctype.h>
00038 
00039 using namespace shogun;
00040 
00041 CSGInterface* interface=NULL;
00042 CSyntaxHighLight hilight;
00043 
00044 #if defined(HAVE_CMDLINE)
00045 #define USAGE(method) "", ""
00046 #define USAGE_I(method, in) "", " " in ""
00047 #define USAGE_O(method, out) "" out " = ", ""
00048 #define USAGE_IO(method, in, out) "" out " = ", " " in ""
00049 #define USAGE_COMMA " "
00050 #define USAGE_STR ""
00051 #elif defined(HAVE_R)
00052 #define USAGE(method) "sg('", "')"
00053 #define USAGE_I(method, in) "sg('", "', " in ")"
00054 #define USAGE_O(method, out) "[" out "] <- sg('", "')"
00055 #define USAGE_IO(method, in, out) "[" out "] <- sg('", "', " in ")"
00056 #define USAGE_COMMA ", "
00057 #define USAGE_STR "'"
00058 #else
00059 #define USAGE(method) "sg('", "')"
00060 #define USAGE_I(method, in) "sg('", "', " in ")"
00061 #define USAGE_O(method, out) "[" out "]=sg('", "')"
00062 #define USAGE_IO(method, in, out) "[" out "]=sg('", "', " in ")"
00063 #define USAGE_COMMA ", "
00064 #define USAGE_STR "'"
00065 #endif
00066 
00067 CSGInterfaceMethod sg_methods[]=
00068 {
00069     { "Features", NULL, NULL, NULL },
00070     {
00071         N_PR_LOQO,
00072         (&CSGInterface::cmd_pr_loqo),
00073         USAGE_IO(N_PR_LOQO,
00074             "'Var1', Var1, 'Var2', Var2", "results")
00075     },
00076     {
00077         N_LOAD_FEATURES,
00078         (&CSGInterface::cmd_load_features),
00079         USAGE_I(N_LOAD_FEATURES,
00080             "filename" USAGE_COMMA "feature_class" USAGE_COMMA "type" USAGE_COMMA "target[" USAGE_COMMA "size[" USAGE_COMMA "comp_features]]")
00081     },
00082     {
00083         N_SAVE_FEATURES,
00084         (&CSGInterface::cmd_save_features),
00085         USAGE_I(N_SAVE_FEATURES, "filename" USAGE_COMMA "type" USAGE_COMMA "target")
00086     },
00087     {
00088         N_CLEAN_FEATURES,
00089         (&CSGInterface::cmd_clean_features),
00090         USAGE_I(N_CLEAN_FEATURES, USAGE_STR "TRAIN|TEST" USAGE_STR )
00091     },
00092     {
00093         N_GET_FEATURES,
00094         (&CSGInterface::cmd_get_features),
00095         USAGE_IO(N_GET_FEATURES, USAGE_STR "TRAIN|TEST" USAGE_STR, "features")
00096     },
00097     {
00098         N_ADD_FEATURES,
00099         (&CSGInterface::cmd_add_features),
00100         USAGE_I(N_ADD_FEATURES,
00101             USAGE_STR "TRAIN|TEST" USAGE_STR USAGE_COMMA "features[" USAGE_COMMA "DNABINFILE|<ALPHABET>]")
00102     },
00103     {
00104         N_ADD_MULTIPLE_FEATURES,
00105         (&CSGInterface::cmd_add_multiple_features),
00106         USAGE_I(N_ADD_MULTIPLE_FEATURES,
00107             USAGE_STR "TRAIN|TEST" USAGE_STR USAGE_COMMA "repetitions" USAGE_COMMA "features[" USAGE_COMMA "DNABINFILE|<ALPHABET>]")
00108     },
00109     {
00110         N_ADD_DOTFEATURES,
00111         (&CSGInterface::cmd_add_dotfeatures),
00112         USAGE_I(N_ADD_DOTFEATURES,
00113             USAGE_STR "TRAIN|TEST" USAGE_STR USAGE_COMMA "features[" USAGE_COMMA "DNABINFILE|<ALPHABET>]")
00114     },
00115     {
00116         N_SET_FEATURES,
00117         (&CSGInterface::cmd_set_features),
00118         USAGE_I(N_SET_FEATURES,
00119             USAGE_STR "TRAIN|TEST" USAGE_STR
00120             USAGE_COMMA "features["
00121             USAGE_COMMA "DNABINFILE|<ALPHABET>]["
00122             USAGE_COMMA "[from_position_list|slide_window]"
00123             USAGE_COMMA "window size"
00124             USAGE_COMMA "[position_list|shift]"
00125             USAGE_COMMA "skip")
00126     },
00127     {
00128         N_SET_REF_FEAT,
00129         (&CSGInterface::cmd_set_reference_features),
00130         USAGE_I(N_SET_REF_FEAT, USAGE_STR "TRAIN|TEST" USAGE_STR)
00131     },
00132     {
00133         N_DEL_LAST_FEATURES,
00134         (&CSGInterface::cmd_del_last_features),
00135         USAGE_I(N_DEL_LAST_FEATURES, USAGE_STR "TRAIN|TEST" USAGE_STR )
00136     },
00137     {
00138         N_CONVERT,
00139         (&CSGInterface::cmd_convert),
00140         USAGE_I(N_CONVERT, USAGE_STR "TRAIN|TEST" USAGE_STR 
00141                 USAGE_COMMA "from_class"
00142                 USAGE_COMMA "from_type"
00143                 USAGE_COMMA "to_class"
00144                 USAGE_COMMA "to_type["
00145                 USAGE_COMMA "order"
00146                 USAGE_COMMA "start"
00147                 USAGE_COMMA "gap"
00148                 USAGE_COMMA "reversed]")
00149     },
00150     {
00151         N_RESHAPE,
00152         (&CSGInterface::cmd_reshape),
00153         USAGE_I(N_RESHAPE, USAGE_STR "TRAIN|TEST"
00154                 USAGE_COMMA "num_feat"
00155                 USAGE_COMMA "num_vec")
00156     },
00157     {
00158         N_LOAD_LABELS,
00159         (&CSGInterface::cmd_load_labels),
00160         USAGE_I(N_LOAD_LABELS, "filename"
00161                 USAGE_COMMA USAGE_STR "TRAIN|TARGET" USAGE_STR)
00162     },
00163     {
00164         N_SET_LABELS,
00165         (&CSGInterface::cmd_set_labels),
00166         USAGE_I(N_SET_LABELS, USAGE_STR "TRAIN|TEST" USAGE_STR
00167                 USAGE_COMMA "labels")
00168     },
00169     {
00170         N_GET_LABELS,
00171         (&CSGInterface::cmd_get_labels),
00172         USAGE_IO(N_GET_LABELS, USAGE_STR "TRAIN|TEST" USAGE_STR, "labels")
00173     },
00174 
00175 
00176     { "Kernel", NULL, NULL },
00177     {
00178         N_SET_KERNEL_NORMALIZATION,
00179         (&CSGInterface::cmd_set_kernel_normalization),
00180         USAGE_I(N_SET_KERNEL_NORMALIZATION, "IDENTITY|AVGDIAG|SQRTDIAG|FIRSTELEMENT|VARIANCE|ZEROMEANCENTER"
00181                 USAGE_COMMA "size[" USAGE_COMMA "kernel-specific parameters]")
00182     },
00183     {
00184         N_SET_KERNEL,
00185         (&CSGInterface::cmd_set_kernel),
00186         USAGE_I(N_SET_KERNEL, "type" USAGE_COMMA "size[" USAGE_COMMA "kernel-specific parameters]")
00187     },
00188     {
00189         N_ADD_KERNEL,
00190         (&CSGInterface::cmd_add_kernel),
00191         USAGE_I(N_ADD_KERNEL, "weight" USAGE_COMMA "kernel-specific parameters")
00192     },
00193     {
00194         N_DEL_LAST_KERNEL,
00195         (&CSGInterface::cmd_del_last_kernel),
00196         USAGE(N_DEL_LAST_KERNEL)
00197     },
00198     {
00199         N_INIT_KERNEL,
00200         (&CSGInterface::cmd_init_kernel),
00201         USAGE_I(N_INIT_KERNEL, USAGE_STR "TRAIN|TEST" USAGE_STR)
00202     },
00203     {
00204         N_CLEAN_KERNEL,
00205         (&CSGInterface::cmd_clean_kernel),
00206         USAGE(N_CLEAN_KERNEL)
00207     },
00208     {
00209         N_SAVE_KERNEL,
00210         (&CSGInterface::cmd_save_kernel),
00211         USAGE_I(N_SAVE_KERNEL, "filename" USAGE_COMMA USAGE_STR "TRAIN|TEST" USAGE_STR)
00212     },
00213     {
00214         N_GET_KERNEL_MATRIX,
00215         (&CSGInterface::cmd_get_kernel_matrix),
00216         USAGE_IO(N_GET_KERNEL_MATRIX, "[" USAGE_STR "TRAIN|TEST" USAGE_STR, "K]")
00217     },
00218     {
00219         N_SET_WD_POS_WEIGHTS,
00220         (&CSGInterface::cmd_set_WD_position_weights),
00221         USAGE_I(N_SET_WD_POS_WEIGHTS, "W[" USAGE_COMMA USAGE_STR "TRAIN|TEST" USAGE_STR "]")
00222     },
00223     {
00224         N_GET_SUBKERNEL_WEIGHTS,
00225         (&CSGInterface::cmd_get_subkernel_weights),
00226         USAGE_O(N_GET_SUBKERNEL_WEIGHTS, "W")
00227     },
00228     {
00229         N_SET_SUBKERNEL_WEIGHTS,
00230         (&CSGInterface::cmd_set_subkernel_weights),
00231         USAGE_I(N_SET_SUBKERNEL_WEIGHTS, "W")
00232     },
00233     {
00234         N_SET_SUBKERNEL_WEIGHTS_COMBINED,
00235         (&CSGInterface::cmd_set_subkernel_weights_combined),
00236         USAGE_I(N_SET_SUBKERNEL_WEIGHTS_COMBINED, "W" USAGE_COMMA "idx")
00237     },
00238     {
00239         N_GET_DOTFEATURE_WEIGHTS_COMBINED,
00240         (&CSGInterface::cmd_get_dotfeature_weights_combined),
00241         USAGE_IO(N_GET_DOTFEATURE_WEIGHTS_COMBINED,  USAGE_STR "TRAIN|TEST" USAGE_STR, "W")
00242     },
00243     {
00244         N_SET_DOTFEATURE_WEIGHTS_COMBINED,
00245         (&CSGInterface::cmd_set_dotfeature_weights_combined),
00246         USAGE_I(N_SET_DOTFEATURE_WEIGHTS_COMBINED, "W" USAGE_COMMA "idx")
00247     },
00248     {
00249         N_SET_LAST_SUBKERNEL_WEIGHTS,
00250         (&CSGInterface::cmd_set_last_subkernel_weights),
00251         USAGE_I(N_SET_LAST_SUBKERNEL_WEIGHTS, "W")
00252     },
00253     {
00254         N_GET_WD_POS_WEIGHTS,
00255         (&CSGInterface::cmd_get_WD_position_weights),
00256         USAGE_O(N_GET_WD_POS_WEIGHTS, "W")
00257     },
00258     {
00259         N_GET_LAST_SUBKERNEL_WEIGHTS,
00260         (&CSGInterface::cmd_get_last_subkernel_weights),
00261         USAGE_O(N_GET_LAST_SUBKERNEL_WEIGHTS, "W")
00262     },
00263     {
00264         N_COMPUTE_BY_SUBKERNELS,
00265         (&CSGInterface::cmd_compute_by_subkernels),
00266         USAGE_O(N_COMPUTE_BY_SUBKERNELS, "W")
00267     },
00268     {
00269         N_INIT_KERNEL_OPTIMIZATION,
00270         (&CSGInterface::cmd_init_kernel_optimization),
00271         USAGE(N_INIT_KERNEL_OPTIMIZATION)
00272     },
00273     {
00274         N_GET_KERNEL_OPTIMIZATION,
00275         (&CSGInterface::cmd_get_kernel_optimization),
00276         USAGE_O(N_GET_KERNEL_OPTIMIZATION, "W")
00277     },
00278     {
00279         N_DELETE_KERNEL_OPTIMIZATION,
00280         (&CSGInterface::cmd_delete_kernel_optimization),
00281         USAGE(N_DELETE_KERNEL_OPTIMIZATION)
00282     },
00283     {
00284         N_USE_DIAGONAL_SPEEDUP,
00285         (&CSGInterface::cmd_use_diagonal_speedup),
00286         USAGE_I(N_USE_DIAGONAL_SPEEDUP, USAGE_STR "0|1" USAGE_STR)
00287     },
00288     {
00289         N_SET_KERNEL_OPTIMIZATION_TYPE,
00290         (&CSGInterface::cmd_set_kernel_optimization_type),
00291         USAGE_I(N_SET_KERNEL_OPTIMIZATION_TYPE, USAGE_STR "FASTBUTMEMHUNGRY|SLOWBUTMEMEFFICIENT" USAGE_STR)
00292     },
00293     {
00294         N_SET_SOLVER,
00295         (&CSGInterface::cmd_set_solver),
00296         USAGE_I(N_SET_SOLVER, USAGE_STR "AUTO|CPLEX|GLPK|INTERNAL" USAGE_STR)
00297     },
00298     {
00299         N_SET_CONSTRAINT_GENERATOR,
00300         (&CSGInterface::cmd_set_constraint_generator),
00301         USAGE_I(N_SET_CONSTRAINT_GENERATOR, USAGE_STR "LIBSVM_ONECLASS|LIBSVM_MULTICLASS|LIBSVM"
00302                 "|SVMLIGHT|LIGHT|SVMLIGHT_ONECLASS|GPBTSVM|MPDSVM|GNPPSVM|GMNPSVM"
00303                 USAGE_STR)
00304     },
00305     {
00306         N_SET_PRIOR_PROBS,
00307         (&CSGInterface::cmd_set_prior_probs),
00308         USAGE_I(N_SET_PRIOR_PROBS, USAGE_STR "pos probs, neg_probs" USAGE_STR)
00309     },
00310     {
00311         N_SET_PRIOR_PROBS_FROM_LABELS,
00312         (&CSGInterface::cmd_set_prior_probs_from_labels),
00313         USAGE_I(N_SET_PRIOR_PROBS_FROM_LABELS, USAGE_STR "labels" USAGE_STR)
00314     },
00315 
00316 
00317 
00318     { "Distance", NULL, NULL },
00319     {
00320         N_SET_DISTANCE,
00321         (&CSGInterface::cmd_set_distance),
00322         USAGE_I(N_SET_DISTANCE, "type" USAGE_COMMA "data type[" USAGE_COMMA "distance-specific parameters]")
00323     },
00324     {
00325         N_INIT_DISTANCE,
00326         (&CSGInterface::cmd_init_distance),
00327         USAGE_I(N_INIT_DISTANCE, USAGE_STR "TRAIN|TEST" USAGE_STR)
00328     },
00329     {
00330         N_GET_DISTANCE_MATRIX,
00331         (&CSGInterface::cmd_get_distance_matrix),
00332         USAGE_O(N_GET_DISTANCE_MATRIX, "D")
00333     },
00334 
00335 
00336     { "Classifier", NULL, NULL },
00337     {
00338         N_CLASSIFY,
00339         (&CSGInterface::cmd_classify),
00340         USAGE_O(N_CLASSIFY, "result")
00341     },
00342     {
00343         N_SVM_CLASSIFY,
00344         (&CSGInterface::cmd_classify),
00345         USAGE_O(N_SVM_CLASSIFY, "result")
00346     },
00347     {
00348         N_CLASSIFY_EXAMPLE,
00349         (&CSGInterface::cmd_classify_example),
00350         USAGE_IO(N_CLASSIFY_EXAMPLE, "feature_vector_index", "result")
00351     },
00352     {
00353         N_SVM_CLASSIFY_EXAMPLE,
00354         (&CSGInterface::cmd_classify_example),
00355         USAGE_IO(N_SVM_CLASSIFY_EXAMPLE, "feature_vector_index", "result")
00356     },
00357     {
00358         N_GET_CLASSIFIER,
00359         (&CSGInterface::cmd_get_classifier),
00360         USAGE_IO(N_GET_CLASSIFIER, "[index in case of MultiClassSVM]", "bias" USAGE_COMMA "weights")
00361     },
00362     {
00363         N_GET_CLUSTERING,
00364         (&CSGInterface::cmd_get_classifier),
00365         USAGE_O(N_GET_CLUSTERING, "radi" USAGE_COMMA "centers|merge_distances" USAGE_COMMA "pairs")
00366     },
00367     {
00368         N_NEW_SVM,
00369         (&CSGInterface::cmd_new_classifier),
00370         USAGE_I(N_NEW_SVM, USAGE_STR "LIBSVM_ONECLASS|LIBSVM_MULTICLASS|LIBSVM"
00371                 "|SVMLIGHT|LIGHT|LIGHT_ONECLASS|SVMLIN|GPBTSVM|MPDSVM|GNPPSVM|GMNPSVM"
00372                 "|SUBGRADIENTSVM|WDSVMOCAS|SVMOCAS|SVMSGD|SVMBMRM|SVMPERF"
00373                 "|KERNELPERCEPTRON|PERCEPTRON|LIBLINEAR_LR|LIBLINEAR_L2|LDA"
00374                 "|LPM|LPBOOST|SUBGRADIENTLPM|KNN" USAGE_STR)
00375     },
00376     {
00377         N_NEW_CLASSIFIER,
00378         (&CSGInterface::cmd_new_classifier),
00379         USAGE_I(N_NEW_CLASSIFIER, USAGE_STR "LIBSVM_ONECLASS|LIBSVM_MULTICLASS"
00380                 "|LIBSVM|SVMLIGHT|LIGHT|LIGHT_ONECLASS|SVMLIN|GPBTSVM|MPDSVM|GNPPSVM|GMNPSVM"
00381                 "|SUBGRADIENTSVM|WDSVMOCAS|SVMOCAS|SVMSGD|SVMBMRM|SVMPERF"
00382                 "|KERNELPERCEPTRON|PERCEPTRON|LIBLINEAR_LR|LIBLINEAR_L2|LDA"
00383                 "|LPM|LPBOOST|SUBGRADIENTLPM|KNN" USAGE_STR)
00384     },
00385     {
00386         N_NEW_REGRESSION,
00387         (&CSGInterface::cmd_new_classifier),
00388         USAGE_I(N_NEW_REGRESSION, USAGE_STR "SVRLIGHT|LIBSVR|KRR" USAGE_STR)
00389     },
00390     {
00391         N_NEW_CLUSTERING,
00392         (&CSGInterface::cmd_new_classifier),
00393         USAGE_I(N_NEW_CLUSTERING, USAGE_STR "KMEANS|HIERARCHICAL" USAGE_STR)
00394     },
00395     {
00396         N_LOAD_CLASSIFIER,
00397         (&CSGInterface::cmd_load_classifier),
00398         USAGE_O(N_LOAD_CLASSIFIER, "filename" USAGE_COMMA "type")
00399     },
00400     {
00401         N_SAVE_CLASSIFIER,
00402         (&CSGInterface::cmd_save_classifier),
00403         USAGE_I(N_SAVE_CLASSIFIER, "filename")
00404     },
00405     {
00406         N_GET_NUM_SVMS,
00407         (&CSGInterface::cmd_get_num_svms),
00408         USAGE_O(N_GET_NUM_SVMS, "number of SVMs in MultiClassSVM")
00409     },
00410     {
00411         N_GET_SVM,
00412         (&CSGInterface::cmd_get_svm),
00413         USAGE_IO(N_GET_SVM, "[index in case of MultiClassSVM]", "bias" USAGE_COMMA "alphas")
00414     },
00415     {
00416         N_SET_SVM,
00417         (&CSGInterface::cmd_set_svm),
00418         USAGE_I(N_SET_SVM, "bias" USAGE_COMMA "alphas")
00419     },
00420     {
00421         N_SET_LINEAR_CLASSIFIER,
00422         (&CSGInterface::cmd_set_linear_classifier),
00423         USAGE_I(N_SET_LINEAR_CLASSIFIER, "bias" USAGE_COMMA "w")
00424     },
00425     {
00426         N_GET_SVM_OBJECTIVE,
00427         (&CSGInterface::cmd_get_svm_objective),
00428         USAGE_O(N_GET_SVM_OBJECTIVE, "objective")
00429     },
00430     {
00431         N_COMPUTE_SVM_PRIMAL_OBJECTIVE,
00432         (&CSGInterface::cmd_compute_svm_primal_objective),
00433         USAGE_O(N_COMPUTE_SVM_PRIMAL_OBJECTIVE, "objective")
00434     },
00435     {
00436         N_COMPUTE_SVM_DUAL_OBJECTIVE,
00437         (&CSGInterface::cmd_compute_svm_dual_objective),
00438         USAGE_O(N_COMPUTE_SVM_DUAL_OBJECTIVE, "objective")
00439     },
00440     {
00441         N_COMPUTE_MKL_PRIMAL_OBJECTIVE,
00442         (&CSGInterface::cmd_compute_svm_primal_objective),
00443         USAGE_O(N_COMPUTE_MKL_PRIMAL_OBJECTIVE, "objective")
00444     },
00445     {
00446         N_COMPUTE_MKL_DUAL_OBJECTIVE,
00447         (&CSGInterface::cmd_compute_mkl_dual_objective),
00448         USAGE_O(N_COMPUTE_MKL_DUAL_OBJECTIVE, "objective")
00449     },
00450     {
00451         N_COMPUTE_RELATIVE_MKL_DUALITY_GAP,
00452         (&CSGInterface::cmd_compute_relative_mkl_duality_gap),
00453         USAGE_O(N_COMPUTE_RELATIVE_MKL_DUALITY_GAP, "gap")
00454     },
00455     {
00456         N_COMPUTE_ABSOLUTE_MKL_DUALITY_GAP,
00457         (&CSGInterface::cmd_compute_absolute_mkl_duality_gap),
00458         USAGE_O(N_COMPUTE_ABSOLUTE_MKL_DUALITY_GAP, "gap")
00459     },
00460     {
00461         N_DO_AUC_MAXIMIZATION,
00462         (&CSGInterface::cmd_do_auc_maximization),
00463         USAGE_I(N_DO_AUC_MAXIMIZATION, USAGE_STR "auc" USAGE_STR)
00464     },
00465     {
00466         N_SET_PERCEPTRON_PARAMETERS,
00467         (&CSGInterface::cmd_set_perceptron_parameters),
00468         USAGE_I(N_SET_PERCEPTRON_PARAMETERS, "learnrate" USAGE_COMMA "maxiter")
00469     },
00470     {
00471         N_TRAIN_CLASSIFIER,
00472         (&CSGInterface::cmd_train_classifier),
00473         USAGE_I(N_TRAIN_CLASSIFIER, "[classifier-specific parameters]")
00474     },
00475     {
00476         N_TRAIN_REGRESSION,
00477         (&CSGInterface::cmd_train_classifier),
00478         USAGE(N_TRAIN_REGRESSION)
00479     },
00480     {
00481         N_TRAIN_CLUSTERING,
00482         (&CSGInterface::cmd_train_classifier),
00483         USAGE(N_TRAIN_CLUSTERING)
00484     },
00485     {
00486         N_SVM_TRAIN,
00487         (&CSGInterface::cmd_train_classifier),
00488         USAGE_I(N_SVM_TRAIN, "[classifier-specific parameters]")
00489     },
00490     {
00491         N_SVMQPSIZE,
00492         (&CSGInterface::cmd_set_svm_qpsize),
00493         USAGE_I(N_SVMQPSIZE, "size")
00494     },
00495     {
00496         N_SVMMAXQPSIZE,
00497         (&CSGInterface::cmd_set_svm_max_qpsize),
00498         USAGE_I(N_SVMMAXQPSIZE, "size")
00499     },
00500     {
00501         N_SVMBUFSIZE,
00502         (&CSGInterface::cmd_set_svm_bufsize),
00503         USAGE_I(N_SVMBUFSIZE, "size")
00504     },
00505     {
00506         N_C,
00507         (&CSGInterface::cmd_set_svm_C),
00508         USAGE_I(N_C, "C1[" USAGE_COMMA "C2]")
00509     },
00510     {
00511         N_SVM_EPSILON,
00512         (&CSGInterface::cmd_set_svm_epsilon),
00513         USAGE_I(N_SVM_EPSILON, "epsilon")
00514     },
00515     {
00516         N_SVR_TUBE_EPSILON,
00517         (&CSGInterface::cmd_set_svr_tube_epsilon),
00518         USAGE_I(N_SVR_TUBE_EPSILON, "tube_epsilon")
00519     },
00520     {
00521         N_SVM_NU,
00522         (&CSGInterface::cmd_set_svm_nu),
00523         USAGE_I(N_SVM_NU, "nu")
00524     },
00525     {
00526         N_MKL_PARAMETERS,
00527         (&CSGInterface::cmd_set_svm_mkl_parameters),
00528         USAGE_I(N_MKL_PARAMETERS, "weight_epsilon" USAGE_COMMA "C_MKL [" USAGE_COMMA "mkl_norm ]")
00529     },
00530     {
00531       N_ENT_LAMBDA,
00532       (&CSGInterface::cmd_set_elasticnet_lambda),
00533       USAGE_I(N_ENT_LAMBDA, "ent_lambda")
00534     },
00535     {
00536       N_MKL_BLOCK_NORM,
00537       (&CSGInterface::cmd_set_mkl_block_norm),
00538       USAGE_I(N_MKL_BLOCK_NORM, "mkl_block_norm")
00539     },
00540     {
00541         N_SVM_MAX_TRAIN_TIME,
00542         (&CSGInterface::cmd_set_max_train_time),
00543         USAGE_I(N_SVM_MAX_TRAIN_TIME, "max_train_time")
00544     },
00545     {
00546         N_USE_SHRINKING,
00547         (&CSGInterface::cmd_set_svm_shrinking_enabled),
00548         USAGE_I(N_USE_SHRINKING, "enable_shrinking")
00549     },
00550     {
00551         N_USE_BATCH_COMPUTATION,
00552         (&CSGInterface::cmd_set_svm_batch_computation_enabled),
00553         USAGE_I(N_USE_BATCH_COMPUTATION, "enable_batch_computation")
00554     },
00555     {
00556         N_USE_LINADD,
00557         (&CSGInterface::cmd_set_svm_linadd_enabled),
00558         USAGE_I(N_USE_LINADD, "enable_linadd")
00559     },
00560     {
00561         N_SVM_USE_BIAS,
00562         (&CSGInterface::cmd_set_svm_bias_enabled),
00563         USAGE_I(N_SVM_USE_BIAS, "enable_bias")
00564     },
00565     {
00566         N_MKL_USE_INTERLEAVED_OPTIMIZATION,
00567         (&CSGInterface::cmd_set_mkl_interleaved_enabled),
00568         USAGE_I(N_MKL_USE_INTERLEAVED_OPTIMIZATION, "enable_interleaved_optimization")
00569     },
00570     {
00571         N_KRR_TAU,
00572         (&CSGInterface::cmd_set_krr_tau),
00573         USAGE_I(N_KRR_TAU, "tau")
00574     },
00575 
00576 
00577     { "Preprocessors", NULL, NULL },
00578     {
00579         N_ADD_PREPROC,
00580         (&CSGInterface::cmd_add_preproc),
00581         USAGE_I(N_ADD_PREPROC, "preproc[, preproc-specific parameters]")
00582     },
00583     {
00584         N_DEL_PREPROC,
00585         (&CSGInterface::cmd_del_preproc),
00586         USAGE(N_DEL_PREPROC)
00587     },
00588     {
00589         N_ATTACH_PREPROC,
00590         (&CSGInterface::cmd_attach_preproc),
00591         USAGE_I(N_ATTACH_PREPROC, USAGE_STR "TRAIN|TEST" USAGE_STR USAGE_COMMA "force")
00592     },
00593     {
00594         N_CLEAN_PREPROC,
00595         (&CSGInterface::cmd_clean_preproc),
00596         USAGE(N_CLEAN_PREPROC)
00597     },
00598 
00599 
00600     { "HMM", NULL, NULL },
00601     {
00602         N_NEW_HMM,
00603         (&CSGInterface::cmd_new_hmm),
00604         USAGE_I(N_NEW_HMM, "N" USAGE_COMMA "M")
00605     },
00606     {
00607         N_LOAD_HMM,
00608         (&CSGInterface::cmd_load_hmm),
00609         USAGE_I(N_LOAD_HMM, "filename")
00610     },
00611     {
00612         N_SAVE_HMM,
00613         (&CSGInterface::cmd_save_hmm),
00614         USAGE_I(N_SAVE_HMM, "filename[" USAGE_COMMA "save_binary]")
00615     },
00616     {
00617         N_GET_HMM,
00618         (&CSGInterface::cmd_get_hmm),
00619         USAGE_O(N_GET_HMM, "p" USAGE_COMMA "q" USAGE_COMMA "a" USAGE_COMMA "b")
00620     },
00621     {
00622         N_APPEND_HMM,
00623         (&CSGInterface::cmd_append_hmm),
00624         USAGE_I(N_APPEND_HMM, "p" USAGE_COMMA "q" USAGE_COMMA "a" USAGE_COMMA "b")
00625     },
00626     {
00627         N_APPEND_MODEL,
00628         (&CSGInterface::cmd_append_model),
00629         USAGE_I(N_APPEND_MODEL, USAGE_STR "filename" USAGE_STR "[" USAGE_COMMA "base1" USAGE_COMMA "base2]")
00630     },
00631     {
00632         N_SET_HMM,
00633         (&CSGInterface::cmd_set_hmm),
00634         USAGE_I(N_SET_HMM, "p" USAGE_COMMA "q" USAGE_COMMA "a" USAGE_COMMA "b")
00635     },
00636     {
00637         N_SET_HMM_AS,
00638         (&CSGInterface::cmd_set_hmm_as),
00639         USAGE_I(N_SET_HMM_AS, "POS|NEG|TEST")
00640     },
00641     {
00642         N_CHOP,
00643         (&CSGInterface::cmd_set_chop),
00644         USAGE_I(N_CHOP, "chop")
00645     },
00646     {
00647         N_PSEUDO,
00648         (&CSGInterface::cmd_set_pseudo),
00649         USAGE_I(N_PSEUDO, "pseudo")
00650     },
00651     {
00652         N_LOAD_DEFINITIONS,
00653         (&CSGInterface::cmd_load_definitions),
00654         USAGE_I(N_LOAD_DEFINITIONS, "filename" USAGE_COMMA "init")
00655     },
00656     {
00657         N_HMM_CLASSIFY,
00658         (&CSGInterface::cmd_hmm_classify),
00659         USAGE_O(N_HMM_CLASSIFY, "result")
00660     },
00661     {
00662         N_ONE_CLASS_LINEAR_HMM_CLASSIFY,
00663         (&CSGInterface::cmd_one_class_linear_hmm_classify),
00664         USAGE_O(N_ONE_CLASS_LINEAR_HMM_CLASSIFY, "result")
00665     },
00666     {
00667         N_ONE_CLASS_HMM_CLASSIFY,
00668         (&CSGInterface::cmd_one_class_hmm_classify),
00669         USAGE_O(N_ONE_CLASS_HMM_CLASSIFY, "result")
00670     },
00671     {
00672         N_ONE_CLASS_HMM_CLASSIFY_EXAMPLE,
00673         (&CSGInterface::cmd_one_class_hmm_classify_example),
00674         USAGE_IO(N_ONE_CLASS_HMM_CLASSIFY_EXAMPLE, "feature_vector_index", "result")
00675     },
00676     {
00677         N_HMM_CLASSIFY_EXAMPLE,
00678         (&CSGInterface::cmd_hmm_classify_example),
00679         USAGE_IO(N_HMM_CLASSIFY_EXAMPLE, "feature_vector_index", "result")
00680     },
00681     {
00682         N_OUTPUT_HMM,
00683         (&CSGInterface::cmd_output_hmm),
00684         USAGE(N_OUTPUT_HMM)
00685     },
00686     {
00687         N_OUTPUT_HMM_DEFINED,
00688         (&CSGInterface::cmd_output_hmm_defined),
00689         USAGE(N_OUTPUT_HMM_DEFINED)
00690     },
00691     {
00692         N_HMM_LIKELIHOOD,
00693         (&CSGInterface::cmd_hmm_likelihood),
00694         USAGE_O(N_HMM_LIKELIHOOD, "likelihood")
00695     },
00696     {
00697         N_LIKELIHOOD,
00698         (&CSGInterface::cmd_likelihood),
00699         USAGE(N_LIKELIHOOD)
00700     },
00701     {
00702         N_SAVE_LIKELIHOOD,
00703         (&CSGInterface::cmd_save_likelihood),
00704         USAGE_I(N_SAVE_LIKELIHOOD, "filename[" USAGE_COMMA "save_binary]")
00705     },
00706     {
00707         N_GET_VITERBI_PATH,
00708         (&CSGInterface::cmd_get_viterbi_path),
00709         USAGE_IO(N_GET_VITERBI_PATH, "dim", "path" USAGE_COMMA "likelihood")
00710     },
00711     {
00712         N_VITERBI_TRAIN_DEFINED,
00713         (&CSGInterface::cmd_viterbi_train_defined),
00714         USAGE(N_VITERBI_TRAIN_DEFINED)
00715     },
00716     {
00717         N_VITERBI_TRAIN,
00718         (&CSGInterface::cmd_viterbi_train),
00719         USAGE(N_VITERBI_TRAIN)
00720     },
00721     {
00722         N_BAUM_WELCH_TRAIN,
00723         (&CSGInterface::cmd_baum_welch_train),
00724         USAGE(N_BAUM_WELCH_TRAIN)
00725     },
00726     {
00727         N_BAUM_WELCH_TRAIN_DEFINED,
00728         (&CSGInterface::cmd_baum_welch_train_defined),
00729         USAGE(N_BAUM_WELCH_TRAIN_DEFINED)
00730     },
00731     {
00732         N_BAUM_WELCH_TRANS_TRAIN,
00733         (&CSGInterface::cmd_baum_welch_trans_train),
00734         USAGE(N_BAUM_WELCH_TRANS_TRAIN)
00735     },
00736     {
00737         N_LINEAR_TRAIN,
00738         (&CSGInterface::cmd_linear_train),
00739         USAGE(N_LINEAR_TRAIN)
00740     },
00741     {
00742         N_SAVE_PATH,
00743         (&CSGInterface::cmd_save_path),
00744         USAGE_I(N_SAVE_PATH, "filename[" USAGE_COMMA "save_binary]")
00745     },
00746     {
00747         N_CONVERGENCE_CRITERIA,
00748         (&CSGInterface::cmd_convergence_criteria),
00749         USAGE_I(N_CONVERGENCE_CRITERIA, "num_iterations" USAGE_COMMA "epsilon")
00750     },
00751     {
00752         N_NORMALIZE,
00753         (&CSGInterface::cmd_normalize),
00754         USAGE_I(N_NORMALIZE, "[keep_dead_states]")
00755     },
00756     {
00757         N_ADD_STATES,
00758         (&CSGInterface::cmd_add_states),
00759         USAGE_I(N_ADD_STATES, "states" USAGE_COMMA "value")
00760     },
00761     {
00762         N_PERMUTATION_ENTROPY,
00763         (&CSGInterface::cmd_permutation_entropy),
00764         USAGE_I(N_PERMUTATION_ENTROPY, "width" USAGE_COMMA "seqnum")
00765     },
00766     {
00767         N_RELATIVE_ENTROPY,
00768         (&CSGInterface::cmd_relative_entropy),
00769         USAGE_O(N_RELATIVE_ENTROPY, "result")
00770     },
00771     {
00772         N_ENTROPY,
00773         (&CSGInterface::cmd_entropy),
00774         USAGE_O(N_ENTROPY, "result")
00775     },
00776     {
00777         (char*) N_SET_FEATURE_MATRIX,
00778         (&CSGInterface::cmd_set_feature_matrix),
00779         (char*) USAGE_I(N_SET_FEATURE_MATRIX, "features")
00780     },
00781     {
00782         (char*) N_SET_FEATURE_MATRIX_SPARSE,
00783         (&CSGInterface::cmd_set_feature_matrix_sparse),
00784         (char*) USAGE_I(N_SET_FEATURE_MATRIX_SPARSE, "sp1" USAGE_COMMA "sp2" )
00785     },
00786     {
00787         N_NEW_PLUGIN_ESTIMATOR,
00788         (&CSGInterface::cmd_new_plugin_estimator),
00789         USAGE_I(N_NEW_PLUGIN_ESTIMATOR, "pos_pseudo" USAGE_COMMA "neg_pseudo")
00790     },
00791     {
00792         N_TRAIN_ESTIMATOR,
00793         (&CSGInterface::cmd_train_estimator),
00794         USAGE(N_TRAIN_ESTIMATOR)
00795     },
00796     {
00797         N_PLUGIN_ESTIMATE_CLASSIFY_EXAMPLE,
00798         (&CSGInterface::cmd_plugin_estimate_classify_example),
00799         USAGE_IO(N_PLUGIN_ESTIMATE_CLASSIFY_EXAMPLE, "feature_vector_index", "result")
00800     },
00801     {
00802         N_PLUGIN_ESTIMATE_CLASSIFY,
00803         (&CSGInterface::cmd_plugin_estimate_classify),
00804         USAGE_O(N_PLUGIN_ESTIMATE_CLASSIFY, "result")
00805     },
00806     {
00807         N_SET_PLUGIN_ESTIMATE,
00808         (&CSGInterface::cmd_set_plugin_estimate),
00809         USAGE_I(N_SET_PLUGIN_ESTIMATE, "emission_probs" USAGE_COMMA "model_sizes")
00810     },
00811     {
00812         N_GET_PLUGIN_ESTIMATE,
00813         (&CSGInterface::cmd_get_plugin_estimate),
00814         USAGE_O(N_GET_PLUGIN_ESTIMATE, "emission_probs" USAGE_COMMA "model_sizes")
00815     },
00816     { "Signals", NULL, NULL },
00817     {
00818         N_SIGNALS_SET_MODEL,
00819         (&CSGInterface::cmd_signals_set_model),
00820         USAGE_I(N_SIGNALS_SET_MODEL, "arg1")
00821     },
00822     {
00823         N_SIGNALS_SET_POSITIONS,
00824         (&CSGInterface::cmd_signals_set_positions),
00825         USAGE_I(N_SIGNALS_SET_POSITIONS, "positions")
00826     },
00827     {
00828         N_SIGNALS_SET_LABELS,
00829         (&CSGInterface::cmd_signals_set_labels),
00830         USAGE_I(N_SIGNALS_SET_LABELS, "labels")
00831     },
00832     {
00833         N_SIGNALS_SET_SPLIT,
00834         (&CSGInterface::cmd_signals_set_split),
00835         USAGE_I(N_SIGNALS_SET_SPLIT, "split")
00836     },
00837     {
00838         N_SIGNALS_SET_TRAIN_MASK,
00839         (&CSGInterface::cmd_signals_set_train_mask),
00840         USAGE_I(N_SIGNALS_SET_TRAIN_MASK, "")
00841     },
00842     {
00843         N_SIGNALS_ADD_FEATURE,
00844         (&CSGInterface::cmd_signals_add_feature),
00845         USAGE_I(N_SIGNALS_ADD_FEATURE, "feature")
00846     },
00847     {
00848         N_SIGNALS_ADD_KERNEL,
00849         (&CSGInterface::cmd_signals_add_kernel),
00850         USAGE_I(N_SIGNALS_ADD_KERNEL, "kernelparam")
00851     },
00852     {
00853         N_SIGNALS_RUN,
00854         (&CSGInterface::cmd_signals_run),
00855         USAGE_I(N_SIGNALS_RUN, "arg1")
00856     },
00857     { "Structure", NULL, NULL },
00858     {
00859         N_BEST_PATH,
00860         (&CSGInterface::cmd_best_path),
00861         USAGE_I(N_BEST_PATH, "from" USAGE_COMMA "to")
00862     },
00863     {
00864         N_BEST_PATH_2STRUCT,
00865         (&CSGInterface::cmd_best_path_2struct),
00866         USAGE_IO(N_BEST_PATH_2STRUCT, "p"
00867                 USAGE_COMMA "q" 
00868                 USAGE_COMMA "cmd_trans"
00869                 USAGE_COMMA "seq"
00870                 USAGE_COMMA "pos"
00871                 USAGE_COMMA "genestr"
00872                 USAGE_COMMA "penalties"
00873                 USAGE_COMMA "penalty_info"
00874                 USAGE_COMMA "nbest"
00875                 USAGE_COMMA "content_weights"
00876                 USAGE_COMMA "segment_sum_weights",
00877             "prob" USAGE_COMMA "path" USAGE_COMMA "pos")
00878     },
00879     {
00880         (char*) N_SET_PLIF_STRUCT,
00881         (&CSGInterface::cmd_set_plif_struct),
00882         (char*) USAGE_I(N_SET_PLIF_STRUCT, "id"
00883                 USAGE_COMMA "name"
00884                 USAGE_COMMA "limits"
00885                 USAGE_COMMA "penalties"
00886                 USAGE_COMMA "transform"
00887                 USAGE_COMMA "min_value"
00888                 USAGE_COMMA "max_value"
00889                 USAGE_COMMA "use_cache"
00890                 USAGE_COMMA "use_svm")
00891     },
00892     {
00893         (char*) N_GET_PLIF_STRUCT,
00894         (&CSGInterface::cmd_get_plif_struct),
00895         (char*) USAGE_O(N_GET_PLIF_STRUCT, "id"
00896                 USAGE_COMMA "name"
00897                 USAGE_COMMA "limits"
00898                 USAGE_COMMA "penalties"
00899                 USAGE_COMMA "transform"
00900                 USAGE_COMMA "min_value"
00901                 USAGE_COMMA "max_value"
00902                 USAGE_COMMA "use_cache"
00903                 USAGE_COMMA "use_svm")
00904     },
00905     {
00906         (char*) N_PRECOMPUTE_SUBKERNELS,
00907         (&CSGInterface::cmd_precompute_subkernels),
00908         (char*) USAGE(N_PRECOMPUTE_SUBKERNELS)
00909     },
00910     {
00911         (char*) N_PRECOMPUTE_CONTENT_SVMS,
00912         (&CSGInterface::cmd_precompute_content_svms),
00913         (char*) USAGE_I(N_PRECOMPUTE_CONTENT_SVMS, "sequence"
00914                 USAGE_COMMA "position_list"
00915                 USAGE_COMMA "weights")
00916     },
00917     {
00918         (char*) N_GET_LIN_FEAT,
00919         (&CSGInterface::cmd_get_lin_feat),
00920         (char*) USAGE_O(N_GET_LIN_FEAT, "lin_feat")
00921     },
00922     {
00923         (char*) N_SET_LIN_FEAT,
00924         (&CSGInterface::cmd_set_lin_feat),
00925         (char*) USAGE_I(N_SET_LIN_FEAT, "lin_feat")
00926     },
00927     {
00928         (char*) N_INIT_DYN_PROG,
00929         (&CSGInterface::cmd_init_dyn_prog),
00930         (char*) USAGE_I(N_INIT_DYN_PROG, "num_svms")
00931     },
00932     {
00933         (char*) N_CLEAN_UP_DYN_PROG,
00934         (&CSGInterface::cmd_clean_up_dyn_prog),
00935         (char*) USAGE(N_CLEAN_UP_DYN_PROG)
00936     },
00937     {
00938         (char*) N_INIT_INTRON_LIST,
00939         (&CSGInterface::cmd_init_intron_list),
00940         (char*) USAGE_I(N_INIT_INTRON_LIST, "start_positions"
00941                 USAGE_COMMA "end_positions"
00942                 USAGE_COMMA "quality")
00943     },
00944     {
00945         (char*) N_PRECOMPUTE_TILING_FEATURES,
00946         (&CSGInterface::cmd_precompute_tiling_features),
00947         (char*) USAGE_I(N_PRECOMPUTE_TILING_FEATURES, "intensities"
00948                 USAGE_COMMA "probe_pos"
00949                 USAGE_COMMA "tiling_plif_ids")
00950     },
00951     {
00952         (char*) N_LONG_TRANSITION_SETTINGS,
00953         (&CSGInterface::cmd_long_transition_settings),
00954         (char*) USAGE_I(N_LONG_TRANSITION_SETTINGS, "use_long_transitions"
00955                 USAGE_COMMA "threshold"
00956                 USAGE_COMMA "max_len")
00957     },
00958 
00959     {
00960         (char*) N_SET_MODEL,
00961         (&CSGInterface::cmd_set_model),
00962         (char*) USAGE_I(N_SET_MODEL, "content_weights"
00963                 USAGE_COMMA "transition_pointers"
00964                 USAGE_COMMA "use_orf"
00965                 USAGE_COMMA "mod_words")
00966     },
00967 
00968     {
00969         (char*) N_BEST_PATH_TRANS,
00970         (&CSGInterface::cmd_best_path_trans),
00971         USAGE_IO(N_BEST_PATH_TRANS, "p"
00972                 USAGE_COMMA "q"
00973                 USAGE_COMMA "nbest"
00974                 USAGE_COMMA "seq_path"
00975                 USAGE_COMMA "a_trans"
00976                 USAGE_COMMA "segment_loss",
00977             "prob" USAGE_COMMA "path" USAGE_COMMA "pos")
00978     },
00979     {
00980         N_BEST_PATH_TRANS_DERIV,
00981         (&CSGInterface::cmd_best_path_trans_deriv),
00982         USAGE_IO(N_BEST_PATH_TRANS_DERIV,
00983             USAGE_COMMA "my_path"
00984             USAGE_COMMA "my_pos"
00985             USAGE_COMMA "p"
00986             USAGE_COMMA "q"
00987             USAGE_COMMA "cmd_trans"
00988             USAGE_COMMA "seq"
00989             USAGE_COMMA "pos"
00990             USAGE_COMMA "genestr"
00991             USAGE_COMMA "penalties"
00992             USAGE_COMMA "state_signals"
00993             USAGE_COMMA "penalty_info"
00994             USAGE_COMMA "dict_weights"
00995             USAGE_COMMA "mod_words ["
00996             USAGE_COMMA "segment_loss"
00997             USAGE_COMMA "segmend_ids_mask]", "p_deriv"
00998             USAGE_COMMA "q_deriv"
00999             USAGE_COMMA "cmd_deriv"
01000             USAGE_COMMA "penalties_deriv"
01001             USAGE_COMMA "my_scores"
01002             USAGE_COMMA "my_loss")
01003     },
01004 
01005     { "POIM", NULL, NULL },
01006     {
01007         N_COMPUTE_POIM_WD,
01008         (&CSGInterface::cmd_compute_POIM_WD),
01009         USAGE_IO(N_COMPUTE_POIM_WD, "max_order" USAGE_COMMA "distribution", "W")
01010     },
01011     {
01012         N_GET_SPEC_CONSENSUS,
01013         (&CSGInterface::cmd_get_SPEC_consensus),
01014         USAGE_O(N_GET_SPEC_CONSENSUS, "W")
01015     },
01016     {
01017         N_GET_SPEC_SCORING,
01018         (&CSGInterface::cmd_get_SPEC_scoring),
01019         USAGE_IO(N_GET_SPEC_SCORING, "max_order", "W")
01020     },
01021     {
01022         N_GET_WD_CONSENSUS,
01023         (&CSGInterface::cmd_get_WD_consensus),
01024         USAGE_O(N_GET_WD_CONSENSUS, "W")
01025     },
01026     {
01027         N_GET_WD_SCORING,
01028         (&CSGInterface::cmd_get_WD_scoring),
01029         USAGE_IO(N_GET_WD_SCORING, "max_order", "W")
01030     },
01031 
01032 
01033     { "Utility", NULL, NULL },
01034     {
01035         N_CRC,
01036         (&CSGInterface::cmd_crc),
01037         USAGE_IO(N_CRC, "string", "crc32")
01038     },
01039     {
01040         N_SYSTEM,
01041         (&CSGInterface::cmd_system),
01042         USAGE_I(N_SYSTEM, "system_command")
01043     },
01044     {
01045         N_EXIT,
01046         (&CSGInterface::cmd_exit),
01047         USAGE(N_EXIT)
01048     },
01049     {
01050         N_QUIT,
01051         (&CSGInterface::cmd_exit),
01052         USAGE(N_QUIT)
01053     },
01054     {
01055         N_EXEC,
01056         (&CSGInterface::cmd_exec),
01057         USAGE_I(N_EXEC, "filename")
01058     },
01059     {
01060         N_SET_OUTPUT,
01061         (&CSGInterface::cmd_set_output),
01062         USAGE_I(N_SET_OUTPUT, USAGE_STR "STDERR|STDOUT|filename" USAGE_STR)
01063     },
01064     {
01065         N_SET_THRESHOLD,
01066         (&CSGInterface::cmd_set_threshold),
01067         USAGE_I(N_SET_THRESHOLD, "threshold")
01068     },
01069     {
01070         N_INIT_RANDOM,
01071         (&CSGInterface::cmd_init_random),
01072         USAGE_I(N_INIT_RANDOM, "value_to_initialize_RNG_with")
01073     },
01074     {
01075         N_THREADS,
01076         (&CSGInterface::cmd_set_num_threads),
01077         USAGE_I(N_THREADS, "num_threads")
01078     },
01079     {
01080         N_TRANSLATE_STRING,
01081         (&CSGInterface::cmd_translate_string),
01082         USAGE_IO(N_TRANSLATE_STRING,
01083             "string, order, start", "translation")
01084     },
01085     {
01086         N_CLEAR,
01087         (&CSGInterface::cmd_clear),
01088         USAGE(N_CLEAR)
01089     },
01090     {
01091         N_TIC,
01092         (&CSGInterface::cmd_tic),
01093         USAGE(N_TIC)
01094     },
01095     {
01096         N_TOC,
01097         (&CSGInterface::cmd_toc),
01098         USAGE(N_TOC)
01099     },
01100     {
01101         N_PRINT,
01102         (&CSGInterface::cmd_print),
01103         USAGE_I(N_PRINT, "msg")
01104     },
01105     {
01106         N_ECHO,
01107         (&CSGInterface::cmd_echo),
01108         USAGE_I(N_ECHO, "level")
01109     },
01110     {
01111         N_LOGLEVEL,
01112         (&CSGInterface::cmd_loglevel),
01113         USAGE_I(N_LOGLEVEL, USAGE_STR "ALL|DEBUG|INFO|NOTICE|WARN|ERROR|CRITICAL|ALERT|EMERGENCY" USAGE_STR)
01114     },
01115     {
01116         N_SYNTAX_HIGHLIGHT,
01117         (&CSGInterface::cmd_syntax_highlight),
01118         USAGE_I(N_SYNTAX_HIGHLIGHT, USAGE_STR "ON|OFF" USAGE_STR)
01119     },
01120     {
01121         N_PROGRESS,
01122         (&CSGInterface::cmd_progress),
01123         USAGE_I(N_PROGRESS, USAGE_STR "ON|OFF" USAGE_STR)
01124     },
01125     {
01126         N_GET_VERSION,
01127         (&CSGInterface::cmd_get_version),
01128         USAGE_O(N_GET_VERSION, "version")
01129     },
01130     {
01131         N_HELP,
01132         (&CSGInterface::cmd_help),
01133         USAGE(N_HELP)
01134     },
01135     {
01136         N_WHOS,
01137         (&CSGInterface::cmd_whos),
01138         USAGE(N_WHOS)
01139     },
01140     {
01141         N_SEND_COMMAND,
01142         (&CSGInterface::cmd_send_command),
01143         NULL
01144     },
01145     {
01146         N_RUN_PYTHON,
01147         (&CSGInterface::cmd_run_python),
01148         USAGE_IO(N_RUN_PYTHON,
01149             "'Var1', Var1, 'Var2', Var2,..., python_function", "results")
01150     },
01151     {
01152         N_RUN_OCTAVE,
01153         (&CSGInterface::cmd_run_octave),
01154         USAGE_IO(N_RUN_OCTAVE,
01155             "'Var1', Var1, 'Var2', Var2,..., octave_function", "results")
01156     },
01157     {
01158         N_RUN_R,
01159         (&CSGInterface::cmd_run_r),
01160         USAGE_IO(N_RUN_R,
01161             "'Var1', Var1, 'Var2', Var2,..., r_function", "results")
01162     },
01163     {NULL, NULL, NULL}        /* Sentinel */
01164 };
01165 
01166 
01167 CSGInterface::CSGInterface(bool print_copyright)
01168 : CSGObject(),
01169     ui_classifier(new CGUIClassifier(this)),
01170     ui_distance(new CGUIDistance(this)),
01171     ui_features(new CGUIFeatures(this)),
01172     ui_hmm(new CGUIHMM(this)),
01173     ui_kernel(new CGUIKernel(this)),
01174     ui_labels(new CGUILabels(this)),
01175     ui_math(new CGUIMath(this)),
01176     ui_pluginestimate(new CGUIPluginEstimate(this)),
01177     ui_preproc(new CGUIPreprocessor(this)),
01178     ui_time(new CGUITime(this)),
01179   ui_structure(new CGUIStructure(this))/*,
01180 /   ui_signals(new CGUISignals(this))*/
01181 {
01182     if (print_copyright)
01183     {
01184         version->print_version();
01185         SG_PRINT("( seeding random number generator with %u (seed size %d))\n",
01186                 CMath::get_seed(), RNG_SEED_SIZE);
01187 #ifdef USE_LOGCACHE
01188         SG_PRINT( "initializing log-table (size=%i*%i*%i=%2.1fMB) ... ) ",
01189                 CMath::get_log_range(),CMath::get_log_accuracy(),sizeof(float64_t),
01190                 CMath::get_log_range()*CMath::get_log_accuracy()*sizeof(float64_t)/(1024.0*1024.0));
01191 #else
01192         SG_PRINT("determined range for x in log(1+exp(-x)) is:%d )\n", CMath::get_log_range());
01193 #endif 
01194     }
01195 
01196     reset();
01197 }
01198 
01199 CSGInterface::~CSGInterface()
01200 {
01201     delete ui_classifier;
01202     delete ui_hmm;
01203     delete ui_pluginestimate;
01204     delete ui_kernel;
01205     delete ui_preproc;
01206     delete ui_features;
01207     delete ui_labels;
01208     delete ui_math;
01209     delete ui_structure;
01210     //delete ui_signals; 
01211     delete ui_time;
01212     delete ui_distance;
01213 
01214     if (file_out)
01215         fclose(file_out);
01216 }
01217 
01218 void CSGInterface::reset()
01219 {
01220     m_lhs_counter=0;
01221     m_rhs_counter=0;
01222     m_nlhs=0;
01223     m_nrhs=0;
01224     m_legacy_strptr=NULL;
01225     file_out=NULL;
01226     echo=true;
01227 }
01228 
01229 void CSGInterface::translate_arg(CSGInterface* source, CSGInterface* target)
01230 {
01231     switch (source->get_argument_type())
01232     {
01233         case SCALAR_INT:
01234             target->set_int(source->get_int());
01235             break;
01236         case SCALAR_REAL:
01237             target->set_real(source->get_real());
01238             break;
01239         case SCALAR_BOOL:
01240             target->set_bool(source->get_bool());
01241             break;
01242         case VECTOR_BOOL:
01243             {
01244                 bool* v=NULL;
01245                 int32_t len=0;
01246                 source->get_vector(v, len);
01247                 target->set_vector(v, len);
01248                 SG_FREE(v);
01249                 break;
01250             }
01251         case VECTOR_BYTE:
01252             {
01253                 uint8_t* v=NULL;
01254                 int32_t len=0;
01255                 source->get_vector(v, len);
01256                 target->set_vector(v, len);
01257                 SG_FREE(v);
01258                 break;
01259             }
01260         case VECTOR_CHAR:
01261             {
01262                 char* v=NULL;
01263                 int32_t len=0;
01264                 source->get_vector(v, len);
01265                 target->set_vector(v, len);
01266                 SG_FREE(v);
01267                 break;
01268             }
01269         case VECTOR_INT:
01270             {
01271                 int32_t* v=NULL;
01272                 int32_t len=0;
01273                 source->get_vector(v, len);
01274                 target->set_vector(v, len);
01275                 SG_FREE(v);
01276                 break;
01277             }
01278         case VECTOR_REAL:
01279             {
01280                 float64_t* v=NULL;
01281                 int32_t len=0;
01282                 source->get_vector(v, len);
01283                 target->set_vector(v, len);
01284                 SG_FREE(v);
01285                 break;
01286             }
01287         case VECTOR_SHORTREAL:
01288             {
01289                 float32_t* v=NULL;
01290                 int32_t len=0;
01291                 source->get_vector(v, len);
01292                 target->set_vector(v, len);
01293                 SG_FREE(v);
01294                 break;
01295             }
01296         case VECTOR_SHORT:
01297             {
01298                 int16_t* v=NULL;
01299                 int32_t len=0;
01300                 source->get_vector(v, len);
01301                 target->set_vector(v, len);
01302                 SG_FREE(v);
01303                 break;
01304             }
01305         case VECTOR_WORD:
01306             {
01307                 uint16_t* v=NULL;
01308                 int32_t len=0;
01309                 source->get_vector(v, len);
01310                 target->set_vector(v, len);
01311                 SG_FREE(v);
01312                 break;
01313             }
01314 
01315         case STRING_BYTE:
01316             {
01317                 int32_t num_str=0;
01318                 int32_t max_str_len=0;
01319                 SGString<uint8_t>* strs=NULL;
01320                 source->get_string_list(strs, num_str, max_str_len);
01321                 target->set_string_list(strs, num_str);
01322                 SG_FREE(strs);
01323                 break;
01324             }
01325         case STRING_CHAR:
01326             {
01327                 int32_t num_str=0;
01328                 int32_t max_str_len=0;
01329                 SGString<char>* strs;
01330                 source->get_string_list(strs, num_str,max_str_len);
01331                 target->set_string_list(strs, num_str);
01332                 SG_FREE(strs);
01333                 break;
01334             }
01335         case STRING_INT:
01336             {
01337                 int32_t num_str=0;
01338                 int32_t max_str_len=0;
01339                 SGString<int32_t>* strs;
01340                 source->get_string_list(strs, num_str,max_str_len);
01341                 target->set_string_list(strs, num_str);
01342                 SG_FREE(strs);
01343                 break;
01344             }
01345         case STRING_SHORT:
01346             {
01347                 int32_t num_str=0;
01348                 int32_t max_str_len=0;
01349                 SGString<int16_t>* strs=NULL;
01350                 source->get_string_list(strs, num_str, max_str_len);
01351                 target->set_string_list(strs, num_str);
01352                 SG_FREE(strs);
01353                 break;
01354             }
01355         case STRING_WORD:
01356             {
01357                 int32_t num_str=0;
01358                 int32_t max_str_len=0;
01359                 SGString<uint16_t>* strs=NULL;
01360                 source->get_string_list(strs, num_str, max_str_len);
01361                 target->set_string_list(strs, num_str);
01362                 SG_FREE(strs);
01363                 break;
01364             }
01365         case DENSE_INT:
01366             {
01367                 int32_t num_feat=0;
01368                 int32_t num_vec=0;
01369                 int32_t* fmatrix=NULL;
01370                 source->get_matrix(fmatrix, num_feat, num_vec);
01371                 target->set_matrix(fmatrix, num_feat, num_vec);
01372                 SG_FREE(fmatrix);
01373                 break;
01374             }
01375         case DENSE_REAL:
01376             {
01377                 int32_t num_feat=0;
01378                 int32_t num_vec=0;
01379                 float64_t* fmatrix=NULL;
01380                 source->get_matrix(fmatrix, num_feat, num_vec);
01381                 target->set_matrix(fmatrix, num_feat, num_vec);
01382                 SG_FREE(fmatrix);
01383                 break;
01384             }
01385         case DENSE_SHORT:
01386             {
01387                 int32_t num_feat=0;
01388                 int32_t num_vec=0;
01389                 int16_t* fmatrix=NULL;
01390                 source->get_matrix(fmatrix, num_feat, num_vec);
01391                 target->set_matrix(fmatrix, num_feat, num_vec);
01392                 SG_FREE(fmatrix);
01393                 break;
01394             }
01395         case DENSE_SHORTREAL:
01396             {
01397                 int32_t num_feat=0;
01398                 int32_t num_vec=0;
01399                 float32_t* fmatrix=NULL;
01400                 source->get_matrix(fmatrix, num_feat, num_vec);
01401                 target->set_matrix(fmatrix, num_feat, num_vec);
01402                 SG_FREE(fmatrix);
01403                 break;
01404             }
01405         case DENSE_WORD:
01406             {
01407                 int32_t num_feat=0;
01408                 int32_t num_vec=0;
01409                 uint16_t* fmatrix=NULL;
01410                 source->get_matrix(fmatrix, num_feat, num_vec);
01411                 target->set_matrix(fmatrix, num_feat, num_vec);
01412                 SG_FREE(fmatrix);
01413                 break;
01414             }
01415             /*
01416         case NDARRAY_BYTE:
01417             {
01418                 uint8_t* a=NULL;
01419                 int32_t* dims=NULL;
01420                 int32_t num_dims=0;
01421                 source->get_ndarray(a, dims, num_dims);
01422                 target->set_ndarray(a, dims, num_dims);
01423                 SG_FREE(a);
01424                 SG_FREE(dims);
01425                 break;
01426             }
01427         case NDARRAY_CHAR:
01428             {
01429                 char* a=NULL;
01430                 int32_t* dims=NULL;
01431                 int32_t num_dims=0;
01432                 source->get_ndarray(a, dims, num_dims);
01433                 target->set_ndarray(a, dims, num_dims);
01434                 SG_FREE(a);
01435                 SG_FREE(dims);
01436                 break;
01437             }
01438         case NDARRAY_INT:
01439             {
01440                 int32_t* a=NULL;
01441                 int32_t* dims=NULL;
01442                 int32_t num_dims=0;
01443                 source->get_ndarray(a, dims, num_dims);
01444                 target->set_ndarray(a, dims, num_dims);
01445                 SG_FREE(a);
01446                 SG_FREE(dims);
01447                 break;
01448             }
01449         case NDARRAY_REAL:
01450             {
01451                 float64_t* a=NULL;
01452                 int32_t* dims=NULL;
01453                 int32_t num_dims=0;
01454                 source->get_ndarray(a, dims, num_dims);
01455                 target->set_ndarray(a, dims, num_dims);
01456                 SG_FREE(a);
01457                 SG_FREE(dims);
01458                 break;
01459             }
01460         case NDARRAY_SHORTREAL:
01461             {
01462                 float32_t* a=NULL;
01463                 int32_t* dims=NULL;
01464                 int32_t num_dims=0;
01465                 source->get_ndarray(a, dims, num_dims);
01466                 target->set_ndarray(a, dims, num_dims);
01467                 SG_FREE(a);
01468                 SG_FREE(dims);
01469                 break;
01470             }
01471         case NDARRAY_SHORT:
01472             {
01473                 int16_t* a=NULL;
01474                 int32_t* dims=NULL;
01475                 int32_t num_dims=0;
01476                 source->get_ndarray(a, dims, num_dims);
01477                 target->set_ndarray(a, dims, num_dims);
01478                 SG_FREE(a);
01479                 SG_FREE(dims);
01480                 break;
01481             }
01482         case NDARRAY_WORD:
01483             {
01484                 uint16_t* a=NULL;
01485                 int32_t* dims=NULL;
01486                 int32_t num_dims=0;
01487                 source->get_ndarray(a, dims, num_dims);
01488                 target->set_ndarray(a, dims, num_dims);
01489                 SG_FREE(a);
01490                 SG_FREE(dims);
01491                 break;
01492             }*/
01493         case SPARSE_REAL:
01494             {
01495                 int32_t num_feat=0;
01496                 int32_t num_vec=0;
01497                 SGSparseVector<float64_t>* fmatrix=NULL;
01498                 source->get_sparse_matrix(fmatrix, num_feat, num_vec);
01499                 int64_t nnz=0;
01500                 for (int32_t i=0; i<num_vec; i++)
01501                     nnz+=fmatrix[i].num_feat_entries;
01502                 target->set_sparse_matrix(fmatrix, num_feat, num_vec, nnz);
01503                 SG_FREE(fmatrix);
01504                 break;
01505             }
01506 
01507         default:
01508             SG_ERROR("unknown return type");
01509             break;
01510     }
01511 }
01512 
01514 // commands
01516 
01517 /* Features */
01518 
01519 bool CSGInterface::cmd_load_features()
01520 {
01521     if (m_nrhs<8 || !create_return_values(0))
01522         return false;
01523 
01524     int32_t len=0;
01525     char* filename=get_str_from_str_or_direct(len);
01526     char* fclass=get_str_from_str_or_direct(len);
01527     char* type=get_str_from_str_or_direct(len);
01528     char* target=get_str_from_str_or_direct(len);
01529     int32_t size=get_int_from_int_or_str();
01530     int32_t comp_features=get_int_from_int_or_str();
01531 
01532     bool success=ui_features->load(
01533         filename, fclass, type, target, size, comp_features);
01534 
01535     SG_FREE(filename);
01536     SG_FREE(fclass);
01537     SG_FREE(type);
01538     SG_FREE(target);
01539     return success;
01540 }
01541 
01542 bool CSGInterface::cmd_save_features()
01543 {
01544     if (m_nrhs<5 || !create_return_values(0))
01545         return false;
01546 
01547     int32_t len=0;
01548     char* filename=get_str_from_str_or_direct(len);
01549     char* type=get_str_from_str_or_direct(len);
01550     char* target=get_str_from_str_or_direct(len);
01551 
01552     bool success=ui_features->save(filename, type, target);
01553 
01554     SG_FREE(filename);
01555     SG_FREE(type);
01556     SG_FREE(target);
01557     return success;
01558 }
01559 
01560 bool CSGInterface::cmd_clean_features()
01561 {
01562     if (m_nrhs<2 || !create_return_values(0))
01563         return false;
01564 
01565     int32_t len=0;
01566     char* target=get_str_from_str_or_direct(len);
01567 
01568     bool success=ui_features->clean(target);
01569 
01570     SG_FREE(target);
01571     return success;
01572 }
01573 
01574 bool CSGInterface::cmd_get_features()
01575 {
01576     if (m_nrhs!=2 || !create_return_values(1))
01577         return false;
01578 
01579     int32_t tlen=0;
01580     char* target=get_string(tlen);
01581     CFeatures* feat=NULL;
01582 
01583     if (strmatch(target, "TRAIN"))
01584         feat=ui_features->get_train_features();
01585     else if (strmatch(target, "TEST"))
01586         feat=ui_features->get_test_features();
01587     else
01588     {
01589         SG_FREE(target);
01590         SG_ERROR("Unknown target, neither TRAIN nor TEST.\n");
01591     }
01592     SG_FREE(target);
01593 
01594     ASSERT(feat);
01595 
01596     switch (feat->get_feature_class())
01597     {
01598         case C_SIMPLE:
01599         {
01600             int32_t num_feat=0;
01601             int32_t num_vec=0;
01602 
01603             switch (feat->get_feature_type())
01604             {
01605                 case F_BYTE:
01606                 {
01607                     uint8_t* fmatrix=((CSimpleFeatures<uint8_t> *) feat)->get_feature_matrix(num_feat, num_vec);
01608                     set_matrix(fmatrix, num_feat, num_vec);
01609                     break;
01610                 }
01611 
01612                 case F_CHAR:
01613                 {
01614                     char* fmatrix=((CSimpleFeatures<char> *) feat)->get_feature_matrix(num_feat, num_vec);
01615                     set_matrix(fmatrix, num_feat, num_vec);
01616                     break;
01617                 }
01618 
01619                 case F_DREAL:
01620                 {
01621                     float64_t* fmatrix=((CSimpleFeatures<float64_t> *) feat)->get_feature_matrix(num_feat, num_vec);
01622                     set_matrix(fmatrix, num_feat, num_vec);
01623                     break;
01624                 }
01625 
01626                 case F_INT:
01627                 {
01628                     int32_t* fmatrix=((CSimpleFeatures<int32_t> *) feat)->get_feature_matrix(num_feat, num_vec);
01629                     set_matrix(fmatrix, num_feat, num_vec);
01630                     break;
01631                 }
01632 
01633                 case F_SHORT:
01634                 {
01635                     int16_t* fmatrix=((CSimpleFeatures<int16_t> *) feat)->get_feature_matrix(num_feat, num_vec);
01636                     set_matrix(fmatrix, num_feat, num_vec);
01637                     break;
01638                 }
01639 
01640                 case F_SHORTREAL:
01641                 {
01642                     float32_t* fmatrix=((CSimpleFeatures<float32_t> *) feat)->get_feature_matrix(num_feat, num_vec);
01643                     set_matrix(fmatrix, num_feat, num_vec);
01644                     break;
01645                 }
01646 
01647                 case F_WORD:
01648                 {
01649                     uint16_t* fmatrix=((CSimpleFeatures<uint16_t> *) feat)->get_feature_matrix(num_feat, num_vec);
01650                     set_matrix(fmatrix, num_feat, num_vec);
01651                     break;
01652                 }
01653 
01654                 default:
01655                     SG_NOTIMPLEMENTED;
01656             }
01657             break;
01658         }
01659 
01660         case C_SPARSE:
01661         {
01662             switch (feat->get_feature_type())
01663             {
01664                 case F_DREAL:
01665                 {
01666                     int64_t nnz=((CSparseFeatures<float64_t>*) feat)->
01667                         get_num_nonzero_entries();
01668                     int32_t num_feat=0;
01669                     int32_t num_vec=0;
01670                     SGSparseVector<float64_t>* fmatrix=((CSparseFeatures<float64_t>*) feat)->get_sparse_feature_matrix(num_feat, num_vec);
01671                     SG_INFO("sparse matrix has %d feats, %d vecs and %d nnz elemements\n", num_feat, num_vec, nnz);
01672 
01673                     set_sparse_matrix(fmatrix, num_feat, num_vec, nnz);
01674                     break;
01675                 }
01676 
01677                 default:
01678                     SG_NOTIMPLEMENTED;
01679             }
01680             break;
01681         }
01682 
01683         case C_STRING:
01684         {
01685             int32_t num_str=0;
01686             int32_t max_str_len=0;
01687             switch (feat->get_feature_type())
01688             {
01689                 case F_BYTE:
01690                 {
01691                     SGString<uint8_t>* fmatrix=((CStringFeatures<uint8_t>*) feat)->get_features(num_str, max_str_len);
01692                     set_string_list(fmatrix, num_str);
01693                     break;
01694                 }
01695 
01696                 case F_CHAR:
01697                 {
01698                     SGString<char>* fmatrix=((CStringFeatures<char>*) feat)->get_features(num_str, max_str_len);
01699                     set_string_list(fmatrix, num_str);
01700                     break;
01701                 }
01702 
01703                 case F_WORD:
01704                 {
01705                     SGString<uint16_t>* fmatrix=((CStringFeatures<uint16_t>*) feat)->get_features(num_str, max_str_len);
01706                     set_string_list(fmatrix, num_str);
01707                     break;
01708                 }
01709 
01710                 default:
01711                     SG_NOTIMPLEMENTED;
01712             }
01713             break;
01714         }
01715 
01716         case C_WD:
01717         case C_WEIGHTEDSPEC:
01718         case C_SPEC:
01719         case C_COMBINED_DOT:
01720         case C_POLY:
01721         {
01722 
01723             SGMatrix<float64_t> fmatrix = ((CDotFeatures*) feat)->get_computed_dot_feature_matrix();
01724             set_matrix(fmatrix.matrix, fmatrix.num_cols, fmatrix.num_rows);
01725             fmatrix.free_matrix();
01726             break;
01727         }
01728 
01729         default:
01730             SG_NOTIMPLEMENTED;
01731     }
01732 
01733     return true;
01734 }
01735 
01736 bool CSGInterface::cmd_add_features()
01737 {
01738     if (m_nrhs<3 || !create_return_values(0))
01739         return false;
01740 
01741     return do_set_features(true, false);
01742 }
01743 
01744 bool CSGInterface::cmd_add_multiple_features()
01745 {
01746     if ((m_nrhs!=4 && m_nrhs<5) || !create_return_values(0))
01747         return false;
01748 
01749     int32_t repetitions=get_int();
01750 
01751     ASSERT(repetitions>=1);
01752 
01753     return do_set_features(true, false, repetitions);
01754 }
01755 
01756 bool CSGInterface::cmd_add_dotfeatures()
01757 {
01758     if (m_nrhs<3 || !create_return_values(0))
01759         return false;
01760 
01761     return do_set_features(true, true);
01762 }
01763 
01764 bool CSGInterface::cmd_set_features()
01765 {
01766     if (m_nrhs<3 || !create_return_values(0))
01767         return false;
01768 
01769     return do_set_features(false, false);
01770 }
01771 
01772 bool CSGInterface::do_set_features(bool add, bool check_dot, int32_t repetitions)
01773 {
01774     int32_t tlen=0;
01775     char* target=get_string(tlen);
01776     if (!strmatch(target, "TRAIN") && !strmatch(target, "TEST"))
01777     {
01778         SG_FREE(target);
01779         SG_ERROR("Unknown target, neither TRAIN nor TEST.\n");
01780     }
01781 
01782     CFeatures* feat=NULL;
01783     int32_t num_feat=0;
01784     int32_t num_vec=0;
01785 
01786     switch (get_argument_type())
01787     {
01788         case SPARSE_REAL:
01789         {
01790             SGSparseVector<float64_t>* fmatrix=NULL;
01791             get_sparse_matrix(fmatrix, num_feat, num_vec);
01792 
01793             feat=new CSparseFeatures<float64_t>();
01794             ((CSparseFeatures<float64_t>*) feat)->
01795                 set_sparse_feature_matrix(SGSparseMatrix<float64_t>(fmatrix, num_feat, num_vec));
01796             break;
01797         }
01798 
01799         case DENSE_REAL:
01800         {
01801             float64_t* fmatrix=NULL;
01802             get_matrix(fmatrix, num_feat, num_vec);
01803 
01804             feat=new CSimpleFeatures<float64_t>(0);
01805             ((CSimpleFeatures<float64_t>*) feat)->
01806                 set_feature_matrix(fmatrix, num_feat, num_vec);
01807 
01808             if (m_nrhs==6)
01809                 feat = create_custom_real_features((CSimpleFeatures<float64_t>*) feat);
01810             
01811             break;
01812         }
01813 
01814         case DENSE_INT:
01815         {
01816             int32_t* fmatrix=NULL;
01817             get_matrix(fmatrix, num_feat, num_vec);
01818 
01819             feat=new CSimpleFeatures<int32_t>(0);
01820             ((CSimpleFeatures<int32_t>*) feat)->
01821                 set_feature_matrix(fmatrix, num_feat, num_vec);
01822             break;
01823         }
01824 
01825         case DENSE_SHORT:
01826         {
01827             int16_t* fmatrix=NULL;
01828             get_matrix(fmatrix, num_feat, num_vec);
01829 
01830             feat=new CSimpleFeatures<int16_t>(0);
01831             ((CSimpleFeatures<int16_t>*) feat)->
01832                 set_feature_matrix(fmatrix, num_feat, num_vec);
01833             break;
01834         }
01835 
01836         case DENSE_WORD:
01837         {
01838             uint16_t* fmatrix=NULL;
01839             get_matrix(fmatrix, num_feat, num_vec);
01840 
01841             feat=new CSimpleFeatures<uint16_t>(0);
01842             ((CSimpleFeatures<uint16_t>*) feat)->
01843                 set_feature_matrix(fmatrix, num_feat, num_vec);
01844             break;
01845         }
01846 
01847         case DENSE_SHORTREAL:
01848         {
01849             float32_t* fmatrix=NULL;
01850             get_matrix(fmatrix, num_feat, num_vec);
01851 
01852             feat=new CSimpleFeatures<float32_t>(0);
01853             ((CSimpleFeatures<float32_t>*) feat)->
01854                 set_feature_matrix(fmatrix, num_feat, num_vec);
01855             break;
01856         }
01857 
01858         case STRING_CHAR:
01859         {
01860             if (m_nrhs<4)
01861                 SG_ERROR("Please specify alphabet!\n");
01862 
01863             int32_t num_str=0;
01864             int32_t max_str_len=0;
01865             SGString<char>* fmatrix=NULL;
01866             get_string_list(fmatrix, num_str, max_str_len);
01867 
01868             int32_t alphabet_len=0;
01869             char* alphabet_str=get_string(alphabet_len);
01870             ASSERT(alphabet_str);
01871 
01872             if (strmatch(alphabet_str, "DNABINFILE"))
01873             {
01874                 SG_FREE(alphabet_str);
01875 
01876                 ASSERT(fmatrix[0].string);
01877                 feat=new CStringFeatures<uint8_t>(DNA);
01878 
01879                 try
01880                 {
01881                     ((CStringFeatures<uint8_t>*) feat)->load_ascii_file(fmatrix[0].string);
01882                 }
01883                 catch (...)
01884                 {
01885                     SG_UNREF(feat);
01886                     SG_ERROR("Couldn't load DNA features from file.\n");
01887                 }
01888                 feat=create_custom_string_features((CStringFeatures<uint8_t>*) feat);
01889                 break;
01890             }
01891             else
01892             {
01893                 bool convert_to_word=false;
01894                 bool convert_to_ulong=false;
01895                 CAlphabet* alphabet=NULL;
01896                 if (strmatch(alphabet_str, "DNAWORD"))
01897                 {
01898                     alphabet=new CAlphabet(DNA);
01899                     convert_to_word=true;
01900                 }
01901                 else if (strmatch(alphabet_str, "DNAULONG"))
01902                 {
01903                     alphabet=new CAlphabet(DNA);
01904                     convert_to_ulong=true;
01905                 }
01906                 else
01907                     alphabet=new CAlphabet(alphabet_str, alphabet_len);
01908 
01909                 SG_REF(alphabet);
01910                 SG_FREE(alphabet_str);
01911 
01912                 feat=new CStringFeatures<char>(alphabet);
01913 
01914                 if (!((CStringFeatures<char>*) feat)->set_features(fmatrix, num_str, max_str_len))
01915                 {
01916                     SG_UNREF(alphabet);
01917                     SG_UNREF(feat);
01918                     SG_ERROR("Couldnt set byte string features.\n");
01919                 }
01920 
01921                 SG_UNREF(alphabet);
01922 
01923                 if (convert_to_word || convert_to_ulong)
01924                     convert_to_bitembedding(feat, convert_to_word, convert_to_ulong);
01925             }
01926 
01927             obtain_from_single_string(feat);
01928             break;
01929         }
01930 
01931         case STRING_BYTE:
01932         {
01933             if (m_nrhs<4)
01934                 SG_ERROR("Please specify alphabet!\n");
01935 
01936             int32_t num_str=0;
01937             int32_t max_str_len=0;
01938             SGString<uint8_t>* fmatrix=NULL;
01939             get_string_list(fmatrix, num_str, max_str_len);
01940 
01941             int32_t alphabet_len=0;
01942             char* alphabet_str=get_string(alphabet_len);
01943             ASSERT(alphabet_str);
01944             CAlphabet* alphabet=NULL;
01945             alphabet=new CAlphabet(alphabet_str, alphabet_len);
01946             SG_FREE(alphabet_str);
01947 
01948             feat=new CStringFeatures<uint8_t>(alphabet);
01949             if (!((CStringFeatures<uint8_t>*) feat)->set_features(fmatrix, num_str, max_str_len))
01950             {
01951                 SG_UNREF(alphabet);
01952                 SG_UNREF(feat);
01953                 SG_ERROR("Couldnt set byte string features.\n");
01954             }
01955             feat=create_custom_string_features((CStringFeatures<uint8_t>*) feat);
01956             break;
01957         }
01958 
01959         default:
01960             SG_ERROR("Wrong argument type %d.\n", get_argument_type());
01961     }
01962 
01963     if (check_dot && !feat->has_property(FP_DOT))
01964     {
01965         SG_UNREF(feat);
01966         SG_ERROR("Feature type not supported by DOT Features\n");
01967     }
01968 
01969     if (strmatch(target, "TRAIN"))
01970     {
01971         if (!add)
01972             ui_features->set_train_features(feat);
01973         else if (check_dot)
01974         {
01975             for (int32_t i=0; i<repetitions; i++)
01976             ui_features->add_train_dotfeatures((CDotFeatures*) feat);
01977         }
01978         else
01979         {
01980             for (int32_t i=0; i<repetitions; i++)
01981             ui_features->add_train_features(feat);
01982         }
01983     }
01984     else
01985     {
01986         if (!add)
01987             ui_features->set_test_features(feat);
01988         else if (check_dot)
01989         {
01990             for (int32_t i=0; i<repetitions; i++)
01991                 ui_features->add_test_dotfeatures((CDotFeatures*) feat);
01992         }
01993         else
01994         {
01995             for (int32_t i=0; i<repetitions; i++)
01996                 ui_features->add_test_features(feat);
01997         }
01998     }
01999 
02000     SG_FREE(target);
02001 
02002     return true;
02003 }
02004 
02005 bool CSGInterface::cmd_set_reference_features()
02006 {
02007     if (m_nrhs<3 || !create_return_values(0))
02008         return false;
02009 
02010     int32_t len=0;
02011     char* target=get_str_from_str_or_direct(len);
02012 
02013     bool success=ui_features->set_reference_features(target);
02014 
02015     SG_FREE(target);
02016     return success;
02017 }
02018 
02019 bool CSGInterface::cmd_del_last_features()
02020 {
02021     if (m_nrhs<2 || !create_return_values(0))
02022         return false;
02023 
02024     int32_t len=0;
02025     char* target=get_str_from_str_or_direct(len);
02026     bool success=ui_features->del_last_feature_obj(target);
02027 
02028     SG_FREE(target);
02029     return success;
02030 }
02031 
02032 bool CSGInterface::cmd_convert()
02033 {
02034     if (m_nrhs<5 || !create_return_values(0))
02035         return false;
02036 
02037     int32_t len=0;
02038     char* target=get_str_from_str_or_direct(len);
02039     CFeatures* features=ui_features->get_convert_features(target);
02040     if (!features)
02041     {
02042         SG_FREE(target);
02043         SG_ERROR("No \"%s\" features available.\n", target);
02044     }
02045 
02046     char* from_class=get_str_from_str_or_direct(len);
02047     char* from_type=get_str_from_str_or_direct(len);
02048     char* to_class=get_str_from_str_or_direct(len);
02049     char* to_type=get_str_from_str_or_direct(len);
02050 
02051     CFeatures* result=NULL;
02052     if (strmatch(from_class, "SIMPLE"))
02053     {
02054         if (strmatch(from_type, "REAL"))
02055         {
02056             if (strmatch(to_class, "SPARSE") &&
02057                 strmatch(to_type, "REAL"))
02058             {
02059                 result=ui_features->convert_simple_real_to_sparse_real(
02060                     ((CSimpleFeatures<float64_t>*) features));
02061             }
02062             else
02063                 SG_NOTIMPLEMENTED;
02064         } // from_type REAL
02065 
02066         else if (strmatch(from_type, "CHAR"))
02067         {
02068             if (strmatch(to_class, "STRING") &&
02069                 strmatch(to_type, "CHAR"))
02070             {
02071                 result=ui_features->convert_simple_char_to_string_char(
02072                     ((CSimpleFeatures<char>*) features));
02073             }
02074             else if (strmatch(to_class, "SIMPLE"))
02075             {
02076                 if (strmatch(to_type, "ALIGN") && m_nrhs==8)
02077                 {
02078                     float64_t gap_cost=get_real_from_real_or_str();
02079                     result=ui_features->convert_simple_char_to_simple_align(
02080                         (CSimpleFeatures<char>*) features, gap_cost);
02081                 }
02082                 else
02083                     SG_NOTIMPLEMENTED;
02084             }
02085             else
02086                 SG_NOTIMPLEMENTED;
02087         } // from_type CHAR
02088 
02089         else if (strmatch(from_type, "WORD"))
02090         {
02091             if (strmatch(to_class, "SIMPLE") &&
02092                 strmatch(to_type, "SALZBERG"))
02093             {
02094                 result=ui_features->convert_simple_word_to_simple_salzberg(
02095                     (CSimpleFeatures<uint16_t>*) features);
02096             }
02097             else
02098                 SG_NOTIMPLEMENTED;
02099         } // from_type WORD
02100 
02101         else
02102             SG_NOTIMPLEMENTED;
02103     } // from_class SIMPLE
02104 
02105     else if (strmatch(from_class, "SPARSE"))
02106     {
02107         if (strmatch(from_type, "REAL"))
02108         {
02109             if (strmatch(to_class, "SIMPLE") &&
02110                 strmatch(to_type, "REAL"))
02111             {
02112                 result=ui_features->convert_sparse_real_to_simple_real(
02113                     (CSparseFeatures<float64_t>*) features);
02114             }
02115             else
02116                 SG_NOTIMPLEMENTED;
02117         } // from_type REAL
02118         else
02119             SG_NOTIMPLEMENTED;
02120     } // from_class SPARSE
02121 
02122     else if (strmatch(from_class, "STRING"))
02123     {
02124         if (strmatch(from_type, "CHAR"))
02125         {
02126             if (strmatch(to_class, "STRING"))
02127             {
02128                 int32_t order=1;
02129                 int32_t start=0;
02130                 int32_t gap=0;
02131                 char rev='f';
02132 
02133                 if (m_nrhs>6)
02134                 {
02135                     order=get_int_from_int_or_str();
02136 
02137                     if (m_nrhs>7)
02138                     {
02139                         start=get_int_from_int_or_str();
02140 
02141                         if (m_nrhs>8)
02142                         {
02143                             gap=get_int_from_int_or_str();
02144 
02145                             if (m_nrhs>9)
02146                             {
02147                                 char* rev_str=get_str_from_str_or_direct(len);
02148                                 if (rev_str)
02149                                     rev=rev_str[0];
02150 
02151                                 SG_FREE(rev_str);
02152                             }
02153                         }
02154                     }
02155                 }
02156 
02157                 if (strmatch(to_type, "BYTE"))
02158                 {
02159                         result=ui_features->convert_string_char_to_string_generic<char,uint8_t>(
02160                         (CStringFeatures<char>*) features, order, start,
02161                         gap, rev);
02162                 }
02163                 else if (strmatch(to_type, "WORD"))
02164                 {
02165                         result=ui_features->convert_string_char_to_string_generic<char,uint16_t>(
02166                         (CStringFeatures<char>*) features, order, start,
02167                         gap, rev);
02168                 }
02169                 else if (strmatch(to_type, "ULONG"))
02170                 {
02171                     result=ui_features->convert_string_char_to_string_generic<char,uint64_t>(
02172                     (CStringFeatures<char>*) features, order, start,
02173                         gap, rev);
02174                 }
02175                 else
02176                     SG_NOTIMPLEMENTED;
02177             }
02178             else
02179                 SG_NOTIMPLEMENTED;
02180         } // from_type CHAR
02181 
02182         else if (strmatch(from_type, "BYTE"))
02183         {
02184             if (strmatch(to_class, "STRING"))
02185             {
02186                 int32_t order=1;
02187                 int32_t start=0;
02188                 int32_t gap=0;
02189                 char rev='f';
02190 
02191                 if (m_nrhs>6)
02192                 {
02193                     order=get_int_from_int_or_str();
02194 
02195                     if (m_nrhs>7)
02196                     {
02197                         start=get_int_from_int_or_str();
02198 
02199                         if (m_nrhs>8)
02200                         {
02201                             gap=get_int_from_int_or_str();
02202 
02203                             if (m_nrhs>9)
02204                             {
02205                                 char* rev_str=get_str_from_str_or_direct(len);
02206                                 if (rev_str)
02207                                     rev=rev_str[0];
02208 
02209                                 SG_FREE(rev_str);
02210                             }
02211                         }
02212                     }
02213                 }
02214 
02215                 if (strmatch(to_type, "WORD"))
02216                 {
02217                     result=ui_features->convert_string_char_to_string_generic<uint8_t,uint16_t>(
02218                         (CStringFeatures<uint8_t>*) features, order, start,
02219                         gap, rev);
02220                 }
02221                 else if (strmatch(to_type, "ULONG"))
02222                 {
02223                     result=ui_features->convert_string_char_to_string_generic<uint8_t,uint64_t>(
02224                         (CStringFeatures<uint8_t>*) features, order, start,
02225                         gap, rev);
02226                 }
02227                 else
02228                     SG_NOTIMPLEMENTED;
02229             }
02230             else
02231                 SG_NOTIMPLEMENTED;
02232         } // from_type uint8_t
02233 
02234         else if (strmatch(from_type, "WORD"))
02235         {
02236             if (strmatch(to_class, "SIMPLE") && strmatch(to_type, "TOP"))
02237             {
02238                 result=ui_features->convert_string_word_to_simple_top(
02239                     (CStringFeatures<uint16_t>*) features);
02240             }
02241             else if (strmatch(to_class, "SPEC") && strmatch(to_type, "WORD") && m_nrhs==7)
02242             {
02243                 bool use_norm=get_bool();
02244                 result=ui_features->convert_string_byte_to_spec_word((CStringFeatures<uint16_t>*) features, use_norm);
02245 
02246             }
02247             else
02248                 SG_NOTIMPLEMENTED;
02249         } // from_type WORD
02250 
02251         else if (strmatch(to_class, "SIMPLE") && strmatch(to_type, "FK"))
02252         {
02253             result=ui_features->convert_string_word_to_simple_fk(
02254                 (CStringFeatures<uint16_t>*) features);
02255         } // to_type FK
02256 
02257         else
02258             SG_NOTIMPLEMENTED;
02259 
02260     } // from_class STRING
02261 
02262     if (result && ui_features->set_convert_features(result, target))
02263         SG_INFO("Conversion was successful.\n");
02264     else
02265         SG_ERROR("Conversion failed.\n");
02266 
02267     SG_FREE(target);
02268     SG_FREE(from_class);
02269     SG_FREE(from_type);
02270     SG_FREE(to_class);
02271     SG_FREE(to_type);
02272     return (result!=NULL);
02273 }
02274 
02275 void CSGInterface::convert_to_bitembedding(CFeatures* &features, bool convert_to_word, bool convert_to_ulong)
02276 {
02277     int32_t order=1;
02278     int32_t start=0;
02279     int32_t gap=0;
02280     char rev='f';
02281 
02282     if (m_nrhs<5)
02283         return;
02284 
02285     order=get_int();
02286     // remove arg, for parameters to come
02287     m_nrhs--;
02288 
02289     if (convert_to_word)
02290     {
02291         SG_INFO("Converting into word-bitembedding\n");
02292         features=ui_features->convert_string_char_to_string_generic<char,uint16_t>(
02293                 (CStringFeatures<char>*) features, order, start, gap, rev);
02294     }
02295 
02296     if (convert_to_ulong)
02297     {
02298         SG_INFO("Converting into ulong-bitembedding\n");
02299         features=ui_features->convert_string_char_to_string_generic<char,uint64_t>(
02300                 (CStringFeatures<char>*) features, order, start, gap, rev);
02301     }
02302 }
02303 
02304 void CSGInterface::obtain_from_single_string(CFeatures* features)
02305 {
02306     if (m_nrhs<5)
02307         return;
02308 
02309     int32_t len=0;
02310     char* str=get_string(len);
02311     ASSERT(str);
02312 
02313     if (strmatch(str, "from_position_list"))
02314     {
02315         obtain_from_position_list(features);
02316     }
02317     else if (strmatch(str, "slide_window"))
02318     {
02319         obtain_by_sliding_window(features);
02320     }
02321     else
02322         SG_SERROR("Unknown conversion\n");
02323 }
02324 
02325 bool CSGInterface::obtain_from_position_list(CFeatures* features)
02326 {
02327     int32_t winsize=get_int();
02328 
02329     int32_t* shifts=NULL;
02330     int32_t num_shift=0;
02331     get_vector(shifts, num_shift);
02332 
02333     int32_t skip=0;
02334     if (m_nrhs==8)
02335         skip=get_int();
02336 
02337     SG_DEBUG("winsize: %d num_shifts: %d skip: %d\n", winsize, num_shift, skip);
02338 
02339     CDynamicArray<int32_t> positions(num_shift+1);
02340 
02341     for (int32_t i=0; i<num_shift; i++)
02342         positions.set_element(shifts[i], i);
02343 
02344     if (features->get_feature_class()!=C_STRING)
02345         SG_ERROR("No string features.\n");
02346 
02347     bool success=false;
02348     switch (features->get_feature_type())
02349     {
02350         case F_CHAR:
02351         {
02352             success=(((CStringFeatures<char>*) features)->
02353                 obtain_by_position_list(winsize, &positions, skip)>0);
02354             break;
02355         }
02356         case F_BYTE:
02357         {
02358             success=(((CStringFeatures<uint8_t>*) features)->
02359                 obtain_by_position_list(winsize, &positions, skip)>0);
02360             break;
02361         }
02362         case F_WORD:
02363         {
02364             success=(((CStringFeatures<uint16_t>*) features)->
02365                 obtain_by_position_list(winsize, &positions, skip)>0);
02366             break;
02367         }
02368         case F_ULONG:
02369         {
02370             success=(((CStringFeatures<uint64_t>*) features)->
02371                 obtain_by_position_list(winsize, &positions, skip)>0);
02372             break;
02373         }
02374         default:
02375             SG_ERROR("Unsupported string features type.\n");
02376     }
02377 
02378     return success;
02379 }
02380 
02381 bool CSGInterface::obtain_by_sliding_window(CFeatures* features)
02382 {
02383     int32_t winsize=get_int();
02384     int32_t shift=get_int();
02385     int32_t skip=0;
02386 
02387     if (m_nrhs==8)
02388         skip=get_int();
02389 
02390     bool success=false;
02391 
02392     ASSERT(features);
02393     ASSERT(((CFeatures*) features)->get_feature_class()==C_STRING);
02394 
02395     switch (features->get_feature_type())
02396     {
02397         case F_CHAR:
02398             return ( ((CStringFeatures<char>*) features)->obtain_by_sliding_window(winsize, shift, skip)>0);
02399         case F_BYTE:
02400             return ( ((CStringFeatures<uint8_t>*) features)->obtain_by_sliding_window(winsize, shift, skip)>0);
02401         case F_WORD:
02402             return ( ((CStringFeatures<uint16_t>*) features)->obtain_by_sliding_window(winsize, shift, skip)>0);
02403         case F_ULONG:
02404             return ( ((CStringFeatures<uint64_t>*) features)->obtain_by_sliding_window(winsize, shift, skip)>0);
02405         default:
02406             SG_SERROR("Unsupported string features type.\n");
02407             return false;
02408     }
02409 
02410     return success;
02411 }
02412 
02413 bool CSGInterface::cmd_reshape()
02414 {
02415     if (m_nrhs<4 || !create_return_values(0))
02416         return false;
02417 
02418     int32_t len=0;
02419     char* target=get_str_from_str_or_direct(len);
02420     int32_t num_feat=get_int_from_int_or_str();
02421     int32_t num_vec=get_int_from_int_or_str();
02422 
02423     bool success=ui_features->reshape(target, num_feat, num_vec);
02424 
02425     SG_FREE(target);
02426     return success;
02427 }
02428 
02429 bool CSGInterface::cmd_load_labels()
02430 {
02431     if (m_nrhs<4 || !create_return_values(0))
02432         return false;
02433 
02434     int32_t len=0;
02435     char* filename=get_str_from_str_or_direct(len);
02436     char* target=get_str_from_str_or_direct(len);
02437 
02438     bool success=ui_labels->load(filename, target);
02439 
02440     SG_FREE(filename);
02441     SG_FREE(target);
02442     return success;
02443 }
02444 
02445 bool CSGInterface::cmd_set_labels()
02446 {
02447     if (m_nrhs!=3 || !create_return_values(0))
02448         return false;
02449 
02450     int32_t tlen=0;
02451     char* target=get_string(tlen);
02452     if (!strmatch(target, "TRAIN") && !strmatch(target, "TEST"))
02453     {
02454         SG_FREE(target);
02455         SG_ERROR("Unknown target, neither TRAIN nor TEST.\n");
02456     }
02457 
02458     float64_t* lab=NULL;
02459     int32_t len=0;
02460     get_vector(lab, len);
02461 
02462     CLabels* labels=new CLabels(len);
02463     SG_INFO("num labels: %d\n", labels->get_num_labels());
02464 
02465     for (int32_t i=0; i<len; i++)
02466     {
02467         if (!labels->set_label(i, lab[i]))
02468             SG_ERROR("Couldn't set label %d (of %d): %f.\n", i, len, lab[i]);
02469     }
02470     SG_FREE(lab);
02471 
02472     if (strmatch(target, "TRAIN"))
02473         ui_labels->set_train_labels(labels);
02474     else if (strmatch(target, "TEST"))
02475         ui_labels->set_test_labels(labels);
02476     else
02477     {
02478         SG_FREE(target);
02479         SG_ERROR("Unknown target, neither TRAIN nor TEST.\n");
02480     }
02481     SG_FREE(target);
02482 
02483     return true;
02484 }
02485 
02486 bool CSGInterface::cmd_get_labels()
02487 {
02488     if (m_nrhs!=2 || !create_return_values(1))
02489         return false;
02490 
02491     int32_t tlen=0;
02492     char* target=get_string(tlen);
02493     CLabels* labels=NULL;
02494 
02495     if (strmatch(target, "TRAIN"))
02496         labels=ui_labels->get_train_labels();
02497     else if (strmatch(target, "TEST"))
02498         labels=ui_labels->get_test_labels();
02499     else
02500     {
02501         SG_FREE(target);
02502         SG_ERROR("Unknown target, neither TRAIN nor TEST.\n");
02503     }
02504     SG_FREE(target);
02505 
02506     if (!labels)
02507         SG_ERROR("No labels.\n");
02508 
02509     int32_t num_labels=labels->get_num_labels();
02510     float64_t* lab=SG_MALLOC(float64_t, num_labels);
02511 
02512     for (int32_t i=0; i<num_labels ; i++)
02513         lab[i]=labels->get_label(i);
02514 
02515     set_vector(lab, num_labels);
02516     SG_FREE(lab);
02517 
02518     return true;
02519 }
02520 
02521 
02524 bool CSGInterface::cmd_set_kernel_normalization()
02525 {
02526     if (m_nrhs<2 || !create_return_values(0))
02527         return false;
02528 
02529     int32_t len=0;
02530     char* normalization=get_string(len);
02531 
02532     float64_t c=0;
02533     float64_t r=0;
02534 
02535     if (m_nrhs>=3)
02536         c=get_real();
02537     if (m_nrhs>=4)
02538         r=get_real();
02539 
02540     bool success=ui_kernel->set_normalization(normalization, c, r);
02541 
02542     SG_FREE(normalization);
02543     return success;
02544 }
02545 
02546 bool CSGInterface::cmd_set_kernel()
02547 {
02548     if (m_nrhs<2 || !create_return_values(0))
02549         return false;
02550 
02551     SG_DEBUG("SGInterface: set_kernel\n");
02552     CKernel* kernel=create_kernel();
02553     return ui_kernel->set_kernel(kernel);
02554 }
02555 
02556 bool CSGInterface::cmd_add_kernel()
02557 {
02558     if (m_nrhs<3 || !create_return_values(0))
02559         return false;
02560 
02561     float64_t weight=get_real_from_real_or_str();
02562     // adjust m_nrhs to play well with checks in create_kernel
02563     m_nrhs--;
02564     CKernel* kernel=create_kernel();
02565 
02566     SG_DEBUG("SGInterface: add_kernel\n");
02567     return ui_kernel->add_kernel(kernel, weight);
02568 }
02569 
02570 bool CSGInterface::cmd_del_last_kernel()
02571 {
02572     if (m_nrhs<1 || !create_return_values(0))
02573         return false;
02574 
02575     return ui_kernel->del_last_kernel();
02576 }
02577 
02578 CKernel* CSGInterface::create_kernel()
02579 {
02580     CKernel* kernel=NULL;
02581     int32_t len=0;
02582     char* type=get_str_from_str_or_direct(len);
02583 
02584     SG_DEBUG("set_kernel with type: %s\n", type);
02585 
02586     if (strmatch(type, "COMBINED"))
02587     {
02588         if (m_nrhs<3)
02589             return NULL;
02590 
02591         int32_t size=get_int_from_int_or_str();
02592         bool append_subkernel_weights=false;
02593         if (m_nrhs>3)
02594             append_subkernel_weights=get_bool_from_bool_or_str();
02595 
02596         kernel=ui_kernel->create_combined(size, append_subkernel_weights);
02597     }
02598     else if (strmatch(type, "DISTANCE"))
02599     {
02600         if (m_nrhs<3)
02601             return NULL;
02602 
02603         int32_t size=get_int_from_int_or_str();
02604         float64_t width=1;
02605         if (m_nrhs>3)
02606             width=get_real_from_real_or_str();
02607 
02608         kernel=ui_kernel->create_distance(size, width);
02609     }
02610     else if (strmatch(type, "WAVELET"))
02611     {
02612          
02613                 if (m_nrhs<4)
02614                         return NULL;
02615 
02616                 char* dtype=get_str_from_str_or_direct(len);
02617                 if (strmatch(dtype, "REAL"))
02618                 {
02619                         int32_t size=get_int_from_int_or_str();
02620                         float64_t Wdilation=5.0;
02621                         float64_t Wtranslation=2.0;
02622 
02623                         if (m_nrhs>4)
02624                         {
02625                                 Wdilation=get_real_from_real_or_str();
02626 
02627                                 if (m_nrhs>5)
02628                                         Wtranslation=get_real_from_real_or_str();
02629                         }
02630 
02631                         kernel=ui_kernel->create_sigmoid(size, Wdilation, Wtranslation);
02632                 }
02633 
02634                 SG_FREE(dtype);
02635         }
02636     else if (strmatch(type, "LINEAR"))
02637     {
02638         if (m_nrhs<4)
02639             return NULL;
02640         if (m_nrhs>5)
02641             return NULL;
02642 
02643         char* dtype=get_str_from_str_or_direct(len);
02644         int32_t size=get_int_from_int_or_str();
02645         float64_t scale=-1;
02646         if (m_nrhs==5)
02647             scale=get_real_from_real_or_str();
02648 
02649         if (strmatch(dtype, "BYTE"))
02650             kernel=ui_kernel->create_linearbyte(size, scale);
02651         else if (strmatch(dtype, "WORD"))
02652             kernel=ui_kernel->create_linearword(size, scale);
02653         else if (strmatch(dtype, "CHAR"))
02654             kernel=ui_kernel->create_linearstring(size, scale);
02655         else if (strmatch(dtype, "REAL"))
02656             kernel=ui_kernel->create_linear(size, scale);
02657         else if (strmatch(dtype, "SPARSEREAL"))
02658             kernel=ui_kernel->create_sparselinear(size, scale);
02659 
02660         SG_FREE(dtype);
02661     }
02662     else if (strmatch(type, "HISTOGRAM"))
02663     {
02664         if (m_nrhs<4)
02665             return NULL;
02666 
02667         char* dtype=get_str_from_str_or_direct(len);
02668         if (strmatch(dtype, "WORD"))
02669         {
02670             int32_t size=get_int_from_int_or_str();
02671             kernel=ui_kernel->create_histogramword(size);
02672         }
02673 
02674         SG_FREE(dtype);
02675     }
02676     else if (strmatch(type, "SALZBERG"))
02677     {
02678         if (m_nrhs<4)
02679             return NULL;
02680 
02681         char* dtype=get_str_from_str_or_direct(len);
02682         if (strmatch(dtype, "WORD"))
02683         {
02684             int32_t size=get_int_from_int_or_str();
02685             kernel=ui_kernel->create_salzbergword(size);
02686         }
02687 
02688         SG_FREE(dtype);
02689     }
02690     else if (strmatch(type, "POLYMATCH"))
02691     {
02692         if (m_nrhs<4)
02693             return NULL;
02694 
02695         char* dtype=get_str_from_str_or_direct(len);
02696         int32_t size=get_int_from_int_or_str();
02697         int32_t degree=3;
02698         bool inhomogene=false;
02699         bool normalize=true;
02700 
02701         if (m_nrhs>4)
02702         {
02703             degree=get_int_from_int_or_str();
02704             if (m_nrhs>5)
02705             {
02706                 inhomogene=get_bool_from_bool_or_str();
02707                 if (m_nrhs>6)
02708                     normalize=get_bool_from_bool_or_str();
02709             }
02710         }
02711 
02712         if (strmatch(dtype, "CHAR"))
02713         {
02714             kernel=ui_kernel->create_polymatchstring(
02715                 size, degree, inhomogene, normalize);
02716         }
02717         else if (strmatch(dtype, "WORD"))
02718         {
02719             kernel=ui_kernel->create_polymatchwordstring(
02720                 size, degree, inhomogene, normalize);
02721         }
02722 
02723         SG_FREE(dtype);
02724     }
02725     else if (strmatch(type, "MATCH"))
02726     {
02727         if (m_nrhs<4)
02728             return NULL;
02729 
02730         char* dtype=get_str_from_str_or_direct(len);
02731         if (strmatch(dtype, "WORD"))
02732         {
02733             int32_t size=get_int_from_int_or_str();
02734             int32_t d=3;
02735             bool normalize=true;
02736 
02737             if (m_nrhs>4)
02738                 d=get_int_from_int_or_str();
02739             if (m_nrhs>5)
02740                 normalize=get_bool_from_bool_or_str();
02741 
02742             kernel=ui_kernel->create_matchwordstring(size, d, normalize);
02743         }
02744 
02745         SG_FREE(dtype);
02746     }
02747     else if (strmatch(type, "WEIGHTEDCOMMSTRING") || strmatch(type, "COMMSTRING"))
02748     {
02749         char* dtype=get_str_from_str_or_direct(len);
02750         int32_t size=get_int_from_int_or_str();
02751         bool use_sign=false;
02752         char* norm_str=NULL;
02753 
02754         if (m_nrhs>4)
02755         {
02756              use_sign=get_bool_from_bool_or_str();
02757 
02758              if (m_nrhs>5)
02759                 norm_str=get_str_from_str_or_direct(len);
02760         }
02761 
02762         if (strmatch(dtype, "WORD"))
02763         {
02764             if (strmatch(type, "WEIGHTEDCOMMSTRING"))
02765             {
02766                 kernel=ui_kernel->create_commstring(
02767                     size, use_sign, norm_str, K_WEIGHTEDCOMMWORDSTRING);
02768             }
02769             else if (strmatch(type, "COMMSTRING"))
02770             {
02771                 kernel=ui_kernel->create_commstring(
02772                     size, use_sign, norm_str, K_COMMWORDSTRING);
02773             }
02774         }
02775         else if (strmatch(dtype, "ULONG"))
02776         {
02777             kernel=ui_kernel->create_commstring(
02778                 size, use_sign, norm_str, K_COMMULONGSTRING);
02779         }
02780 
02781         SG_FREE(dtype);
02782         SG_FREE(norm_str);
02783     }
02784     else if (strmatch(type, "CHI2"))
02785     {
02786         if (m_nrhs<4)
02787             return NULL;
02788 
02789         char* dtype=get_str_from_str_or_direct(len);
02790         if (strmatch(dtype, "REAL"))
02791         {
02792             int32_t size=get_int_from_int_or_str();
02793             float64_t width=1;
02794 
02795             if (m_nrhs>4)
02796                 width=get_real_from_real_or_str();
02797 
02798             kernel=ui_kernel->create_chi2(size, width);
02799         }
02800 
02801         SG_FREE(dtype);
02802     }
02803     else if (strmatch(type, "FIXEDDEGREE"))
02804     {
02805         if (m_nrhs<4)
02806             return NULL;
02807 
02808         char* dtype=get_str_from_str_or_direct(len);
02809         if (strmatch(dtype, "CHAR"))
02810         {
02811             int32_t size=get_int_from_int_or_str();
02812             int32_t d=3;
02813             if (m_nrhs>4)
02814                 d=get_int_from_int_or_str();
02815 
02816             kernel=ui_kernel->create_fixeddegreestring(size, d);
02817         }
02818 
02819         SG_FREE(dtype);
02820     }
02821     else if (strmatch(type, "LOCALALIGNMENT"))
02822     {
02823         if (m_nrhs<4)
02824             return NULL;
02825 
02826         char* dtype=get_str_from_str_or_direct(len);
02827         if (strmatch(dtype, "CHAR"))
02828         {
02829             int32_t size=get_int_from_int_or_str();
02830 
02831             kernel=ui_kernel->create_localalignmentstring(size);
02832         }
02833 
02834         SG_FREE(dtype);
02835     }
02836     else if (strmatch(type, "OLIGO"))
02837     {
02838         if (m_nrhs<6)
02839             return NULL;
02840 
02841         char* dtype=get_str_from_str_or_direct(len);
02842         if (strmatch(dtype, "CHAR"))
02843         {
02844             int32_t size=get_int_from_int_or_str();
02845             int32_t k=get_int_from_int_or_str();
02846             float64_t w=get_real_from_real_or_str();
02847 
02848             kernel=ui_kernel->create_oligo(size, k, w);
02849         }
02850 
02851         SG_FREE(dtype);
02852     }
02853     else if (strmatch(type, "WEIGHTEDDEGREEPOS2") ||
02854         strmatch(type, "WEIGHTEDDEGREEPOS2_NONORM"))
02855     {
02856         if (m_nrhs<7)
02857             return NULL;
02858 
02859         char* dtype=get_str_from_str_or_direct(len);
02860         if (strmatch(dtype, "CHAR") || strmatch(dtype, "STRING"))
02861         {
02862             int32_t size=get_int_from_int_or_str();
02863             int32_t order=get_int_from_int_or_str();
02864             int32_t max_mismatch=get_int_from_int_or_str();
02865             int32_t length=get_int_from_int_or_str();
02866             int32_t* shifts=NULL;
02867             int32_t l=0;
02868             get_vector_from_int_vector_or_str(shifts, l);
02869 
02870             ASSERT(l==length);
02871 
02872             bool use_normalization=true;
02873             if (strmatch(type, "WEIGHTEDDEGREEPOS2_NONORM"))
02874                 use_normalization=false;
02875 
02876             kernel=ui_kernel->create_weighteddegreepositionstring2(
02877                 size, order, max_mismatch, shifts, length,
02878                 use_normalization);
02879 
02880             SG_FREE(shifts);
02881         }
02882 
02883         SG_FREE(dtype);
02884     }
02885     else if (strmatch(type, "WEIGHTEDDEGREEPOS3"))
02886     {
02887         if (m_nrhs<7)
02888             return NULL;
02889 
02890         char* dtype=get_str_from_str_or_direct(len);
02891         if (strmatch(dtype, "CHAR") || strmatch(dtype, "STRING"))
02892         {
02893             int32_t size=get_int_from_int_or_str();
02894             int32_t order=get_int_from_int_or_str();
02895             int32_t max_mismatch=get_int_from_int_or_str();
02896             int32_t length=get_int_from_int_or_str();
02897             int32_t mkl_stepsize=get_int_from_int_or_str();
02898             int32_t* shifts=NULL;
02899             int32_t l=0;
02900             get_vector_from_int_vector_or_str(shifts, l);
02901             ASSERT(l==length);
02902 
02903             float64_t* position_weights=NULL;
02904             if (m_nrhs>9+length)
02905             {
02906                 get_vector_from_real_vector_or_str(
02907                     position_weights, length);
02908             }
02909 
02910             kernel=ui_kernel->create_weighteddegreepositionstring3(
02911                 size, order, max_mismatch, shifts, length,
02912                 mkl_stepsize, position_weights);
02913 
02914             SG_FREE(position_weights);
02915             SG_FREE(shifts);
02916         }
02917 
02918         SG_FREE(dtype);
02919     }
02920     else if (strmatch(type, "WEIGHTEDDEGREEPOS"))
02921     {
02922         if (m_nrhs<4)
02923             return NULL;
02924 
02925         char* dtype=get_str_from_str_or_direct(len);
02926         if (strmatch(dtype, "CHAR") || strmatch(dtype, "STRING"))
02927         {
02928             int32_t size=get_int_from_int_or_str();
02929             int32_t order=3;
02930             int32_t max_mismatch=0;
02931             int32_t length=0;
02932             int32_t center=0;
02933             float64_t step=1;
02934 
02935             if (m_nrhs>4)
02936             {
02937                 order=get_int_from_int_or_str();
02938 
02939                 if (m_nrhs>5)
02940                 {
02941                     max_mismatch=get_int_from_int_or_str();
02942 
02943                     if (m_nrhs>6)
02944                     {
02945                         length=get_int_from_int_or_str();
02946 
02947                         if (m_nrhs>7)
02948                         {
02949                             center=get_int_from_int_or_str();
02950 
02951                             if (m_nrhs>8)
02952                                 step=get_real_from_real_or_str();
02953                         }
02954                     }
02955                 }
02956             }
02957 
02958             kernel=ui_kernel->create_weighteddegreepositionstring(
02959                 size, order, max_mismatch, length, center, step);
02960         }
02961 
02962         SG_FREE(dtype);
02963     }
02964     else if (strmatch(type, "WEIGHTEDDEGREE"))
02965     {
02966         if (m_nrhs<4)
02967             return NULL;
02968 
02969         char* dtype=get_str_from_str_or_direct(len);
02970         if (strmatch(dtype, "CHAR") || strmatch(dtype, "STRING"))
02971         {
02972             int32_t size=get_int_from_int_or_str();
02973             int32_t order=3;
02974             int32_t max_mismatch=0;
02975             bool use_normalization=true;
02976             int32_t mkl_stepsize=1;
02977             bool block_computation=true;
02978             int32_t single_degree=-1;
02979 
02980             if (m_nrhs>4)
02981             {
02982                 order=get_int_from_int_or_str();
02983 
02984                 if (m_nrhs>5)
02985                 {
02986                     max_mismatch=get_int_from_int_or_str();
02987 
02988                     if (m_nrhs>6)
02989                     {
02990                         use_normalization=get_bool_from_bool_or_str();
02991 
02992                         if (m_nrhs>7)
02993                         {
02994                             mkl_stepsize=get_int_from_int_or_str();
02995 
02996                             if (m_nrhs>8)
02997                             {
02998                                 block_computation=get_int_from_int_or_str();
02999 
03000                                 if (m_nrhs>9)
03001                                     single_degree=get_int_from_int_or_str();
03002                             }
03003                         }
03004                     }
03005                 }
03006             }
03007 
03008             kernel=ui_kernel->create_weighteddegreestring(
03009                 size, order, max_mismatch, use_normalization,
03010                 mkl_stepsize, block_computation, single_degree);
03011         }
03012 
03013         SG_FREE(dtype);
03014     }
03015     else if (strmatch(type, "WEIGHTEDDEGREERBF"))
03016     {
03017         if (m_nrhs<5)
03018             return NULL;
03019 
03020         char* dtype=get_str_from_str_or_direct(len);
03021         int32_t size=get_int_from_int_or_str();
03022         int32_t nof_properties=get_int_from_int_or_str();
03023         int32_t degree=1;
03024         float64_t width=1;
03025         if (m_nrhs>5)
03026         {
03027             degree=get_int_from_int_or_str();
03028             if (m_nrhs>6)
03029             {
03030                 width=get_real_from_real_or_str();
03031             }
03032 
03033         }
03034         //if (strmatch(dtype, "REAL"))
03035         
03036         kernel=ui_kernel->create_weighteddegreerbf(size, degree, nof_properties, width);
03037 
03038         SG_FREE(dtype);
03039 
03040     }
03041     else if (strmatch(type, "SPECTRUMMISMATCHRBF"))
03042     {
03043         if (m_nrhs<7)
03044             return NULL;
03045 
03046         char* dtype=get_str_from_str_or_direct(len);
03047         if (strmatch(dtype, "CHAR") || strmatch(dtype, "STRING"))
03048         {
03049             int32_t size=get_int_from_int_or_str();
03050             int32_t degree=get_int_from_int_or_str();
03051             int32_t max_mismatch=get_int_from_int_or_str();
03052             float64_t width=get_real_from_real_or_str();
03053             float64_t* AA_matrix = NULL;
03054 
03055             //int32_t length=128*128;
03056             //get_vector_from_real_vector_or_str(AA_matrix, length);
03057             float64_t* helper_matrix=NULL;
03058             int32_t N=0;
03059             int32_t M=0;
03060             get_matrix(helper_matrix, N, M);
03061 
03062             if (N == 128 && M == 128)
03063             {
03064                 AA_matrix=SG_MALLOC(float64_t, N*M);
03065                 memcpy(AA_matrix, helper_matrix, N*M*sizeof(float64_t)) ;
03066                 kernel=ui_kernel->create_spectrummismatchrbf(size, AA_matrix, max_mismatch, degree, width);
03067             }
03068             else
03069             {
03070                 SG_ERROR("Matrix size %d %d\n", N, M);
03071             }
03072         }
03073         SG_FREE(dtype);
03074 
03075     }
03076 
03077     else if (strmatch(type, "SLIK") || strmatch(type, "LIK"))
03078     {
03079         if (m_nrhs<4)
03080             return NULL;
03081 
03082         char* dtype=get_str_from_str_or_direct(len);
03083         if (strmatch(dtype, "CHAR"))
03084         {
03085             int32_t size=get_int_from_int_or_str();
03086             int32_t length=3;
03087             int32_t inner_degree=3;
03088             int32_t outer_degree=1;
03089 
03090             if (m_nrhs>4)
03091             {
03092                 length=get_int_from_int_or_str();
03093 
03094                 if (m_nrhs>5)
03095                 {
03096                     inner_degree=get_int_from_int_or_str();
03097 
03098                     if (m_nrhs>6)
03099                         outer_degree=get_int_from_int_or_str();
03100                 }
03101             }
03102 
03103             if (strmatch(type, "SLIK"))
03104             {
03105                 kernel=ui_kernel->create_localityimprovedstring(
03106                     size, length, inner_degree, outer_degree,
03107                     K_SIMPLELOCALITYIMPROVED);
03108             }
03109             else
03110             {
03111                 kernel=ui_kernel->create_localityimprovedstring(
03112                     size, length, inner_degree, outer_degree,
03113                     K_LOCALITYIMPROVED);
03114             }
03115         }
03116 
03117         SG_FREE(dtype);
03118     }
03119     else if (strmatch(type, "POLY"))
03120     {
03121         if (m_nrhs<4)
03122             return NULL;
03123 
03124         char* dtype=get_str_from_str_or_direct(len);
03125         int32_t size=get_int_from_int_or_str();
03126         int32_t degree=2;
03127         bool inhomogene=false;
03128         bool normalize=true;
03129 
03130         if (m_nrhs>4)
03131         {
03132             degree=get_int_from_int_or_str();
03133 
03134             if (m_nrhs>5)
03135             {
03136                 inhomogene=get_bool_from_bool_or_str();
03137 
03138                 if (m_nrhs>6)
03139                     normalize=get_bool_from_bool_or_str();
03140             }
03141         }
03142 
03143         if (strmatch(dtype, "REAL"))
03144         {
03145             kernel=ui_kernel->create_poly(
03146                 size, degree, inhomogene, normalize);
03147         }
03148         else if (strmatch(dtype, "SPARSEREAL"))
03149         {
03150             kernel=ui_kernel->create_sparsepoly(
03151                 size, degree, inhomogene, normalize);
03152         }
03153 
03154         SG_FREE(dtype);
03155     }
03156     else if (strmatch(type, "SIGMOID"))
03157     {
03158         if (m_nrhs<4)
03159             return NULL;
03160 
03161         char* dtype=get_str_from_str_or_direct(len);
03162         if (strmatch(dtype, "REAL"))
03163         {
03164             int32_t size=get_int_from_int_or_str();
03165             float64_t gamma=0.01;
03166             float64_t coef0=0;
03167 
03168             if (m_nrhs>4)
03169             {
03170                 gamma=get_real_from_real_or_str();
03171 
03172                 if (m_nrhs>5)
03173                     coef0=get_real_from_real_or_str();
03174             }
03175 
03176             kernel=ui_kernel->create_sigmoid(size, gamma, coef0);
03177         }
03178 
03179         SG_FREE(dtype);
03180     }
03181     else if (strmatch(type, "GAUSSIAN")) // RBF
03182     {
03183         if (m_nrhs<4)
03184             return NULL;
03185 
03186         char* dtype=get_str_from_str_or_direct(len);
03187         int32_t size=get_int_from_int_or_str();
03188         float64_t width=1;
03189         if (m_nrhs>4)
03190             width=get_real_from_real_or_str();
03191 
03192         if (strmatch(dtype, "REAL"))
03193             kernel=ui_kernel->create_gaussian(size, width);
03194         else if (strmatch(dtype, "SPARSEREAL"))
03195             kernel=ui_kernel->create_sparsegaussian(size, width);
03196 
03197         SG_FREE(dtype);
03198     }
03199     else if (strmatch(type, "GAUSSIANSHIFT")) // RBF
03200     {
03201         if (m_nrhs<7)
03202             return NULL;
03203 
03204         char* dtype=get_str_from_str_or_direct(len);
03205         if (strmatch(dtype, "REAL"))
03206         {
03207             int32_t size=get_int_from_int_or_str();
03208             float64_t width=get_real_from_real_or_str();
03209             int32_t max_shift=get_int_from_int_or_str();
03210             int32_t shift_step=get_int_from_int_or_str();
03211 
03212             kernel=ui_kernel->create_gaussianshift(
03213                 size, width, max_shift, shift_step);
03214         }
03215 
03216         SG_FREE(dtype);
03217     }
03218     else if (strmatch(type, "CUSTOM"))
03219     {
03220         if (m_nrhs!=4 || !create_return_values(0))
03221             return false;
03222 
03223         float64_t* kmatrix=NULL;
03224         int32_t num_feat=0;
03225         int32_t num_vec=0;
03226         get_matrix(kmatrix, num_feat, num_vec);
03227 
03228         int32_t tlen=0;
03229         char* ktype=get_string(tlen);
03230 
03231         if (!strmatch(ktype, "DIAG") &&
03232                 !strmatch(ktype, "FULL") &&
03233                 !strmatch(ktype, "FULL2DIAG"))
03234         {
03235             SG_FREE(ktype);
03236             SG_ERROR("Undefined type, not DIAG, FULL or FULL2DIAG.\n");
03237         }
03238 
03239         bool source_is_diag=false;
03240         bool dest_is_diag=false;
03241 
03242         if (strmatch(ktype, "FULL2DIAG"))
03243             dest_is_diag=true;
03244         else if (strmatch(ktype, "DIAG"))
03245         {
03246             source_is_diag=true;
03247             dest_is_diag=true;
03248         }
03249 
03250         kernel=ui_kernel->create_custom(kmatrix, num_feat, num_vec,
03251                 source_is_diag, dest_is_diag);
03252     }
03253     else if (strmatch(type, "CONST"))
03254     {
03255         if (m_nrhs<4)
03256             return NULL;
03257 
03258         char* dtype=get_str_from_str_or_direct(len);
03259         if (strmatch(dtype, "REAL"))
03260         {
03261             int32_t size=get_int_from_int_or_str();
03262             float64_t c=1;
03263             if (m_nrhs>4)
03264                 c=get_real_from_real_or_str();
03265 
03266             kernel=ui_kernel->create_const(size, c);
03267         }
03268 
03269         SG_FREE(dtype);
03270     }
03271     else if (strmatch(type, "DIAG"))
03272     {
03273         if (m_nrhs<4)
03274             return NULL;
03275 
03276         char* dtype=get_str_from_str_or_direct(len);
03277         if (strmatch(dtype, "REAL"))
03278         {
03279             int32_t size=get_int_from_int_or_str();
03280             float64_t diag=1;
03281             if (m_nrhs>4)
03282                 diag=get_real_from_real_or_str();
03283 
03284             kernel=ui_kernel->create_diag(size, diag);
03285         }
03286 
03287         SG_FREE(dtype);
03288     }
03289 
03290     else if (strmatch(type, "TPPK"))
03291     {
03292         if (m_nrhs!=5)
03293             return NULL;
03294 
03295         char* dtype=get_str_from_str_or_direct(len);
03296         if (strmatch(dtype, "INT"))
03297         {
03298             int32_t size=get_int_from_int_or_str();
03299             float64_t* km=NULL;
03300             int32_t rows=0;
03301             int32_t cols=0;
03302             get_matrix(km, rows, cols);
03303             kernel=ui_kernel->create_tppk(size, km, rows, cols);
03304         }
03305 
03306         SG_FREE(dtype);
03307     }
03308     else
03309         SG_NOTIMPLEMENTED;
03310 
03311     SG_FREE(type);
03312     SG_DEBUG("created kernel: %p\n", kernel);
03313     return kernel;
03314 }
03315 
03316 
03317 CFeatures* CSGInterface::create_custom_string_features(CStringFeatures<uint8_t>* orig_feat)
03318 {
03319     CFeatures* feat=orig_feat;
03320 
03321     if (m_nrhs>4)
03322     {
03323         int32_t start=-1;
03324         int32_t order=0;
03325         int32_t from_order=0;
03326         bool normalize=true;
03327 
03328         int32_t feature_class_len=0;
03329         char* feature_class_str=get_string(feature_class_len);
03330         ASSERT(feature_class_str);
03331         CAlphabet* alphabet=NULL;
03332         if (strmatch(feature_class_str, "WD"))
03333         {
03334             if (m_nrhs!=7)
03335                 SG_ERROR("Please specify alphabet, WD, order, from_order\n");
03336 
03337             alphabet=new CAlphabet(RAWDNA);
03338             order=get_int();
03339             from_order=get_int();
03340             feat = new CWDFeatures((CStringFeatures<uint8_t>*) feat, order, from_order);
03341         }
03342         else if (strmatch(feature_class_str, "WSPEC"))
03343         {
03344             if (m_nrhs!=8)
03345                 SG_ERROR("Please specify alphabet, order, WSPEC, start, normalize\n");
03346 
03347             alphabet=new CAlphabet(RAWDNA);
03348             order=get_int();
03349             start=get_int();
03350             normalize=get_bool();
03351             CStringFeatures<uint16_t>* sf=new CStringFeatures<uint16_t>(RAWDNA);
03352             sf->obtain_from_char_features((CStringFeatures<uint8_t>*) feat, start, order, 0, normalize);
03353             sf->add_preprocessor(new CSortWordString());
03354             sf->apply_preprocessor();
03355             SG_UNREF(feat);
03356             feat = new CImplicitWeightedSpecFeatures(sf, normalize);
03357         }
03358         SG_FREE(feature_class_str);
03359 
03360         SG_UNREF(alphabet);
03361     }
03362 
03363     return feat;
03364 }
03365 
03366 CFeatures* CSGInterface::create_custom_real_features(CSimpleFeatures<float64_t>* orig_feat)
03367 {
03368     CFeatures* feat=orig_feat;
03369 
03370     if (m_nrhs==6)
03371     {
03372         int32_t degree=0;
03373         int32_t feature_class_len=0;
03374         bool normalize;
03375         char* feature_class_str=get_string(feature_class_len);
03376         ASSERT(feature_class_str);
03377         if (strmatch(feature_class_str, "POLY"))
03378         {
03379             //if (m_nrhs!=7)
03380             //  SG_ERROR("Please specify POLY, degree\n");
03381 
03382             degree=get_int();
03383             normalize = get_bool();
03384             feat = new CPolyFeatures((CSimpleFeatures<float64_t>*) feat, degree, normalize);
03385 
03386         }
03387         else    
03388             SG_ERROR("Unknown feature class: %s\n", feature_class_str);
03389 
03390         SG_FREE(feature_class_str);
03391     }
03392 
03393     return feat;
03394 }
03395 
03396 bool CSGInterface::cmd_init_kernel()
03397 {
03398     SG_DEPRECATED;
03399     return true;
03400 }
03401 
03402 bool CSGInterface::cmd_clean_kernel()
03403 {
03404     if (m_nrhs<1 || !create_return_values(0))
03405         return false;
03406 
03407     return ui_kernel->clean_kernel();
03408 }
03409 
03410 bool CSGInterface::cmd_save_kernel()
03411 {
03412     if (m_nrhs<2 || !create_return_values(0))
03413         return false;
03414 
03415     int32_t len=0;
03416     char* filename=get_str_from_str_or_direct(len);
03417 
03418     bool success=ui_kernel->save_kernel(filename);
03419 
03420     SG_FREE(filename);
03421     return success;
03422 }
03423 
03424 bool CSGInterface::cmd_get_kernel_matrix()
03425 {
03426     if (m_nrhs>2 || !create_return_values(1))
03427         return false;
03428 
03429     int32_t len=0;
03430     char* target=NULL;
03431     
03432     if (m_nrhs==2)
03433         target=get_string(len);
03434     bool success=ui_kernel->init_kernel(target);
03435 
03436     if (success)
03437     {
03438         CKernel* kernel=ui_kernel->get_kernel();
03439         if (!kernel || !kernel->has_features())
03440             SG_ERROR("No kernel defined or not initialized.\n");
03441 
03442         SGMatrix<float64_t> km=kernel->get_kernel_matrix<float64_t>();
03443         set_matrix(km.matrix, km.num_rows, km.num_cols);
03444     }
03445 
03446     SG_FREE(target);
03447 
03448     return success;
03449 }
03450 
03451 bool CSGInterface::cmd_set_WD_position_weights()
03452 {
03453     if (m_nrhs<2 || m_nrhs>3 || !create_return_values(0))
03454         return false;
03455 
03456     CKernel* kernel=ui_kernel->get_kernel();
03457     if (!kernel)
03458         SG_ERROR("No kernel.\n");
03459 
03460     if (kernel->get_kernel_type()==K_COMBINED)
03461     {
03462         kernel=((CCombinedKernel*) kernel)->get_last_kernel();
03463         if (!kernel)
03464             SG_ERROR("No last kernel.\n");
03465 
03466         EKernelType ktype=kernel->get_kernel_type();
03467         if (ktype!=K_WEIGHTEDDEGREE && ktype!=K_WEIGHTEDDEGREEPOS)
03468             SG_ERROR("Unsupported kernel.\n");
03469     }
03470 
03471     bool success=false;
03472     float64_t* weights=NULL;
03473     int32_t dim=0;
03474     int32_t len=0;
03475     get_matrix(weights, dim, len);
03476 
03477     if (kernel->get_kernel_type()==K_WEIGHTEDDEGREE)
03478     {
03479         CWeightedDegreeStringKernel* k=
03480             (CWeightedDegreeStringKernel*) kernel;
03481 
03482         if (dim!=1 && len>0)
03483             SG_ERROR("Dimension mismatch (should be 1 x seq_length or 0x0\n");
03484 
03485         ui_kernel->init_kernel("TRAIN");
03486         success=k->set_position_weights(weights, len);
03487     }
03488     else
03489     {
03490         CWeightedDegreePositionStringKernel* k=
03491             (CWeightedDegreePositionStringKernel*) kernel;
03492         char* target=NULL;
03493         bool is_train=true;
03494 
03495         if (m_nrhs==3)
03496         {
03497             int32_t tlen=0;
03498             target=get_string(tlen);
03499             if (!target)
03500             {
03501                 SG_FREE(weights);
03502                 SG_ERROR("Couldn't find second argument to method.\n");
03503             }
03504 
03505             if (!strmatch(target, "TRAIN") && !strmatch(target, "TEST"))
03506             {
03507                 SG_FREE(target);
03508                 SG_ERROR("Second argument none of TRAIN or TEST.\n");
03509             }
03510 
03511             if (strmatch(target, "TEST"))
03512                 is_train=false;
03513         }
03514         
03515         if (dim!=1 && len>0)
03516         {
03517             SG_FREE(target);
03518             SG_ERROR("Dimension mismatch (should be 1 x seq_length or 0x0\n");
03519             }
03520 
03521         if (dim==0 && len==0)
03522         {
03523             if (create_return_values(3))
03524             {
03525                 if (is_train)
03526                     success=k->delete_position_weights_lhs();
03527                 else
03528                     success=k->delete_position_weights_rhs();
03529             }
03530             else
03531                 success=k->delete_position_weights();
03532         }
03533         else
03534         {
03535             if (create_return_values(3))
03536             {
03537                 if (is_train)
03538                     success=k->set_position_weights_lhs(weights, dim, len);
03539                 else
03540                     success=k->set_position_weights_rhs(weights, dim, len);
03541             }
03542             else
03543             {
03544                 ui_kernel->init_kernel("TRAIN");
03545                 k->set_position_weights(SGVector<float64_t>(weights, len));
03546                 success=true;
03547             }
03548         }
03549 
03550         SG_FREE(target);
03551     }
03552 
03553     return success;
03554 }
03555 
03556 bool CSGInterface::cmd_get_subkernel_weights()
03557 {
03558     if (m_nrhs!=1 || !create_return_values(1))
03559         return false;
03560 
03561     CKernel *kernel=ui_kernel->get_kernel();
03562     if (!kernel)
03563         SG_ERROR("Invalid kernel.\n");
03564 
03565     EKernelType ktype=kernel->get_kernel_type();
03566     const float64_t* weights=NULL;
03567 
03568     if (ktype==K_COMBINED)
03569     {
03570         int32_t num_weights=-1;
03571         weights=((CCombinedKernel *) kernel)->get_subkernel_weights(num_weights);
03572 
03573         // matrices of shape 1 x num_weight are returned
03574         set_matrix(weights, 1, num_weights);
03575         return true;
03576     }
03577 
03578     int32_t degree=-1;
03579     int32_t length=-1;
03580 
03581     if (ktype==K_WEIGHTEDDEGREE)
03582     {
03583         weights=((CWeightedDegreeStringKernel *) kernel)->
03584             get_degree_weights(degree, length);
03585     }
03586     else if (ktype==K_WEIGHTEDDEGREEPOS)
03587     {
03588         weights=((CWeightedDegreePositionStringKernel *) kernel)->
03589             get_degree_weights(degree, length);
03590     }
03591     else
03592         SG_ERROR("Setting subkernel weights not supported on this kernel.\n");
03593 
03594     if (length==0)
03595         length=1;
03596 
03597     set_matrix(weights, degree, length);
03598     return true;
03599 }
03600 
03601 bool CSGInterface::cmd_set_subkernel_weights()
03602 {
03603     if (m_nrhs!=2 || !create_return_values(0))
03604         return false;
03605 
03606     CKernel* kernel=ui_kernel->get_kernel();
03607     if (!kernel)
03608         SG_ERROR("No kernel.\n");
03609 
03610     bool success=false;
03611     float64_t* weights=NULL;
03612     int32_t dim=0;
03613     int32_t len=0;
03614     get_matrix(weights, dim, len);
03615 
03616     EKernelType ktype=kernel->get_kernel_type();
03617     if (ktype==K_WEIGHTEDDEGREE)
03618     {
03619         CWeightedDegreeStringKernel* k=
03620             (CWeightedDegreeStringKernel*) kernel;
03621         int32_t degree=k->get_degree();
03622         if (dim!=degree || len<1)
03623             SG_ERROR("WD: Dimension mismatch (should be (seq_length | 1) x degree) got (%d x %d)\n", len, degree);
03624 
03625         if (len==1)
03626             len=0;
03627 
03628         success=k->set_weights(SGMatrix<float64_t>(weights, dim, len));
03629     }
03630     else if (ktype==K_WEIGHTEDDEGREEPOS)
03631     {
03632         CWeightedDegreePositionStringKernel* k=
03633             (CWeightedDegreePositionStringKernel*) kernel;
03634         int32_t degree=k->get_degree();
03635         if (dim!=degree || len<1)
03636             SG_ERROR("WDPos: Dimension mismatch (should be (seq_length | 1) x degree) got (%d x %d)\n", len, degree);
03637 
03638         if (len==1)
03639             len=0;
03640 
03641         success=k->set_weights(SGMatrix<float64_t>(weights, dim, len));
03642     }
03643     else // all other kernels
03644     {
03645         int32_t num_subkernels=kernel->get_num_subkernels();
03646         if (dim!=1 || len!=num_subkernels)
03647             SG_ERROR("All: Dimension mismatch (should be 1 x num_subkernels)\n");
03648 
03649         kernel->set_subkernel_weights(SGVector<float64_t>(weights, len));
03650         success=true;
03651     }
03652 
03653     return success;
03654 }
03655 
03656 bool CSGInterface::cmd_set_subkernel_weights_combined()
03657 {
03658     if (m_nrhs!=3 || !create_return_values(0))
03659         return false;
03660 
03661     CKernel* kernel=ui_kernel->get_kernel();
03662     if (!kernel)
03663         SG_ERROR("No kernel.\n");
03664     if (kernel->get_kernel_type()!=K_COMBINED)
03665         SG_ERROR("Only works for combined kernels.\n");
03666 
03667     bool success=false;
03668     float64_t* weights=NULL;
03669     int32_t dim=0;
03670     int32_t len=0;
03671     get_matrix(weights, dim, len);
03672 
03673     int32_t idx=get_int();
03674     SG_DEBUG("using kernel_idx=%i\n", idx);
03675 
03676     kernel=((CCombinedKernel*) kernel)->get_kernel(idx);
03677     if (!kernel)
03678         SG_ERROR("No subkernel at idx %d.\n", idx);
03679 
03680     EKernelType ktype=kernel->get_kernel_type();
03681     if (ktype==K_WEIGHTEDDEGREE)
03682     {
03683         CWeightedDegreeStringKernel* k=
03684             (CWeightedDegreeStringKernel*) kernel;
03685         int32_t degree=k->get_degree();
03686         if (dim!=degree || len<1)
03687             SG_ERROR("Dimension mismatch (should be de(seq_length | 1) x degree)\n");
03688 
03689         if (len==1)
03690             len=0;
03691 
03692         success=k->set_weights(SGMatrix<float64_t>(weights, dim, len));
03693     }
03694     else if (ktype==K_WEIGHTEDDEGREEPOS)
03695     {
03696         CWeightedDegreePositionStringKernel* k=
03697             (CWeightedDegreePositionStringKernel*) kernel;
03698         int32_t degree=k->get_degree();
03699         if (dim!=degree || len<1)
03700             SG_ERROR("Dimension mismatch (should be de(seq_length | 1) x degree)\n");
03701 
03702         if (len==1)
03703             len=0;
03704 
03705         success=k->set_weights(SGMatrix<float64_t>(weights, dim, len));
03706     }
03707     else // all other kernels
03708     {
03709         int32_t num_subkernels=kernel->get_num_subkernels();
03710         if (dim!=1 || len!=num_subkernels)
03711             SG_ERROR("Dimension mismatch (should be 1 x num_subkernels)\n");
03712 
03713         kernel->set_subkernel_weights(SGVector<float64_t>(weights, len));
03714         success=true;
03715     }
03716 
03717     return success;
03718 }
03719 
03720 bool CSGInterface::cmd_get_dotfeature_weights_combined()
03721 {
03722     if (m_nrhs!=2 || !create_return_values(1))
03723         return false;
03724 
03725     int32_t tlen=0;
03726     char* target=get_string(tlen);
03727     CFeatures* features=NULL;
03728 
03729     if (strmatch(target, "TRAIN"))
03730         features=ui_features->get_train_features();
03731     else if (strmatch(target, "TEST"))
03732         features=ui_features->get_test_features();
03733     else
03734     {
03735         SG_FREE(target);
03736         SG_ERROR("Unknown target, neither TRAIN nor TEST.\n");
03737     }
03738     SG_FREE(target);
03739 
03740     if (!features)
03741         SG_ERROR("No features.\n");
03742     if (features->get_feature_class()!=C_COMBINED_DOT)
03743         SG_ERROR("Only works for combined dot features.\n");
03744 
03745     float64_t* weights=NULL;
03746     int32_t len=0;
03747     ((CCombinedDotFeatures*) features)->get_subfeature_weights(&weights, &len);
03748     set_vector(weights, len);
03749     SG_FREE(weights);
03750 
03751     return true;
03752 }
03753 
03754 bool CSGInterface::cmd_set_dotfeature_weights_combined()
03755 {
03756     if (m_nrhs!=3 || !create_return_values(0))
03757         return false;
03758 
03759     int32_t tlen=0;
03760     char* target=get_string(tlen);
03761     CFeatures* features=NULL;
03762 
03763     if (strmatch(target, "TRAIN"))
03764         features=ui_features->get_train_features();
03765     else if (strmatch(target, "TEST"))
03766         features=ui_features->get_test_features();
03767     else
03768     {
03769         SG_FREE(target);
03770         SG_ERROR("Unknown target, neither TRAIN nor TEST.\n");
03771     }
03772     SG_FREE(target);
03773 
03774     if (!features)
03775         SG_ERROR("No features.\n");
03776     if (features->get_feature_class()!=C_COMBINED_DOT)
03777         SG_ERROR("Only works for combined dot features.\n");
03778 
03779     float64_t* weights=NULL;
03780     int32_t dim=0;
03781     int32_t len=0;
03782     get_matrix(weights, dim, len);
03783 
03784     ((CCombinedDotFeatures*) features)->set_subfeature_weights(weights, len);
03785 
03786     return true;
03787 }
03788 
03789 bool CSGInterface::cmd_set_last_subkernel_weights()
03790 {
03791     if (m_nrhs!=2 || !create_return_values(0))
03792         return false;
03793 
03794     CKernel* kernel=ui_kernel->get_kernel();
03795     if (!kernel)
03796         SG_ERROR("No kernel.\n");
03797     if (kernel->get_kernel_type()!=K_COMBINED)
03798         SG_ERROR("Only works for Combined kernels.\n");
03799 
03800     kernel=((CCombinedKernel*) kernel)->get_last_kernel();
03801     if (!kernel)
03802         SG_ERROR("No last kernel.\n");
03803 
03804     bool success=false;
03805     float64_t* weights=NULL;
03806     int32_t dim=0;
03807     int32_t len=0;
03808     get_matrix(weights, dim, len);
03809 
03810     EKernelType ktype=kernel->get_kernel_type();
03811     if (ktype==K_WEIGHTEDDEGREE)
03812     {
03813         CWeightedDegreeStringKernel* k=(CWeightedDegreeStringKernel*) kernel;
03814         if (dim!=k->get_degree() || len<1)
03815             SG_ERROR("Dimension mismatch (should be de(seq_length | 1) x degree)\n");
03816 
03817         if (len==1)
03818             len=0;
03819 
03820         success=k->set_weights(SGMatrix<float64_t>(weights, dim, len));
03821     }
03822     else if (ktype==K_WEIGHTEDDEGREEPOS)
03823     {
03824         CWeightedDegreePositionStringKernel* k=
03825             (CWeightedDegreePositionStringKernel*) kernel;
03826         if (dim!=k->get_degree() || len<1)
03827             SG_ERROR("Dimension mismatch (should be de(seq_length | 1) x degree)\n");
03828 
03829         if (len==1)
03830             len=0;
03831 
03832         success=k->set_weights(SGMatrix<float64_t>(weights, dim, len));
03833     }
03834     else // all other kernels
03835     {
03836         int32_t num_subkernels=kernel->get_num_subkernels();
03837         if (dim!=1 || len!=num_subkernels)
03838             SG_ERROR("Dimension mismatch (should be 1 x num_subkernels)\n");
03839 
03840         kernel->set_subkernel_weights(SGVector<float64_t>(weights, len));
03841         success=true;
03842     }
03843 
03844     return success;
03845 }
03846 
03847 bool CSGInterface::cmd_get_WD_position_weights()
03848 {
03849     if (m_nrhs!=1 || !create_return_values(1))
03850         return false;
03851 
03852     CKernel* kernel=ui_kernel->get_kernel();
03853     if (!kernel)
03854         SG_ERROR("No kernel.\n");
03855 
03856     if (kernel->get_kernel_type()==K_COMBINED)
03857     {
03858         kernel=((CCombinedKernel*) kernel)->get_last_kernel();
03859         if (!kernel)
03860             SG_ERROR("Couldn't find last kernel.\n");
03861 
03862         EKernelType ktype=kernel->get_kernel_type();
03863         if (ktype!=K_WEIGHTEDDEGREE && ktype!=K_WEIGHTEDDEGREEPOS)
03864             SG_ERROR("Wrong subkernel type.\n");
03865     }
03866 
03867     int32_t len=0;
03868     const float64_t* position_weights;
03869 
03870     if (kernel->get_kernel_type()==K_WEIGHTEDDEGREE)
03871         position_weights=((CWeightedDegreeStringKernel*) kernel)->get_position_weights(len);
03872     else
03873         position_weights=((CWeightedDegreePositionStringKernel*) kernel)->get_position_weights(len);
03874 
03875     if (position_weights==NULL)
03876         set_vector(position_weights, 0);
03877     else
03878         set_vector(position_weights, len);
03879 
03880     return true;
03881 }
03882 
03883 bool CSGInterface::cmd_get_last_subkernel_weights()
03884 {
03885     if (m_nrhs!=1 || !create_return_values(1))
03886         return false;
03887 
03888     CKernel* kernel=ui_kernel->get_kernel();
03889     EKernelType ktype=kernel->get_kernel_type();
03890     if (!kernel)
03891         SG_ERROR("No kernel.\n");
03892     if (ktype!=K_COMBINED)
03893         SG_ERROR("Only works for Combined kernels.\n");
03894 
03895     kernel=((CCombinedKernel*) kernel)->get_last_kernel();
03896     if (!kernel)
03897         SG_ERROR("Couldn't find last kernel.\n");
03898 
03899     int32_t degree=0;
03900     int32_t len=0;
03901 
03902     if (ktype==K_COMBINED)
03903     {
03904         int32_t num_weights=0;
03905         const float64_t* weights=
03906             ((CCombinedKernel*) kernel)->get_subkernel_weights(num_weights);
03907 
03908         set_vector(weights, num_weights);
03909         return true;
03910     }
03911 
03912     float64_t* weights=NULL;
03913     if (ktype==K_WEIGHTEDDEGREE)
03914         weights=((CWeightedDegreeStringKernel*) kernel)->
03915             get_degree_weights(degree, len);
03916     else if (ktype==K_WEIGHTEDDEGREEPOS)
03917         weights=((CWeightedDegreePositionStringKernel*) kernel)->
03918             get_degree_weights(degree, len);
03919     else
03920         SG_ERROR("Only works for Weighted Degree (Position) kernels.\n");
03921 
03922     if (len==0)
03923         len=1;
03924 
03925     set_matrix(weights, degree, len);
03926 
03927     return true;
03928 }
03929 
03930 bool CSGInterface::cmd_compute_by_subkernels()
03931 {
03932     if (m_nrhs!=1 || !create_return_values(1))
03933         return false;
03934 
03935     CKernel* kernel=ui_kernel->get_kernel();
03936     if (!kernel)
03937         SG_ERROR("No kernel.\n");
03938     if (!kernel->get_rhs())
03939         SG_ERROR("No rhs.\n");
03940 
03941     int32_t num_vec=kernel->get_rhs()->get_num_vectors();
03942     int32_t degree=0;
03943     int32_t len=0;
03944     EKernelType ktype=kernel->get_kernel_type();
03945 
03946     // it would be nice to have a common base class for the WD kernels
03947     if (ktype==K_WEIGHTEDDEGREE)
03948     {
03949         CWeightedDegreeStringKernel* k=(CWeightedDegreeStringKernel*) kernel;
03950         k->get_degree_weights(degree, len);
03951         if (!k->is_tree_initialized())
03952             SG_ERROR("Kernel optimization not initialized.\n");
03953     }
03954     else if (ktype==K_WEIGHTEDDEGREEPOS)
03955     {
03956         CWeightedDegreePositionStringKernel* k=
03957             (CWeightedDegreePositionStringKernel*) kernel;
03958         k->get_degree_weights(degree, len);
03959         if (!k->is_tree_initialized())
03960             SG_ERROR("Kernel optimization not initialized.\n");
03961     }
03962     else
03963         SG_ERROR("Only works for Weighted Degree (Position) kernels.\n");
03964 
03965     if (len==0)
03966         len=1;
03967 
03968     int32_t num_feat=degree*len;
03969     int32_t num=num_feat*num_vec;
03970     float64_t* result=SG_MALLOC(float64_t, num);
03971 
03972     for (int32_t i=0; i<num; i++)
03973         result[i]=0;
03974 
03975     if (ktype==K_WEIGHTEDDEGREE)
03976     {
03977         CWeightedDegreeStringKernel* k=(CWeightedDegreeStringKernel*) kernel;
03978         for (int32_t i=0; i<num_vec; i++)
03979             k->compute_by_tree(i, &result[i*num_feat]);
03980     }
03981     else
03982     {
03983         CWeightedDegreePositionStringKernel* k=
03984             (CWeightedDegreePositionStringKernel*) kernel;
03985         for (int32_t i=0; i<num_vec; i++)
03986             k->compute_by_tree(i, &result[i*num_feat]);
03987     }
03988 
03989     set_matrix(result, num_feat, num_vec);
03990     SG_FREE(result);
03991 
03992     return true;
03993 }
03994 
03995 bool CSGInterface::cmd_init_kernel_optimization()
03996 {
03997     if (m_nrhs<1 || !create_return_values(0))
03998         return false;
03999 
04000     return ui_kernel->init_kernel_optimization();
04001 }
04002 
04003 bool CSGInterface::cmd_get_kernel_optimization()
04004 {
04005     if (m_nrhs<1 || !create_return_values(1))
04006         return false;
04007 
04008     CKernel* kernel=ui_kernel->get_kernel();
04009     if (!kernel)
04010         SG_ERROR("No kernel defined.\n");
04011 
04012     switch (kernel->get_kernel_type())
04013     {
04014         case K_WEIGHTEDDEGREEPOS:
04015         {
04016             if (m_nrhs!=2)
04017                 SG_ERROR("parameter missing\n");
04018 
04019             int32_t max_order=get_int();
04020             if ((max_order<1) || (max_order>12))
04021             {
04022                 SG_WARNING( "max_order out of range 1..12 (%d). setting to 1\n", max_order);
04023                 max_order=1;
04024             }
04025 
04026             CWeightedDegreePositionStringKernel* k=(CWeightedDegreePositionStringKernel*) kernel;
04027             CSVM* svm=(CSVM*) ui_classifier->get_classifier();
04028             if (!svm)
04029                 SG_ERROR("No SVM defined.\n");
04030 
04031             int32_t num_suppvec=svm->get_num_support_vectors();
04032             int32_t* sv_idx=SG_MALLOC(int32_t, num_suppvec);
04033             float64_t* sv_weight=SG_MALLOC(float64_t, num_suppvec);
04034             int32_t num_feat=0;
04035             int32_t num_sym=0;
04036 
04037             for (int32_t i=0; i<num_suppvec; i++)
04038             {
04039                 sv_idx[i]=svm->get_support_vector(i);
04040                 sv_weight[i]=svm->get_alpha(i);
04041             }
04042 
04043             float64_t* position_weights=k->extract_w(max_order, num_feat,
04044                 num_sym, NULL, num_suppvec, sv_idx, sv_weight);
04045             SG_FREE(sv_idx);
04046             SG_FREE(sv_weight);
04047 
04048             set_matrix(position_weights, num_sym, num_feat);
04049             SG_FREE(position_weights);
04050 
04051             return true;
04052         }
04053 
04054         case K_COMMWORDSTRING:
04055         case K_WEIGHTEDCOMMWORDSTRING:
04056         {
04057             CCommWordStringKernel* k=(CCommWordStringKernel*) kernel;
04058             int32_t len=0;
04059             float64_t* weights;
04060             k->get_dictionary(len, weights);
04061 
04062             set_vector(weights, len);
04063             return true;
04064         }
04065         case K_LINEAR:
04066         {
04067             CLinearKernel* k=(CLinearKernel*) kernel;
04068             int32_t len=0;
04069             const float64_t* weights=k->get_normal(len);
04070 
04071             set_vector(weights, len);
04072             return true;
04073         }
04074         default:
04075             SG_ERROR("Unsupported kernel %s.\n", kernel->get_name());
04076     }
04077 
04078     return true;
04079 }
04080 
04081 bool CSGInterface::cmd_delete_kernel_optimization()
04082 {
04083     if (m_nrhs<1 || !create_return_values(0))
04084         return false;
04085 
04086     return ui_kernel->delete_kernel_optimization();
04087 }
04088 
04089 bool CSGInterface::cmd_use_diagonal_speedup()
04090 {
04091     if (m_nrhs<2 || !create_return_values(0))
04092         return false;
04093 
04094     bool speedup=get_bool();
04095 
04096     CKernel* kernel=ui_kernel->get_kernel();
04097     if (!kernel)
04098         SG_ERROR("No kernel defined.\n");
04099 
04100     if (kernel->get_kernel_type()==K_COMBINED)
04101     {
04102         SG_DEBUG("Identified combined kernel.\n");
04103         kernel=((CCombinedKernel*) kernel)->get_last_kernel();
04104         if (!kernel)
04105             SG_ERROR("No last kernel defined.\n");
04106     }
04107 
04108     if (kernel->get_kernel_type()!=K_COMMWORDSTRING)
04109         SG_ERROR("Currently only commwordstring kernel supports diagonal speedup\n");
04110 
04111     ((CCommWordStringKernel*) kernel)->set_use_dict_diagonal_optimization(speedup);
04112 
04113     SG_INFO("Diagonal speedup %s.\n", speedup ? "enabled" : "disabled");
04114 
04115     return true;
04116 }
04117 
04118 bool CSGInterface::cmd_set_kernel_optimization_type()
04119 {
04120     if (m_nrhs<2 || !create_return_values(0))
04121         return false;
04122 
04123     int32_t len=0;
04124     char* opt_type=get_str_from_str_or_direct(len);
04125 
04126     bool success=ui_kernel->set_optimization_type(opt_type);
04127 
04128     SG_FREE(opt_type);
04129     return success;
04130 }
04131 
04132 bool CSGInterface::cmd_set_solver()
04133 {
04134     if (m_nrhs<2 || !create_return_values(0))
04135         return false;
04136 
04137     int32_t len=0;
04138     char* solver=get_str_from_str_or_direct(len);
04139 
04140     bool success=ui_classifier->set_solver(solver);
04141 
04142     SG_FREE(solver);
04143     return success;
04144 }
04145 
04146 bool CSGInterface::cmd_set_constraint_generator()
04147 {
04148     if (m_nrhs<2 || !create_return_values(0))
04149         return false;
04150 
04151     int32_t len=0;
04152     char* cg=get_str_from_str_or_direct(len);
04153 
04154     bool success=ui_classifier->set_constraint_generator(cg);
04155 
04156     SG_FREE(cg);
04157     return success;
04158 }
04159 
04160 bool CSGInterface::cmd_set_prior_probs()
04161 {
04162     if (m_nrhs<3 || !create_return_values(0))
04163         return false;
04164 
04165     CSalzbergWordStringKernel* kernel=
04166         (CSalzbergWordStringKernel*) ui_kernel->get_kernel();
04167     if (kernel->get_kernel_type()!=K_SALZBERG)
04168         SG_ERROR("SalzbergWordStringKernel required for setting prior probs!\n");
04169 
04170     float64_t pos_probs=get_real_from_real_or_str();
04171     float64_t neg_probs=get_real_from_real_or_str();
04172 
04173     kernel->set_prior_probs(pos_probs, neg_probs);
04174 
04175     return true;
04176 }
04177 
04178 bool CSGInterface::cmd_set_prior_probs_from_labels()
04179 {
04180     if (m_nrhs<2 || !create_return_values(0))
04181         return false;
04182 
04183     CSalzbergWordStringKernel* kernel=
04184         (CSalzbergWordStringKernel*) ui_kernel->get_kernel();
04185     if (kernel->get_kernel_type()!=K_SALZBERG)
04186     SG_ERROR("SalzbergWordStringKernel required for setting prior probs!\n");
04187 
04188     float64_t* lab=NULL;
04189     int32_t len=0;
04190     get_vector(lab, len);
04191 
04192     CLabels* labels=new CLabels(len);
04193     for (int32_t i=0; i<len; i++)
04194     {
04195         if (!labels->set_label(i, lab[i]))
04196             SG_ERROR("Couldn't set label %d (of %d): %f.\n", i, len, lab[i]);
04197     }
04198     SG_FREE(lab);
04199 
04200     kernel->set_prior_probs_from_labels(labels);
04201 
04202     SG_UNREF(labels);
04203     return true;
04204 }
04205 
04206 
04207 
04208 
04211 bool CSGInterface::cmd_set_distance()
04212 {
04213     if (m_nrhs<3 || !create_return_values(0))
04214         return false;
04215 
04216     CDistance* distance=NULL;
04217     int32_t len=0;
04218     char* type=get_str_from_str_or_direct(len);
04219     char* dtype=get_str_from_str_or_direct(len);
04220 
04221     if (strmatch(type, "MINKOWSKI") && m_nrhs==4)
04222     {
04223         float64_t k=get_real_from_real_or_str();
04224         distance=ui_distance->create_minkowski(k);
04225     }
04226     else if (strmatch(type, "MANHATTAN"))
04227     {
04228         if (strmatch(dtype, "REAL"))
04229             distance=ui_distance->create_generic(D_MANHATTAN);
04230         else if (strmatch(dtype, "WORD"))
04231             distance=ui_distance->create_generic(D_MANHATTANWORD);
04232     }
04233     else if (strmatch(type, "HAMMING") && strmatch(dtype, "WORD"))
04234     {
04235         bool use_sign=false;
04236         if (m_nrhs==4)
04237             use_sign=get_bool_from_bool_or_str(); // optional
04238 
04239         distance=ui_distance->create_hammingword(use_sign);
04240     }
04241     else if (strmatch(type, "CANBERRA"))
04242     {
04243         if (strmatch(dtype, "REAL"))
04244             distance=ui_distance->create_generic(D_CANBERRA);
04245         else if (strmatch(dtype, "WORD"))
04246             distance=ui_distance->create_generic(D_CANBERRAWORD);
04247     }
04248     else if (strmatch(type, "CHEBYSHEW") && strmatch(dtype, "REAL"))
04249     {
04250         distance=ui_distance->create_generic(D_CHEBYSHEW);
04251     }
04252     else if (strmatch(type, "GEODESIC") && strmatch(dtype, "REAL"))
04253     {
04254         distance=ui_distance->create_generic(D_GEODESIC);
04255     }
04256     else if (strmatch(type, "JENSEN") && strmatch(dtype, "REAL"))
04257     {
04258         distance=ui_distance->create_generic(D_JENSEN);
04259     }
04260     else if (strmatch(type, "CHISQUARE") && strmatch(dtype, "REAL"))
04261     {
04262         distance=ui_distance->create_generic(D_CHISQUARE);
04263     }
04264     else if (strmatch(type, "TANIMOTO") && strmatch(dtype, "REAL"))
04265     {
04266         distance=ui_distance->create_generic(D_TANIMOTO);
04267     }
04268     else if (strmatch(type, "COSINE") && strmatch(dtype, "REAL"))
04269     {
04270         distance=ui_distance->create_generic(D_COSINE);
04271     }
04272     else if (strmatch(type, "BRAYCURTIS") && strmatch(dtype, "REAL"))
04273     {
04274         distance=ui_distance->create_generic(D_BRAYCURTIS);
04275     }
04276     else if (strmatch(type, "EUCLIDIAN"))
04277     {
04278         if (strmatch(dtype, "REAL"))
04279             distance=ui_distance->create_generic(D_EUCLIDIAN);
04280         else if (strmatch(dtype, "SPARSEREAL"))
04281             distance=ui_distance->create_generic(D_SPARSEEUCLIDIAN);
04282     }
04283     else
04284         SG_NOTIMPLEMENTED;
04285 
04286     SG_FREE(type);
04287     SG_FREE(dtype);
04288     return ui_distance->set_distance(distance);
04289 }
04290 
04291 bool CSGInterface::cmd_init_distance()
04292 {
04293     SG_DEPRECATED;
04294     return true;
04295 }
04296 
04297 bool CSGInterface::cmd_get_distance_matrix()
04298 {
04299     if (m_nrhs!=2 || !create_return_values(1))
04300         return false;
04301 
04302     int32_t len=0;
04303     char* target=get_string(len);
04304 
04305     bool success=ui_distance->init_distance(target);
04306 
04307     if (success)
04308     {
04309         CDistance* distance=ui_distance->get_distance();
04310         if (!distance || !distance->has_features())
04311             SG_ERROR("No distance defined or not initialized.\n");
04312 
04313         int32_t num_vec_lhs=0;
04314         int32_t num_vec_rhs=0;
04315         float64_t* dmatrix=NULL;
04316         dmatrix=distance->get_distance_matrix_real(num_vec_lhs, num_vec_rhs, dmatrix);
04317 
04318         set_matrix(dmatrix, num_vec_lhs, num_vec_rhs);
04319         SG_FREE(dmatrix);
04320     }
04321 
04322     return success;
04323 }
04324 
04325 
04326 /* POIM */
04327 
04328 bool CSGInterface::cmd_get_SPEC_consensus()
04329 {
04330     if (m_nrhs!=1 || !create_return_values(1))
04331         return false;
04332 
04333     CKernel* kernel=ui_kernel->get_kernel();
04334     if (!kernel)
04335         SG_ERROR("No kernel.\n");
04336     if (kernel->get_kernel_type()!=K_COMMWORDSTRING)
04337         SG_ERROR("Only works for CommWordString kernels.\n");
04338 
04339     CSVM* svm=(CSVM*) ui_classifier->get_classifier();
04340     ASSERT(svm);
04341     int32_t num_suppvec=svm->get_num_support_vectors();
04342     int32_t* sv_idx=SG_MALLOC(int32_t, num_suppvec);
04343     float64_t* sv_weight=SG_MALLOC(float64_t, num_suppvec);
04344     int32_t num_feat=0;
04345 
04346     for (int32_t i=0; i<num_suppvec; i++)
04347     {
04348         sv_idx[i]=svm->get_support_vector(i);
04349         sv_weight[i]=svm->get_alpha(i);
04350     }
04351 
04352     char* consensus=((CCommWordStringKernel*) kernel)->compute_consensus(
04353         num_feat, num_suppvec, sv_idx, sv_weight);
04354     SG_FREE(sv_idx);
04355     SG_FREE(sv_weight);
04356 
04357     set_vector(consensus, num_feat);
04358     SG_FREE(consensus);
04359 
04360     return true;
04361 }
04362 
04363 bool CSGInterface::cmd_get_SPEC_scoring()
04364 {
04365     if (m_nrhs!=2 || !create_return_values(1))
04366         return false;
04367 
04368     int32_t max_order=get_int();
04369     CKernel* kernel=ui_kernel->get_kernel();
04370     if (!kernel)
04371         SG_ERROR("No kernel.\n");
04372 
04373     EKernelType ktype=kernel->get_kernel_type();
04374     if (ktype!=K_COMMWORDSTRING && ktype!=K_WEIGHTEDCOMMWORDSTRING)
04375         SG_ERROR("Only works for (Weighted) CommWordString kernels.\n");
04376 
04377     CSVM* svm=(CSVM*) ui_classifier->get_classifier();
04378     ASSERT(svm);
04379     int32_t num_suppvec=svm->get_num_support_vectors();
04380     int32_t* sv_idx=SG_MALLOC(int32_t, num_suppvec);
04381     float64_t* sv_weight=SG_MALLOC(float64_t, num_suppvec);
04382     int32_t num_feat=0;
04383     int32_t num_sym=0;
04384 
04385     for (int32_t i=0; i<num_suppvec; i++)
04386     {
04387         sv_idx[i]=svm->get_support_vector(i);
04388         sv_weight[i]=svm->get_alpha(i);
04389     }
04390 
04391     if ((max_order<1) || (max_order>8))
04392     {
04393         SG_WARNING( "max_order out of range 1..8 (%d). setting to 1\n", max_order);
04394         max_order=1;
04395     }
04396 
04397     float64_t* position_weights=NULL;
04398     if (ktype==K_COMMWORDSTRING)
04399         position_weights=((CCommWordStringKernel*) kernel)->compute_scoring(
04400             max_order, num_feat, num_sym, NULL,
04401             num_suppvec, sv_idx, sv_weight);
04402     else
04403         position_weights=((CWeightedCommWordStringKernel*) kernel)->compute_scoring(
04404             max_order, num_feat, num_sym, NULL,
04405             num_suppvec, sv_idx, sv_weight);
04406     SG_FREE(sv_idx);
04407     SG_FREE(sv_weight);
04408 
04409     set_matrix(position_weights, num_sym, num_feat);
04410     SG_FREE(position_weights);
04411 
04412     return true;
04413 }
04414 
04415 bool CSGInterface::cmd_get_WD_consensus()
04416 {
04417     if (m_nrhs!=1 || !create_return_values(1))
04418         return false;
04419 
04420     CKernel* kernel=ui_kernel->get_kernel();
04421     if (!kernel)
04422         SG_ERROR("No kernel.\n");
04423     if (kernel->get_kernel_type()!=K_WEIGHTEDDEGREEPOS)
04424         SG_ERROR("Only works for Weighted Degree Position kernels.\n");
04425 
04426     CSVM* svm=(CSVM*) ui_classifier->get_classifier();
04427     ASSERT(svm);
04428     int32_t num_suppvec=svm->get_num_support_vectors();
04429     int32_t* sv_idx=SG_MALLOC(int32_t, num_suppvec);
04430     float64_t* sv_weight=SG_MALLOC(float64_t, num_suppvec);
04431     int32_t num_feat=0;
04432 
04433     for (int32_t i=0; i<num_suppvec; i++)
04434     {
04435         sv_idx[i]=svm->get_support_vector(i);
04436         sv_weight[i]=svm->get_alpha(i);
04437     }
04438 
04439     char* consensus=((CWeightedDegreePositionStringKernel*) kernel)->compute_consensus(
04440             num_feat, num_suppvec, sv_idx, sv_weight);
04441     SG_FREE(sv_idx);
04442     SG_FREE(sv_weight);
04443 
04444     set_vector(consensus, num_feat);
04445     SG_FREE(consensus);
04446 
04447     return true;
04448 }
04449 
04450 bool CSGInterface::cmd_compute_POIM_WD()
04451 {
04452     if (m_nrhs!=3 || !create_return_values(1))
04453         return false;
04454 
04455     int32_t max_order=get_int();
04456     float64_t* distribution=NULL;
04457     int32_t num_dfeat=0;
04458     int32_t num_dvec=0;
04459     get_matrix(distribution, num_dfeat, num_dvec);
04460 
04461     if (!distribution)
04462         SG_ERROR("Wrong distribution.\n");
04463 
04464     CKernel* kernel=ui_kernel->get_kernel();
04465     if (!kernel)
04466         SG_ERROR("No Kernel.\n");
04467     if (kernel->get_kernel_type()!=K_WEIGHTEDDEGREEPOS)
04468         SG_ERROR("Only works for Weighted Degree Position kernels.\n");
04469 
04470     int32_t seqlen=0;
04471     int32_t num_sym=0;
04472     CStringFeatures<char>* sfeat=(CStringFeatures<char>*)
04473         (((CWeightedDegreePositionStringKernel*) kernel)->get_lhs());
04474     ASSERT(sfeat);
04475     seqlen=sfeat->get_max_vector_length();
04476     num_sym=(int32_t) sfeat->get_num_symbols();
04477 
04478     if (num_dvec!=seqlen || num_dfeat!=num_sym)
04479     {
04480         SG_ERROR("distribution should have (seqlen x num_sym) elements"
04481                 "(seqlen: %d vs. %d symbols: %d vs. %d)\n", seqlen,
04482                 num_dvec, num_sym, num_dfeat);
04483     }
04484 
04485         CSVM* svm=(CSVM*) ui_classifier->get_classifier();
04486         ASSERT(svm);
04487         int32_t num_suppvec=svm->get_num_support_vectors();
04488         int32_t* sv_idx=SG_MALLOC(int32_t, num_suppvec);
04489         float64_t* sv_weight=SG_MALLOC(float64_t, num_suppvec);
04490 
04491         for (int32_t i=0; i<num_suppvec; i++)
04492         {
04493             sv_idx[i]=svm->get_support_vector(i);
04494             sv_weight[i]=svm->get_alpha(i);
04495         }
04496 
04497         /*
04498         if ((max_order < 1) || (max_order > 12))
04499         {
04500             SG_WARNING( "max_order out of range 1..12 (%d). setting to 1.\n", max_order);
04501             max_order=1;
04502         }
04503         */
04504 
04505         float64_t* position_weights;
04506         position_weights=((CWeightedDegreePositionStringKernel*) kernel)->compute_POIM(
04507                 max_order, seqlen, num_sym, NULL,
04508                 num_suppvec, sv_idx, sv_weight, distribution);
04509         SG_FREE(sv_idx);
04510         SG_FREE(sv_weight);
04511 
04512         set_matrix(position_weights, num_sym, seqlen);
04513         SG_FREE(position_weights);
04514 
04515         return true;
04516     }
04517 
04518     bool CSGInterface::cmd_get_WD_scoring()
04519     {
04520         if (m_nrhs!=2 || !create_return_values(1))
04521             return false;
04522 
04523         int32_t max_order=get_int();
04524 
04525         CKernel* kernel=ui_kernel->get_kernel();
04526         if (!kernel)
04527             SG_ERROR("No kernel.\n");
04528         if (kernel->get_kernel_type()!=K_WEIGHTEDDEGREEPOS)
04529             SG_ERROR("Only works for Weighted Degree Position kernels.\n");
04530 
04531     CSVM* svm=(CSVM*) ui_classifier->get_classifier();
04532     ASSERT(svm);
04533     int32_t num_suppvec=svm->get_num_support_vectors();
04534     int32_t* sv_idx=SG_MALLOC(int32_t, num_suppvec);
04535     float64_t* sv_weight=SG_MALLOC(float64_t, num_suppvec);
04536     int32_t num_feat=0;
04537     int32_t num_sym=0;
04538 
04539     for (int32_t i=0; i<num_suppvec; i++)
04540     {
04541         sv_idx[i]=svm->get_support_vector(i);
04542         sv_weight[i]=svm->get_alpha(i);
04543     }
04544 
04545     if ((max_order<1) || (max_order>12))
04546     {
04547         SG_WARNING("max_order out of range 1..12 (%d). setting to 1\n", max_order);
04548         max_order=1;
04549     }
04550 
04551     float64_t* position_weights=
04552         ((CWeightedDegreePositionStringKernel*) kernel)->compute_scoring(
04553             max_order, num_feat, num_sym, NULL, num_suppvec, sv_idx, sv_weight);
04554     SG_FREE(sv_idx);
04555     SG_FREE(sv_weight);
04556 
04557     set_matrix(position_weights, num_sym, num_feat);
04558     SG_FREE(position_weights);
04559 
04560     return true;
04561 }
04562 
04563 
04564 /* Classifier */
04565 
04566 bool CSGInterface::cmd_classify()
04567 {
04568     if (m_nrhs!=1 || !create_return_values(1))
04569         return false;
04570 
04571     if (!ui_kernel->get_kernel() ||
04572             !ui_kernel->get_kernel()->get_kernel_type()==K_CUSTOM)
04573     {
04574         CFeatures* feat=ui_features->get_test_features();
04575         if (!feat)
04576             SG_ERROR("No features found.\n");
04577     }
04578 
04579     CLabels* labels=ui_classifier->classify();
04580     if (!labels)
04581         SG_ERROR("Classify failed\n");
04582 
04583     int32_t num_vec=labels->get_num_labels();
04584     float64_t* result=SG_MALLOC(float64_t, num_vec);
04585     for (int32_t i=0; i<num_vec; i++)
04586         result[i]=labels->get_label(i);
04587     SG_UNREF(labels);
04588 
04589     set_vector(result, num_vec);
04590     SG_FREE(result);
04591 
04592     return true;
04593 }
04594 
04595 bool CSGInterface::cmd_classify_example()
04596 {
04597     if (m_nrhs!=2 || !create_return_values(1))
04598         return false;
04599 
04600     int32_t idx=get_int();
04601     float64_t result=0;
04602 
04603     if (!ui_classifier->classify_example(idx, result))
04604         SG_ERROR("Classify_example failed.\n");
04605 
04606     set_real(result);
04607 
04608     return true;
04609 }
04610 
04611 bool CSGInterface::cmd_get_classifier()
04612 {
04613     if (m_nrhs<1 || m_nrhs>2 || !create_return_values(2))
04614         return false;
04615 
04616     int32_t idx=-1;
04617     if (m_nrhs==2)
04618         idx=get_int();
04619 
04620     float64_t* bias=NULL;
04621     float64_t* weights=NULL;
04622     int32_t rows=0;
04623     int32_t cols=0;
04624     int32_t brows=0;
04625     int32_t bcols=0;
04626 
04627     if (!ui_classifier->get_trained_classifier(
04628         weights, rows, cols, bias, brows, bcols, idx))
04629         return false;
04630 
04631     //SG_PRINT("brows %d, bcols %d\n", brows, bcols);
04632     //CMath::display_matrix(bias, brows, bcols);
04633     set_matrix(bias, brows, bcols);
04634     SG_FREE(bias);
04635 
04636     //SG_PRINT("rows %d, cols %d\n", rows, cols);
04637     //CMath::display_matrix(weights, rows, cols);
04638     set_matrix(weights, rows, cols);
04639     SG_FREE(weights);
04640 
04641     return true;
04642 }
04643 
04644 bool CSGInterface::cmd_new_classifier()
04645 {
04646     if (m_nrhs<2 || !create_return_values(0))
04647         return false;
04648 
04649     int32_t len=0;
04650     char* name=get_str_from_str_or_direct(len);
04651     int32_t d=6;
04652     int32_t from_d=40;
04653 
04654     if (m_nrhs>2)
04655     {
04656         d=get_int_from_int_or_str();
04657 
04658         if (m_nrhs>3)
04659             from_d=get_int_from_int_or_str();
04660     }
04661 
04662     bool success=ui_classifier->new_classifier(name, d, from_d);
04663 
04664     SG_FREE(name);
04665     return success;
04666 }
04667 
04668 bool CSGInterface::cmd_save_classifier()
04669 {
04670     if (m_nrhs<2 || !create_return_values(0))
04671         return false;
04672 
04673     int32_t len=0;
04674     char* filename=get_str_from_str_or_direct(len);
04675 
04676     bool success=ui_classifier->save(filename);
04677 
04678     SG_FREE(filename);
04679     return success;
04680 }
04681 
04682 bool CSGInterface::cmd_load_classifier()
04683 {
04684     if (m_nrhs<3 || !create_return_values(0))
04685         return false;
04686 
04687     int32_t len=0;
04688     char* filename=get_str_from_str_or_direct(len);
04689     char* type=get_str_from_str_or_direct(len);
04690 
04691     bool success=ui_classifier->load(filename, type);
04692 
04693     SG_FREE(filename);
04694     SG_FREE(type);
04695     return success;
04696 }
04697 
04698 
04699 bool CSGInterface::cmd_get_num_svms()
04700 {
04701     if (m_nrhs!=1 || !create_return_values(1))
04702         return false;
04703 
04704     set_int(ui_classifier->get_num_svms());
04705 
04706     return true;
04707 }
04708 
04709 
04710 bool CSGInterface::cmd_get_svm()
04711 {
04712     return cmd_get_classifier();
04713 }
04714 
04715 bool CSGInterface::cmd_set_svm()
04716 {
04717     if (m_nrhs!=3 || !create_return_values(0))
04718         return false;
04719 
04720     float64_t bias=get_real();
04721 
04722     float64_t* alphas=NULL;
04723     int32_t num_feat_alphas=0;
04724     int32_t num_vec_alphas=0;
04725     get_matrix(alphas, num_feat_alphas, num_vec_alphas);
04726 
04727     if (!alphas)
04728         SG_ERROR("No proper alphas given.\n");
04729     if (num_vec_alphas!=2)
04730         SG_ERROR("Not 2 vectors in alphas.\n");
04731 
04732     CSVM* svm=(CSVM*) ui_classifier->get_classifier();
04733     if (!svm)
04734         SG_ERROR("No SVM object available.\n");
04735 
04736     svm->create_new_model(num_feat_alphas);
04737     svm->set_bias(bias);
04738 
04739     int32_t num_support_vectors=svm->get_num_support_vectors();
04740     for (int32_t i=0; i<num_support_vectors; i++)
04741     {
04742         svm->set_alpha(i, alphas[i]);
04743         svm->set_support_vector(i, (int32_t) alphas[i+num_support_vectors]);
04744     }
04745     SG_FREE(alphas);
04746 
04747     return true;
04748 }
04749 
04750 bool CSGInterface::cmd_set_linear_classifier()
04751 {
04752     if (m_nrhs!=3 || !create_return_values(0))
04753         return false;
04754 
04755     float64_t bias=get_real();
04756 
04757     float64_t* w=NULL;
04758     int32_t len=0;
04759     get_vector(w, len);
04760 
04761     if (!len)
04762         SG_ERROR("No proper weight vector given.\n");
04763 
04764     CLinearMachine* c=(CLinearMachine*) ui_classifier->get_classifier();
04765     if (!c)
04766         SG_ERROR("No Linear Classifier object available.\n");
04767 
04768     c->set_w(SGVector<float64_t>(w, len));
04769     c->set_bias(bias);
04770 
04771     SG_FREE(w);
04772 
04773     return true;
04774 }
04775 
04776 bool CSGInterface::cmd_get_svm_objective()
04777 {
04778     if (m_nrhs!=1 || !create_return_values(1))
04779         return false;
04780 
04781     CSVM* svm=(CSVM*) ui_classifier->get_classifier();
04782     if (!svm)
04783         SG_ERROR("No SVM set.\n");
04784 
04785     set_real(svm->get_objective());
04786 
04787     return true;
04788 }
04789 
04790 bool CSGInterface::cmd_compute_svm_primal_objective()
04791 {
04792     return do_compute_objective(SVM_PRIMAL);
04793 }
04794 
04795 bool CSGInterface::cmd_compute_svm_dual_objective()
04796 {
04797     return do_compute_objective(SVM_DUAL);
04798 }
04799 
04800 bool CSGInterface::cmd_compute_mkl_dual_objective()
04801 {
04802     return do_compute_objective(MKL_DUAL);
04803 }
04804 
04805 bool CSGInterface::cmd_compute_relative_mkl_duality_gap()
04806 {
04807     return do_compute_objective(MKL_RELATIVE_DUALITY_GAP);
04808 }
04809 
04810 bool CSGInterface::cmd_compute_absolute_mkl_duality_gap()
04811 {
04812     return do_compute_objective(MKL_ABSOLUTE_DUALITY_GAP);
04813 }
04814 
04815 bool CSGInterface::do_compute_objective(E_WHICH_OBJ obj)
04816 {
04817     if (m_nrhs!=1 || !create_return_values(1))
04818         return false;
04819 
04820     float64_t result=23.5;
04821 
04822     CSVM* svm=(CSVM*) ui_classifier->get_classifier();
04823     if (!svm)
04824         SG_ERROR("No SVM set.\n");
04825 
04826     CLabels* trainlabels=NULL;
04827     trainlabels=ui_labels->get_train_labels();
04828 
04829     if (!trainlabels)
04830         SG_ERROR("No trainlabels available.\n");
04831 
04832     CKernel* kernel=ui_kernel->get_kernel();
04833     if (!kernel)
04834         SG_ERROR("No kernel available.\n");
04835 
04836     if (!ui_kernel->is_initialized() || !kernel->has_features())
04837         SG_ERROR("Kernel not initialized.\n");
04838 
04839     ((CKernelMachine*) svm)->set_labels(trainlabels);
04840     ((CKernelMachine*) svm)->set_kernel(kernel);
04841 
04842 
04843     switch (obj)
04844     {
04845         case  SVM_PRIMAL:
04846             result=svm->compute_svm_primal_objective();
04847             break;
04848         case  SVM_DUAL:
04849             result=svm->compute_svm_dual_objective();
04850             break;
04851         case  MKL_PRIMAL:
04852             ASSERT( svm->get_classifier_type() == CT_MKLCLASSIFICATION );
04853             result=((CMKL*) svm)->compute_mkl_primal_objective();
04854             break;
04855         case  MKL_DUAL:
04856             ASSERT( svm->get_classifier_type() == CT_MKLCLASSIFICATION );
04857             result=((CMKL*) svm)->compute_mkl_dual_objective();
04858             break;
04859         case  MKL_RELATIVE_DUALITY_GAP:
04860             {
04861                 ASSERT( svm->get_classifier_type() == CT_MKLCLASSIFICATION );
04862                 float64_t primal=((CMKL*) svm)->compute_mkl_dual_objective();
04863                 float64_t dual=((CMKL*) svm)->compute_mkl_primal_objective();
04864                 result=(primal-dual)/dual;
04865             }
04866             break;
04867         case  MKL_ABSOLUTE_DUALITY_GAP:
04868             {
04869                 ASSERT( svm->get_classifier_type() == CT_MKLCLASSIFICATION );
04870                 float64_t primal=((CMKL*) svm)->compute_mkl_dual_objective();
04871                 float64_t dual=((CMKL*) svm)->compute_mkl_primal_objective();
04872                 result=dual-primal;
04873             }
04874             break;
04875         default:
04876             SG_SERROR("Error calling do_compute_objective\n");
04877             return false;
04878     };
04879 
04880     set_real(result);
04881     return true;
04882 }
04883 
04884 bool CSGInterface::cmd_train_classifier()
04885 {
04886     if (m_nrhs<1 || !create_return_values(0))
04887         return false;
04888 
04889     CMachine* classifier=ui_classifier->get_classifier();
04890     if (!classifier)
04891         SG_ERROR("No classifier available.\n");
04892 
04893     EClassifierType type=classifier->get_classifier_type();
04894     switch (type)
04895     {
04896         case CT_LIGHT:
04897         case CT_LIGHTONECLASS:
04898         case CT_LIBSVM:
04899         case CT_SCATTERSVM:
04900         case CT_MPD:
04901         case CT_GPBT:
04902         case CT_CPLEXSVM:
04903         case CT_GMNPSVM:
04904         case CT_GNPPSVM:
04905         case CT_KERNELPERCEPTRON:
04906         case CT_LIBSVR:
04907         case CT_LIBSVMMULTICLASS:
04908         case CT_LIBSVMONECLASS:
04909         case CT_SVRLIGHT:
04910         case CT_LARANK:
04911             return ui_classifier->train_svm();
04912         case CT_MKLMULTICLASS:
04913             return ui_classifier->train_mkl_multiclass();
04914         case CT_MKLCLASSIFICATION:
04915         case CT_MKLREGRESSION:
04916         case CT_MKLONECLASS:
04917             return ui_classifier->train_mkl();
04918 
04919         case CT_KRR:
04920             return ui_classifier->train_krr();
04921 
04922         case CT_KNN:
04923         {
04924             if (m_nrhs<2)
04925                 return false;
04926 
04927             int32_t k=get_int_from_int_or_str();
04928 
04929             return ui_classifier->train_knn(k);
04930         }
04931 
04932         case CT_KMEANS:
04933         {
04934             if (m_nrhs<3)
04935                 return false;
04936 
04937             int32_t k=get_int_from_int_or_str();
04938             int32_t max_iter=get_int_from_int_or_str();
04939 
04940             return ui_classifier->train_clustering(k, max_iter);
04941         }
04942 
04943         case CT_HIERARCHICAL:
04944         {
04945             if (m_nrhs<2)
04946                 return false;
04947 
04948             int32_t merges=get_int_from_int_or_str();
04949 
04950             return ui_classifier->train_clustering(merges);
04951         }
04952 
04953         case CT_LDA:
04954         {
04955             float64_t gamma=0;
04956             if (m_nrhs==2)
04957                 gamma=get_real_from_real_or_str();
04958 
04959             return ui_classifier->train_linear(gamma);
04960         }
04961 
04962         case CT_PERCEPTRON:
04963         case CT_SVMLIN:
04964         case CT_SVMPERF:
04965         case CT_SUBGRADIENTSVM:
04966         case CT_SVMOCAS:
04967         case CT_SVMSGD:
04968         case CT_LPM:
04969         case CT_LPBOOST:
04970         case CT_SUBGRADIENTLPM:
04971         case CT_LIBLINEAR:
04972             return ui_classifier->train_linear();
04973 
04974         case CT_WDSVMOCAS:
04975             return ui_classifier->train_wdocas();
04976 
04977         default:
04978             SG_ERROR("Unknown classifier type %d.\n", type);
04979     }
04980 
04981     return false;
04982 }
04983 
04984 bool CSGInterface::cmd_do_auc_maximization()
04985 {
04986     if (m_nrhs!=2 || !create_return_values(0))
04987         return false;
04988 
04989     bool do_auc=get_bool_from_bool_or_str();
04990 
04991     return ui_classifier->set_do_auc_maximization(do_auc);
04992 }
04993 
04994 bool CSGInterface::cmd_set_perceptron_parameters()
04995 {
04996     if (m_nrhs!=3 || !create_return_values(0))
04997         return false;
04998 
04999     float64_t lernrate=get_real_from_real_or_str();
05000     int32_t maxiter=get_int_from_int_or_str();
05001 
05002     return ui_classifier->set_perceptron_parameters(lernrate, maxiter);
05003 }
05004 
05005 bool CSGInterface::cmd_set_svm_qpsize()
05006 {
05007     if (m_nrhs!=2 || !create_return_values(0))
05008         return false;
05009 
05010     int32_t qpsize=get_int_from_int_or_str();
05011 
05012     return ui_classifier->set_svm_qpsize(qpsize);
05013 }
05014 
05015 bool CSGInterface::cmd_set_svm_max_qpsize()
05016 {
05017     if (m_nrhs!=2 || !create_return_values(0))
05018         return false;
05019 
05020     int32_t max_qpsize=get_int_from_int_or_str();
05021 
05022     return ui_classifier->set_svm_max_qpsize(max_qpsize);
05023 }
05024 
05025 bool CSGInterface::cmd_set_svm_bufsize()
05026 {
05027     if (m_nrhs!=2 || !create_return_values(0))
05028         return false;
05029 
05030     int32_t bufsize=get_int_from_int_or_str();
05031 
05032     return ui_classifier->set_svm_bufsize(bufsize);
05033 }
05034 
05035 bool CSGInterface::cmd_set_svm_C()
05036 {
05037     if (m_nrhs<2 || !create_return_values(0))
05038         return false;
05039 
05040     float64_t C1=get_real_from_real_or_str();
05041     float64_t C2=C1;
05042 
05043     if (m_nrhs==3)
05044         C2=get_real_from_real_or_str();
05045 
05046     return ui_classifier->set_svm_C(C1, C2);
05047 }
05048 
05049 bool CSGInterface::cmd_set_svm_epsilon()
05050 {
05051     if (m_nrhs!=2 || !create_return_values(0))
05052         return false;
05053 
05054     float64_t epsilon=get_real_from_real_or_str();
05055 
05056     return ui_classifier->set_svm_epsilon(epsilon);
05057 }
05058 
05059 bool CSGInterface::cmd_set_svr_tube_epsilon()
05060 {
05061     if (m_nrhs!=2 || !create_return_values(0))
05062         return false;
05063 
05064     float64_t tube_epsilon=get_real_from_real_or_str();
05065 
05066     return ui_classifier->set_svr_tube_epsilon(tube_epsilon);
05067 }
05068 
05069 bool CSGInterface::cmd_set_svm_nu()
05070 {
05071     if (m_nrhs!=2 || !create_return_values(0))
05072         return false;
05073 
05074     float64_t nu=get_real_from_real_or_str();
05075 
05076     return ui_classifier->set_svm_nu(nu);
05077 }
05078 
05079 bool CSGInterface::cmd_set_svm_mkl_parameters()
05080 {
05081     if (m_nrhs<3 || m_nrhs>4 || !create_return_values(0))
05082         return false;
05083 
05084     float64_t weight_epsilon=get_real_from_real_or_str();
05085     float64_t C_mkl=get_real_from_real_or_str();
05086     float64_t mkl_norm=1.0;
05087     
05088     if (m_nrhs==4)
05089         mkl_norm=get_real_from_real_or_str();
05090 
05091     return ui_classifier->set_svm_mkl_parameters(weight_epsilon, C_mkl, mkl_norm);
05092 }
05093 
05094 bool CSGInterface::cmd_set_elasticnet_lambda()
05095 {
05096     if (m_nrhs!=2 || !create_return_values(0))
05097         return false;
05098     float64_t lambda=get_real_from_real_or_str();
05099     return ui_classifier->set_elasticnet_lambda(lambda);
05100 }
05101 
05102 bool CSGInterface::cmd_set_mkl_block_norm()
05103 {
05104     if (m_nrhs!=2 || !create_return_values(0))
05105         return false;
05106     float64_t bnorm=get_real_from_real_or_str();
05107     return ui_classifier->set_mkl_block_norm(bnorm);
05108 }
05109 
05110 
05111 bool CSGInterface::cmd_set_max_train_time()
05112 {
05113     if (m_nrhs!=2 || !create_return_values(0))
05114         return false;
05115 
05116     float64_t max_train_time=get_real_from_real_or_str();
05117 
05118     return ui_classifier->set_max_train_time(max_train_time);
05119 }
05120 
05121 bool CSGInterface::cmd_set_svm_shrinking_enabled()
05122 {
05123     if (m_nrhs!=2 || !create_return_values(0))
05124         return false;
05125 
05126     bool shrinking_enabled=get_bool_from_bool_or_str();
05127 
05128     return ui_classifier->set_svm_shrinking_enabled(shrinking_enabled);
05129 }
05130 
05131 bool CSGInterface::cmd_set_svm_batch_computation_enabled()
05132 {
05133     if (m_nrhs!=2 || !create_return_values(0))
05134         return false;
05135 
05136     bool batch_computation_enabled=get_bool_from_bool_or_str();
05137 
05138     return ui_classifier->set_svm_batch_computation_enabled(
05139         batch_computation_enabled);
05140 }
05141 
05142 bool CSGInterface::cmd_set_svm_linadd_enabled()
05143 {
05144     if (m_nrhs!=2 || !create_return_values(0))
05145         return false;
05146 
05147     bool linadd_enabled=get_bool_from_bool_or_str();
05148 
05149     return ui_classifier->set_svm_linadd_enabled(linadd_enabled);
05150 }
05151 
05152 bool CSGInterface::cmd_set_svm_bias_enabled()
05153 {
05154     if (m_nrhs!=2 || !create_return_values(0))
05155         return false;
05156 
05157     bool bias_enabled=get_bool_from_bool_or_str();
05158 
05159     return ui_classifier->set_svm_bias_enabled(bias_enabled);
05160 }
05161 
05162 bool CSGInterface::cmd_set_mkl_interleaved_enabled()
05163 {
05164     if (m_nrhs!=2 || !create_return_values(0))
05165         return false;
05166 
05167     bool interleaved_enabled=get_bool_from_bool_or_str();
05168 
05169     return ui_classifier->set_mkl_interleaved_enabled(interleaved_enabled);
05170 }
05171 
05172 bool CSGInterface::cmd_set_krr_tau()
05173 {
05174     if (m_nrhs!=2 || !create_return_values(0))
05175         return false;
05176 
05177     float64_t tau=get_real_from_real_or_str();
05178 
05179     return ui_classifier->set_krr_tau(tau);
05180 }
05181 
05182 
05183 /* Preproc */
05184 
05185 bool CSGInterface::cmd_add_preproc()
05186 {
05187     if (m_nrhs<2 || !create_return_values(0))
05188         return false;
05189 
05190     int32_t len=0;
05191     char* type=get_str_from_str_or_direct(len);
05192     CPreprocessor* preproc=NULL;
05193 
05194     if (strmatch(type, "NORMONE"))
05195         preproc=ui_preproc->create_generic(P_NORMONE);
05196     else if (strmatch(type, "LOGPLUSONE"))
05197         preproc=ui_preproc->create_generic(P_LOGPLUSONE);
05198     else if (strmatch(type, "SORTWORDSTRING"))
05199         preproc=ui_preproc->create_generic(P_SORTWORDSTRING);
05200     else if (strmatch(type, "SORTULONGSTRING"))
05201         preproc=ui_preproc->create_generic(P_SORTULONGSTRING);
05202     else if (strmatch(type, "DECOMPRESSCHARSTRING"))
05203         preproc=ui_preproc->create_generic(P_DECOMPRESSCHARSTRING);
05204     else if (strmatch(type, "SORTWORD"))
05205         preproc=ui_preproc->create_generic(P_SORTWORD);
05206 
05207     else if (strmatch(type, "PRUNEVARSUBMEAN"))
05208     {
05209         bool divide_by_std=false;
05210         if (m_nrhs==3)
05211             divide_by_std=get_bool_from_bool_or_str();
05212 
05213         preproc=ui_preproc->create_prunevarsubmean(divide_by_std);
05214     }
05215 
05216 #ifdef HAVE_LAPACK
05217     else if (strmatch(type, "PCA") && m_nrhs==4)
05218     {
05219         bool do_whitening=get_bool_from_bool_or_str();
05220         float64_t threshold=get_real_from_real_or_str();
05221 
05222         preproc=ui_preproc->create_pca(do_whitening, threshold);
05223     }
05224 #endif
05225 
05226     else
05227         SG_NOTIMPLEMENTED;
05228 
05229     SG_FREE(type);
05230     return ui_preproc->add_preproc(preproc);
05231 }
05232 
05233 bool CSGInterface::cmd_del_preproc()
05234 {
05235     if (m_nrhs!=1 || !create_return_values(0))
05236         return false;
05237 
05238     return ui_preproc->del_preproc();
05239 }
05240 
05241 bool CSGInterface::cmd_attach_preproc()
05242 {
05243     if (m_nrhs<2 || !create_return_values(0))
05244         return false;
05245 
05246     int32_t len=0;
05247     char* target=get_str_from_str_or_direct(len);
05248 
05249     bool do_force=false;
05250     if (m_nrhs==3)
05251         do_force=get_bool_from_bool_or_str();
05252 
05253     bool success=ui_preproc->attach_preproc(target, do_force);
05254 
05255     SG_FREE(target);
05256     return success;
05257 }
05258 
05259 bool CSGInterface::cmd_clean_preproc()
05260 {
05261     if (m_nrhs!=1 || !create_return_values(0))
05262         return false;
05263 
05264     return ui_preproc->clean_preproc();
05265 }
05266 
05267 
05268 /* HMM */
05269 
05270 bool CSGInterface::cmd_new_plugin_estimator()
05271 {
05272     if (m_nrhs<2 || !create_return_values(0))
05273         return false;
05274 
05275     float64_t pos_pseudo=get_real_from_real_or_str();
05276     float64_t neg_pseudo=get_real_from_real_or_str();
05277 
05278     return ui_pluginestimate->new_estimator(pos_pseudo, neg_pseudo);
05279 }
05280 
05281 bool CSGInterface::cmd_train_estimator()
05282 {
05283     if (m_nrhs!=1 || !create_return_values(0))
05284         return false;
05285 
05286     return ui_pluginestimate->train();
05287 }
05288 
05289 bool CSGInterface::cmd_plugin_estimate_classify_example()
05290 {
05291     if (m_nrhs!=2 || !create_return_values(1))
05292         return false;
05293 
05294     int32_t idx=get_int();
05295     float64_t result=ui_pluginestimate->apply(idx);
05296 
05297     set_vector(&result, 1);
05298     return true;
05299 }
05300 
05301 bool CSGInterface::cmd_plugin_estimate_classify()
05302 {
05303     if (m_nrhs!=1 || !create_return_values(1))
05304         return false;
05305 
05306     CFeatures* feat=ui_features->get_test_features();
05307     if (!feat)
05308         SG_ERROR("No features found.\n");
05309 
05310     int32_t num_vec=feat->get_num_vectors();
05311     float64_t* result=SG_MALLOC(float64_t, num_vec);
05312     CLabels* labels=ui_pluginestimate->apply();
05313     for (int32_t i=0; i<num_vec; i++)
05314         result[i]=labels->get_label(i);
05315     SG_UNREF(labels);
05316 
05317     set_vector(result, num_vec);
05318     SG_FREE(result);
05319 
05320     return true;
05321 }
05322 
05323 bool CSGInterface::cmd_set_plugin_estimate()
05324 {
05325     if (m_nrhs!=3 || !create_return_values(0))
05326         return false;
05327 
05328     float64_t* emission_probs=NULL;
05329     int32_t num_probs=0;
05330     int32_t num_vec=0;
05331     get_matrix(emission_probs, num_probs, num_vec);
05332 
05333     if (num_vec!=2)
05334         SG_ERROR("Need at least 1 set of positive and 1 set of negative params.\n");
05335 
05336     float64_t* pos_params=emission_probs;
05337     float64_t* neg_params=&(emission_probs[num_probs]);
05338 
05339     float64_t* model_sizes=NULL;
05340     int32_t len=0;
05341     get_vector(model_sizes, len);
05342 
05343     int32_t seq_length=(int32_t) model_sizes[0];
05344     int32_t num_symbols=(int32_t) model_sizes[1];
05345     if (num_probs!=seq_length*num_symbols)
05346         SG_ERROR("Mismatch in number of emission probs and sequence length * number of symbols.\n");
05347 
05348     ui_pluginestimate->get_estimator()->set_model_params(
05349         pos_params, neg_params, seq_length, num_symbols);
05350 
05351     return true;
05352 }
05353 
05354 bool CSGInterface::cmd_get_plugin_estimate()
05355 {
05356     if (m_nrhs!=1 || !create_return_values(2))
05357         return false;
05358 
05359     float64_t* pos_params=NULL;
05360     float64_t* neg_params=NULL;
05361     int32_t num_params=0;
05362     int32_t seq_length=0;
05363     int32_t num_symbols=0;
05364 
05365     if (!ui_pluginestimate->get_estimator()->get_model_params(
05366         pos_params, neg_params, seq_length, num_symbols))
05367         return false;
05368 
05369     num_params=seq_length*num_symbols;
05370 
05371     float64_t* result=SG_MALLOC(float64_t, num_params*2);
05372     for (int32_t i=0; i<num_params; i++)
05373         result[i]=pos_params[i];
05374     for (int32_t i=0; i<num_params; i++)
05375         result[i+num_params]=neg_params[i];
05376 
05377     set_matrix(result, num_params, 2);
05378     SG_FREE(result);
05379 
05380     float64_t model_sizes[2];
05381     model_sizes[0]=(float64_t) seq_length;
05382     model_sizes[1]=(float64_t) num_symbols;
05383     set_vector(model_sizes, 2);
05384 
05385     return true;
05386 }
05387 
05388 bool CSGInterface::cmd_convergence_criteria()
05389 {
05390     if (m_nrhs<3 || !create_return_values(0))
05391         return false;
05392 
05393     int32_t num_iterations=get_int_from_int_or_str();
05394     float64_t epsilon=get_real_from_real_or_str();
05395 
05396     return ui_hmm->convergence_criteria(num_iterations, epsilon);
05397 }
05398 
05399 bool CSGInterface::cmd_normalize()
05400 {
05401     if (m_nrhs<2 || !create_return_values(0))
05402         return false;
05403 
05404     bool keep_dead_states=get_bool_from_bool_or_str();
05405 
05406     return ui_hmm->normalize(keep_dead_states);
05407 }
05408 
05409 bool CSGInterface::cmd_add_states()
05410 {
05411     if (m_nrhs<3 || !create_return_values(0))
05412         return false;
05413 
05414     int32_t num_states=get_int_from_int_or_str();
05415     float64_t value=get_real_from_real_or_str();
05416 
05417     return ui_hmm->add_states(num_states, value);
05418 }
05419 
05420 bool CSGInterface::cmd_permutation_entropy()
05421 {
05422     if (m_nrhs<3 || !create_return_values(0))
05423         return false;
05424 
05425     int32_t width=get_int_from_int_or_str();
05426     int32_t seq_num=get_int_from_int_or_str();
05427 
05428     return ui_hmm->permutation_entropy(width, seq_num);
05429 }
05430 
05431 bool CSGInterface::cmd_relative_entropy()
05432 {
05433     if (m_nrhs!=1 || !create_return_values(1))
05434         return false;
05435 
05436     float64_t* entropy=NULL;
05437     int32_t len=0;
05438     bool success=ui_hmm->relative_entropy(entropy, len);
05439     if (!success)
05440         return false;
05441 
05442     set_vector(entropy, len);
05443 
05444     SG_FREE(entropy);
05445     return true;
05446 }
05447 
05448 bool CSGInterface::cmd_entropy()
05449 {
05450     if (m_nrhs!=1 || !create_return_values(1))
05451         return false;
05452 
05453     float64_t* entropy=NULL;
05454     int32_t len=0;
05455     bool success=ui_hmm->entropy(entropy, len);
05456     if (!success)
05457         return false;
05458 
05459     set_vector(entropy, len);
05460 
05461     SG_FREE(entropy);
05462     return true;
05463 }
05464 
05465 bool CSGInterface::cmd_hmm_classify()
05466 {
05467     return do_hmm_classify(false, false);
05468 }
05469 
05470 bool CSGInterface::cmd_one_class_hmm_classify()
05471 {
05472     return do_hmm_classify(false, true);
05473 }
05474 
05475 bool CSGInterface::cmd_one_class_linear_hmm_classify()
05476 {
05477     return do_hmm_classify(true, true);
05478 }
05479 
05480 bool CSGInterface::do_hmm_classify(bool linear, bool one_class)
05481 {
05482     if (m_nrhs>1 || !create_return_values(1))
05483         return false;
05484 
05485     CFeatures* feat=ui_features->get_test_features();
05486     if (!feat)
05487         return false;
05488 
05489     int32_t num_vec=feat->get_num_vectors();
05490     CLabels* labels=NULL;
05491 
05492     if (linear) // must be one_class as well
05493     {
05494         labels=ui_hmm->linear_one_class_classify();
05495     }
05496     else
05497     {
05498         if (one_class)
05499             labels=ui_hmm->one_class_classify();
05500         else
05501             labels=ui_hmm->classify();
05502     }
05503     if (!labels)
05504         return false;
05505 
05506     float64_t* result=SG_MALLOC(float64_t, num_vec);
05507     for (int32_t i=0; i<num_vec; i++)
05508         result[i]=labels->get_label(i);
05509     SG_UNREF(labels);
05510 
05511     set_vector(result, num_vec);
05512     SG_FREE(result);
05513 
05514     return true;
05515 }
05516 
05517 bool CSGInterface::cmd_one_class_hmm_classify_example()
05518 {
05519     return do_hmm_classify_example(true);
05520 }
05521 
05522 bool CSGInterface::cmd_hmm_classify_example()
05523 {
05524     return do_hmm_classify_example(false);
05525 }
05526 
05527 bool CSGInterface::do_hmm_classify_example(bool one_class)
05528 {
05529     if (m_nrhs!=2 || !create_return_values(1))
05530         return false;
05531 
05532     int32_t idx=get_int();
05533     float64_t result=0;
05534 
05535     if (one_class)
05536         result=ui_hmm->one_class_classify_example(idx);
05537     else
05538         result=ui_hmm->classify_example(idx);
05539 
05540     set_real(result);
05541 
05542     return true;
05543 }
05544 
05545 bool CSGInterface::cmd_output_hmm()
05546 {
05547     if (m_nrhs!=1 || !create_return_values(0))
05548         return false;
05549 
05550     return ui_hmm->output_hmm();
05551 }
05552 
05553 bool CSGInterface::cmd_output_hmm_defined()
05554 {
05555     if (m_nrhs!=1 || !create_return_values(0))
05556         return false;
05557 
05558     return ui_hmm->output_hmm_defined();
05559 }
05560 
05561 bool CSGInterface::cmd_hmm_likelihood()
05562 {
05563     if (m_nrhs!=1 || !create_return_values(1))
05564         return false;
05565 
05566     CHMM* h=ui_hmm->get_current();
05567     if (!h)
05568         SG_ERROR("No HMM.\n");
05569 
05570     float64_t likelihood=h->model_probability();
05571     set_real(likelihood);
05572 
05573     return true;
05574 }
05575 
05576 bool CSGInterface::cmd_likelihood()
05577 {
05578     if (m_nrhs!=1 || !create_return_values(0))
05579         return false;
05580 
05581     return ui_hmm->likelihood();
05582 }
05583 
05584 bool CSGInterface::cmd_save_likelihood()
05585 {
05586     if (m_nrhs<2 || !create_return_values(0))
05587         return false;
05588 
05589     int32_t len=0;
05590     char* filename=get_str_from_str_or_direct(len);
05591 
05592     bool is_binary=false;
05593     if (m_nrhs==3)
05594         is_binary=get_bool_from_bool_or_str();
05595 
05596     bool success=ui_hmm->save_likelihood(filename, is_binary);
05597 
05598     SG_FREE(filename);
05599     return success;
05600 }
05601 
05602 bool CSGInterface::cmd_get_viterbi_path()
05603 {
05604     if (m_nrhs!=2 || !create_return_values(2))
05605         return false;
05606 
05607     int32_t dim=get_int();
05608     SG_DEBUG("dim: %f\n", dim);
05609 
05610     CHMM* h=ui_hmm->get_current();
05611     if (!h)
05612         return false;
05613 
05614     CFeatures* feat=ui_features->get_test_features();
05615     if (!feat || (feat->get_feature_class()!=C_STRING) ||
05616             (feat->get_feature_type()!=F_WORD))
05617         return false;
05618 
05619     h->set_observations((CStringFeatures<uint16_t>*) feat);
05620 
05621     int32_t num_feat=0;
05622     bool free_vec;
05623     uint16_t* vec=((CStringFeatures<uint16_t>*) feat)->get_feature_vector(dim, num_feat, free_vec);
05624     if (!vec || num_feat<=0)
05625     {
05626         ((CStringFeatures<uint16_t>*) feat)->free_feature_vector(vec, dim, free_vec);
05627         return false;
05628     }
05629     ((CStringFeatures<uint16_t>*) feat)->free_feature_vector(vec, dim, free_vec);
05630 
05631     SG_DEBUG( "computing viterbi path for vector %d (length %d)\n", dim, num_feat);
05632     float64_t likelihood=0;
05633     T_STATES* path=h->get_path(dim, likelihood);
05634 
05635     set_vector(path, num_feat);
05636     SG_FREE(path);
05637     set_real(likelihood);
05638 
05639     return true;
05640 }
05641 
05642 bool CSGInterface::cmd_viterbi_train()
05643 {
05644     if (m_nrhs!=1 || !create_return_values(0))
05645         return false;
05646 
05647     return ui_hmm->viterbi_train();
05648 }
05649 
05650 bool CSGInterface::cmd_viterbi_train_defined()
05651 {
05652     if (m_nrhs!=1 || !create_return_values(0))
05653         return false;
05654 
05655     return ui_hmm->viterbi_train_defined();
05656 }
05657 
05658 bool CSGInterface::cmd_baum_welch_train()
05659 {
05660     if (m_nrhs!=1 || !create_return_values(0))
05661         return false;
05662 
05663     return ui_hmm->baum_welch_train();
05664 }
05665 
05666 bool CSGInterface::cmd_baum_welch_train_defined()
05667 {
05668     if (m_nrhs!=1 || !create_return_values(0))
05669         return false;
05670 
05671     return ui_hmm->baum_welch_train_defined();
05672 }
05673 
05674 
05675 bool CSGInterface::cmd_baum_welch_trans_train()
05676 {
05677     if (m_nrhs!=1 || !create_return_values(0))
05678         return false;
05679 
05680     return ui_hmm->baum_welch_trans_train();
05681 }
05682 
05683 bool CSGInterface::cmd_linear_train()
05684 {
05685     if (m_nrhs<1 || !create_return_values(0))
05686         return false;
05687 
05688     if (m_nrhs==2)
05689     {
05690         int32_t len=0;
05691         char* align=get_str_from_str_or_direct(len);
05692 
05693         bool success=ui_hmm->linear_train(align[0]);
05694 
05695         SG_FREE(align);
05696         return success;
05697     }
05698     else
05699         return ui_hmm->linear_train();
05700 }
05701 
05702 bool CSGInterface::cmd_save_path()
05703 {
05704     if (m_nrhs<2 || !create_return_values(0))
05705         return false;
05706 
05707     int32_t len=0;
05708     char* filename=get_str_from_str_or_direct(len);
05709 
05710     bool is_binary=false;
05711     if (m_nrhs==3)
05712         is_binary=get_bool_from_bool_or_str();
05713 
05714     bool success=ui_hmm->save_path(filename, is_binary);
05715 
05716     SG_FREE(filename);
05717     return success;
05718 }
05719 
05720 bool CSGInterface::cmd_append_hmm()
05721 {
05722     if (m_nrhs!=5 || !create_return_values(0))
05723         return false;
05724 
05725     CHMM* old_h=ui_hmm->get_current();
05726     if (!old_h)
05727         SG_ERROR("No current HMM set.\n");
05728 
05729     float64_t* p=NULL;
05730     int32_t N_p=0;
05731     get_vector(p, N_p);
05732 
05733     float64_t* q=NULL;
05734     int32_t N_q=0;
05735     get_vector(q, N_q);
05736 
05737     float64_t* a=NULL;
05738     int32_t M_a=0;
05739     int32_t N_a=0;
05740     get_matrix(a, M_a, N_a);
05741     int32_t N=N_a;
05742 
05743     float64_t* b=NULL;
05744     int32_t M_b=0;
05745     int32_t N_b=0;
05746     get_matrix(b, M_b, N_b);
05747     int32_t M=N_b;
05748 
05749     if (N_p!=N || N_q!=N || N_a!=N || M_a!=N || N_b!=M || M_b!=N)
05750     {
05751         SG_ERROR("Model matrices not matching in size.\n"
05752                 "p:(%d) q:(%d) a:(%d,%d) b(%d,%d)\n",
05753                 N_p, N_q, N_a, M_a, N_b, M_b);
05754     }
05755 
05756     CHMM* h=new CHMM(N, M, NULL, ui_hmm->get_pseudo());
05757     int32_t i,j;
05758 
05759     for (i=0; i<N; i++)
05760     {
05761         h->set_p(i, p[i]);
05762         h->set_q(i, q[i]);
05763     }
05764 
05765     for (i=0; i<N; i++)
05766         for (j=0; j<N; j++)
05767             h->set_a(i,j, a[i+j*N]);
05768 
05769     for (i=0; i<N; i++)
05770         for (j=0; j<M; j++)
05771             h->set_b(i,j, b[i+j*N]);
05772 
05773     old_h->append_model(h);
05774     SG_UNREF(h);
05775 
05776     return true;
05777 }
05778 
05779 bool CSGInterface::cmd_append_model()
05780 {
05781     if (m_nrhs<2 || !create_return_values(0))
05782         return false;
05783     if (m_nrhs>2 && m_nrhs!=4)
05784         return false;
05785 
05786     int32_t len=0;
05787     char* filename=get_str_from_str_or_direct(len);
05788     int32_t base1=-1;
05789     int32_t base2=-1;
05790     if (m_nrhs>2)
05791     {
05792         base1=get_int_from_int_or_str();
05793         base2=get_int_from_int_or_str();
05794     }
05795 
05796     bool success=ui_hmm->append_model(filename, base1, base2);
05797 
05798     SG_FREE(filename);
05799     return success;
05800 }
05801 
05802 bool CSGInterface::cmd_new_hmm()
05803 {
05804     if (m_nrhs!=3 || !create_return_values(0))
05805         return false;
05806 
05807     int32_t n=get_int_from_int_or_str();
05808     int32_t m=get_int_from_int_or_str();
05809 
05810     return ui_hmm->new_hmm(n, m);
05811 }
05812 
05813 bool CSGInterface::cmd_load_hmm()
05814 {
05815     if (m_nrhs!=2 || !create_return_values(0))
05816         return false;
05817 
05818     int32_t len=0;
05819     char* filename=get_str_from_str_or_direct(len);
05820 
05821     bool success=ui_hmm->load(filename);
05822 
05823     SG_FREE(filename);
05824     return success;
05825 }
05826 
05827 bool CSGInterface::cmd_save_hmm()
05828 {
05829     if (m_nrhs<2 || !create_return_values(0))
05830         return false;
05831 
05832     int32_t len=0;
05833     char* filename=get_str_from_str_or_direct(len);
05834 
05835     bool is_binary=false;
05836     if (m_nrhs==3)
05837         is_binary=get_bool_from_bool_or_str();
05838 
05839     bool success=ui_hmm->save(filename, is_binary);
05840 
05841     SG_FREE(filename);
05842     return success;
05843 }
05844 
05845 bool CSGInterface::cmd_set_hmm()
05846 {
05847     if (m_nrhs!=5 || !create_return_values(0))
05848         return false;
05849 
05850     float64_t* p=NULL;
05851     int32_t N_p=0;
05852     get_vector(p, N_p);
05853 
05854     float64_t* q=NULL;
05855     int32_t N_q=0;
05856     get_vector(q, N_q);
05857 
05858     float64_t* a=NULL;
05859     int32_t M_a=0;
05860     int32_t N_a=0;
05861     get_matrix(a, M_a, N_a);
05862     int32_t N=N_a;
05863 
05864     float64_t* b=NULL;
05865     int32_t M_b=0;
05866     int32_t N_b=0;
05867     get_matrix(b, M_b, N_b);
05868     int32_t M=N_b;
05869 
05870     if (N_p!=N || N_q!=N || N_a!=N || M_a!=N || N_b!=M || M_b!=N)
05871     {
05872         SG_ERROR("Model matrices not matching in size.\n"
05873                 "p:(%d) q:(%d) a:(%d,%d) b(%d,%d)\n",
05874                 N_p, N_q, N_a, M_a, N_b, M_b);
05875     }
05876 
05877     CHMM* current=ui_hmm->get_current();
05878     if (!current)
05879         SG_ERROR("Need a previously created HMM.\n");
05880 
05881     int32_t i,j;
05882 
05883     for (i=0; i<N; i++)
05884     {
05885         current->set_p(i, p[i]);
05886         current->set_q(i, q[i]);
05887     }
05888 
05889     for (i=0; i<N; i++)
05890         for (j=0; j<N; j++)
05891             current->set_a(i,j, a[i+j*N]);
05892 
05893     for (i=0; i<N; i++)
05894         for (j=0; j<M; j++)
05895             current->set_b(i,j, b[i+j*N]);
05896 
05897     CStringFeatures<uint16_t>* sf = ((CStringFeatures<uint16_t>*) (ui_features->get_train_features()));
05898     current->set_observations(sf);
05899 
05900     return true;
05901 }
05902 
05903 bool CSGInterface::cmd_set_hmm_as()
05904 {
05905     if (m_nrhs!=2 || !create_return_values(0))
05906         return false;
05907 
05908     int32_t len=0;
05909     char* target=get_str_from_str_or_direct(len);
05910 
05911     bool success=ui_hmm->set_hmm_as(target);
05912 
05913     SG_FREE(target);
05914     return success;
05915 }
05916 
05917 bool CSGInterface::cmd_set_chop()
05918 {
05919     if (m_nrhs!=2 || !create_return_values(0))
05920         return false;
05921 
05922     float64_t value=get_real_from_real_or_str();
05923     return ui_hmm->chop(value);
05924 }
05925 
05926 bool CSGInterface::cmd_set_pseudo()
05927 {
05928     if (m_nrhs!=2 || !create_return_values(0))
05929         return false;
05930 
05931     float64_t value=get_real_from_real_or_str();
05932     return ui_hmm->set_pseudo(value);
05933 }
05934 
05935 bool CSGInterface::cmd_load_definitions()
05936 {
05937     if (m_nrhs<2 || !create_return_values(0))
05938         return false;
05939 
05940     int32_t len=0;
05941     char* filename=get_str_from_str_or_direct(len);
05942 
05943     bool do_init=false;
05944     if (m_nrhs==3)
05945         do_init=get_bool_from_bool_or_str();
05946 
05947     bool success=ui_hmm->load_definitions(filename, do_init);
05948 
05949     SG_FREE(filename);
05950     return success;
05951 }
05952 
05953 bool CSGInterface::cmd_get_hmm()
05954 {
05955     if (m_nrhs!=1 || !create_return_values(4))
05956         return false;
05957 
05958     CHMM* h=ui_hmm->get_current();
05959     if (!h)
05960         return false;
05961 
05962     int32_t N=h->get_N();
05963     int32_t M=h->get_M();
05964     int32_t i=0;
05965     int32_t j=0;
05966     float64_t* p=SG_MALLOC(float64_t, N);
05967     float64_t* q=SG_MALLOC(float64_t, N);
05968 
05969     for (i=0; i<N; i++)
05970     {
05971         p[i]=h->get_p(i);
05972         q[i]=h->get_q(i);
05973     }
05974 
05975     set_vector(p, N);
05976     SG_FREE(p);
05977     set_vector(q, N);
05978     SG_FREE(q);
05979 
05980     float64_t* a=SG_MALLOC(float64_t, N*N);
05981     for (i=0; i<N; i++)
05982         for (j=0; j<N; j++)
05983             a[i+j*N]=h->get_a(i, j);
05984     set_matrix(a, N, N);
05985     SG_FREE(a);
05986 
05987     float64_t* b=SG_MALLOC(float64_t, N*M);
05988     for (i=0; i<N; i++)
05989         for (j=0; j<M; j++)
05990             b[i+j*N]=h->get_b(i, j);
05991     set_matrix(b, N, M);
05992     SG_FREE(b);
05993 
05994     return true;
05995 }
05996 
05997 bool CSGInterface::cmd_best_path()
05998 {
05999     if (m_nrhs!=3 || !create_return_values(0))
06000         return false;
06001 
06002     int32_t from=get_int_from_int_or_str();
06003     int32_t to=get_int_from_int_or_str();
06004 
06005     return ui_hmm->best_path(from, to);
06006 }
06007 
06008 bool CSGInterface::cmd_best_path_2struct()
06009 {
06010     if (m_nrhs!=12 || !create_return_values(3))
06011         return false;
06012 
06013     SG_ERROR("Sorry, this parameter list is awful!\n");
06014 
06015     return true;
06016 }
06017 
06018 void CSGInterface::get_vector(bool*& vector, int32_t& len)
06019 {
06020     int32_t* int_vector;
06021     get_vector(int_vector, len);
06022 
06023     ASSERT(len>0);
06024     vector= SG_MALLOC(bool, len);
06025 
06026     for (int32_t i=0; i<len; i++)
06027         vector[i]= (int_vector[i]!=0);
06028 
06029     SG_FREE(int_vector);
06030 }
06031 
06032 void CSGInterface::set_vector(const bool* vector, int32_t len)
06033 {
06034     int32_t* int_vector = SG_MALLOC(int32_t, len);
06035     for (int32_t i=0;i<len;i++)
06036     {
06037         if (vector[i])
06038             int_vector[i]=1;
06039         else
06040             int_vector[i]=0;
06041     }
06042     set_vector(int_vector,len);
06043     SG_FREE(int_vector);
06044 }
06045 
06046 bool CSGInterface::cmd_set_plif_struct()
06047 {
06048     // ARG 2 
06049     int32_t Nid=0;
06050     int32_t* ids;
06051     get_vector(ids,Nid);
06052 
06053     // ARG 3
06054     int32_t Nname=0;
06055     int32_t Mname=0;
06056     SGString<char>* names;
06057     get_string_list(names, Nname,Mname);
06058 
06059     // ARG 4
06060     int32_t Nlimits=0;
06061     int32_t Mlimits=0;
06062     float64_t* all_limits;
06063     get_matrix(all_limits, Mlimits, Nlimits);
06064 
06065     // ARG 5
06066     int32_t Npenalties=0;
06067     int32_t Mpenalties=0;
06068     float64_t* all_penalties;
06069     get_matrix(all_penalties, Mpenalties, Npenalties);
06070 
06071     // ARG 6
06072     int32_t Ntransform=0;
06073     int32_t Mtransform=0;
06074     SGString<char>* all_transform;
06075     get_string_list(all_transform, Ntransform, Mtransform);
06076 
06077     // ARG 7
06078     int32_t Nmin=0;
06079     float64_t* min_values;
06080     get_vector(min_values,Nmin);
06081 
06082     // ARG 8
06083     int32_t Nmax=0;
06084     float64_t* max_values;
06085     get_vector(max_values,Nmax);
06086 
06087     // ARG 9
06088     int32_t Ncache=0;
06089     bool* all_use_cache;
06090     get_vector(all_use_cache,Ncache);
06091 
06092     // ARG 10
06093     int32_t Nsvm=0;
06094     int32_t* all_use_svm;
06095     get_vector(all_use_svm,Nsvm);
06096 
06097     // ARG 11
06098     int32_t Ncalc=0;
06099     bool* all_do_calc;
06100     get_vector(all_do_calc,Ncalc);
06101 
06102     if (Ncalc!=Nsvm)
06103         SG_ERROR("Ncalc!=Nsvm, Ncalc:%i, Nsvm:%i\n",Ncalc,Nsvm);
06104     if (Ncalc!=Ncache)
06105         SG_ERROR("Ncalc!=Ncache, Ncalc:%i, Ncache:%i\n",Ncalc,Ncache);
06106     if (Ncalc!=Ntransform)
06107         SG_ERROR("Ncalc!=Ntransform, Ncalc:%i, Ntransform:%i\n",Ncalc,Ntransform);
06108     if (Ncalc!=Nmin)
06109         SG_ERROR("Ncalc!=Nmin, Ncalc:%i, Nmin:%i\n",Ncalc,Nmin);
06110     if (Ncalc!=Nmax)
06111         SG_ERROR("Ncalc!=Nmax, Ncalc:%i, Nmax:%i\n",Ncalc,Nmax);
06112     if (Ncalc!=Npenalties)
06113         SG_ERROR("Ncalc!=Npenalties, Ncalc:%i, Npenalties:%i\n",Ncalc,Npenalties);
06114     if (Ncalc!=Nlimits)
06115         SG_ERROR("Ncalc!=Nlimits, Ncalc:%i, Nlimits:%i\n",Ncalc,Nlimits);
06116     if (Ncalc!=Nname)
06117         SG_ERROR("Ncalc!=Nname, Ncalc:%i, Nname:%i\n",Ncalc,Nname);
06118     if (Ncalc!=Nid)
06119         SG_ERROR("Ncalc!=Nid, Ncalc:%i, Nid:%i\n",Ncalc,Nid);
06120     if (Mlimits!=Mpenalties)
06121         SG_ERROR("Mlimits!=Mpenalties, Mlimits:%i, Mpenalties:%i\n",Mlimits,Mpenalties);
06122 
06123     int32_t N = Ncalc;
06124     int32_t M = Mlimits;    
06125     CPlifMatrix* pm=ui_structure->get_plif_matrix();
06126     pm->create_plifs(N, M);
06127     pm->set_plif_ids(SGVector<int32_t>(ids, N));
06128     pm->set_plif_min_values(SGVector<float64_t>(min_values, N));
06129     pm->set_plif_max_values(SGVector<float64_t>(max_values, N));
06130     pm->set_plif_use_cache(SGVector<bool>(all_use_cache, N));
06131     pm->set_plif_use_svm(SGVector<int32_t>(all_use_svm, N));
06132     pm->set_plif_limits(SGMatrix<float64_t>(all_limits, N, M));
06133     pm->set_plif_penalties(SGMatrix<float64_t>(all_penalties, N, M));
06134     pm->set_plif_names(names, N);
06135     pm->set_plif_transform_type(all_transform, N);
06136 
06137     SG_FREE(all_limits);
06138     SG_FREE(all_penalties);
06139     SG_FREE(names);
06140     SG_FREE(all_transform);
06141     SG_FREE(min_values);
06142     SG_FREE(max_values); 
06143     SG_FREE(all_use_cache); 
06144     SG_FREE(all_use_svm);
06145     SG_FREE(all_do_calc);
06146 
06147     return true;
06148 }
06149 
06150 bool CSGInterface::cmd_get_plif_struct()
06151 {
06152     CPlifMatrix* pm=ui_structure->get_plif_matrix();
06153     CPlif** PEN = pm->get_PEN();
06154     int32_t N = pm->get_num_plifs();
06155     int32_t M = pm->get_num_limits();
06156 
06157     
06158     int32_t* ids = SG_MALLOC(int32_t, N);
06159     float64_t* max_values = SG_MALLOC(float64_t, N);
06160     float64_t* min_values = SG_MALLOC(float64_t, N);
06161     SGString<char>* names = SG_MALLOC(SGString<char>, N);
06162     SGString<char>* all_transform = SG_MALLOC(SGString<char>, N);
06163     float64_t* all_limits = SG_MALLOC(float64_t, N*M);
06164     float64_t* all_penalties = SG_MALLOC(float64_t, N*M);
06165     bool* all_use_cache = SG_MALLOC(bool, N);
06166     int32_t* all_use_svm = SG_MALLOC(int32_t, N);
06167     bool* all_do_calc = SG_MALLOC(bool, N);
06168     for (int32_t i=0;i<N;i++)
06169     {
06170         ids[i]=PEN[i]->get_id();
06171         names[i].string = PEN[i]->get_plif_name();
06172         names[i].slen = strlen(PEN[i]->get_plif_name());
06173         float64_t* limits = PEN[i]->get_plif_limits();
06174         float64_t* penalties = PEN[i]->get_plif_penalties();
06175         for (int32_t j=0;j<M;j++)
06176         {
06177             all_limits[i*M+j]=limits[j];
06178             all_penalties[i*M+j]=penalties[j];
06179         }
06180         all_transform[i].string = (char*) PEN[i]->get_transform_type();
06181         all_transform[i].slen = strlen(PEN[i]->get_transform_type());
06182         min_values[i]=PEN[i]->get_min_value();
06183         max_values[i]=PEN[i]->get_max_value();
06184         all_use_cache[i]=PEN[i]->get_use_cache();
06185         all_use_svm[i]=PEN[i]->get_use_svm();
06186         all_do_calc[i]=PEN[i]->get_do_calc();
06187         
06188     }
06189     set_vector(ids,N);
06190     set_string_list(names, N);
06191     set_matrix(all_limits, M, N);
06192     set_matrix(all_penalties, M, N);
06193     set_string_list(all_transform, N);
06194     set_vector(min_values,N);
06195     set_vector(max_values,N);
06196     set_vector(all_use_cache,N);
06197     set_vector(all_use_svm,N);
06198     set_vector(all_do_calc,N);
06199 
06200     SG_FREE(ids);
06201     SG_FREE(max_values);    
06202     SG_FREE(min_values);
06203     SG_FREE(names);
06204     SG_FREE(all_transform);
06205     SG_FREE(all_limits);
06206     SG_FREE(all_penalties);
06207     SG_FREE(all_use_cache);
06208     SG_FREE(all_use_svm);
06209     SG_FREE(all_do_calc);
06210 
06211     return true;
06212 }
06213 /*bool CSGInterface::cmd_signals_set_model()
06214 {
06215     // ARG 1
06216     int32_t len=0;
06217     char* filename;
06218     filename = get_string(len);
06219 
06220     CTrainPredMaster* tpm = new CTrainPredMaster(ui_kernel);
06221 
06222     tpm->read_models_from_file(filename);
06223 
06224     return true;
06225     }*/
06226 bool CSGInterface::cmd_signals_set_positions()
06227 {
06228     return true;
06229 }
06230 bool CSGInterface::cmd_signals_set_labels()
06231 {
06232     return true;
06233 }
06234 bool CSGInterface::cmd_signals_set_split()
06235 {
06236     return true;
06237 }
06238 bool CSGInterface::cmd_signals_set_train_mask()
06239 {
06240     return true;
06241 }
06242 bool CSGInterface::cmd_signals_add_feature()
06243 {
06244     return true;
06245 }
06246 bool CSGInterface::cmd_signals_add_kernel()
06247 {
06248     return true;
06249 }
06250 bool CSGInterface::cmd_signals_run()
06251 {
06252     return true;
06253 }
06254 
06255 bool CSGInterface::cmd_init_dyn_prog()
06256 {
06257     //ARG 1
06258     int32_t num_svms=get_int();
06259 
06260     CDynProg* h=new CDynProg(num_svms);
06261     ui_structure->set_dyn_prog(h);
06262     return true;
06263 }
06264 
06265 bool CSGInterface::cmd_clean_up_dyn_prog()
06266 {
06267     return ui_structure->cleanup();
06268 }
06269 
06270 bool CSGInterface::cmd_set_model()
06271 {
06272     
06273     CPlifMatrix* pm=ui_structure->get_plif_matrix();
06274     
06275     CDynProg* h = ui_structure->get_dyn_prog();
06276     int32_t num_svms = h->get_num_svms();
06277     //CDynProg* h=new CDynProg(Nweights/* = num_svms */);
06278 
06279     //ARG 1
06280     // transition pointers 
06281     // link transitions to length, content, frame (and tiling)
06282     // plifs (#states x #states x 3 or 4)
06283     int32_t numDim=0;
06284     int32_t* Dim=0;
06285     float64_t* penalties_array=NULL;
06286     get_ndarray(penalties_array,Dim,numDim);
06287     ASSERT(numDim==3);
06288     ASSERT(Dim[0]==Dim[1]);
06289 
06290     if (!pm->compute_plif_matrix(SGNDArray<float64_t>(penalties_array, Dim, numDim)))
06291         SG_ERROR("error computing plif  matrix\n");
06292     ui_structure->set_num_states(Dim[0]);
06293     SG_FREE(penalties_array);
06294 
06295     // ARG 2
06296     // bool-> determines if orf information should be used
06297     bool use_orf = get_bool();
06298     ui_structure->set_use_orf(use_orf);
06299 
06300     // ARG 3
06301     // determines for which contents which orf should be used (#contents x 2)
06302     int32_t Nmod=0;
06303     int32_t Mmod=0;
06304     int32_t* mod_words;
06305     get_matrix(mod_words, Nmod,Mmod);
06306     if (Nmod != num_svms)
06307         SG_ERROR("should be equal: Nmod: %i, num_svms: %i\n",Nmod,num_svms);
06308     ASSERT(Mmod == 2)
06309     h->init_mod_words_array(SGMatrix<int32_t>(mod_words, Nmod, Mmod));
06310     SG_FREE(mod_words);
06311 
06312     // ARG 4
06313     // links: states -> signal plifs (#states x 2)
06314     int32_t num_states=0;
06315     int32_t feat_dim3=0;
06316     int32_t* state_signals;
06317     get_matrix(state_signals,num_states,feat_dim3);
06318     ASSERT(num_states==Dim[0]);
06319     pm->compute_signal_plifs(SGMatrix<int32_t>(state_signals, feat_dim3, num_states));
06320     SG_FREE(state_signals);
06321 
06322 
06323     // ARG 5
06324     // ORF info (#states x 2)
06325     int32_t Norf=0;
06326     int32_t Morf=0;
06327     int32_t* orf_info;
06328     get_matrix(orf_info,Norf,Morf);
06329     ASSERT(Norf==num_states)
06330     ASSERT(Morf==2)
06331 
06332     ui_structure->set_orf_info(orf_info, Norf, Morf);
06333     h->set_orf_info(SGMatrix<int32_t>(orf_info, Norf, Morf));
06334     SG_FREE(orf_info);
06335 
06336     h->set_num_states(num_states) ;
06337     
06338     return true;
06339 }
06340 
06341 bool CSGInterface::cmd_precompute_content_svms()
06342 {
06343 
06344     // ARG 1
06345     int32_t seq_len=0;
06346     char* seq;
06347     seq = get_string(seq_len);
06348 
06349     // ARG 2
06350     // all feature positions
06351     int32_t Npos=0;
06352     int32_t* all_pos;
06353     get_vector(all_pos, Npos);
06354 
06355     //ARG 3
06356     // content svm weights
06357     int32_t Nweights=0;
06358     int32_t num_svms=0;
06359     float64_t* weights;
06360     get_matrix(weights, Nweights, num_svms);
06361     if (Nweights!=5440)
06362       SG_PRINT("Dimension mismatch: got %i, expect %i\n", Nweights, 5440) ;
06363     ui_structure->set_content_svm_weights(weights, Nweights, num_svms);
06364 
06365     CDynProg* h = ui_structure->get_dyn_prog();
06366     if (!h)
06367         SG_ERROR("no DynProg object found, use init_dyn_prog first\n");
06368 
06369 
06370     //float64_t* weights = ui_structure->get_content_svm_weights();
06371     //int32_t Mweights = h->get_num_svms();
06372     //int32_t Nweights = ui_structure->get_num_svm_weights();
06373     h->set_pos(SGVector<int32_t>(all_pos, Npos));
06374     h->set_gene_string(SGVector<char>(seq, seq_len));
06375     SG_FREE(seq);
06376     h->create_word_string();
06377     h->precompute_stop_codons();
06378     h->init_content_svm_value_array(num_svms);
06379     h->set_dict_weights(SGMatrix<float64_t>(weights, Nweights, num_svms));
06380     SG_FREE(weights);
06381     h->precompute_content_values();
06382     SG_DEBUG("precompute_content_svms done\n");
06383     return true;
06384 }
06385 
06386 bool CSGInterface::cmd_get_lin_feat()
06387 {
06388     CDynProg* h = ui_structure->get_dyn_prog();
06389     if (!h)
06390         SG_ERROR("no DynProg object found, use set_model first\n");
06391 
06392 
06393     int32_t dim1, dim2 = 0;
06394     float64_t* lin_feat = h->get_lin_feat(dim1, dim2);
06395 
06396     set_matrix(lin_feat, dim1, dim2);
06397 
06398     return true;
06399 }
06400 bool CSGInterface::cmd_set_lin_feat()
06401 {
06402     // ARG 1
06403     int32_t Nseq=0;
06404     char* seq;
06405     seq = get_string(Nseq);
06406 
06407     // ARG 2
06408     // all feature positions
06409     int32_t Npos=0;
06410     int32_t* all_pos;
06411     get_vector(all_pos, Npos);
06412 
06413     //ARG 3 
06414     //
06415     int32_t num_svms, seq_len;
06416     float64_t* lin_feat=NULL;
06417     get_matrix(lin_feat, num_svms, seq_len);
06418 
06419         if (Npos!=seq_len)
06420       {
06421         SG_ERROR("Dimension mismatch: got %i positions and (%ix%i) values\n", Npos, num_svms, seq_len) ;
06422 
06423         SG_FREE(lin_feat);
06424         SG_FREE(seq);
06425         SG_FREE(all_pos);
06426         
06427         return false ;
06428       }
06429 
06430     CDynProg* h = ui_structure->get_dyn_prog();
06431     if (!h)
06432         SG_ERROR("no DynProg object found, use set_model first\n");
06433 
06434     h->set_pos(SGVector<int32_t>(all_pos, Npos));
06435     h->set_gene_string(SGVector<char>(seq, Nseq));
06436     h->precompute_stop_codons();
06437     h->init_content_svm_value_array(num_svms);
06438     h->set_lin_feat(lin_feat, num_svms, seq_len);
06439 
06440     SG_FREE(lin_feat);
06441     SG_FREE(seq);
06442     SG_FREE(all_pos);
06443 
06444     return true;
06445 }
06446 bool CSGInterface::cmd_long_transition_settings()
06447 {
06448     bool use_long_transitions = get_bool();
06449     int32_t threshold = get_int();
06450     int32_t max_len = get_int();
06451 
06452     CDynProg* h = ui_structure->get_dyn_prog();
06453         if (!h) 
06454                 SG_ERROR("no DynProg object found, use set_model first\n");
06455 
06456     h->long_transition_settings(use_long_transitions, threshold, max_len);
06457 
06458     return true;
06459 }
06460 bool CSGInterface::cmd_set_feature_matrix()
06461 {
06462     int32_t num_states = ui_structure->get_num_states();
06463 
06464     //ARG 1
06465     // feature matrix (#states x #feature_positions x max_num_signals)
06466     int32_t* Dims=0;
06467     int32_t numDims=0;
06468     float64_t* features = NULL;
06469     get_ndarray(features, Dims, numDims);
06470     
06471     if (numDims!=3)
06472         SG_ERROR("expected a 3 dimensional array, got %i dimensions\n", numDims);
06473     if (Dims[0]!=num_states)
06474         SG_ERROR("number of rows (%i) not equal number of states (%i)\n",Dims[0], num_states);
06475     ASSERT(ui_structure->set_feature_matrix(features, Dims));
06476 
06477     ASSERT(ui_structure->set_feature_dims(Dims));
06478 
06479     SG_FREE(features);
06480     SG_FREE(Dims);
06481 
06482     return true;
06483 }
06484 bool CSGInterface::cmd_set_feature_matrix_sparse()
06485 {
06486     int32_t num_pos = ui_structure->get_num_positions();
06487     int32_t num_states = ui_structure->get_num_states();
06488 
06489     //ARG 1
06490     // feature matrix (#states x #feature_positions x max_num_signals)
06491     int32_t dim11, dim12 ;
06492     SGSparseVector<float64_t> *features1=NULL ;
06493     get_sparse_matrix(features1, dim11, dim12);
06494     
06495     int32_t dim21, dim22 ;
06496     SGSparseVector<float64_t> *features2=NULL ;
06497     get_sparse_matrix(features2, dim21, dim22);
06498 
06499     ASSERT(dim11==dim21) ;
06500     ASSERT(dim12==dim22) ;
06501 
06502     int32_t *Dims = SG_MALLOC(int32_t, 3);
06503     Dims[0]=dim11 ;
06504     Dims[1]=dim12 ;
06505     Dims[2]=2 ;
06506 
06507     ASSERT(Dims[0]==num_states)
06508     ASSERT(Dims[1]==num_pos)
06509 
06510     ASSERT(ui_structure->set_feature_matrix_sparse(features1, features2, Dims));
06511     ASSERT(ui_structure->set_feature_dims(Dims));
06512 
06513     SG_FREE(features1);
06514     SG_FREE(features2);
06515     SG_FREE(Dims);
06516     
06517     return true;
06518 }
06519 bool CSGInterface::cmd_init_intron_list()
06520 {
06521     //ARG1 start_positions
06522     int32_t Nstart_positions;
06523     int32_t* start_positions;
06524     get_vector(start_positions, Nstart_positions);
06525         //SG_PRINT("Nstart_positions:%i\n",Nstart_positions);
06526     
06527     //ARG2 end_positions
06528     int32_t Nend_positions;
06529     int32_t* end_positions;
06530     get_vector(end_positions, Nend_positions);
06531         //SG_PRINT("Nend_positions:%i\n",Nend_positions);
06532 
06533     //ARG3 quality  
06534     int32_t Nquality;
06535         int32_t* quality;
06536         get_vector(quality, Nquality);
06537         //SG_PRINT("Nquality:%i\n",Nquality);
06538 
06539     //ARG4 all candidate positions
06540     int32_t Nall_pos;
06541         int32_t* all_pos;
06542         get_vector(all_pos, Nall_pos);
06543         //SG_PRINT("Nall_pos:%i\n",Nall_pos);
06544 
06545     ASSERT(Nquality==Nend_positions);
06546     ASSERT(Nend_positions==Nstart_positions);
06547 
06548     CIntronList* intron_list = new CIntronList();
06549 
06550     intron_list->init_list(all_pos, Nall_pos);
06551 
06552     intron_list->read_introns(start_positions, end_positions, quality, Nstart_positions);
06553 
06554     SG_FREE(start_positions);
06555     SG_FREE(end_positions);
06556     SG_FREE(quality);
06557     SG_FREE(all_pos);
06558 
06559     //int32_t test;
06560     //int32_t testq;
06561     //intron_list->get_coverage(&test, &testq, 15 ,16);
06562 
06563     //SG_PRINT("coverage: %i, quality: %i\n",test, testq);
06564     
06565     CDynProg* h = ui_structure->get_dyn_prog();
06566     if (!h)
06567         SG_ERROR("no DynProg object found, use set_model first\n");
06568 
06569     h->set_intron_list(intron_list, 2);
06570 
06571     return true;
06572 }
06573 bool CSGInterface::cmd_precompute_tiling_features()
06574 {
06575     CPlifMatrix* pm=ui_structure->get_plif_matrix();
06576     CPlif** PEN  = pm->get_PEN();
06577     CDynProg* h  = ui_structure->get_dyn_prog();
06578 
06579     int32_t Nintensities=0;
06580     float64_t* intensities;
06581     get_vector(intensities, Nintensities);
06582 
06583     int32_t Nprobe_pos=0;
06584     int32_t* probe_pos;
06585     get_vector(probe_pos, Nprobe_pos);
06586     ASSERT(Nprobe_pos==Nintensities);
06587 
06588     int32_t Ntiling_plif_ids=0;
06589     int32_t* tiling_plif_ids;
06590     get_vector(tiling_plif_ids, Ntiling_plif_ids);
06591 
06592     h->init_tiling_data(probe_pos,intensities, Nprobe_pos);
06593     h->precompute_tiling_plifs(PEN, tiling_plif_ids, Ntiling_plif_ids);
06594     return true;
06595 }
06596 
06597 bool CSGInterface::cmd_best_path_trans()
06598 {
06599     CDynProg* h = ui_structure->get_dyn_prog();
06600 
06601     CSegmentLoss* seg_loss_obj = h->get_segment_loss_object();  
06602 
06603     CPlifMatrix* pm=ui_structure->get_plif_matrix();
06604 
06605     int32_t num_states = h->get_num_states();
06606     int32_t* feat_dims = ui_structure->get_feature_dims();
06607     float64_t* features = (ui_structure->get_feature_matrix(false));
06608     CSparseFeatures<float64_t>* features_sparse1 = (ui_structure->get_feature_matrix_sparse(0));
06609     CSparseFeatures<float64_t>* features_sparse2 = (ui_structure->get_feature_matrix_sparse(1));
06610     int32_t* orf_info = ui_structure->get_orf_info();
06611     bool use_orf = ui_structure->get_use_orf();
06612     int32_t Nplif = pm->get_num_plifs();
06613 
06614     // ARG 1
06615     // transitions from initial state (#states x 1)
06616     int32_t Np=0;
06617     float64_t* p;
06618     get_vector(p, Np);
06619     if (Np!=num_states)
06620         SG_ERROR("# transitions from initial state (%i) does not match # states (%i)\n", Np, num_states);
06621 
06622     // ARG 2
06623     // transitions to end state (#states x 1)
06624     int32_t Nq=0;
06625     float64_t* q;
06626     get_vector(q, Nq);
06627     if (Nq!=num_states)
06628         SG_ERROR("# transitions to end state (%i) does not match # states (%i)\n", Nq, num_states);
06629 
06630     // ARG 3
06631     // number of best paths
06632     int32_t Nnbest=0;
06633     int32_t* all_nbest;
06634     get_vector(all_nbest, Nnbest);
06635     int32_t nbest;
06636     int32_t nother = 0;
06637     if (Nnbest==2)
06638     {
06639         nbest =all_nbest[0];    
06640         nother=all_nbest[1];    
06641     }
06642     else
06643         nbest =all_nbest[0];    
06644     SG_FREE(all_nbest);
06645 
06646     // ARG 4
06647     // segment path (2 x #feature_positions)
06648     // masking/weighting of loss for specific 
06649     // regions of the true path
06650     int32_t Nseg_path=0;
06651     int32_t Mseg_path=0;
06652     float64_t* seg_path;
06653     get_matrix(seg_path, Nseg_path, Mseg_path);
06654 
06655     // ARG 5
06656     // links for transitions (#transitions x 4)
06657     int32_t Na_trans=0;
06658     int32_t num_a_trans=0;
06659     float64_t* a_trans;
06660     get_matrix(a_trans, num_a_trans, Na_trans);
06661 
06662     // ARG 6
06663     // loss matrix (#segment x 2*#segments)
06664     // one (#segment x #segments)-matrix for segment loss 
06665     // and one for nucleotide loss
06666     int32_t Nloss=0;
06667     int32_t Mloss=0;
06668     float64_t* loss;
06669     get_matrix(loss, Nloss,Mloss);
06670     
06671     int32_t M = h->get_num_positions();
06672     
06674     // check input
06676     ASSERT(num_states==Nq); 
06677 
06678     CPlif** PEN=pm->get_PEN();
06679     ASSERT(PEN);
06680     
06681     h->set_p_vector(SGVector<float64_t>(p, num_states));
06682     SG_FREE(p); p=NULL ;
06683     h->set_q_vector(SGVector<float64_t>(q, num_states));
06684     SG_FREE(q); q=NULL ;
06685 
06686     if (seg_path!=NULL)
06687     {
06688         h->set_a_trans_matrix(SGMatrix<float64_t>(a_trans, num_a_trans, Na_trans)) ;
06689     }
06690     else
06691     {
06692         h->set_a_trans_matrix(SGMatrix<float64_t>(a_trans, num_a_trans, 3)) ; // segment_id = 0 
06693     }
06694     SG_FREE(a_trans);
06695     a_trans=NULL ;
06696 
06697     if (!h->check_svm_arrays())
06698     {
06699         SG_ERROR( "svm arrays inconsistent\n") ;
06700         CPlif::delete_penalty_struct(PEN, Nplif) ;
06701         return false ;
06702     }
06703     
06704     SG_DEBUG("best_path_trans: M: %i, Mseg_path: %i\n", M, Mseg_path);
06705     
06706     h->set_observation_matrix(SGNDArray<float64_t>(features, feat_dims, 3));
06707 
06708     if (seg_path!=NULL)
06709     {
06710         h->best_path_set_segment_loss(SGMatrix<float64_t>(loss, Nloss, Mloss)) ;
06711         seg_loss_obj->set_segment_loss(loss, Nloss, Mloss);
06712     }
06713     else
06714     {
06715         float64_t zero2[2] = {0.0, 0.0} ;
06716         h->best_path_set_segment_loss(SGMatrix<float64_t>(zero2, 2, 1)) ;
06717         seg_loss_obj->set_segment_loss(zero2, 2, 1);
06718     }
06719     h->set_content_type_array(SGMatrix<float64_t>(seg_path,Nseg_path,Mseg_path));
06720     SG_FREE(seg_path);
06721     
06722     bool segment_loss_non_zero=false;
06723     for (int32_t i=0; i<Nloss*Mloss; i++)
06724     {
06725         if (loss[i]>1e-3)
06726             segment_loss_non_zero=true;
06727     }
06728 
06729     SG_FREE(loss);
06730     loss=NULL;
06731 
06732     h->set_orf_info(SGMatrix<int32_t>(orf_info, num_states, 2));
06733     h->set_sparse_features(features_sparse1, features_sparse2);
06734     h->set_plif_matrices(pm);
06735 
06736     if (segment_loss_non_zero)
06737     {
06738             SG_DEBUG("Using version with segment_loss\n") ;
06739             if (nbest==1)
06740                 h->compute_nbest_paths(feat_dims[2], use_orf, 1,true,false);
06741             else
06742                 h->compute_nbest_paths(feat_dims[2], use_orf, 2,true,false);
06743     }
06744     else
06745     {
06746             SG_DEBUG("Using version without segment_loss\n") ;
06747             if (nbest==1)
06748                 h->compute_nbest_paths(feat_dims[2], use_orf, 1,false,false);
06749             else
06750                 h->compute_nbest_paths(feat_dims[2], use_orf, 2,false,false);
06751     }
06752 
06753     SGVector<float64_t> p_prob=h->get_scores();
06754 
06755     SGMatrix<int32_t> states=h->get_states();
06756 
06757     SGMatrix<int32_t> my_pos=h->get_positions();
06758 
06759     // transcribe result
06760     float64_t* d_my_path= SG_MALLOC(float64_t, (nbest+nother)*M);
06761     float64_t* d_my_pos= SG_MALLOC(float64_t, (nbest+nother)*M);
06762     
06763     for (int32_t k=0; k<(nbest+nother); k++)
06764     {
06765         for (int32_t i=0; i<M; i++)
06766         {
06767             d_my_path[i*(nbest+nother)+k] = states.matrix[i+k*M] ;
06768             d_my_pos[i*(nbest+nother)+k] = my_pos.matrix[i+k*M] ;
06769         }
06770     }
06771     SG_FREE(states.matrix);
06772     SG_FREE(my_pos.matrix);
06773 
06774     set_vector(p_prob.vector,nbest+nother);
06775     set_vector(d_my_path, (nbest+nother)*M);
06776     set_vector(d_my_pos, (nbest+nother)*M);
06777 
06778     SG_FREE(d_my_path);
06779     SG_FREE(d_my_pos);
06780 
06781     return true;
06782 
06783 }
06784 
06785 bool CSGInterface::cmd_best_path_trans_deriv()
06786 {
06787     int32_t num_states = ui_structure->get_num_states();
06788     int32_t* feat_dims = ui_structure->get_feature_dims();
06789     float64_t* features = (ui_structure->get_feature_matrix(false));
06790 
06791     CPlifMatrix* pm=ui_structure->get_plif_matrix();
06792     int32_t Nplif = pm->get_num_plifs();
06793     CPlif** PEN = pm->get_PEN();
06794 
06795     // ARG 1
06796     // transitions from initial state (#states x 1)
06797     int32_t Np=0;
06798     float64_t* p=NULL;
06799     get_vector(p, Np);
06800     if (Np!=num_states)
06801         SG_ERROR("Np!=num_states; Np:%i num_states:%i",Np,num_states);
06802 
06803     // ARG 2
06804     // transitions to end state (#states x 1)
06805     int32_t Nq=0;
06806     float64_t* q=NULL;
06807     get_vector(q, Nq);
06808     if (Nq!=num_states)
06809         SG_ERROR("Nq!=num_states; Nq:%i num_states:%i",Nq,num_states);
06810 
06811 
06812     // ARG 3
06813     // segment path (2 x #feature_positions)
06814     // masking/weighting of loss for specific 
06815     // regions of the true path
06816     int32_t Nseg_path=0;
06817     int32_t Mseg_path=0;
06818     float64_t* seg_path;
06819     get_matrix(seg_path,Nseg_path,Mseg_path);
06820 
06821     // ARG 4
06822     // links for transitions (#transitions x 4)
06823     int32_t Na_trans=0;
06824     int32_t num_a_trans=0;
06825     float64_t* a_trans=NULL;
06826     get_matrix(a_trans, num_a_trans, Na_trans);
06827 
06828     // ARG 5
06829     // loss matrix (#segment x 2*#segments)
06830     // one (#segment x #segments)-matrix for segment loss 
06831     // and one for nucleotide loss
06832     int32_t Nloss=0;
06833     int32_t Mloss=0;
06834     float64_t* loss=NULL;
06835     get_matrix(loss, Nloss,Mloss);
06836 
06837     // ARG 6
06838     // path to calc derivative for 
06839     int32_t Nmystate_seq=0;
06840     int32_t* mystate_seq=NULL;
06841     get_vector(mystate_seq, Nmystate_seq);
06842 
06843     // ARG 7
06844     // positions of the path
06845     int32_t Nmypos_seq=0;
06846     int32_t* mypos_seq=NULL;
06847     get_vector(mypos_seq, Nmypos_seq);
06848 
06849 
06850     //a => a_trans
06851 
06852     int32_t max_plif_id = 0 ;
06853     int32_t max_plif_len = 1 ;
06854     for (int32_t i=0; i<Nplif; i++)
06855     {
06856         if (i>0 && PEN[i]->get_id()!=i)
06857             SG_ERROR("PEN[i]->get_id()!=i; PEN[%i]->get_id():%i  ,\n",i, PEN[i]->get_id());
06858         if (i>max_plif_id)
06859             max_plif_id=i ;
06860         if (PEN[i]->get_plif_len()>max_plif_len)
06861             max_plif_len=PEN[i]->get_plif_len() ;
06862     } ;
06863 
06864 
06865     CDynProg* h = ui_structure->get_dyn_prog();
06866     CSegmentLoss* seg_loss_obj = h->get_segment_loss_object();
06867     h->set_num_states(num_states) ;
06868     h->set_p_vector(SGVector<float64_t>(p, num_states)) ;
06869     h->set_q_vector(SGVector<float64_t>(q, num_states)) ;
06870 
06871     if (seg_path!=NULL) 
06872         h->set_a_trans_matrix(SGMatrix<float64_t>(a_trans, num_a_trans, Na_trans)) ;
06873     else
06874         h->set_a_trans_matrix(SGMatrix<float64_t>(a_trans, num_a_trans, 3)) ;
06875 
06876     if (!h->check_svm_arrays())
06877         SG_ERROR( "svm arrays inconsistent\n") ;
06878 
06879     int32_t *my_path = SG_MALLOC(int32_t, Nmypos_seq+1);
06880     memset(my_path, -1, Nmypos_seq*sizeof(int32_t)) ;
06881     int32_t *my_pos = SG_MALLOC(int32_t, Nmypos_seq+1);
06882     memset(my_pos, -1, Nmypos_seq*sizeof(int32_t)) ;
06883 
06884     h->set_observation_matrix(SGNDArray<float64_t>(features, feat_dims, 3));
06885     for (int32_t i=0; i<Nmypos_seq; i++)
06886     {
06887         my_path[i] = mystate_seq[i] ;
06888         my_pos[i]  = mypos_seq[i] ;
06889     }
06890 
06891     if (seg_path!=NULL)
06892     {
06893         h->best_path_set_segment_loss(SGMatrix<float64_t>(loss, Nloss, Mloss)) ;
06894         seg_loss_obj->set_segment_loss(loss, Nloss, Mloss);
06895     }
06896     else
06897     {
06898         float64_t zero2[2] = {0.0, 0.0} ;
06899         h->best_path_set_segment_loss(SGMatrix<float64_t>(zero2, 2, 1)) ;
06900         seg_loss_obj->set_segment_loss(zero2, 2, 1);
06901     }
06902     h->set_content_type_array(SGMatrix<float64_t>(seg_path,Nseg_path,Mseg_path));
06903 
06904     float64_t* p_Plif_deriv = SG_MALLOC(float64_t, (max_plif_id+1)*max_plif_len);
06905     CArray2<float64_t> a_Plif_deriv(p_Plif_deriv, max_plif_id+1, max_plif_len, false, false) ;
06906 
06907     float64_t* p_A_deriv   = SG_MALLOC(float64_t, num_states*num_states);
06908     float64_t* p_p_deriv   = SG_MALLOC(float64_t, num_states);
06909     float64_t* p_q_deriv   = SG_MALLOC(float64_t, num_states);
06910 
06911     h->set_plif_matrices(pm);
06912     h->best_path_trans_deriv(my_path, my_pos, Nmypos_seq, features, feat_dims[2]);
06913 
06914     float64_t* p_my_scores;
06915     int32_t n_scores;
06916     h->get_path_scores(&p_my_scores, &n_scores);
06917 
06918     float64_t* p_my_losses;
06919     int32_t n_losses;
06920     h->get_path_losses(&p_my_losses, &n_losses);
06921 
06922     for (int32_t i=0; i<num_states; i++)
06923     {
06924         for (int32_t j=0; j<num_states; j++)
06925             p_A_deriv[i+j*num_states] = h->get_a_deriv(i, j) ;
06926 
06927         p_p_deriv[i]=h->get_p_deriv(i) ;
06928         p_q_deriv[i]=h->get_q_deriv(i) ;
06929     }
06930 
06931     for (int32_t id=0; id<=max_plif_id; id++)
06932     {
06933         int32_t len=0 ;
06934         const float64_t * deriv = PEN[id]->get_cum_derivative(len) ;
06935         ASSERT(len<=max_plif_len) ;
06936         for (int32_t j=0; j<max_plif_len; j++)
06937             a_Plif_deriv.element(id, j)= deriv[j] ;
06938     }
06939 
06940     set_vector(p_p_deriv, num_states);
06941     set_vector(p_q_deriv, num_states);
06942     set_matrix(p_A_deriv, num_states, num_states);
06943     set_matrix(p_Plif_deriv, (max_plif_id+1), max_plif_len);
06944     set_vector(p_my_scores, Nmypos_seq);
06945     set_vector(p_my_losses, Nmypos_seq);
06946 
06947     SG_FREE(p_A_deriv);
06948     SG_FREE(p_p_deriv);
06949     SG_FREE(p_q_deriv);
06950     SG_FREE(p_Plif_deriv);
06951     free(p_my_scores);
06952     free(p_my_losses);
06953 
06954     SG_FREE(my_path);
06955     SG_FREE(my_pos);
06956 
06957     SG_FREE(p);
06958     SG_FREE(q);
06959     SG_FREE(seg_path);
06960     SG_FREE(a_trans);
06961     SG_FREE(loss);
06962     SG_FREE(mystate_seq);
06963     SG_FREE(mypos_seq);
06964 
06965     return true ;
06966 }
06967 
06968 bool CSGInterface::cmd_precompute_subkernels()
06969 {
06970     if (m_nrhs!=1 || !create_return_values(0))
06971         return false;
06972 
06973     return ui_kernel->precompute_subkernels();
06974 }
06975 bool CSGInterface::cmd_crc()
06976 {
06977     if (m_nrhs!=2 || !create_return_values(1))
06978         return false;
06979 
06980     int32_t slen=0;
06981     char* string=get_string(slen);
06982     ASSERT(string);
06983     uint8_t* bstring=SG_MALLOC(uint8_t, slen);
06984 
06985     for (int32_t i=0; i<slen; i++)
06986         bstring[i]=string[i];
06987     SG_FREE(string);
06988 
06989     int32_t val=CHash::crc32(bstring, slen);
06990     SG_FREE(bstring);
06991     set_int(val);
06992 
06993     return true;
06994 }
06995 
06996 bool CSGInterface::cmd_system()
06997 {
06998     if (m_nrhs<2 || !create_return_values(0))
06999         return false;
07000 
07001     int32_t len=0;
07002     char* command=SG_MALLOC(char, 10000);
07003     memset(command, 0, sizeof(char)*10000);
07004     char* cmd=get_str_from_str_or_direct(len);
07005     strncat(command, cmd, 10000);
07006     SG_FREE(cmd);
07007 
07008     while (m_rhs_counter<m_nrhs)
07009     {
07010         strncat(command, " ", 10000);
07011         char* arg=get_str_from_str_or_direct(len);
07012         strncat(command, arg, 10000);
07013         SG_FREE(arg);
07014     }
07015 
07016     int32_t success=system(command);
07017 
07018     return (success==0);
07019 }
07020 
07021 bool CSGInterface::cmd_exit()
07022 {
07023     exit(0);
07024     return 0; //never reached but necessary to keep sun compiler happy
07025 }
07026 
07027 bool CSGInterface::cmd_exec()
07028 {
07029     if (m_nrhs<2 || !create_return_values(0))
07030         return false;
07031 
07032     int32_t len=0;
07033     char* filename=get_str_from_str_or_direct(len);
07034     FILE* file=fopen(filename, "r");
07035     if (!file)
07036     {
07037         SG_FREE(filename);
07038         SG_ERROR("Error opening file: %s.\n", filename);
07039     }
07040 
07041     while (!feof(file))
07042     {
07043         // FIXME: interpret lines as input
07044         break;
07045     }
07046 
07047     fclose(file);
07048     return true;
07049 }
07050 
07051 bool CSGInterface::cmd_set_output()
07052 {
07053     if (m_nrhs<2 || !create_return_values(0))
07054         return false;
07055 
07056     int32_t len=0;
07057     char* filename=get_str_from_str_or_direct(len);
07058 
07059     if (file_out)
07060         fclose(file_out);
07061     file_out=NULL;
07062 
07063     SG_INFO("Setting output file to: %s.\n", filename);
07064 
07065     if (strmatch(filename, "STDERR"))
07066         io->set_target(stderr);
07067     else if (strmatch(filename, "STDOUT"))
07068         io->set_target(stdout);
07069     else
07070     {
07071         file_out=fopen(filename, "w");
07072         if (!file_out)
07073             SG_ERROR("Error opening output file %s.\n", filename);
07074         io->set_target(file_out);
07075     }
07076 
07077     return true;
07078 }
07079 
07080 bool CSGInterface::cmd_set_threshold()
07081 {
07082     if (m_nrhs!=2 || !create_return_values(0))
07083         return false;
07084 
07085     float64_t value=get_real_from_real_or_str();
07086 
07087     ui_math->set_threshold(value);
07088     return true;
07089 }
07090 
07091 bool CSGInterface::cmd_init_random()
07092 {
07093     if (m_nrhs!=2 || !create_return_values(0))
07094         return false;
07095 
07096     uint32_t initseed=(uint32_t) get_int_from_int_or_str();
07097     ui_math->init_random(initseed);
07098 
07099     return true;
07100 }
07101 
07102 bool CSGInterface::cmd_set_num_threads()
07103 {
07104     if (m_nrhs!=2 || !create_return_values(0))
07105         return false;
07106 
07107     int32_t num_threads=get_int_from_int_or_str();
07108 
07109     parallel->set_num_threads(num_threads);
07110     SG_INFO("Set number of threads to %d.\n", num_threads);
07111 
07112     return true;
07113 }
07114 
07115 bool CSGInterface::cmd_translate_string()
07116 {
07117     if (m_nrhs!=4 || !create_return_values(1))
07118         return false;
07119 
07120     float64_t* string=NULL;
07121     int32_t len;
07122     get_vector(string, len);
07123 
07124     int32_t order=get_int();
07125     int32_t start=get_int();
07126 
07127     const int32_t max_val=2; /* DNA->2bits */
07128     int32_t i,j;
07129     uint16_t* obs=SG_MALLOC(uint16_t, len);
07130 
07131     for (i=0; i<len; i++)
07132     {
07133         switch ((char) string[i])
07134         {
07135             case 'A': obs[i]=0; break;
07136             case 'C': obs[i]=1; break;
07137             case 'G': obs[i]=2; break;
07138             case 'T': obs[i]=3; break;
07139             case 'a': obs[i]=0; break;
07140             case 'c': obs[i]=1; break;
07141             case 'g': obs[i]=2; break;
07142             case 't': obs[i]=3; break;
07143             default: SG_ERROR("Wrong letter in string.\n");
07144         }
07145     }
07146 
07147     //convert interval of size T
07148     for (i=len-1; i>=order-1; i--)
07149     {
07150         uint16_t value=0;
07151         for (j=i; j>=i-order+1; j--)
07152             value=(value>>max_val) | ((obs[j])<<(max_val*(order-1)));
07153         
07154         obs[i]=(uint16_t) value;
07155     }
07156     
07157     for (i=order-2;i>=0;i--)
07158     {
07159         uint16_t value=0;
07160         for (j=i; j>=i-order+1; j--)
07161         {
07162             value= (value >> max_val);
07163             if (j>=0)
07164                 value|=(obs[j]) << (max_val * (order-1));
07165         }
07166         obs[i]=value;
07167     }
07168 
07169     float64_t* real_obs=SG_MALLOC(float64_t, len);
07170     for (i=start; i<len; i++)
07171         real_obs[i-start]=(float64_t) obs[i];
07172     SG_FREE(obs);
07173 
07174     set_vector(real_obs, len);
07175     SG_FREE(real_obs);
07176 
07177     return true;
07178 }
07179 
07180 bool CSGInterface::cmd_clear()
07181 {
07182     // reset guilib
07183     SG_UNREF(ui_classifier);
07184     ui_classifier=new CGUIClassifier(this);
07185     SG_UNREF(ui_distance);
07186     ui_distance=new CGUIDistance(this);
07187     SG_UNREF(ui_features);
07188     ui_features=new CGUIFeatures(this);
07189     SG_UNREF(ui_hmm);
07190     ui_hmm=new CGUIHMM(this);
07191     SG_UNREF(ui_kernel);
07192     ui_kernel=new CGUIKernel(this);
07193     SG_UNREF(ui_labels);
07194     ui_labels=new CGUILabels(this);
07195     SG_UNREF(ui_math);
07196     ui_math=new CGUIMath(this);
07197     SG_UNREF(ui_pluginestimate);
07198     ui_pluginestimate=new CGUIPluginEstimate(this);
07199     SG_UNREF(ui_preproc);
07200     ui_preproc=new CGUIPreprocessor(this);
07201     SG_UNREF(ui_time);
07202     ui_time=new CGUITime(this);
07203 
07204     return true;
07205 }
07206 
07207 bool CSGInterface::cmd_tic()
07208 {
07209     ui_time->start();
07210     return true;
07211 }
07212 
07213 bool CSGInterface::cmd_toc()
07214 {
07215     ui_time->stop();
07216     return true;
07217 }
07218 
07219 bool CSGInterface::cmd_print()
07220 {
07221     if (m_nrhs<2 || !create_return_values(0))
07222         return false;
07223 
07224     int32_t len=0;
07225     char* msg=get_str_from_str_or_direct(len);
07226 
07227     SG_PRINT("%s\n", msg);
07228 
07229     SG_FREE(msg);
07230     return true;
07231 }
07232 
07233 bool CSGInterface::cmd_echo()
07234 {
07235     if (m_nrhs<2 || !create_return_values(0))
07236         return false;
07237 
07238     int32_t len=0;
07239     char* level=get_str_from_str_or_direct(len);
07240 
07241     if (strmatch(level, "OFF"))
07242     {
07243         echo=false;
07244         SG_INFO("Echo is off.\n");
07245     }
07246     else
07247     {
07248         echo=true;
07249         SG_INFO("Echo is on.\n");
07250     }
07251 
07252     SG_FREE(level);
07253     return true;
07254 }
07255 
07256 bool CSGInterface::cmd_loglevel()
07257 {
07258     if (m_nrhs<2 || !create_return_values(0))
07259         return false;
07260 
07261     int32_t len=0;
07262     char* level=get_str_from_str_or_direct(len);
07263 
07264     if (strmatch(level, "ALL") || strmatch(level, "GCDEBUG"))
07265         io->set_loglevel(MSG_GCDEBUG);
07266     else if (strmatch(level, "DEBUG"))
07267         io->set_loglevel(MSG_DEBUG);
07268     else if (strmatch(level, "INFO"))
07269         io->set_loglevel(MSG_INFO);
07270     else if (strmatch(level, "NOTICE"))
07271         io->set_loglevel(MSG_NOTICE);
07272     else if (strmatch(level, "WARN"))
07273         io->set_loglevel(MSG_WARN);
07274     else if (strmatch(level, "ERROR"))
07275         io->set_loglevel(MSG_ERROR);
07276     else if (strmatch(level, "CRITICAL"))
07277         io->set_loglevel(MSG_CRITICAL);
07278     else if (strmatch(level, "ALERT"))
07279         io->set_loglevel(MSG_ALERT);
07280     else if (strmatch(level, "EMERGENCY"))
07281         io->set_loglevel(MSG_EMERGENCY);
07282     else
07283         SG_ERROR("Unknown loglevel '%s'.\n", level);
07284 
07285     SG_INFO("Loglevel set to %s.\n", level);
07286 
07287     SG_FREE(level);
07288     return true;
07289 }
07290 
07291 bool CSGInterface::cmd_syntax_highlight()
07292 {
07293     if (m_nrhs<2 || !create_return_values(0))
07294         return false;
07295 
07296     int32_t len=0;
07297     char* hili=get_str_from_str_or_direct(len);
07298 
07299     if (strmatch(hili, "ON"))
07300     {
07301         hilight.set_ansi_syntax_hilighting();
07302         io->enable_syntax_highlighting();
07303     }
07304     else if (strmatch(hili, "OFF"))
07305     {
07306         hilight.disable_syntax_hilighting();
07307         io->disable_syntax_highlighting();
07308     }
07309     else
07310         SG_ERROR("arguments to " N_SYNTAX_HIGHLIGHT " are ON|OFF - found '%s'.\n", hili);
07311 
07312     SG_INFO("Syntax hilighting set to %s.\n", hili);
07313 
07314     SG_FREE(hili);
07315     return true;
07316 }
07317 
07318 bool CSGInterface::cmd_progress()
07319 {
07320     if (m_nrhs<2 || !create_return_values(0))
07321         return false;
07322 
07323     int32_t len=0;
07324     char* progress=get_str_from_str_or_direct(len);
07325 
07326     if (strmatch(progress, "ON"))
07327         io->enable_progress();
07328     else if (strmatch(progress, "OFF"))
07329         io->disable_progress();
07330     else
07331         SG_ERROR("arguments to progress are ON|OFF - found '%s'.\n", progress);
07332 
07333     SG_INFO("Progress set to %s.\n", progress);
07334 
07335     SG_FREE(progress);
07336     return true;
07337 }
07338 
07339 bool CSGInterface::cmd_get_version()
07340 {
07341     if (m_nrhs!=1 || !create_return_values(1))
07342         return false;
07343 
07344     set_int(version->get_version_revision());
07345 
07346     return true;
07347 }
07348 
07349 bool CSGInterface::cmd_help()
07350 {
07351     if ((m_nrhs!=1 && m_nrhs!=2) || !create_return_values(0))
07352         return false;
07353 
07354     int32_t i=0;
07355 
07356     SG_PRINT("\n");
07357     if (m_nrhs==1) // unspecified help
07358     {
07359         SG_PRINT("Help is available for the following topics.\n"
07360                  "-------------------------------------------\n\n");
07361         while (sg_methods[i].command)
07362         {
07363             bool is_group_item=false;
07364             if (!sg_methods[i].method && !sg_methods[i].usage_prefix)
07365                 is_group_item=true;
07366 
07367             if (is_group_item)
07368             {
07369                 SG_PRINT("%s%s%s\n",
07370                         hilight.get_command_prefix(),
07371                         sg_methods[i].command,
07372                         hilight.get_command_suffix());
07373             }
07374 
07375             i++;
07376         }
07377         SG_PRINT("\nUse sg('%shelp%s', '%s<topic>%s')"
07378                 " to see the list of commands in this group, e.g.\n\n"
07379                 "\tsg('%shelp%s', '%sFeatures%s')\n\n"
07380                 "to see the list of commands for the 'Features' group.\n"
07381                 "\nOr use sg('%shelp%s', '%sall%s')"
07382                 " to see a brief listing of all commands.\n\nTo disable syntax"
07383                 " highlighting (useful e.g. in the matlab GUI) use\n\n"
07384                 "\tsg('syntax_highlight','OFF')\n",
07385                     hilight.get_command_prefix(), hilight.get_command_suffix(),
07386                     hilight.get_command_prefix(), hilight.get_command_suffix(),
07387                     hilight.get_command_prefix(), hilight.get_command_suffix(),
07388                     hilight.get_command_prefix(), hilight.get_command_suffix(),
07389                     hilight.get_command_prefix(), hilight.get_command_suffix(),
07390                     hilight.get_command_prefix(), hilight.get_command_suffix());
07391     }
07392     else // m_nrhs == 2 -> all commands, single command or group help
07393     {
07394         bool found=false;
07395         bool in_group=false;
07396         int32_t clen=0;
07397         char* command=get_string(clen);
07398 
07399         if (strmatch("doxygen", command) || strmatch("DOXYGEN", command))
07400         {
07401             found=true;
07402             while (sg_methods[i].command)
07403             {
07404                 if (sg_methods[i].usage_prefix) // display group item
07405                 {
07406                     SG_PRINT("\\arg \\b %s \\verbatim %s%s%s \\endverbatim\n",
07407                             sg_methods[i].command,
07408                             sg_methods[i].usage_prefix,
07409                             sg_methods[i].command,
07410                             sg_methods[i].usage_suffix);
07411                 }
07412                 else if (!sg_methods[i].method) // display group
07413                 {
07414                     SG_PRINT("\n\\section %s_sec %s\n", 
07415                             sg_methods[i].command, sg_methods[i].command);
07416                 }
07417                 i++;
07418             }
07419         }
07420         if (strmatch("all", command) || strmatch("ALL", command))
07421         {
07422             found=true;
07423             while (sg_methods[i].command)
07424             {
07425                 if (sg_methods[i].usage_prefix) // display group item
07426                 {
07427                     SG_PRINT("\t%s%s%s%s%s\n", sg_methods[i].usage_prefix, 
07428                             hilight.get_command_prefix(),
07429                             sg_methods[i].command,
07430                             hilight.get_command_suffix(),
07431                             sg_methods[i].usage_suffix);
07432                 }
07433                 else if (!sg_methods[i].method) // display group
07434                 {
07435                     SG_PRINT("\nCommands in group %s%s%s\n", 
07436                             hilight.get_command_prefix(),
07437                             sg_methods[i].command,
07438                             hilight.get_command_suffix());
07439                 }
07440                 i++;
07441             }
07442         }
07443         else
07444         {
07445             while (sg_methods[i].command)
07446             {
07447                 if (in_group)
07448                 {
07449                     if (sg_methods[i].usage_prefix) // display group item
07450                         SG_PRINT("\t%s%s%s\n",
07451                             hilight.get_command_prefix(),
07452                             sg_methods[i].command,
07453                             hilight.get_command_suffix());
07454                     else // next group reached -> end
07455                         break;
07456                 }
07457                 else
07458                 {
07459                     found=strmatch(sg_methods[i].command, command);
07460                     if (found)
07461                     {
07462                         if (sg_methods[i].usage_prefix) // found item
07463                         {
07464                             SG_PRINT("Usage for %s%s%s\n\n\t%s%s%s%s%s\n",
07465                                     hilight.get_command_prefix(),
07466                                     sg_methods[i].command,
07467                                     hilight.get_command_suffix(),
07468                                     sg_methods[i].usage_prefix,
07469                                     hilight.get_command_prefix(),
07470                                     sg_methods[i].command,
07471                                     hilight.get_command_suffix(),
07472                                     sg_methods[i].usage_suffix);
07473                             break;
07474                         }
07475                         else // found group item
07476                         {
07477                             SG_PRINT("Commands in group %s%s%s\n\n",
07478                                     hilight.get_command_prefix(),
07479                                     sg_methods[i].command,
07480                                     hilight.get_command_suffix());
07481                             in_group=true;
07482                         }
07483                     }
07484                 }
07485 
07486                 i++;
07487             }
07488         }
07489 
07490         if (!found)
07491             SG_PRINT("Could not find help for command %s.\n", command);
07492         else if (in_group)
07493         {
07494             SG_PRINT("\n\nUse sg('%shelp%s', '%s<command>%s')"
07495                     " to see the usage pattern of a single command, e.g.\n\n"
07496                     "\tsg('%shelp%s', '%sclassify%s')\n\n"
07497                     " to see the usage pattern of the command 'classify'.\n",
07498                     hilight.get_command_prefix(), hilight.get_command_suffix(),
07499                     hilight.get_command_prefix(), hilight.get_command_suffix(),
07500                     hilight.get_command_prefix(), hilight.get_command_suffix(),
07501                     hilight.get_command_prefix(), hilight.get_command_suffix());
07502         }
07503 
07504         SG_FREE(command);
07505     }
07506 
07507 
07508     SG_PRINT("\n");
07509 
07510     return true;
07511 }
07512 #ifdef TRACE_MEMORY_ALLOCS
07513     extern CSet<MemoryBlock>* sg_mallocs;
07514 #endif
07515 
07516 bool CSGInterface::cmd_whos()
07517 {
07518     if ((m_nrhs!=1) || !create_return_values(0))
07519         return false;
07520 
07521 #ifdef TRACE_MEMORY_ALLOCS
07522     SG_PRINT("Blocks allocated by shogun\n");
07523     list_memory_allocs();
07524     SG_PRINT("\n");
07525     return true;
07526 #else
07527     SG_PRINT("Requires shogun to be compiled with --enable-trace-mallocs\n");
07528     return false;
07529 #endif
07530 }
07531 
07532 bool CSGInterface::cmd_send_command()
07533 {
07534     SG_DEPRECATED;
07535 
07536     int32_t len=0;
07537     char* arg=get_string(len);
07538     //SG_DEBUG("legacy: arg == %s\n", arg);
07539     m_legacy_strptr=arg;
07540 
07541     char* command=get_str_from_str(len);
07542     int32_t i=0;
07543     bool success=false;
07544 
07545     while (sg_methods[i].command)
07546     {
07547         if (strmatch(command, sg_methods[i].command))
07548         {
07549             SG_DEBUG("legacy: found command %s\n", sg_methods[i].command);
07550             // fix-up m_nrhs; +1 to include command
07551             m_nrhs=get_num_args_in_str()+1;
07552 
07553             if (!(interface->*(sg_methods[i].method))())
07554             {
07555                 SG_ERROR("Usage: %s%s%s\n\n\t%s%s%s%s%s\n",
07556                         hilight.get_command_prefix(),
07557                         sg_methods[i].command,
07558                         hilight.get_command_suffix(),
07559                         sg_methods[i].usage_prefix,
07560                         hilight.get_command_prefix(),
07561                         sg_methods[i].command,
07562                         hilight.get_command_suffix(),
07563                         sg_methods[i].usage_suffix);
07564             }
07565             else
07566             {
07567                 success=true;
07568                 break;
07569             }
07570         }
07571 
07572         i++;
07573     }
07574 
07575     if (!success)
07576         SG_ERROR("Non-supported legacy command %s.\n", command);
07577 
07578     SG_FREE(command);
07579     SG_FREE(arg);
07580     return success;
07581 }
07582 
07583 bool CSGInterface::cmd_run_python()
07584 {
07585     SG_ERROR("Only available in the elwms interface\n");
07586     return false;
07587 }
07588 
07589 bool CSGInterface::cmd_run_octave()
07590 {
07591     SG_ERROR("Only available in the elwms interface\n");
07592     return false;
07593 }
07594 
07595 bool CSGInterface::cmd_run_r()
07596 {
07597     SG_ERROR("Only available in the elwms interface\n");
07598     return false;
07599 }
07600 
07601 bool CSGInterface::cmd_pr_loqo()
07602 {
07603     if (m_nrhs!=7 || !create_return_values(2))
07604         return false;
07605 
07606     float64_t* c=NULL;
07607     int32_t lenc=0;
07608     get_vector(c, lenc);
07609 
07610     int32_t n = lenc;
07611 
07612     float64_t* H=NULL;
07613     int32_t nH=0;
07614     int32_t mH=0;
07615     get_matrix(H, nH, mH);
07616     ASSERT(nH==n && mH==n);
07617 
07618     float64_t* A=NULL;
07619     int32_t nA=0;
07620     int32_t mA=0;
07621     get_matrix(A, nA, mA);
07622     ASSERT(mA==n);
07623     int32_t m=nA;
07624 
07625     float64_t* b=NULL;
07626     int32_t lenb=0;
07627     get_vector(b, lenb);
07628     ASSERT(lenb==m);
07629 
07630     float64_t* l=NULL;
07631     int32_t lenl=0;
07632     get_vector(l, lenl);
07633     ASSERT(lenl==n);
07634 
07635     float64_t* u=NULL;
07636     int32_t lenu=0;
07637     get_vector(u, lenu);
07638     ASSERT(lenu==n);
07639 
07640     float64_t* x=SG_MALLOC(float64_t, 3*n);
07641     CMath::fill_vector(x, 3*n, 0.0);
07642 
07643     float64_t* y=SG_MALLOC(float64_t, m+2*n);
07644     CMath::fill_vector(y, m+2*n, 0.0);
07645 
07646     pr_loqo(n,m, c, H, A, b, l, u, x, y, 0, 5, 50, 0.05, 100, 0);
07647 
07648     set_vector(x, n);
07649     set_vector(y, m);
07650 
07651     SG_FREE(c);
07652     SG_FREE(H);
07653     SG_FREE(A);
07654     SG_FREE(b);
07655     SG_FREE(l);
07656     SG_FREE(u);
07657     SG_FREE(x);
07658     SG_FREE(y);
07659     return true;
07660 }
07661 
07662 void CSGInterface::print_prompt()
07663 {
07664     SG_PRINT("%sshogun%s >> ",
07665             hilight.get_prompt_prefix(),
07666             hilight.get_prompt_suffix());
07667 }
07668 
07670 // legacy-related methods
07672 
07673 char* CSGInterface::get_str_from_str_or_direct(int32_t& len)
07674 {
07675     if (m_legacy_strptr)
07676         return get_str_from_str(len);
07677     else
07678         return get_string(len);
07679 }
07680 
07681 int32_t CSGInterface::get_int_from_int_or_str()
07682 {
07683     if (m_legacy_strptr)
07684     {
07685         int32_t len=0;
07686         char* str=get_str_from_str(len);
07687         int32_t val=strtol(str, NULL, 10);
07688 
07689         SG_FREE(str);
07690         return val;
07691     }
07692     else
07693         return get_int();
07694 }
07695 
07696 float64_t CSGInterface::get_real_from_real_or_str()
07697 {
07698     if (m_legacy_strptr)
07699     {
07700         int32_t len=0;
07701         char* str=get_str_from_str(len);
07702         float64_t val=strtod(str, NULL);
07703 
07704         SG_FREE(str);
07705         return val;
07706     }
07707     else
07708         return get_real();
07709 }
07710 
07711 bool CSGInterface::get_bool_from_bool_or_str()
07712 {
07713     if (m_legacy_strptr)
07714     {
07715         int32_t len=0;
07716         char* str=get_str_from_str(len);
07717         bool val=strtol(str, NULL, 10)!=0;
07718 
07719         SG_FREE(str);
07720         return val;
07721     }
07722     else
07723         return get_bool();
07724 }
07725 
07726 void CSGInterface::get_vector_from_int_vector_or_str(int32_t*& vector, int32_t& len)
07727 {
07728     if (m_legacy_strptr)
07729     {
07730         len=get_vector_len_from_str(len);
07731         if (len==0)
07732         {
07733             vector=NULL;
07734             return;
07735         }
07736 
07737         vector=SG_MALLOC(int32_t, len);
07738         char* str=NULL;
07739         int32_t slen=0;
07740         for (int32_t i=0; i<len; i++)
07741         {
07742             str=get_str_from_str(slen);
07743             vector[i]=strtol(str, NULL, 10);
07744             //SG_DEBUG("vec[%d]: %d\n", i, vector[i]);
07745             SG_FREE(str);
07746         }
07747     }
07748     else
07749         get_vector(vector, len);
07750 }
07751 
07752 void CSGInterface::get_vector_from_real_vector_or_str(
07753     float64_t*& vector, int32_t& len)
07754 {
07755     if (m_legacy_strptr)
07756     {
07757         len=get_vector_len_from_str(len);
07758         if (len==0)
07759         {
07760             vector=NULL;
07761             return;
07762         }
07763 
07764         vector=SG_MALLOC(float64_t, len);
07765         char* str=NULL;
07766         int32_t slen=0;
07767         for (int32_t i=0; i<len; i++)
07768         {
07769             str=get_str_from_str(slen);
07770             vector[i]=strtod(str, NULL);
07771             //SG_DEBUG("vec[%d]: %f\n", i, vector[i]);
07772             SG_FREE(str);
07773         }
07774     }
07775     else
07776         get_vector(vector, len);
07777 }
07778 
07779 int32_t CSGInterface::get_vector_len_from_str(int32_t expected_len)
07780 {
07781     int32_t num_args=get_num_args_in_str();
07782 
07783     if (expected_len==0 || num_args==expected_len)
07784         return num_args;
07785     else if (num_args==2*expected_len)
07786     {
07787         // special case for position_weights; a bit shaky...
07788         return expected_len;
07789     }
07790     else
07791         SG_ERROR("Expected vector length %d does not match actual length %d.\n", expected_len, num_args);
07792 
07793     return 0;
07794 }
07795 
07796 char* CSGInterface::get_str_from_str(int32_t& len)
07797 {
07798     if (!m_legacy_strptr)
07799         return NULL;
07800 
07801     int32_t i=0;
07802     while (m_legacy_strptr[i]!='\0' && !isspace(m_legacy_strptr[i]))
07803         i++;
07804 
07805     len=i;
07806     char* str=SG_MALLOC(char, len+1);
07807     for (i=0; i<len; i++)
07808         str[i]=m_legacy_strptr[i];
07809     str[len]='\0';
07810 
07811     // move legacy strptr
07812     if (m_legacy_strptr[len]=='\0')
07813         m_legacy_strptr=NULL;
07814     else
07815     {
07816         m_legacy_strptr=m_legacy_strptr+len;
07817         m_legacy_strptr=SGIO::skip_spaces(m_legacy_strptr);
07818     }
07819 
07820     return str;
07821 }
07822 
07823 int32_t CSGInterface::get_num_args_in_str()
07824 {
07825     if (!m_legacy_strptr)
07826         return 0;
07827 
07828     int32_t count=0;
07829     int32_t i=0;
07830     bool in_arg=false;
07831     while (m_legacy_strptr[i]!='\0')
07832     {
07833         if (!isspace(m_legacy_strptr[i]) && !in_arg)
07834         {
07835             count++;
07836             in_arg=true;
07837         }
07838         else if (isspace(m_legacy_strptr[i]) && in_arg)
07839             in_arg=false;
07840 
07841         i++;
07842     }
07843 
07844     return count;
07845 }
07846 
07848 // handler
07850 
07851 bool CSGInterface::handle()
07852 {
07853     int32_t len=0;
07854     bool success=false;
07855 
07856 #ifndef WIN32
07857     CSignal::set_handler();
07858 #endif
07859 
07860     char* command=NULL;
07861     command=interface->get_command(len);
07862 
07863     SG_DEBUG("command: %s, nrhs %d\n", command, m_nrhs);
07864     int32_t i=0;
07865     while (sg_methods[i].command)
07866     {
07867         if (strmatch(command, sg_methods[i].command))
07868         {
07869             SG_DEBUG("found command %s%s%s\n",
07870                     hilight.get_command_prefix(),
07871                     sg_methods[i].command,
07872                     hilight.get_command_suffix());
07873 
07874             if (!(interface->*(sg_methods[i].method))())
07875             {
07876                 if (sg_methods[i].usage_prefix)
07877                 {
07878                     SG_ERROR("Usage: %s%s%s\n\n\t%s%s%s%s%s\n",
07879                             hilight.get_command_prefix(),
07880                             sg_methods[i].command,
07881                             hilight.get_command_suffix(),
07882                             sg_methods[i].usage_prefix,
07883                             hilight.get_command_prefix(),
07884                             sg_methods[i].command,
07885                             hilight.get_command_suffix(),
07886                             sg_methods[i].usage_suffix);
07887                 }
07888                 else
07889                     SG_ERROR("Non-supported command %s%s%s.\n",
07890                             hilight.get_command_prefix(),
07891                             sg_methods[i].command,
07892                             hilight.get_command_suffix());
07893             }
07894             else
07895             {
07896                 success=true;
07897                 break;
07898             }
07899         }
07900         i++;
07901     }
07902 
07903 #ifndef WIN32
07904     CSignal::unset_handler();
07905 #endif
07906 
07907     if (!success)
07908         SG_ERROR("Unknown command %s%s%s.\n",
07909                 hilight.get_command_prefix(),
07910                 command,
07911                 hilight.get_command_suffix());
07912 
07913     SG_FREE(command);
07914     return success;
07915 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation