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

SHOGUN Machine Learning Toolbox - Documentation