16 #include <shogun/lib/external/pr_loqo.h>
43 using namespace shogun;
48 #if defined(HAVE_CMDLINE)
49 #define USAGE(method) "", ""
50 #define USAGE_I(method, in) "", " " in ""
51 #define USAGE_O(method, out) "" out " = ", ""
52 #define USAGE_IO(method, in, out) "" out " = ", " " in ""
53 #define USAGE_COMMA " "
56 #define USAGE(method) "sg('", "')"
57 #define USAGE_I(method, in) "sg('", "', " in ")"
58 #define USAGE_O(method, out) "[" out "] <- sg('", "')"
59 #define USAGE_IO(method, in, out) "[" out "] <- sg('", "', " in ")"
60 #define USAGE_COMMA ", "
63 #define USAGE(method) "sg('", "')"
64 #define USAGE_I(method, in) "sg('", "', " in ")"
65 #define USAGE_O(method, out) "[" out "]=sg('", "')"
66 #define USAGE_IO(method, in, out) "[" out "]=sg('", "', " in ")"
67 #define USAGE_COMMA ", "
73 {
"Features", NULL, NULL, NULL },
76 (&CSGInterface::cmd_pr_loqo),
78 "'Var1', Var1, 'Var2', Var2",
"results")
82 (&CSGInterface::cmd_load_features),
88 (&CSGInterface::cmd_save_features),
93 (&CSGInterface::cmd_clean_features),
98 (&CSGInterface::cmd_get_features),
103 (&CSGInterface::cmd_add_features),
109 (&CSGInterface::cmd_add_multiple_features),
115 (&CSGInterface::cmd_add_dotfeatures),
121 (&CSGInterface::cmd_set_features),
133 (&CSGInterface::cmd_set_reference_features),
138 (&CSGInterface::cmd_del_last_features),
143 (&CSGInterface::cmd_convert),
156 (&CSGInterface::cmd_reshape),
163 (&CSGInterface::cmd_load_labels),
169 (&CSGInterface::cmd_set_labels),
175 (&CSGInterface::cmd_get_labels),
180 {
"Kernel", NULL, NULL },
183 (&CSGInterface::cmd_set_kernel_normalization),
189 (&CSGInterface::cmd_set_kernel),
194 (&CSGInterface::cmd_add_kernel),
199 (&CSGInterface::cmd_del_last_kernel),
204 (&CSGInterface::cmd_init_kernel),
209 (&CSGInterface::cmd_clean_kernel),
214 (&CSGInterface::cmd_save_kernel),
219 (&CSGInterface::cmd_get_kernel_matrix),
224 (&CSGInterface::cmd_set_WD_position_weights),
229 (&CSGInterface::cmd_get_subkernel_weights),
234 (&CSGInterface::cmd_set_subkernel_weights),
239 (&CSGInterface::cmd_set_subkernel_weights_combined),
244 (&CSGInterface::cmd_get_dotfeature_weights_combined),
249 (&CSGInterface::cmd_set_dotfeature_weights_combined),
254 (&CSGInterface::cmd_set_last_subkernel_weights),
259 (&CSGInterface::cmd_get_WD_position_weights),
264 (&CSGInterface::cmd_get_last_subkernel_weights),
269 (&CSGInterface::cmd_compute_by_subkernels),
274 (&CSGInterface::cmd_init_kernel_optimization),
279 (&CSGInterface::cmd_get_kernel_optimization),
284 (&CSGInterface::cmd_delete_kernel_optimization),
289 (&CSGInterface::cmd_use_diagonal_speedup),
294 (&CSGInterface::cmd_set_kernel_optimization_type),
299 (&CSGInterface::cmd_set_solver),
304 (&CSGInterface::cmd_set_constraint_generator),
306 "|SVMLIGHT|LIGHT|SVMLIGHT_ONECLASS|GPBTSVM|MPDSVM|GNPPSVM|GMNPSVM"
311 (&CSGInterface::cmd_set_prior_probs),
316 (&CSGInterface::cmd_set_prior_probs_from_labels),
322 (&CSGInterface::cmd_resize_kernel_cache),
325 #endif //USE_SVMLIGHT
328 {
"Distance", NULL, NULL },
331 (&CSGInterface::cmd_set_distance),
336 (&CSGInterface::cmd_init_distance),
341 (&CSGInterface::cmd_get_distance_matrix),
346 {
"Classifier", NULL, NULL },
349 (&CSGInterface::cmd_classify),
354 (&CSGInterface::cmd_classify),
359 (&CSGInterface::cmd_classify_example),
364 (&CSGInterface::cmd_classify_example),
369 (&CSGInterface::cmd_get_classifier),
374 (&CSGInterface::cmd_get_classifier),
379 (&CSGInterface::cmd_new_classifier),
381 "|SVMLIGHT|LIGHT|LIGHT_ONECLASS|SVMLIN|GPBTSVM|MPDSVM|GNPPSVM|GMNPSVM"
382 "|WDSVMOCAS|SVMOCAS|SVMSGD|SVMBMRM|SVMPERF"
383 "|KERNELPERCEPTRON|PERCEPTRON|LIBLINEAR_LR|LIBLINEAR_L2|LDA"
388 (&CSGInterface::cmd_new_classifier),
390 "|LIBSVM|SVMLIGHT|LIGHT|LIGHT_ONECLASS|SVMLIN|GPBTSVM|MPDSVM|GNPPSVM|GMNPSVM"
391 "|WDSVMOCAS|SVMOCAS|SVMSGD|SVMBMRM|SVMPERF"
392 "|KERNELPERCEPTRON|PERCEPTRON|LIBLINEAR_LR|LIBLINEAR_L2|LDA"
397 (&CSGInterface::cmd_new_classifier),
402 (&CSGInterface::cmd_new_classifier),
407 (&CSGInterface::cmd_load_classifier),
412 (&CSGInterface::cmd_save_classifier),
417 (&CSGInterface::cmd_get_num_svms),
422 (&CSGInterface::cmd_get_svm),
427 (&CSGInterface::cmd_set_svm),
432 (&CSGInterface::cmd_set_linear_classifier),
437 (&CSGInterface::cmd_get_svm_objective),
442 (&CSGInterface::cmd_compute_svm_primal_objective),
447 (&CSGInterface::cmd_compute_svm_dual_objective),
452 (&CSGInterface::cmd_compute_svm_primal_objective),
457 (&CSGInterface::cmd_compute_mkl_dual_objective),
462 (&CSGInterface::cmd_compute_relative_mkl_duality_gap),
467 (&CSGInterface::cmd_compute_absolute_mkl_duality_gap),
472 (&CSGInterface::cmd_do_auc_maximization),
477 (&CSGInterface::cmd_set_perceptron_parameters),
482 (&CSGInterface::cmd_train_classifier),
487 (&CSGInterface::cmd_train_classifier),
492 (&CSGInterface::cmd_train_classifier),
497 (&CSGInterface::cmd_train_classifier),
502 (&CSGInterface::cmd_set_svm_qpsize),
507 (&CSGInterface::cmd_set_svm_max_qpsize),
512 (&CSGInterface::cmd_set_svm_bufsize),
517 (&CSGInterface::cmd_set_svm_C),
522 (&CSGInterface::cmd_set_svm_epsilon),
527 (&CSGInterface::cmd_set_svr_tube_epsilon),
532 (&CSGInterface::cmd_set_svm_nu),
537 (&CSGInterface::cmd_set_svm_mkl_parameters),
542 (&CSGInterface::cmd_set_elasticnet_lambda),
547 (&CSGInterface::cmd_set_mkl_block_norm),
552 (&CSGInterface::cmd_set_max_train_time),
557 (&CSGInterface::cmd_set_svm_shrinking_enabled),
562 (&CSGInterface::cmd_set_svm_batch_computation_enabled),
567 (&CSGInterface::cmd_set_svm_linadd_enabled),
572 (&CSGInterface::cmd_set_svm_bias_enabled),
577 (&CSGInterface::cmd_set_mkl_interleaved_enabled),
582 (&CSGInterface::cmd_set_krr_tau),
587 {
"Preprocessors", NULL, NULL },
590 (&CSGInterface::cmd_add_preproc),
595 (&CSGInterface::cmd_del_preproc),
600 (&CSGInterface::cmd_attach_preproc),
605 (&CSGInterface::cmd_clean_preproc),
609 {
"Converters", NULL, NULL },
612 (&CSGInterface::cmd_set_converter),
617 (&CSGInterface::cmd_apply_converter),
622 (&CSGInterface::cmd_embed),
627 {
"HMM", NULL, NULL },
630 (&CSGInterface::cmd_new_hmm),
635 (&CSGInterface::cmd_load_hmm),
640 (&CSGInterface::cmd_save_hmm),
645 (&CSGInterface::cmd_get_hmm),
650 (&CSGInterface::cmd_append_hmm),
655 (&CSGInterface::cmd_append_model),
660 (&CSGInterface::cmd_set_hmm),
665 (&CSGInterface::cmd_set_hmm_as),
670 (&CSGInterface::cmd_set_chop),
675 (&CSGInterface::cmd_set_pseudo),
680 (&CSGInterface::cmd_load_definitions),
685 (&CSGInterface::cmd_hmm_classify),
690 (&CSGInterface::cmd_one_class_linear_hmm_classify),
695 (&CSGInterface::cmd_one_class_hmm_classify),
700 (&CSGInterface::cmd_one_class_hmm_classify_example),
705 (&CSGInterface::cmd_hmm_classify_example),
710 (&CSGInterface::cmd_output_hmm),
715 (&CSGInterface::cmd_output_hmm_defined),
720 (&CSGInterface::cmd_hmm_likelihood),
725 (&CSGInterface::cmd_likelihood),
730 (&CSGInterface::cmd_save_likelihood),
735 (&CSGInterface::cmd_get_viterbi_path),
740 (&CSGInterface::cmd_viterbi_train_defined),
745 (&CSGInterface::cmd_viterbi_train),
750 (&CSGInterface::cmd_baum_welch_train),
755 (&CSGInterface::cmd_baum_welch_train_defined),
760 (&CSGInterface::cmd_baum_welch_trans_train),
765 (&CSGInterface::cmd_linear_train),
770 (&CSGInterface::cmd_save_path),
775 (&CSGInterface::cmd_convergence_criteria),
780 (&CSGInterface::cmd_normalize),
785 (&CSGInterface::cmd_add_states),
790 (&CSGInterface::cmd_permutation_entropy),
795 (&CSGInterface::cmd_relative_entropy),
800 (&CSGInterface::cmd_entropy),
805 (&CSGInterface::cmd_set_feature_matrix),
810 (&CSGInterface::cmd_set_feature_matrix_sparse),
815 (&CSGInterface::cmd_new_plugin_estimator),
820 (&CSGInterface::cmd_train_estimator),
825 (&CSGInterface::cmd_plugin_estimate_classify_example),
830 (&CSGInterface::cmd_plugin_estimate_classify),
835 (&CSGInterface::cmd_set_plugin_estimate),
840 (&CSGInterface::cmd_get_plugin_estimate),
843 {
"Signals", NULL, NULL },
846 (&CSGInterface::cmd_signals_set_model),
851 (&CSGInterface::cmd_signals_set_positions),
856 (&CSGInterface::cmd_signals_set_labels),
861 (&CSGInterface::cmd_signals_set_split),
866 (&CSGInterface::cmd_signals_set_train_mask),
871 (&CSGInterface::cmd_signals_add_feature),
876 (&CSGInterface::cmd_signals_add_kernel),
881 (&CSGInterface::cmd_signals_run),
884 {
"Structure", NULL, NULL },
887 (&CSGInterface::cmd_best_path),
892 (&CSGInterface::cmd_best_path_2struct),
904 "prob" USAGE_COMMA
"path" USAGE_COMMA
"pos")
908 (&CSGInterface::cmd_set_plif_struct),
921 (&CSGInterface::cmd_get_plif_struct),
934 (&CSGInterface::cmd_precompute_subkernels),
939 (&CSGInterface::cmd_precompute_content_svms),
946 (&CSGInterface::cmd_get_lin_feat),
951 (&CSGInterface::cmd_set_lin_feat),
956 (&CSGInterface::cmd_init_dyn_prog),
961 (&CSGInterface::cmd_clean_up_dyn_prog),
966 (&CSGInterface::cmd_init_intron_list),
973 (&CSGInterface::cmd_precompute_tiling_features),
980 (&CSGInterface::cmd_long_transition_settings),
988 (&CSGInterface::cmd_set_model),
997 (&CSGInterface::cmd_best_path_trans),
1004 "prob" USAGE_COMMA
"path" USAGE_COMMA
"pos")
1008 (&CSGInterface::cmd_best_path_trans_deriv),
1025 USAGE_COMMA
"q_deriv"
1026 USAGE_COMMA
"cmd_deriv"
1027 USAGE_COMMA
"penalties_deriv"
1028 USAGE_COMMA
"my_scores"
1029 USAGE_COMMA
"my_loss")
1032 {
"POIM", NULL, NULL },
1035 (&CSGInterface::cmd_compute_POIM_WD),
1040 (&CSGInterface::cmd_get_SPEC_consensus),
1045 (&CSGInterface::cmd_get_SPEC_scoring),
1050 (&CSGInterface::cmd_get_WD_consensus),
1055 (&CSGInterface::cmd_get_WD_scoring),
1060 {
"Utility", NULL, NULL },
1063 (&CSGInterface::cmd_crc),
1068 (&CSGInterface::cmd_system),
1073 (&CSGInterface::cmd_exit),
1078 (&CSGInterface::cmd_exit),
1083 (&CSGInterface::cmd_exec),
1088 (&CSGInterface::cmd_set_output),
1093 (&CSGInterface::cmd_set_threshold),
1098 (&CSGInterface::cmd_init_random),
1103 (&CSGInterface::cmd_set_num_threads),
1108 (&CSGInterface::cmd_translate_string),
1110 "string, order, start",
"translation")
1114 (&CSGInterface::cmd_clear),
1119 (&CSGInterface::cmd_tic),
1124 (&CSGInterface::cmd_toc),
1129 (&CSGInterface::cmd_print),
1134 (&CSGInterface::cmd_echo),
1139 (&CSGInterface::cmd_loglevel),
1144 (&CSGInterface::cmd_syntax_highlight),
1149 (&CSGInterface::cmd_progress),
1154 (&CSGInterface::cmd_get_version),
1159 (&CSGInterface::cmd_help),
1164 (&CSGInterface::cmd_whos),
1169 (&CSGInterface::cmd_send_command),
1174 (&CSGInterface::cmd_run_python),
1176 "'Var1', Var1, 'Var2', Var2,..., python_function",
"results")
1180 (&CSGInterface::cmd_run_octave),
1182 "'Var1', Var1, 'Var2', Var2,..., octave_function",
"results")
1186 (&CSGInterface::cmd_run_r),
1188 "'Var1', Var1, 'Var2', Var2,..., r_function",
"results")
1194 CSGInterface::CSGInterface(
bool print_copyright)
1210 if (print_copyright)
1212 version->print_version();
1213 SG_PRINT(
"( seeding random number generator with %u (seed size %d))\n",
1216 SG_PRINT(
"initializing log-table (size=%i*%i*%i=%2.1fMB) ... ) ",
1227 CSGInterface::~CSGInterface()
1229 delete ui_classifier;
1231 delete ui_pluginestimate;
1237 delete ui_structure;
1241 delete ui_converter;
1247 void CSGInterface::reset()
1253 m_legacy_strptr=NULL;
1258 void CSGInterface::translate_arg(CSGInterface* source, CSGInterface* target)
1260 switch (source->get_argument_type())
1263 target->set_int(source->get_int());
1266 target->set_real(source->get_real());
1269 target->set_bool(source->get_bool());
1275 source->get_vector(v, len);
1276 target->set_vector(v, len);
1284 source->get_vector(v, len);
1285 target->set_vector(v, len);
1293 source->get_vector(v, len);
1294 target->set_vector(v, len);
1302 source->get_vector(v, len);
1303 target->set_vector(v, len);
1311 source->get_vector(v, len);
1312 target->set_vector(v, len);
1316 case VECTOR_SHORTREAL:
1320 source->get_vector(v, len);
1321 target->set_vector(v, len);
1329 source->get_vector(v, len);
1330 target->set_vector(v, len);
1338 source->get_vector(v, len);
1339 target->set_vector(v, len);
1347 int32_t max_str_len=0;
1349 source->get_string_list(strs, num_str, max_str_len);
1350 target->set_string_list(strs, num_str);
1357 int32_t max_str_len=0;
1359 source->get_string_list(strs, num_str,max_str_len);
1360 target->set_string_list(strs, num_str);
1367 int32_t max_str_len=0;
1369 source->get_string_list(strs, num_str,max_str_len);
1370 target->set_string_list(strs, num_str);
1377 int32_t max_str_len=0;
1379 source->get_string_list(strs, num_str, max_str_len);
1380 target->set_string_list(strs, num_str);
1387 int32_t max_str_len=0;
1389 source->get_string_list(strs, num_str, max_str_len);
1390 target->set_string_list(strs, num_str);
1398 int32_t* fmatrix=NULL;
1399 source->get_matrix(fmatrix, num_feat, num_vec);
1400 target->set_matrix(fmatrix, num_feat, num_vec);
1409 source->get_matrix(fmatrix, num_feat, num_vec);
1410 target->set_matrix(fmatrix, num_feat, num_vec);
1418 int16_t* fmatrix=NULL;
1419 source->get_matrix(fmatrix, num_feat, num_vec);
1420 target->set_matrix(fmatrix, num_feat, num_vec);
1424 case DENSE_SHORTREAL:
1429 source->get_matrix(fmatrix, num_feat, num_vec);
1430 target->set_matrix(fmatrix, num_feat, num_vec);
1438 uint16_t* fmatrix=NULL;
1439 source->get_matrix(fmatrix, num_feat, num_vec);
1440 target->set_matrix(fmatrix, num_feat, num_vec);
1527 source->get_sparse_matrix(fmatrix, num_feat, num_vec);
1529 for (int32_t i=0; i<num_vec; i++)
1530 nnz+=fmatrix[i].num_feat_entries;
1531 target->set_sparse_matrix(fmatrix, num_feat, num_vec, nnz);
1548 bool CSGInterface::cmd_load_features()
1550 if (m_nrhs<8 || !create_return_values(0))
1554 char* filename=get_str_from_str_or_direct(len);
1555 char* fclass=get_str_from_str_or_direct(len);
1556 char* type=get_str_from_str_or_direct(len);
1557 char* target=get_str_from_str_or_direct(len);
1558 int32_t size=get_int_from_int_or_str();
1559 int32_t comp_features=get_int_from_int_or_str();
1561 bool success=ui_features->load(
1562 filename, fclass, type, target, size, comp_features);
1571 bool CSGInterface::cmd_save_features()
1573 if (m_nrhs<5 || !create_return_values(0))
1577 char* filename=get_str_from_str_or_direct(len);
1578 char* type=get_str_from_str_or_direct(len);
1579 char* target=get_str_from_str_or_direct(len);
1581 bool success=ui_features->save(filename, type, target);
1589 bool CSGInterface::cmd_clean_features()
1591 if (m_nrhs<2 || !create_return_values(0))
1595 char* target=get_str_from_str_or_direct(len);
1597 bool success=ui_features->clean(target);
1603 bool CSGInterface::cmd_get_features()
1605 if (m_nrhs!=2 || !create_return_values(1))
1609 char* target=get_string(tlen);
1612 if (strmatch(target,
"TRAIN"))
1613 feat=ui_features->get_train_features();
1614 else if (strmatch(target,
"TEST"))
1615 feat=ui_features->get_test_features();
1619 SG_ERROR(
"Unknown target, neither TRAIN nor TEST.\n")
1637 set_matrix(fmatrix, num_feat, num_vec);
1644 set_matrix(fmatrix, num_feat, num_vec);
1651 set_matrix(fmatrix, num_feat, num_vec);
1658 set_matrix(fmatrix, num_feat, num_vec);
1665 set_matrix(fmatrix, num_feat, num_vec);
1672 set_matrix(fmatrix, num_feat, num_vec);
1679 set_matrix(fmatrix, num_feat, num_vec);
1696 get_num_nonzero_entries();
1713 int32_t max_str_len=0;
1719 set_string_list(fmatrix, num_str);
1726 set_string_list(fmatrix, num_str);
1733 set_string_list(fmatrix, num_str);
1762 bool CSGInterface::cmd_add_features()
1764 if (m_nrhs<3 || !create_return_values(0))
1767 return do_set_features(
true,
false);
1770 bool CSGInterface::cmd_add_multiple_features()
1772 if ((m_nrhs!=4 && m_nrhs<5) || !create_return_values(0))
1775 int32_t repetitions=get_int();
1779 return do_set_features(
true,
false, repetitions);
1782 bool CSGInterface::cmd_add_dotfeatures()
1784 if (m_nrhs<3 || !create_return_values(0))
1787 return do_set_features(
true,
true);
1790 bool CSGInterface::cmd_set_features()
1792 if (m_nrhs<3 || !create_return_values(0))
1795 return do_set_features(
false,
false);
1798 bool CSGInterface::do_set_features(
bool add,
bool check_dot, int32_t repetitions)
1801 char* target=get_string(tlen);
1802 if (!strmatch(target,
"TRAIN") && !strmatch(target,
"TEST"))
1805 SG_ERROR(
"Unknown target, neither TRAIN nor TEST.\n")
1812 switch (get_argument_type())
1817 get_sparse_matrix(fmatrix, num_feat, num_vec);
1826 get_matrix(fmatrix, num_feat, num_vec);
1840 int32_t* fmatrix=NULL;
1841 get_matrix(fmatrix, num_feat, num_vec);
1851 int16_t* fmatrix=NULL;
1852 get_matrix(fmatrix, num_feat, num_vec);
1862 uint16_t* fmatrix=NULL;
1863 get_matrix(fmatrix, num_feat, num_vec);
1871 case DENSE_SHORTREAL:
1874 get_matrix(fmatrix, num_feat, num_vec);
1885 SG_ERROR(
"Please specify alphabet!\n")
1888 int32_t max_str_len=0;
1890 get_string_list(fmatrix, num_str, max_str_len);
1892 int32_t alphabet_len=0;
1893 char* alphabet_str=get_string(alphabet_len);
1896 if (strmatch(alphabet_str,
"DNABINFILE"))
1898 SG_FREE(alphabet_str);
1900 ASSERT(fmatrix[0].
string)
1910 SG_ERROR(
"Couldn't load DNA features from file.\n")
1917 bool convert_to_word=
false;
1918 bool convert_to_ulong=
false;
1920 if (strmatch(alphabet_str,
"DNAWORD"))
1923 convert_to_word=
true;
1925 else if (strmatch(alphabet_str,
"DNAULONG"))
1928 convert_to_ulong=
true;
1931 alphabet=
new CAlphabet(alphabet_str, alphabet_len);
1934 SG_FREE(alphabet_str);
1942 SG_ERROR(
"Couldnt set byte string features.\n")
1947 if (convert_to_word || convert_to_ulong)
1948 convert_to_bitembedding(feat, convert_to_word, convert_to_ulong);
1951 obtain_from_single_string(feat);
1958 SG_ERROR(
"Please specify alphabet!\n")
1961 int32_t max_str_len=0;
1963 get_string_list(fmatrix, num_str, max_str_len);
1965 int32_t alphabet_len=0;
1966 char* alphabet_str=get_string(alphabet_len);
1969 alphabet=
new CAlphabet(alphabet_str, alphabet_len);
1970 SG_FREE(alphabet_str);
1977 SG_ERROR(
"Couldnt set byte string features.\n")
1984 SG_ERROR(
"Wrong argument type %d.\n", get_argument_type())
1990 SG_ERROR(
"Feature type not supported by DOT Features\n")
1993 if (strmatch(target,
"TRAIN"))
1996 ui_features->set_train_features(feat);
1999 for (int32_t i=0; i<repetitions; i++)
2000 ui_features->add_train_dotfeatures((
CDotFeatures*) feat);
2004 for (int32_t i=0; i<repetitions; i++)
2005 ui_features->add_train_features(feat);
2011 ui_features->set_test_features(feat);
2014 for (int32_t i=0; i<repetitions; i++)
2015 ui_features->add_test_dotfeatures((
CDotFeatures*) feat);
2019 for (int32_t i=0; i<repetitions; i++)
2020 ui_features->add_test_features(feat);
2029 bool CSGInterface::cmd_set_reference_features()
2031 if (m_nrhs<3 || !create_return_values(0))
2035 char* target=get_str_from_str_or_direct(len);
2037 bool success=ui_features->set_reference_features(target);
2043 bool CSGInterface::cmd_del_last_features()
2045 if (m_nrhs<2 || !create_return_values(0))
2049 char* target=get_str_from_str_or_direct(len);
2050 bool success=ui_features->del_last_feature_obj(target);
2056 bool CSGInterface::cmd_convert()
2058 if (m_nrhs<5 || !create_return_values(0))
2062 char* target=get_str_from_str_or_direct(len);
2063 CFeatures* features=ui_features->get_convert_features(target);
2067 SG_ERROR(
"No \"%s\" features available.\n", target)
2070 char* from_class=get_str_from_str_or_direct(len);
2071 char* from_type=get_str_from_str_or_direct(len);
2072 char* to_class=get_str_from_str_or_direct(len);
2073 char* to_type=get_str_from_str_or_direct(len);
2076 if (strmatch(from_class,
"SIMPLE"))
2078 if (strmatch(from_type,
"REAL"))
2080 if (strmatch(to_class,
"SPARSE") &&
2081 strmatch(to_type,
"REAL"))
2083 result=ui_features->convert_simple_real_to_sparse_real(
2090 else if (strmatch(from_type,
"CHAR"))
2092 if (strmatch(to_class,
"STRING") &&
2093 strmatch(to_type,
"CHAR"))
2095 result=ui_features->convert_simple_char_to_string_char(
2098 else if (strmatch(to_class,
"SIMPLE"))
2100 if (strmatch(to_type,
"ALIGN") && m_nrhs==8)
2102 float64_t gap_cost=get_real_from_real_or_str();
2103 result=ui_features->convert_simple_char_to_simple_align(
2113 else if (strmatch(from_type,
"WORD"))
2115 if (strmatch(to_class,
"SIMPLE") &&
2116 strmatch(to_type,
"SALZBERG"))
2118 result=ui_features->convert_simple_word_to_simple_salzberg(
2129 else if (strmatch(from_class,
"SPARSE"))
2131 if (strmatch(from_type,
"REAL"))
2133 if (strmatch(to_class,
"SIMPLE") &&
2134 strmatch(to_type,
"REAL"))
2136 result=ui_features->convert_sparse_real_to_simple_real(
2146 else if (strmatch(from_class,
"STRING"))
2148 if (strmatch(from_type,
"CHAR"))
2150 if (strmatch(to_class,
"STRING"))
2159 order=get_int_from_int_or_str();
2163 start=get_int_from_int_or_str();
2167 gap=get_int_from_int_or_str();
2171 char* rev_str=get_str_from_str_or_direct(len);
2181 if (strmatch(to_type,
"BYTE"))
2183 result=ui_features->convert_string_char_to_string_generic<char,uint8_t>(
2187 else if (strmatch(to_type,
"WORD"))
2189 result=ui_features->convert_string_char_to_string_generic<char,uint16_t>(
2193 else if (strmatch(to_type,
"ULONG"))
2195 result=ui_features->convert_string_char_to_string_generic<char,uint64_t>(
2206 else if (strmatch(from_type,
"BYTE"))
2208 if (strmatch(to_class,
"STRING"))
2217 order=get_int_from_int_or_str();
2221 start=get_int_from_int_or_str();
2225 gap=get_int_from_int_or_str();
2229 char* rev_str=get_str_from_str_or_direct(len);
2239 if (strmatch(to_type,
"WORD"))
2241 result=ui_features->convert_string_char_to_string_generic<uint8_t,uint16_t>(
2245 else if (strmatch(to_type,
"ULONG"))
2247 result=ui_features->convert_string_char_to_string_generic<uint8_t,uint64_t>(
2258 else if (strmatch(from_type,
"WORD"))
2260 if (strmatch(to_class,
"SIMPLE") && strmatch(to_type,
"TOP"))
2262 result=ui_features->convert_string_word_to_simple_top(
2265 else if (strmatch(to_class,
"SPEC") && strmatch(to_type,
"WORD") && m_nrhs==7)
2267 bool use_norm=get_bool();
2275 else if (strmatch(to_class,
"SIMPLE") && strmatch(to_type,
"FK"))
2277 result=ui_features->convert_string_word_to_simple_fk(
2286 if (result && ui_features->set_convert_features(result, target))
2287 SG_INFO(
"Conversion was successful.\n")
2292 SG_FREE(from_class);
2296 return (result!=NULL);
2299 void CSGInterface::convert_to_bitembedding(
CFeatures* &features,
bool convert_to_word,
bool convert_to_ulong)
2313 if (convert_to_word)
2315 SG_INFO(
"Converting into word-bitembedding\n")
2316 features=ui_features->convert_string_char_to_string_generic<char,uint16_t>(
2320 if (convert_to_ulong)
2322 SG_INFO(
"Converting into ulong-bitembedding\n")
2323 features=ui_features->convert_string_char_to_string_generic<char,uint64_t>(
2328 void CSGInterface::obtain_from_single_string(
CFeatures* features)
2334 char* str=get_string(len);
2337 if (strmatch(str,
"from_position_list"))
2339 obtain_from_position_list(features);
2341 else if (strmatch(str,
"slide_window"))
2343 obtain_by_sliding_window(features);
2349 bool CSGInterface::obtain_from_position_list(
CFeatures* features)
2351 int32_t winsize=get_int();
2353 int32_t* shifts=NULL;
2354 int32_t num_shift=0;
2355 get_vector(shifts, num_shift);
2361 SG_DEBUG(
"winsize: %d num_shifts: %d skip: %d\n", winsize, num_shift, skip)
2365 for (int32_t i=0; i<num_shift; i++)
2377 obtain_by_position_list(winsize, &positions, skip)>0);
2383 obtain_by_position_list(winsize, &positions, skip)>0);
2389 obtain_by_position_list(winsize, &positions, skip)>0);
2395 obtain_by_position_list(winsize, &positions, skip)>0);
2399 SG_ERROR(
"Unsupported string features type.\n")
2405 bool CSGInterface::obtain_by_sliding_window(
CFeatures* features)
2407 int32_t winsize=get_int();
2408 int32_t shift=get_int();
2430 SG_SERROR(
"Unsupported string features type.\n")
2437 bool CSGInterface::cmd_reshape()
2439 if (m_nrhs<4 || !create_return_values(0))
2443 char* target=get_str_from_str_or_direct(len);
2444 int32_t num_feat=get_int_from_int_or_str();
2445 int32_t num_vec=get_int_from_int_or_str();
2447 bool success=ui_features->reshape(target, num_feat, num_vec);
2453 bool CSGInterface::cmd_load_labels()
2455 if (m_nrhs<4 || !create_return_values(0))
2459 char* filename=get_str_from_str_or_direct(len);
2460 char* target=get_str_from_str_or_direct(len);
2462 bool success=ui_labels->load(filename, target);
2469 bool CSGInterface::cmd_set_labels()
2471 if (m_nrhs!=3 || !create_return_values(0))
2475 char* target=get_string(tlen);
2476 if (!strmatch(target,
"TRAIN") && !strmatch(target,
"TEST"))
2479 SG_ERROR(
"Unknown target, neither TRAIN nor TEST.\n")
2484 get_vector(lab, len);
2486 CLabels* labels=ui_labels->infer_labels(lab, len);
2490 if (strmatch(target,
"TRAIN"))
2491 ui_labels->set_train_labels(labels);
2492 else if (strmatch(target,
"TEST"))
2493 ui_labels->set_test_labels(labels);
2497 SG_ERROR(
"Unknown target, neither TRAIN nor TEST.\n")
2504 bool CSGInterface::cmd_get_labels()
2506 if (m_nrhs!=2 || !create_return_values(1))
2510 char* target=get_string(tlen);
2513 if (strmatch(target,
"TRAIN"))
2514 labels=ui_labels->get_train_labels();
2515 else if (strmatch(target,
"TEST"))
2516 labels=ui_labels->get_test_labels();
2520 SG_ERROR(
"Unknown target, neither TRAIN nor TEST.\n")
2537 bool CSGInterface::cmd_set_kernel_normalization()
2539 if (m_nrhs<2 || !create_return_values(0))
2543 char* normalization=get_string(len);
2553 bool success=ui_kernel->set_normalization(normalization, c, r);
2555 SG_FREE(normalization);
2559 bool CSGInterface::cmd_set_kernel()
2561 if (m_nrhs<2 || !create_return_values(0))
2564 SG_DEBUG(
"SGInterface: set_kernel\n")
2565 CKernel* kernel=create_kernel();
2566 return ui_kernel->set_kernel(kernel);
2569 bool CSGInterface::cmd_add_kernel()
2571 if (m_nrhs<3 || !create_return_values(0))
2574 float64_t weight=get_real_from_real_or_str();
2577 CKernel* kernel=create_kernel();
2579 SG_DEBUG(
"SGInterface: add_kernel\n")
2580 return ui_kernel->add_kernel(kernel, weight);
2583 bool CSGInterface::cmd_del_last_kernel()
2585 if (m_nrhs<1 || !create_return_values(0))
2588 return ui_kernel->del_last_kernel();
2591 CKernel* CSGInterface::create_kernel()
2595 char* type=get_str_from_str_or_direct(len);
2597 SG_DEBUG(
"set_kernel with type: %s\n", type)
2599 if (strmatch(type,
"COMBINED"))
2604 int32_t size=get_int_from_int_or_str();
2605 bool append_subkernel_weights=
false;
2607 append_subkernel_weights=get_bool_from_bool_or_str();
2609 kernel=ui_kernel->create_combined(size, append_subkernel_weights);
2611 else if (strmatch(type,
"DISTANCE"))
2616 int32_t size=get_int_from_int_or_str();
2619 width=get_real_from_real_or_str();
2621 kernel=ui_kernel->create_distance(size, width);
2623 else if (strmatch(type,
"WAVELET"))
2629 char* dtype=get_str_from_str_or_direct(len);
2630 if (strmatch(dtype,
"REAL"))
2632 int32_t size=get_int_from_int_or_str();
2638 Wdilation=get_real_from_real_or_str();
2641 Wtranslation=get_real_from_real_or_str();
2644 kernel=ui_kernel->create_sigmoid(size, Wdilation, Wtranslation);
2649 else if (strmatch(type,
"LINEAR"))
2656 char* dtype=get_str_from_str_or_direct(len);
2657 int32_t size=get_int_from_int_or_str();
2660 scale=get_real_from_real_or_str();
2662 if (strmatch(dtype,
"BYTE"))
2663 kernel=ui_kernel->create_linearbyte(size, scale);
2664 else if (strmatch(dtype,
"WORD"))
2665 kernel=ui_kernel->create_linearword(size, scale);
2666 else if (strmatch(dtype,
"CHAR"))
2667 kernel=ui_kernel->create_linearstring(size, scale);
2668 else if (strmatch(dtype,
"REAL"))
2669 kernel=ui_kernel->create_linear(size, scale);
2670 else if (strmatch(dtype,
"SPARSEREAL"))
2671 kernel=ui_kernel->create_sparselinear(size, scale);
2675 else if (strmatch(type,
"HISTOGRAM"))
2680 char* dtype=get_str_from_str_or_direct(len);
2681 if (strmatch(dtype,
"WORD"))
2683 int32_t size=get_int_from_int_or_str();
2684 kernel=ui_kernel->create_histogramword(size);
2689 else if (strmatch(type,
"SALZBERG"))
2694 char* dtype=get_str_from_str_or_direct(len);
2695 if (strmatch(dtype,
"WORD"))
2697 int32_t size=get_int_from_int_or_str();
2698 kernel=ui_kernel->create_salzbergword(size);
2703 else if (strmatch(type,
"POLYMATCH"))
2708 char* dtype=get_str_from_str_or_direct(len);
2709 int32_t size=get_int_from_int_or_str();
2711 bool inhomogene=
false;
2712 bool normalize=
true;
2716 degree=get_int_from_int_or_str();
2719 inhomogene=get_bool_from_bool_or_str();
2721 normalize=get_bool_from_bool_or_str();
2725 if (strmatch(dtype,
"CHAR"))
2727 kernel=ui_kernel->create_polymatchstring(
2728 size, degree, inhomogene, normalize);
2730 else if (strmatch(dtype,
"WORD"))
2732 kernel=ui_kernel->create_polymatchwordstring(
2733 size, degree, inhomogene, normalize);
2738 else if (strmatch(type,
"MATCH"))
2743 char* dtype=get_str_from_str_or_direct(len);
2744 if (strmatch(dtype,
"WORD"))
2746 int32_t size=get_int_from_int_or_str();
2748 bool normalize=
true;
2751 d=get_int_from_int_or_str();
2753 normalize=get_bool_from_bool_or_str();
2755 kernel=ui_kernel->create_matchwordstring(size, d, normalize);
2760 else if (strmatch(type,
"WEIGHTEDCOMMSTRING") || strmatch(type,
"COMMSTRING"))
2762 char* dtype=get_str_from_str_or_direct(len);
2763 int32_t size=get_int_from_int_or_str();
2764 bool use_sign=
false;
2765 char* norm_str=NULL;
2769 use_sign=get_bool_from_bool_or_str();
2772 norm_str=get_str_from_str_or_direct(len);
2775 if (strmatch(dtype,
"WORD"))
2777 if (strmatch(type,
"WEIGHTEDCOMMSTRING"))
2779 kernel=ui_kernel->create_commstring(
2782 else if (strmatch(type,
"COMMSTRING"))
2784 kernel=ui_kernel->create_commstring(
2788 else if (strmatch(dtype,
"ULONG"))
2790 kernel=ui_kernel->create_commstring(
2797 else if (strmatch(type,
"CHI2"))
2802 char* dtype=get_str_from_str_or_direct(len);
2803 if (strmatch(dtype,
"REAL"))
2805 int32_t size=get_int_from_int_or_str();
2809 width=get_real_from_real_or_str();
2811 kernel=ui_kernel->create_chi2(size, width);
2816 else if (strmatch(type,
"FIXEDDEGREE"))
2821 char* dtype=get_str_from_str_or_direct(len);
2822 if (strmatch(dtype,
"CHAR"))
2824 int32_t size=get_int_from_int_or_str();
2827 d=get_int_from_int_or_str();
2829 kernel=ui_kernel->create_fixeddegreestring(size, d);
2834 else if (strmatch(type,
"LOCALALIGNMENT"))
2839 char* dtype=get_str_from_str_or_direct(len);
2840 if (strmatch(dtype,
"CHAR"))
2842 int32_t size=get_int_from_int_or_str();
2844 kernel=ui_kernel->create_localalignmentstring(size);
2849 else if (strmatch(type,
"OLIGO"))
2854 char* dtype=get_str_from_str_or_direct(len);
2855 if (strmatch(dtype,
"CHAR"))
2857 int32_t size=get_int_from_int_or_str();
2858 int32_t k=get_int_from_int_or_str();
2859 float64_t w=get_real_from_real_or_str();
2861 kernel=ui_kernel->create_oligo(size, k, w);
2866 else if (strmatch(type,
"WEIGHTEDDEGREEPOS2") ||
2867 strmatch(type,
"WEIGHTEDDEGREEPOS2_NONORM"))
2872 char* dtype=get_str_from_str_or_direct(len);
2873 if (strmatch(dtype,
"CHAR") || strmatch(dtype,
"STRING"))
2875 int32_t size=get_int_from_int_or_str();
2876 int32_t order=get_int_from_int_or_str();
2877 int32_t max_mismatch=get_int_from_int_or_str();
2878 int32_t length=get_int_from_int_or_str();
2879 int32_t* shifts=NULL;
2881 get_vector_from_int_vector_or_str(shifts, l);
2885 bool use_normalization=
true;
2886 if (strmatch(type,
"WEIGHTEDDEGREEPOS2_NONORM"))
2887 use_normalization=
false;
2889 kernel=ui_kernel->create_weighteddegreepositionstring2(
2890 size, order, max_mismatch, shifts, length,
2898 else if (strmatch(type,
"WEIGHTEDDEGREEPOS3"))
2903 char* dtype=get_str_from_str_or_direct(len);
2904 if (strmatch(dtype,
"CHAR") || strmatch(dtype,
"STRING"))
2906 int32_t size=get_int_from_int_or_str();
2907 int32_t order=get_int_from_int_or_str();
2908 int32_t max_mismatch=get_int_from_int_or_str();
2909 int32_t length=get_int_from_int_or_str();
2910 int32_t mkl_stepsize=get_int_from_int_or_str();
2911 int32_t* shifts=NULL;
2913 get_vector_from_int_vector_or_str(shifts, l);
2917 if (m_nrhs>9+length)
2919 get_vector_from_real_vector_or_str(
2920 position_weights, length);
2923 kernel=ui_kernel->create_weighteddegreepositionstring3(
2924 size, order, max_mismatch, shifts, length,
2925 mkl_stepsize, position_weights);
2927 SG_FREE(position_weights);
2933 else if (strmatch(type,
"WEIGHTEDDEGREEPOS"))
2938 char* dtype=get_str_from_str_or_direct(len);
2939 if (strmatch(dtype,
"CHAR") || strmatch(dtype,
"STRING"))
2941 int32_t size=get_int_from_int_or_str();
2943 int32_t max_mismatch=0;
2950 order=get_int_from_int_or_str();
2954 max_mismatch=get_int_from_int_or_str();
2958 length=get_int_from_int_or_str();
2962 center=get_int_from_int_or_str();
2965 step=get_real_from_real_or_str();
2971 kernel=ui_kernel->create_weighteddegreepositionstring(
2972 size, order, max_mismatch, length, center, step);
2977 else if (strmatch(type,
"WEIGHTEDDEGREE"))
2982 char* dtype=get_str_from_str_or_direct(len);
2983 if (strmatch(dtype,
"CHAR") || strmatch(dtype,
"STRING"))
2985 int32_t size=get_int_from_int_or_str();
2987 int32_t max_mismatch=0;
2988 bool use_normalization=
true;
2989 int32_t mkl_stepsize=1;
2990 bool block_computation=
true;
2991 int32_t single_degree=-1;
2995 order=get_int_from_int_or_str();
2999 max_mismatch=get_int_from_int_or_str();
3003 use_normalization=get_bool_from_bool_or_str();
3007 mkl_stepsize=get_int_from_int_or_str();
3011 block_computation=get_int_from_int_or_str();
3014 single_degree=get_int_from_int_or_str();
3021 kernel=ui_kernel->create_weighteddegreestring(
3022 size, order, max_mismatch, use_normalization,
3023 mkl_stepsize, block_computation, single_degree);
3028 else if (strmatch(type,
"WEIGHTEDDEGREERBF"))
3033 char* dtype=get_str_from_str_or_direct(len);
3034 int32_t size=get_int_from_int_or_str();
3035 int32_t nof_properties=get_int_from_int_or_str();
3040 degree=get_int_from_int_or_str();
3043 width=get_real_from_real_or_str();
3049 kernel=ui_kernel->create_weighteddegreerbf(size, degree, nof_properties, width);
3054 else if (strmatch(type,
"SPECTRUMMISMATCHRBF"))
3059 char* dtype=get_str_from_str_or_direct(len);
3060 if (strmatch(dtype,
"CHAR") || strmatch(dtype,
"STRING"))
3062 int32_t size=get_int_from_int_or_str();
3063 int32_t degree=get_int_from_int_or_str();
3064 int32_t max_mismatch=get_int_from_int_or_str();
3065 float64_t width=get_real_from_real_or_str();
3073 get_matrix(helper_matrix, N, M);
3075 if (N == 128 && M == 128)
3078 memcpy(AA_matrix, helper_matrix, N*M*
sizeof(
float64_t)) ;
3079 kernel=ui_kernel->create_spectrummismatchrbf(size, AA_matrix, 128, 128, max_mismatch, degree, width);
3083 SG_ERROR(
"Matrix size %d %d\n", N, M)
3090 else if (strmatch(type,
"SLIK") || strmatch(type,
"LIK"))
3095 char* dtype=get_str_from_str_or_direct(len);
3096 if (strmatch(dtype,
"CHAR"))
3098 int32_t size=get_int_from_int_or_str();
3100 int32_t inner_degree=3;
3101 int32_t outer_degree=1;
3105 length=get_int_from_int_or_str();
3109 inner_degree=get_int_from_int_or_str();
3112 outer_degree=get_int_from_int_or_str();
3116 if (strmatch(type,
"SLIK"))
3118 kernel=ui_kernel->create_localityimprovedstring(
3119 size, length, inner_degree, outer_degree,
3124 kernel=ui_kernel->create_localityimprovedstring(
3125 size, length, inner_degree, outer_degree,
3132 else if (strmatch(type,
"POLY"))
3137 char* dtype=get_str_from_str_or_direct(len);
3138 int32_t size=get_int_from_int_or_str();
3140 bool inhomogene=
false;
3141 bool normalize=
true;
3145 degree=get_int_from_int_or_str();
3149 inhomogene=get_bool_from_bool_or_str();
3152 normalize=get_bool_from_bool_or_str();
3156 if (strmatch(dtype,
"REAL"))
3158 kernel=ui_kernel->create_poly(
3159 size, degree, inhomogene, normalize);
3161 else if (strmatch(dtype,
"SPARSEREAL"))
3163 kernel=ui_kernel->create_sparsepoly(
3164 size, degree, inhomogene, normalize);
3169 else if (strmatch(type,
"SIGMOID"))
3174 char* dtype=get_str_from_str_or_direct(len);
3175 if (strmatch(dtype,
"REAL"))
3177 int32_t size=get_int_from_int_or_str();
3183 gamma=get_real_from_real_or_str();
3186 coef0=get_real_from_real_or_str();
3189 kernel=ui_kernel->create_sigmoid(size, gamma, coef0);
3194 else if (strmatch(type,
"GAUSSIAN"))
3199 char* dtype=get_str_from_str_or_direct(len);
3200 int32_t size=get_int_from_int_or_str();
3203 width=get_real_from_real_or_str();
3205 if (strmatch(dtype,
"REAL"))
3206 kernel=ui_kernel->create_gaussian(size, width);
3207 else if (strmatch(dtype,
"SPARSEREAL"))
3208 kernel=ui_kernel->create_sparsegaussian(size, width);
3212 else if (strmatch(type,
"GAUSSIANSHIFT"))
3217 char* dtype=get_str_from_str_or_direct(len);
3218 if (strmatch(dtype,
"REAL"))
3220 int32_t size=get_int_from_int_or_str();
3221 float64_t width=get_real_from_real_or_str();
3222 int32_t max_shift=get_int_from_int_or_str();
3223 int32_t shift_step=get_int_from_int_or_str();
3225 kernel=ui_kernel->create_gaussianshift(
3226 size, width, max_shift, shift_step);
3231 else if (strmatch(type,
"CUSTOM"))
3233 if (m_nrhs!=4 || !create_return_values(0))
3239 get_matrix(kmatrix, num_feat, num_vec);
3242 char* ktype=get_string(tlen);
3244 if (!strmatch(ktype,
"DIAG") &&
3245 !strmatch(ktype,
"FULL") &&
3246 !strmatch(ktype,
"FULL2DIAG"))
3249 SG_ERROR(
"Undefined type, not DIAG, FULL or FULL2DIAG.\n")
3252 bool source_is_diag=
false;
3253 bool dest_is_diag=
false;
3255 if (strmatch(ktype,
"FULL2DIAG"))
3257 else if (strmatch(ktype,
"DIAG"))
3259 source_is_diag=
true;
3263 kernel=ui_kernel->create_custom(kmatrix, num_feat, num_vec,
3264 source_is_diag, dest_is_diag);
3266 else if (strmatch(type,
"CONST"))
3271 char* dtype=get_str_from_str_or_direct(len);
3272 if (strmatch(dtype,
"REAL"))
3274 int32_t size=get_int_from_int_or_str();
3277 c=get_real_from_real_or_str();
3279 kernel=ui_kernel->create_const(size, c);
3284 else if (strmatch(type,
"DIAG"))
3289 char* dtype=get_str_from_str_or_direct(len);
3290 if (strmatch(dtype,
"REAL"))
3292 int32_t size=get_int_from_int_or_str();
3295 diag=get_real_from_real_or_str();
3297 kernel=ui_kernel->create_diag(size, diag);
3303 else if (strmatch(type,
"TPPK"))
3308 char* dtype=get_str_from_str_or_direct(len);
3309 if (strmatch(dtype,
"INT"))
3311 int32_t size=get_int_from_int_or_str();
3315 get_matrix(km, rows, cols);
3316 kernel=ui_kernel->create_tppk(size, km, rows, cols);
3325 SG_DEBUG(
"created kernel: %p\n", kernel)
3338 int32_t from_order=0;
3339 bool normalize=
true;
3341 int32_t feature_class_len=0;
3342 char* feature_class_str=get_string(feature_class_len);
3343 ASSERT(feature_class_str)
3345 if (strmatch(feature_class_str,
"WD"))
3348 SG_ERROR(
"Please specify alphabet, WD, order, from_order\n")
3352 from_order=get_int();
3355 else if (strmatch(feature_class_str,
"WSPEC"))
3358 SG_ERROR(
"Please specify alphabet, order, WSPEC, start, normalize\n")
3363 normalize=get_bool();
3371 SG_FREE(feature_class_str);
3386 int32_t feature_class_len=0;
3388 char* feature_class_str=get_string(feature_class_len);
3389 ASSERT(feature_class_str)
3390 if (strmatch(feature_class_str,
"POLY"))
3396 normalize = get_bool();
3401 SG_ERROR(
"Unknown feature class: %s\n", feature_class_str)
3403 SG_FREE(feature_class_str);
3409 bool CSGInterface::cmd_init_kernel()
3415 bool CSGInterface::cmd_clean_kernel()
3417 if (m_nrhs<1 || !create_return_values(0))
3420 return ui_kernel->clean_kernel();
3423 bool CSGInterface::cmd_save_kernel()
3425 if (m_nrhs<2 || !create_return_values(0))
3429 char* filename=get_str_from_str_or_direct(len);
3431 bool success=ui_kernel->save_kernel(filename);
3437 bool CSGInterface::cmd_get_kernel_matrix()
3439 if (m_nrhs>2 || !create_return_values(1))
3446 target=get_string(len);
3447 bool success=ui_kernel->init_kernel(target);
3451 CKernel* kernel=ui_kernel->get_kernel();
3453 SG_ERROR(
"No kernel defined or not initialized.\n")
3464 bool CSGInterface::cmd_set_WD_position_weights()
3466 if (m_nrhs<2 || m_nrhs>3 || !create_return_values(0))
3469 CKernel* kernel=ui_kernel->get_kernel();
3488 get_matrix(weights, dim, len);
3495 if (dim!=1 && len>0)
3496 SG_ERROR(
"Dimension mismatch (should be 1 x seq_length or 0x0\n")
3498 ui_kernel->init_kernel(
"TRAIN");
3511 target=get_string(tlen);
3515 SG_ERROR(
"Couldn't find second argument to method.\n")
3518 if (!strmatch(target,
"TRAIN") && !strmatch(target,
"TEST"))
3521 SG_ERROR(
"Second argument none of TRAIN or TEST.\n")
3524 if (strmatch(target,
"TEST"))
3528 if (dim!=1 && len>0)
3531 SG_ERROR(
"Dimension mismatch (should be 1 x seq_length or 0x0\n")
3534 if (dim==0 && len==0)
3536 if (create_return_values(3))
3548 if (create_return_values(3))
3557 ui_kernel->init_kernel(
"TRAIN");
3569 bool CSGInterface::cmd_get_subkernel_weights()
3571 if (m_nrhs!=1 || !create_return_values(1))
3574 CKernel *kernel=ui_kernel->get_kernel();
3583 int32_t num_weights=-1;
3584 weights=((
CCombinedKernel *) kernel)->get_subkernel_weights(num_weights);
3587 set_matrix(weights, 1, num_weights);
3597 get_degree_weights(degree, length);
3602 get_degree_weights(degree, length);
3605 SG_ERROR(
"Setting subkernel weights not supported on this kernel.\n")
3610 set_matrix(weights, degree, length);
3614 bool CSGInterface::cmd_set_subkernel_weights()
3616 if (m_nrhs!=2 || !create_return_values(0))
3619 CKernel* kernel=ui_kernel->get_kernel();
3627 get_matrix(weights, dim, len);
3635 if (dim!=degree || len<1)
3636 SG_ERROR(
"WD: Dimension mismatch (should be (seq_length | 1) x degree) got (%d x %d)\n", len, degree)
3648 if (dim!=degree || len<1)
3649 SG_ERROR(
"WDPos: Dimension mismatch (should be (seq_length | 1) x degree) got (%d x %d)\n", len, degree)
3659 if (dim!=1 || len!=num_subkernels)
3660 SG_ERROR(
"All: Dimension mismatch (should be 1 x num_subkernels)\n")
3669 bool CSGInterface::cmd_set_subkernel_weights_combined()
3671 if (m_nrhs!=3 || !create_return_values(0))
3674 CKernel* kernel=ui_kernel->get_kernel();
3678 SG_ERROR(
"Only works for combined kernels.\n")
3684 get_matrix(weights, dim, len);
3686 int32_t idx=get_int();
3687 SG_DEBUG(
"using kernel_idx=%i\n", idx)
3691 SG_ERROR(
"No subkernel at idx %d.\n", idx)
3699 if (dim!=degree || len<1)
3700 SG_ERROR(
"Dimension mismatch (should be de(seq_length | 1) x degree)\n")
3712 if (dim!=degree || len<1)
3713 SG_ERROR(
"Dimension mismatch (should be de(seq_length | 1) x degree)\n")
3723 if (dim!=1 || len!=num_subkernels)
3724 SG_ERROR(
"Dimension mismatch (should be 1 x num_subkernels)\n")
3733 bool CSGInterface::cmd_get_dotfeature_weights_combined()
3735 if (m_nrhs!=2 || !create_return_values(1))
3739 char* target=get_string(tlen);
3742 if (strmatch(target,
"TRAIN"))
3743 features=ui_features->get_train_features();
3744 else if (strmatch(target,
"TEST"))
3745 features=ui_features->get_test_features();
3749 SG_ERROR(
"Unknown target, neither TRAIN nor TEST.\n")
3756 SG_ERROR(
"Only works for combined dot features.\n")
3764 bool CSGInterface::cmd_set_dotfeature_weights_combined()
3766 if (m_nrhs!=3 || !create_return_values(0))
3770 char* target=get_string(tlen);
3773 if (strmatch(target,
"TRAIN"))
3774 features=ui_features->get_train_features();
3775 else if (strmatch(target,
"TEST"))
3776 features=ui_features->get_test_features();
3780 SG_ERROR(
"Unknown target, neither TRAIN nor TEST.\n")
3787 SG_ERROR(
"Only works for combined dot features.\n")
3792 get_matrix(weights, dim, len);
3799 bool CSGInterface::cmd_set_last_subkernel_weights()
3801 if (m_nrhs!=2 || !create_return_values(0))
3804 CKernel* kernel=ui_kernel->get_kernel();
3808 SG_ERROR(
"Only works for Combined kernels.\n")
3818 get_matrix(weights, dim, len);
3825 SG_ERROR(
"Dimension mismatch (should be de(seq_length | 1) x degree)\n")
3837 SG_ERROR(
"Dimension mismatch (should be de(seq_length | 1) x degree)\n")
3847 if (dim!=1 || len!=num_subkernels)
3848 SG_ERROR(
"Dimension mismatch (should be 1 x num_subkernels)\n")
3857 bool CSGInterface::cmd_get_WD_position_weights()
3859 if (m_nrhs!=1 || !create_return_values(1))
3862 CKernel* kernel=ui_kernel->get_kernel();
3870 SG_ERROR(
"Couldn't find last kernel.\n")
3874 SG_ERROR(
"Wrong subkernel type.\n")
3885 if (position_weights==NULL)
3886 set_vector(position_weights, 0);
3888 set_vector(position_weights, len);
3893 bool CSGInterface::cmd_get_last_subkernel_weights()
3895 if (m_nrhs!=1 || !create_return_values(1))
3898 CKernel* kernel=ui_kernel->get_kernel();
3903 SG_ERROR(
"Only works for Combined kernels.\n")
3907 SG_ERROR(
"Couldn't find last kernel.\n")
3914 int32_t num_weights=0;
3918 set_vector(weights, num_weights);
3925 get_degree_weights(degree, len);
3928 get_degree_weights(degree, len);
3930 SG_ERROR(
"Only works for Weighted Degree (Position) kernels.\n")
3935 set_matrix(weights, degree, len);
3940 bool CSGInterface::cmd_compute_by_subkernels()
3942 if (m_nrhs!=1 || !create_return_values(1))
3945 CKernel* kernel=ui_kernel->get_kernel();
3962 SG_ERROR(
"Kernel optimization not initialized.\n")
3970 SG_ERROR(
"Kernel optimization not initialized.\n")
3973 SG_ERROR(
"Only works for Weighted Degree (Position) kernels.\n")
3978 int32_t num_feat=degree*len;
3979 int32_t num=num_feat*num_vec;
3982 for (int32_t i=0; i<num; i++)
3988 for (int32_t i=0; i<num_vec; i++)
3995 for (int32_t i=0; i<num_vec; i++)
3999 set_matrix(result, num_feat, num_vec);
4005 bool CSGInterface::cmd_init_kernel_optimization()
4007 if (m_nrhs<1 || !create_return_values(0))
4010 return ui_kernel->init_kernel_optimization();
4013 bool CSGInterface::cmd_get_kernel_optimization()
4015 if (m_nrhs<1 || !create_return_values(1))
4018 CKernel* kernel=ui_kernel->get_kernel();
4029 int32_t max_order=get_int();
4030 if ((max_order<1) || (max_order>12))
4032 SG_WARNING(
"max_order out of range 1..12 (%d). setting to 1\n", max_order)
4037 CSVM* svm=(
CSVM*) ui_classifier->get_classifier();
4042 int32_t* sv_idx=SG_MALLOC(int32_t, num_suppvec);
4047 for (int32_t i=0; i<num_suppvec; i++)
4054 num_sym, NULL, num_suppvec, sv_idx, sv_weight);
4058 set_matrix(position_weights, num_sym, num_feat);
4059 SG_FREE(position_weights);
4072 set_vector(weights, len);
4090 bool CSGInterface::cmd_delete_kernel_optimization()
4092 if (m_nrhs<1 || !create_return_values(0))
4095 return ui_kernel->delete_kernel_optimization();
4098 bool CSGInterface::cmd_use_diagonal_speedup()
4100 if (m_nrhs<2 || !create_return_values(0))
4103 bool speedup=get_bool();
4105 CKernel* kernel=ui_kernel->get_kernel();
4111 SG_DEBUG(
"Identified combined kernel.\n")
4114 SG_ERROR(
"No last kernel defined.\n")
4118 SG_ERROR(
"Currently only commwordstring kernel supports diagonal speedup\n")
4122 SG_INFO(
"Diagonal speedup %s.\n", speedup ?
"enabled" :
"disabled")
4127 bool CSGInterface::cmd_set_kernel_optimization_type()
4129 if (m_nrhs<2 || !create_return_values(0))
4133 char* opt_type=get_str_from_str_or_direct(len);
4135 bool success=ui_kernel->set_optimization_type(opt_type);
4141 bool CSGInterface::cmd_set_solver()
4143 if (m_nrhs<2 || !create_return_values(0))
4147 char* solver=get_str_from_str_or_direct(len);
4149 bool success=ui_classifier->set_solver(solver);
4155 bool CSGInterface::cmd_set_constraint_generator()
4157 if (m_nrhs<2 || !create_return_values(0))
4161 char* cg=get_str_from_str_or_direct(len);
4163 bool success=ui_classifier->set_constraint_generator(cg);
4169 bool CSGInterface::cmd_set_prior_probs()
4171 if (m_nrhs<3 || !create_return_values(0))
4177 SG_ERROR(
"SalzbergWordStringKernel required for setting prior probs!\n")
4179 float64_t pos_probs=get_real_from_real_or_str();
4180 float64_t neg_probs=get_real_from_real_or_str();
4187 bool CSGInterface::cmd_set_prior_probs_from_labels()
4189 if (m_nrhs<2 || !create_return_values(0))
4195 SG_ERROR(
"SalzbergWordStringKernel required for setting prior probs!\n")
4199 get_vector(lab, len);
4202 for (int32_t i=0; i<len; i++)
4205 SG_ERROR(
"Couldn't set label %d (of %d): %f.\n", i, len, lab[i])
4216 bool CSGInterface::cmd_resize_kernel_cache()
4218 if (m_nrhs<2 || !create_return_values(0))
4221 int32_t size=get_int_from_int_or_str();
4222 return ui_kernel->resize_kernel_cache(size);
4224 #endif //USE_SVMLIGHT
4229 bool CSGInterface::cmd_set_distance()
4231 if (m_nrhs<3 || !create_return_values(0))
4236 char* type=get_str_from_str_or_direct(len);
4237 char* dtype=get_str_from_str_or_direct(len);
4239 if (strmatch(type,
"MINKOWSKI") && m_nrhs==4)
4241 float64_t k=get_real_from_real_or_str();
4242 distance=ui_distance->create_minkowski(k);
4244 else if (strmatch(type,
"MANHATTAN"))
4246 if (strmatch(dtype,
"REAL"))
4247 distance=ui_distance->create_generic(
D_MANHATTAN);
4248 else if (strmatch(dtype,
"WORD"))
4251 else if (strmatch(type,
"HAMMING") && strmatch(dtype,
"WORD"))
4253 bool use_sign=
false;
4255 use_sign=get_bool_from_bool_or_str();
4257 distance=ui_distance->create_hammingword(use_sign);
4259 else if (strmatch(type,
"CANBERRA"))
4261 if (strmatch(dtype,
"REAL"))
4262 distance=ui_distance->create_generic(
D_CANBERRA);
4263 else if (strmatch(dtype,
"WORD"))
4266 else if (strmatch(type,
"CHEBYSHEW") && strmatch(dtype,
"REAL"))
4268 distance=ui_distance->create_generic(
D_CHEBYSHEW);
4270 else if (strmatch(type,
"GEODESIC") && strmatch(dtype,
"REAL"))
4272 distance=ui_distance->create_generic(
D_GEODESIC);
4274 else if (strmatch(type,
"JENSEN") && strmatch(dtype,
"REAL"))
4276 distance=ui_distance->create_generic(
D_JENSEN);
4278 else if (strmatch(type,
"CHISQUARE") && strmatch(dtype,
"REAL"))
4280 distance=ui_distance->create_generic(
D_CHISQUARE);
4282 else if (strmatch(type,
"TANIMOTO") && strmatch(dtype,
"REAL"))
4284 distance=ui_distance->create_generic(
D_TANIMOTO);
4286 else if (strmatch(type,
"COSINE") && strmatch(dtype,
"REAL"))
4288 distance=ui_distance->create_generic(
D_COSINE);
4290 else if (strmatch(type,
"BRAYCURTIS") && strmatch(dtype,
"REAL"))
4294 else if (strmatch(type,
"EUCLIDEAN"))
4296 if (strmatch(dtype,
"REAL"))
4297 distance=ui_distance->create_generic(
D_EUCLIDEAN);
4298 else if (strmatch(dtype,
"SPARSEREAL"))
4306 return ui_distance->set_distance(distance);
4309 bool CSGInterface::cmd_init_distance()
4315 bool CSGInterface::cmd_get_distance_matrix()
4317 if (m_nrhs!=2 || !create_return_values(1))
4321 char* target=get_string(len);
4323 bool success=ui_distance->init_distance(target);
4329 SG_ERROR(
"No distance defined or not initialized.\n")
4341 bool CSGInterface::cmd_get_SPEC_consensus()
4343 if (m_nrhs!=1 || !create_return_values(1))
4346 CKernel* kernel=ui_kernel->get_kernel();
4350 SG_ERROR(
"Only works for CommWordString kernels.\n")
4352 CSVM* svm=(
CSVM*) ui_classifier->get_classifier();
4355 int32_t* sv_idx=SG_MALLOC(int32_t, num_suppvec);
4359 for (int32_t i=0; i<num_suppvec; i++)
4366 num_feat, num_suppvec, sv_idx, sv_weight);
4370 set_vector(consensus, num_feat);
4376 bool CSGInterface::cmd_get_SPEC_scoring()
4378 if (m_nrhs!=2 || !create_return_values(1))
4381 int32_t max_order=get_int();
4382 CKernel* kernel=ui_kernel->get_kernel();
4388 SG_ERROR(
"Only works for (Weighted) CommWordString kernels.\n")
4390 CSVM* svm=(
CSVM*) ui_classifier->get_classifier();
4393 int32_t* sv_idx=SG_MALLOC(int32_t, num_suppvec);
4398 for (int32_t i=0; i<num_suppvec; i++)
4404 if ((max_order<1) || (max_order>8))
4406 SG_WARNING(
"max_order out of range 1..8 (%d). setting to 1\n", max_order)
4413 max_order, num_feat, num_sym, NULL,
4414 num_suppvec, sv_idx, sv_weight);
4417 max_order, num_feat, num_sym, NULL,
4418 num_suppvec, sv_idx, sv_weight);
4422 set_matrix(position_weights, num_sym, num_feat);
4423 SG_FREE(position_weights);
4428 bool CSGInterface::cmd_get_WD_consensus()
4430 if (m_nrhs!=1 || !create_return_values(1))
4433 CKernel* kernel=ui_kernel->get_kernel();
4437 SG_ERROR(
"Only works for Weighted Degree Position kernels.\n")
4439 CSVM* svm=(
CSVM*) ui_classifier->get_classifier();
4442 int32_t* sv_idx=SG_MALLOC(int32_t, num_suppvec);
4446 for (int32_t i=0; i<num_suppvec; i++)
4453 num_feat, num_suppvec, sv_idx, sv_weight);
4457 set_vector(consensus, num_feat);
4463 bool CSGInterface::cmd_compute_POIM_WD()
4465 if (m_nrhs!=3 || !create_return_values(1))
4468 int32_t max_order=get_int();
4470 int32_t num_dfeat=0;
4472 get_matrix(distribution, num_dfeat, num_dvec);
4477 CKernel* kernel=ui_kernel->get_kernel();
4481 SG_ERROR(
"Only works for Weighted Degree Position kernels.\n")
4491 if (num_dvec!=seqlen || num_dfeat!=num_sym)
4493 SG_ERROR(
"distribution should have (seqlen x num_sym) elements"
4494 "(seqlen: %d vs. %d symbols: %d vs. %d)\n", seqlen,
4495 num_dvec, num_sym, num_dfeat);
4498 CSVM* svm=(
CSVM*) ui_classifier->get_classifier();
4501 int32_t* sv_idx=SG_MALLOC(int32_t, num_suppvec);
4504 for (int32_t i=0; i<num_suppvec; i++)
4520 max_order, seqlen, num_sym, NULL,
4521 num_suppvec, sv_idx, sv_weight, distribution);
4525 set_matrix(position_weights, num_sym, seqlen);
4526 SG_FREE(position_weights);
4531 bool CSGInterface::cmd_get_WD_scoring()
4533 if (m_nrhs!=2 || !create_return_values(1))
4536 int32_t max_order=get_int();
4538 CKernel* kernel=ui_kernel->get_kernel();
4542 SG_ERROR(
"Only works for Weighted Degree Position kernels.\n")
4544 CSVM* svm=(
CSVM*) ui_classifier->get_classifier();
4547 int32_t* sv_idx=SG_MALLOC(int32_t, num_suppvec);
4552 for (int32_t i=0; i<num_suppvec; i++)
4558 if ((max_order<1) || (max_order>12))
4560 SG_WARNING(
"max_order out of range 1..12 (%d). setting to 1\n", max_order)
4566 max_order, num_feat, num_sym, NULL, num_suppvec, sv_idx, sv_weight);
4570 set_matrix(position_weights, num_sym, num_feat);
4571 SG_FREE(position_weights);
4579 bool CSGInterface::cmd_classify()
4581 if (m_nrhs!=1 || !create_return_values(1))
4584 if (!ui_kernel->get_kernel() ||
4585 ui_kernel->get_kernel()->get_kernel_type()!=
K_CUSTOM)
4587 CFeatures* feat=ui_features->get_test_features();
4592 CLabels* labels=ui_classifier->classify();
4598 for (int32_t i=0; i<num_vec; i++)
4620 set_vector(result, num_vec);
4626 bool CSGInterface::cmd_classify_example()
4628 if (m_nrhs!=2 || !create_return_values(1))
4631 int32_t idx=get_int();
4634 if (!ui_classifier->classify_example(idx, result))
4635 SG_ERROR(
"Classify_example failed.\n")
4642 bool CSGInterface::cmd_get_classifier()
4644 if (m_nrhs<1 || m_nrhs>2 || !create_return_values(2))
4658 if (!ui_classifier->get_trained_classifier(
4659 weights, rows, cols, bias, brows, bcols, idx))
4664 set_matrix(bias, brows, bcols);
4669 set_matrix(weights, rows, cols);
4675 bool CSGInterface::cmd_new_classifier()
4677 if (m_nrhs<2 || !create_return_values(0))
4681 char* name=get_str_from_str_or_direct(len);
4687 d=get_int_from_int_or_str();
4690 from_d=get_int_from_int_or_str();
4693 bool success=ui_classifier->new_classifier(name, d, from_d);
4699 bool CSGInterface::cmd_save_classifier()
4701 if (m_nrhs<2 || !create_return_values(0))
4705 char* filename=get_str_from_str_or_direct(len);
4707 bool success=ui_classifier->save(filename);
4713 bool CSGInterface::cmd_load_classifier()
4715 if (m_nrhs<3 || !create_return_values(0))
4719 char* filename=get_str_from_str_or_direct(len);
4720 char* type=get_str_from_str_or_direct(len);
4722 bool success=ui_classifier->load(filename, type);
4724 if (dynamic_cast<CKernelMachine*>(ui_classifier->get_classifier()))
4737 bool CSGInterface::cmd_get_num_svms()
4739 if (m_nrhs!=1 || !create_return_values(1))
4742 set_int(ui_classifier->get_num_svms());
4748 bool CSGInterface::cmd_get_svm()
4750 return cmd_get_classifier();
4753 bool CSGInterface::cmd_set_svm()
4755 if (m_nrhs!=3 || !create_return_values(0))
4761 int32_t num_feat_alphas=0;
4762 int32_t num_vec_alphas=0;
4763 get_matrix(alphas, num_feat_alphas, num_vec_alphas);
4766 SG_ERROR(
"No proper alphas given.\n")
4767 if (num_vec_alphas!=2)
4768 SG_ERROR(
"Not 2 vectors in alphas.\n")
4770 CSVM* svm=(
CSVM*) ui_classifier->get_classifier();
4772 SG_ERROR(
"No SVM object available.\n")
4778 for (int32_t i=0; i<num_support_vectors; i++)
4788 bool CSGInterface::cmd_set_linear_classifier()
4790 if (m_nrhs!=3 || !create_return_values(0))
4800 SG_ERROR(
"No proper weight vector given.\n")
4804 SG_ERROR(
"No Linear Classifier object available.\n")
4811 bool CSGInterface::cmd_get_svm_objective()
4813 if (m_nrhs!=1 || !create_return_values(1))
4816 CSVM* svm=(
CSVM*) ui_classifier->get_classifier();
4825 bool CSGInterface::cmd_compute_svm_primal_objective()
4827 return do_compute_objective(SVM_PRIMAL);
4830 bool CSGInterface::cmd_compute_svm_dual_objective()
4832 return do_compute_objective(SVM_DUAL);
4835 bool CSGInterface::cmd_compute_mkl_dual_objective()
4837 return do_compute_objective(MKL_DUAL);
4840 bool CSGInterface::cmd_compute_relative_mkl_duality_gap()
4842 return do_compute_objective(MKL_RELATIVE_DUALITY_GAP);
4845 bool CSGInterface::cmd_compute_absolute_mkl_duality_gap()
4847 return do_compute_objective(MKL_ABSOLUTE_DUALITY_GAP);
4850 bool CSGInterface::do_compute_objective(E_WHICH_OBJ obj)
4852 if (m_nrhs!=1 || !create_return_values(1))
4857 CSVM* svm=(
CSVM*) ui_classifier->get_classifier();
4862 trainlabels=ui_labels->get_train_labels();
4865 SG_ERROR(
"No trainlabels available.\n")
4867 CKernel* kernel=ui_kernel->get_kernel();
4871 if (!ui_kernel->is_initialized() || !kernel->
has_features())
4872 SG_ERROR(
"Kernel not initialized.\n")
4888 result=((
CMKL*) svm)->compute_mkl_primal_objective();
4892 result=((
CMKL*) svm)->compute_mkl_dual_objective();
4894 case MKL_RELATIVE_DUALITY_GAP:
4897 float64_t primal=((
CMKL*) svm)->compute_mkl_dual_objective();
4898 float64_t dual=((
CMKL*) svm)->compute_mkl_primal_objective();
4899 result=(primal-dual)/dual;
4902 case MKL_ABSOLUTE_DUALITY_GAP:
4905 float64_t primal=((
CMKL*) svm)->compute_mkl_dual_objective();
4906 float64_t dual=((
CMKL*) svm)->compute_mkl_primal_objective();
4911 SG_SERROR(
"Error calling do_compute_objective\n")
4919 bool CSGInterface::cmd_train_classifier()
4921 if (m_nrhs<1 || !create_return_values(0))
4924 CMachine* classifier=ui_classifier->get_classifier();
4926 SG_ERROR(
"No classifier available.\n")
4946 return ui_classifier->train_svm();
4948 return ui_classifier->train_mkl_multiclass();
4952 return ui_classifier->train_mkl();
4955 return ui_classifier->train_krr();
4962 int32_t k=get_int_from_int_or_str();
4964 return ui_classifier->train_knn(k);
4972 int32_t k=get_int_from_int_or_str();
4973 int32_t max_iter=get_int_from_int_or_str();
4975 return ui_classifier->train_clustering(k, max_iter);
4983 int32_t merges=get_int_from_int_or_str();
4985 return ui_classifier->train_clustering(merges);
4992 gamma=get_real_from_real_or_str();
4994 return ui_classifier->train_linear(gamma);
5005 return ui_classifier->train_linear();
5008 return ui_classifier->train_wdocas();
5011 SG_ERROR(
"Unknown classifier type %d.\n", type)
5017 bool CSGInterface::cmd_do_auc_maximization()
5019 if (m_nrhs!=2 || !create_return_values(0))
5022 bool do_auc=get_bool_from_bool_or_str();
5024 return ui_classifier->set_do_auc_maximization(do_auc);
5027 bool CSGInterface::cmd_set_perceptron_parameters()
5029 if (m_nrhs!=3 || !create_return_values(0))
5032 float64_t lernrate=get_real_from_real_or_str();
5033 int32_t maxiter=get_int_from_int_or_str();
5035 return ui_classifier->set_perceptron_parameters(lernrate, maxiter);
5038 bool CSGInterface::cmd_set_svm_qpsize()
5040 if (m_nrhs!=2 || !create_return_values(0))
5043 int32_t qpsize=get_int_from_int_or_str();
5045 return ui_classifier->set_svm_qpsize(qpsize);
5048 bool CSGInterface::cmd_set_svm_max_qpsize()
5050 if (m_nrhs!=2 || !create_return_values(0))
5053 int32_t max_qpsize=get_int_from_int_or_str();
5055 return ui_classifier->set_svm_max_qpsize(max_qpsize);
5058 bool CSGInterface::cmd_set_svm_bufsize()
5060 if (m_nrhs!=2 || !create_return_values(0))
5063 int32_t bufsize=get_int_from_int_or_str();
5065 return ui_classifier->set_svm_bufsize(bufsize);
5068 bool CSGInterface::cmd_set_svm_C()
5070 if (m_nrhs<2 || !create_return_values(0))
5073 float64_t C1=get_real_from_real_or_str();
5077 C2=get_real_from_real_or_str();
5079 return ui_classifier->set_svm_C(C1, C2);
5082 bool CSGInterface::cmd_set_svm_epsilon()
5084 if (m_nrhs!=2 || !create_return_values(0))
5089 return ui_classifier->set_svm_epsilon(epsilon);
5092 bool CSGInterface::cmd_set_svr_tube_epsilon()
5094 if (m_nrhs!=2 || !create_return_values(0))
5097 float64_t tube_epsilon=get_real_from_real_or_str();
5099 return ui_classifier->set_svr_tube_epsilon(tube_epsilon);
5102 bool CSGInterface::cmd_set_svm_nu()
5104 if (m_nrhs!=2 || !create_return_values(0))
5107 float64_t nu=get_real_from_real_or_str();
5109 return ui_classifier->set_svm_nu(nu);
5112 bool CSGInterface::cmd_set_svm_mkl_parameters()
5114 if (m_nrhs<3 || m_nrhs>4 || !create_return_values(0))
5117 float64_t weight_epsilon=get_real_from_real_or_str();
5118 float64_t C_mkl=get_real_from_real_or_str();
5122 mkl_norm=get_real_from_real_or_str();
5124 return ui_classifier->set_svm_mkl_parameters(weight_epsilon, C_mkl, mkl_norm);
5127 bool CSGInterface::cmd_set_elasticnet_lambda()
5129 if (m_nrhs!=2 || !create_return_values(0))
5131 float64_t lambda=get_real_from_real_or_str();
5132 return ui_classifier->set_elasticnet_lambda(lambda);
5135 bool CSGInterface::cmd_set_mkl_block_norm()
5137 if (m_nrhs!=2 || !create_return_values(0))
5139 float64_t bnorm=get_real_from_real_or_str();
5140 return ui_classifier->set_mkl_block_norm(bnorm);
5144 bool CSGInterface::cmd_set_max_train_time()
5146 if (m_nrhs!=2 || !create_return_values(0))
5149 float64_t max_train_time=get_real_from_real_or_str();
5151 return ui_classifier->set_max_train_time(max_train_time);
5154 bool CSGInterface::cmd_set_svm_shrinking_enabled()
5156 if (m_nrhs!=2 || !create_return_values(0))
5159 bool shrinking_enabled=get_bool_from_bool_or_str();
5161 return ui_classifier->set_svm_shrinking_enabled(shrinking_enabled);
5164 bool CSGInterface::cmd_set_svm_batch_computation_enabled()
5166 if (m_nrhs!=2 || !create_return_values(0))
5169 bool batch_computation_enabled=get_bool_from_bool_or_str();
5171 return ui_classifier->set_svm_batch_computation_enabled(
5172 batch_computation_enabled);
5175 bool CSGInterface::cmd_set_svm_linadd_enabled()
5177 if (m_nrhs!=2 || !create_return_values(0))
5180 bool linadd_enabled=get_bool_from_bool_or_str();
5182 return ui_classifier->set_svm_linadd_enabled(linadd_enabled);
5185 bool CSGInterface::cmd_set_svm_bias_enabled()
5187 if (m_nrhs!=2 || !create_return_values(0))
5190 bool bias_enabled=get_bool_from_bool_or_str();
5192 return ui_classifier->set_svm_bias_enabled(bias_enabled);
5195 bool CSGInterface::cmd_set_mkl_interleaved_enabled()
5197 if (m_nrhs!=2 || !create_return_values(0))
5200 bool interleaved_enabled=get_bool_from_bool_or_str();
5202 return ui_classifier->set_mkl_interleaved_enabled(interleaved_enabled);
5205 bool CSGInterface::cmd_set_krr_tau()
5207 if (m_nrhs!=2 || !create_return_values(0))
5210 float64_t tau=get_real_from_real_or_str();
5212 return ui_classifier->set_krr_tau(tau);
5218 bool CSGInterface::cmd_add_preproc()
5220 if (m_nrhs<2 || !create_return_values(0))
5224 char* type=get_str_from_str_or_direct(len);
5227 if (strmatch(type,
"NORMONE"))
5228 preproc=ui_preproc->create_generic(
P_NORMONE);
5229 else if (strmatch(type,
"LOGPLUSONE"))
5231 else if (strmatch(type,
"SORTWORDSTRING"))
5233 else if (strmatch(type,
"SORTULONGSTRING"))
5235 else if (strmatch(type,
"DECOMPRESSCHARSTRING"))
5237 else if (strmatch(type,
"SORTWORD"))
5238 preproc=ui_preproc->create_generic(
P_SORTWORD);
5240 else if (strmatch(type,
"PRUNEVARSUBMEAN"))
5242 bool divide_by_std=
false;
5244 divide_by_std=get_bool_from_bool_or_str();
5246 preproc=ui_preproc->create_prunevarsubmean(divide_by_std);
5250 else if (strmatch(type,
"PCA") && m_nrhs==4)
5252 bool do_whitening=get_bool_from_bool_or_str();
5253 float64_t threshold=get_real_from_real_or_str();
5255 preproc=ui_preproc->create_pca(do_whitening, threshold);
5263 return ui_preproc->add_preproc(preproc);
5266 bool CSGInterface::cmd_del_preproc()
5268 if (m_nrhs!=1 || !create_return_values(0))
5271 return ui_preproc->del_preproc();
5274 bool CSGInterface::cmd_attach_preproc()
5276 if (m_nrhs<2 || !create_return_values(0))
5280 char* target=get_str_from_str_or_direct(len);
5282 bool do_force=
false;
5284 do_force=get_bool_from_bool_or_str();
5286 bool success=ui_preproc->attach_preproc(target, do_force);
5292 bool CSGInterface::cmd_clean_preproc()
5294 if (m_nrhs!=1 || !create_return_values(0))
5297 return ui_preproc->clean_preproc();
5302 bool CSGInterface::cmd_set_converter()
5305 char* type=get_str_from_str_or_direct(len);
5307 if (strmatch(type,
"lle"))
5309 int32_t k = get_int_from_int_or_str();
5310 ui_converter->create_locallylinearembedding(k);
5313 if (strmatch(type,
"npe"))
5315 int32_t k = get_int_from_int_or_str();
5316 ui_converter->create_neighborhoodpreservingembedding(k);
5319 if (strmatch(type,
"ltsa"))
5321 int32_t k = get_int_from_int_or_str();
5322 ui_converter->create_localtangentspacealignment(k);
5325 if (strmatch(type,
"lltsa"))
5327 int32_t k = get_int_from_int_or_str();
5328 ui_converter->create_linearlocaltangentspacealignment(k);
5331 if (strmatch(type,
"hlle"))
5333 int32_t k = get_int_from_int_or_str();
5334 ui_converter->create_hessianlocallylinearembedding(k);
5337 if (strmatch(type,
"laplacian_eigenmaps"))
5339 int32_t k = get_int_from_int_or_str();
5340 int32_t width = get_real_from_real_or_str();
5341 ui_converter->create_laplacianeigenmaps(k,width);
5344 if (strmatch(type,
"lpp"))
5346 int32_t k = get_int_from_int_or_str();
5347 int32_t width = get_real_from_real_or_str();
5348 ui_converter->create_localitypreservingprojections(k,width);
5351 if (strmatch(type,
"diffusion_maps"))
5353 int32_t t = get_int_from_int_or_str();
5354 int32_t width = get_real_from_real_or_str();
5355 ui_converter->create_diffusionmaps(t,width);
5358 if (strmatch(type,
"isomap"))
5360 int32_t k = get_int_from_int_or_str();
5361 ui_converter->create_isomap(k);
5364 if (strmatch(type,
"mds"))
5366 ui_converter->create_multidimensionalscaling();
5369 if (strmatch(type,
"jade"))
5371 ui_converter->create_jade();
5377 bool CSGInterface::cmd_apply_converter()
5379 if (m_nrhs!=1 || !create_return_values(1))
5388 bool CSGInterface::cmd_embed()
5390 int32_t target_dim = get_int_from_int_or_str();
5392 if (m_nrhs!=1 || !create_return_values(1))
5403 bool CSGInterface::cmd_new_plugin_estimator()
5405 if (m_nrhs<2 || !create_return_values(0))
5408 float64_t pos_pseudo=get_real_from_real_or_str();
5409 float64_t neg_pseudo=get_real_from_real_or_str();
5411 return ui_pluginestimate->new_estimator(pos_pseudo, neg_pseudo);
5414 bool CSGInterface::cmd_train_estimator()
5416 if (m_nrhs!=1 || !create_return_values(0))
5419 return ui_pluginestimate->train();
5422 bool CSGInterface::cmd_plugin_estimate_classify_example()
5424 if (m_nrhs!=2 || !create_return_values(1))
5427 int32_t idx=get_int();
5428 float64_t result=ui_pluginestimate->apply_one(idx);
5430 set_vector(&result, 1);
5434 bool CSGInterface::cmd_plugin_estimate_classify()
5436 if (m_nrhs!=1 || !create_return_values(1))
5439 CFeatures* feat=ui_features->get_test_features();
5445 CLabels* labels=ui_pluginestimate->apply();
5446 for (int32_t i=0; i<num_vec; i++)
5450 set_vector(result, num_vec);
5456 bool CSGInterface::cmd_set_plugin_estimate()
5458 if (m_nrhs!=3 || !create_return_values(0))
5462 int32_t num_probs=0;
5464 get_matrix(emission_probs, num_probs, num_vec);
5467 SG_ERROR(
"Need at least 1 set of positive and 1 set of negative params.\n")
5470 float64_t* neg_params=&(emission_probs[num_probs]);
5474 get_vector(model_sizes, len);
5476 int32_t seq_length=(int32_t) model_sizes[0];
5477 int32_t num_symbols=(int32_t) model_sizes[1];
5478 if (num_probs!=seq_length*num_symbols)
5479 SG_ERROR(
"Mismatch in number of emission probs and sequence length * number of symbols.\n")
5481 ui_pluginestimate->get_estimator()->set_model_params(
5482 pos_params, neg_params, seq_length, num_symbols);
5487 bool CSGInterface::cmd_get_plugin_estimate()
5489 if (m_nrhs!=1 || !create_return_values(2))
5494 int32_t num_params=0;
5495 int32_t seq_length=0;
5496 int32_t num_symbols=0;
5498 if (!ui_pluginestimate->get_estimator()->get_model_params(
5499 pos_params, neg_params, seq_length, num_symbols))
5502 num_params=seq_length*num_symbols;
5505 for (int32_t i=0; i<num_params; i++)
5506 result[i]=pos_params[i];
5507 for (int32_t i=0; i<num_params; i++)
5508 result[i+num_params]=neg_params[i];
5510 set_matrix(result, num_params, 2);
5516 set_vector(model_sizes, 2);
5521 bool CSGInterface::cmd_convergence_criteria()
5523 if (m_nrhs<3 || !create_return_values(0))
5526 int32_t num_iterations=get_int_from_int_or_str();
5529 return ui_hmm->convergence_criteria(num_iterations, epsilon);
5532 bool CSGInterface::cmd_normalize()
5534 if (m_nrhs<2 || !create_return_values(0))
5537 bool keep_dead_states=get_bool_from_bool_or_str();
5539 return ui_hmm->normalize(keep_dead_states);
5542 bool CSGInterface::cmd_add_states()
5544 if (m_nrhs<3 || !create_return_values(0))
5547 int32_t num_states=get_int_from_int_or_str();
5548 float64_t value=get_real_from_real_or_str();
5550 return ui_hmm->add_states(num_states, value);
5553 bool CSGInterface::cmd_permutation_entropy()
5555 if (m_nrhs<3 || !create_return_values(0))
5558 int32_t width=get_int_from_int_or_str();
5559 int32_t seq_num=get_int_from_int_or_str();
5561 return ui_hmm->permutation_entropy(width, seq_num);
5564 bool CSGInterface::cmd_relative_entropy()
5566 if (m_nrhs!=1 || !create_return_values(1))
5571 bool success=ui_hmm->relative_entropy(entropy, len);
5575 set_vector(entropy, len);
5581 bool CSGInterface::cmd_entropy()
5583 if (m_nrhs!=1 || !create_return_values(1))
5588 bool success=ui_hmm->entropy(entropy, len);
5592 set_vector(entropy, len);
5598 bool CSGInterface::cmd_hmm_classify()
5600 return do_hmm_classify(
false,
false);
5603 bool CSGInterface::cmd_one_class_hmm_classify()
5605 return do_hmm_classify(
false,
true);
5608 bool CSGInterface::cmd_one_class_linear_hmm_classify()
5610 return do_hmm_classify(
true,
true);
5613 bool CSGInterface::do_hmm_classify(
bool linear,
bool one_class)
5615 if (m_nrhs>1 || !create_return_values(1))
5618 CFeatures* feat=ui_features->get_test_features();
5627 labels=ui_hmm->linear_one_class_classify();
5632 labels=ui_hmm->one_class_classify();
5634 labels=ui_hmm->classify();
5640 for (int32_t i=0; i<num_vec; i++)
5644 set_vector(result, num_vec);
5650 bool CSGInterface::cmd_one_class_hmm_classify_example()
5652 return do_hmm_classify_example(
true);
5655 bool CSGInterface::cmd_hmm_classify_example()
5657 return do_hmm_classify_example(
false);
5660 bool CSGInterface::do_hmm_classify_example(
bool one_class)
5662 if (m_nrhs!=2 || !create_return_values(1))
5665 int32_t idx=get_int();
5669 result=ui_hmm->one_class_classify_example(idx);
5671 result=ui_hmm->classify_example(idx);
5678 bool CSGInterface::cmd_output_hmm()
5680 if (m_nrhs!=1 || !create_return_values(0))
5683 return ui_hmm->output_hmm();
5686 bool CSGInterface::cmd_output_hmm_defined()
5688 if (m_nrhs!=1 || !create_return_values(0))
5691 return ui_hmm->output_hmm_defined();
5694 bool CSGInterface::cmd_hmm_likelihood()
5696 if (m_nrhs!=1 || !create_return_values(1))
5699 CHMM* h=ui_hmm->get_current();
5704 set_real(likelihood);
5709 bool CSGInterface::cmd_likelihood()
5711 if (m_nrhs!=1 || !create_return_values(0))
5714 return ui_hmm->likelihood();
5717 bool CSGInterface::cmd_save_likelihood()
5719 if (m_nrhs<2 || !create_return_values(0))
5723 char* filename=get_str_from_str_or_direct(len);
5725 bool is_binary=
false;
5727 is_binary=get_bool_from_bool_or_str();
5729 bool success=ui_hmm->save_likelihood(filename, is_binary);
5735 bool CSGInterface::cmd_get_viterbi_path()
5737 if (m_nrhs!=2 || !create_return_values(2))
5740 int32_t dim=get_int();
5743 CHMM* h=ui_hmm->get_current();
5747 CFeatures* feat=ui_features->get_test_features();
5757 if (!vec || num_feat<=0)
5764 SG_DEBUG(
"computing viterbi path for vector %d (length %d)\n", dim, num_feat)
5768 set_vector(path, num_feat);
5770 set_real(likelihood);
5775 bool CSGInterface::cmd_viterbi_train()
5777 if (m_nrhs!=1 || !create_return_values(0))
5780 return ui_hmm->viterbi_train();
5783 bool CSGInterface::cmd_viterbi_train_defined()
5785 if (m_nrhs!=1 || !create_return_values(0))
5788 return ui_hmm->viterbi_train_defined();
5791 bool CSGInterface::cmd_baum_welch_train()
5793 if (m_nrhs!=1 || !create_return_values(0))
5796 return ui_hmm->baum_welch_train();
5799 bool CSGInterface::cmd_baum_welch_train_defined()
5801 if (m_nrhs!=1 || !create_return_values(0))
5804 return ui_hmm->baum_welch_train_defined();
5808 bool CSGInterface::cmd_baum_welch_trans_train()
5810 if (m_nrhs!=1 || !create_return_values(0))
5813 return ui_hmm->baum_welch_trans_train();
5816 bool CSGInterface::cmd_linear_train()
5818 if (m_nrhs<1 || !create_return_values(0))
5824 char* align=get_str_from_str_or_direct(len);
5826 bool success=ui_hmm->linear_train(align[0]);
5832 return ui_hmm->linear_train();
5835 bool CSGInterface::cmd_save_path()
5837 if (m_nrhs<2 || !create_return_values(0))
5841 char* filename=get_str_from_str_or_direct(len);
5843 bool is_binary=
false;
5845 is_binary=get_bool_from_bool_or_str();
5847 bool success=ui_hmm->save_path(filename, is_binary);
5853 bool CSGInterface::cmd_append_hmm()
5855 if (m_nrhs!=5 || !create_return_values(0))
5858 CHMM* old_h=ui_hmm->get_current();
5873 get_matrix(a, M_a, N_a);
5879 get_matrix(b, M_b, N_b);
5882 if (N_p!=N || N_q!=N || N_a!=N || M_a!=N || N_b!=M || M_b!=N)
5884 SG_ERROR(
"Model matrices not matching in size.\n"
5885 "p:(%d) q:(%d) a:(%d,%d) b(%d,%d)\n",
5886 N_p, N_q, N_a, M_a, N_b, M_b);
5889 CHMM* h=
new CHMM(N, M, NULL, ui_hmm->get_pseudo());
5900 h->
set_a(i,j, a[i+j*N]);
5904 h->
set_b(i,j, b[i+j*N]);
5912 bool CSGInterface::cmd_append_model()
5914 if (m_nrhs<2 || !create_return_values(0))
5916 if (m_nrhs>2 && m_nrhs!=4)
5920 char* filename=get_str_from_str_or_direct(len);
5925 base1=get_int_from_int_or_str();
5926 base2=get_int_from_int_or_str();
5929 bool success=ui_hmm->append_model(filename, base1, base2);
5935 bool CSGInterface::cmd_new_hmm()
5937 if (m_nrhs!=3 || !create_return_values(0))
5940 int32_t n=get_int_from_int_or_str();
5941 int32_t m=get_int_from_int_or_str();
5943 return ui_hmm->new_hmm(n, m);
5946 bool CSGInterface::cmd_load_hmm()
5948 if (m_nrhs!=2 || !create_return_values(0))
5952 char* filename=get_str_from_str_or_direct(len);
5954 bool success=ui_hmm->load(filename);
5960 bool CSGInterface::cmd_save_hmm()
5962 if (m_nrhs<2 || !create_return_values(0))
5966 char* filename=get_str_from_str_or_direct(len);
5968 bool is_binary=
false;
5970 is_binary=get_bool_from_bool_or_str();
5972 bool success=ui_hmm->save(filename, is_binary);
5978 bool CSGInterface::cmd_set_hmm()
5980 if (m_nrhs!=5 || !create_return_values(0))
5994 get_matrix(a, M_a, N_a);
6000 get_matrix(b, M_b, N_b);
6003 if (N_p!=N || N_q!=N || N_a!=N || M_a!=N || N_b!=M || M_b!=N)
6005 SG_ERROR(
"Model matrices not matching in size.\n"
6006 "p:(%d) q:(%d) a:(%d,%d) b(%d,%d)\n",
6007 N_p, N_q, N_a, M_a, N_b, M_b);
6010 CHMM* current=ui_hmm->get_current();
6012 SG_ERROR(
"Need a previously created HMM.\n")
6018 current->
set_p(i, p[i]);
6019 current->
set_q(i, q[i]);
6024 current->
set_a(i,j, a[i+j*N]);
6028 current->
set_b(i,j, b[i+j*N]);
6036 bool CSGInterface::cmd_set_hmm_as()
6038 if (m_nrhs!=2 || !create_return_values(0))
6042 char* target=get_str_from_str_or_direct(len);
6044 bool success=ui_hmm->set_hmm_as(target);
6050 bool CSGInterface::cmd_set_chop()
6052 if (m_nrhs!=2 || !create_return_values(0))
6055 float64_t value=get_real_from_real_or_str();
6056 return ui_hmm->chop(value);
6059 bool CSGInterface::cmd_set_pseudo()
6061 if (m_nrhs!=2 || !create_return_values(0))
6064 float64_t value=get_real_from_real_or_str();
6065 return ui_hmm->set_pseudo(value);
6068 bool CSGInterface::cmd_load_definitions()
6070 if (m_nrhs<2 || !create_return_values(0))
6074 char* filename=get_str_from_str_or_direct(len);
6078 do_init=get_bool_from_bool_or_str();
6080 bool success=ui_hmm->load_definitions(filename, do_init);
6086 bool CSGInterface::cmd_get_hmm()
6088 if (m_nrhs!=1 || !create_return_values(4))
6091 CHMM* h=ui_hmm->get_current();
6095 int32_t N=h->
get_N();
6096 int32_t M=h->
get_M();
6116 a[i+j*N]=h->
get_a(i, j);
6117 set_matrix(a, N, N);
6123 b[i+j*N]=h->
get_b(i, j);
6124 set_matrix(b, N, M);
6130 bool CSGInterface::cmd_best_path()
6132 if (m_nrhs!=3 || !create_return_values(0))
6135 int32_t from=get_int_from_int_or_str();
6136 int32_t to=get_int_from_int_or_str();
6138 return ui_hmm->best_path(from, to);
6141 bool CSGInterface::cmd_best_path_2struct()
6143 if (m_nrhs!=12 || !create_return_values(3))
6146 SG_ERROR(
"Sorry, this parameter list is awful!\n")
6151 void CSGInterface::get_vector(
bool*& vector, int32_t& len)
6153 int32_t* int_vector;
6154 get_vector(int_vector, len);
6157 vector= SG_MALLOC(
bool, len);
6159 for (int32_t i=0; i<len; i++)
6160 vector[i]= (int_vector[i]!=0);
6162 SG_FREE(int_vector);
6165 void CSGInterface::set_vector(
const bool* vector, int32_t len)
6167 int32_t* int_vector = SG_MALLOC(int32_t, len);
6168 for (int32_t i=0;i<len;i++)
6175 set_vector(int_vector,len);
6176 SG_FREE(int_vector);
6179 bool CSGInterface::cmd_set_plif_struct()
6184 get_vector(ids,Nid);
6190 get_string_list(names, Nname,Mname);
6196 get_matrix(all_limits, Mlimits, Nlimits);
6199 int32_t Npenalties=0;
6200 int32_t Mpenalties=0;
6202 get_matrix(all_penalties, Mpenalties, Npenalties);
6205 int32_t Ntransform=0;
6206 int32_t Mtransform=0;
6208 get_string_list(all_transform, Ntransform, Mtransform);
6213 get_vector(min_values,Nmin);
6218 get_vector(max_values,Nmax);
6222 bool* all_use_cache;
6223 get_vector(all_use_cache,Ncache);
6227 int32_t* all_use_svm;
6228 get_vector(all_use_svm,Nsvm);
6233 get_vector(all_do_calc,Ncalc);
6236 SG_ERROR(
"Ncalc!=Nsvm, Ncalc:%i, Nsvm:%i\n",Ncalc,Nsvm)
6238 SG_ERROR(
"Ncalc!=Ncache, Ncalc:%i, Ncache:%i\n",Ncalc,Ncache)
6239 if (Ncalc!=Ntransform)
6240 SG_ERROR(
"Ncalc!=Ntransform, Ncalc:%i, Ntransform:%i\n",Ncalc,Ntransform)
6242 SG_ERROR(
"Ncalc!=Nmin, Ncalc:%i, Nmin:%i\n",Ncalc,Nmin)
6244 SG_ERROR(
"Ncalc!=Nmax, Ncalc:%i, Nmax:%i\n",Ncalc,Nmax)
6245 if (Ncalc!=Npenalties)
6246 SG_ERROR(
"Ncalc!=Npenalties, Ncalc:%i, Npenalties:%i\n",Ncalc,Npenalties)
6248 SG_ERROR(
"Ncalc!=Nlimits, Ncalc:%i, Nlimits:%i\n",Ncalc,Nlimits)
6250 SG_ERROR(
"Ncalc!=Nname, Ncalc:%i, Nname:%i\n",Ncalc,Nname)
6252 SG_ERROR(
"Ncalc!=Nid, Ncalc:%i, Nid:%i\n",Ncalc,Nid)
6253 if (Mlimits!=Mpenalties)
6254 SG_ERROR(
"Mlimits!=Mpenalties, Mlimits:%i, Mpenalties:%i\n",Mlimits,Mpenalties)
6257 int32_t M = Mlimits;
6271 SG_FREE(all_transform);
6272 SG_FREE(all_do_calc);
6277 bool CSGInterface::cmd_get_plif_struct()
6285 int32_t* ids = SG_MALLOC(int32_t, N);
6292 bool* all_use_cache = SG_MALLOC(
bool, N);
6293 int32_t* all_use_svm = SG_MALLOC(int32_t, N);
6294 bool* all_do_calc = SG_MALLOC(
bool, N);
6295 for (int32_t i=0;i<N;i++)
6299 names[i].
slen = strlen(PEN[i]->get_plif_name());
6302 for (int32_t j=0;j<M;j++)
6304 all_limits[i*M+j]=limits[j];
6305 all_penalties[i*M+j]=penalties[j];
6307 all_transform[i].
string = (
char*) PEN[i]->get_transform_type();
6308 all_transform[i].
slen = strlen(PEN[i]->get_transform_type());
6317 set_string_list(names, N);
6318 set_matrix(all_limits, M, N);
6319 set_matrix(all_penalties, M, N);
6320 set_string_list(all_transform, N);
6321 set_vector(min_values,N);
6322 set_vector(max_values,N);
6323 set_vector(all_use_cache,N);
6324 set_vector(all_use_svm,N);
6325 set_vector(all_do_calc,N);
6328 SG_FREE(max_values);
6329 SG_FREE(min_values);
6331 SG_FREE(all_transform);
6332 SG_FREE(all_limits);
6333 SG_FREE(all_penalties);
6334 SG_FREE(all_use_cache);
6335 SG_FREE(all_use_svm);
6336 SG_FREE(all_do_calc);
6353 bool CSGInterface::cmd_signals_set_positions()
6357 bool CSGInterface::cmd_signals_set_labels()
6361 bool CSGInterface::cmd_signals_set_split()
6365 bool CSGInterface::cmd_signals_set_train_mask()
6369 bool CSGInterface::cmd_signals_add_feature()
6373 bool CSGInterface::cmd_signals_add_kernel()
6377 bool CSGInterface::cmd_signals_run()
6382 bool CSGInterface::cmd_init_dyn_prog()
6385 int32_t num_svms=get_int();
6388 ui_structure->set_dyn_prog(h);
6392 bool CSGInterface::cmd_clean_up_dyn_prog()
6394 return ui_structure->cleanup();
6397 bool CSGInterface::cmd_set_model()
6402 CDynProg* h = ui_structure->get_dyn_prog();
6413 get_ndarray(penalties_array,Dim,numDim);
6418 SG_ERROR(
"error computing plif matrix\n")
6419 ui_structure->set_num_states(Dim[0]);
6420 SG_FREE(penalties_array);
6424 bool use_orf = get_bool();
6425 ui_structure->set_use_orf(use_orf);
6432 get_matrix(mod_words, Nmod,Mmod);
6433 if (Nmod != num_svms)
6434 SG_ERROR(
"should be equal: Nmod: %i, num_svms: %i\n",Nmod,num_svms)
6440 int32_t num_states=0;
6441 int32_t feat_dim3=0;
6442 int32_t* state_signals;
6443 get_matrix(state_signals,num_states,feat_dim3);
6444 ASSERT(num_states==Dim[0])
6453 get_matrix(orf_info,Norf,Morf);
6457 ui_structure->set_orf_info(orf_info, Norf, Morf);
6465 bool CSGInterface::cmd_precompute_content_svms()
6471 seq = get_string(seq_len);
6477 get_vector(all_pos, Npos);
6484 get_matrix(weights, Nweights, num_svms);
6486 SG_PRINT(
"Dimension mismatch: got %i, expect %i\n", Nweights, 5440)
6487 ui_structure->set_content_svm_weights(weights, Nweights, num_svms);
6489 CDynProg* h = ui_structure->get_dyn_prog();
6491 SG_ERROR(
"no DynProg object found, use init_dyn_prog first\n")
6504 SG_DEBUG(
"precompute_content_svms done\n")
6508 bool CSGInterface::cmd_get_lin_feat()
6510 CDynProg* h = ui_structure->get_dyn_prog();
6512 SG_ERROR(
"no DynProg object found, use set_model first\n")
6515 int32_t dim1, dim2 = 0;
6518 set_matrix(lin_feat, dim1, dim2);
6522 bool CSGInterface::cmd_set_lin_feat()
6527 seq = get_string(Nseq);
6533 get_vector(all_pos, Npos);
6537 int32_t num_svms, seq_len;
6539 get_matrix(lin_feat, num_svms, seq_len);
6543 SG_ERROR(
"Dimension mismatch: got %i positions and (%ix%i) values\n", Npos, num_svms, seq_len)
6552 CDynProg* h = ui_structure->get_dyn_prog();
6554 SG_ERROR(
"no DynProg object found, use set_model first\n")
6566 bool CSGInterface::cmd_long_transition_settings()
6568 bool use_long_transitions = get_bool();
6569 int32_t threshold = get_int();
6570 int32_t max_len = get_int();
6572 CDynProg* h = ui_structure->get_dyn_prog();
6574 SG_ERROR(
"no DynProg object found, use set_model first\n")
6580 bool CSGInterface::cmd_set_feature_matrix()
6582 int32_t num_states = ui_structure->get_num_states();
6589 get_ndarray(features, Dims, numDims);
6592 SG_ERROR(
"expected a 3 dimensional array, got %i dimensions\n", numDims)
6593 if (Dims[0]!=num_states)
6594 SG_ERROR(
"number of rows (%i) not equal number of states (%i)\n",Dims[0], num_states)
6595 ASSERT(ui_structure->set_feature_matrix(features, Dims))
6597 ASSERT(ui_structure->set_feature_dims(Dims))
6604 bool CSGInterface::cmd_set_feature_matrix_sparse()
6606 int32_t num_pos = ui_structure->get_num_positions();
6607 int32_t num_states = ui_structure->get_num_states();
6611 int32_t dim11, dim12 ;
6613 get_sparse_matrix(features1, dim11, dim12);
6615 int32_t dim21, dim22 ;
6617 get_sparse_matrix(features2, dim21, dim22);
6622 int32_t *Dims = SG_MALLOC(int32_t, 3);
6627 ASSERT(Dims[0]==num_states)
6630 ASSERT(ui_structure->set_feature_matrix_sparse(features1, features2, Dims))
6631 ASSERT(ui_structure->set_feature_dims(Dims))
6639 bool CSGInterface::cmd_init_intron_list()
6642 int32_t Nstart_positions;
6643 int32_t* start_positions;
6644 get_vector(start_positions, Nstart_positions);
6648 int32_t Nend_positions;
6649 int32_t* end_positions;
6650 get_vector(end_positions, Nend_positions);
6656 get_vector(quality, Nquality);
6662 get_vector(all_pos, Nall_pos);
6665 ASSERT(Nquality==Nend_positions)
6666 ASSERT(Nend_positions==Nstart_positions)
6670 intron_list->
init_list(all_pos, Nall_pos);
6672 intron_list->
read_introns(start_positions, end_positions, quality, Nstart_positions);
6674 SG_FREE(start_positions);
6675 SG_FREE(end_positions);
6685 CDynProg* h = ui_structure->get_dyn_prog();
6687 SG_ERROR(
"no DynProg object found, use set_model first\n")
6693 bool CSGInterface::cmd_precompute_tiling_features()
6697 CDynProg* h = ui_structure->get_dyn_prog();
6699 int32_t Nintensities=0;
6701 get_vector(intensities, Nintensities);
6703 int32_t Nprobe_pos=0;
6705 get_vector(probe_pos, Nprobe_pos);
6706 ASSERT(Nprobe_pos==Nintensities)
6708 int32_t Ntiling_plif_ids=0;
6709 int32_t* tiling_plif_ids;
6710 get_vector(tiling_plif_ids, Ntiling_plif_ids);
6717 bool CSGInterface::cmd_best_path_trans()
6719 CDynProg* h = ui_structure->get_dyn_prog();
6726 int32_t* feat_dims = ui_structure->get_feature_dims();
6727 float64_t* features = (ui_structure->get_feature_matrix(
false));
6730 int32_t* orf_info = ui_structure->get_orf_info();
6731 bool use_orf = ui_structure->get_use_orf();
6740 SG_ERROR(
"# transitions from initial state (%i) does not match # states (%i)\n", Np, num_states)
6748 SG_ERROR(
"# transitions to end state (%i) does not match # states (%i)\n", Nq, num_states)
6754 get_vector(all_nbest, Nnbest);
6759 nbest =all_nbest[0];
6760 nother=all_nbest[1];
6763 nbest =all_nbest[0];
6770 int32_t Nseg_path=0;
6771 int32_t Mseg_path=0;
6773 get_matrix(seg_path, Nseg_path, Mseg_path);
6778 int32_t num_a_trans=0;
6780 get_matrix(a_trans, num_a_trans, Na_trans);
6789 get_matrix(loss, Nloss,Mloss);
6815 SG_ERROR(
"svm arrays inconsistent\n")
6820 SG_DEBUG(
"best_path_trans: M: %i, Mseg_path: %i\n", M, Mseg_path)
6837 bool segment_loss_non_zero=
false;
6838 for (int32_t i=0; i<Nloss*Mloss; i++)
6841 segment_loss_non_zero=
true;
6851 if (segment_loss_non_zero)
6853 SG_DEBUG(
"Using version with segment_loss\n")
6861 SG_DEBUG(
"Using version without segment_loss\n")
6878 for (int32_t k=0; k<(nbest+nother); k++)
6880 for (int32_t i=0; i<M; i++)
6882 d_my_path[i*(nbest+nother)+k] = states.
matrix[i+k*M] ;
6883 d_my_pos[i*(nbest+nother)+k] = my_pos.
matrix[i+k*M] ;
6886 set_vector(p_prob.
vector,nbest+nother);
6887 set_vector(d_my_path, (nbest+nother)*M);
6888 set_vector(d_my_pos, (nbest+nother)*M);
6897 bool CSGInterface::cmd_best_path_trans_deriv()
6899 int32_t num_states = ui_structure->get_num_states();
6900 int32_t* feat_dims = ui_structure->get_feature_dims();
6901 float64_t* features = (ui_structure->get_feature_matrix(
false));
6913 SG_ERROR(
"Np!=num_states; Np:%i num_states:%i",Np,num_states)
6921 SG_ERROR(
"Nq!=num_states; Nq:%i num_states:%i",Nq,num_states)
6928 int32_t Nseg_path=0;
6929 int32_t Mseg_path=0;
6931 get_matrix(seg_path,Nseg_path,Mseg_path);
6936 int32_t num_a_trans=0;
6938 get_matrix(a_trans, num_a_trans, Na_trans);
6947 get_matrix(loss, Nloss,Mloss);
6951 int32_t Nmystate_seq=0;
6952 int32_t* mystate_seq=NULL;
6953 get_vector(mystate_seq, Nmystate_seq);
6957 int32_t Nmypos_seq=0;
6958 int32_t* mypos_seq=NULL;
6959 get_vector(mypos_seq, Nmypos_seq);
6964 int32_t max_plif_id = 0 ;
6965 int32_t max_plif_len = 1 ;
6966 for (int32_t i=0; i<Nplif; i++)
6968 if (i>0 && PEN[i]->get_id()!=i)
6969 SG_ERROR(
"PEN[i]->get_id()!=i; PEN[%i]->get_id():%i ,\n",i, PEN[i]->get_id())
6972 if (PEN[i]->get_plif_len()>max_plif_len)
6977 CDynProg* h = ui_structure->get_dyn_prog();
6989 SG_ERROR(
"svm arrays inconsistent\n")
6991 int32_t *my_path = SG_MALLOC(int32_t, Nmypos_seq+1);
6992 memset(my_path, -1, Nmypos_seq*
sizeof(int32_t)) ;
6993 int32_t *my_pos = SG_MALLOC(int32_t, Nmypos_seq+1);
6994 memset(my_pos, -1, Nmypos_seq*
sizeof(int32_t)) ;
6997 for (int32_t i=0; i<Nmypos_seq; i++)
6999 my_path[i] = mystate_seq[i] ;
7000 my_pos[i] = mypos_seq[i] ;
7034 for (int32_t i=0; i<num_states; i++)
7036 for (int32_t j=0; j<num_states; j++)
7037 p_A_deriv[i+j*num_states] = h->
get_a_deriv(i, j) ;
7043 for (int32_t
id=0;
id<=max_plif_id;
id++)
7047 ASSERT(len<=max_plif_len)
7048 for (int32_t j=0; j<max_plif_len; j++)
7049 a_Plif_deriv.
element(
id, j)= deriv[j] ;
7052 set_vector(p_p_deriv, num_states);
7053 set_vector(p_q_deriv, num_states);
7054 set_matrix(p_A_deriv, num_states, num_states);
7055 set_matrix(p_Plif_deriv, (max_plif_id+1), max_plif_len);
7056 set_vector(p_my_scores, Nmypos_seq);
7057 set_vector(p_my_losses, Nmypos_seq);
7062 SG_FREE(p_Plif_deriv);
7073 SG_FREE(mystate_seq);
7079 bool CSGInterface::cmd_precompute_subkernels()
7081 if (m_nrhs!=1 || !create_return_values(0))
7084 return ui_kernel->precompute_subkernels();
7086 bool CSGInterface::cmd_crc()
7088 if (m_nrhs!=2 || !create_return_values(1))
7092 char*
string=get_string(slen);
7094 uint8_t* bstring=SG_MALLOC(uint8_t, slen);
7096 for (int32_t i=0; i<slen; i++)
7097 bstring[i]=
string[i];
7107 bool CSGInterface::cmd_system()
7109 if (m_nrhs<2 || !create_return_values(0))
7113 char* command=SG_MALLOC(
char, 10000);
7114 memset(command, 0,
sizeof(
char)*10000);
7115 char* cmd=get_str_from_str_or_direct(len);
7116 strncat(command, cmd, 10000);
7119 while (m_rhs_counter<m_nrhs)
7121 strncat(command,
" ", 10000);
7122 char* arg=get_str_from_str_or_direct(len);
7123 strncat(command, arg, 10000);
7127 int32_t success=system(command);
7129 return (success==0);
7132 bool CSGInterface::cmd_exit()
7138 bool CSGInterface::cmd_exec()
7140 if (m_nrhs<2 || !create_return_values(0))
7144 char* filename=get_str_from_str_or_direct(len);
7145 FILE* file=fopen(filename,
"r");
7149 SG_ERROR(
"Error opening file: %s.\n", filename)
7162 bool CSGInterface::cmd_set_output()
7164 if (m_nrhs<2 || !create_return_values(0))
7168 char* filename=get_str_from_str_or_direct(len);
7174 SG_INFO(
"Setting output file to: %s.\n", filename)
7176 if (strmatch(filename,
"STDERR"))
7177 io->set_target(stderr);
7178 else if (strmatch(filename,
"STDOUT"))
7179 io->set_target(stdout);
7182 file_out=fopen(filename,
"w");
7184 SG_ERROR(
"Error opening output file %s.\n", filename)
7185 io->set_target(file_out);
7191 bool CSGInterface::cmd_set_threshold()
7193 if (m_nrhs!=2 || !create_return_values(0))
7196 float64_t value=get_real_from_real_or_str();
7198 ui_math->set_threshold(value);
7202 bool CSGInterface::cmd_init_random()
7204 if (m_nrhs!=2 || !create_return_values(0))
7207 uint32_t initseed=(uint32_t) get_int_from_int_or_str();
7208 ui_math->init_random(initseed);
7213 bool CSGInterface::cmd_set_num_threads()
7215 if (m_nrhs!=2 || !create_return_values(0))
7218 int32_t num_threads=get_int_from_int_or_str();
7220 parallel->set_num_threads(num_threads);
7221 SG_INFO(
"Set number of threads to %d.\n", num_threads)
7226 bool CSGInterface::cmd_translate_string()
7228 if (m_nrhs!=4 || !create_return_values(1))
7233 get_vector(
string, len);
7235 int32_t order=get_int();
7236 int32_t start=get_int();
7238 const int32_t max_val=2;
7240 uint16_t* obs=SG_MALLOC(uint16_t, len);
7242 for (i=0; i<len; i++)
7244 switch ((
char)
string[i])
7246 case 'A': obs[i]=0;
break;
7247 case 'C': obs[i]=1;
break;
7248 case 'G': obs[i]=2;
break;
7249 case 'T': obs[i]=3;
break;
7250 case 'a': obs[i]=0;
break;
7251 case 'c': obs[i]=1;
break;
7252 case 'g': obs[i]=2;
break;
7253 case 't': obs[i]=3;
break;
7254 default:
SG_ERROR(
"Wrong letter in string.\n")
7259 for (i=len-1; i>=order-1; i--)
7262 for (j=i; j>=i-order+1; j--)
7263 value=(value>>max_val) | ((obs[j])<<(max_val*(order-1)));
7265 obs[i]=(uint16_t) value;
7268 for (i=order-2;i>=0;i--)
7271 for (j=i; j>=i-order+1; j--)
7273 value= (value >> max_val);
7275 value|=(obs[j]) << (max_val * (order-1));
7281 for (i=start; i<len; i++)
7285 set_vector(real_obs, len);
7291 bool CSGInterface::cmd_clear()
7318 bool CSGInterface::cmd_tic()
7324 bool CSGInterface::cmd_toc()
7330 bool CSGInterface::cmd_print()
7332 if (m_nrhs<2 || !create_return_values(0))
7336 char* msg=get_str_from_str_or_direct(len);
7344 bool CSGInterface::cmd_echo()
7346 if (m_nrhs<2 || !create_return_values(0))
7350 char* level=get_str_from_str_or_direct(len);
7352 if (strmatch(level,
"OFF"))
7367 bool CSGInterface::cmd_loglevel()
7369 if (m_nrhs<2 || !create_return_values(0))
7373 char* level=get_str_from_str_or_direct(len);
7375 if (strmatch(level,
"ALL") || strmatch(level,
"GCDEBUG"))
7377 else if (strmatch(level,
"DEBUG"))
7379 else if (strmatch(level,
"INFO"))
7381 else if (strmatch(level,
"NOTICE"))
7383 else if (strmatch(level,
"WARN"))
7385 else if (strmatch(level,
"ERROR"))
7387 else if (strmatch(level,
"CRITICAL"))
7389 else if (strmatch(level,
"ALERT"))
7391 else if (strmatch(level,
"EMERGENCY"))
7394 SG_ERROR(
"Unknown loglevel '%s'.\n", level)
7396 SG_INFO(
"Loglevel set to %s.\n", level)
7402 bool CSGInterface::cmd_syntax_highlight()
7404 if (m_nrhs<2 || !create_return_values(0))
7408 char* hili=get_str_from_str_or_direct(len);
7410 if (strmatch(hili,
"ON"))
7413 io->enable_syntax_highlighting();
7415 else if (strmatch(hili,
"OFF"))
7418 io->disable_syntax_highlighting();
7423 SG_INFO(
"Syntax hilighting set to %s.\n", hili)
7429 bool CSGInterface::cmd_progress()
7431 if (m_nrhs<2 || !create_return_values(0))
7435 char* progress=get_str_from_str_or_direct(len);
7437 if (strmatch(progress,
"ON"))
7438 io->enable_progress();
7439 else if (strmatch(progress,
"OFF"))
7440 io->disable_progress();
7442 SG_ERROR(
"arguments to progress are ON|OFF - found '%s'.\n", progress)
7444 SG_INFO(
"Progress set to %s.\n", progress)
7450 bool CSGInterface::cmd_get_version()
7452 if (m_nrhs!=1 || !create_return_values(1))
7455 set_int(version->get_version_revision());
7460 bool CSGInterface::cmd_help()
7462 if ((m_nrhs!=1 && m_nrhs!=2) || !create_return_values(0))
7470 SG_PRINT(
"Help is available for the following topics.\n"
7471 "-------------------------------------------\n\n");
7474 bool is_group_item=
false;
7488 SG_PRINT(
"\nUse sg('%shelp%s', '%s<topic>%s')"
7489 " to see the list of commands in this group, e.g.\n\n"
7490 "\tsg('%shelp%s', '%sFeatures%s')\n\n"
7491 "to see the list of commands for the 'Features' group.\n"
7492 "\nOr use sg('%shelp%s', '%sall%s')"
7493 " to see a brief listing of all commands.\n\nTo disable syntax"
7494 " highlighting (useful e.g. in the matlab GUI) use\n\n"
7495 "\tsg('syntax_highlight','OFF')\n",
7506 bool in_group=
false;
7508 char* command=get_string(clen);
7510 if (strmatch(
"doxygen", command) || strmatch(
"DOXYGEN", command))
7517 SG_PRINT(
"\\arg \\b %s \\verbatim %s%s%s \\endverbatim\n",
7525 SG_PRINT(
"\n\\section %s_sec %s\n",
7531 if (strmatch(
"all", command) || strmatch(
"ALL", command))
7546 SG_PRINT(
"\nCommands in group %s%s%s\n",
7570 found=strmatch(
sg_methods[i].command, command);
7575 SG_PRINT(
"Usage for %s%s%s\n\n\t%s%s%s%s%s\n",
7588 SG_PRINT(
"Commands in group %s%s%s\n\n",
7602 SG_PRINT(
"Could not find help for command %s.\n", command)
7605 SG_PRINT(
"\n\nUse sg('%shelp%s', '%s<command>%s')"
7606 " to see the usage pattern of a single command, e.g.\n\n"
7607 "\tsg('%shelp%s', '%sclassify%s')\n\n"
7608 " to see the usage pattern of the command 'classify'.\n",
7623 #ifdef TRACE_MEMORY_ALLOCS
7627 bool CSGInterface::cmd_whos()
7629 if ((m_nrhs!=1) || !create_return_values(0))
7632 #ifdef TRACE_MEMORY_ALLOCS
7633 SG_PRINT(
"Blocks allocated by shogun\n")
7634 list_memory_allocs();
7638 SG_PRINT(
"Requires shogun to be compiled with --enable-trace-mallocs\n")
7643 bool CSGInterface::cmd_send_command()
7648 char* arg=get_string(len);
7650 m_legacy_strptr=arg;
7652 char* command=get_str_from_str(len);
7658 if (strmatch(command,
sg_methods[i].command))
7662 m_nrhs=get_num_args_in_str()+1;
7666 SG_ERROR(
"Usage: %s%s%s\n\n\t%s%s%s%s%s\n",
7687 SG_ERROR(
"Non-supported legacy command %s.\n", command)
7694 bool CSGInterface::cmd_run_python()
7696 SG_ERROR(
"Only available in the elwms interface\n")
7700 bool CSGInterface::cmd_run_octave()
7702 SG_ERROR(
"Only available in the elwms interface\n")
7706 bool CSGInterface::cmd_run_r()
7708 SG_ERROR(
"Only available in the elwms interface\n")
7712 bool CSGInterface::cmd_pr_loqo()
7714 if (m_nrhs!=7 || !create_return_values(2))
7719 get_vector(c, lenc);
7726 get_matrix(H, nH, mH);
7732 get_matrix(A, nA, mA);
7738 get_vector(b, lenb);
7743 get_vector(l, lenl);
7748 get_vector(u, lenu);
7757 pr_loqo(n,m, c, H, A, b, l, u, x, y, 0, 5, 50, 0.05, 100, 0);
7773 void CSGInterface::print_prompt()
7784 char* CSGInterface::get_str_from_str_or_direct(int32_t& len)
7786 if (m_legacy_strptr)
7787 return get_str_from_str(len);
7789 return get_string(len);
7792 int32_t CSGInterface::get_int_from_int_or_str()
7794 if (m_legacy_strptr)
7797 char* str=get_str_from_str(len);
7798 int32_t val=strtol(str, NULL, 10);
7807 float64_t CSGInterface::get_real_from_real_or_str()
7809 if (m_legacy_strptr)
7812 char* str=get_str_from_str(len);
7822 bool CSGInterface::get_bool_from_bool_or_str()
7824 if (m_legacy_strptr)
7827 char* str=get_str_from_str(len);
7828 bool val=strtol(str, NULL, 10)!=0;
7837 void CSGInterface::get_vector_from_int_vector_or_str(int32_t*& vector, int32_t& len)
7839 if (m_legacy_strptr)
7841 len=get_vector_len_from_str(len);
7848 vector=SG_MALLOC(int32_t, len);
7851 for (int32_t i=0; i<len; i++)
7853 str=get_str_from_str(slen);
7854 vector[i]=strtol(str, NULL, 10);
7860 get_vector(vector, len);
7863 void CSGInterface::get_vector_from_real_vector_or_str(
7866 if (m_legacy_strptr)
7868 len=get_vector_len_from_str(len);
7878 for (int32_t i=0; i<len; i++)
7880 str=get_str_from_str(slen);
7881 vector[i]=strtod(str, NULL);
7887 get_vector(vector, len);
7890 int32_t CSGInterface::get_vector_len_from_str(int32_t expected_len)
7892 int32_t num_args=get_num_args_in_str();
7894 if (expected_len==0 || num_args==expected_len)
7896 else if (num_args==2*expected_len)
7899 return expected_len;
7902 SG_ERROR(
"Expected vector length %d does not match actual length %d.\n", expected_len, num_args)
7907 char* CSGInterface::get_str_from_str(int32_t& len)
7909 if (!m_legacy_strptr)
7913 while (m_legacy_strptr[i]!=
'\0' && !isspace(m_legacy_strptr[i]))
7917 char* str=SG_MALLOC(
char, len+1);
7918 for (i=0; i<len; i++)
7919 str[i]=m_legacy_strptr[i];
7923 if (m_legacy_strptr[len]==
'\0')
7924 m_legacy_strptr=NULL;
7927 m_legacy_strptr=m_legacy_strptr+len;
7934 int32_t CSGInterface::get_num_args_in_str()
7936 if (!m_legacy_strptr)
7942 while (m_legacy_strptr[i]!=
'\0')
7944 if (!isspace(m_legacy_strptr[i]) && !in_arg)
7949 else if (isspace(m_legacy_strptr[i]) && in_arg)
7962 bool CSGInterface::handle()
7974 SG_DEBUG(
"command: %s, nrhs %d\n", command, m_nrhs)
7978 if (strmatch(command,
sg_methods[i].command))
7989 SG_ERROR(
"Usage: %s%s%s\n\n\t%s%s%s%s%s\n",
8000 SG_ERROR(
"Non-supported command %s%s%s.\n",
8019 SG_ERROR(
"Unknown command %s%s%s.\n",