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

SHOGUN Machine Learning Toolbox - Documentation