SHOGUN  v2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
SGInterface.cpp
Go to the documentation of this file.
00001 #include <shogun/ui/GUICommands.h>
00002 #include <shogun/ui/SGInterface.h>
00003 #include <shogun/ui/SyntaxHighLight.h>
00004 
00005 #include <shogun/lib/config.h>
00006 #include <shogun/lib/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             int32_t len=0;
04074             const float64_t* weights=k->get_normal(len);
04075 
04076             set_vector(weights, len);
04077             return true;
04078         }
04079         default:
04080             SG_ERROR("Unsupported kernel %s.\n", kernel->get_name());
04081     }
04082 
04083     return true;
04084 }
04085 
04086 bool CSGInterface::cmd_delete_kernel_optimization()
04087 {
04088     if (m_nrhs<1 || !create_return_values(0))
04089         return false;
04090 
04091     return ui_kernel->delete_kernel_optimization();
04092 }
04093 
04094 bool CSGInterface::cmd_use_diagonal_speedup()
04095 {
04096     if (m_nrhs<2 || !create_return_values(0))
04097         return false;
04098 
04099     bool speedup=get_bool();
04100 
04101     CKernel* kernel=ui_kernel->get_kernel();
04102     if (!kernel)
04103         SG_ERROR("No kernel defined.\n");
04104 
04105     if (kernel->get_kernel_type()==K_COMBINED)
04106     {
04107         SG_DEBUG("Identified combined kernel.\n");
04108         kernel=((CCombinedKernel*) kernel)->get_last_kernel();
04109         if (!kernel)
04110             SG_ERROR("No last kernel defined.\n");
04111     }
04112 
04113     if (kernel->get_kernel_type()!=K_COMMWORDSTRING)
04114         SG_ERROR("Currently only commwordstring kernel supports diagonal speedup\n");
04115 
04116     ((CCommWordStringKernel*) kernel)->set_use_dict_diagonal_optimization(speedup);
04117 
04118     SG_INFO("Diagonal speedup %s.\n", speedup ? "enabled" : "disabled");
04119 
04120     return true;
04121 }
04122 
04123 bool CSGInterface::cmd_set_kernel_optimization_type()
04124 {
04125     if (m_nrhs<2 || !create_return_values(0))
04126         return false;
04127 
04128     int32_t len=0;
04129     char* opt_type=get_str_from_str_or_direct(len);
04130 
04131     bool success=ui_kernel->set_optimization_type(opt_type);
04132 
04133     SG_FREE(opt_type);
04134     return success;
04135 }
04136 
04137 bool CSGInterface::cmd_set_solver()
04138 {
04139     if (m_nrhs<2 || !create_return_values(0))
04140         return false;
04141 
04142     int32_t len=0;
04143     char* solver=get_str_from_str_or_direct(len);
04144 
04145     bool success=ui_classifier->set_solver(solver);
04146 
04147     SG_FREE(solver);
04148     return success;
04149 }
04150 
04151 bool CSGInterface::cmd_set_constraint_generator()
04152 {
04153     if (m_nrhs<2 || !create_return_values(0))
04154         return false;
04155 
04156     int32_t len=0;
04157     char* cg=get_str_from_str_or_direct(len);
04158 
04159     bool success=ui_classifier->set_constraint_generator(cg);
04160 
04161     SG_FREE(cg);
04162     return success;
04163 }
04164 
04165 bool CSGInterface::cmd_set_prior_probs()
04166 {
04167     if (m_nrhs<3 || !create_return_values(0))
04168         return false;
04169 
04170     CSalzbergWordStringKernel* kernel=
04171         (CSalzbergWordStringKernel*) ui_kernel->get_kernel();
04172     if (kernel->get_kernel_type()!=K_SALZBERG)
04173         SG_ERROR("SalzbergWordStringKernel required for setting prior probs!\n");
04174 
04175     float64_t pos_probs=get_real_from_real_or_str();
04176     float64_t neg_probs=get_real_from_real_or_str();
04177 
04178     kernel->set_prior_probs(pos_probs, neg_probs);
04179 
04180     return true;
04181 }
04182 
04183 bool CSGInterface::cmd_set_prior_probs_from_labels()
04184 {
04185     if (m_nrhs<2 || !create_return_values(0))
04186         return false;
04187 
04188     CSalzbergWordStringKernel* kernel=
04189         (CSalzbergWordStringKernel*) ui_kernel->get_kernel();
04190     if (kernel->get_kernel_type()!=K_SALZBERG)
04191     SG_ERROR("SalzbergWordStringKernel required for setting prior probs!\n");
04192 
04193     float64_t* lab=NULL;
04194     int32_t len=0;
04195     get_vector(lab, len);
04196 
04197     CBinaryLabels* labels=new CBinaryLabels(len);
04198     for (int32_t i=0; i<len; i++)
04199     {
04200         if (!labels->set_label(i, lab[i]))
04201             SG_ERROR("Couldn't set label %d (of %d): %f.\n", i, len, lab[i]);
04202     }
04203     SG_FREE(lab);
04204 
04205     kernel->set_prior_probs_from_labels(labels);
04206 
04207     SG_UNREF(labels);
04208     return true;
04209 }
04210 
04211 #ifdef USE_SVMLIGHT
04212 bool CSGInterface::cmd_resize_kernel_cache()
04213 {
04214     if (m_nrhs<2 || !create_return_values(0))
04215         return false;
04216 
04217     int32_t size=get_int_from_int_or_str();
04218     return ui_kernel->resize_kernel_cache(size);
04219 }
04220 #endif //USE_SVMLIGHT
04221 
04222 
04225 bool CSGInterface::cmd_set_distance()
04226 {
04227     if (m_nrhs<3 || !create_return_values(0))
04228         return false;
04229 
04230     CDistance* distance=NULL;
04231     int32_t len=0;
04232     char* type=get_str_from_str_or_direct(len);
04233     char* dtype=get_str_from_str_or_direct(len);
04234 
04235     if (strmatch(type, "MINKOWSKI") && m_nrhs==4)
04236     {
04237         float64_t k=get_real_from_real_or_str();
04238         distance=ui_distance->create_minkowski(k);
04239     }
04240     else if (strmatch(type, "MANHATTAN"))
04241     {
04242         if (strmatch(dtype, "REAL"))
04243             distance=ui_distance->create_generic(D_MANHATTAN);
04244         else if (strmatch(dtype, "WORD"))
04245             distance=ui_distance->create_generic(D_MANHATTANWORD);
04246     }
04247     else if (strmatch(type, "HAMMING") && strmatch(dtype, "WORD"))
04248     {
04249         bool use_sign=false;
04250         if (m_nrhs==4)
04251             use_sign=get_bool_from_bool_or_str(); // optional
04252 
04253         distance=ui_distance->create_hammingword(use_sign);
04254     }
04255     else if (strmatch(type, "CANBERRA"))
04256     {
04257         if (strmatch(dtype, "REAL"))
04258             distance=ui_distance->create_generic(D_CANBERRA);
04259         else if (strmatch(dtype, "WORD"))
04260             distance=ui_distance->create_generic(D_CANBERRAWORD);
04261     }
04262     else if (strmatch(type, "CHEBYSHEW") && strmatch(dtype, "REAL"))
04263     {
04264         distance=ui_distance->create_generic(D_CHEBYSHEW);
04265     }
04266     else if (strmatch(type, "GEODESIC") && strmatch(dtype, "REAL"))
04267     {
04268         distance=ui_distance->create_generic(D_GEODESIC);
04269     }
04270     else if (strmatch(type, "JENSEN") && strmatch(dtype, "REAL"))
04271     {
04272         distance=ui_distance->create_generic(D_JENSEN);
04273     }
04274     else if (strmatch(type, "CHISQUARE") && strmatch(dtype, "REAL"))
04275     {
04276         distance=ui_distance->create_generic(D_CHISQUARE);
04277     }
04278     else if (strmatch(type, "TANIMOTO") && strmatch(dtype, "REAL"))
04279     {
04280         distance=ui_distance->create_generic(D_TANIMOTO);
04281     }
04282     else if (strmatch(type, "COSINE") && strmatch(dtype, "REAL"))
04283     {
04284         distance=ui_distance->create_generic(D_COSINE);
04285     }
04286     else if (strmatch(type, "BRAYCURTIS") && strmatch(dtype, "REAL"))
04287     {
04288         distance=ui_distance->create_generic(D_BRAYCURTIS);
04289     }
04290     else if (strmatch(type, "EUCLIDEAN"))
04291     {
04292         if (strmatch(dtype, "REAL"))
04293             distance=ui_distance->create_generic(D_EUCLIDEAN);
04294         else if (strmatch(dtype, "SPARSEREAL"))
04295             distance=ui_distance->create_generic(D_SPARSEEUCLIDEAN);
04296     }
04297     else
04298         SG_NOTIMPLEMENTED;
04299 
04300     SG_FREE(type);
04301     SG_FREE(dtype);
04302     return ui_distance->set_distance(distance);
04303 }
04304 
04305 bool CSGInterface::cmd_init_distance()
04306 {
04307     SG_DEPRECATED;
04308     return true;
04309 }
04310 
04311 bool CSGInterface::cmd_get_distance_matrix()
04312 {
04313     if (m_nrhs!=2 || !create_return_values(1))
04314         return false;
04315 
04316     int32_t len=0;
04317     char* target=get_string(len);
04318 
04319     bool success=ui_distance->init_distance(target);
04320 
04321     if (success)
04322     {
04323         CDistance* distance=ui_distance->get_distance();
04324         if (!distance || !distance->has_features())
04325             SG_ERROR("No distance defined or not initialized.\n");
04326 
04327         int32_t num_vec_lhs=0;
04328         int32_t num_vec_rhs=0;
04329         float64_t* dmatrix=NULL;
04330         dmatrix=distance->get_distance_matrix_real(num_vec_lhs, num_vec_rhs, dmatrix);
04331 
04332         set_matrix(dmatrix, num_vec_lhs, num_vec_rhs);
04333         SG_FREE(dmatrix);
04334     }
04335 
04336     return success;
04337 }
04338 
04339 
04340 /* POIM */
04341 
04342 bool CSGInterface::cmd_get_SPEC_consensus()
04343 {
04344     if (m_nrhs!=1 || !create_return_values(1))
04345         return false;
04346 
04347     CKernel* kernel=ui_kernel->get_kernel();
04348     if (!kernel)
04349         SG_ERROR("No kernel.\n");
04350     if (kernel->get_kernel_type()!=K_COMMWORDSTRING)
04351         SG_ERROR("Only works for CommWordString kernels.\n");
04352 
04353     CSVM* svm=(CSVM*) ui_classifier->get_classifier();
04354     ASSERT(svm);
04355     int32_t num_suppvec=svm->get_num_support_vectors();
04356     int32_t* sv_idx=SG_MALLOC(int32_t, num_suppvec);
04357     float64_t* sv_weight=SG_MALLOC(float64_t, num_suppvec);
04358     int32_t num_feat=0;
04359 
04360     for (int32_t i=0; i<num_suppvec; i++)
04361     {
04362         sv_idx[i]=svm->get_support_vector(i);
04363         sv_weight[i]=svm->get_alpha(i);
04364     }
04365 
04366     char* consensus=((CCommWordStringKernel*) kernel)->compute_consensus(
04367         num_feat, num_suppvec, sv_idx, sv_weight);
04368     SG_FREE(sv_idx);
04369     SG_FREE(sv_weight);
04370 
04371     set_vector(consensus, num_feat);
04372     SG_FREE(consensus);
04373 
04374     return true;
04375 }
04376 
04377 bool CSGInterface::cmd_get_SPEC_scoring()
04378 {
04379     if (m_nrhs!=2 || !create_return_values(1))
04380         return false;
04381 
04382     int32_t max_order=get_int();
04383     CKernel* kernel=ui_kernel->get_kernel();
04384     if (!kernel)
04385         SG_ERROR("No kernel.\n");
04386 
04387     EKernelType ktype=kernel->get_kernel_type();
04388     if (ktype!=K_COMMWORDSTRING && ktype!=K_WEIGHTEDCOMMWORDSTRING)
04389         SG_ERROR("Only works for (Weighted) CommWordString kernels.\n");
04390 
04391     CSVM* svm=(CSVM*) ui_classifier->get_classifier();
04392     ASSERT(svm);
04393     int32_t num_suppvec=svm->get_num_support_vectors();
04394     int32_t* sv_idx=SG_MALLOC(int32_t, num_suppvec);
04395     float64_t* sv_weight=SG_MALLOC(float64_t, num_suppvec);
04396     int32_t num_feat=0;
04397     int32_t num_sym=0;
04398 
04399     for (int32_t i=0; i<num_suppvec; i++)
04400     {
04401         sv_idx[i]=svm->get_support_vector(i);
04402         sv_weight[i]=svm->get_alpha(i);
04403     }
04404 
04405     if ((max_order<1) || (max_order>8))
04406     {
04407         SG_WARNING( "max_order out of range 1..8 (%d). setting to 1\n", max_order);
04408         max_order=1;
04409     }
04410 
04411     float64_t* position_weights=NULL;
04412     if (ktype==K_COMMWORDSTRING)
04413         position_weights=((CCommWordStringKernel*) kernel)->compute_scoring(
04414             max_order, num_feat, num_sym, NULL,
04415             num_suppvec, sv_idx, sv_weight);
04416     else
04417         position_weights=((CWeightedCommWordStringKernel*) kernel)->compute_scoring(
04418             max_order, num_feat, num_sym, NULL,
04419             num_suppvec, sv_idx, sv_weight);
04420     SG_FREE(sv_idx);
04421     SG_FREE(sv_weight);
04422 
04423     set_matrix(position_weights, num_sym, num_feat);
04424     SG_FREE(position_weights);
04425 
04426     return true;
04427 }
04428 
04429 bool CSGInterface::cmd_get_WD_consensus()
04430 {
04431     if (m_nrhs!=1 || !create_return_values(1))
04432         return false;
04433 
04434     CKernel* kernel=ui_kernel->get_kernel();
04435     if (!kernel)
04436         SG_ERROR("No kernel.\n");
04437     if (kernel->get_kernel_type()!=K_WEIGHTEDDEGREEPOS)
04438         SG_ERROR("Only works for Weighted Degree Position kernels.\n");
04439 
04440     CSVM* svm=(CSVM*) ui_classifier->get_classifier();
04441     ASSERT(svm);
04442     int32_t num_suppvec=svm->get_num_support_vectors();
04443     int32_t* sv_idx=SG_MALLOC(int32_t, num_suppvec);
04444     float64_t* sv_weight=SG_MALLOC(float64_t, num_suppvec);
04445     int32_t num_feat=0;
04446 
04447     for (int32_t i=0; i<num_suppvec; i++)
04448     {
04449         sv_idx[i]=svm->get_support_vector(i);
04450         sv_weight[i]=svm->get_alpha(i);
04451     }
04452 
04453     char* consensus=((CWeightedDegreePositionStringKernel*) kernel)->compute_consensus(
04454             num_feat, num_suppvec, sv_idx, sv_weight);
04455     SG_FREE(sv_idx);
04456     SG_FREE(sv_weight);
04457 
04458     set_vector(consensus, num_feat);
04459     SG_FREE(consensus);
04460 
04461     return true;
04462 }
04463 
04464 bool CSGInterface::cmd_compute_POIM_WD()
04465 {
04466     if (m_nrhs!=3 || !create_return_values(1))
04467         return false;
04468 
04469     int32_t max_order=get_int();
04470     float64_t* distribution=NULL;
04471     int32_t num_dfeat=0;
04472     int32_t num_dvec=0;
04473     get_matrix(distribution, num_dfeat, num_dvec);
04474 
04475     if (!distribution)
04476         SG_ERROR("Wrong distribution.\n");
04477 
04478     CKernel* kernel=ui_kernel->get_kernel();
04479     if (!kernel)
04480         SG_ERROR("No Kernel.\n");
04481     if (kernel->get_kernel_type()!=K_WEIGHTEDDEGREEPOS)
04482         SG_ERROR("Only works for Weighted Degree Position kernels.\n");
04483 
04484     int32_t seqlen=0;
04485     int32_t num_sym=0;
04486     CStringFeatures<char>* sfeat=(CStringFeatures<char>*)
04487         (((CWeightedDegreePositionStringKernel*) kernel)->get_lhs());
04488     ASSERT(sfeat);
04489     seqlen=sfeat->get_max_vector_length();
04490     num_sym=(int32_t) sfeat->get_num_symbols();
04491 
04492     if (num_dvec!=seqlen || num_dfeat!=num_sym)
04493     {
04494         SG_ERROR("distribution should have (seqlen x num_sym) elements"
04495                 "(seqlen: %d vs. %d symbols: %d vs. %d)\n", seqlen,
04496                 num_dvec, num_sym, num_dfeat);
04497     }
04498 
04499         CSVM* svm=(CSVM*) ui_classifier->get_classifier();
04500         ASSERT(svm);
04501         int32_t num_suppvec=svm->get_num_support_vectors();
04502         int32_t* sv_idx=SG_MALLOC(int32_t, num_suppvec);
04503         float64_t* sv_weight=SG_MALLOC(float64_t, num_suppvec);
04504 
04505         for (int32_t i=0; i<num_suppvec; i++)
04506         {
04507             sv_idx[i]=svm->get_support_vector(i);
04508             sv_weight[i]=svm->get_alpha(i);
04509         }
04510 
04511         /*
04512         if ((max_order < 1) || (max_order > 12))
04513         {
04514             SG_WARNING( "max_order out of range 1..12 (%d). setting to 1.\n", max_order);
04515             max_order=1;
04516         }
04517         */
04518 
04519         float64_t* position_weights;
04520         position_weights=((CWeightedDegreePositionStringKernel*) kernel)->compute_POIM(
04521                 max_order, seqlen, num_sym, NULL,
04522                 num_suppvec, sv_idx, sv_weight, distribution);
04523         SG_FREE(sv_idx);
04524         SG_FREE(sv_weight);
04525 
04526         set_matrix(position_weights, num_sym, seqlen);
04527         SG_FREE(position_weights);
04528 
04529         return true;
04530     }
04531 
04532     bool CSGInterface::cmd_get_WD_scoring()
04533     {
04534         if (m_nrhs!=2 || !create_return_values(1))
04535             return false;
04536 
04537         int32_t max_order=get_int();
04538 
04539         CKernel* kernel=ui_kernel->get_kernel();
04540         if (!kernel)
04541             SG_ERROR("No kernel.\n");
04542         if (kernel->get_kernel_type()!=K_WEIGHTEDDEGREEPOS)
04543             SG_ERROR("Only works for Weighted Degree Position kernels.\n");
04544 
04545     CSVM* svm=(CSVM*) ui_classifier->get_classifier();
04546     ASSERT(svm);
04547     int32_t num_suppvec=svm->get_num_support_vectors();
04548     int32_t* sv_idx=SG_MALLOC(int32_t, num_suppvec);
04549     float64_t* sv_weight=SG_MALLOC(float64_t, num_suppvec);
04550     int32_t num_feat=0;
04551     int32_t num_sym=0;
04552 
04553     for (int32_t i=0; i<num_suppvec; i++)
04554     {
04555         sv_idx[i]=svm->get_support_vector(i);
04556         sv_weight[i]=svm->get_alpha(i);
04557     }
04558 
04559     if ((max_order<1) || (max_order>12))
04560     {
04561         SG_WARNING("max_order out of range 1..12 (%d). setting to 1\n", max_order);
04562         max_order=1;
04563     }
04564 
04565     float64_t* position_weights=
04566         ((CWeightedDegreePositionStringKernel*) kernel)->compute_scoring(
04567             max_order, num_feat, num_sym, NULL, num_suppvec, sv_idx, sv_weight);
04568     SG_FREE(sv_idx);
04569     SG_FREE(sv_weight);
04570 
04571     set_matrix(position_weights, num_sym, num_feat);
04572     SG_FREE(position_weights);
04573 
04574     return true;
04575 }
04576 
04577 
04578 /* Classifier */
04579 
04580 bool CSGInterface::cmd_classify()
04581 {
04582     if (m_nrhs!=1 || !create_return_values(1))
04583         return false;
04584 
04585     if (!ui_kernel->get_kernel() ||
04586             !ui_kernel->get_kernel()->get_kernel_type()==K_CUSTOM)
04587     {
04588         CFeatures* feat=ui_features->get_test_features();
04589         if (!feat)
04590             SG_ERROR("No features found.\n");
04591     }
04592 
04593     CLabels* labels=ui_classifier->classify();
04594     if (!labels)
04595         SG_ERROR("Classify failed\n");
04596 
04597     int32_t num_vec=labels->get_num_labels();
04598     float64_t* result=SG_MALLOC(float64_t, num_vec);
04599     for (int32_t i=0; i<num_vec; i++)
04600         result[i]=((CRegressionLabels*) labels)->get_label(i);
04601     SG_UNREF(labels);
04602 
04603     set_vector(result, num_vec);
04604     SG_FREE(result);
04605 
04606     return true;
04607 }
04608 
04609 bool CSGInterface::cmd_classify_example()
04610 {
04611     if (m_nrhs!=2 || !create_return_values(1))
04612         return false;
04613 
04614     int32_t idx=get_int();
04615     float64_t result=0;
04616 
04617     if (!ui_classifier->classify_example(idx, result))
04618         SG_ERROR("Classify_example failed.\n");
04619 
04620     set_real(result);
04621 
04622     return true;
04623 }
04624 
04625 bool CSGInterface::cmd_get_classifier()
04626 {
04627     if (m_nrhs<1 || m_nrhs>2 || !create_return_values(2))
04628         return false;
04629 
04630     int32_t idx=-1;
04631     if (m_nrhs==2)
04632         idx=get_int();
04633 
04634     float64_t* bias=NULL;
04635     float64_t* weights=NULL;
04636     int32_t rows=0;
04637     int32_t cols=0;
04638     int32_t brows=0;
04639     int32_t bcols=0;
04640 
04641     if (!ui_classifier->get_trained_classifier(
04642         weights, rows, cols, bias, brows, bcols, idx))
04643         return false;
04644 
04645     //SG_PRINT("brows %d, bcols %d\n", brows, bcols);
04646     //CMath::display_matrix(bias, brows, bcols);
04647     set_matrix(bias, brows, bcols);
04648     SG_FREE(bias);
04649 
04650     //SG_PRINT("rows %d, cols %d\n", rows, cols);
04651     //CMath::display_matrix(weights, rows, cols);
04652     set_matrix(weights, rows, cols);
04653     SG_FREE(weights);
04654 
04655     return true;
04656 }
04657 
04658 bool CSGInterface::cmd_new_classifier()
04659 {
04660     if (m_nrhs<2 || !create_return_values(0))
04661         return false;
04662 
04663     int32_t len=0;
04664     char* name=get_str_from_str_or_direct(len);
04665     int32_t d=6;
04666     int32_t from_d=40;
04667 
04668     if (m_nrhs>2)
04669     {
04670         d=get_int_from_int_or_str();
04671 
04672         if (m_nrhs>3)
04673             from_d=get_int_from_int_or_str();
04674     }
04675 
04676     bool success=ui_classifier->new_classifier(name, d, from_d);
04677 
04678     SG_FREE(name);
04679     return success;
04680 }
04681 
04682 bool CSGInterface::cmd_save_classifier()
04683 {
04684     if (m_nrhs<2 || !create_return_values(0))
04685         return false;
04686 
04687     int32_t len=0;
04688     char* filename=get_str_from_str_or_direct(len);
04689 
04690     bool success=ui_classifier->save(filename);
04691 
04692     SG_FREE(filename);
04693     return success;
04694 }
04695 
04696 bool CSGInterface::cmd_load_classifier()
04697 {
04698     if (m_nrhs<3 || !create_return_values(0))
04699         return false;
04700 
04701     int32_t len=0;
04702     char* filename=get_str_from_str_or_direct(len);
04703     char* type=get_str_from_str_or_direct(len);
04704 
04705     bool success=ui_classifier->load(filename, type);
04706 
04707     SG_FREE(filename);
04708     SG_FREE(type);
04709     return success;
04710 }
04711 
04712 
04713 bool CSGInterface::cmd_get_num_svms()
04714 {
04715     if (m_nrhs!=1 || !create_return_values(1))
04716         return false;
04717 
04718     set_int(ui_classifier->get_num_svms());
04719 
04720     return true;
04721 }
04722 
04723 
04724 bool CSGInterface::cmd_get_svm()
04725 {
04726     return cmd_get_classifier();
04727 }
04728 
04729 bool CSGInterface::cmd_set_svm()
04730 {
04731     if (m_nrhs!=3 || !create_return_values(0))
04732         return false;
04733 
04734     float64_t bias=get_real();
04735 
04736     float64_t* alphas=NULL;
04737     int32_t num_feat_alphas=0;
04738     int32_t num_vec_alphas=0;
04739     get_matrix(alphas, num_feat_alphas, num_vec_alphas);
04740 
04741     if (!alphas)
04742         SG_ERROR("No proper alphas given.\n");
04743     if (num_vec_alphas!=2)
04744         SG_ERROR("Not 2 vectors in alphas.\n");
04745 
04746     CSVM* svm=(CSVM*) ui_classifier->get_classifier();
04747     if (!svm)
04748         SG_ERROR("No SVM object available.\n");
04749 
04750     svm->create_new_model(num_feat_alphas);
04751     svm->set_bias(bias);
04752 
04753     int32_t num_support_vectors=svm->get_num_support_vectors();
04754     for (int32_t i=0; i<num_support_vectors; i++)
04755     {
04756         svm->set_alpha(i, alphas[i]);
04757         svm->set_support_vector(i, (int32_t) alphas[i+num_support_vectors]);
04758     }
04759     SG_FREE(alphas);
04760 
04761     return true;
04762 }
04763 
04764 bool CSGInterface::cmd_set_linear_classifier()
04765 {
04766     if (m_nrhs!=3 || !create_return_values(0))
04767         return false;
04768 
04769     float64_t bias=get_real();
04770 
04771     float64_t* w=NULL;
04772     int32_t len=0;
04773     get_vector(w, len);
04774 
04775     if (!len)
04776         SG_ERROR("No proper weight vector given.\n");
04777 
04778     CLinearMachine* c=(CLinearMachine*) ui_classifier->get_classifier();
04779     if (!c)
04780         SG_ERROR("No Linear Classifier object available.\n");
04781 
04782     c->set_w(SGVector<float64_t>(w, len));
04783     c->set_bias(bias);
04784     return true;
04785 }
04786 
04787 bool CSGInterface::cmd_get_svm_objective()
04788 {
04789     if (m_nrhs!=1 || !create_return_values(1))
04790         return false;
04791 
04792     CSVM* svm=(CSVM*) ui_classifier->get_classifier();
04793     if (!svm)
04794         SG_ERROR("No SVM set.\n");
04795 
04796     set_real(svm->get_objective());
04797 
04798     return true;
04799 }
04800 
04801 bool CSGInterface::cmd_compute_svm_primal_objective()
04802 {
04803     return do_compute_objective(SVM_PRIMAL);
04804 }
04805 
04806 bool CSGInterface::cmd_compute_svm_dual_objective()
04807 {
04808     return do_compute_objective(SVM_DUAL);
04809 }
04810 
04811 bool CSGInterface::cmd_compute_mkl_dual_objective()
04812 {
04813     return do_compute_objective(MKL_DUAL);
04814 }
04815 
04816 bool CSGInterface::cmd_compute_relative_mkl_duality_gap()
04817 {
04818     return do_compute_objective(MKL_RELATIVE_DUALITY_GAP);
04819 }
04820 
04821 bool CSGInterface::cmd_compute_absolute_mkl_duality_gap()
04822 {
04823     return do_compute_objective(MKL_ABSOLUTE_DUALITY_GAP);
04824 }
04825 
04826 bool CSGInterface::do_compute_objective(E_WHICH_OBJ obj)
04827 {
04828     if (m_nrhs!=1 || !create_return_values(1))
04829         return false;
04830 
04831     float64_t result=23.5;
04832 
04833     CSVM* svm=(CSVM*) ui_classifier->get_classifier();
04834     if (!svm)
04835         SG_ERROR("No SVM set.\n");
04836 
04837     CLabels* trainlabels=NULL;
04838     trainlabels=ui_labels->get_train_labels();
04839 
04840     if (!trainlabels)
04841         SG_ERROR("No trainlabels available.\n");
04842 
04843     CKernel* kernel=ui_kernel->get_kernel();
04844     if (!kernel)
04845         SG_ERROR("No kernel available.\n");
04846 
04847     if (!ui_kernel->is_initialized() || !kernel->has_features())
04848         SG_ERROR("Kernel not initialized.\n");
04849 
04850     ((CKernelMachine*) svm)->set_labels(trainlabels);
04851     ((CKernelMachine*) svm)->set_kernel(kernel);
04852 
04853 
04854     switch (obj)
04855     {
04856         case  SVM_PRIMAL:
04857             result=svm->compute_svm_primal_objective();
04858             break;
04859         case  SVM_DUAL:
04860             result=svm->compute_svm_dual_objective();
04861             break;
04862         case  MKL_PRIMAL:
04863             ASSERT( svm->get_classifier_type() == CT_MKLCLASSIFICATION );
04864             result=((CMKL*) svm)->compute_mkl_primal_objective();
04865             break;
04866         case  MKL_DUAL:
04867             ASSERT( svm->get_classifier_type() == CT_MKLCLASSIFICATION );
04868             result=((CMKL*) svm)->compute_mkl_dual_objective();
04869             break;
04870         case  MKL_RELATIVE_DUALITY_GAP:
04871             {
04872                 ASSERT( svm->get_classifier_type() == CT_MKLCLASSIFICATION );
04873                 float64_t primal=((CMKL*) svm)->compute_mkl_dual_objective();
04874                 float64_t dual=((CMKL*) svm)->compute_mkl_primal_objective();
04875                 result=(primal-dual)/dual;
04876             }
04877             break;
04878         case  MKL_ABSOLUTE_DUALITY_GAP:
04879             {
04880                 ASSERT( svm->get_classifier_type() == CT_MKLCLASSIFICATION );
04881                 float64_t primal=((CMKL*) svm)->compute_mkl_dual_objective();
04882                 float64_t dual=((CMKL*) svm)->compute_mkl_primal_objective();
04883                 result=dual-primal;
04884             }
04885             break;
04886         default:
04887             SG_SERROR("Error calling do_compute_objective\n");
04888             return false;
04889     };
04890 
04891     set_real(result);
04892     return true;
04893 }
04894 
04895 bool CSGInterface::cmd_train_classifier()
04896 {
04897     if (m_nrhs<1 || !create_return_values(0))
04898         return false;
04899 
04900     CMachine* classifier=ui_classifier->get_classifier();
04901     if (!classifier)
04902         SG_ERROR("No classifier available.\n");
04903 
04904     EMachineType type=classifier->get_classifier_type();
04905     switch (type)
04906     {
04907         case CT_LIGHT:
04908         case CT_LIGHTONECLASS:
04909         case CT_LIBSVM:
04910         case CT_SCATTERSVM:
04911         case CT_MPD:
04912         case CT_GPBT:
04913         case CT_CPLEXSVM:
04914         case CT_GMNPSVM:
04915         case CT_GNPPSVM:
04916         case CT_KERNELPERCEPTRON:
04917         case CT_LIBSVR:
04918         case CT_LIBSVMMULTICLASS:
04919         case CT_LIBSVMONECLASS:
04920         case CT_SVRLIGHT:
04921         case CT_LARANK:
04922             return ui_classifier->train_svm();
04923         case CT_MKLMULTICLASS:
04924             return ui_classifier->train_mkl_multiclass();
04925         case CT_MKLCLASSIFICATION:
04926         case CT_MKLREGRESSION:
04927         case CT_MKLONECLASS:
04928             return ui_classifier->train_mkl();
04929 
04930         case CT_KERNELRIDGEREGRESSION:
04931             return ui_classifier->train_krr();
04932 
04933         case CT_KNN:
04934         {
04935             if (m_nrhs<2)
04936                 return false;
04937 
04938             int32_t k=get_int_from_int_or_str();
04939 
04940             return ui_classifier->train_knn(k);
04941         }
04942 
04943         case CT_KMEANS:
04944         {
04945             if (m_nrhs<3)
04946                 return false;
04947 
04948             int32_t k=get_int_from_int_or_str();
04949             int32_t max_iter=get_int_from_int_or_str();
04950 
04951             return ui_classifier->train_clustering(k, max_iter);
04952         }
04953 
04954         case CT_HIERARCHICAL:
04955         {
04956             if (m_nrhs<2)
04957                 return false;
04958 
04959             int32_t merges=get_int_from_int_or_str();
04960 
04961             return ui_classifier->train_clustering(merges);
04962         }
04963 
04964         case CT_LDA:
04965         {
04966             float64_t gamma=0;
04967             if (m_nrhs==2)
04968                 gamma=get_real_from_real_or_str();
04969 
04970             return ui_classifier->train_linear(gamma);
04971         }
04972 
04973         case CT_PERCEPTRON:
04974         case CT_SVMLIN:
04975         case CT_SVMPERF:
04976         case CT_SUBGRADIENTSVM:
04977         case CT_SVMOCAS:
04978         case CT_SVMSGD:
04979         case CT_LPM:
04980         case CT_LPBOOST:
04981         case CT_SUBGRADIENTLPM:
04982         case CT_LIBLINEAR:
04983             return ui_classifier->train_linear();
04984 
04985         case CT_WDSVMOCAS:
04986             return ui_classifier->train_wdocas();
04987 
04988         default:
04989             SG_ERROR("Unknown classifier type %d.\n", type);
04990     }
04991 
04992     return false;
04993 }
04994 
04995 bool CSGInterface::cmd_do_auc_maximization()
04996 {
04997     if (m_nrhs!=2 || !create_return_values(0))
04998         return false;
04999 
05000     bool do_auc=get_bool_from_bool_or_str();
05001 
05002     return ui_classifier->set_do_auc_maximization(do_auc);
05003 }
05004 
05005 bool CSGInterface::cmd_set_perceptron_parameters()
05006 {
05007     if (m_nrhs!=3 || !create_return_values(0))
05008         return false;
05009 
05010     float64_t lernrate=get_real_from_real_or_str();
05011     int32_t maxiter=get_int_from_int_or_str();
05012 
05013     return ui_classifier->set_perceptron_parameters(lernrate, maxiter);
05014 }
05015 
05016 bool CSGInterface::cmd_set_svm_qpsize()
05017 {
05018     if (m_nrhs!=2 || !create_return_values(0))
05019         return false;
05020 
05021     int32_t qpsize=get_int_from_int_or_str();
05022 
05023     return ui_classifier->set_svm_qpsize(qpsize);
05024 }
05025 
05026 bool CSGInterface::cmd_set_svm_max_qpsize()
05027 {
05028     if (m_nrhs!=2 || !create_return_values(0))
05029         return false;
05030 
05031     int32_t max_qpsize=get_int_from_int_or_str();
05032 
05033     return ui_classifier->set_svm_max_qpsize(max_qpsize);
05034 }
05035 
05036 bool CSGInterface::cmd_set_svm_bufsize()
05037 {
05038     if (m_nrhs!=2 || !create_return_values(0))
05039         return false;
05040 
05041     int32_t bufsize=get_int_from_int_or_str();
05042 
05043     return ui_classifier->set_svm_bufsize(bufsize);
05044 }
05045 
05046 bool CSGInterface::cmd_set_svm_C()
05047 {
05048     if (m_nrhs<2 || !create_return_values(0))
05049         return false;
05050 
05051     float64_t C1=get_real_from_real_or_str();
05052     float64_t C2=C1;
05053 
05054     if (m_nrhs==3)
05055         C2=get_real_from_real_or_str();
05056 
05057     return ui_classifier->set_svm_C(C1, C2);
05058 }
05059 
05060 bool CSGInterface::cmd_set_svm_epsilon()
05061 {
05062     if (m_nrhs!=2 || !create_return_values(0))
05063         return false;
05064 
05065     float64_t epsilon=get_real_from_real_or_str();
05066 
05067     return ui_classifier->set_svm_epsilon(epsilon);
05068 }
05069 
05070 bool CSGInterface::cmd_set_svr_tube_epsilon()
05071 {
05072     if (m_nrhs!=2 || !create_return_values(0))
05073         return false;
05074 
05075     float64_t tube_epsilon=get_real_from_real_or_str();
05076 
05077     return ui_classifier->set_svr_tube_epsilon(tube_epsilon);
05078 }
05079 
05080 bool CSGInterface::cmd_set_svm_nu()
05081 {
05082     if (m_nrhs!=2 || !create_return_values(0))
05083         return false;
05084 
05085     float64_t nu=get_real_from_real_or_str();
05086 
05087     return ui_classifier->set_svm_nu(nu);
05088 }
05089 
05090 bool CSGInterface::cmd_set_svm_mkl_parameters()
05091 {
05092     if (m_nrhs<3 || m_nrhs>4 || !create_return_values(0))
05093         return false;
05094 
05095     float64_t weight_epsilon=get_real_from_real_or_str();
05096     float64_t C_mkl=get_real_from_real_or_str();
05097     float64_t mkl_norm=1.0;
05098 
05099     if (m_nrhs==4)
05100         mkl_norm=get_real_from_real_or_str();
05101 
05102     return ui_classifier->set_svm_mkl_parameters(weight_epsilon, C_mkl, mkl_norm);
05103 }
05104 
05105 bool CSGInterface::cmd_set_elasticnet_lambda()
05106 {
05107     if (m_nrhs!=2 || !create_return_values(0))
05108         return false;
05109     float64_t lambda=get_real_from_real_or_str();
05110     return ui_classifier->set_elasticnet_lambda(lambda);
05111 }
05112 
05113 bool CSGInterface::cmd_set_mkl_block_norm()
05114 {
05115     if (m_nrhs!=2 || !create_return_values(0))
05116         return false;
05117     float64_t bnorm=get_real_from_real_or_str();
05118     return ui_classifier->set_mkl_block_norm(bnorm);
05119 }
05120 
05121 
05122 bool CSGInterface::cmd_set_max_train_time()
05123 {
05124     if (m_nrhs!=2 || !create_return_values(0))
05125         return false;
05126 
05127     float64_t max_train_time=get_real_from_real_or_str();
05128 
05129     return ui_classifier->set_max_train_time(max_train_time);
05130 }
05131 
05132 bool CSGInterface::cmd_set_svm_shrinking_enabled()
05133 {
05134     if (m_nrhs!=2 || !create_return_values(0))
05135         return false;
05136 
05137     bool shrinking_enabled=get_bool_from_bool_or_str();
05138 
05139     return ui_classifier->set_svm_shrinking_enabled(shrinking_enabled);
05140 }
05141 
05142 bool CSGInterface::cmd_set_svm_batch_computation_enabled()
05143 {
05144     if (m_nrhs!=2 || !create_return_values(0))
05145         return false;
05146 
05147     bool batch_computation_enabled=get_bool_from_bool_or_str();
05148 
05149     return ui_classifier->set_svm_batch_computation_enabled(
05150         batch_computation_enabled);
05151 }
05152 
05153 bool CSGInterface::cmd_set_svm_linadd_enabled()
05154 {
05155     if (m_nrhs!=2 || !create_return_values(0))
05156         return false;
05157 
05158     bool linadd_enabled=get_bool_from_bool_or_str();
05159 
05160     return ui_classifier->set_svm_linadd_enabled(linadd_enabled);
05161 }
05162 
05163 bool CSGInterface::cmd_set_svm_bias_enabled()
05164 {
05165     if (m_nrhs!=2 || !create_return_values(0))
05166         return false;
05167 
05168     bool bias_enabled=get_bool_from_bool_or_str();
05169 
05170     return ui_classifier->set_svm_bias_enabled(bias_enabled);
05171 }
05172 
05173 bool CSGInterface::cmd_set_mkl_interleaved_enabled()
05174 {
05175     if (m_nrhs!=2 || !create_return_values(0))
05176         return false;
05177 
05178     bool interleaved_enabled=get_bool_from_bool_or_str();
05179 
05180     return ui_classifier->set_mkl_interleaved_enabled(interleaved_enabled);
05181 }
05182 
05183 bool CSGInterface::cmd_set_krr_tau()
05184 {
05185     if (m_nrhs!=2 || !create_return_values(0))
05186         return false;
05187 
05188     float64_t tau=get_real_from_real_or_str();
05189 
05190     return ui_classifier->set_krr_tau(tau);
05191 }
05192 
05193 
05194 /* Preproc */
05195 
05196 bool CSGInterface::cmd_add_preproc()
05197 {
05198     if (m_nrhs<2 || !create_return_values(0))
05199         return false;
05200 
05201     int32_t len=0;
05202     char* type=get_str_from_str_or_direct(len);
05203     CPreprocessor* preproc=NULL;
05204 
05205     if (strmatch(type, "NORMONE"))
05206         preproc=ui_preproc->create_generic(P_NORMONE);
05207     else if (strmatch(type, "LOGPLUSONE"))
05208         preproc=ui_preproc->create_generic(P_LOGPLUSONE);
05209     else if (strmatch(type, "SORTWORDSTRING"))
05210         preproc=ui_preproc->create_generic(P_SORTWORDSTRING);
05211     else if (strmatch(type, "SORTULONGSTRING"))
05212         preproc=ui_preproc->create_generic(P_SORTULONGSTRING);
05213     else if (strmatch(type, "DECOMPRESSCHARSTRING"))
05214         preproc=ui_preproc->create_generic(P_DECOMPRESSCHARSTRING);
05215     else if (strmatch(type, "SORTWORD"))
05216         preproc=ui_preproc->create_generic(P_SORTWORD);
05217 
05218     else if (strmatch(type, "PRUNEVARSUBMEAN"))
05219     {
05220         bool divide_by_std=false;
05221         if (m_nrhs==3)
05222             divide_by_std=get_bool_from_bool_or_str();
05223 
05224         preproc=ui_preproc->create_prunevarsubmean(divide_by_std);
05225     }
05226 
05227 #ifdef HAVE_LAPACK
05228     else if (strmatch(type, "PCA") && m_nrhs==4)
05229     {
05230         bool do_whitening=get_bool_from_bool_or_str();
05231         float64_t threshold=get_real_from_real_or_str();
05232 
05233         preproc=ui_preproc->create_pca(do_whitening, threshold);
05234     }
05235 #endif
05236 
05237     else
05238         SG_NOTIMPLEMENTED;
05239 
05240     SG_FREE(type);
05241     return ui_preproc->add_preproc(preproc);
05242 }
05243 
05244 bool CSGInterface::cmd_del_preproc()
05245 {
05246     if (m_nrhs!=1 || !create_return_values(0))
05247         return false;
05248 
05249     return ui_preproc->del_preproc();
05250 }
05251 
05252 bool CSGInterface::cmd_attach_preproc()
05253 {
05254     if (m_nrhs<2 || !create_return_values(0))
05255         return false;
05256 
05257     int32_t len=0;
05258     char* target=get_str_from_str_or_direct(len);
05259 
05260     bool do_force=false;
05261     if (m_nrhs==3)
05262         do_force=get_bool_from_bool_or_str();
05263 
05264     bool success=ui_preproc->attach_preproc(target, do_force);
05265 
05266     SG_FREE(target);
05267     return success;
05268 }
05269 
05270 bool CSGInterface::cmd_clean_preproc()
05271 {
05272     if (m_nrhs!=1 || !create_return_values(0))
05273         return false;
05274 
05275     return ui_preproc->clean_preproc();
05276 }
05277 
05278 /* Converter */
05279 
05280 bool CSGInterface::cmd_set_converter()
05281 {
05282     int32_t len=0;
05283     char* type=get_str_from_str_or_direct(len);
05284 
05285     if (strmatch(type, "lle"))
05286     {
05287         int32_t k = get_int_from_int_or_str();
05288         ui_converter->create_locallylinearembedding(k);
05289         return true;
05290     }
05291     if (strmatch(type, "npe"))
05292     {
05293         int32_t k = get_int_from_int_or_str();
05294         ui_converter->create_neighborhoodpreservingembedding(k);
05295         return true;
05296     }
05297     if (strmatch(type, "ltsa"))
05298     {
05299         int32_t k = get_int_from_int_or_str();
05300         ui_converter->create_localtangentspacealignment(k);
05301         return true;
05302     }
05303     if (strmatch(type, "lltsa"))
05304     {
05305         int32_t k = get_int_from_int_or_str();
05306         ui_converter->create_linearlocaltangentspacealignment(k);
05307         return true;
05308     }
05309     if (strmatch(type, "hlle"))
05310     {
05311         int32_t k = get_int_from_int_or_str();
05312         ui_converter->create_hessianlocallylinearembedding(k);
05313         return true;
05314     }
05315     if (strmatch(type, "laplacian_eigenmaps"))
05316     {
05317         int32_t k = get_int_from_int_or_str();
05318         int32_t width = get_real_from_real_or_str();
05319         ui_converter->create_laplacianeigenmaps(k,width);
05320         return true;
05321     }
05322     if (strmatch(type, "lpp"))
05323     {
05324         int32_t k = get_int_from_int_or_str();
05325         int32_t width = get_real_from_real_or_str();
05326         ui_converter->create_localitypreservingprojections(k,width);
05327         return true;
05328     }
05329     if (strmatch(type, "diffusion_maps"))
05330     {
05331         int32_t t = get_int_from_int_or_str();
05332         int32_t width = get_real_from_real_or_str();
05333         ui_converter->create_diffusionmaps(t,width);
05334         return true;
05335     }
05336     if (strmatch(type, "isomap"))
05337     {
05338         int32_t k = get_int_from_int_or_str();
05339         ui_converter->create_isomap(k);
05340         return true;
05341     }
05342     if (strmatch(type, "mds"))
05343     {
05344         ui_converter->create_multidimensionalscaling();
05345         return true;
05346     }
05347     return false;
05348 }
05349 
05350 bool CSGInterface::cmd_embed()
05351 {
05352     int32_t target_dim = get_int_from_int_or_str();
05353     CDenseFeatures<float64_t>* embedding = ui_converter->embed(target_dim);
05354     SGMatrix<float64_t> embedding_matrix = embedding->get_feature_matrix();
05355     set_matrix(embedding_matrix.matrix,embedding_matrix.num_cols,embedding_matrix.num_rows);
05356     return true;
05357 }
05358 
05359 /* HMM */
05360 
05361 bool CSGInterface::cmd_new_plugin_estimator()
05362 {
05363     if (m_nrhs<2 || !create_return_values(0))
05364         return false;
05365 
05366     float64_t pos_pseudo=get_real_from_real_or_str();
05367     float64_t neg_pseudo=get_real_from_real_or_str();
05368 
05369     return ui_pluginestimate->new_estimator(pos_pseudo, neg_pseudo);
05370 }
05371 
05372 bool CSGInterface::cmd_train_estimator()
05373 {
05374     if (m_nrhs!=1 || !create_return_values(0))
05375         return false;
05376 
05377     return ui_pluginestimate->train();
05378 }
05379 
05380 bool CSGInterface::cmd_plugin_estimate_classify_example()
05381 {
05382     if (m_nrhs!=2 || !create_return_values(1))
05383         return false;
05384 
05385     int32_t idx=get_int();
05386     float64_t result=ui_pluginestimate->apply_one(idx);
05387 
05388     set_vector(&result, 1);
05389     return true;
05390 }
05391 
05392 bool CSGInterface::cmd_plugin_estimate_classify()
05393 {
05394     if (m_nrhs!=1 || !create_return_values(1))
05395         return false;
05396 
05397     CFeatures* feat=ui_features->get_test_features();
05398     if (!feat)
05399         SG_ERROR("No features found.\n");
05400 
05401     int32_t num_vec=feat->get_num_vectors();
05402     float64_t* result=SG_MALLOC(float64_t, num_vec);
05403     CLabels* labels=ui_pluginestimate->apply();
05404     for (int32_t i=0; i<num_vec; i++)
05405         result[i]=((CRegressionLabels*) labels)->get_label(i);
05406     SG_UNREF(labels);
05407 
05408     set_vector(result, num_vec);
05409     SG_FREE(result);
05410 
05411     return true;
05412 }
05413 
05414 bool CSGInterface::cmd_set_plugin_estimate()
05415 {
05416     if (m_nrhs!=3 || !create_return_values(0))
05417         return false;
05418 
05419     float64_t* emission_probs=NULL;
05420     int32_t num_probs=0;
05421     int32_t num_vec=0;
05422     get_matrix(emission_probs, num_probs, num_vec);
05423 
05424     if (num_vec!=2)
05425         SG_ERROR("Need at least 1 set of positive and 1 set of negative params.\n");
05426 
05427     float64_t* pos_params=emission_probs;
05428     float64_t* neg_params=&(emission_probs[num_probs]);
05429 
05430     float64_t* model_sizes=NULL;
05431     int32_t len=0;
05432     get_vector(model_sizes, len);
05433 
05434     int32_t seq_length=(int32_t) model_sizes[0];
05435     int32_t num_symbols=(int32_t) model_sizes[1];
05436     if (num_probs!=seq_length*num_symbols)
05437         SG_ERROR("Mismatch in number of emission probs and sequence length * number of symbols.\n");
05438 
05439     ui_pluginestimate->get_estimator()->set_model_params(
05440         pos_params, neg_params, seq_length, num_symbols);
05441 
05442     return true;
05443 }
05444 
05445 bool CSGInterface::cmd_get_plugin_estimate()
05446 {
05447     if (m_nrhs!=1 || !create_return_values(2))
05448         return false;
05449 
05450     float64_t* pos_params=NULL;
05451     float64_t* neg_params=NULL;
05452     int32_t num_params=0;
05453     int32_t seq_length=0;
05454     int32_t num_symbols=0;
05455 
05456     if (!ui_pluginestimate->get_estimator()->get_model_params(
05457         pos_params, neg_params, seq_length, num_symbols))
05458         return false;
05459 
05460     num_params=seq_length*num_symbols;
05461 
05462     float64_t* result=SG_MALLOC(float64_t, num_params*2);
05463     for (int32_t i=0; i<num_params; i++)
05464         result[i]=pos_params[i];
05465     for (int32_t i=0; i<num_params; i++)
05466         result[i+num_params]=neg_params[i];
05467 
05468     set_matrix(result, num_params, 2);
05469     SG_FREE(result);
05470 
05471     float64_t model_sizes[2];
05472     model_sizes[0]=(float64_t) seq_length;
05473     model_sizes[1]=(float64_t) num_symbols;
05474     set_vector(model_sizes, 2);
05475 
05476     return true;
05477 }
05478 
05479 bool CSGInterface::cmd_convergence_criteria()
05480 {
05481     if (m_nrhs<3 || !create_return_values(0))
05482         return false;
05483 
05484     int32_t num_iterations=get_int_from_int_or_str();
05485     float64_t epsilon=get_real_from_real_or_str();
05486 
05487     return ui_hmm->convergence_criteria(num_iterations, epsilon);
05488 }
05489 
05490 bool CSGInterface::cmd_normalize()
05491 {
05492     if (m_nrhs<2 || !create_return_values(0))
05493         return false;
05494 
05495     bool keep_dead_states=get_bool_from_bool_or_str();
05496 
05497     return ui_hmm->normalize(keep_dead_states);
05498 }
05499 
05500 bool CSGInterface::cmd_add_states()
05501 {
05502     if (m_nrhs<3 || !create_return_values(0))
05503         return false;
05504 
05505     int32_t num_states=get_int_from_int_or_str();
05506     float64_t value=get_real_from_real_or_str();
05507 
05508     return ui_hmm->add_states(num_states, value);
05509 }
05510 
05511 bool CSGInterface::cmd_permutation_entropy()
05512 {
05513     if (m_nrhs<3 || !create_return_values(0))
05514         return false;
05515 
05516     int32_t width=get_int_from_int_or_str();
05517     int32_t seq_num=get_int_from_int_or_str();
05518 
05519     return ui_hmm->permutation_entropy(width, seq_num);
05520 }
05521 
05522 bool CSGInterface::cmd_relative_entropy()
05523 {
05524     if (m_nrhs!=1 || !create_return_values(1))
05525         return false;
05526 
05527     float64_t* entropy=NULL;
05528     int32_t len=0;
05529     bool success=ui_hmm->relative_entropy(entropy, len);
05530     if (!success)
05531         return false;
05532 
05533     set_vector(entropy, len);
05534 
05535     SG_FREE(entropy);
05536     return true;
05537 }
05538 
05539 bool CSGInterface::cmd_entropy()
05540 {
05541     if (m_nrhs!=1 || !create_return_values(1))
05542         return false;
05543 
05544     float64_t* entropy=NULL;
05545     int32_t len=0;
05546     bool success=ui_hmm->entropy(entropy, len);
05547     if (!success)
05548         return false;
05549 
05550     set_vector(entropy, len);
05551 
05552     SG_FREE(entropy);
05553     return true;
05554 }
05555 
05556 bool CSGInterface::cmd_hmm_classify()
05557 {
05558     return do_hmm_classify(false, false);
05559 }
05560 
05561 bool CSGInterface::cmd_one_class_hmm_classify()
05562 {
05563     return do_hmm_classify(false, true);
05564 }
05565 
05566 bool CSGInterface::cmd_one_class_linear_hmm_classify()
05567 {
05568     return do_hmm_classify(true, true);
05569 }
05570 
05571 bool CSGInterface::do_hmm_classify(bool linear, bool one_class)
05572 {
05573     if (m_nrhs>1 || !create_return_values(1))
05574         return false;
05575 
05576     CFeatures* feat=ui_features->get_test_features();
05577     if (!feat)
05578         return false;
05579 
05580     int32_t num_vec=feat->get_num_vectors();
05581     CRegressionLabels* labels=NULL;
05582 
05583     if (linear) // must be one_class as well
05584     {
05585         labels=ui_hmm->linear_one_class_classify();
05586     }
05587     else
05588     {
05589         if (one_class)
05590             labels=ui_hmm->one_class_classify();
05591         else
05592             labels=ui_hmm->classify();
05593     }
05594     if (!labels)
05595         return false;
05596 
05597     float64_t* result=SG_MALLOC(float64_t, num_vec);
05598     for (int32_t i=0; i<num_vec; i++)
05599         result[i]=labels->get_label(i);
05600     SG_UNREF(labels);
05601 
05602     set_vector(result, num_vec);
05603     SG_FREE(result);
05604 
05605     return true;
05606 }
05607 
05608 bool CSGInterface::cmd_one_class_hmm_classify_example()
05609 {
05610     return do_hmm_classify_example(true);
05611 }
05612 
05613 bool CSGInterface::cmd_hmm_classify_example()
05614 {
05615     return do_hmm_classify_example(false);
05616 }
05617 
05618 bool CSGInterface::do_hmm_classify_example(bool one_class)
05619 {
05620     if (m_nrhs!=2 || !create_return_values(1))
05621         return false;
05622 
05623     int32_t idx=get_int();
05624     float64_t result=0;
05625 
05626     if (one_class)
05627         result=ui_hmm->one_class_classify_example(idx);
05628     else
05629         result=ui_hmm->classify_example(idx);
05630 
05631     set_real(result);
05632 
05633     return true;
05634 }
05635 
05636 bool CSGInterface::cmd_output_hmm()
05637 {
05638     if (m_nrhs!=1 || !create_return_values(0))
05639         return false;
05640 
05641     return ui_hmm->output_hmm();
05642 }
05643 
05644 bool CSGInterface::cmd_output_hmm_defined()
05645 {
05646     if (m_nrhs!=1 || !create_return_values(0))
05647         return false;
05648 
05649     return ui_hmm->output_hmm_defined();
05650 }
05651 
05652 bool CSGInterface::cmd_hmm_likelihood()
05653 {
05654     if (m_nrhs!=1 || !create_return_values(1))
05655         return false;
05656 
05657     CHMM* h=ui_hmm->get_current();
05658     if (!h)
05659         SG_ERROR("No HMM.\n");
05660 
05661     float64_t likelihood=h->model_probability();
05662     set_real(likelihood);
05663 
05664     return true;
05665 }
05666 
05667 bool CSGInterface::cmd_likelihood()
05668 {
05669     if (m_nrhs!=1 || !create_return_values(0))
05670         return false;
05671 
05672     return ui_hmm->likelihood();
05673 }
05674 
05675 bool CSGInterface::cmd_save_likelihood()
05676 {
05677     if (m_nrhs<2 || !create_return_values(0))
05678         return false;
05679 
05680     int32_t len=0;
05681     char* filename=get_str_from_str_or_direct(len);
05682 
05683     bool is_binary=false;
05684     if (m_nrhs==3)
05685         is_binary=get_bool_from_bool_or_str();
05686 
05687     bool success=ui_hmm->save_likelihood(filename, is_binary);
05688 
05689     SG_FREE(filename);
05690     return success;
05691 }
05692 
05693 bool CSGInterface::cmd_get_viterbi_path()
05694 {
05695     if (m_nrhs!=2 || !create_return_values(2))
05696         return false;
05697 
05698     int32_t dim=get_int();
05699     SG_DEBUG("dim: %f\n", dim);
05700 
05701     CHMM* h=ui_hmm->get_current();
05702     if (!h)
05703         return false;
05704 
05705     CFeatures* feat=ui_features->get_test_features();
05706     if (!feat || (feat->get_feature_class()!=C_STRING) ||
05707             (feat->get_feature_type()!=F_WORD))
05708         return false;
05709 
05710     h->set_observations((CStringFeatures<uint16_t>*) feat);
05711 
05712     int32_t num_feat=0;
05713     bool free_vec;
05714     uint16_t* vec=((CStringFeatures<uint16_t>*) feat)->get_feature_vector(dim, num_feat, free_vec);
05715     if (!vec || num_feat<=0)
05716     {
05717         ((CStringFeatures<uint16_t>*) feat)->free_feature_vector(vec, dim, free_vec);
05718         return false;
05719     }
05720     ((CStringFeatures<uint16_t>*) feat)->free_feature_vector(vec, dim, free_vec);
05721 
05722     SG_DEBUG( "computing viterbi path for vector %d (length %d)\n", dim, num_feat);
05723     float64_t likelihood=0;
05724     T_STATES* path=h->get_path(dim, likelihood);
05725 
05726     set_vector(path, num_feat);
05727     SG_FREE(path);
05728     set_real(likelihood);
05729 
05730     return true;
05731 }
05732 
05733 bool CSGInterface::cmd_viterbi_train()
05734 {
05735     if (m_nrhs!=1 || !create_return_values(0))
05736         return false;
05737 
05738     return ui_hmm->viterbi_train();
05739 }
05740 
05741 bool CSGInterface::cmd_viterbi_train_defined()
05742 {
05743     if (m_nrhs!=1 || !create_return_values(0))
05744         return false;
05745 
05746     return ui_hmm->viterbi_train_defined();
05747 }
05748 
05749 bool CSGInterface::cmd_baum_welch_train()
05750 {
05751     if (m_nrhs!=1 || !create_return_values(0))
05752         return false;
05753 
05754     return ui_hmm->baum_welch_train();
05755 }
05756 
05757 bool CSGInterface::cmd_baum_welch_train_defined()
05758 {
05759     if (m_nrhs!=1 || !create_return_values(0))
05760         return false;
05761 
05762     return ui_hmm->baum_welch_train_defined();
05763 }
05764 
05765 
05766 bool CSGInterface::cmd_baum_welch_trans_train()
05767 {
05768     if (m_nrhs!=1 || !create_return_values(0))
05769         return false;
05770 
05771     return ui_hmm->baum_welch_trans_train();
05772 }
05773 
05774 bool CSGInterface::cmd_linear_train()
05775 {
05776     if (m_nrhs<1 || !create_return_values(0))
05777         return false;
05778 
05779     if (m_nrhs==2)
05780     {
05781         int32_t len=0;
05782         char* align=get_str_from_str_or_direct(len);
05783 
05784         bool success=ui_hmm->linear_train(align[0]);
05785 
05786         SG_FREE(align);
05787         return success;
05788     }
05789     else
05790         return ui_hmm->linear_train();
05791 }
05792 
05793 bool CSGInterface::cmd_save_path()
05794 {
05795     if (m_nrhs<2 || !create_return_values(0))
05796         return false;
05797 
05798     int32_t len=0;
05799     char* filename=get_str_from_str_or_direct(len);
05800 
05801     bool is_binary=false;
05802     if (m_nrhs==3)
05803         is_binary=get_bool_from_bool_or_str();
05804 
05805     bool success=ui_hmm->save_path(filename, is_binary);
05806 
05807     SG_FREE(filename);
05808     return success;
05809 }
05810 
05811 bool CSGInterface::cmd_append_hmm()
05812 {
05813     if (m_nrhs!=5 || !create_return_values(0))
05814         return false;
05815 
05816     CHMM* old_h=ui_hmm->get_current();
05817     if (!old_h)
05818         SG_ERROR("No current HMM set.\n");
05819 
05820     float64_t* p=NULL;
05821     int32_t N_p=0;
05822     get_vector(p, N_p);
05823 
05824     float64_t* q=NULL;
05825     int32_t N_q=0;
05826     get_vector(q, N_q);
05827 
05828     float64_t* a=NULL;
05829     int32_t M_a=0;
05830     int32_t N_a=0;
05831     get_matrix(a, M_a, N_a);
05832     int32_t N=N_a;
05833 
05834     float64_t* b=NULL;
05835     int32_t M_b=0;
05836     int32_t N_b=0;
05837     get_matrix(b, M_b, N_b);
05838     int32_t M=N_b;
05839 
05840     if (N_p!=N || N_q!=N || N_a!=N || M_a!=N || N_b!=M || M_b!=N)
05841     {
05842         SG_ERROR("Model matrices not matching in size.\n"
05843                 "p:(%d) q:(%d) a:(%d,%d) b(%d,%d)\n",
05844                 N_p, N_q, N_a, M_a, N_b, M_b);
05845     }
05846 
05847     CHMM* h=new CHMM(N, M, NULL, ui_hmm->get_pseudo());
05848     int32_t i,j;
05849 
05850     for (i=0; i<N; i++)
05851     {
05852         h->set_p(i, p[i]);
05853         h->set_q(i, q[i]);
05854     }
05855 
05856     for (i=0; i<N; i++)
05857         for (j=0; j<N; j++)
05858             h->set_a(i,j, a[i+j*N]);
05859 
05860     for (i=0; i<N; i++)
05861         for (j=0; j<M; j++)
05862             h->set_b(i,j, b[i+j*N]);
05863 
05864     old_h->append_model(h);
05865     SG_UNREF(h);
05866 
05867     return true;
05868 }
05869 
05870 bool CSGInterface::cmd_append_model()
05871 {
05872     if (m_nrhs<2 || !create_return_values(0))
05873         return false;
05874     if (m_nrhs>2 && m_nrhs!=4)
05875         return false;
05876 
05877     int32_t len=0;
05878     char* filename=get_str_from_str_or_direct(len);
05879     int32_t base1=-1;
05880     int32_t base2=-1;
05881     if (m_nrhs>2)
05882     {
05883         base1=get_int_from_int_or_str();
05884         base2=get_int_from_int_or_str();
05885     }
05886 
05887     bool success=ui_hmm->append_model(filename, base1, base2);
05888 
05889     SG_FREE(filename);
05890     return success;
05891 }
05892 
05893 bool CSGInterface::cmd_new_hmm()
05894 {
05895     if (m_nrhs!=3 || !create_return_values(0))
05896         return false;
05897 
05898     int32_t n=get_int_from_int_or_str();
05899     int32_t m=get_int_from_int_or_str();
05900 
05901     return ui_hmm->new_hmm(n, m);
05902 }
05903 
05904 bool CSGInterface::cmd_load_hmm()
05905 {
05906     if (m_nrhs!=2 || !create_return_values(0))
05907         return false;
05908 
05909     int32_t len=0;
05910     char* filename=get_str_from_str_or_direct(len);
05911 
05912     bool success=ui_hmm->load(filename);
05913 
05914     SG_FREE(filename);
05915     return success;
05916 }
05917 
05918 bool CSGInterface::cmd_save_hmm()
05919 {
05920     if (m_nrhs<2 || !create_return_values(0))
05921         return false;
05922 
05923     int32_t len=0;
05924     char* filename=get_str_from_str_or_direct(len);
05925 
05926     bool is_binary=false;
05927     if (m_nrhs==3)
05928         is_binary=get_bool_from_bool_or_str();
05929 
05930     bool success=ui_hmm->save(filename, is_binary);
05931 
05932     SG_FREE(filename);
05933     return success;
05934 }
05935 
05936 bool CSGInterface::cmd_set_hmm()
05937 {
05938     if (m_nrhs!=5 || !create_return_values(0))
05939         return false;
05940 
05941     float64_t* p=NULL;
05942     int32_t N_p=0;
05943     get_vector(p, N_p);
05944 
05945     float64_t* q=NULL;
05946     int32_t N_q=0;
05947     get_vector(q, N_q);
05948 
05949     float64_t* a=NULL;
05950     int32_t M_a=0;
05951     int32_t N_a=0;
05952     get_matrix(a, M_a, N_a);
05953     int32_t N=N_a;
05954 
05955     float64_t* b=NULL;
05956     int32_t M_b=0;
05957     int32_t N_b=0;
05958     get_matrix(b, M_b, N_b);
05959     int32_t M=N_b;
05960 
05961     if (N_p!=N || N_q!=N || N_a!=N || M_a!=N || N_b!=M || M_b!=N)
05962     {
05963         SG_ERROR("Model matrices not matching in size.\n"
05964                 "p:(%d) q:(%d) a:(%d,%d) b(%d,%d)\n",
05965                 N_p, N_q, N_a, M_a, N_b, M_b);
05966     }
05967 
05968     CHMM* current=ui_hmm->get_current();
05969     if (!current)
05970         SG_ERROR("Need a previously created HMM.\n");
05971 
05972     int32_t i,j;
05973 
05974     for (i=0; i<N; i++)
05975     {
05976         current->set_p(i, p[i]);
05977         current->set_q(i, q[i]);
05978     }
05979 
05980     for (i=0; i<N; i++)
05981         for (j=0; j<N; j++)
05982             current->set_a(i,j, a[i+j*N]);
05983 
05984     for (i=0; i<N; i++)
05985         for (j=0; j<M; j++)
05986             current->set_b(i,j, b[i+j*N]);
05987 
05988     CStringFeatures<uint16_t>* sf = ((CStringFeatures<uint16_t>*) (ui_features->get_train_features()));
05989     current->set_observations(sf);
05990 
05991     return true;
05992 }
05993 
05994 bool CSGInterface::cmd_set_hmm_as()
05995 {
05996     if (m_nrhs!=2 || !create_return_values(0))
05997         return false;
05998 
05999     int32_t len=0;
06000     char* target=get_str_from_str_or_direct(len);
06001 
06002     bool success=ui_hmm->set_hmm_as(target);
06003 
06004     SG_FREE(target);
06005     return success;
06006 }
06007 
06008 bool CSGInterface::cmd_set_chop()
06009 {
06010     if (m_nrhs!=2 || !create_return_values(0))
06011         return false;
06012 
06013     float64_t value=get_real_from_real_or_str();
06014     return ui_hmm->chop(value);
06015 }
06016 
06017 bool CSGInterface::cmd_set_pseudo()
06018 {
06019     if (m_nrhs!=2 || !create_return_values(0))
06020         return false;
06021 
06022     float64_t value=get_real_from_real_or_str();
06023     return ui_hmm->set_pseudo(value);
06024 }
06025 
06026 bool CSGInterface::cmd_load_definitions()
06027 {
06028     if (m_nrhs<2 || !create_return_values(0))
06029         return false;
06030 
06031     int32_t len=0;
06032     char* filename=get_str_from_str_or_direct(len);
06033 
06034     bool do_init=false;
06035     if (m_nrhs==3)
06036         do_init=get_bool_from_bool_or_str();
06037 
06038     bool success=ui_hmm->load_definitions(filename, do_init);
06039 
06040     SG_FREE(filename);
06041     return success;
06042 }
06043 
06044 bool CSGInterface::cmd_get_hmm()
06045 {
06046     if (m_nrhs!=1 || !create_return_values(4))
06047         return false;
06048 
06049     CHMM* h=ui_hmm->get_current();
06050     if (!h)
06051         return false;
06052 
06053     int32_t N=h->get_N();
06054     int32_t M=h->get_M();
06055     int32_t i=0;
06056     int32_t j=0;
06057     float64_t* p=SG_MALLOC(float64_t, N);
06058     float64_t* q=SG_MALLOC(float64_t, N);
06059 
06060     for (i=0; i<N; i++)
06061     {
06062         p[i]=h->get_p(i);
06063         q[i]=h->get_q(i);
06064     }
06065 
06066     set_vector(p, N);
06067     SG_FREE(p);
06068     set_vector(q, N);
06069     SG_FREE(q);
06070 
06071     float64_t* a=SG_MALLOC(float64_t, N*N);
06072     for (i=0; i<N; i++)
06073         for (j=0; j<N; j++)
06074             a[i+j*N]=h->get_a(i, j);
06075     set_matrix(a, N, N);
06076     SG_FREE(a);
06077 
06078     float64_t* b=SG_MALLOC(float64_t, N*M);
06079     for (i=0; i<N; i++)
06080         for (j=0; j<M; j++)
06081             b[i+j*N]=h->get_b(i, j);
06082     set_matrix(b, N, M);
06083     SG_FREE(b);
06084 
06085     return true;
06086 }
06087 
06088 bool CSGInterface::cmd_best_path()
06089 {
06090     if (m_nrhs!=3 || !create_return_values(0))
06091         return false;
06092 
06093     int32_t from=get_int_from_int_or_str();
06094     int32_t to=get_int_from_int_or_str();
06095 
06096     return ui_hmm->best_path(from, to);
06097 }
06098 
06099 bool CSGInterface::cmd_best_path_2struct()
06100 {
06101     if (m_nrhs!=12 || !create_return_values(3))
06102         return false;
06103 
06104     SG_ERROR("Sorry, this parameter list is awful!\n");
06105 
06106     return true;
06107 }
06108 
06109 void CSGInterface::get_vector(bool*& vector, int32_t& len)
06110 {
06111     int32_t* int_vector;
06112     get_vector(int_vector, len);
06113 
06114     ASSERT(len>0);
06115     vector= SG_MALLOC(bool, len);
06116 
06117     for (int32_t i=0; i<len; i++)
06118         vector[i]= (int_vector[i]!=0);
06119 
06120     SG_FREE(int_vector);
06121 }
06122 
06123 void CSGInterface::set_vector(const bool* vector, int32_t len)
06124 {
06125     int32_t* int_vector = SG_MALLOC(int32_t, len);
06126     for (int32_t i=0;i<len;i++)
06127     {
06128         if (vector[i])
06129             int_vector[i]=1;
06130         else
06131             int_vector[i]=0;
06132     }
06133     set_vector(int_vector,len);
06134     SG_FREE(int_vector);
06135 }
06136 
06137 bool CSGInterface::cmd_set_plif_struct()
06138 {
06139     // ARG 2
06140     int32_t Nid=0;
06141     int32_t* ids;
06142     get_vector(ids,Nid);
06143 
06144     // ARG 3
06145     int32_t Nname=0;
06146     int32_t Mname=0;
06147     SGString<char>* names;
06148     get_string_list(names, Nname,Mname);
06149 
06150     // ARG 4
06151     int32_t Nlimits=0;
06152     int32_t Mlimits=0;
06153     float64_t* all_limits;
06154     get_matrix(all_limits, Mlimits, Nlimits);
06155 
06156     // ARG 5
06157     int32_t Npenalties=0;
06158     int32_t Mpenalties=0;
06159     float64_t* all_penalties;
06160     get_matrix(all_penalties, Mpenalties, Npenalties);
06161 
06162     // ARG 6
06163     int32_t Ntransform=0;
06164     int32_t Mtransform=0;
06165     SGString<char>* all_transform;
06166     get_string_list(all_transform, Ntransform, Mtransform);
06167 
06168     // ARG 7
06169     int32_t Nmin=0;
06170     float64_t* min_values;
06171     get_vector(min_values,Nmin);
06172 
06173     // ARG 8
06174     int32_t Nmax=0;
06175     float64_t* max_values;
06176     get_vector(max_values,Nmax);
06177 
06178     // ARG 9
06179     int32_t Ncache=0;
06180     bool* all_use_cache;
06181     get_vector(all_use_cache,Ncache);
06182 
06183     // ARG 10
06184     int32_t Nsvm=0;
06185     int32_t* all_use_svm;
06186     get_vector(all_use_svm,Nsvm);
06187 
06188     // ARG 11
06189     int32_t Ncalc=0;
06190     bool* all_do_calc;
06191     get_vector(all_do_calc,Ncalc);
06192 
06193     if (Ncalc!=Nsvm)
06194         SG_ERROR("Ncalc!=Nsvm, Ncalc:%i, Nsvm:%i\n",Ncalc,Nsvm);
06195     if (Ncalc!=Ncache)
06196         SG_ERROR("Ncalc!=Ncache, Ncalc:%i, Ncache:%i\n",Ncalc,Ncache);
06197     if (Ncalc!=Ntransform)
06198         SG_ERROR("Ncalc!=Ntransform, Ncalc:%i, Ntransform:%i\n",Ncalc,Ntransform);
06199     if (Ncalc!=Nmin)
06200         SG_ERROR("Ncalc!=Nmin, Ncalc:%i, Nmin:%i\n",Ncalc,Nmin);
06201     if (Ncalc!=Nmax)
06202         SG_ERROR("Ncalc!=Nmax, Ncalc:%i, Nmax:%i\n",Ncalc,Nmax);
06203     if (Ncalc!=Npenalties)
06204         SG_ERROR("Ncalc!=Npenalties, Ncalc:%i, Npenalties:%i\n",Ncalc,Npenalties);
06205     if (Ncalc!=Nlimits)
06206         SG_ERROR("Ncalc!=Nlimits, Ncalc:%i, Nlimits:%i\n",Ncalc,Nlimits);
06207     if (Ncalc!=Nname)
06208         SG_ERROR("Ncalc!=Nname, Ncalc:%i, Nname:%i\n",Ncalc,Nname);
06209     if (Ncalc!=Nid)
06210         SG_ERROR("Ncalc!=Nid, Ncalc:%i, Nid:%i\n",Ncalc,Nid);
06211     if (Mlimits!=Mpenalties)
06212         SG_ERROR("Mlimits!=Mpenalties, Mlimits:%i, Mpenalties:%i\n",Mlimits,Mpenalties);
06213 
06214     int32_t N = Ncalc;
06215     int32_t M = Mlimits;
06216     CPlifMatrix* pm=ui_structure->get_plif_matrix();
06217     pm->create_plifs(N, M);
06218     pm->set_plif_ids(SGVector<int32_t>(ids, N));
06219     pm->set_plif_min_values(SGVector<float64_t>(min_values, N));
06220     pm->set_plif_max_values(SGVector<float64_t>(max_values, N));
06221     pm->set_plif_use_cache(SGVector<bool>(all_use_cache, N));
06222     pm->set_plif_use_svm(SGVector<int32_t>(all_use_svm, N));
06223     pm->set_plif_limits(SGMatrix<float64_t>(all_limits, N, M));
06224     pm->set_plif_penalties(SGMatrix<float64_t>(all_penalties, N, M));
06225     pm->set_plif_names(names, N);
06226     pm->set_plif_transform_type(all_transform, N);
06227 
06228     SG_FREE(names);
06229     SG_FREE(all_transform);
06230     SG_FREE(all_do_calc);
06231 
06232     return true;
06233 }
06234 
06235 bool CSGInterface::cmd_get_plif_struct()
06236 {
06237     CPlifMatrix* pm=ui_structure->get_plif_matrix();
06238     CPlif** PEN = pm->get_PEN();
06239     int32_t N = pm->get_num_plifs();
06240     int32_t M = pm->get_num_limits();
06241 
06242 
06243     int32_t* ids = SG_MALLOC(int32_t, N);
06244     float64_t* max_values = SG_MALLOC(float64_t, N);
06245     float64_t* min_values = SG_MALLOC(float64_t, N);
06246     SGString<char>* names = SG_MALLOC(SGString<char>, N);
06247     SGString<char>* all_transform = SG_MALLOC(SGString<char>, N);
06248     float64_t* all_limits = SG_MALLOC(float64_t, N*M);
06249     float64_t* all_penalties = SG_MALLOC(float64_t, N*M);
06250     bool* all_use_cache = SG_MALLOC(bool, N);
06251     int32_t* all_use_svm = SG_MALLOC(int32_t, N);
06252     bool* all_do_calc = SG_MALLOC(bool, N);
06253     for (int32_t i=0;i<N;i++)
06254     {
06255         ids[i]=PEN[i]->get_id();
06256         names[i].string = PEN[i]->get_plif_name();
06257         names[i].slen = strlen(PEN[i]->get_plif_name());
06258         float64_t* limits = PEN[i]->get_plif_limits();
06259         float64_t* penalties = PEN[i]->get_plif_penalties();
06260         for (int32_t j=0;j<M;j++)
06261         {
06262             all_limits[i*M+j]=limits[j];
06263             all_penalties[i*M+j]=penalties[j];
06264         }
06265         all_transform[i].string = (char*) PEN[i]->get_transform_type();
06266         all_transform[i].slen = strlen(PEN[i]->get_transform_type());
06267         min_values[i]=PEN[i]->get_min_value();
06268         max_values[i]=PEN[i]->get_max_value();
06269         all_use_cache[i]=PEN[i]->get_use_cache();
06270         all_use_svm[i]=PEN[i]->get_use_svm();
06271         all_do_calc[i]=PEN[i]->get_do_calc();
06272 
06273     }
06274     set_vector(ids,N);
06275     set_string_list(names, N);
06276     set_matrix(all_limits, M, N);
06277     set_matrix(all_penalties, M, N);
06278     set_string_list(all_transform, N);
06279     set_vector(min_values,N);
06280     set_vector(max_values,N);
06281     set_vector(all_use_cache,N);
06282     set_vector(all_use_svm,N);
06283     set_vector(all_do_calc,N);
06284 
06285     SG_FREE(ids);
06286     SG_FREE(max_values);
06287     SG_FREE(min_values);
06288     SG_FREE(names);
06289     SG_FREE(all_transform);
06290     SG_FREE(all_limits);
06291     SG_FREE(all_penalties);
06292     SG_FREE(all_use_cache);
06293     SG_FREE(all_use_svm);
06294     SG_FREE(all_do_calc);
06295 
06296     return true;
06297 }
06298 /*bool CSGInterface::cmd_signals_set_model()
06299 {
06300     // ARG 1
06301     int32_t len=0;
06302     char* filename;
06303     filename = get_string(len);
06304 
06305     CTrainPredMaster* tpm = new CTrainPredMaster(ui_kernel);
06306 
06307     tpm->read_models_from_file(filename);
06308 
06309     return true;
06310     }*/
06311 bool CSGInterface::cmd_signals_set_positions()
06312 {
06313     return true;
06314 }
06315 bool CSGInterface::cmd_signals_set_labels()
06316 {
06317     return true;
06318 }
06319 bool CSGInterface::cmd_signals_set_split()
06320 {
06321     return true;
06322 }
06323 bool CSGInterface::cmd_signals_set_train_mask()
06324 {
06325     return true;
06326 }
06327 bool CSGInterface::cmd_signals_add_feature()
06328 {
06329     return true;
06330 }
06331 bool CSGInterface::cmd_signals_add_kernel()
06332 {
06333     return true;
06334 }
06335 bool CSGInterface::cmd_signals_run()
06336 {
06337     return true;
06338 }
06339 
06340 bool CSGInterface::cmd_init_dyn_prog()
06341 {
06342     //ARG 1
06343     int32_t num_svms=get_int();
06344 
06345     CDynProg* h=new CDynProg(num_svms);
06346     ui_structure->set_dyn_prog(h);
06347     return true;
06348 }
06349 
06350 bool CSGInterface::cmd_clean_up_dyn_prog()
06351 {
06352     return ui_structure->cleanup();
06353 }
06354 
06355 bool CSGInterface::cmd_set_model()
06356 {
06357 
06358     CPlifMatrix* pm=ui_structure->get_plif_matrix();
06359 
06360     CDynProg* h = ui_structure->get_dyn_prog();
06361     int32_t num_svms = h->get_num_svms();
06362     //CDynProg* h=new CDynProg(Nweights/* = num_svms */);
06363 
06364     //ARG 1
06365     // transition pointers
06366     // link transitions to length, content, frame (and tiling)
06367     // plifs (#states x #states x 3 or 4)
06368     int32_t numDim=0;
06369     int32_t* Dim=0;
06370     float64_t* penalties_array=NULL;
06371     get_ndarray(penalties_array,Dim,numDim);
06372     ASSERT(numDim==3);
06373     ASSERT(Dim[0]==Dim[1]);
06374 
06375     if (!pm->compute_plif_matrix(SGNDArray<float64_t>(penalties_array, Dim, numDim)))
06376         SG_ERROR("error computing plif  matrix\n");
06377     ui_structure->set_num_states(Dim[0]);
06378     SG_FREE(penalties_array);
06379 
06380     // ARG 2
06381     // bool-> determines if orf information should be used
06382     bool use_orf = get_bool();
06383     ui_structure->set_use_orf(use_orf);
06384 
06385     // ARG 3
06386     // determines for which contents which orf should be used (#contents x 2)
06387     int32_t Nmod=0;
06388     int32_t Mmod=0;
06389     int32_t* mod_words;
06390     get_matrix(mod_words, Nmod,Mmod);
06391     if (Nmod != num_svms)
06392         SG_ERROR("should be equal: Nmod: %i, num_svms: %i\n",Nmod,num_svms);
06393     ASSERT(Mmod == 2)
06394     h->init_mod_words_array(SGMatrix<int32_t>(mod_words, Nmod, Mmod));
06395 
06396     // ARG 4
06397     // links: states -> signal plifs (#states x 2)
06398     int32_t num_states=0;
06399     int32_t feat_dim3=0;
06400     int32_t* state_signals;
06401     get_matrix(state_signals,num_states,feat_dim3);
06402     ASSERT(num_states==Dim[0]);
06403     pm->compute_signal_plifs(SGMatrix<int32_t>(state_signals, feat_dim3, num_states));
06404 
06405 
06406     // ARG 5
06407     // ORF info (#states x 2)
06408     int32_t Norf=0;
06409     int32_t Morf=0;
06410     int32_t* orf_info;
06411     get_matrix(orf_info,Norf,Morf);
06412     ASSERT(Norf==num_states)
06413     ASSERT(Morf==2)
06414 
06415     ui_structure->set_orf_info(orf_info, Norf, Morf);
06416     h->set_orf_info(SGMatrix<int32_t>(orf_info, Norf, Morf));
06417 
06418     h->set_num_states(num_states) ;
06419 
06420     return true;
06421 }
06422 
06423 bool CSGInterface::cmd_precompute_content_svms()
06424 {
06425 
06426     // ARG 1
06427     int32_t seq_len=0;
06428     char* seq;
06429     seq = get_string(seq_len);
06430 
06431     // ARG 2
06432     // all feature positions
06433     int32_t Npos=0;
06434     int32_t* all_pos;
06435     get_vector(all_pos, Npos);
06436 
06437     //ARG 3
06438     // content svm weights
06439     int32_t Nweights=0;
06440     int32_t num_svms=0;
06441     float64_t* weights;
06442     get_matrix(weights, Nweights, num_svms);
06443     if (Nweights!=5440)
06444       SG_PRINT("Dimension mismatch: got %i, expect %i\n", Nweights, 5440) ;
06445     ui_structure->set_content_svm_weights(weights, Nweights, num_svms);
06446 
06447     CDynProg* h = ui_structure->get_dyn_prog();
06448     if (!h)
06449         SG_ERROR("no DynProg object found, use init_dyn_prog first\n");
06450 
06451 
06452     //float64_t* weights = ui_structure->get_content_svm_weights();
06453     //int32_t Mweights = h->get_num_svms();
06454     //int32_t Nweights = ui_structure->get_num_svm_weights();
06455     h->set_pos(SGVector<int32_t>(all_pos, Npos));
06456     h->set_gene_string(SGVector<char>(seq, seq_len));
06457     h->create_word_string();
06458     h->precompute_stop_codons();
06459     h->init_content_svm_value_array(num_svms);
06460     h->set_dict_weights(SGMatrix<float64_t>(weights, Nweights, num_svms));
06461     h->precompute_content_values();
06462     SG_DEBUG("precompute_content_svms done\n");
06463     return true;
06464 }
06465 
06466 bool CSGInterface::cmd_get_lin_feat()
06467 {
06468     CDynProg* h = ui_structure->get_dyn_prog();
06469     if (!h)
06470         SG_ERROR("no DynProg object found, use set_model first\n");
06471 
06472 
06473     int32_t dim1, dim2 = 0;
06474     float64_t* lin_feat = h->get_lin_feat(dim1, dim2);
06475 
06476     set_matrix(lin_feat, dim1, dim2);
06477 
06478     return true;
06479 }
06480 bool CSGInterface::cmd_set_lin_feat()
06481 {
06482     // ARG 1
06483     int32_t Nseq=0;
06484     char* seq;
06485     seq = get_string(Nseq);
06486 
06487     // ARG 2
06488     // all feature positions
06489     int32_t Npos=0;
06490     int32_t* all_pos;
06491     get_vector(all_pos, Npos);
06492 
06493     //ARG 3
06494     //
06495     int32_t num_svms, seq_len;
06496     float64_t* lin_feat=NULL;
06497     get_matrix(lin_feat, num_svms, seq_len);
06498 
06499         if (Npos!=seq_len)
06500       {
06501         SG_ERROR("Dimension mismatch: got %i positions and (%ix%i) values\n", Npos, num_svms, seq_len) ;
06502 
06503         SG_FREE(lin_feat);
06504         SG_FREE(seq);
06505         SG_FREE(all_pos);
06506 
06507         return false ;
06508       }
06509 
06510     CDynProg* h = ui_structure->get_dyn_prog();
06511     if (!h)
06512         SG_ERROR("no DynProg object found, use set_model first\n");
06513 
06514     h->set_pos(SGVector<int32_t>(all_pos, Npos));
06515     h->set_gene_string(SGVector<char>(seq, Nseq));
06516     h->precompute_stop_codons();
06517     h->init_content_svm_value_array(num_svms);
06518     h->set_lin_feat(lin_feat, num_svms, seq_len);
06519 
06520     SG_FREE(lin_feat);
06521 
06522     return true;
06523 }
06524 bool CSGInterface::cmd_long_transition_settings()
06525 {
06526     bool use_long_transitions = get_bool();
06527     int32_t threshold = get_int();
06528     int32_t max_len = get_int();
06529 
06530     CDynProg* h = ui_structure->get_dyn_prog();
06531         if (!h)
06532                 SG_ERROR("no DynProg object found, use set_model first\n");
06533 
06534     h->long_transition_settings(use_long_transitions, threshold, max_len);
06535 
06536     return true;
06537 }
06538 bool CSGInterface::cmd_set_feature_matrix()
06539 {
06540     int32_t num_states = ui_structure->get_num_states();
06541 
06542     //ARG 1
06543     // feature matrix (#states x #feature_positions x max_num_signals)
06544     int32_t* Dims=0;
06545     int32_t numDims=0;
06546     float64_t* features = NULL;
06547     get_ndarray(features, Dims, numDims);
06548 
06549     if (numDims!=3)
06550         SG_ERROR("expected a 3 dimensional array, got %i dimensions\n", numDims);
06551     if (Dims[0]!=num_states)
06552         SG_ERROR("number of rows (%i) not equal number of states (%i)\n",Dims[0], num_states);
06553     ASSERT(ui_structure->set_feature_matrix(features, Dims));
06554 
06555     ASSERT(ui_structure->set_feature_dims(Dims));
06556 
06557     SG_FREE(features);
06558     SG_FREE(Dims);
06559 
06560     return true;
06561 }
06562 bool CSGInterface::cmd_set_feature_matrix_sparse()
06563 {
06564     int32_t num_pos = ui_structure->get_num_positions();
06565     int32_t num_states = ui_structure->get_num_states();
06566 
06567     //ARG 1
06568     // feature matrix (#states x #feature_positions x max_num_signals)
06569     int32_t dim11, dim12 ;
06570     SGSparseVector<float64_t> *features1=NULL ;
06571     get_sparse_matrix(features1, dim11, dim12);
06572 
06573     int32_t dim21, dim22 ;
06574     SGSparseVector<float64_t> *features2=NULL ;
06575     get_sparse_matrix(features2, dim21, dim22);
06576 
06577     ASSERT(dim11==dim21) ;
06578     ASSERT(dim12==dim22) ;
06579 
06580     int32_t *Dims = SG_MALLOC(int32_t, 3);
06581     Dims[0]=dim11 ;
06582     Dims[1]=dim12 ;
06583     Dims[2]=2 ;
06584 
06585     ASSERT(Dims[0]==num_states)
06586     ASSERT(Dims[1]==num_pos)
06587 
06588     ASSERT(ui_structure->set_feature_matrix_sparse(features1, features2, Dims));
06589     ASSERT(ui_structure->set_feature_dims(Dims));
06590 
06591     SG_FREE(features1);
06592     SG_FREE(features2);
06593     SG_FREE(Dims);
06594 
06595     return true;
06596 }
06597 bool CSGInterface::cmd_init_intron_list()
06598 {
06599     //ARG1 start_positions
06600     int32_t Nstart_positions;
06601     int32_t* start_positions;
06602     get_vector(start_positions, Nstart_positions);
06603         //SG_PRINT("Nstart_positions:%i\n",Nstart_positions);
06604 
06605     //ARG2 end_positions
06606     int32_t Nend_positions;
06607     int32_t* end_positions;
06608     get_vector(end_positions, Nend_positions);
06609         //SG_PRINT("Nend_positions:%i\n",Nend_positions);
06610 
06611     //ARG3 quality
06612     int32_t Nquality;
06613         int32_t* quality;
06614         get_vector(quality, Nquality);
06615         //SG_PRINT("Nquality:%i\n",Nquality);
06616 
06617     //ARG4 all candidate positions
06618     int32_t Nall_pos;
06619         int32_t* all_pos;
06620         get_vector(all_pos, Nall_pos);
06621         //SG_PRINT("Nall_pos:%i\n",Nall_pos);
06622 
06623     ASSERT(Nquality==Nend_positions);
06624     ASSERT(Nend_positions==Nstart_positions);
06625 
06626     CIntronList* intron_list = new CIntronList();
06627 
06628     intron_list->init_list(all_pos, Nall_pos);
06629 
06630     intron_list->read_introns(start_positions, end_positions, quality, Nstart_positions);
06631 
06632     SG_FREE(start_positions);
06633     SG_FREE(end_positions);
06634     SG_FREE(quality);
06635     SG_FREE(all_pos);
06636 
06637     //int32_t test;
06638     //int32_t testq;
06639     //intron_list->get_coverage(&test, &testq, 15 ,16);
06640 
06641     //SG_PRINT("coverage: %i, quality: %i\n",test, testq);
06642 
06643     CDynProg* h = ui_structure->get_dyn_prog();
06644     if (!h)
06645         SG_ERROR("no DynProg object found, use set_model first\n");
06646 
06647     h->set_intron_list(intron_list, 2);
06648 
06649     return true;
06650 }
06651 bool CSGInterface::cmd_precompute_tiling_features()
06652 {
06653     CPlifMatrix* pm=ui_structure->get_plif_matrix();
06654     CPlif** PEN  = pm->get_PEN();
06655     CDynProg* h  = ui_structure->get_dyn_prog();
06656 
06657     int32_t Nintensities=0;
06658     float64_t* intensities;
06659     get_vector(intensities, Nintensities);
06660 
06661     int32_t Nprobe_pos=0;
06662     int32_t* probe_pos;
06663     get_vector(probe_pos, Nprobe_pos);
06664     ASSERT(Nprobe_pos==Nintensities);
06665 
06666     int32_t Ntiling_plif_ids=0;
06667     int32_t* tiling_plif_ids;
06668     get_vector(tiling_plif_ids, Ntiling_plif_ids);
06669 
06670     h->init_tiling_data(probe_pos,intensities, Nprobe_pos);
06671     h->precompute_tiling_plifs(PEN, tiling_plif_ids, Ntiling_plif_ids);
06672     return true;
06673 }
06674 
06675 bool CSGInterface::cmd_best_path_trans()
06676 {
06677     CDynProg* h = ui_structure->get_dyn_prog();
06678 
06679     CSegmentLoss* seg_loss_obj = h->get_segment_loss_object();
06680 
06681     CPlifMatrix* pm=ui_structure->get_plif_matrix();
06682 
06683     int32_t num_states = h->get_num_states();
06684     int32_t* feat_dims = ui_structure->get_feature_dims();
06685     float64_t* features = (ui_structure->get_feature_matrix(false));
06686     CSparseFeatures<float64_t>* features_sparse1 = (ui_structure->get_feature_matrix_sparse(0));
06687     CSparseFeatures<float64_t>* features_sparse2 = (ui_structure->get_feature_matrix_sparse(1));
06688     int32_t* orf_info = ui_structure->get_orf_info();
06689     bool use_orf = ui_structure->get_use_orf();
06690     int32_t Nplif = pm->get_num_plifs();
06691 
06692     // ARG 1
06693     // transitions from initial state (#states x 1)
06694     int32_t Np=0;
06695     float64_t* p;
06696     get_vector(p, Np);
06697     if (Np!=num_states)
06698         SG_ERROR("# transitions from initial state (%i) does not match # states (%i)\n", Np, num_states);
06699 
06700     // ARG 2
06701     // transitions to end state (#states x 1)
06702     int32_t Nq=0;
06703     float64_t* q;
06704     get_vector(q, Nq);
06705     if (Nq!=num_states)
06706         SG_ERROR("# transitions to end state (%i) does not match # states (%i)\n", Nq, num_states);
06707 
06708     // ARG 3
06709     // number of best paths
06710     int32_t Nnbest=0;
06711     int32_t* all_nbest;
06712     get_vector(all_nbest, Nnbest);
06713     int32_t nbest;
06714     int32_t nother = 0;
06715     if (Nnbest==2)
06716     {
06717         nbest =all_nbest[0];
06718         nother=all_nbest[1];
06719     }
06720     else
06721         nbest =all_nbest[0];
06722     SG_FREE(all_nbest);
06723 
06724     // ARG 4
06725     // segment path (2 x #feature_positions)
06726     // masking/weighting of loss for specific
06727     // regions of the true path
06728     int32_t Nseg_path=0;
06729     int32_t Mseg_path=0;
06730     float64_t* seg_path;
06731     get_matrix(seg_path, Nseg_path, Mseg_path);
06732 
06733     // ARG 5
06734     // links for transitions (#transitions x 4)
06735     int32_t Na_trans=0;
06736     int32_t num_a_trans=0;
06737     float64_t* a_trans;
06738     get_matrix(a_trans, num_a_trans, Na_trans);
06739 
06740     // ARG 6
06741     // loss matrix (#segment x 2*#segments)
06742     // one (#segment x #segments)-matrix for segment loss
06743     // and one for nucleotide loss
06744     int32_t Nloss=0;
06745     int32_t Mloss=0;
06746     float64_t* loss;
06747     get_matrix(loss, Nloss,Mloss);
06748 
06749     int32_t M = h->get_num_positions();
06750 
06752     // check input
06754     ASSERT(num_states==Nq);
06755 
06756     CPlif** PEN=pm->get_PEN();
06757     ASSERT(PEN);
06758 
06759     h->set_p_vector(SGVector<float64_t>(p, num_states));
06760     h->set_q_vector(SGVector<float64_t>(q, num_states));
06761 
06762     if (seg_path!=NULL)
06763     {
06764         h->set_a_trans_matrix(SGMatrix<float64_t>(a_trans, num_a_trans, Na_trans)) ;
06765     }
06766     else
06767     {
06768         h->set_a_trans_matrix(SGMatrix<float64_t>(a_trans, num_a_trans, 3)) ; // segment_id = 0
06769     }
06770 
06771     if (!h->check_svm_arrays())
06772     {
06773         SG_ERROR( "svm arrays inconsistent\n") ;
06774         CPlif::delete_penalty_struct(PEN, Nplif) ;
06775         return false ;
06776     }
06777 
06778     SG_DEBUG("best_path_trans: M: %i, Mseg_path: %i\n", M, Mseg_path);
06779 
06780     h->set_observation_matrix(SGNDArray<float64_t>(features, feat_dims, 3));
06781 
06782     if (seg_path!=NULL)
06783     {
06784         h->best_path_set_segment_loss(SGMatrix<float64_t>(loss, Nloss, Mloss, false)) ;
06785         seg_loss_obj->set_segment_loss(loss, Nloss, Mloss);
06786     }
06787     else
06788     {
06789         float64_t zero2[2] = {0.0, 0.0} ;
06790         h->best_path_set_segment_loss(SGMatrix<float64_t>(zero2, 2, 1)) ;
06791         seg_loss_obj->set_segment_loss(zero2, 2, 1);
06792     }
06793     h->set_content_type_array(SGMatrix<float64_t>(seg_path,Nseg_path,Mseg_path));
06794 
06795     bool segment_loss_non_zero=false;
06796     for (int32_t i=0; i<Nloss*Mloss; i++)
06797     {
06798         if (loss[i]>1e-3)
06799             segment_loss_non_zero=true;
06800     }
06801 
06802     SG_FREE(loss);
06803     loss=NULL;
06804 
06805     h->set_orf_info(SGMatrix<int32_t>(orf_info, num_states, 2));
06806     h->set_sparse_features(features_sparse1, features_sparse2);
06807     h->set_plif_matrices(pm);
06808 
06809     if (segment_loss_non_zero)
06810     {
06811             SG_DEBUG("Using version with segment_loss\n") ;
06812             if (nbest==1)
06813                 h->compute_nbest_paths(feat_dims[2], use_orf, 1,true,false);
06814             else
06815                 h->compute_nbest_paths(feat_dims[2], use_orf, 2,true,false);
06816     }
06817     else
06818     {
06819             SG_DEBUG("Using version without segment_loss\n") ;
06820             if (nbest==1)
06821                 h->compute_nbest_paths(feat_dims[2], use_orf, 1,false,false);
06822             else
06823                 h->compute_nbest_paths(feat_dims[2], use_orf, 2,false,false);
06824     }
06825 
06826     SGVector<float64_t> p_prob=h->get_scores();
06827 
06828     SGMatrix<int32_t> states=h->get_states();
06829 
06830     SGMatrix<int32_t> my_pos=h->get_positions();
06831 
06832     // transcribe result
06833     float64_t* d_my_path= SG_MALLOC(float64_t, (nbest+nother)*M);
06834     float64_t* d_my_pos= SG_MALLOC(float64_t, (nbest+nother)*M);
06835 
06836     for (int32_t k=0; k<(nbest+nother); k++)
06837     {
06838         for (int32_t i=0; i<M; i++)
06839         {
06840             d_my_path[i*(nbest+nother)+k] = states.matrix[i+k*M] ;
06841             d_my_pos[i*(nbest+nother)+k] = my_pos.matrix[i+k*M] ;
06842         }
06843     }
06844     set_vector(p_prob.vector,nbest+nother);
06845     set_vector(d_my_path, (nbest+nother)*M);
06846     set_vector(d_my_pos, (nbest+nother)*M);
06847 
06848     SG_FREE(d_my_path);
06849     SG_FREE(d_my_pos);
06850 
06851     return true;
06852 
06853 }
06854 
06855 bool CSGInterface::cmd_best_path_trans_deriv()
06856 {
06857     int32_t num_states = ui_structure->get_num_states();
06858     int32_t* feat_dims = ui_structure->get_feature_dims();
06859     float64_t* features = (ui_structure->get_feature_matrix(false));
06860 
06861     CPlifMatrix* pm=ui_structure->get_plif_matrix();
06862     int32_t Nplif = pm->get_num_plifs();
06863     CPlif** PEN = pm->get_PEN();
06864 
06865     // ARG 1
06866     // transitions from initial state (#states x 1)
06867     int32_t Np=0;
06868     float64_t* p=NULL;
06869     get_vector(p, Np);
06870     if (Np!=num_states)
06871         SG_ERROR("Np!=num_states; Np:%i num_states:%i",Np,num_states);
06872 
06873     // ARG 2
06874     // transitions to end state (#states x 1)
06875     int32_t Nq=0;
06876     float64_t* q=NULL;
06877     get_vector(q, Nq);
06878     if (Nq!=num_states)
06879         SG_ERROR("Nq!=num_states; Nq:%i num_states:%i",Nq,num_states);
06880 
06881 
06882     // ARG 3
06883     // segment path (2 x #feature_positions)
06884     // masking/weighting of loss for specific
06885     // regions of the true path
06886     int32_t Nseg_path=0;
06887     int32_t Mseg_path=0;
06888     float64_t* seg_path;
06889     get_matrix(seg_path,Nseg_path,Mseg_path);
06890 
06891     // ARG 4
06892     // links for transitions (#transitions x 4)
06893     int32_t Na_trans=0;
06894     int32_t num_a_trans=0;
06895     float64_t* a_trans=NULL;
06896     get_matrix(a_trans, num_a_trans, Na_trans);
06897 
06898     // ARG 5
06899     // loss matrix (#segment x 2*#segments)
06900     // one (#segment x #segments)-matrix for segment loss
06901     // and one for nucleotide loss
06902     int32_t Nloss=0;
06903     int32_t Mloss=0;
06904     float64_t* loss=NULL;
06905     get_matrix(loss, Nloss,Mloss);
06906 
06907     // ARG 6
06908     // path to calc derivative for
06909     int32_t Nmystate_seq=0;
06910     int32_t* mystate_seq=NULL;
06911     get_vector(mystate_seq, Nmystate_seq);
06912 
06913     // ARG 7
06914     // positions of the path
06915     int32_t Nmypos_seq=0;
06916     int32_t* mypos_seq=NULL;
06917     get_vector(mypos_seq, Nmypos_seq);
06918 
06919 
06920     //a => a_trans
06921 
06922     int32_t max_plif_id = 0 ;
06923     int32_t max_plif_len = 1 ;
06924     for (int32_t i=0; i<Nplif; i++)
06925     {
06926         if (i>0 && PEN[i]->get_id()!=i)
06927             SG_ERROR("PEN[i]->get_id()!=i; PEN[%i]->get_id():%i  ,\n",i, PEN[i]->get_id());
06928         if (i>max_plif_id)
06929             max_plif_id=i ;
06930         if (PEN[i]->get_plif_len()>max_plif_len)
06931             max_plif_len=PEN[i]->get_plif_len() ;
06932     } ;
06933 
06934 
06935     CDynProg* h = ui_structure->get_dyn_prog();
06936     CSegmentLoss* seg_loss_obj = h->get_segment_loss_object();
06937     h->set_num_states(num_states) ;
06938     h->set_p_vector(SGVector<float64_t>(p, num_states)) ;
06939     h->set_q_vector(SGVector<float64_t>(q, num_states)) ;
06940 
06941     if (seg_path!=NULL)
06942         h->set_a_trans_matrix(SGMatrix<float64_t>(a_trans, num_a_trans, Na_trans)) ;
06943     else
06944         h->set_a_trans_matrix(SGMatrix<float64_t>(a_trans, num_a_trans, 3)) ;
06945 
06946     if (!h->check_svm_arrays())
06947         SG_ERROR( "svm arrays inconsistent\n") ;
06948 
06949     int32_t *my_path = SG_MALLOC(int32_t, Nmypos_seq+1);
06950     memset(my_path, -1, Nmypos_seq*sizeof(int32_t)) ;
06951     int32_t *my_pos = SG_MALLOC(int32_t, Nmypos_seq+1);
06952     memset(my_pos, -1, Nmypos_seq*sizeof(int32_t)) ;
06953 
06954     h->set_observation_matrix(SGNDArray<float64_t>(features, feat_dims, 3));
06955     for (int32_t i=0; i<Nmypos_seq; i++)
06956     {
06957         my_path[i] = mystate_seq[i] ;
06958         my_pos[i]  = mypos_seq[i] ;
06959     }
06960 
06961     if (seg_path!=NULL)
06962     {
06963         h->best_path_set_segment_loss(SGMatrix<float64_t>(loss, Nloss, Mloss)) ;
06964         seg_loss_obj->set_segment_loss(loss, Nloss, Mloss);
06965     }
06966     else
06967     {
06968         float64_t zero2[2] = {0.0, 0.0} ;
06969         h->best_path_set_segment_loss(SGMatrix<float64_t>(zero2, 2, 1, false)) ;
06970         seg_loss_obj->set_segment_loss(zero2, 2, 1);
06971     }
06972     h->set_content_type_array(SGMatrix<float64_t>(seg_path,Nseg_path,Mseg_path));
06973 
06974     float64_t* p_Plif_deriv = SG_MALLOC(float64_t, (max_plif_id+1)*max_plif_len);
06975     CDynamicArray <float64_t> a_Plif_deriv(p_Plif_deriv, max_plif_id+1, max_plif_len, false, false) ; // 2d
06976 
06977     float64_t* p_A_deriv   = SG_MALLOC(float64_t, num_states*num_states);
06978     float64_t* p_p_deriv   = SG_MALLOC(float64_t, num_states);
06979     float64_t* p_q_deriv   = SG_MALLOC(float64_t, num_states);
06980 
06981     h->set_plif_matrices(pm);
06982     h->best_path_trans_deriv(my_path, my_pos, Nmypos_seq, features, feat_dims[2]);
06983 
06984     float64_t* p_my_scores;
06985     int32_t n_scores;
06986     h->get_path_scores(&p_my_scores, &n_scores);
06987 
06988     float64_t* p_my_losses;
06989     int32_t n_losses;
06990     h->get_path_losses(&p_my_losses, &n_losses);
06991 
06992     for (int32_t i=0; i<num_states; i++)
06993     {
06994         for (int32_t j=0; j<num_states; j++)
06995             p_A_deriv[i+j*num_states] = h->get_a_deriv(i, j) ;
06996 
06997         p_p_deriv[i]=h->get_p_deriv(i) ;
06998         p_q_deriv[i]=h->get_q_deriv(i) ;
06999     }
07000 
07001     for (int32_t id=0; id<=max_plif_id; id++)
07002     {
07003         int32_t len=0 ;
07004         const float64_t * deriv = PEN[id]->get_cum_derivative(len) ;
07005         ASSERT(len<=max_plif_len) ;
07006         for (int32_t j=0; j<max_plif_len; j++)
07007             a_Plif_deriv.element(id, j)= deriv[j] ;
07008     }
07009 
07010     set_vector(p_p_deriv, num_states);
07011     set_vector(p_q_deriv, num_states);
07012     set_matrix(p_A_deriv, num_states, num_states);
07013     set_matrix(p_Plif_deriv, (max_plif_id+1), max_plif_len);
07014     set_vector(p_my_scores, Nmypos_seq);
07015     set_vector(p_my_losses, Nmypos_seq);
07016 
07017     SG_FREE(p_A_deriv);
07018     SG_FREE(p_p_deriv);
07019     SG_FREE(p_q_deriv);
07020     SG_FREE(p_Plif_deriv);
07021     free(p_my_scores);
07022     free(p_my_losses);
07023 
07024     SG_FREE(my_path);
07025     SG_FREE(my_pos);
07026 
07027     SG_FREE(p);
07028     SG_FREE(q);
07029     SG_FREE(a_trans);
07030     SG_FREE(loss);
07031     SG_FREE(mystate_seq);
07032     SG_FREE(mypos_seq);
07033 
07034     return true ;
07035 }
07036 
07037 bool CSGInterface::cmd_precompute_subkernels()
07038 {
07039     if (m_nrhs!=1 || !create_return_values(0))
07040         return false;
07041 
07042     return ui_kernel->precompute_subkernels();
07043 }
07044 bool CSGInterface::cmd_crc()
07045 {
07046     if (m_nrhs!=2 || !create_return_values(1))
07047         return false;
07048 
07049     int32_t slen=0;
07050     char* string=get_string(slen);
07051     ASSERT(string);
07052     uint8_t* bstring=SG_MALLOC(uint8_t, slen);
07053 
07054     for (int32_t i=0; i<slen; i++)
07055         bstring[i]=string[i];
07056     SG_FREE(string);
07057 
07058     int32_t val=CHash::crc32(bstring, slen);
07059     SG_FREE(bstring);
07060     set_int(val);
07061 
07062     return true;
07063 }
07064 
07065 bool CSGInterface::cmd_system()
07066 {
07067     if (m_nrhs<2 || !create_return_values(0))
07068         return false;
07069 
07070     int32_t len=0;
07071     char* command=SG_MALLOC(char, 10000);
07072     memset(command, 0, sizeof(char)*10000);
07073     char* cmd=get_str_from_str_or_direct(len);
07074     strncat(command, cmd, 10000);
07075     SG_FREE(cmd);
07076 
07077     while (m_rhs_counter<m_nrhs)
07078     {
07079         strncat(command, " ", 10000);
07080         char* arg=get_str_from_str_or_direct(len);
07081         strncat(command, arg, 10000);
07082         SG_FREE(arg);
07083     }
07084 
07085     int32_t success=system(command);
07086 
07087     return (success==0);
07088 }
07089 
07090 bool CSGInterface::cmd_exit()
07091 {
07092     exit(0);
07093     return 0; //never reached but necessary to keep sun compiler happy
07094 }
07095 
07096 bool CSGInterface::cmd_exec()
07097 {
07098     if (m_nrhs<2 || !create_return_values(0))
07099         return false;
07100 
07101     int32_t len=0;
07102     char* filename=get_str_from_str_or_direct(len);
07103     FILE* file=fopen(filename, "r");
07104     if (!file)
07105     {
07106         SG_FREE(filename);
07107         SG_ERROR("Error opening file: %s.\n", filename);
07108     }
07109 
07110     while (!feof(file))
07111     {
07112         // FIXME: interpret lines as input
07113         break;
07114     }
07115 
07116     fclose(file);
07117     return true;
07118 }
07119 
07120 bool CSGInterface::cmd_set_output()
07121 {
07122     if (m_nrhs<2 || !create_return_values(0))
07123         return false;
07124 
07125     int32_t len=0;
07126     char* filename=get_str_from_str_or_direct(len);
07127 
07128     if (file_out)
07129         fclose(file_out);
07130     file_out=NULL;
07131 
07132     SG_INFO("Setting output file to: %s.\n", filename);
07133 
07134     if (strmatch(filename, "STDERR"))
07135         io->set_target(stderr);
07136     else if (strmatch(filename, "STDOUT"))
07137         io->set_target(stdout);
07138     else
07139     {
07140         file_out=fopen(filename, "w");
07141         if (!file_out)
07142             SG_ERROR("Error opening output file %s.\n", filename);
07143         io->set_target(file_out);
07144     }
07145 
07146     return true;
07147 }
07148 
07149 bool CSGInterface::cmd_set_threshold()
07150 {
07151     if (m_nrhs!=2 || !create_return_values(0))
07152         return false;
07153 
07154     float64_t value=get_real_from_real_or_str();
07155 
07156     ui_math->set_threshold(value);
07157     return true;
07158 }
07159 
07160 bool CSGInterface::cmd_init_random()
07161 {
07162     if (m_nrhs!=2 || !create_return_values(0))
07163         return false;
07164 
07165     uint32_t initseed=(uint32_t) get_int_from_int_or_str();
07166     ui_math->init_random(initseed);
07167 
07168     return true;
07169 }
07170 
07171 bool CSGInterface::cmd_set_num_threads()
07172 {
07173     if (m_nrhs!=2 || !create_return_values(0))
07174         return false;
07175 
07176     int32_t num_threads=get_int_from_int_or_str();
07177 
07178     parallel->set_num_threads(num_threads);
07179     SG_INFO("Set number of threads to %d.\n", num_threads);
07180 
07181     return true;
07182 }
07183 
07184 bool CSGInterface::cmd_translate_string()
07185 {
07186     if (m_nrhs!=4 || !create_return_values(1))
07187         return false;
07188 
07189     float64_t* string=NULL;
07190     int32_t len;
07191     get_vector(string, len);
07192 
07193     int32_t order=get_int();
07194     int32_t start=get_int();
07195 
07196     const int32_t max_val=2; /* DNA->2bits */
07197     int32_t i,j;
07198     uint16_t* obs=SG_MALLOC(uint16_t, len);
07199 
07200     for (i=0; i<len; i++)
07201     {
07202         switch ((char) string[i])
07203         {
07204             case 'A': obs[i]=0; break;
07205             case 'C': obs[i]=1; break;
07206             case 'G': obs[i]=2; break;
07207             case 'T': obs[i]=3; break;
07208             case 'a': obs[i]=0; break;
07209             case 'c': obs[i]=1; break;
07210             case 'g': obs[i]=2; break;
07211             case 't': obs[i]=3; break;
07212             default: SG_ERROR("Wrong letter in string.\n");
07213         }
07214     }
07215 
07216     //convert interval of size T
07217     for (i=len-1; i>=order-1; i--)
07218     {
07219         uint16_t value=0;
07220         for (j=i; j>=i-order+1; j--)
07221             value=(value>>max_val) | ((obs[j])<<(max_val*(order-1)));
07222 
07223         obs[i]=(uint16_t) value;
07224     }
07225 
07226     for (i=order-2;i>=0;i--)
07227     {
07228         uint16_t value=0;
07229         for (j=i; j>=i-order+1; j--)
07230         {
07231             value= (value >> max_val);
07232             if (j>=0)
07233                 value|=(obs[j]) << (max_val * (order-1));
07234         }
07235         obs[i]=value;
07236     }
07237 
07238     float64_t* real_obs=SG_MALLOC(float64_t, len);
07239     for (i=start; i<len; i++)
07240         real_obs[i-start]=(float64_t) obs[i];
07241     SG_FREE(obs);
07242 
07243     set_vector(real_obs, len);
07244     SG_FREE(real_obs);
07245 
07246     return true;
07247 }
07248 
07249 bool CSGInterface::cmd_clear()
07250 {
07251     // reset guilib
07252     SG_UNREF(ui_classifier);
07253     ui_classifier=new CGUIClassifier(this);
07254     SG_UNREF(ui_distance);
07255     ui_distance=new CGUIDistance(this);
07256     SG_UNREF(ui_features);
07257     ui_features=new CGUIFeatures(this);
07258     SG_UNREF(ui_hmm);
07259     ui_hmm=new CGUIHMM(this);
07260     SG_UNREF(ui_kernel);
07261     ui_kernel=new CGUIKernel(this);
07262     SG_UNREF(ui_labels);
07263     ui_labels=new CGUILabels(this);
07264     SG_UNREF(ui_math);
07265     ui_math=new CGUIMath(this);
07266     SG_UNREF(ui_pluginestimate);
07267     ui_pluginestimate=new CGUIPluginEstimate(this);
07268     SG_UNREF(ui_preproc);
07269     ui_preproc=new CGUIPreprocessor(this);
07270     SG_UNREF(ui_time);
07271     ui_time=new CGUITime(this);
07272 
07273     return true;
07274 }
07275 
07276 bool CSGInterface::cmd_tic()
07277 {
07278     ui_time->start();
07279     return true;
07280 }
07281 
07282 bool CSGInterface::cmd_toc()
07283 {
07284     ui_time->stop();
07285     return true;
07286 }
07287 
07288 bool CSGInterface::cmd_print()
07289 {
07290     if (m_nrhs<2 || !create_return_values(0))
07291         return false;
07292 
07293     int32_t len=0;
07294     char* msg=get_str_from_str_or_direct(len);
07295 
07296     SG_PRINT("%s\n", msg);
07297 
07298     SG_FREE(msg);
07299     return true;
07300 }
07301 
07302 bool CSGInterface::cmd_echo()
07303 {
07304     if (m_nrhs<2 || !create_return_values(0))
07305         return false;
07306 
07307     int32_t len=0;
07308     char* level=get_str_from_str_or_direct(len);
07309 
07310     if (strmatch(level, "OFF"))
07311     {
07312         echo=false;
07313         SG_INFO("Echo is off.\n");
07314     }
07315     else
07316     {
07317         echo=true;
07318         SG_INFO("Echo is on.\n");
07319     }
07320 
07321     SG_FREE(level);
07322     return true;
07323 }
07324 
07325 bool CSGInterface::cmd_loglevel()
07326 {
07327     if (m_nrhs<2 || !create_return_values(0))
07328         return false;
07329 
07330     int32_t len=0;
07331     char* level=get_str_from_str_or_direct(len);
07332 
07333     if (strmatch(level, "ALL") || strmatch(level, "GCDEBUG"))
07334         io->set_loglevel(MSG_GCDEBUG);
07335     else if (strmatch(level, "DEBUG"))
07336         io->set_loglevel(MSG_DEBUG);
07337     else if (strmatch(level, "INFO"))
07338         io->set_loglevel(MSG_INFO);
07339     else if (strmatch(level, "NOTICE"))
07340         io->set_loglevel(MSG_NOTICE);
07341     else if (strmatch(level, "WARN"))
07342         io->set_loglevel(MSG_WARN);
07343     else if (strmatch(level, "ERROR"))
07344         io->set_loglevel(MSG_ERROR);
07345     else if (strmatch(level, "CRITICAL"))
07346         io->set_loglevel(MSG_CRITICAL);
07347     else if (strmatch(level, "ALERT"))
07348         io->set_loglevel(MSG_ALERT);
07349     else if (strmatch(level, "EMERGENCY"))
07350         io->set_loglevel(MSG_EMERGENCY);
07351     else
07352         SG_ERROR("Unknown loglevel '%s'.\n", level);
07353 
07354     SG_INFO("Loglevel set to %s.\n", level);
07355 
07356     SG_FREE(level);
07357     return true;
07358 }
07359 
07360 bool CSGInterface::cmd_syntax_highlight()
07361 {
07362     if (m_nrhs<2 || !create_return_values(0))
07363         return false;
07364 
07365     int32_t len=0;
07366     char* hili=get_str_from_str_or_direct(len);
07367 
07368     if (strmatch(hili, "ON"))
07369     {
07370         hilight.set_ansi_syntax_hilighting();
07371         io->enable_syntax_highlighting();
07372     }
07373     else if (strmatch(hili, "OFF"))
07374     {
07375         hilight.disable_syntax_hilighting();
07376         io->disable_syntax_highlighting();
07377     }
07378     else
07379         SG_ERROR("arguments to " N_SYNTAX_HIGHLIGHT " are ON|OFF - found '%s'.\n", hili);
07380 
07381     SG_INFO("Syntax hilighting set to %s.\n", hili);
07382 
07383     SG_FREE(hili);
07384     return true;
07385 }
07386 
07387 bool CSGInterface::cmd_progress()
07388 {
07389     if (m_nrhs<2 || !create_return_values(0))
07390         return false;
07391 
07392     int32_t len=0;
07393     char* progress=get_str_from_str_or_direct(len);
07394 
07395     if (strmatch(progress, "ON"))
07396         io->enable_progress();
07397     else if (strmatch(progress, "OFF"))
07398         io->disable_progress();
07399     else
07400         SG_ERROR("arguments to progress are ON|OFF - found '%s'.\n", progress);
07401 
07402     SG_INFO("Progress set to %s.\n", progress);
07403 
07404     SG_FREE(progress);
07405     return true;
07406 }
07407 
07408 bool CSGInterface::cmd_get_version()
07409 {
07410     if (m_nrhs!=1 || !create_return_values(1))
07411         return false;
07412 
07413     set_int(version->get_version_revision());
07414 
07415     return true;
07416 }
07417 
07418 bool CSGInterface::cmd_help()
07419 {
07420     if ((m_nrhs!=1 && m_nrhs!=2) || !create_return_values(0))
07421         return false;
07422 
07423     int32_t i=0;
07424 
07425     SG_PRINT("\n");
07426     if (m_nrhs==1) // unspecified help
07427     {
07428         SG_PRINT("Help is available for the following topics.\n"
07429                  "-------------------------------------------\n\n");
07430         while (sg_methods[i].command)
07431         {
07432             bool is_group_item=false;
07433             if (!sg_methods[i].method && !sg_methods[i].usage_prefix)
07434                 is_group_item=true;
07435 
07436             if (is_group_item)
07437             {
07438                 SG_PRINT("%s%s%s\n",
07439                         hilight.get_command_prefix(),
07440                         sg_methods[i].command,
07441                         hilight.get_command_suffix());
07442             }
07443 
07444             i++;
07445         }
07446         SG_PRINT("\nUse sg('%shelp%s', '%s<topic>%s')"
07447                 " to see the list of commands in this group, e.g.\n\n"
07448                 "\tsg('%shelp%s', '%sFeatures%s')\n\n"
07449                 "to see the list of commands for the 'Features' group.\n"
07450                 "\nOr use sg('%shelp%s', '%sall%s')"
07451                 " to see a brief listing of all commands.\n\nTo disable syntax"
07452                 " highlighting (useful e.g. in the matlab GUI) use\n\n"
07453                 "\tsg('syntax_highlight','OFF')\n",
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                     hilight.get_command_prefix(), hilight.get_command_suffix());
07460     }
07461     else // m_nrhs == 2 -> all commands, single command or group help
07462     {
07463         bool found=false;
07464         bool in_group=false;
07465         int32_t clen=0;
07466         char* command=get_string(clen);
07467 
07468         if (strmatch("doxygen", command) || strmatch("DOXYGEN", command))
07469         {
07470             found=true;
07471             while (sg_methods[i].command)
07472             {
07473                 if (sg_methods[i].usage_prefix) // display group item
07474                 {
07475                     SG_PRINT("\\arg \\b %s \\verbatim %s%s%s \\endverbatim\n",
07476                             sg_methods[i].command,
07477                             sg_methods[i].usage_prefix,
07478                             sg_methods[i].command,
07479                             sg_methods[i].usage_suffix);
07480                 }
07481                 else if (!sg_methods[i].method) // display group
07482                 {
07483                     SG_PRINT("\n\\section %s_sec %s\n",
07484                             sg_methods[i].command, sg_methods[i].command);
07485                 }
07486                 i++;
07487             }
07488         }
07489         if (strmatch("all", command) || strmatch("ALL", command))
07490         {
07491             found=true;
07492             while (sg_methods[i].command)
07493             {
07494                 if (sg_methods[i].usage_prefix) // display group item
07495                 {
07496                     SG_PRINT("\t%s%s%s%s%s\n", sg_methods[i].usage_prefix,
07497                             hilight.get_command_prefix(),
07498                             sg_methods[i].command,
07499                             hilight.get_command_suffix(),
07500                             sg_methods[i].usage_suffix);
07501                 }
07502                 else if (!sg_methods[i].method) // display group
07503                 {
07504                     SG_PRINT("\nCommands in group %s%s%s\n",
07505                             hilight.get_command_prefix(),
07506                             sg_methods[i].command,
07507                             hilight.get_command_suffix());
07508                 }
07509                 i++;
07510             }
07511         }
07512         else
07513         {
07514             while (sg_methods[i].command)
07515             {
07516                 if (in_group)
07517                 {
07518                     if (sg_methods[i].usage_prefix) // display group item
07519                         SG_PRINT("\t%s%s%s\n",
07520                             hilight.get_command_prefix(),
07521                             sg_methods[i].command,
07522                             hilight.get_command_suffix());
07523                     else // next group reached -> end
07524                         break;
07525                 }
07526                 else
07527                 {
07528                     found=strmatch(sg_methods[i].command, command);
07529                     if (found)
07530                     {
07531                         if (sg_methods[i].usage_prefix) // found item
07532                         {
07533                             SG_PRINT("Usage for %s%s%s\n\n\t%s%s%s%s%s\n",
07534                                     hilight.get_command_prefix(),
07535                                     sg_methods[i].command,
07536                                     hilight.get_command_suffix(),
07537                                     sg_methods[i].usage_prefix,
07538                                     hilight.get_command_prefix(),
07539                                     sg_methods[i].command,
07540                                     hilight.get_command_suffix(),
07541                                     sg_methods[i].usage_suffix);
07542                             break;
07543                         }
07544                         else // found group item
07545                         {
07546                             SG_PRINT("Commands in group %s%s%s\n\n",
07547                                     hilight.get_command_prefix(),
07548                                     sg_methods[i].command,
07549                                     hilight.get_command_suffix());
07550                             in_group=true;
07551                         }
07552                     }
07553                 }
07554 
07555                 i++;
07556             }
07557         }
07558 
07559         if (!found)
07560             SG_PRINT("Could not find help for command %s.\n", command);
07561         else if (in_group)
07562         {
07563             SG_PRINT("\n\nUse sg('%shelp%s', '%s<command>%s')"
07564                     " to see the usage pattern of a single command, e.g.\n\n"
07565                     "\tsg('%shelp%s', '%sclassify%s')\n\n"
07566                     " to see the usage pattern of the command 'classify'.\n",
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                     hilight.get_command_prefix(), hilight.get_command_suffix());
07571         }
07572 
07573         SG_FREE(command);
07574     }
07575 
07576 
07577     SG_PRINT("\n");
07578 
07579     return true;
07580 }
07581 #ifdef TRACE_MEMORY_ALLOCS
07582     extern CMap<void*, MemoryBlock>* sg_mallocs;
07583 #endif
07584 
07585 bool CSGInterface::cmd_whos()
07586 {
07587     if ((m_nrhs!=1) || !create_return_values(0))
07588         return false;
07589 
07590 #ifdef TRACE_MEMORY_ALLOCS
07591     SG_PRINT("Blocks allocated by shogun\n");
07592     list_memory_allocs();
07593     SG_PRINT("\n");
07594     return true;
07595 #else
07596     SG_PRINT("Requires shogun to be compiled with --enable-trace-mallocs\n");
07597     return false;
07598 #endif
07599 }
07600 
07601 bool CSGInterface::cmd_send_command()
07602 {
07603     SG_DEPRECATED;
07604 
07605     int32_t len=0;
07606     char* arg=get_string(len);
07607     //SG_DEBUG("legacy: arg == %s\n", arg);
07608     m_legacy_strptr=arg;
07609 
07610     char* command=get_str_from_str(len);
07611     int32_t i=0;
07612     bool success=false;
07613 
07614     while (sg_methods[i].command)
07615     {
07616         if (strmatch(command, sg_methods[i].command))
07617         {
07618             SG_DEBUG("legacy: found command %s\n", sg_methods[i].command);
07619             // fix-up m_nrhs; +1 to include command
07620             m_nrhs=get_num_args_in_str()+1;
07621 
07622             if (!(interface->*(sg_methods[i].method))())
07623             {
07624                 SG_ERROR("Usage: %s%s%s\n\n\t%s%s%s%s%s\n",
07625                         hilight.get_command_prefix(),
07626                         sg_methods[i].command,
07627                         hilight.get_command_suffix(),
07628                         sg_methods[i].usage_prefix,
07629                         hilight.get_command_prefix(),
07630                         sg_methods[i].command,
07631                         hilight.get_command_suffix(),
07632                         sg_methods[i].usage_suffix);
07633             }
07634             else
07635             {
07636                 success=true;
07637                 break;
07638             }
07639         }
07640 
07641         i++;
07642     }
07643 
07644     if (!success)
07645         SG_ERROR("Non-supported legacy command %s.\n", command);
07646 
07647     SG_FREE(command);
07648     SG_FREE(arg);
07649     return success;
07650 }
07651 
07652 bool CSGInterface::cmd_run_python()
07653 {
07654     SG_ERROR("Only available in the elwms interface\n");
07655     return false;
07656 }
07657 
07658 bool CSGInterface::cmd_run_octave()
07659 {
07660     SG_ERROR("Only available in the elwms interface\n");
07661     return false;
07662 }
07663 
07664 bool CSGInterface::cmd_run_r()
07665 {
07666     SG_ERROR("Only available in the elwms interface\n");
07667     return false;
07668 }
07669 
07670 bool CSGInterface::cmd_pr_loqo()
07671 {
07672     if (m_nrhs!=7 || !create_return_values(2))
07673         return false;
07674 
07675     float64_t* c=NULL;
07676     int32_t lenc=0;
07677     get_vector(c, lenc);
07678 
07679     int32_t n = lenc;
07680 
07681     float64_t* H=NULL;
07682     int32_t nH=0;
07683     int32_t mH=0;
07684     get_matrix(H, nH, mH);
07685     ASSERT(nH==n && mH==n);
07686 
07687     float64_t* A=NULL;
07688     int32_t nA=0;
07689     int32_t mA=0;
07690     get_matrix(A, nA, mA);
07691     ASSERT(mA==n);
07692     int32_t m=nA;
07693 
07694     float64_t* b=NULL;
07695     int32_t lenb=0;
07696     get_vector(b, lenb);
07697     ASSERT(lenb==m);
07698 
07699     float64_t* l=NULL;
07700     int32_t lenl=0;
07701     get_vector(l, lenl);
07702     ASSERT(lenl==n);
07703 
07704     float64_t* u=NULL;
07705     int32_t lenu=0;
07706     get_vector(u, lenu);
07707     ASSERT(lenu==n);
07708 
07709     float64_t* x=SG_MALLOC(float64_t, 3*n);
07710     SGVector<float64_t>::fill_vector(x, 3*n, 0.0);
07711 
07712     float64_t* y=SG_MALLOC(float64_t, m+2*n);
07713     SGVector<float64_t>::fill_vector(y, m+2*n, 0.0);
07714 
07715     pr_loqo(n,m, c, H, A, b, l, u, x, y, 0, 5, 50, 0.05, 100, 0);
07716 
07717     set_vector(x, n);
07718     set_vector(y, m);
07719 
07720     SG_FREE(c);
07721     SG_FREE(H);
07722     SG_FREE(A);
07723     SG_FREE(b);
07724     SG_FREE(l);
07725     SG_FREE(u);
07726     SG_FREE(x);
07727     SG_FREE(y);
07728     return true;
07729 }
07730 
07731 void CSGInterface::print_prompt()
07732 {
07733     SG_PRINT("%sshogun%s >> ",
07734             hilight.get_prompt_prefix(),
07735             hilight.get_prompt_suffix());
07736 }
07737 
07739 // legacy-related methods
07741 
07742 char* CSGInterface::get_str_from_str_or_direct(int32_t& len)
07743 {
07744     if (m_legacy_strptr)
07745         return get_str_from_str(len);
07746     else
07747         return get_string(len);
07748 }
07749 
07750 int32_t CSGInterface::get_int_from_int_or_str()
07751 {
07752     if (m_legacy_strptr)
07753     {
07754         int32_t len=0;
07755         char* str=get_str_from_str(len);
07756         int32_t val=strtol(str, NULL, 10);
07757 
07758         SG_FREE(str);
07759         return val;
07760     }
07761     else
07762         return get_int();
07763 }
07764 
07765 float64_t CSGInterface::get_real_from_real_or_str()
07766 {
07767     if (m_legacy_strptr)
07768     {
07769         int32_t len=0;
07770         char* str=get_str_from_str(len);
07771         float64_t val=strtod(str, NULL);
07772 
07773         SG_FREE(str);
07774         return val;
07775     }
07776     else
07777         return get_real();
07778 }
07779 
07780 bool CSGInterface::get_bool_from_bool_or_str()
07781 {
07782     if (m_legacy_strptr)
07783     {
07784         int32_t len=0;
07785         char* str=get_str_from_str(len);
07786         bool val=strtol(str, NULL, 10)!=0;
07787 
07788         SG_FREE(str);
07789         return val;
07790     }
07791     else
07792         return get_bool();
07793 }
07794 
07795 void CSGInterface::get_vector_from_int_vector_or_str(int32_t*& vector, int32_t& len)
07796 {
07797     if (m_legacy_strptr)
07798     {
07799         len=get_vector_len_from_str(len);
07800         if (len==0)
07801         {
07802             vector=NULL;
07803             return;
07804         }
07805 
07806         vector=SG_MALLOC(int32_t, len);
07807         char* str=NULL;
07808         int32_t slen=0;
07809         for (int32_t i=0; i<len; i++)
07810         {
07811             str=get_str_from_str(slen);
07812             vector[i]=strtol(str, NULL, 10);
07813             //SG_DEBUG("vec[%d]: %d\n", i, vector[i]);
07814             SG_FREE(str);
07815         }
07816     }
07817     else
07818         get_vector(vector, len);
07819 }
07820 
07821 void CSGInterface::get_vector_from_real_vector_or_str(
07822     float64_t*& vector, int32_t& len)
07823 {
07824     if (m_legacy_strptr)
07825     {
07826         len=get_vector_len_from_str(len);
07827         if (len==0)
07828         {
07829             vector=NULL;
07830             return;
07831         }
07832 
07833         vector=SG_MALLOC(float64_t, len);
07834         char* str=NULL;
07835         int32_t slen=0;
07836         for (int32_t i=0; i<len; i++)
07837         {
07838             str=get_str_from_str(slen);
07839             vector[i]=strtod(str, NULL);
07840             //SG_DEBUG("vec[%d]: %f\n", i, vector[i]);
07841             SG_FREE(str);
07842         }
07843     }
07844     else
07845         get_vector(vector, len);
07846 }
07847 
07848 int32_t CSGInterface::get_vector_len_from_str(int32_t expected_len)
07849 {
07850     int32_t num_args=get_num_args_in_str();
07851 
07852     if (expected_len==0 || num_args==expected_len)
07853         return num_args;
07854     else if (num_args==2*expected_len)
07855     {
07856         // special case for position_weights; a bit shaky...
07857         return expected_len;
07858     }
07859     else
07860         SG_ERROR("Expected vector length %d does not match actual length %d.\n", expected_len, num_args);
07861 
07862     return 0;
07863 }
07864 
07865 char* CSGInterface::get_str_from_str(int32_t& len)
07866 {
07867     if (!m_legacy_strptr)
07868         return NULL;
07869 
07870     int32_t i=0;
07871     while (m_legacy_strptr[i]!='\0' && !isspace(m_legacy_strptr[i]))
07872         i++;
07873 
07874     len=i;
07875     char* str=SG_MALLOC(char, len+1);
07876     for (i=0; i<len; i++)
07877         str[i]=m_legacy_strptr[i];
07878     str[len]='\0';
07879 
07880     // move legacy strptr
07881     if (m_legacy_strptr[len]=='\0')
07882         m_legacy_strptr=NULL;
07883     else
07884     {
07885         m_legacy_strptr=m_legacy_strptr+len;
07886         m_legacy_strptr=SGIO::skip_spaces(m_legacy_strptr);
07887     }
07888 
07889     return str;
07890 }
07891 
07892 int32_t CSGInterface::get_num_args_in_str()
07893 {
07894     if (!m_legacy_strptr)
07895         return 0;
07896 
07897     int32_t count=0;
07898     int32_t i=0;
07899     bool in_arg=false;
07900     while (m_legacy_strptr[i]!='\0')
07901     {
07902         if (!isspace(m_legacy_strptr[i]) && !in_arg)
07903         {
07904             count++;
07905             in_arg=true;
07906         }
07907         else if (isspace(m_legacy_strptr[i]) && in_arg)
07908             in_arg=false;
07909 
07910         i++;
07911     }
07912 
07913     return count;
07914 }
07915 
07917 // handler
07919 
07920 bool CSGInterface::handle()
07921 {
07922     int32_t len=0;
07923     bool success=false;
07924 
07925 #ifndef WIN32
07926     CSignal::set_handler();
07927 #endif
07928 
07929     char* command=NULL;
07930     command=interface->get_command(len);
07931 
07932     SG_DEBUG("command: %s, nrhs %d\n", command, m_nrhs);
07933     int32_t i=0;
07934     while (sg_methods[i].command)
07935     {
07936         if (strmatch(command, sg_methods[i].command))
07937         {
07938             SG_DEBUG("found command %s%s%s\n",
07939                     hilight.get_command_prefix(),
07940                     sg_methods[i].command,
07941                     hilight.get_command_suffix());
07942 
07943             if (!(interface->*(sg_methods[i].method))())
07944             {
07945                 if (sg_methods[i].usage_prefix)
07946                 {
07947                     SG_ERROR("Usage: %s%s%s\n\n\t%s%s%s%s%s\n",
07948                             hilight.get_command_prefix(),
07949                             sg_methods[i].command,
07950                             hilight.get_command_suffix(),
07951                             sg_methods[i].usage_prefix,
07952                             hilight.get_command_prefix(),
07953                             sg_methods[i].command,
07954                             hilight.get_command_suffix(),
07955                             sg_methods[i].usage_suffix);
07956                 }
07957                 else
07958                     SG_ERROR("Non-supported command %s%s%s.\n",
07959                             hilight.get_command_prefix(),
07960                             sg_methods[i].command,
07961                             hilight.get_command_suffix());
07962             }
07963             else
07964             {
07965                 success=true;
07966                 break;
07967             }
07968         }
07969         i++;
07970     }
07971 
07972 #ifndef WIN32
07973     CSignal::unset_handler();
07974 #endif
07975 
07976     if (!success)
07977         SG_ERROR("Unknown command %s%s%s.\n",
07978                 hilight.get_command_prefix(),
07979                 command,
07980                 hilight.get_command_suffix());
07981 
07982     SG_FREE(command);
07983     return success;
07984 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation