16 #include <shogun/lib/external/pr_loqo.h>
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 },
77 (&CSGInterface::cmd_pr_loqo),
79 "'Var1', Var1, 'Var2', Var2",
"results")
81 #endif //USE_GPL_SHOGUN
84 (&CSGInterface::cmd_load_features),
90 (&CSGInterface::cmd_save_features),
95 (&CSGInterface::cmd_clean_features),
100 (&CSGInterface::cmd_get_features),
105 (&CSGInterface::cmd_add_features),
111 (&CSGInterface::cmd_add_multiple_features),
117 (&CSGInterface::cmd_add_dotfeatures),
123 (&CSGInterface::cmd_set_features),
135 (&CSGInterface::cmd_set_reference_features),
140 (&CSGInterface::cmd_del_last_features),
145 (&CSGInterface::cmd_convert),
158 (&CSGInterface::cmd_reshape),
165 (&CSGInterface::cmd_load_labels),
171 (&CSGInterface::cmd_set_labels),
177 (&CSGInterface::cmd_get_labels),
182 {
"Kernel", NULL, NULL },
185 (&CSGInterface::cmd_set_kernel_normalization),
191 (&CSGInterface::cmd_set_kernel),
196 (&CSGInterface::cmd_add_kernel),
201 (&CSGInterface::cmd_del_last_kernel),
206 (&CSGInterface::cmd_init_kernel),
211 (&CSGInterface::cmd_clean_kernel),
216 (&CSGInterface::cmd_save_kernel),
221 (&CSGInterface::cmd_get_kernel_matrix),
226 (&CSGInterface::cmd_set_WD_position_weights),
231 (&CSGInterface::cmd_get_subkernel_weights),
236 (&CSGInterface::cmd_set_subkernel_weights),
241 (&CSGInterface::cmd_set_subkernel_weights_combined),
246 (&CSGInterface::cmd_get_dotfeature_weights_combined),
251 (&CSGInterface::cmd_set_dotfeature_weights_combined),
256 (&CSGInterface::cmd_set_last_subkernel_weights),
261 (&CSGInterface::cmd_get_WD_position_weights),
266 (&CSGInterface::cmd_get_last_subkernel_weights),
271 (&CSGInterface::cmd_compute_by_subkernels),
276 (&CSGInterface::cmd_init_kernel_optimization),
281 (&CSGInterface::cmd_get_kernel_optimization),
286 (&CSGInterface::cmd_delete_kernel_optimization),
291 (&CSGInterface::cmd_use_diagonal_speedup),
296 (&CSGInterface::cmd_set_kernel_optimization_type),
301 (&CSGInterface::cmd_set_solver),
306 (&CSGInterface::cmd_set_constraint_generator),
308 "|SVMLIGHT|LIGHT|SVMLIGHT_ONECLASS|GPBTSVM|MPDSVM|GNPPSVM|GMNPSVM"
313 (&CSGInterface::cmd_set_prior_probs),
318 (&CSGInterface::cmd_set_prior_probs_from_labels),
324 (&CSGInterface::cmd_resize_kernel_cache),
327 #endif //USE_SVMLIGHT
330 {
"Distance", NULL, NULL },
333 (&CSGInterface::cmd_set_distance),
338 (&CSGInterface::cmd_init_distance),
343 (&CSGInterface::cmd_get_distance_matrix),
348 {
"Classifier", NULL, NULL },
351 (&CSGInterface::cmd_classify),
356 (&CSGInterface::cmd_classify),
361 (&CSGInterface::cmd_classify_example),
366 (&CSGInterface::cmd_classify_example),
371 (&CSGInterface::cmd_get_classifier),
376 (&CSGInterface::cmd_get_classifier),
381 (&CSGInterface::cmd_new_classifier),
383 "|SVMLIGHT|LIGHT|LIGHT_ONECLASS|SVMLIN|GPBTSVM|MPDSVM|GNPPSVM|GMNPSVM"
384 "|WDSVMOCAS|SVMOCAS|SVMSGD|SVMBMRM|SVMPERF"
385 "|KERNELPERCEPTRON|PERCEPTRON|LIBLINEAR_LR|LIBLINEAR_L2|LDA"
390 (&CSGInterface::cmd_new_classifier),
392 "|LIBSVM|SVMLIGHT|LIGHT|LIGHT_ONECLASS|SVMLIN|GPBTSVM|MPDSVM|GNPPSVM|GMNPSVM"
393 "|WDSVMOCAS|SVMOCAS|SVMSGD|SVMBMRM|SVMPERF"
394 "|KERNELPERCEPTRON|PERCEPTRON|LIBLINEAR_LR|LIBLINEAR_L2|LDA"
399 (&CSGInterface::cmd_new_classifier),
404 (&CSGInterface::cmd_new_classifier),
409 (&CSGInterface::cmd_load_classifier),
414 (&CSGInterface::cmd_save_classifier),
419 (&CSGInterface::cmd_get_num_svms),
424 (&CSGInterface::cmd_get_svm),
429 (&CSGInterface::cmd_set_svm),
434 (&CSGInterface::cmd_set_linear_classifier),
439 (&CSGInterface::cmd_get_svm_objective),
444 (&CSGInterface::cmd_compute_svm_primal_objective),
449 (&CSGInterface::cmd_compute_svm_dual_objective),
454 (&CSGInterface::cmd_compute_svm_primal_objective),
459 (&CSGInterface::cmd_compute_mkl_dual_objective),
464 (&CSGInterface::cmd_compute_relative_mkl_duality_gap),
469 (&CSGInterface::cmd_compute_absolute_mkl_duality_gap),
474 (&CSGInterface::cmd_do_auc_maximization),
479 (&CSGInterface::cmd_set_perceptron_parameters),
484 (&CSGInterface::cmd_train_classifier),
489 (&CSGInterface::cmd_train_classifier),
494 (&CSGInterface::cmd_train_classifier),
499 (&CSGInterface::cmd_train_classifier),
504 (&CSGInterface::cmd_set_svm_qpsize),
509 (&CSGInterface::cmd_set_svm_max_qpsize),
514 (&CSGInterface::cmd_set_svm_bufsize),
519 (&CSGInterface::cmd_set_svm_C),
524 (&CSGInterface::cmd_set_svm_epsilon),
529 (&CSGInterface::cmd_set_svr_tube_epsilon),
534 (&CSGInterface::cmd_set_svm_nu),
539 (&CSGInterface::cmd_set_svm_mkl_parameters),
544 (&CSGInterface::cmd_set_elasticnet_lambda),
549 (&CSGInterface::cmd_set_mkl_block_norm),
554 (&CSGInterface::cmd_set_max_train_time),
559 (&CSGInterface::cmd_set_svm_shrinking_enabled),
564 (&CSGInterface::cmd_set_svm_batch_computation_enabled),
569 (&CSGInterface::cmd_set_svm_linadd_enabled),
574 (&CSGInterface::cmd_set_svm_bias_enabled),
579 (&CSGInterface::cmd_set_mkl_interleaved_enabled),
584 (&CSGInterface::cmd_set_krr_tau),
589 {
"Preprocessors", NULL, NULL },
592 (&CSGInterface::cmd_add_preproc),
597 (&CSGInterface::cmd_del_preproc),
602 (&CSGInterface::cmd_attach_preproc),
607 (&CSGInterface::cmd_clean_preproc),
611 {
"Converters", NULL, NULL },
614 (&CSGInterface::cmd_set_converter),
619 (&CSGInterface::cmd_apply_converter),
624 (&CSGInterface::cmd_embed),
629 {
"HMM", NULL, NULL },
632 (&CSGInterface::cmd_new_hmm),
637 (&CSGInterface::cmd_load_hmm),
642 (&CSGInterface::cmd_save_hmm),
647 (&CSGInterface::cmd_get_hmm),
652 (&CSGInterface::cmd_append_hmm),
657 (&CSGInterface::cmd_append_model),
662 (&CSGInterface::cmd_set_hmm),
667 (&CSGInterface::cmd_set_hmm_as),
672 (&CSGInterface::cmd_set_chop),
677 (&CSGInterface::cmd_set_pseudo),
682 (&CSGInterface::cmd_load_definitions),
687 (&CSGInterface::cmd_hmm_classify),
692 (&CSGInterface::cmd_one_class_linear_hmm_classify),
697 (&CSGInterface::cmd_one_class_hmm_classify),
702 (&CSGInterface::cmd_one_class_hmm_classify_example),
707 (&CSGInterface::cmd_hmm_classify_example),
712 (&CSGInterface::cmd_output_hmm),
717 (&CSGInterface::cmd_output_hmm_defined),
722 (&CSGInterface::cmd_hmm_likelihood),
727 (&CSGInterface::cmd_likelihood),
732 (&CSGInterface::cmd_save_likelihood),
737 (&CSGInterface::cmd_get_viterbi_path),
742 (&CSGInterface::cmd_viterbi_train_defined),
747 (&CSGInterface::cmd_viterbi_train),
752 (&CSGInterface::cmd_baum_welch_train),
757 (&CSGInterface::cmd_baum_welch_train_defined),
762 (&CSGInterface::cmd_baum_welch_trans_train),
767 (&CSGInterface::cmd_linear_train),
772 (&CSGInterface::cmd_save_path),
777 (&CSGInterface::cmd_convergence_criteria),
782 (&CSGInterface::cmd_normalize),
787 (&CSGInterface::cmd_add_states),
792 (&CSGInterface::cmd_permutation_entropy),
797 (&CSGInterface::cmd_relative_entropy),
802 (&CSGInterface::cmd_entropy),
807 (&CSGInterface::cmd_set_feature_matrix),
812 (&CSGInterface::cmd_set_feature_matrix_sparse),
817 (&CSGInterface::cmd_new_plugin_estimator),
822 (&CSGInterface::cmd_train_estimator),
827 (&CSGInterface::cmd_plugin_estimate_classify_example),
832 (&CSGInterface::cmd_plugin_estimate_classify),
837 (&CSGInterface::cmd_set_plugin_estimate),
842 (&CSGInterface::cmd_get_plugin_estimate),
845 {
"Signals", NULL, NULL },
848 (&CSGInterface::cmd_signals_set_model),
853 (&CSGInterface::cmd_signals_set_positions),
858 (&CSGInterface::cmd_signals_set_labels),
863 (&CSGInterface::cmd_signals_set_split),
868 (&CSGInterface::cmd_signals_set_train_mask),
873 (&CSGInterface::cmd_signals_add_feature),
878 (&CSGInterface::cmd_signals_add_kernel),
883 (&CSGInterface::cmd_signals_run),
886 {
"Structure", NULL, NULL },
889 (&CSGInterface::cmd_best_path),
894 (&CSGInterface::cmd_best_path_2struct),
906 "prob" USAGE_COMMA
"path" USAGE_COMMA
"pos")
910 (&CSGInterface::cmd_set_plif_struct),
923 (&CSGInterface::cmd_get_plif_struct),
936 (&CSGInterface::cmd_precompute_subkernels),
941 (&CSGInterface::cmd_precompute_content_svms),
948 (&CSGInterface::cmd_get_lin_feat),
953 (&CSGInterface::cmd_set_lin_feat),
958 (&CSGInterface::cmd_init_dyn_prog),
963 (&CSGInterface::cmd_clean_up_dyn_prog),
968 (&CSGInterface::cmd_init_intron_list),
975 (&CSGInterface::cmd_precompute_tiling_features),
982 (&CSGInterface::cmd_long_transition_settings),
990 (&CSGInterface::cmd_set_model),
999 (&CSGInterface::cmd_best_path_trans),
1006 "prob" USAGE_COMMA
"path" USAGE_COMMA
"pos")
1010 (&CSGInterface::cmd_best_path_trans_deriv),
1027 USAGE_COMMA
"q_deriv"
1028 USAGE_COMMA
"cmd_deriv"
1029 USAGE_COMMA
"penalties_deriv"
1030 USAGE_COMMA
"my_scores"
1031 USAGE_COMMA
"my_loss")
1034 {
"POIM", NULL, NULL },
1037 (&CSGInterface::cmd_compute_POIM_WD),
1042 (&CSGInterface::cmd_get_SPEC_consensus),
1047 (&CSGInterface::cmd_get_SPEC_scoring),
1052 (&CSGInterface::cmd_get_WD_consensus),
1057 (&CSGInterface::cmd_get_WD_scoring),
1062 {
"Utility", NULL, NULL },
1065 (&CSGInterface::cmd_crc),
1070 (&CSGInterface::cmd_system),
1075 (&CSGInterface::cmd_exit),
1080 (&CSGInterface::cmd_exit),
1085 (&CSGInterface::cmd_exec),
1090 (&CSGInterface::cmd_set_output),
1095 (&CSGInterface::cmd_set_threshold),
1100 (&CSGInterface::cmd_init_random),
1105 (&CSGInterface::cmd_set_num_threads),
1110 (&CSGInterface::cmd_translate_string),
1112 "string, order, start",
"translation")
1116 (&CSGInterface::cmd_clear),
1121 (&CSGInterface::cmd_tic),
1126 (&CSGInterface::cmd_toc),
1131 (&CSGInterface::cmd_print),
1136 (&CSGInterface::cmd_echo),
1141 (&CSGInterface::cmd_loglevel),
1146 (&CSGInterface::cmd_syntax_highlight),
1151 (&CSGInterface::cmd_progress),
1156 (&CSGInterface::cmd_get_version),
1161 (&CSGInterface::cmd_help),
1166 (&CSGInterface::cmd_whos),
1171 (&CSGInterface::cmd_send_command),
1176 (&CSGInterface::cmd_run_python),
1178 "'Var1', Var1, 'Var2', Var2,..., python_function",
"results")
1182 (&CSGInterface::cmd_run_octave),
1184 "'Var1', Var1, 'Var2', Var2,..., octave_function",
"results")
1188 (&CSGInterface::cmd_run_r),
1190 "'Var1', Var1, 'Var2', Var2,..., r_function",
"results")
1196 CSGInterface::CSGInterface(
bool print_copyright)
1212 if (print_copyright)
1214 version->print_version();
1215 SG_PRINT(
"( seeding random number generator with %u (seed size %d))\n",
1218 SG_PRINT(
"initializing log-table (size=%i*%i*%i=%2.1fMB) ... ) ",
1229 CSGInterface::~CSGInterface()
1231 delete ui_classifier;
1233 delete ui_pluginestimate;
1239 delete ui_structure;
1243 delete ui_converter;
1249 void CSGInterface::reset()
1255 m_legacy_strptr=NULL;
1260 void CSGInterface::translate_arg(CSGInterface* source, CSGInterface* target)
1262 switch (source->get_argument_type())
1265 target->set_int(source->get_int());
1268 target->set_real(source->get_real());
1271 target->set_bool(source->get_bool());
1277 source->get_vector(v, len);
1278 target->set_vector(v, len);
1286 source->get_vector(v, len);
1287 target->set_vector(v, len);
1295 source->get_vector(v, len);
1296 target->set_vector(v, len);
1304 source->get_vector(v, len);
1305 target->set_vector(v, len);
1313 source->get_vector(v, len);
1314 target->set_vector(v, len);
1318 case VECTOR_SHORTREAL:
1322 source->get_vector(v, len);
1323 target->set_vector(v, len);
1331 source->get_vector(v, len);
1332 target->set_vector(v, len);
1340 source->get_vector(v, len);
1341 target->set_vector(v, len);
1349 int32_t max_str_len=0;
1351 source->get_string_list(strs, num_str, max_str_len);
1352 target->set_string_list(strs, num_str);
1359 int32_t max_str_len=0;
1361 source->get_string_list(strs, num_str,max_str_len);
1362 target->set_string_list(strs, num_str);
1369 int32_t max_str_len=0;
1371 source->get_string_list(strs, num_str,max_str_len);
1372 target->set_string_list(strs, num_str);
1379 int32_t max_str_len=0;
1381 source->get_string_list(strs, num_str, max_str_len);
1382 target->set_string_list(strs, num_str);
1389 int32_t max_str_len=0;
1391 source->get_string_list(strs, num_str, max_str_len);
1392 target->set_string_list(strs, num_str);
1400 int32_t* fmatrix=NULL;
1401 source->get_matrix(fmatrix, num_feat, num_vec);
1402 target->set_matrix(fmatrix, num_feat, num_vec);
1411 source->get_matrix(fmatrix, num_feat, num_vec);
1412 target->set_matrix(fmatrix, num_feat, num_vec);
1420 int16_t* fmatrix=NULL;
1421 source->get_matrix(fmatrix, num_feat, num_vec);
1422 target->set_matrix(fmatrix, num_feat, num_vec);
1426 case DENSE_SHORTREAL:
1431 source->get_matrix(fmatrix, num_feat, num_vec);
1432 target->set_matrix(fmatrix, num_feat, num_vec);
1440 uint16_t* fmatrix=NULL;
1441 source->get_matrix(fmatrix, num_feat, num_vec);
1442 target->set_matrix(fmatrix, num_feat, num_vec);
1529 source->get_sparse_matrix(fmatrix, num_feat, num_vec);
1531 for (int32_t i=0; i<num_vec; i++)
1532 nnz+=fmatrix[i].num_feat_entries;
1533 target->set_sparse_matrix(fmatrix, num_feat, num_vec, nnz);
1550 bool CSGInterface::cmd_load_features()
1552 if (m_nrhs<8 || !create_return_values(0))
1556 char* filename=get_str_from_str_or_direct(len);
1557 char* fclass=get_str_from_str_or_direct(len);
1558 char* type=get_str_from_str_or_direct(len);
1559 char* target=get_str_from_str_or_direct(len);
1560 int32_t size=get_int_from_int_or_str();
1561 int32_t comp_features=get_int_from_int_or_str();
1563 bool success=ui_features->load(
1564 filename, fclass, type, target, size, comp_features);
1573 bool CSGInterface::cmd_save_features()
1575 if (m_nrhs<5 || !create_return_values(0))
1579 char* filename=get_str_from_str_or_direct(len);
1580 char* type=get_str_from_str_or_direct(len);
1581 char* target=get_str_from_str_or_direct(len);
1583 bool success=ui_features->save(filename, type, target);
1591 bool CSGInterface::cmd_clean_features()
1593 if (m_nrhs<2 || !create_return_values(0))
1597 char* target=get_str_from_str_or_direct(len);
1599 bool success=ui_features->clean(target);
1605 bool CSGInterface::cmd_get_features()
1607 if (m_nrhs!=2 || !create_return_values(1))
1611 char* target=get_string(tlen);
1614 if (strmatch(target,
"TRAIN"))
1615 feat=ui_features->get_train_features();
1616 else if (strmatch(target,
"TEST"))
1617 feat=ui_features->get_test_features();
1621 SG_ERROR(
"Unknown target, neither TRAIN nor TEST.\n")
1639 set_matrix(fmatrix, num_feat, num_vec);
1646 set_matrix(fmatrix, num_feat, num_vec);
1653 set_matrix(fmatrix, num_feat, num_vec);
1660 set_matrix(fmatrix, num_feat, num_vec);
1667 set_matrix(fmatrix, num_feat, num_vec);
1674 set_matrix(fmatrix, num_feat, num_vec);
1681 set_matrix(fmatrix, num_feat, num_vec);
1698 get_num_nonzero_entries();
1715 int32_t max_str_len=0;
1721 set_string_list(fmatrix, num_str);
1728 set_string_list(fmatrix, num_str);
1735 set_string_list(fmatrix, num_str);
1764 bool CSGInterface::cmd_add_features()
1766 if (m_nrhs<3 || !create_return_values(0))
1769 return do_set_features(
true,
false);
1772 bool CSGInterface::cmd_add_multiple_features()
1774 if ((m_nrhs!=4 && m_nrhs<5) || !create_return_values(0))
1777 int32_t repetitions=get_int();
1781 return do_set_features(
true,
false, repetitions);
1784 bool CSGInterface::cmd_add_dotfeatures()
1786 if (m_nrhs<3 || !create_return_values(0))
1789 return do_set_features(
true,
true);
1792 bool CSGInterface::cmd_set_features()
1794 if (m_nrhs<3 || !create_return_values(0))
1797 return do_set_features(
false,
false);
1800 bool CSGInterface::do_set_features(
bool add,
bool check_dot, int32_t repetitions)
1803 char* target=get_string(tlen);
1804 if (!strmatch(target,
"TRAIN") && !strmatch(target,
"TEST"))
1807 SG_ERROR(
"Unknown target, neither TRAIN nor TEST.\n")
1814 switch (get_argument_type())
1819 get_sparse_matrix(fmatrix, num_feat, num_vec);
1828 get_matrix(fmatrix, num_feat, num_vec);
1842 int32_t* fmatrix=NULL;
1843 get_matrix(fmatrix, num_feat, num_vec);
1853 int16_t* fmatrix=NULL;
1854 get_matrix(fmatrix, num_feat, num_vec);
1864 uint16_t* fmatrix=NULL;
1865 get_matrix(fmatrix, num_feat, num_vec);
1873 case DENSE_SHORTREAL:
1876 get_matrix(fmatrix, num_feat, num_vec);
1887 SG_ERROR(
"Please specify alphabet!\n")
1890 int32_t max_str_len=0;
1892 get_string_list(fmatrix, num_str, max_str_len);
1894 int32_t alphabet_len=0;
1895 char* alphabet_str=get_string(alphabet_len);
1898 if (strmatch(alphabet_str,
"DNABINFILE"))
1900 SG_FREE(alphabet_str);
1902 ASSERT(fmatrix[0].
string)
1912 SG_ERROR(
"Couldn't load DNA features from file.\n")
1919 bool convert_to_word=
false;
1920 bool convert_to_ulong=
false;
1922 if (strmatch(alphabet_str,
"DNAWORD"))
1925 convert_to_word=
true;
1927 else if (strmatch(alphabet_str,
"DNAULONG"))
1930 convert_to_ulong=
true;
1933 alphabet=
new CAlphabet(alphabet_str, alphabet_len);
1936 SG_FREE(alphabet_str);
1944 SG_ERROR(
"Couldnt set byte string features.\n")
1949 if (convert_to_word || convert_to_ulong)
1950 convert_to_bitembedding(feat, convert_to_word, convert_to_ulong);
1953 obtain_from_single_string(feat);
1960 SG_ERROR(
"Please specify alphabet!\n")
1963 int32_t max_str_len=0;
1965 get_string_list(fmatrix, num_str, max_str_len);
1967 int32_t alphabet_len=0;
1968 char* alphabet_str=get_string(alphabet_len);
1971 alphabet=
new CAlphabet(alphabet_str, alphabet_len);
1972 SG_FREE(alphabet_str);
1979 SG_ERROR(
"Couldnt set byte string features.\n")
1986 SG_ERROR(
"Wrong argument type %d.\n", get_argument_type())
1992 SG_ERROR(
"Feature type not supported by DOT Features\n")
1995 if (strmatch(target,
"TRAIN"))
1998 ui_features->set_train_features(feat);
2001 for (int32_t i=0; i<repetitions; i++)
2002 ui_features->add_train_dotfeatures((
CDotFeatures*) feat);
2006 for (int32_t i=0; i<repetitions; i++)
2007 ui_features->add_train_features(feat);
2013 ui_features->set_test_features(feat);
2016 for (int32_t i=0; i<repetitions; i++)
2017 ui_features->add_test_dotfeatures((
CDotFeatures*) feat);
2021 for (int32_t i=0; i<repetitions; i++)
2022 ui_features->add_test_features(feat);
2031 bool CSGInterface::cmd_set_reference_features()
2033 if (m_nrhs<3 || !create_return_values(0))
2037 char* target=get_str_from_str_or_direct(len);
2039 bool success=ui_features->set_reference_features(target);
2045 bool CSGInterface::cmd_del_last_features()
2047 if (m_nrhs<2 || !create_return_values(0))
2051 char* target=get_str_from_str_or_direct(len);
2052 bool success=ui_features->del_last_feature_obj(target);
2058 bool CSGInterface::cmd_convert()
2060 if (m_nrhs<5 || !create_return_values(0))
2064 char* target=get_str_from_str_or_direct(len);
2065 CFeatures* features=ui_features->get_convert_features(target);
2069 SG_ERROR(
"No \"%s\" features available.\n", target)
2072 char* from_class=get_str_from_str_or_direct(len);
2073 char* from_type=get_str_from_str_or_direct(len);
2074 char* to_class=get_str_from_str_or_direct(len);
2075 char* to_type=get_str_from_str_or_direct(len);
2078 if (strmatch(from_class,
"SIMPLE"))
2080 if (strmatch(from_type,
"REAL"))
2082 if (strmatch(to_class,
"SPARSE") &&
2083 strmatch(to_type,
"REAL"))
2085 result=ui_features->convert_simple_real_to_sparse_real(
2092 else if (strmatch(from_type,
"CHAR"))
2094 if (strmatch(to_class,
"STRING") &&
2095 strmatch(to_type,
"CHAR"))
2097 result=ui_features->convert_simple_char_to_string_char(
2100 else if (strmatch(to_class,
"SIMPLE"))
2102 if (strmatch(to_type,
"ALIGN") && m_nrhs==8)
2104 float64_t gap_cost=get_real_from_real_or_str();
2105 result=ui_features->convert_simple_char_to_simple_align(
2115 else if (strmatch(from_type,
"WORD"))
2117 if (strmatch(to_class,
"SIMPLE") &&
2118 strmatch(to_type,
"SALZBERG"))
2120 result=ui_features->convert_simple_word_to_simple_salzberg(
2131 else if (strmatch(from_class,
"SPARSE"))
2133 if (strmatch(from_type,
"REAL"))
2135 if (strmatch(to_class,
"SIMPLE") &&
2136 strmatch(to_type,
"REAL"))
2138 result=ui_features->convert_sparse_real_to_simple_real(
2148 else if (strmatch(from_class,
"STRING"))
2150 if (strmatch(from_type,
"CHAR"))
2152 if (strmatch(to_class,
"STRING"))
2161 order=get_int_from_int_or_str();
2165 start=get_int_from_int_or_str();
2169 gap=get_int_from_int_or_str();
2173 char* rev_str=get_str_from_str_or_direct(len);
2183 if (strmatch(to_type,
"BYTE"))
2185 result=ui_features->convert_string_char_to_string_generic<char,uint8_t>(
2189 else if (strmatch(to_type,
"WORD"))
2191 result=ui_features->convert_string_char_to_string_generic<char,uint16_t>(
2195 else if (strmatch(to_type,
"ULONG"))
2197 result=ui_features->convert_string_char_to_string_generic<char,uint64_t>(
2208 else if (strmatch(from_type,
"BYTE"))
2210 if (strmatch(to_class,
"STRING"))
2219 order=get_int_from_int_or_str();
2223 start=get_int_from_int_or_str();
2227 gap=get_int_from_int_or_str();
2231 char* rev_str=get_str_from_str_or_direct(len);
2241 if (strmatch(to_type,
"WORD"))
2243 result=ui_features->convert_string_char_to_string_generic<uint8_t,uint16_t>(
2247 else if (strmatch(to_type,
"ULONG"))
2249 result=ui_features->convert_string_char_to_string_generic<uint8_t,uint64_t>(
2260 else if (strmatch(from_type,
"WORD"))
2262 if (strmatch(to_class,
"SIMPLE") && strmatch(to_type,
"TOP"))
2264 result=ui_features->convert_string_word_to_simple_top(
2267 else if (strmatch(to_class,
"SPEC") && strmatch(to_type,
"WORD") && m_nrhs==7)
2269 bool use_norm=get_bool();
2277 else if (strmatch(to_class,
"SIMPLE") && strmatch(to_type,
"FK"))
2279 result=ui_features->convert_string_word_to_simple_fk(
2288 if (result && ui_features->set_convert_features(result, target))
2289 SG_INFO(
"Conversion was successful.\n")
2294 SG_FREE(from_class);
2298 return (result!=NULL);
2301 void CSGInterface::convert_to_bitembedding(
CFeatures* &features,
bool convert_to_word,
bool convert_to_ulong)
2315 if (convert_to_word)
2317 SG_INFO(
"Converting into word-bitembedding\n")
2318 features=ui_features->convert_string_char_to_string_generic<char,uint16_t>(
2322 if (convert_to_ulong)
2324 SG_INFO(
"Converting into ulong-bitembedding\n")
2325 features=ui_features->convert_string_char_to_string_generic<char,uint64_t>(
2330 void CSGInterface::obtain_from_single_string(
CFeatures* features)
2336 char* str=get_string(len);
2339 if (strmatch(str,
"from_position_list"))
2341 obtain_from_position_list(features);
2343 else if (strmatch(str,
"slide_window"))
2345 obtain_by_sliding_window(features);
2351 bool CSGInterface::obtain_from_position_list(
CFeatures* features)
2353 int32_t winsize=get_int();
2355 int32_t* shifts=NULL;
2356 int32_t num_shift=0;
2357 get_vector(shifts, num_shift);
2363 SG_DEBUG(
"winsize: %d num_shifts: %d skip: %d\n", winsize, num_shift, skip)
2367 for (int32_t i=0; i<num_shift; i++)
2379 obtain_by_position_list(winsize, &positions, skip)>0);
2385 obtain_by_position_list(winsize, &positions, skip)>0);
2391 obtain_by_position_list(winsize, &positions, skip)>0);
2397 obtain_by_position_list(winsize, &positions, skip)>0);
2401 SG_ERROR(
"Unsupported string features type.\n")
2407 bool CSGInterface::obtain_by_sliding_window(
CFeatures* features)
2409 int32_t winsize=get_int();
2410 int32_t shift=get_int();
2432 SG_SERROR(
"Unsupported string features type.\n")
2439 bool CSGInterface::cmd_reshape()
2441 if (m_nrhs<4 || !create_return_values(0))
2445 char* target=get_str_from_str_or_direct(len);
2446 int32_t num_feat=get_int_from_int_or_str();
2447 int32_t num_vec=get_int_from_int_or_str();
2449 bool success=ui_features->reshape(target, num_feat, num_vec);
2455 bool CSGInterface::cmd_load_labels()
2457 if (m_nrhs<4 || !create_return_values(0))
2461 char* filename=get_str_from_str_or_direct(len);
2462 char* target=get_str_from_str_or_direct(len);
2464 bool success=ui_labels->load(filename, target);
2471 bool CSGInterface::cmd_set_labels()
2473 if (m_nrhs!=3 || !create_return_values(0))
2477 char* target=get_string(tlen);
2478 if (!strmatch(target,
"TRAIN") && !strmatch(target,
"TEST"))
2481 SG_ERROR(
"Unknown target, neither TRAIN nor TEST.\n")
2486 get_vector(lab, len);
2488 CLabels* labels=ui_labels->infer_labels(lab, len);
2492 if (strmatch(target,
"TRAIN"))
2493 ui_labels->set_train_labels(labels);
2494 else if (strmatch(target,
"TEST"))
2495 ui_labels->set_test_labels(labels);
2499 SG_ERROR(
"Unknown target, neither TRAIN nor TEST.\n")
2506 bool CSGInterface::cmd_get_labels()
2508 if (m_nrhs!=2 || !create_return_values(1))
2512 char* target=get_string(tlen);
2515 if (strmatch(target,
"TRAIN"))
2516 labels=ui_labels->get_train_labels();
2517 else if (strmatch(target,
"TEST"))
2518 labels=ui_labels->get_test_labels();
2522 SG_ERROR(
"Unknown target, neither TRAIN nor TEST.\n")
2539 bool CSGInterface::cmd_set_kernel_normalization()
2541 if (m_nrhs<2 || !create_return_values(0))
2545 char* normalization=get_string(len);
2555 bool success=ui_kernel->set_normalization(normalization, c, r);
2557 SG_FREE(normalization);
2561 bool CSGInterface::cmd_set_kernel()
2563 if (m_nrhs<2 || !create_return_values(0))
2566 SG_DEBUG(
"SGInterface: set_kernel\n")
2567 CKernel* kernel=create_kernel();
2568 return ui_kernel->set_kernel(kernel);
2571 bool CSGInterface::cmd_add_kernel()
2573 if (m_nrhs<3 || !create_return_values(0))
2576 float64_t weight=get_real_from_real_or_str();
2579 CKernel* kernel=create_kernel();
2581 SG_DEBUG(
"SGInterface: add_kernel\n")
2582 return ui_kernel->add_kernel(kernel, weight);
2585 bool CSGInterface::cmd_del_last_kernel()
2587 if (m_nrhs<1 || !create_return_values(0))
2590 return ui_kernel->del_last_kernel();
2593 CKernel* CSGInterface::create_kernel()
2597 char* type=get_str_from_str_or_direct(len);
2599 SG_DEBUG(
"set_kernel with type: %s\n", type)
2601 if (strmatch(type,
"COMBINED"))
2606 int32_t size=get_int_from_int_or_str();
2607 bool append_subkernel_weights=
false;
2609 append_subkernel_weights=get_bool_from_bool_or_str();
2611 kernel=ui_kernel->create_combined(size, append_subkernel_weights);
2613 else if (strmatch(type,
"DISTANCE"))
2618 int32_t size=get_int_from_int_or_str();
2621 width=get_real_from_real_or_str();
2623 kernel=ui_kernel->create_distance(size, width);
2625 else if (strmatch(type,
"WAVELET"))
2631 char* dtype=get_str_from_str_or_direct(len);
2632 if (strmatch(dtype,
"REAL"))
2634 int32_t size=get_int_from_int_or_str();
2640 Wdilation=get_real_from_real_or_str();
2643 Wtranslation=get_real_from_real_or_str();
2646 kernel=ui_kernel->create_sigmoid(size, Wdilation, Wtranslation);
2651 else if (strmatch(type,
"LINEAR"))
2658 char* dtype=get_str_from_str_or_direct(len);
2659 int32_t size=get_int_from_int_or_str();
2662 scale=get_real_from_real_or_str();
2664 if (strmatch(dtype,
"BYTE"))
2665 kernel=ui_kernel->create_linearbyte(size, scale);
2666 else if (strmatch(dtype,
"WORD"))
2667 kernel=ui_kernel->create_linearword(size, scale);
2668 else if (strmatch(dtype,
"CHAR"))
2669 kernel=ui_kernel->create_linearstring(size, scale);
2670 else if (strmatch(dtype,
"REAL"))
2671 kernel=ui_kernel->create_linear(size, scale);
2672 else if (strmatch(dtype,
"SPARSEREAL"))
2673 kernel=ui_kernel->create_sparselinear(size, scale);
2677 else if (strmatch(type,
"HISTOGRAM"))
2682 char* dtype=get_str_from_str_or_direct(len);
2683 if (strmatch(dtype,
"WORD"))
2685 int32_t size=get_int_from_int_or_str();
2686 kernel=ui_kernel->create_histogramword(size);
2691 else if (strmatch(type,
"SALZBERG"))
2696 char* dtype=get_str_from_str_or_direct(len);
2697 if (strmatch(dtype,
"WORD"))
2699 int32_t size=get_int_from_int_or_str();
2700 kernel=ui_kernel->create_salzbergword(size);
2705 else if (strmatch(type,
"POLYMATCH"))
2710 char* dtype=get_str_from_str_or_direct(len);
2711 int32_t size=get_int_from_int_or_str();
2713 bool inhomogene=
false;
2714 bool normalize=
true;
2718 degree=get_int_from_int_or_str();
2721 inhomogene=get_bool_from_bool_or_str();
2723 normalize=get_bool_from_bool_or_str();
2727 if (strmatch(dtype,
"CHAR"))
2729 kernel=ui_kernel->create_polymatchstring(
2730 size, degree, inhomogene, normalize);
2732 else if (strmatch(dtype,
"WORD"))
2734 kernel=ui_kernel->create_polymatchwordstring(
2735 size, degree, inhomogene, normalize);
2740 else if (strmatch(type,
"MATCH"))
2745 char* dtype=get_str_from_str_or_direct(len);
2746 if (strmatch(dtype,
"WORD"))
2748 int32_t size=get_int_from_int_or_str();
2750 bool normalize=
true;
2753 d=get_int_from_int_or_str();
2755 normalize=get_bool_from_bool_or_str();
2757 kernel=ui_kernel->create_matchwordstring(size, d, normalize);
2762 else if (strmatch(type,
"WEIGHTEDCOMMSTRING") || strmatch(type,
"COMMSTRING"))
2764 char* dtype=get_str_from_str_or_direct(len);
2765 int32_t size=get_int_from_int_or_str();
2766 bool use_sign=
false;
2767 char* norm_str=NULL;
2771 use_sign=get_bool_from_bool_or_str();
2774 norm_str=get_str_from_str_or_direct(len);
2777 if (strmatch(dtype,
"WORD"))
2779 if (strmatch(type,
"WEIGHTEDCOMMSTRING"))
2781 kernel=ui_kernel->create_commstring(
2784 else if (strmatch(type,
"COMMSTRING"))
2786 kernel=ui_kernel->create_commstring(
2790 else if (strmatch(dtype,
"ULONG"))
2792 kernel=ui_kernel->create_commstring(
2799 else if (strmatch(type,
"CHI2"))
2804 char* dtype=get_str_from_str_or_direct(len);
2805 if (strmatch(dtype,
"REAL"))
2807 int32_t size=get_int_from_int_or_str();
2811 width=get_real_from_real_or_str();
2813 kernel=ui_kernel->create_chi2(size, width);
2818 else if (strmatch(type,
"FIXEDDEGREE"))
2823 char* dtype=get_str_from_str_or_direct(len);
2824 if (strmatch(dtype,
"CHAR"))
2826 int32_t size=get_int_from_int_or_str();
2829 d=get_int_from_int_or_str();
2831 kernel=ui_kernel->create_fixeddegreestring(size, d);
2836 else if (strmatch(type,
"LOCALALIGNMENT"))
2841 char* dtype=get_str_from_str_or_direct(len);
2842 if (strmatch(dtype,
"CHAR"))
2844 int32_t size=get_int_from_int_or_str();
2846 kernel=ui_kernel->create_localalignmentstring(size);
2851 else if (strmatch(type,
"OLIGO"))
2856 char* dtype=get_str_from_str_or_direct(len);
2857 if (strmatch(dtype,
"CHAR"))
2859 int32_t size=get_int_from_int_or_str();
2860 int32_t k=get_int_from_int_or_str();
2861 float64_t w=get_real_from_real_or_str();
2863 kernel=ui_kernel->create_oligo(size, k, w);
2868 else if (strmatch(type,
"WEIGHTEDDEGREEPOS2") ||
2869 strmatch(type,
"WEIGHTEDDEGREEPOS2_NONORM"))
2874 char* dtype=get_str_from_str_or_direct(len);
2875 if (strmatch(dtype,
"CHAR") || strmatch(dtype,
"STRING"))
2877 int32_t size=get_int_from_int_or_str();
2878 int32_t order=get_int_from_int_or_str();
2879 int32_t max_mismatch=get_int_from_int_or_str();
2880 int32_t length=get_int_from_int_or_str();
2881 int32_t* shifts=NULL;
2883 get_vector_from_int_vector_or_str(shifts, l);
2887 bool use_normalization=
true;
2888 if (strmatch(type,
"WEIGHTEDDEGREEPOS2_NONORM"))
2889 use_normalization=
false;
2891 kernel=ui_kernel->create_weighteddegreepositionstring2(
2892 size, order, max_mismatch, shifts, length,
2900 else if (strmatch(type,
"WEIGHTEDDEGREEPOS3"))
2905 char* dtype=get_str_from_str_or_direct(len);
2906 if (strmatch(dtype,
"CHAR") || strmatch(dtype,
"STRING"))
2908 int32_t size=get_int_from_int_or_str();
2909 int32_t order=get_int_from_int_or_str();
2910 int32_t max_mismatch=get_int_from_int_or_str();
2911 int32_t length=get_int_from_int_or_str();
2912 int32_t mkl_stepsize=get_int_from_int_or_str();
2913 int32_t* shifts=NULL;
2915 get_vector_from_int_vector_or_str(shifts, l);
2919 if (m_nrhs>9+length)
2921 get_vector_from_real_vector_or_str(
2922 position_weights, length);
2925 kernel=ui_kernel->create_weighteddegreepositionstring3(
2926 size, order, max_mismatch, shifts, length,
2927 mkl_stepsize, position_weights);
2929 SG_FREE(position_weights);
2935 else if (strmatch(type,
"WEIGHTEDDEGREEPOS"))
2940 char* dtype=get_str_from_str_or_direct(len);
2941 if (strmatch(dtype,
"CHAR") || strmatch(dtype,
"STRING"))
2943 int32_t size=get_int_from_int_or_str();
2945 int32_t max_mismatch=0;
2952 order=get_int_from_int_or_str();
2956 max_mismatch=get_int_from_int_or_str();
2960 length=get_int_from_int_or_str();
2964 center=get_int_from_int_or_str();
2967 step=get_real_from_real_or_str();
2973 kernel=ui_kernel->create_weighteddegreepositionstring(
2974 size, order, max_mismatch, length, center, step);
2979 else if (strmatch(type,
"WEIGHTEDDEGREE"))
2984 char* dtype=get_str_from_str_or_direct(len);
2985 if (strmatch(dtype,
"CHAR") || strmatch(dtype,
"STRING"))
2987 int32_t size=get_int_from_int_or_str();
2989 int32_t max_mismatch=0;
2990 bool use_normalization=
true;
2991 int32_t mkl_stepsize=1;
2992 bool block_computation=
true;
2993 int32_t single_degree=-1;
2997 order=get_int_from_int_or_str();
3001 max_mismatch=get_int_from_int_or_str();
3005 use_normalization=get_bool_from_bool_or_str();
3009 mkl_stepsize=get_int_from_int_or_str();
3013 block_computation=get_int_from_int_or_str();
3016 single_degree=get_int_from_int_or_str();
3023 kernel=ui_kernel->create_weighteddegreestring(
3024 size, order, max_mismatch, use_normalization,
3025 mkl_stepsize, block_computation, single_degree);
3030 else if (strmatch(type,
"WEIGHTEDDEGREERBF"))
3035 char* dtype=get_str_from_str_or_direct(len);
3036 int32_t size=get_int_from_int_or_str();
3037 int32_t nof_properties=get_int_from_int_or_str();
3042 degree=get_int_from_int_or_str();
3045 width=get_real_from_real_or_str();
3051 kernel=ui_kernel->create_weighteddegreerbf(size, degree, nof_properties, width);
3056 else if (strmatch(type,
"SPECTRUMMISMATCHRBF"))
3061 char* dtype=get_str_from_str_or_direct(len);
3062 if (strmatch(dtype,
"CHAR") || strmatch(dtype,
"STRING"))
3064 int32_t size=get_int_from_int_or_str();
3065 int32_t degree=get_int_from_int_or_str();
3066 int32_t max_mismatch=get_int_from_int_or_str();
3067 float64_t width=get_real_from_real_or_str();
3075 get_matrix(helper_matrix, N, M);
3077 if (N == 128 && M == 128)
3080 memcpy(AA_matrix, helper_matrix, N*M*
sizeof(
float64_t)) ;
3081 kernel=ui_kernel->create_spectrummismatchrbf(size, AA_matrix, 128, 128, max_mismatch, degree, width);
3085 SG_ERROR(
"Matrix size %d %d\n", N, M)
3092 else if (strmatch(type,
"SLIK") || strmatch(type,
"LIK"))
3097 char* dtype=get_str_from_str_or_direct(len);
3098 if (strmatch(dtype,
"CHAR"))
3100 int32_t size=get_int_from_int_or_str();
3102 int32_t inner_degree=3;
3103 int32_t outer_degree=1;
3107 length=get_int_from_int_or_str();
3111 inner_degree=get_int_from_int_or_str();
3114 outer_degree=get_int_from_int_or_str();
3118 if (strmatch(type,
"SLIK"))
3120 kernel=ui_kernel->create_localityimprovedstring(
3121 size, length, inner_degree, outer_degree,
3126 kernel=ui_kernel->create_localityimprovedstring(
3127 size, length, inner_degree, outer_degree,
3134 else if (strmatch(type,
"POLY"))
3139 char* dtype=get_str_from_str_or_direct(len);
3140 int32_t size=get_int_from_int_or_str();
3142 bool inhomogene=
false;
3143 bool normalize=
true;
3147 degree=get_int_from_int_or_str();
3151 inhomogene=get_bool_from_bool_or_str();
3154 normalize=get_bool_from_bool_or_str();
3158 if (strmatch(dtype,
"REAL"))
3160 kernel=ui_kernel->create_poly(
3161 size, degree, inhomogene, normalize);
3163 else if (strmatch(dtype,
"SPARSEREAL"))
3165 kernel=ui_kernel->create_sparsepoly(
3166 size, degree, inhomogene, normalize);
3171 else if (strmatch(type,
"SIGMOID"))
3176 char* dtype=get_str_from_str_or_direct(len);
3177 if (strmatch(dtype,
"REAL"))
3179 int32_t size=get_int_from_int_or_str();
3185 gamma=get_real_from_real_or_str();
3188 coef0=get_real_from_real_or_str();
3191 kernel=ui_kernel->create_sigmoid(size, gamma, coef0);
3196 else if (strmatch(type,
"GAUSSIAN"))
3201 char* dtype=get_str_from_str_or_direct(len);
3202 int32_t size=get_int_from_int_or_str();
3205 width=get_real_from_real_or_str();
3207 if (strmatch(dtype,
"REAL"))
3208 kernel=ui_kernel->create_gaussian(size, width);
3209 else if (strmatch(dtype,
"SPARSEREAL"))
3210 kernel=ui_kernel->create_sparsegaussian(size, width);
3214 else if (strmatch(type,
"GAUSSIANSHIFT"))
3219 char* dtype=get_str_from_str_or_direct(len);
3220 if (strmatch(dtype,
"REAL"))
3222 int32_t size=get_int_from_int_or_str();
3223 float64_t width=get_real_from_real_or_str();
3224 int32_t max_shift=get_int_from_int_or_str();
3225 int32_t shift_step=get_int_from_int_or_str();
3227 kernel=ui_kernel->create_gaussianshift(
3228 size, width, max_shift, shift_step);
3233 else if (strmatch(type,
"CUSTOM"))
3235 if (m_nrhs!=4 || !create_return_values(0))
3241 get_matrix(kmatrix, num_feat, num_vec);
3244 char* ktype=get_string(tlen);
3246 if (!strmatch(ktype,
"DIAG") &&
3247 !strmatch(ktype,
"FULL") &&
3248 !strmatch(ktype,
"FULL2DIAG"))
3251 SG_ERROR(
"Undefined type, not DIAG, FULL or FULL2DIAG.\n")
3254 bool source_is_diag=
false;
3255 bool dest_is_diag=
false;
3257 if (strmatch(ktype,
"FULL2DIAG"))
3259 else if (strmatch(ktype,
"DIAG"))
3261 source_is_diag=
true;
3265 kernel=ui_kernel->create_custom(kmatrix, num_feat, num_vec,
3266 source_is_diag, dest_is_diag);
3268 else if (strmatch(type,
"CONST"))
3273 char* dtype=get_str_from_str_or_direct(len);
3274 if (strmatch(dtype,
"REAL"))
3276 int32_t size=get_int_from_int_or_str();
3279 c=get_real_from_real_or_str();
3281 kernel=ui_kernel->create_const(size, c);
3286 else if (strmatch(type,
"DIAG"))
3291 char* dtype=get_str_from_str_or_direct(len);
3292 if (strmatch(dtype,
"REAL"))
3294 int32_t size=get_int_from_int_or_str();
3297 diag=get_real_from_real_or_str();
3299 kernel=ui_kernel->create_diag(size, diag);
3305 else if (strmatch(type,
"TPPK"))
3310 char* dtype=get_str_from_str_or_direct(len);
3311 if (strmatch(dtype,
"INT"))
3313 int32_t size=get_int_from_int_or_str();
3317 get_matrix(km, rows, cols);
3318 kernel=ui_kernel->create_tppk(size, km, rows, cols);
3327 SG_DEBUG(
"created kernel: %p\n", kernel)
3340 int32_t from_order=0;
3341 bool normalize=
true;
3343 int32_t feature_class_len=0;
3344 char* feature_class_str=get_string(feature_class_len);
3345 ASSERT(feature_class_str)
3347 if (strmatch(feature_class_str,
"WD"))
3350 SG_ERROR(
"Please specify alphabet, WD, order, from_order\n")
3354 from_order=get_int();
3357 else if (strmatch(feature_class_str,
"WSPEC"))
3360 SG_ERROR(
"Please specify alphabet, order, WSPEC, start, normalize\n")
3365 normalize=get_bool();
3373 SG_FREE(feature_class_str);
3388 int32_t feature_class_len=0;
3390 char* feature_class_str=get_string(feature_class_len);
3391 ASSERT(feature_class_str)
3392 if (strmatch(feature_class_str,
"POLY"))
3398 normalize = get_bool();
3403 SG_ERROR(
"Unknown feature class: %s\n", feature_class_str)
3405 SG_FREE(feature_class_str);
3411 bool CSGInterface::cmd_init_kernel()
3417 bool CSGInterface::cmd_clean_kernel()
3419 if (m_nrhs<1 || !create_return_values(0))
3422 return ui_kernel->clean_kernel();
3425 bool CSGInterface::cmd_save_kernel()
3427 if (m_nrhs<2 || !create_return_values(0))
3431 char* filename=get_str_from_str_or_direct(len);
3433 bool success=ui_kernel->save_kernel(filename);
3439 bool CSGInterface::cmd_get_kernel_matrix()
3441 if (m_nrhs>2 || !create_return_values(1))
3448 target=get_string(len);
3449 bool success=ui_kernel->init_kernel(target);
3453 CKernel* kernel=ui_kernel->get_kernel();
3455 SG_ERROR(
"No kernel defined or not initialized.\n")
3466 bool CSGInterface::cmd_set_WD_position_weights()
3468 if (m_nrhs<2 || m_nrhs>3 || !create_return_values(0))
3471 CKernel* kernel=ui_kernel->get_kernel();
3490 get_matrix(weights, dim, len);
3497 if (dim!=1 && len>0)
3498 SG_ERROR(
"Dimension mismatch (should be 1 x seq_length or 0x0\n")
3500 ui_kernel->init_kernel(
"TRAIN");
3513 target=get_string(tlen);
3517 SG_ERROR(
"Couldn't find second argument to method.\n")
3520 if (!strmatch(target,
"TRAIN") && !strmatch(target,
"TEST"))
3523 SG_ERROR(
"Second argument none of TRAIN or TEST.\n")
3526 if (strmatch(target,
"TEST"))
3530 if (dim!=1 && len>0)
3533 SG_ERROR(
"Dimension mismatch (should be 1 x seq_length or 0x0\n")
3536 if (dim==0 && len==0)
3538 if (create_return_values(3))
3550 if (create_return_values(3))
3559 ui_kernel->init_kernel(
"TRAIN");
3571 bool CSGInterface::cmd_get_subkernel_weights()
3573 if (m_nrhs!=1 || !create_return_values(1))
3576 CKernel *kernel=ui_kernel->get_kernel();
3585 int32_t num_weights=-1;
3586 weights=((
CCombinedKernel *) kernel)->get_subkernel_weights(num_weights);
3589 set_matrix(weights, 1, num_weights);
3599 get_degree_weights(degree, length);
3604 get_degree_weights(degree, length);
3607 SG_ERROR(
"Setting subkernel weights not supported on this kernel.\n")
3612 set_matrix(weights, degree, length);
3616 bool CSGInterface::cmd_set_subkernel_weights()
3618 if (m_nrhs!=2 || !create_return_values(0))
3621 CKernel* kernel=ui_kernel->get_kernel();
3629 get_matrix(weights, dim, len);
3637 if (dim!=degree || len<1)
3638 SG_ERROR(
"WD: Dimension mismatch (should be (seq_length | 1) x degree) got (%d x %d)\n", len, degree)
3650 if (dim!=degree || len<1)
3651 SG_ERROR(
"WDPos: Dimension mismatch (should be (seq_length | 1) x degree) got (%d x %d)\n", len, degree)
3661 if (dim!=1 || len!=num_subkernels)
3662 SG_ERROR(
"All: Dimension mismatch (should be 1 x num_subkernels)\n")
3671 bool CSGInterface::cmd_set_subkernel_weights_combined()
3673 if (m_nrhs!=3 || !create_return_values(0))
3676 CKernel* kernel=ui_kernel->get_kernel();
3680 SG_ERROR(
"Only works for combined kernels.\n")
3686 get_matrix(weights, dim, len);
3688 int32_t idx=get_int();
3689 SG_DEBUG(
"using kernel_idx=%i\n", idx)
3693 SG_ERROR(
"No subkernel at idx %d.\n", idx)
3701 if (dim!=degree || len<1)
3702 SG_ERROR(
"Dimension mismatch (should be de(seq_length | 1) x degree)\n")
3714 if (dim!=degree || len<1)
3715 SG_ERROR(
"Dimension mismatch (should be de(seq_length | 1) x degree)\n")
3725 if (dim!=1 || len!=num_subkernels)
3726 SG_ERROR(
"Dimension mismatch (should be 1 x num_subkernels)\n")
3735 bool CSGInterface::cmd_get_dotfeature_weights_combined()
3737 if (m_nrhs!=2 || !create_return_values(1))
3741 char* target=get_string(tlen);
3744 if (strmatch(target,
"TRAIN"))
3745 features=ui_features->get_train_features();
3746 else if (strmatch(target,
"TEST"))
3747 features=ui_features->get_test_features();
3751 SG_ERROR(
"Unknown target, neither TRAIN nor TEST.\n")
3758 SG_ERROR(
"Only works for combined dot features.\n")
3766 bool CSGInterface::cmd_set_dotfeature_weights_combined()
3768 if (m_nrhs!=3 || !create_return_values(0))
3772 char* target=get_string(tlen);
3775 if (strmatch(target,
"TRAIN"))
3776 features=ui_features->get_train_features();
3777 else if (strmatch(target,
"TEST"))
3778 features=ui_features->get_test_features();
3782 SG_ERROR(
"Unknown target, neither TRAIN nor TEST.\n")
3789 SG_ERROR(
"Only works for combined dot features.\n")
3794 get_matrix(weights, dim, len);
3801 bool CSGInterface::cmd_set_last_subkernel_weights()
3803 if (m_nrhs!=2 || !create_return_values(0))
3806 CKernel* kernel=ui_kernel->get_kernel();
3810 SG_ERROR(
"Only works for Combined kernels.\n")
3820 get_matrix(weights, dim, len);
3827 SG_ERROR(
"Dimension mismatch (should be de(seq_length | 1) x degree)\n")
3839 SG_ERROR(
"Dimension mismatch (should be de(seq_length | 1) x degree)\n")
3849 if (dim!=1 || len!=num_subkernels)
3850 SG_ERROR(
"Dimension mismatch (should be 1 x num_subkernels)\n")
3859 bool CSGInterface::cmd_get_WD_position_weights()
3861 if (m_nrhs!=1 || !create_return_values(1))
3864 CKernel* kernel=ui_kernel->get_kernel();
3872 SG_ERROR(
"Couldn't find last kernel.\n")
3876 SG_ERROR(
"Wrong subkernel type.\n")
3887 if (position_weights==NULL)
3888 set_vector(position_weights, 0);
3890 set_vector(position_weights, len);
3895 bool CSGInterface::cmd_get_last_subkernel_weights()
3897 if (m_nrhs!=1 || !create_return_values(1))
3900 CKernel* kernel=ui_kernel->get_kernel();
3905 SG_ERROR(
"Only works for Combined kernels.\n")
3909 SG_ERROR(
"Couldn't find last kernel.\n")
3916 int32_t num_weights=0;
3920 set_vector(weights, num_weights);
3927 get_degree_weights(degree, len);
3930 get_degree_weights(degree, len);
3932 SG_ERROR(
"Only works for Weighted Degree (Position) kernels.\n")
3937 set_matrix(weights, degree, len);
3942 bool CSGInterface::cmd_compute_by_subkernels()
3944 if (m_nrhs!=1 || !create_return_values(1))
3947 CKernel* kernel=ui_kernel->get_kernel();
3964 SG_ERROR(
"Kernel optimization not initialized.\n")
3972 SG_ERROR(
"Kernel optimization not initialized.\n")
3975 SG_ERROR(
"Only works for Weighted Degree (Position) kernels.\n")
3980 int32_t num_feat=degree*len;
3981 int32_t num=num_feat*num_vec;
3984 for (int32_t i=0; i<num; i++)
3990 for (int32_t i=0; i<num_vec; i++)
3997 for (int32_t i=0; i<num_vec; i++)
4001 set_matrix(result, num_feat, num_vec);
4007 bool CSGInterface::cmd_init_kernel_optimization()
4009 if (m_nrhs<1 || !create_return_values(0))
4012 return ui_kernel->init_kernel_optimization();
4015 bool CSGInterface::cmd_get_kernel_optimization()
4017 if (m_nrhs<1 || !create_return_values(1))
4020 CKernel* kernel=ui_kernel->get_kernel();
4031 int32_t max_order=get_int();
4032 if ((max_order<1) || (max_order>12))
4034 SG_WARNING(
"max_order out of range 1..12 (%d). setting to 1\n", max_order)
4039 CSVM* svm=(
CSVM*) ui_classifier->get_classifier();
4044 int32_t* sv_idx=SG_MALLOC(int32_t, num_suppvec);
4049 for (int32_t i=0; i<num_suppvec; i++)
4056 num_sym, NULL, num_suppvec, sv_idx, sv_weight);
4060 set_matrix(position_weights, num_sym, num_feat);
4061 SG_FREE(position_weights);
4074 set_vector(weights, len);
4092 bool CSGInterface::cmd_delete_kernel_optimization()
4094 if (m_nrhs<1 || !create_return_values(0))
4097 return ui_kernel->delete_kernel_optimization();
4100 bool CSGInterface::cmd_use_diagonal_speedup()
4102 if (m_nrhs<2 || !create_return_values(0))
4105 bool speedup=get_bool();
4107 CKernel* kernel=ui_kernel->get_kernel();
4113 SG_DEBUG(
"Identified combined kernel.\n")
4116 SG_ERROR(
"No last kernel defined.\n")
4120 SG_ERROR(
"Currently only commwordstring kernel supports diagonal speedup\n")
4124 SG_INFO(
"Diagonal speedup %s.\n", speedup ?
"enabled" :
"disabled")
4129 bool CSGInterface::cmd_set_kernel_optimization_type()
4131 if (m_nrhs<2 || !create_return_values(0))
4135 char* opt_type=get_str_from_str_or_direct(len);
4137 bool success=ui_kernel->set_optimization_type(opt_type);
4143 bool CSGInterface::cmd_set_solver()
4145 if (m_nrhs<2 || !create_return_values(0))
4149 char* solver=get_str_from_str_or_direct(len);
4151 bool success=ui_classifier->set_solver(solver);
4157 bool CSGInterface::cmd_set_constraint_generator()
4159 if (m_nrhs<2 || !create_return_values(0))
4163 char* cg=get_str_from_str_or_direct(len);
4165 bool success=ui_classifier->set_constraint_generator(cg);
4171 bool CSGInterface::cmd_set_prior_probs()
4173 if (m_nrhs<3 || !create_return_values(0))
4179 SG_ERROR(
"SalzbergWordStringKernel required for setting prior probs!\n")
4181 float64_t pos_probs=get_real_from_real_or_str();
4182 float64_t neg_probs=get_real_from_real_or_str();
4189 bool CSGInterface::cmd_set_prior_probs_from_labels()
4191 if (m_nrhs<2 || !create_return_values(0))
4197 SG_ERROR(
"SalzbergWordStringKernel required for setting prior probs!\n")
4201 get_vector(lab, len);
4204 for (int32_t i=0; i<len; i++)
4207 SG_ERROR(
"Couldn't set label %d (of %d): %f.\n", i, len, lab[i])
4218 bool CSGInterface::cmd_resize_kernel_cache()
4220 if (m_nrhs<2 || !create_return_values(0))
4223 int32_t size=get_int_from_int_or_str();
4224 return ui_kernel->resize_kernel_cache(size);
4226 #endif //USE_SVMLIGHT
4231 bool CSGInterface::cmd_set_distance()
4233 if (m_nrhs<3 || !create_return_values(0))
4238 char* type=get_str_from_str_or_direct(len);
4239 char* dtype=get_str_from_str_or_direct(len);
4241 if (strmatch(type,
"MINKOWSKI") && m_nrhs==4)
4243 float64_t k=get_real_from_real_or_str();
4244 distance=ui_distance->create_minkowski(k);
4246 else if (strmatch(type,
"MANHATTAN"))
4248 if (strmatch(dtype,
"REAL"))
4249 distance=ui_distance->create_generic(
D_MANHATTAN);
4250 else if (strmatch(dtype,
"WORD"))
4253 else if (strmatch(type,
"HAMMING") && strmatch(dtype,
"WORD"))
4255 bool use_sign=
false;
4257 use_sign=get_bool_from_bool_or_str();
4259 distance=ui_distance->create_hammingword(use_sign);
4261 else if (strmatch(type,
"CANBERRA"))
4263 if (strmatch(dtype,
"REAL"))
4264 distance=ui_distance->create_generic(
D_CANBERRA);
4265 else if (strmatch(dtype,
"WORD"))
4268 else if (strmatch(type,
"CHEBYSHEW") && strmatch(dtype,
"REAL"))
4270 distance=ui_distance->create_generic(
D_CHEBYSHEW);
4272 else if (strmatch(type,
"GEODESIC") && strmatch(dtype,
"REAL"))
4274 distance=ui_distance->create_generic(
D_GEODESIC);
4276 else if (strmatch(type,
"JENSEN") && strmatch(dtype,
"REAL"))
4278 distance=ui_distance->create_generic(
D_JENSEN);
4280 else if (strmatch(type,
"CHISQUARE") && strmatch(dtype,
"REAL"))
4282 distance=ui_distance->create_generic(
D_CHISQUARE);
4284 else if (strmatch(type,
"TANIMOTO") && strmatch(dtype,
"REAL"))
4286 distance=ui_distance->create_generic(
D_TANIMOTO);
4288 else if (strmatch(type,
"COSINE") && strmatch(dtype,
"REAL"))
4290 distance=ui_distance->create_generic(
D_COSINE);
4292 else if (strmatch(type,
"BRAYCURTIS") && strmatch(dtype,
"REAL"))
4296 else if (strmatch(type,
"EUCLIDEAN"))
4298 if (strmatch(dtype,
"REAL"))
4299 distance=ui_distance->create_generic(
D_EUCLIDEAN);
4300 else if (strmatch(dtype,
"SPARSEREAL"))
4308 return ui_distance->set_distance(distance);
4311 bool CSGInterface::cmd_init_distance()
4317 bool CSGInterface::cmd_get_distance_matrix()
4319 if (m_nrhs!=2 || !create_return_values(1))
4323 char* target=get_string(len);
4325 bool success=ui_distance->init_distance(target);
4331 SG_ERROR(
"No distance defined or not initialized.\n")
4343 bool CSGInterface::cmd_get_SPEC_consensus()
4345 if (m_nrhs!=1 || !create_return_values(1))
4348 CKernel* kernel=ui_kernel->get_kernel();
4352 SG_ERROR(
"Only works for CommWordString kernels.\n")
4354 CSVM* svm=(
CSVM*) ui_classifier->get_classifier();
4357 int32_t* sv_idx=SG_MALLOC(int32_t, num_suppvec);
4361 for (int32_t i=0; i<num_suppvec; i++)
4368 num_feat, num_suppvec, sv_idx, sv_weight);
4372 set_vector(consensus, num_feat);
4378 bool CSGInterface::cmd_get_SPEC_scoring()
4380 if (m_nrhs!=2 || !create_return_values(1))
4383 int32_t max_order=get_int();
4384 CKernel* kernel=ui_kernel->get_kernel();
4390 SG_ERROR(
"Only works for (Weighted) CommWordString kernels.\n")
4392 CSVM* svm=(
CSVM*) ui_classifier->get_classifier();
4395 int32_t* sv_idx=SG_MALLOC(int32_t, num_suppvec);
4400 for (int32_t i=0; i<num_suppvec; i++)
4406 if ((max_order<1) || (max_order>8))
4408 SG_WARNING(
"max_order out of range 1..8 (%d). setting to 1\n", max_order)
4415 max_order, num_feat, num_sym, NULL,
4416 num_suppvec, sv_idx, sv_weight);
4419 max_order, num_feat, num_sym, NULL,
4420 num_suppvec, sv_idx, sv_weight);
4424 set_matrix(position_weights, num_sym, num_feat);
4425 SG_FREE(position_weights);
4430 bool CSGInterface::cmd_get_WD_consensus()
4432 if (m_nrhs!=1 || !create_return_values(1))
4435 CKernel* kernel=ui_kernel->get_kernel();
4439 SG_ERROR(
"Only works for Weighted Degree Position kernels.\n")
4441 CSVM* svm=(
CSVM*) ui_classifier->get_classifier();
4444 int32_t* sv_idx=SG_MALLOC(int32_t, num_suppvec);
4448 for (int32_t i=0; i<num_suppvec; i++)
4455 num_feat, num_suppvec, sv_idx, sv_weight);
4459 set_vector(consensus, num_feat);
4465 bool CSGInterface::cmd_compute_POIM_WD()
4467 if (m_nrhs!=3 || !create_return_values(1))
4470 int32_t max_order=get_int();
4472 int32_t num_dfeat=0;
4474 get_matrix(distribution, num_dfeat, num_dvec);
4479 CKernel* kernel=ui_kernel->get_kernel();
4483 SG_ERROR(
"Only works for Weighted Degree Position kernels.\n")
4493 if (num_dvec!=seqlen || num_dfeat!=num_sym)
4495 SG_ERROR(
"distribution should have (seqlen x num_sym) elements"
4496 "(seqlen: %d vs. %d symbols: %d vs. %d)\n", seqlen,
4497 num_dvec, num_sym, num_dfeat);
4500 CSVM* svm=(
CSVM*) ui_classifier->get_classifier();
4503 int32_t* sv_idx=SG_MALLOC(int32_t, num_suppvec);
4506 for (int32_t i=0; i<num_suppvec; i++)
4522 max_order, seqlen, num_sym, NULL,
4523 num_suppvec, sv_idx, sv_weight, distribution);
4527 set_matrix(position_weights, num_sym, seqlen);
4528 SG_FREE(position_weights);
4533 bool CSGInterface::cmd_get_WD_scoring()
4535 if (m_nrhs!=2 || !create_return_values(1))
4538 int32_t max_order=get_int();
4540 CKernel* kernel=ui_kernel->get_kernel();
4544 SG_ERROR(
"Only works for Weighted Degree Position kernels.\n")
4546 CSVM* svm=(
CSVM*) ui_classifier->get_classifier();
4549 int32_t* sv_idx=SG_MALLOC(int32_t, num_suppvec);
4554 for (int32_t i=0; i<num_suppvec; i++)
4560 if ((max_order<1) || (max_order>12))
4562 SG_WARNING(
"max_order out of range 1..12 (%d). setting to 1\n", max_order)
4568 max_order, num_feat, num_sym, NULL, num_suppvec, sv_idx, sv_weight);
4572 set_matrix(position_weights, num_sym, num_feat);
4573 SG_FREE(position_weights);
4581 bool CSGInterface::cmd_classify()
4583 if (m_nrhs!=1 || !create_return_values(1))
4586 if (!ui_kernel->get_kernel() ||
4587 ui_kernel->get_kernel()->get_kernel_type()!=
K_CUSTOM)
4589 CFeatures* feat=ui_features->get_test_features();
4594 CLabels* labels=ui_classifier->classify();
4600 for (int32_t i=0; i<num_vec; i++)
4622 set_vector(result, num_vec);
4628 bool CSGInterface::cmd_classify_example()
4630 if (m_nrhs!=2 || !create_return_values(1))
4633 int32_t idx=get_int();
4636 if (!ui_classifier->classify_example(idx, result))
4637 SG_ERROR(
"Classify_example failed.\n")
4644 bool CSGInterface::cmd_get_classifier()
4646 if (m_nrhs<1 || m_nrhs>2 || !create_return_values(2))
4660 if (!ui_classifier->get_trained_classifier(
4661 weights, rows, cols, bias, brows, bcols, idx))
4666 set_matrix(bias, brows, bcols);
4671 set_matrix(weights, rows, cols);
4677 bool CSGInterface::cmd_new_classifier()
4679 if (m_nrhs<2 || !create_return_values(0))
4683 char* name=get_str_from_str_or_direct(len);
4689 d=get_int_from_int_or_str();
4692 from_d=get_int_from_int_or_str();
4695 bool success=ui_classifier->new_classifier(name, d, from_d);
4701 bool CSGInterface::cmd_save_classifier()
4703 if (m_nrhs<2 || !create_return_values(0))
4707 char* filename=get_str_from_str_or_direct(len);
4709 bool success=ui_classifier->save(filename);
4715 bool CSGInterface::cmd_load_classifier()
4717 if (m_nrhs<3 || !create_return_values(0))
4721 char* filename=get_str_from_str_or_direct(len);
4722 char* type=get_str_from_str_or_direct(len);
4724 bool success=ui_classifier->load(filename, type);
4726 if (dynamic_cast<CKernelMachine*>(ui_classifier->get_classifier()))
4739 bool CSGInterface::cmd_get_num_svms()
4741 if (m_nrhs!=1 || !create_return_values(1))
4744 set_int(ui_classifier->get_num_svms());
4750 bool CSGInterface::cmd_get_svm()
4752 return cmd_get_classifier();
4755 bool CSGInterface::cmd_set_svm()
4757 if (m_nrhs!=3 || !create_return_values(0))
4763 int32_t num_feat_alphas=0;
4764 int32_t num_vec_alphas=0;
4765 get_matrix(alphas, num_feat_alphas, num_vec_alphas);
4768 SG_ERROR(
"No proper alphas given.\n")
4769 if (num_vec_alphas!=2)
4770 SG_ERROR(
"Not 2 vectors in alphas.\n")
4772 CSVM* svm=(
CSVM*) ui_classifier->get_classifier();
4774 SG_ERROR(
"No SVM object available.\n")
4780 for (int32_t i=0; i<num_support_vectors; i++)
4790 bool CSGInterface::cmd_set_linear_classifier()
4792 if (m_nrhs!=3 || !create_return_values(0))
4802 SG_ERROR(
"No proper weight vector given.\n")
4806 SG_ERROR(
"No Linear Classifier object available.\n")
4813 bool CSGInterface::cmd_get_svm_objective()
4815 if (m_nrhs!=1 || !create_return_values(1))
4818 CSVM* svm=(
CSVM*) ui_classifier->get_classifier();
4827 bool CSGInterface::cmd_compute_svm_primal_objective()
4829 return do_compute_objective(SVM_PRIMAL);
4832 bool CSGInterface::cmd_compute_svm_dual_objective()
4834 return do_compute_objective(SVM_DUAL);
4837 bool CSGInterface::cmd_compute_mkl_dual_objective()
4839 return do_compute_objective(MKL_DUAL);
4842 bool CSGInterface::cmd_compute_relative_mkl_duality_gap()
4844 return do_compute_objective(MKL_RELATIVE_DUALITY_GAP);
4847 bool CSGInterface::cmd_compute_absolute_mkl_duality_gap()
4849 return do_compute_objective(MKL_ABSOLUTE_DUALITY_GAP);
4852 bool CSGInterface::do_compute_objective(E_WHICH_OBJ obj)
4854 if (m_nrhs!=1 || !create_return_values(1))
4859 CSVM* svm=(
CSVM*) ui_classifier->get_classifier();
4864 trainlabels=ui_labels->get_train_labels();
4867 SG_ERROR(
"No trainlabels available.\n")
4869 CKernel* kernel=ui_kernel->get_kernel();
4873 if (!ui_kernel->is_initialized() || !kernel->
has_features())
4874 SG_ERROR(
"Kernel not initialized.\n")
4890 result=((
CMKL*) svm)->compute_mkl_primal_objective();
4894 result=((
CMKL*) svm)->compute_mkl_dual_objective();
4896 case MKL_RELATIVE_DUALITY_GAP:
4899 float64_t primal=((
CMKL*) svm)->compute_mkl_dual_objective();
4900 float64_t dual=((
CMKL*) svm)->compute_mkl_primal_objective();
4901 result=(primal-dual)/dual;
4904 case MKL_ABSOLUTE_DUALITY_GAP:
4907 float64_t primal=((
CMKL*) svm)->compute_mkl_dual_objective();
4908 float64_t dual=((
CMKL*) svm)->compute_mkl_primal_objective();
4913 SG_SERROR(
"Error calling do_compute_objective\n")
4921 bool CSGInterface::cmd_train_classifier()
4923 if (m_nrhs<1 || !create_return_values(0))
4926 CMachine* classifier=ui_classifier->get_classifier();
4928 SG_ERROR(
"No classifier available.\n")
4948 return ui_classifier->train_svm();
4950 return ui_classifier->train_mkl_multiclass();
4954 return ui_classifier->train_mkl();
4957 return ui_classifier->train_krr();
4964 int32_t k=get_int_from_int_or_str();
4966 return ui_classifier->train_knn(k);
4974 int32_t k=get_int_from_int_or_str();
4975 int32_t max_iter=get_int_from_int_or_str();
4977 return ui_classifier->train_clustering(k, max_iter);
4985 int32_t merges=get_int_from_int_or_str();
4987 return ui_classifier->train_clustering(merges);
4994 gamma=get_real_from_real_or_str();
4996 return ui_classifier->train_linear(gamma);
5007 return ui_classifier->train_linear();
5009 #ifdef USE_GPL_SHOGUN
5011 return ui_classifier->train_wdocas();
5015 SG_ERROR(
"Unknown classifier type %d.\n", type)
5021 bool CSGInterface::cmd_do_auc_maximization()
5023 if (m_nrhs!=2 || !create_return_values(0))
5026 bool do_auc=get_bool_from_bool_or_str();
5028 return ui_classifier->set_do_auc_maximization(do_auc);
5031 bool CSGInterface::cmd_set_perceptron_parameters()
5033 if (m_nrhs!=3 || !create_return_values(0))
5036 float64_t lernrate=get_real_from_real_or_str();
5037 int32_t maxiter=get_int_from_int_or_str();
5039 return ui_classifier->set_perceptron_parameters(lernrate, maxiter);
5042 bool CSGInterface::cmd_set_svm_qpsize()
5044 if (m_nrhs!=2 || !create_return_values(0))
5047 int32_t qpsize=get_int_from_int_or_str();
5049 return ui_classifier->set_svm_qpsize(qpsize);
5052 bool CSGInterface::cmd_set_svm_max_qpsize()
5054 if (m_nrhs!=2 || !create_return_values(0))
5057 int32_t max_qpsize=get_int_from_int_or_str();
5059 return ui_classifier->set_svm_max_qpsize(max_qpsize);
5062 bool CSGInterface::cmd_set_svm_bufsize()
5064 if (m_nrhs!=2 || !create_return_values(0))
5067 int32_t bufsize=get_int_from_int_or_str();
5069 return ui_classifier->set_svm_bufsize(bufsize);
5072 bool CSGInterface::cmd_set_svm_C()
5074 if (m_nrhs<2 || !create_return_values(0))
5077 float64_t C1=get_real_from_real_or_str();
5081 C2=get_real_from_real_or_str();
5083 return ui_classifier->set_svm_C(C1, C2);
5086 bool CSGInterface::cmd_set_svm_epsilon()
5088 if (m_nrhs!=2 || !create_return_values(0))
5091 float64_t epsilon=get_real_from_real_or_str();
5093 return ui_classifier->set_svm_epsilon(epsilon);
5096 bool CSGInterface::cmd_set_svr_tube_epsilon()
5098 if (m_nrhs!=2 || !create_return_values(0))
5101 float64_t tube_epsilon=get_real_from_real_or_str();
5103 return ui_classifier->set_svr_tube_epsilon(tube_epsilon);
5106 bool CSGInterface::cmd_set_svm_nu()
5108 if (m_nrhs!=2 || !create_return_values(0))
5111 float64_t nu=get_real_from_real_or_str();
5113 return ui_classifier->set_svm_nu(nu);
5116 bool CSGInterface::cmd_set_svm_mkl_parameters()
5118 if (m_nrhs<3 || m_nrhs>4 || !create_return_values(0))
5121 float64_t weight_epsilon=get_real_from_real_or_str();
5122 float64_t C_mkl=get_real_from_real_or_str();
5126 mkl_norm=get_real_from_real_or_str();
5128 return ui_classifier->set_svm_mkl_parameters(weight_epsilon, C_mkl, mkl_norm);
5131 bool CSGInterface::cmd_set_elasticnet_lambda()
5133 if (m_nrhs!=2 || !create_return_values(0))
5135 float64_t lambda=get_real_from_real_or_str();
5136 return ui_classifier->set_elasticnet_lambda(lambda);
5139 bool CSGInterface::cmd_set_mkl_block_norm()
5141 if (m_nrhs!=2 || !create_return_values(0))
5143 float64_t bnorm=get_real_from_real_or_str();
5144 return ui_classifier->set_mkl_block_norm(bnorm);
5148 bool CSGInterface::cmd_set_max_train_time()
5150 if (m_nrhs!=2 || !create_return_values(0))
5153 float64_t max_train_time=get_real_from_real_or_str();
5155 return ui_classifier->set_max_train_time(max_train_time);
5158 bool CSGInterface::cmd_set_svm_shrinking_enabled()
5160 if (m_nrhs!=2 || !create_return_values(0))
5163 bool shrinking_enabled=get_bool_from_bool_or_str();
5165 return ui_classifier->set_svm_shrinking_enabled(shrinking_enabled);
5168 bool CSGInterface::cmd_set_svm_batch_computation_enabled()
5170 if (m_nrhs!=2 || !create_return_values(0))
5173 bool batch_computation_enabled=get_bool_from_bool_or_str();
5175 return ui_classifier->set_svm_batch_computation_enabled(
5176 batch_computation_enabled);
5179 bool CSGInterface::cmd_set_svm_linadd_enabled()
5181 if (m_nrhs!=2 || !create_return_values(0))
5184 bool linadd_enabled=get_bool_from_bool_or_str();
5186 return ui_classifier->set_svm_linadd_enabled(linadd_enabled);
5189 bool CSGInterface::cmd_set_svm_bias_enabled()
5191 if (m_nrhs!=2 || !create_return_values(0))
5194 bool bias_enabled=get_bool_from_bool_or_str();
5196 return ui_classifier->set_svm_bias_enabled(bias_enabled);
5199 bool CSGInterface::cmd_set_mkl_interleaved_enabled()
5201 if (m_nrhs!=2 || !create_return_values(0))
5204 bool interleaved_enabled=get_bool_from_bool_or_str();
5206 return ui_classifier->set_mkl_interleaved_enabled(interleaved_enabled);
5209 bool CSGInterface::cmd_set_krr_tau()
5211 if (m_nrhs!=2 || !create_return_values(0))
5214 float64_t tau=get_real_from_real_or_str();
5216 return ui_classifier->set_krr_tau(tau);
5222 bool CSGInterface::cmd_add_preproc()
5224 if (m_nrhs<2 || !create_return_values(0))
5228 char* type=get_str_from_str_or_direct(len);
5231 if (strmatch(type,
"NORMONE"))
5232 preproc=ui_preproc->create_generic(
P_NORMONE);
5233 else if (strmatch(type,
"LOGPLUSONE"))
5235 else if (strmatch(type,
"SORTWORDSTRING"))
5237 else if (strmatch(type,
"SORTULONGSTRING"))
5239 else if (strmatch(type,
"DECOMPRESSCHARSTRING"))
5241 else if (strmatch(type,
"SORTWORD"))
5242 preproc=ui_preproc->create_generic(
P_SORTWORD);
5244 else if (strmatch(type,
"PRUNEVARSUBMEAN"))
5246 bool divide_by_std=
false;
5248 divide_by_std=get_bool_from_bool_or_str();
5250 preproc=ui_preproc->create_prunevarsubmean(divide_by_std);
5254 else if (strmatch(type,
"PCA") && m_nrhs==4)
5256 bool do_whitening=get_bool_from_bool_or_str();
5257 float64_t threshold=get_real_from_real_or_str();
5259 preproc=ui_preproc->create_pca(do_whitening, threshold);
5267 return ui_preproc->add_preproc(preproc);
5270 bool CSGInterface::cmd_del_preproc()
5272 if (m_nrhs!=1 || !create_return_values(0))
5275 return ui_preproc->del_preproc();
5278 bool CSGInterface::cmd_attach_preproc()
5280 if (m_nrhs<2 || !create_return_values(0))
5284 char* target=get_str_from_str_or_direct(len);
5286 bool do_force=
false;
5288 do_force=get_bool_from_bool_or_str();
5290 bool success=ui_preproc->attach_preproc(target, do_force);
5296 bool CSGInterface::cmd_clean_preproc()
5298 if (m_nrhs!=1 || !create_return_values(0))
5301 return ui_preproc->clean_preproc();
5306 bool CSGInterface::cmd_set_converter()
5309 char* type=get_str_from_str_or_direct(len);
5311 if (strmatch(type,
"lle"))
5313 int32_t k = get_int_from_int_or_str();
5314 ui_converter->create_locallylinearembedding(k);
5317 if (strmatch(type,
"npe"))
5319 int32_t k = get_int_from_int_or_str();
5320 ui_converter->create_neighborhoodpreservingembedding(k);
5323 if (strmatch(type,
"ltsa"))
5325 int32_t k = get_int_from_int_or_str();
5326 ui_converter->create_localtangentspacealignment(k);
5329 if (strmatch(type,
"lltsa"))
5331 int32_t k = get_int_from_int_or_str();
5332 ui_converter->create_linearlocaltangentspacealignment(k);
5335 if (strmatch(type,
"hlle"))
5337 int32_t k = get_int_from_int_or_str();
5338 ui_converter->create_hessianlocallylinearembedding(k);
5341 if (strmatch(type,
"laplacian_eigenmaps"))
5343 int32_t k = get_int_from_int_or_str();
5344 int32_t width = get_real_from_real_or_str();
5345 ui_converter->create_laplacianeigenmaps(k,width);
5348 if (strmatch(type,
"lpp"))
5350 int32_t k = get_int_from_int_or_str();
5351 int32_t width = get_real_from_real_or_str();
5352 ui_converter->create_localitypreservingprojections(k,width);
5355 if (strmatch(type,
"diffusion_maps"))
5357 int32_t t = get_int_from_int_or_str();
5358 int32_t width = get_real_from_real_or_str();
5359 ui_converter->create_diffusionmaps(t,width);
5362 if (strmatch(type,
"isomap"))
5364 int32_t k = get_int_from_int_or_str();
5365 ui_converter->create_isomap(k);
5368 if (strmatch(type,
"mds"))
5370 ui_converter->create_multidimensionalscaling();
5373 if (strmatch(type,
"jade"))
5375 ui_converter->create_jade();
5381 bool CSGInterface::cmd_apply_converter()
5383 if (m_nrhs!=1 || !create_return_values(1))
5392 bool CSGInterface::cmd_embed()
5394 int32_t target_dim = get_int_from_int_or_str();
5396 if (m_nrhs!=1 || !create_return_values(1))
5407 bool CSGInterface::cmd_new_plugin_estimator()
5409 if (m_nrhs<2 || !create_return_values(0))
5412 float64_t pos_pseudo=get_real_from_real_or_str();
5413 float64_t neg_pseudo=get_real_from_real_or_str();
5415 return ui_pluginestimate->new_estimator(pos_pseudo, neg_pseudo);
5418 bool CSGInterface::cmd_train_estimator()
5420 if (m_nrhs!=1 || !create_return_values(0))
5423 return ui_pluginestimate->train();
5426 bool CSGInterface::cmd_plugin_estimate_classify_example()
5428 if (m_nrhs!=2 || !create_return_values(1))
5431 int32_t idx=get_int();
5432 float64_t result=ui_pluginestimate->apply_one(idx);
5434 set_vector(&result, 1);
5438 bool CSGInterface::cmd_plugin_estimate_classify()
5440 if (m_nrhs!=1 || !create_return_values(1))
5443 CFeatures* feat=ui_features->get_test_features();
5449 CLabels* labels=ui_pluginestimate->apply();
5450 for (int32_t i=0; i<num_vec; i++)
5454 set_vector(result, num_vec);
5460 bool CSGInterface::cmd_set_plugin_estimate()
5462 if (m_nrhs!=3 || !create_return_values(0))
5466 int32_t num_probs=0;
5468 get_matrix(emission_probs, num_probs, num_vec);
5471 SG_ERROR(
"Need at least 1 set of positive and 1 set of negative params.\n")
5474 float64_t* neg_params=&(emission_probs[num_probs]);
5478 get_vector(model_sizes, len);
5480 int32_t seq_length=(int32_t) model_sizes[0];
5481 int32_t num_symbols=(int32_t) model_sizes[1];
5482 if (num_probs!=seq_length*num_symbols)
5483 SG_ERROR(
"Mismatch in number of emission probs and sequence length * number of symbols.\n")
5485 ui_pluginestimate->get_estimator()->set_model_params(
5486 pos_params, neg_params, seq_length, num_symbols);
5491 bool CSGInterface::cmd_get_plugin_estimate()
5493 if (m_nrhs!=1 || !create_return_values(2))
5498 int32_t num_params=0;
5499 int32_t seq_length=0;
5500 int32_t num_symbols=0;
5502 if (!ui_pluginestimate->get_estimator()->get_model_params(
5503 pos_params, neg_params, seq_length, num_symbols))
5506 num_params=seq_length*num_symbols;
5509 for (int32_t i=0; i<num_params; i++)
5510 result[i]=pos_params[i];
5511 for (int32_t i=0; i<num_params; i++)
5512 result[i+num_params]=neg_params[i];
5514 set_matrix(result, num_params, 2);
5520 set_vector(model_sizes, 2);
5525 bool CSGInterface::cmd_convergence_criteria()
5527 if (m_nrhs<3 || !create_return_values(0))
5530 int32_t num_iterations=get_int_from_int_or_str();
5531 float64_t epsilon=get_real_from_real_or_str();
5533 return ui_hmm->convergence_criteria(num_iterations, epsilon);
5536 bool CSGInterface::cmd_normalize()
5538 if (m_nrhs<2 || !create_return_values(0))
5541 bool keep_dead_states=get_bool_from_bool_or_str();
5543 return ui_hmm->normalize(keep_dead_states);
5546 bool CSGInterface::cmd_add_states()
5548 if (m_nrhs<3 || !create_return_values(0))
5551 int32_t num_states=get_int_from_int_or_str();
5552 float64_t value=get_real_from_real_or_str();
5554 return ui_hmm->add_states(num_states, value);
5557 bool CSGInterface::cmd_permutation_entropy()
5559 if (m_nrhs<3 || !create_return_values(0))
5562 int32_t width=get_int_from_int_or_str();
5563 int32_t seq_num=get_int_from_int_or_str();
5565 return ui_hmm->permutation_entropy(width, seq_num);
5568 bool CSGInterface::cmd_relative_entropy()
5570 if (m_nrhs!=1 || !create_return_values(1))
5575 bool success=ui_hmm->relative_entropy(entropy, len);
5579 set_vector(entropy, len);
5585 bool CSGInterface::cmd_entropy()
5587 if (m_nrhs!=1 || !create_return_values(1))
5592 bool success=ui_hmm->entropy(entropy, len);
5596 set_vector(entropy, len);
5602 bool CSGInterface::cmd_hmm_classify()
5604 return do_hmm_classify(
false,
false);
5607 bool CSGInterface::cmd_one_class_hmm_classify()
5609 return do_hmm_classify(
false,
true);
5612 bool CSGInterface::cmd_one_class_linear_hmm_classify()
5614 return do_hmm_classify(
true,
true);
5617 bool CSGInterface::do_hmm_classify(
bool linear,
bool one_class)
5619 if (m_nrhs>1 || !create_return_values(1))
5622 CFeatures* feat=ui_features->get_test_features();
5631 labels=ui_hmm->linear_one_class_classify();
5636 labels=ui_hmm->one_class_classify();
5638 labels=ui_hmm->classify();
5644 for (int32_t i=0; i<num_vec; i++)
5648 set_vector(result, num_vec);
5654 bool CSGInterface::cmd_one_class_hmm_classify_example()
5656 return do_hmm_classify_example(
true);
5659 bool CSGInterface::cmd_hmm_classify_example()
5661 return do_hmm_classify_example(
false);
5664 bool CSGInterface::do_hmm_classify_example(
bool one_class)
5666 if (m_nrhs!=2 || !create_return_values(1))
5669 int32_t idx=get_int();
5673 result=ui_hmm->one_class_classify_example(idx);
5675 result=ui_hmm->classify_example(idx);
5682 bool CSGInterface::cmd_output_hmm()
5684 if (m_nrhs!=1 || !create_return_values(0))
5687 return ui_hmm->output_hmm();
5690 bool CSGInterface::cmd_output_hmm_defined()
5692 if (m_nrhs!=1 || !create_return_values(0))
5695 return ui_hmm->output_hmm_defined();
5698 bool CSGInterface::cmd_hmm_likelihood()
5700 if (m_nrhs!=1 || !create_return_values(1))
5703 CHMM* h=ui_hmm->get_current();
5708 set_real(likelihood);
5713 bool CSGInterface::cmd_likelihood()
5715 if (m_nrhs!=1 || !create_return_values(0))
5718 return ui_hmm->likelihood();
5721 bool CSGInterface::cmd_save_likelihood()
5723 if (m_nrhs<2 || !create_return_values(0))
5727 char* filename=get_str_from_str_or_direct(len);
5729 bool is_binary=
false;
5731 is_binary=get_bool_from_bool_or_str();
5733 bool success=ui_hmm->save_likelihood(filename, is_binary);
5739 bool CSGInterface::cmd_get_viterbi_path()
5741 if (m_nrhs!=2 || !create_return_values(2))
5744 int32_t dim=get_int();
5747 CHMM* h=ui_hmm->get_current();
5751 CFeatures* feat=ui_features->get_test_features();
5761 if (!vec || num_feat<=0)
5768 SG_DEBUG(
"computing viterbi path for vector %d (length %d)\n", dim, num_feat)
5772 set_vector(path, num_feat);
5774 set_real(likelihood);
5779 bool CSGInterface::cmd_viterbi_train()
5781 if (m_nrhs!=1 || !create_return_values(0))
5784 return ui_hmm->viterbi_train();
5787 bool CSGInterface::cmd_viterbi_train_defined()
5789 if (m_nrhs!=1 || !create_return_values(0))
5792 return ui_hmm->viterbi_train_defined();
5795 bool CSGInterface::cmd_baum_welch_train()
5797 if (m_nrhs!=1 || !create_return_values(0))
5800 return ui_hmm->baum_welch_train();
5803 bool CSGInterface::cmd_baum_welch_train_defined()
5805 if (m_nrhs!=1 || !create_return_values(0))
5808 return ui_hmm->baum_welch_train_defined();
5812 bool CSGInterface::cmd_baum_welch_trans_train()
5814 if (m_nrhs!=1 || !create_return_values(0))
5817 return ui_hmm->baum_welch_trans_train();
5820 bool CSGInterface::cmd_linear_train()
5822 if (m_nrhs<1 || !create_return_values(0))
5828 char* align=get_str_from_str_or_direct(len);
5830 bool success=ui_hmm->linear_train(align[0]);
5836 return ui_hmm->linear_train();
5839 bool CSGInterface::cmd_save_path()
5841 if (m_nrhs<2 || !create_return_values(0))
5845 char* filename=get_str_from_str_or_direct(len);
5847 bool is_binary=
false;
5849 is_binary=get_bool_from_bool_or_str();
5851 bool success=ui_hmm->save_path(filename, is_binary);
5857 bool CSGInterface::cmd_append_hmm()
5859 if (m_nrhs!=5 || !create_return_values(0))
5862 CHMM* old_h=ui_hmm->get_current();
5877 get_matrix(a, M_a, N_a);
5883 get_matrix(b, M_b, N_b);
5886 if (N_p!=N || N_q!=N || N_a!=N || M_a!=N || N_b!=M || M_b!=N)
5888 SG_ERROR(
"Model matrices not matching in size.\n"
5889 "p:(%d) q:(%d) a:(%d,%d) b(%d,%d)\n",
5890 N_p, N_q, N_a, M_a, N_b, M_b);
5893 CHMM* h=
new CHMM(N, M, NULL, ui_hmm->get_pseudo());
5904 h->
set_a(i,j, a[i+j*N]);
5908 h->
set_b(i,j, b[i+j*N]);
5916 bool CSGInterface::cmd_append_model()
5918 if (m_nrhs<2 || !create_return_values(0))
5920 if (m_nrhs>2 && m_nrhs!=4)
5924 char* filename=get_str_from_str_or_direct(len);
5929 base1=get_int_from_int_or_str();
5930 base2=get_int_from_int_or_str();
5933 bool success=ui_hmm->append_model(filename, base1, base2);
5939 bool CSGInterface::cmd_new_hmm()
5941 if (m_nrhs!=3 || !create_return_values(0))
5944 int32_t n=get_int_from_int_or_str();
5945 int32_t m=get_int_from_int_or_str();
5947 return ui_hmm->new_hmm(n, m);
5950 bool CSGInterface::cmd_load_hmm()
5952 if (m_nrhs!=2 || !create_return_values(0))
5956 char* filename=get_str_from_str_or_direct(len);
5958 bool success=ui_hmm->load(filename);
5964 bool CSGInterface::cmd_save_hmm()
5966 if (m_nrhs<2 || !create_return_values(0))
5970 char* filename=get_str_from_str_or_direct(len);
5972 bool is_binary=
false;
5974 is_binary=get_bool_from_bool_or_str();
5976 bool success=ui_hmm->save(filename, is_binary);
5982 bool CSGInterface::cmd_set_hmm()
5984 if (m_nrhs!=5 || !create_return_values(0))
5998 get_matrix(a, M_a, N_a);
6004 get_matrix(b, M_b, N_b);
6007 if (N_p!=N || N_q!=N || N_a!=N || M_a!=N || N_b!=M || M_b!=N)
6009 SG_ERROR(
"Model matrices not matching in size.\n"
6010 "p:(%d) q:(%d) a:(%d,%d) b(%d,%d)\n",
6011 N_p, N_q, N_a, M_a, N_b, M_b);
6014 CHMM* current=ui_hmm->get_current();
6016 SG_ERROR(
"Need a previously created HMM.\n")
6022 current->
set_p(i, p[i]);
6023 current->
set_q(i, q[i]);
6028 current->
set_a(i,j, a[i+j*N]);
6032 current->
set_b(i,j, b[i+j*N]);
6040 bool CSGInterface::cmd_set_hmm_as()
6042 if (m_nrhs!=2 || !create_return_values(0))
6046 char* target=get_str_from_str_or_direct(len);
6048 bool success=ui_hmm->set_hmm_as(target);
6054 bool CSGInterface::cmd_set_chop()
6056 if (m_nrhs!=2 || !create_return_values(0))
6059 float64_t value=get_real_from_real_or_str();
6060 return ui_hmm->chop(value);
6063 bool CSGInterface::cmd_set_pseudo()
6065 if (m_nrhs!=2 || !create_return_values(0))
6068 float64_t value=get_real_from_real_or_str();
6069 return ui_hmm->set_pseudo(value);
6072 bool CSGInterface::cmd_load_definitions()
6074 if (m_nrhs<2 || !create_return_values(0))
6078 char* filename=get_str_from_str_or_direct(len);
6082 do_init=get_bool_from_bool_or_str();
6084 bool success=ui_hmm->load_definitions(filename, do_init);
6090 bool CSGInterface::cmd_get_hmm()
6092 if (m_nrhs!=1 || !create_return_values(4))
6095 CHMM* h=ui_hmm->get_current();
6099 int32_t N=h->
get_N();
6100 int32_t M=h->
get_M();
6120 a[i+j*N]=h->
get_a(i, j);
6121 set_matrix(a, N, N);
6127 b[i+j*N]=h->
get_b(i, j);
6128 set_matrix(b, N, M);
6134 bool CSGInterface::cmd_best_path()
6136 if (m_nrhs!=3 || !create_return_values(0))
6139 int32_t from=get_int_from_int_or_str();
6140 int32_t to=get_int_from_int_or_str();
6142 return ui_hmm->best_path(from, to);
6145 bool CSGInterface::cmd_best_path_2struct()
6147 if (m_nrhs!=12 || !create_return_values(3))
6150 SG_ERROR(
"Sorry, this parameter list is awful!\n")
6155 void CSGInterface::get_vector(
bool*& vector, int32_t& len)
6157 int32_t* int_vector;
6158 get_vector(int_vector, len);
6161 vector= SG_MALLOC(
bool, len);
6163 for (int32_t i=0; i<len; i++)
6164 vector[i]= (int_vector[i]!=0);
6166 SG_FREE(int_vector);
6169 void CSGInterface::set_vector(
const bool* vector, int32_t len)
6171 int32_t* int_vector = SG_MALLOC(int32_t, len);
6172 for (int32_t i=0;i<len;i++)
6179 set_vector(int_vector,len);
6180 SG_FREE(int_vector);
6183 bool CSGInterface::cmd_set_plif_struct()
6188 get_vector(ids,Nid);
6194 get_string_list(names, Nname,Mname);
6200 get_matrix(all_limits, Mlimits, Nlimits);
6203 int32_t Npenalties=0;
6204 int32_t Mpenalties=0;
6206 get_matrix(all_penalties, Mpenalties, Npenalties);
6209 int32_t Ntransform=0;
6210 int32_t Mtransform=0;
6212 get_string_list(all_transform, Ntransform, Mtransform);
6217 get_vector(min_values,Nmin);
6222 get_vector(max_values,Nmax);
6226 bool* all_use_cache;
6227 get_vector(all_use_cache,Ncache);
6231 int32_t* all_use_svm;
6232 get_vector(all_use_svm,Nsvm);
6237 get_vector(all_do_calc,Ncalc);
6240 SG_ERROR(
"Ncalc!=Nsvm, Ncalc:%i, Nsvm:%i\n",Ncalc,Nsvm)
6242 SG_ERROR(
"Ncalc!=Ncache, Ncalc:%i, Ncache:%i\n",Ncalc,Ncache)
6243 if (Ncalc!=Ntransform)
6244 SG_ERROR(
"Ncalc!=Ntransform, Ncalc:%i, Ntransform:%i\n",Ncalc,Ntransform)
6246 SG_ERROR(
"Ncalc!=Nmin, Ncalc:%i, Nmin:%i\n",Ncalc,Nmin)
6248 SG_ERROR(
"Ncalc!=Nmax, Ncalc:%i, Nmax:%i\n",Ncalc,Nmax)
6249 if (Ncalc!=Npenalties)
6250 SG_ERROR(
"Ncalc!=Npenalties, Ncalc:%i, Npenalties:%i\n",Ncalc,Npenalties)
6252 SG_ERROR(
"Ncalc!=Nlimits, Ncalc:%i, Nlimits:%i\n",Ncalc,Nlimits)
6254 SG_ERROR(
"Ncalc!=Nname, Ncalc:%i, Nname:%i\n",Ncalc,Nname)
6256 SG_ERROR(
"Ncalc!=Nid, Ncalc:%i, Nid:%i\n",Ncalc,Nid)
6257 if (Mlimits!=Mpenalties)
6258 SG_ERROR(
"Mlimits!=Mpenalties, Mlimits:%i, Mpenalties:%i\n",Mlimits,Mpenalties)
6261 int32_t M = Mlimits;
6275 SG_FREE(all_transform);
6276 SG_FREE(all_do_calc);
6281 bool CSGInterface::cmd_get_plif_struct()
6289 int32_t* ids = SG_MALLOC(int32_t, N);
6296 bool* all_use_cache = SG_MALLOC(
bool, N);
6297 int32_t* all_use_svm = SG_MALLOC(int32_t, N);
6298 bool* all_do_calc = SG_MALLOC(
bool, N);
6299 for (int32_t i=0;i<N;i++)
6303 names[i].
slen = strlen(PEN[i]->get_plif_name());
6306 for (int32_t j=0;j<M;j++)
6308 all_limits[i*M+j]=limits[j];
6309 all_penalties[i*M+j]=penalties[j];
6311 all_transform[i].
string = (
char*) PEN[i]->get_transform_type();
6312 all_transform[i].
slen = strlen(PEN[i]->get_transform_type());
6321 set_string_list(names, N);
6322 set_matrix(all_limits, M, N);
6323 set_matrix(all_penalties, M, N);
6324 set_string_list(all_transform, N);
6325 set_vector(min_values,N);
6326 set_vector(max_values,N);
6327 set_vector(all_use_cache,N);
6328 set_vector(all_use_svm,N);
6329 set_vector(all_do_calc,N);
6332 SG_FREE(max_values);
6333 SG_FREE(min_values);
6335 SG_FREE(all_transform);
6336 SG_FREE(all_limits);
6337 SG_FREE(all_penalties);
6338 SG_FREE(all_use_cache);
6339 SG_FREE(all_use_svm);
6340 SG_FREE(all_do_calc);
6357 bool CSGInterface::cmd_signals_set_positions()
6361 bool CSGInterface::cmd_signals_set_labels()
6365 bool CSGInterface::cmd_signals_set_split()
6369 bool CSGInterface::cmd_signals_set_train_mask()
6373 bool CSGInterface::cmd_signals_add_feature()
6377 bool CSGInterface::cmd_signals_add_kernel()
6381 bool CSGInterface::cmd_signals_run()
6386 bool CSGInterface::cmd_init_dyn_prog()
6389 int32_t num_svms=get_int();
6392 ui_structure->set_dyn_prog(h);
6396 bool CSGInterface::cmd_clean_up_dyn_prog()
6398 return ui_structure->cleanup();
6401 bool CSGInterface::cmd_set_model()
6406 CDynProg* h = ui_structure->get_dyn_prog();
6417 get_ndarray(penalties_array,Dim,numDim);
6422 SG_ERROR(
"error computing plif matrix\n")
6423 ui_structure->set_num_states(Dim[0]);
6424 SG_FREE(penalties_array);
6428 bool use_orf = get_bool();
6429 ui_structure->set_use_orf(use_orf);
6436 get_matrix(mod_words, Nmod,Mmod);
6437 if (Nmod != num_svms)
6438 SG_ERROR(
"should be equal: Nmod: %i, num_svms: %i\n",Nmod,num_svms)
6444 int32_t num_states=0;
6445 int32_t feat_dim3=0;
6446 int32_t* state_signals;
6447 get_matrix(state_signals,num_states,feat_dim3);
6448 ASSERT(num_states==Dim[0])
6457 get_matrix(orf_info,Norf,Morf);
6461 ui_structure->set_orf_info(orf_info, Norf, Morf);
6469 bool CSGInterface::cmd_precompute_content_svms()
6475 seq = get_string(seq_len);
6481 get_vector(all_pos, Npos);
6488 get_matrix(weights, Nweights, num_svms);
6490 SG_PRINT(
"Dimension mismatch: got %i, expect %i\n", Nweights, 5440)
6491 ui_structure->set_content_svm_weights(weights, Nweights, num_svms);
6493 CDynProg* h = ui_structure->get_dyn_prog();
6495 SG_ERROR(
"no DynProg object found, use init_dyn_prog first\n")
6508 SG_DEBUG(
"precompute_content_svms done\n")
6512 bool CSGInterface::cmd_get_lin_feat()
6514 CDynProg* h = ui_structure->get_dyn_prog();
6516 SG_ERROR(
"no DynProg object found, use set_model first\n")
6519 int32_t dim1, dim2 = 0;
6522 set_matrix(lin_feat, dim1, dim2);
6526 bool CSGInterface::cmd_set_lin_feat()
6531 seq = get_string(Nseq);
6537 get_vector(all_pos, Npos);
6541 int32_t num_svms, seq_len;
6543 get_matrix(lin_feat, num_svms, seq_len);
6547 SG_ERROR(
"Dimension mismatch: got %i positions and (%ix%i) values\n", Npos, num_svms, seq_len)
6556 CDynProg* h = ui_structure->get_dyn_prog();
6558 SG_ERROR(
"no DynProg object found, use set_model first\n")
6570 bool CSGInterface::cmd_long_transition_settings()
6572 bool use_long_transitions = get_bool();
6573 int32_t threshold = get_int();
6574 int32_t max_len = get_int();
6576 CDynProg* h = ui_structure->get_dyn_prog();
6578 SG_ERROR(
"no DynProg object found, use set_model first\n")
6584 bool CSGInterface::cmd_set_feature_matrix()
6586 int32_t num_states = ui_structure->get_num_states();
6593 get_ndarray(features, Dims, numDims);
6596 SG_ERROR(
"expected a 3 dimensional array, got %i dimensions\n", numDims)
6597 if (Dims[0]!=num_states)
6598 SG_ERROR(
"number of rows (%i) not equal number of states (%i)\n",Dims[0], num_states)
6599 ASSERT(ui_structure->set_feature_matrix(features, Dims))
6601 ASSERT(ui_structure->set_feature_dims(Dims))
6608 bool CSGInterface::cmd_set_feature_matrix_sparse()
6610 int32_t num_pos = ui_structure->get_num_positions();
6611 int32_t num_states = ui_structure->get_num_states();
6615 int32_t dim11, dim12 ;
6617 get_sparse_matrix(features1, dim11, dim12);
6619 int32_t dim21, dim22 ;
6621 get_sparse_matrix(features2, dim21, dim22);
6626 int32_t *Dims = SG_MALLOC(int32_t, 3);
6631 ASSERT(Dims[0]==num_states)
6634 ASSERT(ui_structure->set_feature_matrix_sparse(features1, features2, Dims))
6635 ASSERT(ui_structure->set_feature_dims(Dims))
6643 bool CSGInterface::cmd_init_intron_list()
6646 int32_t Nstart_positions;
6647 int32_t* start_positions;
6648 get_vector(start_positions, Nstart_positions);
6652 int32_t Nend_positions;
6653 int32_t* end_positions;
6654 get_vector(end_positions, Nend_positions);
6660 get_vector(quality, Nquality);
6666 get_vector(all_pos, Nall_pos);
6669 ASSERT(Nquality==Nend_positions)
6670 ASSERT(Nend_positions==Nstart_positions)
6674 intron_list->
init_list(all_pos, Nall_pos);
6676 intron_list->
read_introns(start_positions, end_positions, quality, Nstart_positions);
6678 SG_FREE(start_positions);
6679 SG_FREE(end_positions);
6689 CDynProg* h = ui_structure->get_dyn_prog();
6691 SG_ERROR(
"no DynProg object found, use set_model first\n")
6697 bool CSGInterface::cmd_precompute_tiling_features()
6701 CDynProg* h = ui_structure->get_dyn_prog();
6703 int32_t Nintensities=0;
6705 get_vector(intensities, Nintensities);
6707 int32_t Nprobe_pos=0;
6709 get_vector(probe_pos, Nprobe_pos);
6710 ASSERT(Nprobe_pos==Nintensities)
6712 int32_t Ntiling_plif_ids=0;
6713 int32_t* tiling_plif_ids;
6714 get_vector(tiling_plif_ids, Ntiling_plif_ids);
6721 bool CSGInterface::cmd_best_path_trans()
6723 CDynProg* h = ui_structure->get_dyn_prog();
6730 int32_t* feat_dims = ui_structure->get_feature_dims();
6731 float64_t* features = (ui_structure->get_feature_matrix(
false));
6734 int32_t* orf_info = ui_structure->get_orf_info();
6735 bool use_orf = ui_structure->get_use_orf();
6744 SG_ERROR(
"# transitions from initial state (%i) does not match # states (%i)\n", Np, num_states)
6752 SG_ERROR(
"# transitions to end state (%i) does not match # states (%i)\n", Nq, num_states)
6758 get_vector(all_nbest, Nnbest);
6763 nbest =all_nbest[0];
6764 nother=all_nbest[1];
6767 nbest =all_nbest[0];
6774 int32_t Nseg_path=0;
6775 int32_t Mseg_path=0;
6777 get_matrix(seg_path, Nseg_path, Mseg_path);
6782 int32_t num_a_trans=0;
6784 get_matrix(a_trans, num_a_trans, Na_trans);
6793 get_matrix(loss, Nloss,Mloss);
6819 SG_ERROR(
"svm arrays inconsistent\n")
6824 SG_DEBUG(
"best_path_trans: M: %i, Mseg_path: %i\n", M, Mseg_path)
6841 bool segment_loss_non_zero=
false;
6842 for (int32_t i=0; i<Nloss*Mloss; i++)
6845 segment_loss_non_zero=
true;
6855 if (segment_loss_non_zero)
6857 SG_DEBUG(
"Using version with segment_loss\n")
6865 SG_DEBUG(
"Using version without segment_loss\n")