39 using namespace shogun;
44 #if defined(HAVE_CMDLINE)
45 #define USAGE(method) "", ""
46 #define USAGE_I(method, in) "", " " in ""
47 #define USAGE_O(method, out) "" out " = ", ""
48 #define USAGE_IO(method, in, out) "" out " = ", " " in ""
49 #define USAGE_COMMA " "
52 #define USAGE(method) "sg('", "')"
53 #define USAGE_I(method, in) "sg('", "', " in ")"
54 #define USAGE_O(method, out) "[" out "] <- sg('", "')"
55 #define USAGE_IO(method, in, out) "[" out "] <- sg('", "', " in ")"
56 #define USAGE_COMMA ", "
59 #define USAGE(method) "sg('", "')"
60 #define USAGE_I(method, in) "sg('", "', " in ")"
61 #define USAGE_O(method, out) "[" out "]=sg('", "')"
62 #define USAGE_IO(method, in, out) "[" out "]=sg('", "', " in ")"
63 #define USAGE_COMMA ", "
69 {
"Features", NULL, NULL, NULL },
72 (&CSGInterface::cmd_pr_loqo),
74 "'Var1', Var1, 'Var2', Var2",
"results")
78 (&CSGInterface::cmd_load_features),
84 (&CSGInterface::cmd_save_features),
89 (&CSGInterface::cmd_clean_features),
94 (&CSGInterface::cmd_get_features),
99 (&CSGInterface::cmd_add_features),
105 (&CSGInterface::cmd_add_multiple_features),
111 (&CSGInterface::cmd_add_dotfeatures),
117 (&CSGInterface::cmd_set_features),
129 (&CSGInterface::cmd_set_reference_features),
134 (&CSGInterface::cmd_del_last_features),
139 (&CSGInterface::cmd_convert),
152 (&CSGInterface::cmd_reshape),
159 (&CSGInterface::cmd_load_labels),
165 (&CSGInterface::cmd_set_labels),
171 (&CSGInterface::cmd_get_labels),
176 {
"Kernel", NULL, NULL },
179 (&CSGInterface::cmd_set_kernel_normalization),
185 (&CSGInterface::cmd_set_kernel),
190 (&CSGInterface::cmd_add_kernel),
195 (&CSGInterface::cmd_del_last_kernel),
200 (&CSGInterface::cmd_init_kernel),
205 (&CSGInterface::cmd_clean_kernel),
210 (&CSGInterface::cmd_save_kernel),
215 (&CSGInterface::cmd_get_kernel_matrix),
220 (&CSGInterface::cmd_set_WD_position_weights),
225 (&CSGInterface::cmd_get_subkernel_weights),
230 (&CSGInterface::cmd_set_subkernel_weights),
235 (&CSGInterface::cmd_set_subkernel_weights_combined),
240 (&CSGInterface::cmd_get_dotfeature_weights_combined),
245 (&CSGInterface::cmd_set_dotfeature_weights_combined),
250 (&CSGInterface::cmd_set_last_subkernel_weights),
255 (&CSGInterface::cmd_get_WD_position_weights),
260 (&CSGInterface::cmd_get_last_subkernel_weights),
265 (&CSGInterface::cmd_compute_by_subkernels),
270 (&CSGInterface::cmd_init_kernel_optimization),
275 (&CSGInterface::cmd_get_kernel_optimization),
280 (&CSGInterface::cmd_delete_kernel_optimization),
285 (&CSGInterface::cmd_use_diagonal_speedup),
290 (&CSGInterface::cmd_set_kernel_optimization_type),
295 (&CSGInterface::cmd_set_solver),
300 (&CSGInterface::cmd_set_constraint_generator),
302 "|SVMLIGHT|LIGHT|SVMLIGHT_ONECLASS|GPBTSVM|MPDSVM|GNPPSVM|GMNPSVM"
307 (&CSGInterface::cmd_set_prior_probs),
312 (&CSGInterface::cmd_set_prior_probs_from_labels),
318 {
"Distance", NULL, NULL },
321 (&CSGInterface::cmd_set_distance),
326 (&CSGInterface::cmd_init_distance),
331 (&CSGInterface::cmd_get_distance_matrix),
336 {
"Classifier", NULL, NULL },
339 (&CSGInterface::cmd_classify),
344 (&CSGInterface::cmd_classify),
349 (&CSGInterface::cmd_classify_example),
354 (&CSGInterface::cmd_classify_example),
359 (&CSGInterface::cmd_get_classifier),
364 (&CSGInterface::cmd_get_classifier),
369 (&CSGInterface::cmd_new_classifier),
371 "|SVMLIGHT|LIGHT|LIGHT_ONECLASS|SVMLIN|GPBTSVM|MPDSVM|GNPPSVM|GMNPSVM"
372 "|SUBGRADIENTSVM|WDSVMOCAS|SVMOCAS|SVMSGD|SVMBMRM|SVMPERF"
373 "|KERNELPERCEPTRON|PERCEPTRON|LIBLINEAR_LR|LIBLINEAR_L2|LDA"
374 "|LPM|LPBOOST|SUBGRADIENTLPM|KNN" USAGE_STR)
378 (&CSGInterface::cmd_new_classifier),
380 "|LIBSVM|SVMLIGHT|LIGHT|LIGHT_ONECLASS|SVMLIN|GPBTSVM|MPDSVM|GNPPSVM|GMNPSVM"
381 "|SUBGRADIENTSVM|WDSVMOCAS|SVMOCAS|SVMSGD|SVMBMRM|SVMPERF"
382 "|KERNELPERCEPTRON|PERCEPTRON|LIBLINEAR_LR|LIBLINEAR_L2|LDA"
383 "|LPM|LPBOOST|SUBGRADIENTLPM|KNN" USAGE_STR)
387 (&CSGInterface::cmd_new_classifier),
392 (&CSGInterface::cmd_new_classifier),
397 (&CSGInterface::cmd_load_classifier),
402 (&CSGInterface::cmd_save_classifier),
407 (&CSGInterface::cmd_get_num_svms),
412 (&CSGInterface::cmd_get_svm),
417 (&CSGInterface::cmd_set_svm),
422 (&CSGInterface::cmd_set_linear_classifier),
427 (&CSGInterface::cmd_get_svm_objective),
432 (&CSGInterface::cmd_compute_svm_primal_objective),
437 (&CSGInterface::cmd_compute_svm_dual_objective),
442 (&CSGInterface::cmd_compute_svm_primal_objective),
447 (&CSGInterface::cmd_compute_mkl_dual_objective),
452 (&CSGInterface::cmd_compute_relative_mkl_duality_gap),
457 (&CSGInterface::cmd_compute_absolute_mkl_duality_gap),
462 (&CSGInterface::cmd_do_auc_maximization),
467 (&CSGInterface::cmd_set_perceptron_parameters),
472 (&CSGInterface::cmd_train_classifier),
477 (&CSGInterface::cmd_train_classifier),
482 (&CSGInterface::cmd_train_classifier),
487 (&CSGInterface::cmd_train_classifier),
492 (&CSGInterface::cmd_set_svm_qpsize),
497 (&CSGInterface::cmd_set_svm_max_qpsize),
502 (&CSGInterface::cmd_set_svm_bufsize),
507 (&CSGInterface::cmd_set_svm_C),
512 (&CSGInterface::cmd_set_svm_epsilon),
517 (&CSGInterface::cmd_set_svr_tube_epsilon),
522 (&CSGInterface::cmd_set_svm_nu),
527 (&CSGInterface::cmd_set_svm_mkl_parameters),
532 (&CSGInterface::cmd_set_elasticnet_lambda),
537 (&CSGInterface::cmd_set_mkl_block_norm),
542 (&CSGInterface::cmd_set_max_train_time),
547 (&CSGInterface::cmd_set_svm_shrinking_enabled),
552 (&CSGInterface::cmd_set_svm_batch_computation_enabled),
557 (&CSGInterface::cmd_set_svm_linadd_enabled),
562 (&CSGInterface::cmd_set_svm_bias_enabled),
567 (&CSGInterface::cmd_set_mkl_interleaved_enabled),
572 (&CSGInterface::cmd_set_krr_tau),
577 {
"Preprocessors", NULL, NULL },
580 (&CSGInterface::cmd_add_preproc),
585 (&CSGInterface::cmd_del_preproc),
590 (&CSGInterface::cmd_attach_preproc),
595 (&CSGInterface::cmd_clean_preproc),
600 {
"HMM", NULL, NULL },
603 (&CSGInterface::cmd_new_hmm),
608 (&CSGInterface::cmd_load_hmm),
613 (&CSGInterface::cmd_save_hmm),
618 (&CSGInterface::cmd_get_hmm),
623 (&CSGInterface::cmd_append_hmm),
628 (&CSGInterface::cmd_append_model),
633 (&CSGInterface::cmd_set_hmm),
638 (&CSGInterface::cmd_set_hmm_as),
643 (&CSGInterface::cmd_set_chop),
648 (&CSGInterface::cmd_set_pseudo),
653 (&CSGInterface::cmd_load_definitions),
658 (&CSGInterface::cmd_hmm_classify),
663 (&CSGInterface::cmd_one_class_linear_hmm_classify),
668 (&CSGInterface::cmd_one_class_hmm_classify),
673 (&CSGInterface::cmd_one_class_hmm_classify_example),
678 (&CSGInterface::cmd_hmm_classify_example),
683 (&CSGInterface::cmd_output_hmm),
688 (&CSGInterface::cmd_output_hmm_defined),
693 (&CSGInterface::cmd_hmm_likelihood),
698 (&CSGInterface::cmd_likelihood),
703 (&CSGInterface::cmd_save_likelihood),
708 (&CSGInterface::cmd_get_viterbi_path),
713 (&CSGInterface::cmd_viterbi_train_defined),
718 (&CSGInterface::cmd_viterbi_train),
723 (&CSGInterface::cmd_baum_welch_train),
728 (&CSGInterface::cmd_baum_welch_train_defined),
733 (&CSGInterface::cmd_baum_welch_trans_train),
738 (&CSGInterface::cmd_linear_train),
743 (&CSGInterface::cmd_save_path),
748 (&CSGInterface::cmd_convergence_criteria),
753 (&CSGInterface::cmd_normalize),
758 (&CSGInterface::cmd_add_states),
763 (&CSGInterface::cmd_permutation_entropy),
768 (&CSGInterface::cmd_relative_entropy),
773 (&CSGInterface::cmd_entropy),
778 (&CSGInterface::cmd_set_feature_matrix),
783 (&CSGInterface::cmd_set_feature_matrix_sparse),
788 (&CSGInterface::cmd_new_plugin_estimator),
793 (&CSGInterface::cmd_train_estimator),
798 (&CSGInterface::cmd_plugin_estimate_classify_example),
803 (&CSGInterface::cmd_plugin_estimate_classify),
808 (&CSGInterface::cmd_set_plugin_estimate),
813 (&CSGInterface::cmd_get_plugin_estimate),
816 {
"Signals", NULL, NULL },
819 (&CSGInterface::cmd_signals_set_model),
824 (&CSGInterface::cmd_signals_set_positions),
829 (&CSGInterface::cmd_signals_set_labels),
834 (&CSGInterface::cmd_signals_set_split),
839 (&CSGInterface::cmd_signals_set_train_mask),
844 (&CSGInterface::cmd_signals_add_feature),
849 (&CSGInterface::cmd_signals_add_kernel),
854 (&CSGInterface::cmd_signals_run),
857 {
"Structure", NULL, NULL },
860 (&CSGInterface::cmd_best_path),
865 (&CSGInterface::cmd_best_path_2struct),
877 "prob" USAGE_COMMA
"path" USAGE_COMMA
"pos")
881 (&CSGInterface::cmd_set_plif_struct),
894 (&CSGInterface::cmd_get_plif_struct),
907 (&CSGInterface::cmd_precompute_subkernels),
912 (&CSGInterface::cmd_precompute_content_svms),
919 (&CSGInterface::cmd_get_lin_feat),
924 (&CSGInterface::cmd_set_lin_feat),
929 (&CSGInterface::cmd_init_dyn_prog),
934 (&CSGInterface::cmd_clean_up_dyn_prog),
939 (&CSGInterface::cmd_init_intron_list),
946 (&CSGInterface::cmd_precompute_tiling_features),
953 (&CSGInterface::cmd_long_transition_settings),
961 (&CSGInterface::cmd_set_model),
970 (&CSGInterface::cmd_best_path_trans),
977 "prob" USAGE_COMMA
"path" USAGE_COMMA
"pos")
981 (&CSGInterface::cmd_best_path_trans_deriv),
998 USAGE_COMMA
"q_deriv"
999 USAGE_COMMA
"cmd_deriv"
1000 USAGE_COMMA
"penalties_deriv"
1001 USAGE_COMMA
"my_scores"
1002 USAGE_COMMA
"my_loss")
1005 {
"POIM", NULL, NULL },
1008 (&CSGInterface::cmd_compute_POIM_WD),
1013 (&CSGInterface::cmd_get_SPEC_consensus),
1018 (&CSGInterface::cmd_get_SPEC_scoring),
1023 (&CSGInterface::cmd_get_WD_consensus),
1028 (&CSGInterface::cmd_get_WD_scoring),
1033 {
"Utility", NULL, NULL },
1036 (&CSGInterface::cmd_crc),
1041 (&CSGInterface::cmd_system),
1046 (&CSGInterface::cmd_exit),
1051 (&CSGInterface::cmd_exit),
1056 (&CSGInterface::cmd_exec),
1061 (&CSGInterface::cmd_set_output),
1066 (&CSGInterface::cmd_set_threshold),
1071 (&CSGInterface::cmd_init_random),
1076 (&CSGInterface::cmd_set_num_threads),
1081 (&CSGInterface::cmd_translate_string),
1083 "string, order, start",
"translation")
1087 (&CSGInterface::cmd_clear),
1092 (&CSGInterface::cmd_tic),
1097 (&CSGInterface::cmd_toc),
1102 (&CSGInterface::cmd_print),
1107 (&CSGInterface::cmd_echo),
1112 (&CSGInterface::cmd_loglevel),
1117 (&CSGInterface::cmd_syntax_highlight),
1122 (&CSGInterface::cmd_progress),
1127 (&CSGInterface::cmd_get_version),
1132 (&CSGInterface::cmd_help),
1137 (&CSGInterface::cmd_whos),
1142 (&CSGInterface::cmd_send_command),
1147 (&CSGInterface::cmd_run_python),
1149 "'Var1', Var1, 'Var2', Var2,..., python_function",
"results")
1153 (&CSGInterface::cmd_run_octave),
1155 "'Var1', Var1, 'Var2', Var2,..., octave_function",
"results")
1159 (&CSGInterface::cmd_run_r),
1161 "'Var1', Var1, 'Var2', Var2,..., r_function",
"results")
1167 CSGInterface::CSGInterface(
bool print_copyright)
1182 if (print_copyright)
1184 version->print_version();
1185 SG_PRINT(
"( seeding random number generator with %u (seed size %d))\n",
1188 SG_PRINT(
"initializing log-table (size=%i*%i*%i=%2.1fMB) ... ) ",
1199 CSGInterface::~CSGInterface()
1201 delete ui_classifier;
1203 delete ui_pluginestimate;
1209 delete ui_structure;
1218 void CSGInterface::reset()
1224 m_legacy_strptr=NULL;
1229 void CSGInterface::translate_arg(CSGInterface* source, CSGInterface* target)
1231 switch (source->get_argument_type())
1234 target->set_int(source->get_int());
1237 target->set_real(source->get_real());
1240 target->set_bool(source->get_bool());
1246 source->get_vector(v, len);
1247 target->set_vector(v, len);
1255 source->get_vector(v, len);
1256 target->set_vector(v, len);
1264 source->get_vector(v, len);
1265 target->set_vector(v, len);
1273 source->get_vector(v, len);
1274 target->set_vector(v, len);
1282 source->get_vector(v, len);
1283 target->set_vector(v, len);
1287 case VECTOR_SHORTREAL:
1291 source->get_vector(v, len);
1292 target->set_vector(v, len);
1300 source->get_vector(v, len);
1301 target->set_vector(v, len);
1309 source->get_vector(v, len);
1310 target->set_vector(v, len);
1318 int32_t max_str_len=0;
1320 source->get_string_list(strs, num_str, max_str_len);
1321 target->set_string_list(strs, num_str);
1328 int32_t max_str_len=0;
1330 source->get_string_list(strs, num_str,max_str_len);
1331 target->set_string_list(strs, num_str);
1338 int32_t max_str_len=0;
1340 source->get_string_list(strs, num_str,max_str_len);
1341 target->set_string_list(strs, num_str);
1348 int32_t max_str_len=0;
1350 source->get_string_list(strs, num_str, max_str_len);
1351 target->set_string_list(strs, num_str);
1358 int32_t max_str_len=0;
1360 source->get_string_list(strs, num_str, max_str_len);
1361 target->set_string_list(strs, num_str);
1369 int32_t* fmatrix=NULL;
1370 source->get_matrix(fmatrix, num_feat, num_vec);
1371 target->set_matrix(fmatrix, num_feat, num_vec);
1380 source->get_matrix(fmatrix, num_feat, num_vec);
1381 target->set_matrix(fmatrix, num_feat, num_vec);
1389 int16_t* fmatrix=NULL;
1390 source->get_matrix(fmatrix, num_feat, num_vec);
1391 target->set_matrix(fmatrix, num_feat, num_vec);
1395 case DENSE_SHORTREAL:
1400 source->get_matrix(fmatrix, num_feat, num_vec);
1401 target->set_matrix(fmatrix, num_feat, num_vec);
1409 uint16_t* fmatrix=NULL;
1410 source->get_matrix(fmatrix, num_feat, num_vec);
1411 target->set_matrix(fmatrix, num_feat, num_vec);
1498 source->get_sparse_matrix(fmatrix, num_feat, num_vec);
1500 for (int32_t i=0; i<num_vec; i++)
1501 nnz+=fmatrix[i].num_feat_entries;
1502 target->set_sparse_matrix(fmatrix, num_feat, num_vec, nnz);
1519 bool CSGInterface::cmd_load_features()
1521 if (m_nrhs<8 || !create_return_values(0))
1525 char* filename=get_str_from_str_or_direct(len);
1526 char* fclass=get_str_from_str_or_direct(len);
1527 char* type=get_str_from_str_or_direct(len);
1528 char* target=get_str_from_str_or_direct(len);
1529 int32_t size=get_int_from_int_or_str();
1530 int32_t comp_features=get_int_from_int_or_str();
1532 bool success=ui_features->load(
1533 filename, fclass, type, target, size, comp_features);
1542 bool CSGInterface::cmd_save_features()
1544 if (m_nrhs<5 || !create_return_values(0))
1548 char* filename=get_str_from_str_or_direct(len);
1549 char* type=get_str_from_str_or_direct(len);
1550 char* target=get_str_from_str_or_direct(len);
1552 bool success=ui_features->save(filename, type, target);
1560 bool CSGInterface::cmd_clean_features()
1562 if (m_nrhs<2 || !create_return_values(0))
1566 char* target=get_str_from_str_or_direct(len);
1568 bool success=ui_features->clean(target);
1574 bool CSGInterface::cmd_get_features()
1576 if (m_nrhs!=2 || !create_return_values(1))
1580 char* target=get_string(tlen);
1583 if (strmatch(target,
"TRAIN"))
1584 feat=ui_features->get_train_features();
1585 else if (strmatch(target,
"TEST"))
1586 feat=ui_features->get_test_features();
1590 SG_ERROR(
"Unknown target, neither TRAIN nor TEST.\n");
1608 set_matrix(fmatrix, num_feat, num_vec);
1615 set_matrix(fmatrix, num_feat, num_vec);
1622 set_matrix(fmatrix, num_feat, num_vec);
1629 set_matrix(fmatrix, num_feat, num_vec);
1636 set_matrix(fmatrix, num_feat, num_vec);
1643 set_matrix(fmatrix, num_feat, num_vec);
1650 set_matrix(fmatrix, num_feat, num_vec);
1667 get_num_nonzero_entries();
1671 SG_INFO(
"sparse matrix has %d feats, %d vecs and %d nnz elemements\n", num_feat, num_vec, nnz);
1673 set_sparse_matrix(fmatrix, num_feat, num_vec, nnz);
1686 int32_t max_str_len=0;
1692 set_string_list(fmatrix, num_str);
1699 set_string_list(fmatrix, num_str);
1706 set_string_list(fmatrix, num_str);
1736 bool CSGInterface::cmd_add_features()
1738 if (m_nrhs<3 || !create_return_values(0))
1741 return do_set_features(
true,
false);
1744 bool CSGInterface::cmd_add_multiple_features()
1746 if ((m_nrhs!=4 && m_nrhs<5) || !create_return_values(0))
1749 int32_t repetitions=get_int();
1753 return do_set_features(
true,
false, repetitions);
1756 bool CSGInterface::cmd_add_dotfeatures()
1758 if (m_nrhs<3 || !create_return_values(0))
1761 return do_set_features(
true,
true);
1764 bool CSGInterface::cmd_set_features()
1766 if (m_nrhs<3 || !create_return_values(0))
1769 return do_set_features(
false,
false);
1772 bool CSGInterface::do_set_features(
bool add,
bool check_dot, int32_t repetitions)
1775 char* target=get_string(tlen);
1776 if (!strmatch(target,
"TRAIN") && !strmatch(target,
"TEST"))
1779 SG_ERROR(
"Unknown target, neither TRAIN nor TEST.\n");
1786 switch (get_argument_type())
1791 get_sparse_matrix(fmatrix, num_feat, num_vec);
1802 get_matrix(fmatrix, num_feat, num_vec);
1806 set_feature_matrix(fmatrix, num_feat, num_vec);
1816 int32_t* fmatrix=NULL;
1817 get_matrix(fmatrix, num_feat, num_vec);
1821 set_feature_matrix(fmatrix, num_feat, num_vec);
1827 int16_t* fmatrix=NULL;
1828 get_matrix(fmatrix, num_feat, num_vec);
1832 set_feature_matrix(fmatrix, num_feat, num_vec);
1838 uint16_t* fmatrix=NULL;
1839 get_matrix(fmatrix, num_feat, num_vec);
1843 set_feature_matrix(fmatrix, num_feat, num_vec);
1847 case DENSE_SHORTREAL:
1850 get_matrix(fmatrix, num_feat, num_vec);
1854 set_feature_matrix(fmatrix, num_feat, num_vec);
1861 SG_ERROR(
"Please specify alphabet!\n");
1864 int32_t max_str_len=0;
1866 get_string_list(fmatrix, num_str, max_str_len);
1868 int32_t alphabet_len=0;
1869 char* alphabet_str=get_string(alphabet_len);
1872 if (strmatch(alphabet_str,
"DNABINFILE"))
1876 ASSERT(fmatrix[0].
string);
1886 SG_ERROR(
"Couldn't load DNA features from file.\n");
1893 bool convert_to_word=
false;
1894 bool convert_to_ulong=
false;
1896 if (strmatch(alphabet_str,
"DNAWORD"))
1899 convert_to_word=
true;
1901 else if (strmatch(alphabet_str,
"DNAULONG"))
1904 convert_to_ulong=
true;
1907 alphabet=
new CAlphabet(alphabet_str, alphabet_len);
1918 SG_ERROR(
"Couldnt set byte string features.\n");
1923 if (convert_to_word || convert_to_ulong)
1924 convert_to_bitembedding(feat, convert_to_word, convert_to_ulong);
1927 obtain_from_single_string(feat);
1934 SG_ERROR(
"Please specify alphabet!\n");
1937 int32_t max_str_len=0;
1939 get_string_list(fmatrix, num_str, max_str_len);
1941 int32_t alphabet_len=0;
1942 char* alphabet_str=get_string(alphabet_len);
1945 alphabet=
new CAlphabet(alphabet_str, alphabet_len);
1953 SG_ERROR(
"Couldnt set byte string features.\n");
1960 SG_ERROR(
"Wrong argument type %d.\n", get_argument_type());
1966 SG_ERROR(
"Feature type not supported by DOT Features\n");
1969 if (strmatch(target,
"TRAIN"))
1972 ui_features->set_train_features(feat);
1975 for (int32_t i=0; i<repetitions; i++)
1976 ui_features->add_train_dotfeatures((
CDotFeatures*) feat);
1980 for (int32_t i=0; i<repetitions; i++)
1981 ui_features->add_train_features(feat);
1987 ui_features->set_test_features(feat);
1990 for (int32_t i=0; i<repetitions; i++)
1991 ui_features->add_test_dotfeatures((
CDotFeatures*) feat);
1995 for (int32_t i=0; i<repetitions; i++)
1996 ui_features->add_test_features(feat);
2005 bool CSGInterface::cmd_set_reference_features()
2007 if (m_nrhs<3 || !create_return_values(0))
2011 char* target=get_str_from_str_or_direct(len);
2013 bool success=ui_features->set_reference_features(target);
2019 bool CSGInterface::cmd_del_last_features()
2021 if (m_nrhs<2 || !create_return_values(0))
2025 char* target=get_str_from_str_or_direct(len);
2026 bool success=ui_features->del_last_feature_obj(target);
2032 bool CSGInterface::cmd_convert()
2034 if (m_nrhs<5 || !create_return_values(0))
2038 char* target=get_str_from_str_or_direct(len);
2039 CFeatures* features=ui_features->get_convert_features(target);
2043 SG_ERROR(
"No \"%s\" features available.\n", target);
2046 char* from_class=get_str_from_str_or_direct(len);
2047 char* from_type=get_str_from_str_or_direct(len);
2048 char* to_class=get_str_from_str_or_direct(len);
2049 char* to_type=get_str_from_str_or_direct(len);
2052 if (strmatch(from_class,
"SIMPLE"))
2054 if (strmatch(from_type,
"REAL"))
2056 if (strmatch(to_class,
"SPARSE") &&
2057 strmatch(to_type,
"REAL"))
2059 result=ui_features->convert_simple_real_to_sparse_real(
2066 else if (strmatch(from_type,
"CHAR"))
2068 if (strmatch(to_class,
"STRING") &&
2069 strmatch(to_type,
"CHAR"))
2071 result=ui_features->convert_simple_char_to_string_char(
2074 else if (strmatch(to_class,
"SIMPLE"))
2076 if (strmatch(to_type,
"ALIGN") && m_nrhs==8)
2078 float64_t gap_cost=get_real_from_real_or_str();
2079 result=ui_features->convert_simple_char_to_simple_align(
2089 else if (strmatch(from_type,
"WORD"))
2091 if (strmatch(to_class,
"SIMPLE") &&
2092 strmatch(to_type,
"SALZBERG"))
2094 result=ui_features->convert_simple_word_to_simple_salzberg(
2105 else if (strmatch(from_class,
"SPARSE"))
2107 if (strmatch(from_type,
"REAL"))
2109 if (strmatch(to_class,
"SIMPLE") &&
2110 strmatch(to_type,
"REAL"))
2112 result=ui_features->convert_sparse_real_to_simple_real(
2122 else if (strmatch(from_class,
"STRING"))
2124 if (strmatch(from_type,
"CHAR"))
2126 if (strmatch(to_class,
"STRING"))
2135 order=get_int_from_int_or_str();
2139 start=get_int_from_int_or_str();
2143 gap=get_int_from_int_or_str();
2147 char* rev_str=get_str_from_str_or_direct(len);
2157 if (strmatch(to_type,
"BYTE"))
2159 result=ui_features->convert_string_char_to_string_generic<char,uint8_t>(
2163 else if (strmatch(to_type,
"WORD"))
2165 result=ui_features->convert_string_char_to_string_generic<char,uint16_t>(
2169 else if (strmatch(to_type,
"ULONG"))
2171 result=ui_features->convert_string_char_to_string_generic<char,uint64_t>(
2182 else if (strmatch(from_type,
"BYTE"))
2184 if (strmatch(to_class,
"STRING"))
2193 order=get_int_from_int_or_str();
2197 start=get_int_from_int_or_str();
2201 gap=get_int_from_int_or_str();
2205 char* rev_str=get_str_from_str_or_direct(len);
2215 if (strmatch(to_type,
"WORD"))
2217 result=ui_features->convert_string_char_to_string_generic<uint8_t,uint16_t>(
2221 else if (strmatch(to_type,
"ULONG"))
2223 result=ui_features->convert_string_char_to_string_generic<uint8_t,uint64_t>(
2234 else if (strmatch(from_type,
"WORD"))
2236 if (strmatch(to_class,
"SIMPLE") && strmatch(to_type,
"TOP"))
2238 result=ui_features->convert_string_word_to_simple_top(
2241 else if (strmatch(to_class,
"SPEC") && strmatch(to_type,
"WORD") && m_nrhs==7)
2243 bool use_norm=get_bool();
2251 else if (strmatch(to_class,
"SIMPLE") && strmatch(to_type,
"FK"))
2253 result=ui_features->convert_string_word_to_simple_fk(
2262 if (result && ui_features->set_convert_features(result, target))
2263 SG_INFO(
"Conversion was successful.\n");
2272 return (result!=NULL);
2275 void CSGInterface::convert_to_bitembedding(
CFeatures* &features,
bool convert_to_word,
bool convert_to_ulong)
2289 if (convert_to_word)
2291 SG_INFO(
"Converting into word-bitembedding\n");
2292 features=ui_features->convert_string_char_to_string_generic<char,uint16_t>(
2296 if (convert_to_ulong)
2298 SG_INFO(
"Converting into ulong-bitembedding\n");
2299 features=ui_features->convert_string_char_to_string_generic<char,uint64_t>(
2304 void CSGInterface::obtain_from_single_string(
CFeatures* features)
2310 char* str=get_string(len);
2313 if (strmatch(str,
"from_position_list"))
2315 obtain_from_position_list(features);
2317 else if (strmatch(str,
"slide_window"))
2319 obtain_by_sliding_window(features);
2325 bool CSGInterface::obtain_from_position_list(
CFeatures* features)
2327 int32_t winsize=get_int();
2329 int32_t* shifts=NULL;
2330 int32_t num_shift=0;
2331 get_vector(shifts, num_shift);
2337 SG_DEBUG(
"winsize: %d num_shifts: %d skip: %d\n", winsize, num_shift, skip);
2341 for (int32_t i=0; i<num_shift; i++)
2353 obtain_by_position_list(winsize, &positions, skip)>0);
2359 obtain_by_position_list(winsize, &positions, skip)>0);
2365 obtain_by_position_list(winsize, &positions, skip)>0);
2371 obtain_by_position_list(winsize, &positions, skip)>0);
2375 SG_ERROR(
"Unsupported string features type.\n");
2381 bool CSGInterface::obtain_by_sliding_window(
CFeatures* features)
2383 int32_t winsize=get_int();
2384 int32_t shift=get_int();
2406 SG_SERROR(
"Unsupported string features type.\n");
2413 bool CSGInterface::cmd_reshape()
2415 if (m_nrhs<4 || !create_return_values(0))
2419 char* target=get_str_from_str_or_direct(len);
2420 int32_t num_feat=get_int_from_int_or_str();
2421 int32_t num_vec=get_int_from_int_or_str();
2423 bool success=ui_features->reshape(target, num_feat, num_vec);
2429 bool CSGInterface::cmd_load_labels()
2431 if (m_nrhs<4 || !create_return_values(0))
2435 char* filename=get_str_from_str_or_direct(len);
2436 char* target=get_str_from_str_or_direct(len);
2438 bool success=ui_labels->load(filename, target);
2445 bool CSGInterface::cmd_set_labels()
2447 if (m_nrhs!=3 || !create_return_values(0))
2451 char* target=get_string(tlen);
2452 if (!strmatch(target,
"TRAIN") && !strmatch(target,
"TEST"))
2455 SG_ERROR(
"Unknown target, neither TRAIN nor TEST.\n");
2460 get_vector(lab, len);
2465 for (int32_t i=0; i<len; i++)
2468 SG_ERROR(
"Couldn't set label %d (of %d): %f.\n", i, len, lab[i]);
2472 if (strmatch(target,
"TRAIN"))
2473 ui_labels->set_train_labels(labels);
2474 else if (strmatch(target,
"TEST"))
2475 ui_labels->set_test_labels(labels);
2479 SG_ERROR(
"Unknown target, neither TRAIN nor TEST.\n");
2486 bool CSGInterface::cmd_get_labels()
2488 if (m_nrhs!=2 || !create_return_values(1))
2492 char* target=get_string(tlen);
2495 if (strmatch(target,
"TRAIN"))
2496 labels=ui_labels->get_train_labels();
2497 else if (strmatch(target,
"TEST"))
2498 labels=ui_labels->get_test_labels();
2502 SG_ERROR(
"Unknown target, neither TRAIN nor TEST.\n");
2512 for (int32_t i=0; i<num_labels ; i++)
2515 set_vector(lab, num_labels);
2524 bool CSGInterface::cmd_set_kernel_normalization()
2526 if (m_nrhs<2 || !create_return_values(0))
2530 char* normalization=get_string(len);
2540 bool success=ui_kernel->set_normalization(normalization, c, r);
2546 bool CSGInterface::cmd_set_kernel()
2548 if (m_nrhs<2 || !create_return_values(0))
2551 SG_DEBUG(
"SGInterface: set_kernel\n");
2552 CKernel* kernel=create_kernel();
2553 return ui_kernel->set_kernel(kernel);
2556 bool CSGInterface::cmd_add_kernel()
2558 if (m_nrhs<3 || !create_return_values(0))
2561 float64_t weight=get_real_from_real_or_str();
2564 CKernel* kernel=create_kernel();
2566 SG_DEBUG(
"SGInterface: add_kernel\n");
2567 return ui_kernel->add_kernel(kernel, weight);
2570 bool CSGInterface::cmd_del_last_kernel()
2572 if (m_nrhs<1 || !create_return_values(0))
2575 return ui_kernel->del_last_kernel();
2578 CKernel* CSGInterface::create_kernel()
2582 char* type=get_str_from_str_or_direct(len);
2584 SG_DEBUG(
"set_kernel with type: %s\n", type);
2586 if (strmatch(type,
"COMBINED"))
2591 int32_t size=get_int_from_int_or_str();
2592 bool append_subkernel_weights=
false;
2594 append_subkernel_weights=get_bool_from_bool_or_str();
2596 kernel=ui_kernel->create_combined(size, append_subkernel_weights);
2598 else if (strmatch(type,
"DISTANCE"))
2603 int32_t size=get_int_from_int_or_str();
2606 width=get_real_from_real_or_str();
2608 kernel=ui_kernel->create_distance(size, width);
2610 else if (strmatch(type,
"WAVELET"))
2616 char* dtype=get_str_from_str_or_direct(len);
2617 if (strmatch(dtype,
"REAL"))
2619 int32_t size=get_int_from_int_or_str();
2625 Wdilation=get_real_from_real_or_str();
2628 Wtranslation=get_real_from_real_or_str();
2631 kernel=ui_kernel->create_sigmoid(size, Wdilation, Wtranslation);
2636 else if (strmatch(type,
"LINEAR"))
2643 char* dtype=get_str_from_str_or_direct(len);
2644 int32_t size=get_int_from_int_or_str();
2647 scale=get_real_from_real_or_str();
2649 if (strmatch(dtype,
"BYTE"))
2650 kernel=ui_kernel->create_linearbyte(size, scale);
2651 else if (strmatch(dtype,
"WORD"))
2652 kernel=ui_kernel->create_linearword(size, scale);
2653 else if (strmatch(dtype,
"CHAR"))
2654 kernel=ui_kernel->create_linearstring(size, scale);
2655 else if (strmatch(dtype,
"REAL"))
2656 kernel=ui_kernel->create_linear(size, scale);
2657 else if (strmatch(dtype,
"SPARSEREAL"))
2658 kernel=ui_kernel->create_sparselinear(size, scale);
2662 else if (strmatch(type,
"HISTOGRAM"))
2667 char* dtype=get_str_from_str_or_direct(len);
2668 if (strmatch(dtype,
"WORD"))
2670 int32_t size=get_int_from_int_or_str();
2671 kernel=ui_kernel->create_histogramword(size);
2676 else if (strmatch(type,
"SALZBERG"))
2681 char* dtype=get_str_from_str_or_direct(len);
2682 if (strmatch(dtype,
"WORD"))
2684 int32_t size=get_int_from_int_or_str();
2685 kernel=ui_kernel->create_salzbergword(size);
2690 else if (strmatch(type,
"POLYMATCH"))
2695 char* dtype=get_str_from_str_or_direct(len);
2696 int32_t size=get_int_from_int_or_str();
2698 bool inhomogene=
false;
2699 bool normalize=
true;
2703 degree=get_int_from_int_or_str();
2706 inhomogene=get_bool_from_bool_or_str();
2708 normalize=get_bool_from_bool_or_str();
2712 if (strmatch(dtype,
"CHAR"))
2714 kernel=ui_kernel->create_polymatchstring(
2715 size, degree, inhomogene, normalize);
2717 else if (strmatch(dtype,
"WORD"))
2719 kernel=ui_kernel->create_polymatchwordstring(
2720 size, degree, inhomogene, normalize);
2725 else if (strmatch(type,
"MATCH"))
2730 char* dtype=get_str_from_str_or_direct(len);
2731 if (strmatch(dtype,
"WORD"))
2733 int32_t size=get_int_from_int_or_str();
2735 bool normalize=
true;
2738 d=get_int_from_int_or_str();
2740 normalize=get_bool_from_bool_or_str();
2742 kernel=ui_kernel->create_matchwordstring(size, d, normalize);
2747 else if (strmatch(type,
"WEIGHTEDCOMMSTRING") || strmatch(type,
"COMMSTRING"))
2749 char* dtype=get_str_from_str_or_direct(len);
2750 int32_t size=get_int_from_int_or_str();
2751 bool use_sign=
false;
2752 char* norm_str=NULL;
2756 use_sign=get_bool_from_bool_or_str();
2759 norm_str=get_str_from_str_or_direct(len);
2762 if (strmatch(dtype,
"WORD"))
2764 if (strmatch(type,
"WEIGHTEDCOMMSTRING"))
2766 kernel=ui_kernel->create_commstring(
2769 else if (strmatch(type,
"COMMSTRING"))
2771 kernel=ui_kernel->create_commstring(
2775 else if (strmatch(dtype,
"ULONG"))
2777 kernel=ui_kernel->create_commstring(
2784 else if (strmatch(type,
"CHI2"))
2789 char* dtype=get_str_from_str_or_direct(len);
2790 if (strmatch(dtype,
"REAL"))
2792 int32_t size=get_int_from_int_or_str();
2796 width=get_real_from_real_or_str();
2798 kernel=ui_kernel->create_chi2(size, width);
2803 else if (strmatch(type,
"FIXEDDEGREE"))
2808 char* dtype=get_str_from_str_or_direct(len);
2809 if (strmatch(dtype,
"CHAR"))
2811 int32_t size=get_int_from_int_or_str();
2814 d=get_int_from_int_or_str();
2816 kernel=ui_kernel->create_fixeddegreestring(size, d);
2821 else if (strmatch(type,
"LOCALALIGNMENT"))
2826 char* dtype=get_str_from_str_or_direct(len);
2827 if (strmatch(dtype,
"CHAR"))
2829 int32_t size=get_int_from_int_or_str();
2831 kernel=ui_kernel->create_localalignmentstring(size);
2836 else if (strmatch(type,
"OLIGO"))
2841 char* dtype=get_str_from_str_or_direct(len);
2842 if (strmatch(dtype,
"CHAR"))
2844 int32_t size=get_int_from_int_or_str();
2845 int32_t k=get_int_from_int_or_str();
2846 float64_t w=get_real_from_real_or_str();
2848 kernel=ui_kernel->create_oligo(size, k, w);
2853 else if (strmatch(type,
"WEIGHTEDDEGREEPOS2") ||
2854 strmatch(type,
"WEIGHTEDDEGREEPOS2_NONORM"))
2859 char* dtype=get_str_from_str_or_direct(len);
2860 if (strmatch(dtype,
"CHAR") || strmatch(dtype,
"STRING"))
2862 int32_t size=get_int_from_int_or_str();
2863 int32_t order=get_int_from_int_or_str();
2864 int32_t max_mismatch=get_int_from_int_or_str();
2865 int32_t length=get_int_from_int_or_str();
2866 int32_t* shifts=NULL;
2868 get_vector_from_int_vector_or_str(shifts, l);
2872 bool use_normalization=
true;
2873 if (strmatch(type,
"WEIGHTEDDEGREEPOS2_NONORM"))
2874 use_normalization=
false;
2876 kernel=ui_kernel->create_weighteddegreepositionstring2(
2877 size, order, max_mismatch, shifts, length,
2885 else if (strmatch(type,
"WEIGHTEDDEGREEPOS3"))
2890 char* dtype=get_str_from_str_or_direct(len);
2891 if (strmatch(dtype,
"CHAR") || strmatch(dtype,
"STRING"))
2893 int32_t size=get_int_from_int_or_str();
2894 int32_t order=get_int_from_int_or_str();
2895 int32_t max_mismatch=get_int_from_int_or_str();
2896 int32_t length=get_int_from_int_or_str();
2897 int32_t mkl_stepsize=get_int_from_int_or_str();
2898 int32_t* shifts=NULL;
2900 get_vector_from_int_vector_or_str(shifts, l);
2904 if (m_nrhs>9+length)
2906 get_vector_from_real_vector_or_str(
2907 position_weights, length);
2910 kernel=ui_kernel->create_weighteddegreepositionstring3(
2911 size, order, max_mismatch, shifts, length,
2912 mkl_stepsize, position_weights);
2920 else if (strmatch(type,
"WEIGHTEDDEGREEPOS"))
2925 char* dtype=get_str_from_str_or_direct(len);
2926 if (strmatch(dtype,
"CHAR") || strmatch(dtype,
"STRING"))
2928 int32_t size=get_int_from_int_or_str();
2930 int32_t max_mismatch=0;
2937 order=get_int_from_int_or_str();
2941 max_mismatch=get_int_from_int_or_str();
2945 length=get_int_from_int_or_str();
2949 center=get_int_from_int_or_str();
2952 step=get_real_from_real_or_str();
2958 kernel=ui_kernel->create_weighteddegreepositionstring(
2959 size, order, max_mismatch, length, center, step);
2964 else if (strmatch(type,
"WEIGHTEDDEGREE"))
2969 char* dtype=get_str_from_str_or_direct(len);
2970 if (strmatch(dtype,
"CHAR") || strmatch(dtype,
"STRING"))
2972 int32_t size=get_int_from_int_or_str();
2974 int32_t max_mismatch=0;
2975 bool use_normalization=
true;
2976 int32_t mkl_stepsize=1;
2977 bool block_computation=
true;
2978 int32_t single_degree=-1;
2982 order=get_int_from_int_or_str();
2986 max_mismatch=get_int_from_int_or_str();
2990 use_normalization=get_bool_from_bool_or_str();
2994 mkl_stepsize=get_int_from_int_or_str();
2998 block_computation=get_int_from_int_or_str();
3001 single_degree=get_int_from_int_or_str();
3008 kernel=ui_kernel->create_weighteddegreestring(
3009 size, order, max_mismatch, use_normalization,
3010 mkl_stepsize, block_computation, single_degree);
3015 else if (strmatch(type,
"WEIGHTEDDEGREERBF"))
3020 char* dtype=get_str_from_str_or_direct(len);
3021 int32_t size=get_int_from_int_or_str();
3022 int32_t nof_properties=get_int_from_int_or_str();
3027 degree=get_int_from_int_or_str();
3030 width=get_real_from_real_or_str();
3036 kernel=ui_kernel->create_weighteddegreerbf(size, degree, nof_properties, width);
3041 else if (strmatch(type,
"SPECTRUMMISMATCHRBF"))
3046 char* dtype=get_str_from_str_or_direct(len);
3047 if (strmatch(dtype,
"CHAR") || strmatch(dtype,
"STRING"))
3049 int32_t size=get_int_from_int_or_str();
3050 int32_t degree=get_int_from_int_or_str();
3051 int32_t max_mismatch=get_int_from_int_or_str();
3052 float64_t width=get_real_from_real_or_str();
3060 get_matrix(helper_matrix, N, M);
3062 if (N == 128 && M == 128)
3065 memcpy(AA_matrix, helper_matrix, N*M*
sizeof(
float64_t)) ;
3066 kernel=ui_kernel->create_spectrummismatchrbf(size, AA_matrix, max_mismatch, degree, width);
3070 SG_ERROR(
"Matrix size %d %d\n", N, M);
3077 else if (strmatch(type,
"SLIK") || strmatch(type,
"LIK"))
3082 char* dtype=get_str_from_str_or_direct(len);
3083 if (strmatch(dtype,
"CHAR"))
3085 int32_t size=get_int_from_int_or_str();
3087 int32_t inner_degree=3;
3088 int32_t outer_degree=1;
3092 length=get_int_from_int_or_str();
3096 inner_degree=get_int_from_int_or_str();
3099 outer_degree=get_int_from_int_or_str();
3103 if (strmatch(type,
"SLIK"))
3105 kernel=ui_kernel->create_localityimprovedstring(
3106 size, length, inner_degree, outer_degree,
3111 kernel=ui_kernel->create_localityimprovedstring(
3112 size, length, inner_degree, outer_degree,
3119 else if (strmatch(type,
"POLY"))
3124 char* dtype=get_str_from_str_or_direct(len);
3125 int32_t size=get_int_from_int_or_str();
3127 bool inhomogene=
false;
3128 bool normalize=
true;
3132 degree=get_int_from_int_or_str();
3136 inhomogene=get_bool_from_bool_or_str();
3139 normalize=get_bool_from_bool_or_str();
3143 if (strmatch(dtype,
"REAL"))
3145 kernel=ui_kernel->create_poly(
3146 size, degree, inhomogene, normalize);
3148 else if (strmatch(dtype,
"SPARSEREAL"))
3150 kernel=ui_kernel->create_sparsepoly(
3151 size, degree, inhomogene, normalize);
3156 else if (strmatch(type,
"SIGMOID"))
3161 char* dtype=get_str_from_str_or_direct(len);
3162 if (strmatch(dtype,
"REAL"))
3164 int32_t size=get_int_from_int_or_str();
3170 gamma=get_real_from_real_or_str();
3173 coef0=get_real_from_real_or_str();
3176 kernel=ui_kernel->create_sigmoid(size, gamma, coef0);
3181 else if (strmatch(type,
"GAUSSIAN"))
3186 char* dtype=get_str_from_str_or_direct(len);
3187 int32_t size=get_int_from_int_or_str();
3190 width=get_real_from_real_or_str();
3192 if (strmatch(dtype,
"REAL"))
3193 kernel=ui_kernel->create_gaussian(size, width);
3194 else if (strmatch(dtype,
"SPARSEREAL"))
3195 kernel=ui_kernel->create_sparsegaussian(size, width);
3199 else if (strmatch(type,
"GAUSSIANSHIFT"))
3204 char* dtype=get_str_from_str_or_direct(len);
3205 if (strmatch(dtype,
"REAL"))
3207 int32_t size=get_int_from_int_or_str();
3208 float64_t width=get_real_from_real_or_str();
3209 int32_t max_shift=get_int_from_int_or_str();
3210 int32_t shift_step=get_int_from_int_or_str();
3212 kernel=ui_kernel->create_gaussianshift(
3213 size, width, max_shift, shift_step);
3218 else if (strmatch(type,
"CUSTOM"))
3220 if (m_nrhs!=4 || !create_return_values(0))
3226 get_matrix(kmatrix, num_feat, num_vec);
3229 char* ktype=get_string(tlen);
3231 if (!strmatch(ktype,
"DIAG") &&
3232 !strmatch(ktype,
"FULL") &&
3233 !strmatch(ktype,
"FULL2DIAG"))
3236 SG_ERROR(
"Undefined type, not DIAG, FULL or FULL2DIAG.\n");
3239 bool source_is_diag=
false;
3240 bool dest_is_diag=
false;
3242 if (strmatch(ktype,
"FULL2DIAG"))
3244 else if (strmatch(ktype,
"DIAG"))
3246 source_is_diag=
true;
3250 kernel=ui_kernel->create_custom(kmatrix, num_feat, num_vec,
3251 source_is_diag, dest_is_diag);
3253 else if (strmatch(type,
"CONST"))
3258 char* dtype=get_str_from_str_or_direct(len);
3259 if (strmatch(dtype,
"REAL"))
3261 int32_t size=get_int_from_int_or_str();
3264 c=get_real_from_real_or_str();
3266 kernel=ui_kernel->create_const(size, c);
3271 else if (strmatch(type,
"DIAG"))
3276 char* dtype=get_str_from_str_or_direct(len);
3277 if (strmatch(dtype,
"REAL"))
3279 int32_t size=get_int_from_int_or_str();
3282 diag=get_real_from_real_or_str();
3284 kernel=ui_kernel->create_diag(size, diag);
3290 else if (strmatch(type,
"TPPK"))
3295 char* dtype=get_str_from_str_or_direct(len);
3296 if (strmatch(dtype,
"INT"))
3298 int32_t size=get_int_from_int_or_str();
3302 get_matrix(km, rows, cols);
3303 kernel=ui_kernel->create_tppk(size, km, rows, cols);
3312 SG_DEBUG(
"created kernel: %p\n", kernel);
3325 int32_t from_order=0;
3326 bool normalize=
true;
3328 int32_t feature_class_len=0;
3329 char* feature_class_str=get_string(feature_class_len);
3330 ASSERT(feature_class_str);
3332 if (strmatch(feature_class_str,
"WD"))
3335 SG_ERROR(
"Please specify alphabet, WD, order, from_order\n");
3339 from_order=get_int();
3342 else if (strmatch(feature_class_str,
"WSPEC"))
3345 SG_ERROR(
"Please specify alphabet, order, WSPEC, start, normalize\n");
3350 normalize=get_bool();
3373 int32_t feature_class_len=0;
3375 char* feature_class_str=get_string(feature_class_len);
3376 ASSERT(feature_class_str);
3377 if (strmatch(feature_class_str,
"POLY"))
3383 normalize = get_bool();
3388 SG_ERROR(
"Unknown feature class: %s\n", feature_class_str);
3396 bool CSGInterface::cmd_init_kernel()
3402 bool CSGInterface::cmd_clean_kernel()
3404 if (m_nrhs<1 || !create_return_values(0))
3407 return ui_kernel->clean_kernel();
3410 bool CSGInterface::cmd_save_kernel()
3412 if (m_nrhs<2 || !create_return_values(0))
3416 char* filename=get_str_from_str_or_direct(len);
3418 bool success=ui_kernel->save_kernel(filename);
3424 bool CSGInterface::cmd_get_kernel_matrix()
3426 if (m_nrhs>2 || !create_return_values(1))
3433 target=get_string(len);
3434 bool success=ui_kernel->init_kernel(target);
3438 CKernel* kernel=ui_kernel->get_kernel();
3440 SG_ERROR(
"No kernel defined or not initialized.\n");
3451 bool CSGInterface::cmd_set_WD_position_weights()
3453 if (m_nrhs<2 || m_nrhs>3 || !create_return_values(0))
3456 CKernel* kernel=ui_kernel->get_kernel();
3475 get_matrix(weights, dim, len);
3482 if (dim!=1 && len>0)
3483 SG_ERROR(
"Dimension mismatch (should be 1 x seq_length or 0x0\n");
3485 ui_kernel->init_kernel(
"TRAIN");
3498 target=get_string(tlen);
3502 SG_ERROR(
"Couldn't find second argument to method.\n");
3505 if (!strmatch(target,
"TRAIN") && !strmatch(target,
"TEST"))
3508 SG_ERROR(
"Second argument none of TRAIN or TEST.\n");
3511 if (strmatch(target,
"TEST"))
3515 if (dim!=1 && len>0)
3518 SG_ERROR(
"Dimension mismatch (should be 1 x seq_length or 0x0\n");
3521 if (dim==0 && len==0)
3523 if (create_return_values(3))
3535 if (create_return_values(3))
3544 ui_kernel->init_kernel(
"TRAIN");
3556 bool CSGInterface::cmd_get_subkernel_weights()
3558 if (m_nrhs!=1 || !create_return_values(1))
3561 CKernel *kernel=ui_kernel->get_kernel();
3570 int32_t num_weights=-1;
3571 weights=((
CCombinedKernel *) kernel)->get_subkernel_weights(num_weights);
3574 set_matrix(weights, 1, num_weights);
3584 get_degree_weights(degree, length);
3589 get_degree_weights(degree, length);
3592 SG_ERROR(
"Setting subkernel weights not supported on this kernel.\n");
3597 set_matrix(weights, degree, length);
3601 bool CSGInterface::cmd_set_subkernel_weights()
3603 if (m_nrhs!=2 || !create_return_values(0))
3606 CKernel* kernel=ui_kernel->get_kernel();
3614 get_matrix(weights, dim, len);
3622 if (dim!=degree || len<1)
3623 SG_ERROR(
"WD: Dimension mismatch (should be (seq_length | 1) x degree) got (%d x %d)\n", len, degree);
3635 if (dim!=degree || len<1)
3636 SG_ERROR(
"WDPos: Dimension mismatch (should be (seq_length | 1) x degree) got (%d x %d)\n", len, degree);
3646 if (dim!=1 || len!=num_subkernels)
3647 SG_ERROR(
"All: Dimension mismatch (should be 1 x num_subkernels)\n");
3656 bool CSGInterface::cmd_set_subkernel_weights_combined()
3658 if (m_nrhs!=3 || !create_return_values(0))
3661 CKernel* kernel=ui_kernel->get_kernel();
3665 SG_ERROR(
"Only works for combined kernels.\n");
3671 get_matrix(weights, dim, len);
3673 int32_t idx=get_int();
3674 SG_DEBUG(
"using kernel_idx=%i\n", idx);
3678 SG_ERROR(
"No subkernel at idx %d.\n", idx);
3686 if (dim!=degree || len<1)
3687 SG_ERROR(
"Dimension mismatch (should be de(seq_length | 1) x degree)\n");
3699 if (dim!=degree || len<1)
3700 SG_ERROR(
"Dimension mismatch (should be de(seq_length | 1) x degree)\n");
3710 if (dim!=1 || len!=num_subkernels)
3711 SG_ERROR(
"Dimension mismatch (should be 1 x num_subkernels)\n");
3720 bool CSGInterface::cmd_get_dotfeature_weights_combined()
3722 if (m_nrhs!=2 || !create_return_values(1))
3726 char* target=get_string(tlen);
3729 if (strmatch(target,
"TRAIN"))
3730 features=ui_features->get_train_features();
3731 else if (strmatch(target,
"TEST"))
3732 features=ui_features->get_test_features();
3736 SG_ERROR(
"Unknown target, neither TRAIN nor TEST.\n");
3743 SG_ERROR(
"Only works for combined dot features.\n");
3748 set_vector(weights, len);
3754 bool CSGInterface::cmd_set_dotfeature_weights_combined()
3756 if (m_nrhs!=3 || !create_return_values(0))
3760 char* target=get_string(tlen);
3763 if (strmatch(target,
"TRAIN"))
3764 features=ui_features->get_train_features();
3765 else if (strmatch(target,
"TEST"))
3766 features=ui_features->get_test_features();
3770 SG_ERROR(
"Unknown target, neither TRAIN nor TEST.\n");
3777 SG_ERROR(
"Only works for combined dot features.\n");
3782 get_matrix(weights, dim, len);
3789 bool CSGInterface::cmd_set_last_subkernel_weights()
3791 if (m_nrhs!=2 || !create_return_values(0))
3794 CKernel* kernel=ui_kernel->get_kernel();
3798 SG_ERROR(
"Only works for Combined kernels.\n");
3808 get_matrix(weights, dim, len);
3815 SG_ERROR(
"Dimension mismatch (should be de(seq_length | 1) x degree)\n");
3827 SG_ERROR(
"Dimension mismatch (should be de(seq_length | 1) x degree)\n");
3837 if (dim!=1 || len!=num_subkernels)
3838 SG_ERROR(
"Dimension mismatch (should be 1 x num_subkernels)\n");
3847 bool CSGInterface::cmd_get_WD_position_weights()
3849 if (m_nrhs!=1 || !create_return_values(1))
3852 CKernel* kernel=ui_kernel->get_kernel();
3860 SG_ERROR(
"Couldn't find last kernel.\n");
3864 SG_ERROR(
"Wrong subkernel type.\n");
3875 if (position_weights==NULL)
3876 set_vector(position_weights, 0);
3878 set_vector(position_weights, len);
3883 bool CSGInterface::cmd_get_last_subkernel_weights()
3885 if (m_nrhs!=1 || !create_return_values(1))
3888 CKernel* kernel=ui_kernel->get_kernel();
3893 SG_ERROR(
"Only works for Combined kernels.\n");
3897 SG_ERROR(
"Couldn't find last kernel.\n");
3904 int32_t num_weights=0;
3908 set_vector(weights, num_weights);
3915 get_degree_weights(degree, len);
3918 get_degree_weights(degree, len);
3920 SG_ERROR(
"Only works for Weighted Degree (Position) kernels.\n");
3925 set_matrix(weights, degree, len);
3930 bool CSGInterface::cmd_compute_by_subkernels()
3932 if (m_nrhs!=1 || !create_return_values(1))
3935 CKernel* kernel=ui_kernel->get_kernel();
3952 SG_ERROR(
"Kernel optimization not initialized.\n");
3960 SG_ERROR(
"Kernel optimization not initialized.\n");
3963 SG_ERROR(
"Only works for Weighted Degree (Position) kernels.\n");
3968 int32_t num_feat=degree*len;
3969 int32_t num=num_feat*num_vec;
3972 for (int32_t i=0; i<num; i++)
3978 for (int32_t i=0; i<num_vec; i++)
3985 for (int32_t i=0; i<num_vec; i++)
3989 set_matrix(result, num_feat, num_vec);
3995 bool CSGInterface::cmd_init_kernel_optimization()
3997 if (m_nrhs<1 || !create_return_values(0))
4000 return ui_kernel->init_kernel_optimization();
4003 bool CSGInterface::cmd_get_kernel_optimization()
4005 if (m_nrhs<1 || !create_return_values(1))
4008 CKernel* kernel=ui_kernel->get_kernel();
4019 int32_t max_order=get_int();
4020 if ((max_order<1) || (max_order>12))
4022 SG_WARNING(
"max_order out of range 1..12 (%d). setting to 1\n", max_order);
4027 CSVM* svm=(
CSVM*) ui_classifier->get_classifier();
4032 int32_t* sv_idx=
SG_MALLOC(int32_t, num_suppvec);
4037 for (int32_t i=0; i<num_suppvec; i++)
4044 num_sym, NULL, num_suppvec, sv_idx, sv_weight);
4048 set_matrix(position_weights, num_sym, num_feat);
4062 set_vector(weights, len);
4071 set_vector(weights, len);
4081 bool CSGInterface::cmd_delete_kernel_optimization()
4083 if (m_nrhs<1 || !create_return_values(0))
4086 return ui_kernel->delete_kernel_optimization();
4089 bool CSGInterface::cmd_use_diagonal_speedup()
4091 if (m_nrhs<2 || !create_return_values(0))
4094 bool speedup=get_bool();
4096 CKernel* kernel=ui_kernel->get_kernel();
4102 SG_DEBUG(
"Identified combined kernel.\n");
4105 SG_ERROR(
"No last kernel defined.\n");
4109 SG_ERROR(
"Currently only commwordstring kernel supports diagonal speedup\n");
4113 SG_INFO(
"Diagonal speedup %s.\n", speedup ?
"enabled" :
"disabled");
4118 bool CSGInterface::cmd_set_kernel_optimization_type()
4120 if (m_nrhs<2 || !create_return_values(0))
4124 char* opt_type=get_str_from_str_or_direct(len);
4126 bool success=ui_kernel->set_optimization_type(opt_type);
4132 bool CSGInterface::cmd_set_solver()
4134 if (m_nrhs<2 || !create_return_values(0))
4138 char* solver=get_str_from_str_or_direct(len);
4140 bool success=ui_classifier->set_solver(solver);
4146 bool CSGInterface::cmd_set_constraint_generator()
4148 if (m_nrhs<2 || !create_return_values(0))
4152 char* cg=get_str_from_str_or_direct(len);
4154 bool success=ui_classifier->set_constraint_generator(cg);
4160 bool CSGInterface::cmd_set_prior_probs()
4162 if (m_nrhs<3 || !create_return_values(0))
4168 SG_ERROR(
"SalzbergWordStringKernel required for setting prior probs!\n");
4170 float64_t pos_probs=get_real_from_real_or_str();
4171 float64_t neg_probs=get_real_from_real_or_str();
4178 bool CSGInterface::cmd_set_prior_probs_from_labels()
4180 if (m_nrhs<2 || !create_return_values(0))
4186 SG_ERROR(
"SalzbergWordStringKernel required for setting prior probs!\n");
4190 get_vector(lab, len);
4193 for (int32_t i=0; i<len; i++)
4196 SG_ERROR(
"Couldn't set label %d (of %d): %f.\n", i, len, lab[i]);
4211 bool CSGInterface::cmd_set_distance()
4213 if (m_nrhs<3 || !create_return_values(0))
4218 char* type=get_str_from_str_or_direct(len);
4219 char* dtype=get_str_from_str_or_direct(len);
4221 if (strmatch(type,
"MINKOWSKI") && m_nrhs==4)
4223 float64_t k=get_real_from_real_or_str();
4224 distance=ui_distance->create_minkowski(k);
4226 else if (strmatch(type,
"MANHATTAN"))
4228 if (strmatch(dtype,
"REAL"))
4229 distance=ui_distance->create_generic(
D_MANHATTAN);
4230 else if (strmatch(dtype,
"WORD"))
4233 else if (strmatch(type,
"HAMMING") && strmatch(dtype,
"WORD"))
4235 bool use_sign=
false;
4237 use_sign=get_bool_from_bool_or_str();
4239 distance=ui_distance->create_hammingword(use_sign);
4241 else if (strmatch(type,
"CANBERRA"))
4243 if (strmatch(dtype,
"REAL"))
4244 distance=ui_distance->create_generic(
D_CANBERRA);
4245 else if (strmatch(dtype,
"WORD"))
4248 else if (strmatch(type,
"CHEBYSHEW") && strmatch(dtype,
"REAL"))
4250 distance=ui_distance->create_generic(
D_CHEBYSHEW);
4252 else if (strmatch(type,
"GEODESIC") && strmatch(dtype,
"REAL"))
4254 distance=ui_distance->create_generic(
D_GEODESIC);
4256 else if (strmatch(type,
"JENSEN") && strmatch(dtype,
"REAL"))
4258 distance=ui_distance->create_generic(
D_JENSEN);
4260 else if (strmatch(type,
"CHISQUARE") && strmatch(dtype,
"REAL"))
4262 distance=ui_distance->create_generic(
D_CHISQUARE);
4264 else if (strmatch(type,
"TANIMOTO") && strmatch(dtype,
"REAL"))
4266 distance=ui_distance->create_generic(
D_TANIMOTO);
4268 else if (strmatch(type,
"COSINE") && strmatch(dtype,
"REAL"))
4270 distance=ui_distance->create_generic(
D_COSINE);
4272 else if (strmatch(type,
"BRAYCURTIS") && strmatch(dtype,
"REAL"))
4276 else if (strmatch(type,
"EUCLIDIAN"))
4278 if (strmatch(dtype,
"REAL"))
4279 distance=ui_distance->create_generic(
D_EUCLIDIAN);
4280 else if (strmatch(dtype,
"SPARSEREAL"))
4288 return ui_distance->set_distance(distance);
4291 bool CSGInterface::cmd_init_distance()
4297 bool CSGInterface::cmd_get_distance_matrix()
4299 if (m_nrhs!=2 || !create_return_values(1))
4303 char* target=get_string(len);
4305 bool success=ui_distance->init_distance(target);
4309 CDistance* distance=ui_distance->get_distance();
4311 SG_ERROR(
"No distance defined or not initialized.\n");
4313 int32_t num_vec_lhs=0;
4314 int32_t num_vec_rhs=0;
4318 set_matrix(dmatrix, num_vec_lhs, num_vec_rhs);
4328 bool CSGInterface::cmd_get_SPEC_consensus()
4330 if (m_nrhs!=1 || !create_return_values(1))
4333 CKernel* kernel=ui_kernel->get_kernel();
4337 SG_ERROR(
"Only works for CommWordString kernels.\n");
4339 CSVM* svm=(
CSVM*) ui_classifier->get_classifier();
4342 int32_t* sv_idx=
SG_MALLOC(int32_t, num_suppvec);
4346 for (int32_t i=0; i<num_suppvec; i++)
4353 num_feat, num_suppvec, sv_idx, sv_weight);
4357 set_vector(consensus, num_feat);
4363 bool CSGInterface::cmd_get_SPEC_scoring()
4365 if (m_nrhs!=2 || !create_return_values(1))
4368 int32_t max_order=get_int();
4369 CKernel* kernel=ui_kernel->get_kernel();
4375 SG_ERROR(
"Only works for (Weighted) CommWordString kernels.\n");
4377 CSVM* svm=(
CSVM*) ui_classifier->get_classifier();
4380 int32_t* sv_idx=
SG_MALLOC(int32_t, num_suppvec);
4385 for (int32_t i=0; i<num_suppvec; i++)
4391 if ((max_order<1) || (max_order>8))
4393 SG_WARNING(
"max_order out of range 1..8 (%d). setting to 1\n", max_order);
4400 max_order, num_feat, num_sym, NULL,
4401 num_suppvec, sv_idx, sv_weight);
4404 max_order, num_feat, num_sym, NULL,
4405 num_suppvec, sv_idx, sv_weight);
4409 set_matrix(position_weights, num_sym, num_feat);
4415 bool CSGInterface::cmd_get_WD_consensus()
4417 if (m_nrhs!=1 || !create_return_values(1))
4420 CKernel* kernel=ui_kernel->get_kernel();
4424 SG_ERROR(
"Only works for Weighted Degree Position kernels.\n");
4426 CSVM* svm=(
CSVM*) ui_classifier->get_classifier();
4429 int32_t* sv_idx=
SG_MALLOC(int32_t, num_suppvec);
4433 for (int32_t i=0; i<num_suppvec; i++)
4440 num_feat, num_suppvec, sv_idx, sv_weight);
4444 set_vector(consensus, num_feat);
4450 bool CSGInterface::cmd_compute_POIM_WD()
4452 if (m_nrhs!=3 || !create_return_values(1))
4455 int32_t max_order=get_int();
4457 int32_t num_dfeat=0;
4459 get_matrix(distribution, num_dfeat, num_dvec);
4464 CKernel* kernel=ui_kernel->get_kernel();
4468 SG_ERROR(
"Only works for Weighted Degree Position kernels.\n");
4478 if (num_dvec!=seqlen || num_dfeat!=num_sym)
4480 SG_ERROR(
"distribution should have (seqlen x num_sym) elements"
4481 "(seqlen: %d vs. %d symbols: %d vs. %d)\n", seqlen,
4482 num_dvec, num_sym, num_dfeat);
4485 CSVM* svm=(
CSVM*) ui_classifier->get_classifier();
4488 int32_t* sv_idx=
SG_MALLOC(int32_t, num_suppvec);
4491 for (int32_t i=0; i<num_suppvec; i++)
4507 max_order, seqlen, num_sym, NULL,
4508 num_suppvec, sv_idx, sv_weight, distribution);
4512 set_matrix(position_weights, num_sym, seqlen);
4518 bool CSGInterface::cmd_get_WD_scoring()
4520 if (m_nrhs!=2 || !create_return_values(1))
4523 int32_t max_order=get_int();
4525 CKernel* kernel=ui_kernel->get_kernel();
4529 SG_ERROR(
"Only works for Weighted Degree Position kernels.\n");
4531 CSVM* svm=(
CSVM*) ui_classifier->get_classifier();
4534 int32_t* sv_idx=
SG_MALLOC(int32_t, num_suppvec);
4539 for (int32_t i=0; i<num_suppvec; i++)
4545 if ((max_order<1) || (max_order>12))
4547 SG_WARNING(
"max_order out of range 1..12 (%d). setting to 1\n", max_order);
4553 max_order, num_feat, num_sym, NULL, num_suppvec, sv_idx, sv_weight);
4557 set_matrix(position_weights, num_sym, num_feat);
4566 bool CSGInterface::cmd_classify()
4568 if (m_nrhs!=1 || !create_return_values(1))
4571 if (!ui_kernel->get_kernel() ||
4572 !ui_kernel->get_kernel()->get_kernel_type()==
K_CUSTOM)
4574 CFeatures* feat=ui_features->get_test_features();
4579 CLabels* labels=ui_classifier->classify();
4585 for (int32_t i=0; i<num_vec; i++)
4589 set_vector(result, num_vec);
4595 bool CSGInterface::cmd_classify_example()
4597 if (m_nrhs!=2 || !create_return_values(1))
4600 int32_t idx=get_int();
4603 if (!ui_classifier->classify_example(idx, result))
4604 SG_ERROR(
"Classify_example failed.\n");
4611 bool CSGInterface::cmd_get_classifier()
4613 if (m_nrhs<1 || m_nrhs>2 || !create_return_values(2))
4627 if (!ui_classifier->get_trained_classifier(
4628 weights, rows, cols, bias, brows, bcols, idx))
4633 set_matrix(bias, brows, bcols);
4638 set_matrix(weights, rows, cols);
4644 bool CSGInterface::cmd_new_classifier()
4646 if (m_nrhs<2 || !create_return_values(0))
4650 char* name=get_str_from_str_or_direct(len);
4656 d=get_int_from_int_or_str();
4659 from_d=get_int_from_int_or_str();
4662 bool success=ui_classifier->new_classifier(name, d, from_d);
4668 bool CSGInterface::cmd_save_classifier()
4670 if (m_nrhs<2 || !create_return_values(0))
4674 char* filename=get_str_from_str_or_direct(len);
4676 bool success=ui_classifier->save(filename);
4682 bool CSGInterface::cmd_load_classifier()
4684 if (m_nrhs<3 || !create_return_values(0))
4688 char* filename=get_str_from_str_or_direct(len);
4689 char* type=get_str_from_str_or_direct(len);
4691 bool success=ui_classifier->load(filename, type);
4699 bool CSGInterface::cmd_get_num_svms()
4701 if (m_nrhs!=1 || !create_return_values(1))
4704 set_int(ui_classifier->get_num_svms());
4710 bool CSGInterface::cmd_get_svm()
4712 return cmd_get_classifier();
4715 bool CSGInterface::cmd_set_svm()
4717 if (m_nrhs!=3 || !create_return_values(0))
4723 int32_t num_feat_alphas=0;
4724 int32_t num_vec_alphas=0;
4725 get_matrix(alphas, num_feat_alphas, num_vec_alphas);
4728 SG_ERROR(
"No proper alphas given.\n");
4729 if (num_vec_alphas!=2)
4730 SG_ERROR(
"Not 2 vectors in alphas.\n");
4732 CSVM* svm=(
CSVM*) ui_classifier->get_classifier();
4734 SG_ERROR(
"No SVM object available.\n");
4740 for (int32_t i=0; i<num_support_vectors; i++)
4750 bool CSGInterface::cmd_set_linear_classifier()
4752 if (m_nrhs!=3 || !create_return_values(0))
4762 SG_ERROR(
"No proper weight vector given.\n");
4766 SG_ERROR(
"No Linear Classifier object available.\n");
4776 bool CSGInterface::cmd_get_svm_objective()
4778 if (m_nrhs!=1 || !create_return_values(1))
4781 CSVM* svm=(
CSVM*) ui_classifier->get_classifier();
4790 bool CSGInterface::cmd_compute_svm_primal_objective()
4792 return do_compute_objective(SVM_PRIMAL);
4795 bool CSGInterface::cmd_compute_svm_dual_objective()
4797 return do_compute_objective(SVM_DUAL);
4800 bool CSGInterface::cmd_compute_mkl_dual_objective()
4802 return do_compute_objective(MKL_DUAL);
4805 bool CSGInterface::cmd_compute_relative_mkl_duality_gap()
4807 return do_compute_objective(MKL_RELATIVE_DUALITY_GAP);
4810 bool CSGInterface::cmd_compute_absolute_mkl_duality_gap()
4812 return do_compute_objective(MKL_ABSOLUTE_DUALITY_GAP);
4815 bool CSGInterface::do_compute_objective(E_WHICH_OBJ obj)
4817 if (m_nrhs!=1 || !create_return_values(1))
4822 CSVM* svm=(
CSVM*) ui_classifier->get_classifier();
4827 trainlabels=ui_labels->get_train_labels();
4830 SG_ERROR(
"No trainlabels available.\n");
4832 CKernel* kernel=ui_kernel->get_kernel();
4834 SG_ERROR(
"No kernel available.\n");
4836 if (!ui_kernel->is_initialized() || !kernel->
has_features())
4837 SG_ERROR(
"Kernel not initialized.\n");
4853 result=((
CMKL*) svm)->compute_mkl_primal_objective();
4857 result=((
CMKL*) svm)->compute_mkl_dual_objective();
4859 case MKL_RELATIVE_DUALITY_GAP:
4862 float64_t primal=((
CMKL*) svm)->compute_mkl_dual_objective();
4863 float64_t dual=((
CMKL*) svm)->compute_mkl_primal_objective();
4864 result=(primal-dual)/dual;
4867 case MKL_ABSOLUTE_DUALITY_GAP:
4870 float64_t primal=((
CMKL*) svm)->compute_mkl_dual_objective();
4871 float64_t dual=((
CMKL*) svm)->compute_mkl_primal_objective();
4876 SG_SERROR(
"Error calling do_compute_objective\n");
4884 bool CSGInterface::cmd_train_classifier()
4886 if (m_nrhs<1 || !create_return_values(0))
4889 CMachine* classifier=ui_classifier->get_classifier();
4891 SG_ERROR(
"No classifier available.\n");
4911 return ui_classifier->train_svm();
4913 return ui_classifier->train_mkl_multiclass();
4917 return ui_classifier->train_mkl();
4920 return ui_classifier->train_krr();
4927 int32_t k=get_int_from_int_or_str();
4929 return ui_classifier->train_knn(k);
4937 int32_t k=get_int_from_int_or_str();
4938 int32_t max_iter=get_int_from_int_or_str();
4940 return ui_classifier->train_clustering(k, max_iter);
4948 int32_t merges=get_int_from_int_or_str();
4950 return ui_classifier->train_clustering(merges);
4957 gamma=get_real_from_real_or_str();
4959 return ui_classifier->train_linear(gamma);
4972 return ui_classifier->train_linear();
4975 return ui_classifier->train_wdocas();
4978 SG_ERROR(
"Unknown classifier type %d.\n", type);
4984 bool CSGInterface::cmd_do_auc_maximization()
4986 if (m_nrhs!=2 || !create_return_values(0))
4989 bool do_auc=get_bool_from_bool_or_str();
4991 return ui_classifier->set_do_auc_maximization(do_auc);
4994 bool CSGInterface::cmd_set_perceptron_parameters()
4996 if (m_nrhs!=3 || !create_return_values(0))
4999 float64_t lernrate=get_real_from_real_or_str();
5000 int32_t maxiter=get_int_from_int_or_str();
5002 return ui_classifier->set_perceptron_parameters(lernrate, maxiter);
5005 bool CSGInterface::cmd_set_svm_qpsize()
5007 if (m_nrhs!=2 || !create_return_values(0))
5010 int32_t qpsize=get_int_from_int_or_str();
5012 return ui_classifier->set_svm_qpsize(qpsize);
5015 bool CSGInterface::cmd_set_svm_max_qpsize()
5017 if (m_nrhs!=2 || !create_return_values(0))
5020 int32_t max_qpsize=get_int_from_int_or_str();
5022 return ui_classifier->set_svm_max_qpsize(max_qpsize);
5025 bool CSGInterface::cmd_set_svm_bufsize()
5027 if (m_nrhs!=2 || !create_return_values(0))
5030 int32_t bufsize=get_int_from_int_or_str();
5032 return ui_classifier->set_svm_bufsize(bufsize);
5035 bool CSGInterface::cmd_set_svm_C()
5037 if (m_nrhs<2 || !create_return_values(0))
5040 float64_t C1=get_real_from_real_or_str();
5044 C2=get_real_from_real_or_str();
5046 return ui_classifier->set_svm_C(C1, C2);
5049 bool CSGInterface::cmd_set_svm_epsilon()
5051 if (m_nrhs!=2 || !create_return_values(0))
5054 float64_t epsilon=get_real_from_real_or_str();
5056 return ui_classifier->set_svm_epsilon(epsilon);
5059 bool CSGInterface::cmd_set_svr_tube_epsilon()
5061 if (m_nrhs!=2 || !create_return_values(0))
5064 float64_t tube_epsilon=get_real_from_real_or_str();
5066 return ui_classifier->set_svr_tube_epsilon(tube_epsilon);
5069 bool CSGInterface::cmd_set_svm_nu()
5071 if (m_nrhs!=2 || !create_return_values(0))
5074 float64_t nu=get_real_from_real_or_str();
5076 return ui_classifier->set_svm_nu(nu);
5079 bool CSGInterface::cmd_set_svm_mkl_parameters()
5081 if (m_nrhs<3 || m_nrhs>4 || !create_return_values(0))
5084 float64_t weight_epsilon=get_real_from_real_or_str();
5085 float64_t C_mkl=get_real_from_real_or_str();
5089 mkl_norm=get_real_from_real_or_str();
5091 return ui_classifier->set_svm_mkl_parameters(weight_epsilon, C_mkl, mkl_norm);
5094 bool CSGInterface::cmd_set_elasticnet_lambda()
5096 if (m_nrhs!=2 || !create_return_values(0))
5098 float64_t lambda=get_real_from_real_or_str();
5099 return ui_classifier->set_elasticnet_lambda(lambda);
5102 bool CSGInterface::cmd_set_mkl_block_norm()
5104 if (m_nrhs!=2 || !create_return_values(0))
5106 float64_t bnorm=get_real_from_real_or_str();
5107 return ui_classifier->set_mkl_block_norm(bnorm);
5111 bool CSGInterface::cmd_set_max_train_time()
5113 if (m_nrhs!=2 || !create_return_values(0))
5116 float64_t max_train_time=get_real_from_real_or_str();
5118 return ui_classifier->set_max_train_time(max_train_time);
5121 bool CSGInterface::cmd_set_svm_shrinking_enabled()
5123 if (m_nrhs!=2 || !create_return_values(0))
5126 bool shrinking_enabled=get_bool_from_bool_or_str();
5128 return ui_classifier->set_svm_shrinking_enabled(shrinking_enabled);
5131 bool CSGInterface::cmd_set_svm_batch_computation_enabled()
5133 if (m_nrhs!=2 || !create_return_values(0))
5136 bool batch_computation_enabled=get_bool_from_bool_or_str();
5138 return ui_classifier->set_svm_batch_computation_enabled(
5139 batch_computation_enabled);
5142 bool CSGInterface::cmd_set_svm_linadd_enabled()
5144 if (m_nrhs!=2 || !create_return_values(0))
5147 bool linadd_enabled=get_bool_from_bool_or_str();
5149 return ui_classifier->set_svm_linadd_enabled(linadd_enabled);
5152 bool CSGInterface::cmd_set_svm_bias_enabled()
5154 if (m_nrhs!=2 || !create_return_values(0))
5157 bool bias_enabled=get_bool_from_bool_or_str();
5159 return ui_classifier->set_svm_bias_enabled(bias_enabled);
5162 bool CSGInterface::cmd_set_mkl_interleaved_enabled()
5164 if (m_nrhs!=2 || !create_return_values(0))
5167 bool interleaved_enabled=get_bool_from_bool_or_str();
5169 return ui_classifier->set_mkl_interleaved_enabled(interleaved_enabled);
5172 bool CSGInterface::cmd_set_krr_tau()
5174 if (m_nrhs!=2 || !create_return_values(0))
5177 float64_t tau=get_real_from_real_or_str();
5179 return ui_classifier->set_krr_tau(tau);
5185 bool CSGInterface::cmd_add_preproc()
5187 if (m_nrhs<2 || !create_return_values(0))
5191 char* type=get_str_from_str_or_direct(len);
5194 if (strmatch(type,
"NORMONE"))
5195 preproc=ui_preproc->create_generic(
P_NORMONE);
5196 else if (strmatch(type,
"LOGPLUSONE"))
5198 else if (strmatch(type,
"SORTWORDSTRING"))
5200 else if (strmatch(type,
"SORTULONGSTRING"))
5202 else if (strmatch(type,
"DECOMPRESSCHARSTRING"))
5204 else if (strmatch(type,
"SORTWORD"))
5205 preproc=ui_preproc->create_generic(
P_SORTWORD);
5207 else if (strmatch(type,
"PRUNEVARSUBMEAN"))
5209 bool divide_by_std=
false;
5211 divide_by_std=get_bool_from_bool_or_str();
5213 preproc=ui_preproc->create_prunevarsubmean(divide_by_std);
5217 else if (strmatch(type,
"PCA") && m_nrhs==4)
5219 bool do_whitening=get_bool_from_bool_or_str();
5220 float64_t threshold=get_real_from_real_or_str();
5222 preproc=ui_preproc->create_pca(do_whitening, threshold);
5230 return ui_preproc->add_preproc(preproc);
5233 bool CSGInterface::cmd_del_preproc()
5235 if (m_nrhs!=1 || !create_return_values(0))
5238 return ui_preproc->del_preproc();
5241 bool CSGInterface::cmd_attach_preproc()
5243 if (m_nrhs<2 || !create_return_values(0))
5247 char* target=get_str_from_str_or_direct(len);
5249 bool do_force=
false;
5251 do_force=get_bool_from_bool_or_str();
5253 bool success=ui_preproc->attach_preproc(target, do_force);
5259 bool CSGInterface::cmd_clean_preproc()
5261 if (m_nrhs!=1 || !create_return_values(0))
5264 return ui_preproc->clean_preproc();
5270 bool CSGInterface::cmd_new_plugin_estimator()
5272 if (m_nrhs<2 || !create_return_values(0))
5275 float64_t pos_pseudo=get_real_from_real_or_str();
5276 float64_t neg_pseudo=get_real_from_real_or_str();
5278 return ui_pluginestimate->new_estimator(pos_pseudo, neg_pseudo);
5281 bool CSGInterface::cmd_train_estimator()
5283 if (m_nrhs!=1 || !create_return_values(0))
5286 return ui_pluginestimate->train();
5289 bool CSGInterface::cmd_plugin_estimate_classify_example()
5291 if (m_nrhs!=2 || !create_return_values(1))
5294 int32_t idx=get_int();
5295 float64_t result=ui_pluginestimate->apply(idx);
5297 set_vector(&result, 1);
5301 bool CSGInterface::cmd_plugin_estimate_classify()
5303 if (m_nrhs!=1 || !create_return_values(1))
5306 CFeatures* feat=ui_features->get_test_features();
5312 CLabels* labels=ui_pluginestimate->apply();
5313 for (int32_t i=0; i<num_vec; i++)
5317 set_vector(result, num_vec);
5323 bool CSGInterface::cmd_set_plugin_estimate()
5325 if (m_nrhs!=3 || !create_return_values(0))
5329 int32_t num_probs=0;
5331 get_matrix(emission_probs, num_probs, num_vec);
5334 SG_ERROR(
"Need at least 1 set of positive and 1 set of negative params.\n");
5337 float64_t* neg_params=&(emission_probs[num_probs]);
5341 get_vector(model_sizes, len);
5343 int32_t seq_length=(int32_t) model_sizes[0];
5344 int32_t num_symbols=(int32_t) model_sizes[1];
5345 if (num_probs!=seq_length*num_symbols)
5346 SG_ERROR(
"Mismatch in number of emission probs and sequence length * number of symbols.\n");
5348 ui_pluginestimate->get_estimator()->set_model_params(
5349 pos_params, neg_params, seq_length, num_symbols);
5354 bool CSGInterface::cmd_get_plugin_estimate()
5356 if (m_nrhs!=1 || !create_return_values(2))
5361 int32_t num_params=0;
5362 int32_t seq_length=0;
5363 int32_t num_symbols=0;
5365 if (!ui_pluginestimate->get_estimator()->get_model_params(
5366 pos_params, neg_params, seq_length, num_symbols))
5369 num_params=seq_length*num_symbols;
5372 for (int32_t i=0; i<num_params; i++)
5373 result[i]=pos_params[i];
5374 for (int32_t i=0; i<num_params; i++)
5375 result[i+num_params]=neg_params[i];
5377 set_matrix(result, num_params, 2);
5383 set_vector(model_sizes, 2);
5388 bool CSGInterface::cmd_convergence_criteria()
5390 if (m_nrhs<3 || !create_return_values(0))
5393 int32_t num_iterations=get_int_from_int_or_str();
5394 float64_t epsilon=get_real_from_real_or_str();
5396 return ui_hmm->convergence_criteria(num_iterations, epsilon);
5399 bool CSGInterface::cmd_normalize()
5401 if (m_nrhs<2 || !create_return_values(0))
5404 bool keep_dead_states=get_bool_from_bool_or_str();
5406 return ui_hmm->normalize(keep_dead_states);
5409 bool CSGInterface::cmd_add_states()
5411 if (m_nrhs<3 || !create_return_values(0))
5414 int32_t num_states=get_int_from_int_or_str();
5415 float64_t value=get_real_from_real_or_str();
5417 return ui_hmm->add_states(num_states, value);
5420 bool CSGInterface::cmd_permutation_entropy()
5422 if (m_nrhs<3 || !create_return_values(0))
5425 int32_t width=get_int_from_int_or_str();
5426 int32_t seq_num=get_int_from_int_or_str();
5428 return ui_hmm->permutation_entropy(width, seq_num);
5431 bool CSGInterface::cmd_relative_entropy()
5433 if (m_nrhs!=1 || !create_return_values(1))
5438 bool success=ui_hmm->relative_entropy(entropy, len);
5442 set_vector(entropy, len);
5448 bool CSGInterface::cmd_entropy()
5450 if (m_nrhs!=1 || !create_return_values(1))
5455 bool success=ui_hmm->entropy(entropy, len);
5459 set_vector(entropy, len);
5465 bool CSGInterface::cmd_hmm_classify()
5467 return do_hmm_classify(
false,
false);
5470 bool CSGInterface::cmd_one_class_hmm_classify()
5472 return do_hmm_classify(
false,
true);
5475 bool CSGInterface::cmd_one_class_linear_hmm_classify()
5477 return do_hmm_classify(
true,
true);
5480 bool CSGInterface::do_hmm_classify(
bool linear,
bool one_class)
5482 if (m_nrhs>1 || !create_return_values(1))
5485 CFeatures* feat=ui_features->get_test_features();
5494 labels=ui_hmm->linear_one_class_classify();
5499 labels=ui_hmm->one_class_classify();
5501 labels=ui_hmm->classify();
5507 for (int32_t i=0; i<num_vec; i++)
5511 set_vector(result, num_vec);
5517 bool CSGInterface::cmd_one_class_hmm_classify_example()
5519 return do_hmm_classify_example(
true);
5522 bool CSGInterface::cmd_hmm_classify_example()
5524 return do_hmm_classify_example(
false);
5527 bool CSGInterface::do_hmm_classify_example(
bool one_class)
5529 if (m_nrhs!=2 || !create_return_values(1))
5532 int32_t idx=get_int();
5536 result=ui_hmm->one_class_classify_example(idx);
5538 result=ui_hmm->classify_example(idx);
5545 bool CSGInterface::cmd_output_hmm()
5547 if (m_nrhs!=1 || !create_return_values(0))
5550 return ui_hmm->output_hmm();
5553 bool CSGInterface::cmd_output_hmm_defined()
5555 if (m_nrhs!=1 || !create_return_values(0))
5558 return ui_hmm->output_hmm_defined();
5561 bool CSGInterface::cmd_hmm_likelihood()
5563 if (m_nrhs!=1 || !create_return_values(1))
5566 CHMM* h=ui_hmm->get_current();
5571 set_real(likelihood);
5576 bool CSGInterface::cmd_likelihood()
5578 if (m_nrhs!=1 || !create_return_values(0))
5581 return ui_hmm->likelihood();
5584 bool CSGInterface::cmd_save_likelihood()
5586 if (m_nrhs<2 || !create_return_values(0))
5590 char* filename=get_str_from_str_or_direct(len);
5592 bool is_binary=
false;
5594 is_binary=get_bool_from_bool_or_str();
5596 bool success=ui_hmm->save_likelihood(filename, is_binary);
5602 bool CSGInterface::cmd_get_viterbi_path()
5604 if (m_nrhs!=2 || !create_return_values(2))
5607 int32_t dim=get_int();
5610 CHMM* h=ui_hmm->get_current();
5614 CFeatures* feat=ui_features->get_test_features();
5624 if (!vec || num_feat<=0)
5631 SG_DEBUG(
"computing viterbi path for vector %d (length %d)\n", dim, num_feat);
5635 set_vector(path, num_feat);
5637 set_real(likelihood);
5642 bool CSGInterface::cmd_viterbi_train()
5644 if (m_nrhs!=1 || !create_return_values(0))
5647 return ui_hmm->viterbi_train();
5650 bool CSGInterface::cmd_viterbi_train_defined()
5652 if (m_nrhs!=1 || !create_return_values(0))
5655 return ui_hmm->viterbi_train_defined();
5658 bool CSGInterface::cmd_baum_welch_train()
5660 if (m_nrhs!=1 || !create_return_values(0))
5663 return ui_hmm->baum_welch_train();
5666 bool CSGInterface::cmd_baum_welch_train_defined()
5668 if (m_nrhs!=1 || !create_return_values(0))
5671 return ui_hmm->baum_welch_train_defined();
5675 bool CSGInterface::cmd_baum_welch_trans_train()
5677 if (m_nrhs!=1 || !create_return_values(0))
5680 return ui_hmm->baum_welch_trans_train();
5683 bool CSGInterface::cmd_linear_train()
5685 if (m_nrhs<1 || !create_return_values(0))
5691 char* align=get_str_from_str_or_direct(len);
5693 bool success=ui_hmm->linear_train(align[0]);
5699 return ui_hmm->linear_train();
5702 bool CSGInterface::cmd_save_path()
5704 if (m_nrhs<2 || !create_return_values(0))
5708 char* filename=get_str_from_str_or_direct(len);
5710 bool is_binary=
false;
5712 is_binary=get_bool_from_bool_or_str();
5714 bool success=ui_hmm->save_path(filename, is_binary);
5720 bool CSGInterface::cmd_append_hmm()
5722 if (m_nrhs!=5 || !create_return_values(0))
5725 CHMM* old_h=ui_hmm->get_current();
5740 get_matrix(a, M_a, N_a);
5746 get_matrix(b, M_b, N_b);
5749 if (N_p!=N || N_q!=N || N_a!=N || M_a!=N || N_b!=M || M_b!=N)
5751 SG_ERROR(
"Model matrices not matching in size.\n"
5752 "p:(%d) q:(%d) a:(%d,%d) b(%d,%d)\n",
5753 N_p, N_q, N_a, M_a, N_b, M_b);
5756 CHMM* h=
new CHMM(N, M, NULL, ui_hmm->get_pseudo());
5767 h->
set_a(i,j, a[i+j*N]);
5771 h->
set_b(i,j, b[i+j*N]);
5779 bool CSGInterface::cmd_append_model()
5781 if (m_nrhs<2 || !create_return_values(0))
5783 if (m_nrhs>2 && m_nrhs!=4)
5787 char* filename=get_str_from_str_or_direct(len);
5792 base1=get_int_from_int_or_str();
5793 base2=get_int_from_int_or_str();
5796 bool success=ui_hmm->append_model(filename, base1, base2);
5802 bool CSGInterface::cmd_new_hmm()
5804 if (m_nrhs!=3 || !create_return_values(0))
5807 int32_t n=get_int_from_int_or_str();
5808 int32_t m=get_int_from_int_or_str();
5810 return ui_hmm->new_hmm(n, m);
5813 bool CSGInterface::cmd_load_hmm()
5815 if (m_nrhs!=2 || !create_return_values(0))
5819 char* filename=get_str_from_str_or_direct(len);
5821 bool success=ui_hmm->load(filename);
5827 bool CSGInterface::cmd_save_hmm()
5829 if (m_nrhs<2 || !create_return_values(0))
5833 char* filename=get_str_from_str_or_direct(len);
5835 bool is_binary=
false;
5837 is_binary=get_bool_from_bool_or_str();
5839 bool success=ui_hmm->save(filename, is_binary);
5845 bool CSGInterface::cmd_set_hmm()
5847 if (m_nrhs!=5 || !create_return_values(0))
5861 get_matrix(a, M_a, N_a);
5867 get_matrix(b, M_b, N_b);
5870 if (N_p!=N || N_q!=N || N_a!=N || M_a!=N || N_b!=M || M_b!=N)
5872 SG_ERROR(
"Model matrices not matching in size.\n"
5873 "p:(%d) q:(%d) a:(%d,%d) b(%d,%d)\n",
5874 N_p, N_q, N_a, M_a, N_b, M_b);
5877 CHMM* current=ui_hmm->get_current();
5879 SG_ERROR(
"Need a previously created HMM.\n");
5885 current->
set_p(i, p[i]);
5886 current->
set_q(i, q[i]);
5891 current->
set_a(i,j, a[i+j*N]);
5895 current->
set_b(i,j, b[i+j*N]);
5903 bool CSGInterface::cmd_set_hmm_as()
5905 if (m_nrhs!=2 || !create_return_values(0))
5909 char* target=get_str_from_str_or_direct(len);
5911 bool success=ui_hmm->set_hmm_as(target);
5917 bool CSGInterface::cmd_set_chop()
5919 if (m_nrhs!=2 || !create_return_values(0))
5922 float64_t value=get_real_from_real_or_str();
5923 return ui_hmm->chop(value);
5926 bool CSGInterface::cmd_set_pseudo()
5928 if (m_nrhs!=2 || !create_return_values(0))
5931 float64_t value=get_real_from_real_or_str();
5932 return ui_hmm->set_pseudo(value);
5935 bool CSGInterface::cmd_load_definitions()
5937 if (m_nrhs<2 || !create_return_values(0))
5941 char* filename=get_str_from_str_or_direct(len);
5945 do_init=get_bool_from_bool_or_str();
5947 bool success=ui_hmm->load_definitions(filename, do_init);
5953 bool CSGInterface::cmd_get_hmm()
5955 if (m_nrhs!=1 || !create_return_values(4))
5958 CHMM* h=ui_hmm->get_current();
5962 int32_t N=h->
get_N();
5963 int32_t M=h->
get_M();
5983 a[i+j*N]=h->
get_a(i, j);
5984 set_matrix(a, N, N);
5990 b[i+j*N]=h->
get_b(i, j);
5991 set_matrix(b, N, M);
5997 bool CSGInterface::cmd_best_path()
5999 if (m_nrhs!=3 || !create_return_values(0))
6002 int32_t from=get_int_from_int_or_str();
6003 int32_t to=get_int_from_int_or_str();
6005 return ui_hmm->best_path(from, to);
6008 bool CSGInterface::cmd_best_path_2struct()
6010 if (m_nrhs!=12 || !create_return_values(3))
6013 SG_ERROR(
"Sorry, this parameter list is awful!\n");
6018 void CSGInterface::get_vector(
bool*& vector, int32_t& len)
6020 int32_t* int_vector;
6021 get_vector(int_vector, len);
6026 for (int32_t i=0; i<len; i++)
6027 vector[i]= (int_vector[i]!=0);
6032 void CSGInterface::set_vector(
const bool* vector, int32_t len)
6034 int32_t* int_vector =
SG_MALLOC(int32_t, len);
6035 for (int32_t i=0;i<len;i++)
6042 set_vector(int_vector,len);
6046 bool CSGInterface::cmd_set_plif_struct()
6051 get_vector(ids,Nid);
6057 get_string_list(names, Nname,Mname);
6063 get_matrix(all_limits, Mlimits, Nlimits);
6066 int32_t Npenalties=0;
6067 int32_t Mpenalties=0;
6069 get_matrix(all_penalties, Mpenalties, Npenalties);
6072 int32_t Ntransform=0;
6073 int32_t Mtransform=0;
6075 get_string_list(all_transform, Ntransform, Mtransform);
6080 get_vector(min_values,Nmin);
6085 get_vector(max_values,Nmax);
6089 bool* all_use_cache;
6090 get_vector(all_use_cache,Ncache);
6094 int32_t* all_use_svm;
6095 get_vector(all_use_svm,Nsvm);
6100 get_vector(all_do_calc,Ncalc);
6103 SG_ERROR(
"Ncalc!=Nsvm, Ncalc:%i, Nsvm:%i\n",Ncalc,Nsvm);
6105 SG_ERROR(
"Ncalc!=Ncache, Ncalc:%i, Ncache:%i\n",Ncalc,Ncache);
6106 if (Ncalc!=Ntransform)
6107 SG_ERROR(
"Ncalc!=Ntransform, Ncalc:%i, Ntransform:%i\n",Ncalc,Ntransform);
6109 SG_ERROR(
"Ncalc!=Nmin, Ncalc:%i, Nmin:%i\n",Ncalc,Nmin);
6111 SG_ERROR(
"Ncalc!=Nmax, Ncalc:%i, Nmax:%i\n",Ncalc,Nmax);
6112 if (Ncalc!=Npenalties)
6113 SG_ERROR(
"Ncalc!=Npenalties, Ncalc:%i, Npenalties:%i\n",Ncalc,Npenalties);
6115 SG_ERROR(
"Ncalc!=Nlimits, Ncalc:%i, Nlimits:%i\n",Ncalc,Nlimits);
6117 SG_ERROR(
"Ncalc!=Nname, Ncalc:%i, Nname:%i\n",Ncalc,Nname);
6119 SG_ERROR(
"Ncalc!=Nid, Ncalc:%i, Nid:%i\n",Ncalc,Nid);
6120 if (Mlimits!=Mpenalties)
6121 SG_ERROR(
"Mlimits!=Mpenalties, Mlimits:%i, Mpenalties:%i\n",Mlimits,Mpenalties);
6124 int32_t M = Mlimits;
6150 bool CSGInterface::cmd_get_plif_struct()
6165 bool* all_use_cache =
SG_MALLOC(
bool, N);
6166 int32_t* all_use_svm =
SG_MALLOC(int32_t, N);
6168 for (int32_t i=0;i<N;i++)
6172 names[i].
slen = strlen(PEN[i]->get_plif_name());
6175 for (int32_t j=0;j<M;j++)
6177 all_limits[i*M+j]=limits[j];
6178 all_penalties[i*M+j]=penalties[j];
6180 all_transform[i].
string = (
char*) PEN[i]->get_transform_type();
6181 all_transform[i].
slen = strlen(PEN[i]->get_transform_type());
6190 set_string_list(names, N);
6191 set_matrix(all_limits, M, N);
6192 set_matrix(all_penalties, M, N);
6193 set_string_list(all_transform, N);
6194 set_vector(min_values,N);
6195 set_vector(max_values,N);
6196 set_vector(all_use_cache,N);
6197 set_vector(all_use_svm,N);
6198 set_vector(all_do_calc,N);
6226 bool CSGInterface::cmd_signals_set_positions()
6230 bool CSGInterface::cmd_signals_set_labels()
6234 bool CSGInterface::cmd_signals_set_split()
6238 bool CSGInterface::cmd_signals_set_train_mask()
6242 bool CSGInterface::cmd_signals_add_feature()
6246 bool CSGInterface::cmd_signals_add_kernel()
6250 bool CSGInterface::cmd_signals_run()
6255 bool CSGInterface::cmd_init_dyn_prog()
6258 int32_t num_svms=get_int();
6261 ui_structure->set_dyn_prog(h);
6265 bool CSGInterface::cmd_clean_up_dyn_prog()
6267 return ui_structure->cleanup();
6270 bool CSGInterface::cmd_set_model()
6275 CDynProg* h = ui_structure->get_dyn_prog();
6286 get_ndarray(penalties_array,Dim,numDim);
6291 SG_ERROR(
"error computing plif matrix\n");
6292 ui_structure->set_num_states(Dim[0]);
6297 bool use_orf = get_bool();
6298 ui_structure->set_use_orf(use_orf);
6305 get_matrix(mod_words, Nmod,Mmod);
6306 if (Nmod != num_svms)
6307 SG_ERROR(
"should be equal: Nmod: %i, num_svms: %i\n",Nmod,num_svms);
6314 int32_t num_states=0;
6315 int32_t feat_dim3=0;
6316 int32_t* state_signals;
6317 get_matrix(state_signals,num_states,feat_dim3);
6318 ASSERT(num_states==Dim[0]);
6328 get_matrix(orf_info,Norf,Morf);
6332 ui_structure->set_orf_info(orf_info, Norf, Morf);
6341 bool CSGInterface::cmd_precompute_content_svms()
6347 seq = get_string(seq_len);
6353 get_vector(all_pos, Npos);
6360 get_matrix(weights, Nweights, num_svms);
6362 SG_PRINT(
"Dimension mismatch: got %i, expect %i\n", Nweights, 5440) ;
6363 ui_structure->set_content_svm_weights(weights, Nweights, num_svms);
6365 CDynProg* h = ui_structure->get_dyn_prog();
6367 SG_ERROR(
"no DynProg object found, use init_dyn_prog first\n");
6382 SG_DEBUG(
"precompute_content_svms done\n");
6386 bool CSGInterface::cmd_get_lin_feat()
6388 CDynProg* h = ui_structure->get_dyn_prog();
6390 SG_ERROR(
"no DynProg object found, use set_model first\n");
6393 int32_t dim1, dim2 = 0;
6396 set_matrix(lin_feat, dim1, dim2);
6400 bool CSGInterface::cmd_set_lin_feat()
6405 seq = get_string(Nseq);
6411 get_vector(all_pos, Npos);
6415 int32_t num_svms, seq_len;
6417 get_matrix(lin_feat, num_svms, seq_len);
6421 SG_ERROR(
"Dimension mismatch: got %i positions and (%ix%i) values\n", Npos, num_svms, seq_len) ;
6430 CDynProg* h = ui_structure->get_dyn_prog();
6432 SG_ERROR(
"no DynProg object found, use set_model first\n");
6446 bool CSGInterface::cmd_long_transition_settings()
6448 bool use_long_transitions = get_bool();
6449 int32_t threshold = get_int();
6450 int32_t max_len = get_int();
6452 CDynProg* h = ui_structure->get_dyn_prog();
6454 SG_ERROR(
"no DynProg object found, use set_model first\n");
6460 bool CSGInterface::cmd_set_feature_matrix()
6462 int32_t num_states = ui_structure->get_num_states();
6469 get_ndarray(features, Dims, numDims);
6472 SG_ERROR(
"expected a 3 dimensional array, got %i dimensions\n", numDims);
6473 if (Dims[0]!=num_states)
6474 SG_ERROR(
"number of rows (%i) not equal number of states (%i)\n",Dims[0], num_states);
6475 ASSERT(ui_structure->set_feature_matrix(features, Dims));
6477 ASSERT(ui_structure->set_feature_dims(Dims));
6484 bool CSGInterface::cmd_set_feature_matrix_sparse()
6486 int32_t num_pos = ui_structure->get_num_positions();
6487 int32_t num_states = ui_structure->get_num_states();
6491 int32_t dim11, dim12 ;
6493 get_sparse_matrix(features1, dim11, dim12);
6495 int32_t dim21, dim22 ;
6497 get_sparse_matrix(features2, dim21, dim22);
6507 ASSERT(Dims[0]==num_states)
6510 ASSERT(ui_structure->set_feature_matrix_sparse(features1, features2, Dims));
6511 ASSERT(ui_structure->set_feature_dims(Dims));
6519 bool CSGInterface::cmd_init_intron_list()
6522 int32_t Nstart_positions;
6523 int32_t* start_positions;
6524 get_vector(start_positions, Nstart_positions);
6528 int32_t Nend_positions;
6529 int32_t* end_positions;
6530 get_vector(end_positions, Nend_positions);
6536 get_vector(quality, Nquality);
6542 get_vector(all_pos, Nall_pos);
6545 ASSERT(Nquality==Nend_positions);
6546 ASSERT(Nend_positions==Nstart_positions);
6550 intron_list->
init_list(all_pos, Nall_pos);
6552 intron_list->
read_introns(start_positions, end_positions, quality, Nstart_positions);
6565 CDynProg* h = ui_structure->get_dyn_prog();
6567 SG_ERROR(
"no DynProg object found, use set_model first\n");
6573 bool CSGInterface::cmd_precompute_tiling_features()
6577 CDynProg* h = ui_structure->get_dyn_prog();
6579 int32_t Nintensities=0;
6581 get_vector(intensities, Nintensities);
6583 int32_t Nprobe_pos=0;
6585 get_vector(probe_pos, Nprobe_pos);
6586 ASSERT(Nprobe_pos==Nintensities);
6588 int32_t Ntiling_plif_ids=0;
6589 int32_t* tiling_plif_ids;
6590 get_vector(tiling_plif_ids, Ntiling_plif_ids);
6597 bool CSGInterface::cmd_best_path_trans()
6599 CDynProg* h = ui_structure->get_dyn_prog();
6606 int32_t* feat_dims = ui_structure->get_feature_dims();
6607 float64_t* features = (ui_structure->get_feature_matrix(
false));
6610 int32_t* orf_info = ui_structure->get_orf_info();
6611 bool use_orf = ui_structure->get_use_orf();
6620 SG_ERROR(
"# transitions from initial state (%i) does not match # states (%i)\n", Np, num_states);
6628 SG_ERROR(
"# transitions to end state (%i) does not match # states (%i)\n", Nq, num_states);
6634 get_vector(all_nbest, Nnbest);
6639 nbest =all_nbest[0];
6640 nother=all_nbest[1];
6643 nbest =all_nbest[0];
6650 int32_t Nseg_path=0;
6651 int32_t Mseg_path=0;
6653 get_matrix(seg_path, Nseg_path, Mseg_path);
6658 int32_t num_a_trans=0;
6660 get_matrix(a_trans, num_a_trans, Na_trans);
6669 get_matrix(loss, Nloss,Mloss);
6699 SG_ERROR(
"svm arrays inconsistent\n") ;
6704 SG_DEBUG(
"best_path_trans: M: %i, Mseg_path: %i\n", M, Mseg_path);
6722 bool segment_loss_non_zero=
false;
6723 for (int32_t i=0; i<Nloss*Mloss; i++)
6726 segment_loss_non_zero=
true;
6736 if (segment_loss_non_zero)
6738 SG_DEBUG(
"Using version with segment_loss\n") ;
6746 SG_DEBUG(
"Using version without segment_loss\n") ;
6763 for (int32_t k=0; k<(nbest+nother); k++)
6765 for (int32_t i=0; i<M; i++)
6767 d_my_path[i*(nbest+nother)+k] = states.
matrix[i+k*M] ;
6768 d_my_pos[i*(nbest+nother)+k] = my_pos.
matrix[i+k*M] ;
6774 set_vector(p_prob.
vector,nbest+nother);
6775 set_vector(d_my_path, (nbest+nother)*M);
6776 set_vector(d_my_pos, (nbest+nother)*M);
6785 bool CSGInterface::cmd_best_path_trans_deriv()
6787 int32_t num_states = ui_structure->get_num_states();
6788 int32_t* feat_dims = ui_structure->get_feature_dims();
6789 float64_t* features = (ui_structure->get_feature_matrix(
false));
6801 SG_ERROR(
"Np!=num_states; Np:%i num_states:%i",Np,num_states);
6809 SG_ERROR(
"Nq!=num_states; Nq:%i num_states:%i",Nq,num_states);
6816 int32_t Nseg_path=0;
6817 int32_t Mseg_path=0;
6819 get_matrix(seg_path,Nseg_path,Mseg_path);
6824 int32_t num_a_trans=0;
6826 get_matrix(a_trans, num_a_trans, Na_trans);
6835 get_matrix(loss, Nloss,Mloss);
6839 int32_t Nmystate_seq=0;
6840 int32_t* mystate_seq=NULL;
6841 get_vector(mystate_seq, Nmystate_seq);
6845 int32_t Nmypos_seq=0;
6846 int32_t* mypos_seq=NULL;
6847 get_vector(mypos_seq, Nmypos_seq);
6852 int32_t max_plif_id = 0 ;
6853 int32_t max_plif_len = 1 ;
6854 for (int32_t i=0; i<Nplif; i++)
6856 if (i>0 && PEN[i]->get_id()!=i)
6857 SG_ERROR(
"PEN[i]->get_id()!=i; PEN[%i]->get_id():%i ,\n",i, PEN[i]->get_id());
6860 if (PEN[i]->get_plif_len()>max_plif_len)
6865 CDynProg* h = ui_structure->get_dyn_prog();
6877 SG_ERROR(
"svm arrays inconsistent\n") ;
6879 int32_t *my_path =
SG_MALLOC(int32_t, Nmypos_seq+1);
6880 memset(my_path, -1, Nmypos_seq*
sizeof(int32_t)) ;
6881 int32_t *my_pos =
SG_MALLOC(int32_t, Nmypos_seq+1);
6882 memset(my_pos, -1, Nmypos_seq*
sizeof(int32_t)) ;
6885 for (int32_t i=0; i<Nmypos_seq; i++)
6887 my_path[i] = mystate_seq[i] ;
6888 my_pos[i] = mypos_seq[i] ;
6905 CArray2<float64_t> a_Plif_deriv(p_Plif_deriv, max_plif_id+1, max_plif_len,
false,
false) ;
6922 for (int32_t i=0; i<num_states; i++)
6924 for (int32_t j=0; j<num_states; j++)
6925 p_A_deriv[i+j*num_states] = h->
get_a_deriv(i, j) ;
6931 for (int32_t
id=0;
id<=max_plif_id;
id++)
6935 ASSERT(len<=max_plif_len) ;
6936 for (int32_t j=0; j<max_plif_len; j++)
6937 a_Plif_deriv.
element(
id, j)= deriv[j] ;
6940 set_vector(p_p_deriv, num_states);
6941 set_vector(p_q_deriv, num_states);
6942 set_matrix(p_A_deriv, num_states, num_states);
6943 set_matrix(p_Plif_deriv, (max_plif_id+1), max_plif_len);
6944 set_vector(p_my_scores, Nmypos_seq);
6945 set_vector(p_my_losses, Nmypos_seq);
6968 bool CSGInterface::cmd_precompute_subkernels()
6970 if (m_nrhs!=1 || !create_return_values(0))
6973 return ui_kernel->precompute_subkernels();
6975 bool CSGInterface::cmd_crc()
6977 if (m_nrhs!=2 || !create_return_values(1))
6981 char*
string=get_string(slen);
6983 uint8_t* bstring=
SG_MALLOC(uint8_t, slen);
6985 for (int32_t i=0; i<slen; i++)
6986 bstring[i]=
string[i];
6996 bool CSGInterface::cmd_system()
6998 if (m_nrhs<2 || !create_return_values(0))
7003 memset(command, 0,
sizeof(
char)*10000);
7004 char* cmd=get_str_from_str_or_direct(len);
7005 strncat(command, cmd, 10000);
7008 while (m_rhs_counter<m_nrhs)
7010 strncat(command,
" ", 10000);
7011 char* arg=get_str_from_str_or_direct(len);
7012 strncat(command, arg, 10000);
7016 int32_t success=system(command);
7018 return (success==0);
7021 bool CSGInterface::cmd_exit()
7027 bool CSGInterface::cmd_exec()
7029 if (m_nrhs<2 || !create_return_values(0))
7033 char* filename=get_str_from_str_or_direct(len);
7034 FILE* file=fopen(filename,
"r");
7038 SG_ERROR(
"Error opening file: %s.\n", filename);
7051 bool CSGInterface::cmd_set_output()
7053 if (m_nrhs<2 || !create_return_values(0))
7057 char* filename=get_str_from_str_or_direct(len);
7063 SG_INFO(
"Setting output file to: %s.\n", filename);
7065 if (strmatch(filename,
"STDERR"))
7066 io->set_target(stderr);
7067 else if (strmatch(filename,
"STDOUT"))
7068 io->set_target(stdout);
7071 file_out=fopen(filename,
"w");
7073 SG_ERROR(
"Error opening output file %s.\n", filename);
7074 io->set_target(file_out);
7080 bool CSGInterface::cmd_set_threshold()
7082 if (m_nrhs!=2 || !create_return_values(0))
7085 float64_t value=get_real_from_real_or_str();
7087 ui_math->set_threshold(value);
7091 bool CSGInterface::cmd_init_random()
7093 if (m_nrhs!=2 || !create_return_values(0))
7096 uint32_t initseed=(uint32_t) get_int_from_int_or_str();
7097 ui_math->init_random(initseed);
7102 bool CSGInterface::cmd_set_num_threads()
7104 if (m_nrhs!=2 || !create_return_values(0))
7107 int32_t num_threads=get_int_from_int_or_str();
7109 parallel->set_num_threads(num_threads);
7110 SG_INFO(
"Set number of threads to %d.\n", num_threads);
7115 bool CSGInterface::cmd_translate_string()
7117 if (m_nrhs!=4 || !create_return_values(1))
7122 get_vector(
string, len);
7124 int32_t order=get_int();
7125 int32_t start=get_int();
7127 const int32_t max_val=2;
7131 for (i=0; i<len; i++)
7133 switch ((
char)
string[i])
7135 case 'A': obs[i]=0;
break;
7136 case 'C': obs[i]=1;
break;
7137 case 'G': obs[i]=2;
break;
7138 case 'T': obs[i]=3;
break;
7139 case 'a': obs[i]=0;
break;
7140 case 'c': obs[i]=1;
break;
7141 case 'g': obs[i]=2;
break;
7142 case 't': obs[i]=3;
break;
7143 default:
SG_ERROR(
"Wrong letter in string.\n");
7148 for (i=len-1; i>=order-1; i--)
7151 for (j=i; j>=i-order+1; j--)
7152 value=(value>>max_val) | ((obs[j])<<(max_val*(order-1)));
7154 obs[i]=(uint16_t) value;
7157 for (i=order-2;i>=0;i--)
7160 for (j=i; j>=i-order+1; j--)
7162 value= (value >> max_val);
7164 value|=(obs[j]) << (max_val * (order-1));
7170 for (i=start; i<len; i++)
7174 set_vector(real_obs, len);
7180 bool CSGInterface::cmd_clear()
7207 bool CSGInterface::cmd_tic()
7213 bool CSGInterface::cmd_toc()
7219 bool CSGInterface::cmd_print()
7221 if (m_nrhs<2 || !create_return_values(0))
7225 char* msg=get_str_from_str_or_direct(len);
7233 bool CSGInterface::cmd_echo()
7235 if (m_nrhs<2 || !create_return_values(0))
7239 char* level=get_str_from_str_or_direct(len);
7241 if (strmatch(level,
"OFF"))
7256 bool CSGInterface::cmd_loglevel()
7258 if (m_nrhs<2 || !create_return_values(0))
7262 char* level=get_str_from_str_or_direct(len);
7264 if (strmatch(level,
"ALL") || strmatch(level,
"GCDEBUG"))
7266 else if (strmatch(level,
"DEBUG"))
7268 else if (strmatch(level,
"INFO"))
7270 else if (strmatch(level,
"NOTICE"))
7272 else if (strmatch(level,
"WARN"))
7274 else if (strmatch(level,
"ERROR"))
7276 else if (strmatch(level,
"CRITICAL"))
7278 else if (strmatch(level,
"ALERT"))
7280 else if (strmatch(level,
"EMERGENCY"))
7283 SG_ERROR(
"Unknown loglevel '%s'.\n", level);
7285 SG_INFO(
"Loglevel set to %s.\n", level);
7291 bool CSGInterface::cmd_syntax_highlight()
7293 if (m_nrhs<2 || !create_return_values(0))
7297 char* hili=get_str_from_str_or_direct(len);
7299 if (strmatch(hili,
"ON"))
7302 io->enable_syntax_highlighting();
7304 else if (strmatch(hili,
"OFF"))
7307 io->disable_syntax_highlighting();
7312 SG_INFO(
"Syntax hilighting set to %s.\n", hili);
7318 bool CSGInterface::cmd_progress()
7320 if (m_nrhs<2 || !create_return_values(0))
7324 char* progress=get_str_from_str_or_direct(len);
7326 if (strmatch(progress,
"ON"))
7327 io->enable_progress();
7328 else if (strmatch(progress,
"OFF"))
7329 io->disable_progress();
7331 SG_ERROR(
"arguments to progress are ON|OFF - found '%s'.\n", progress);
7333 SG_INFO(
"Progress set to %s.\n", progress);
7339 bool CSGInterface::cmd_get_version()
7341 if (m_nrhs!=1 || !create_return_values(1))
7344 set_int(version->get_version_revision());
7349 bool CSGInterface::cmd_help()
7351 if ((m_nrhs!=1 && m_nrhs!=2) || !create_return_values(0))
7359 SG_PRINT(
"Help is available for the following topics.\n"
7360 "-------------------------------------------\n\n");
7363 bool is_group_item=
false;
7377 SG_PRINT(
"\nUse sg('%shelp%s', '%s<topic>%s')"
7378 " to see the list of commands in this group, e.g.\n\n"
7379 "\tsg('%shelp%s', '%sFeatures%s')\n\n"
7380 "to see the list of commands for the 'Features' group.\n"
7381 "\nOr use sg('%shelp%s', '%sall%s')"
7382 " to see a brief listing of all commands.\n\nTo disable syntax"
7383 " highlighting (useful e.g. in the matlab GUI) use\n\n"
7384 "\tsg('syntax_highlight','OFF')\n",
7395 bool in_group=
false;
7397 char* command=get_string(clen);
7399 if (strmatch(
"doxygen", command) || strmatch(
"DOXYGEN", command))
7406 SG_PRINT(
"\\arg \\b %s \\verbatim %s%s%s \\endverbatim\n",
7414 SG_PRINT(
"\n\\section %s_sec %s\n",
7420 if (strmatch(
"all", command) || strmatch(
"ALL", command))
7435 SG_PRINT(
"\nCommands in group %s%s%s\n",
7459 found=strmatch(
sg_methods[i].command, command);
7464 SG_PRINT(
"Usage for %s%s%s\n\n\t%s%s%s%s%s\n",
7477 SG_PRINT(
"Commands in group %s%s%s\n\n",
7491 SG_PRINT(
"Could not find help for command %s.\n", command);
7494 SG_PRINT(
"\n\nUse sg('%shelp%s', '%s<command>%s')"
7495 " to see the usage pattern of a single command, e.g.\n\n"
7496 "\tsg('%shelp%s', '%sclassify%s')\n\n"
7497 " to see the usage pattern of the command 'classify'.\n",
7512 #ifdef TRACE_MEMORY_ALLOCS
7516 bool CSGInterface::cmd_whos()
7518 if ((m_nrhs!=1) || !create_return_values(0))
7521 #ifdef TRACE_MEMORY_ALLOCS
7522 SG_PRINT(
"Blocks allocated by shogun\n");
7523 list_memory_allocs();
7527 SG_PRINT(
"Requires shogun to be compiled with --enable-trace-mallocs\n");
7532 bool CSGInterface::cmd_send_command()
7537 char* arg=get_string(len);
7539 m_legacy_strptr=arg;
7541 char* command=get_str_from_str(len);
7547 if (strmatch(command,
sg_methods[i].command))
7551 m_nrhs=get_num_args_in_str()+1;
7555 SG_ERROR(
"Usage: %s%s%s\n\n\t%s%s%s%s%s\n",
7576 SG_ERROR(
"Non-supported legacy command %s.\n", command);
7583 bool CSGInterface::cmd_run_python()
7585 SG_ERROR(
"Only available in the elwms interface\n");
7589 bool CSGInterface::cmd_run_octave()
7591 SG_ERROR(
"Only available in the elwms interface\n");
7595 bool CSGInterface::cmd_run_r()
7597 SG_ERROR(
"Only available in the elwms interface\n");
7601 bool CSGInterface::cmd_pr_loqo()
7603 if (m_nrhs!=7 || !create_return_values(2))
7608 get_vector(c, lenc);
7615 get_matrix(H, nH, mH);
7621 get_matrix(A, nA, mA);
7627 get_vector(b, lenb);
7632 get_vector(l, lenl);
7637 get_vector(u, lenu);
7646 pr_loqo(n,m, c, H, A, b, l, u, x, y, 0, 5, 50, 0.05, 100, 0);
7662 void CSGInterface::print_prompt()
7673 char* CSGInterface::get_str_from_str_or_direct(int32_t& len)
7675 if (m_legacy_strptr)
7676 return get_str_from_str(len);
7678 return get_string(len);
7681 int32_t CSGInterface::get_int_from_int_or_str()
7683 if (m_legacy_strptr)
7686 char* str=get_str_from_str(len);
7687 int32_t val=strtol(str, NULL, 10);
7696 float64_t CSGInterface::get_real_from_real_or_str()
7698 if (m_legacy_strptr)
7701 char* str=get_str_from_str(len);
7711 bool CSGInterface::get_bool_from_bool_or_str()
7713 if (m_legacy_strptr)
7716 char* str=get_str_from_str(len);
7717 bool val=strtol(str, NULL, 10)!=0;
7726 void CSGInterface::get_vector_from_int_vector_or_str(int32_t*& vector, int32_t& len)
7728 if (m_legacy_strptr)
7730 len=get_vector_len_from_str(len);
7740 for (int32_t i=0; i<len; i++)
7742 str=get_str_from_str(slen);
7743 vector[i]=strtol(str, NULL, 10);
7749 get_vector(vector, len);
7752 void CSGInterface::get_vector_from_real_vector_or_str(
7755 if (m_legacy_strptr)
7757 len=get_vector_len_from_str(len);
7767 for (int32_t i=0; i<len; i++)
7769 str=get_str_from_str(slen);
7770 vector[i]=strtod(str, NULL);
7776 get_vector(vector, len);
7779 int32_t CSGInterface::get_vector_len_from_str(int32_t expected_len)
7781 int32_t num_args=get_num_args_in_str();
7783 if (expected_len==0 || num_args==expected_len)
7785 else if (num_args==2*expected_len)
7788 return expected_len;
7791 SG_ERROR(
"Expected vector length %d does not match actual length %d.\n", expected_len, num_args);
7796 char* CSGInterface::get_str_from_str(int32_t& len)
7798 if (!m_legacy_strptr)
7802 while (m_legacy_strptr[i]!=
'\0' && !isspace(m_legacy_strptr[i]))
7807 for (i=0; i<len; i++)
7808 str[i]=m_legacy_strptr[i];
7812 if (m_legacy_strptr[len]==
'\0')
7813 m_legacy_strptr=NULL;
7816 m_legacy_strptr=m_legacy_strptr+len;
7823 int32_t CSGInterface::get_num_args_in_str()
7825 if (!m_legacy_strptr)
7831 while (m_legacy_strptr[i]!=
'\0')
7833 if (!isspace(m_legacy_strptr[i]) && !in_arg)
7838 else if (isspace(m_legacy_strptr[i]) && in_arg)
7851 bool CSGInterface::handle()
7863 SG_DEBUG(
"command: %s, nrhs %d\n", command, m_nrhs);
7867 if (strmatch(command,
sg_methods[i].command))
7878 SG_ERROR(
"Usage: %s%s%s\n\n\t%s%s%s%s%s\n",
7889 SG_ERROR(
"Non-supported command %s%s%s.\n",
7908 SG_ERROR(
"Unknown command %s%s%s.\n",