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