SHOGUN  v2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SGInterface.h
Go to the documentation of this file.
1 #ifndef __SGINTERFACE__H_
2 #define __SGINTERFACE__H_
3 
4 #include <shogun/lib/memory.h>
5 #include <shogun/lib/config.h>
6 #include <shogun/lib/common.h>
7 #include <shogun/base/SGObject.h>
12 #include <shogun/kernel/Kernel.h>
13 
15 #include <shogun/ui/GUIDistance.h>
16 #include <shogun/ui/GUIFeatures.h>
17 #include <shogun/ui/GUIHMM.h>
18 #include <shogun/ui/GUIKernel.h>
19 #include <shogun/ui/GUILabels.h>
20 #include <shogun/ui/GUIMath.h>
23 #include <shogun/ui/GUITime.h>
24 #include <shogun/ui/GUIStructure.h>
25 #include <shogun/ui/GUISignals.h>
26 #include <shogun/ui/GUIConverter.h>
27 
28 namespace shogun
29 {
34 enum IFType
35 {
37  UNDEFINED,
38 
40  SCALAR_INT,
41  SCALAR_REAL,
42  SCALAR_BOOL,
43  STANDARD_STRING,
44 
46  VECTOR_BOOL,
47  VECTOR_BYTE,
48  VECTOR_CHAR,
49  VECTOR_INT,
50  VECTOR_REAL,
51  VECTOR_SHORTREAL,
52  VECTOR_SHORT,
53  VECTOR_WORD,
54 
56  DENSE_INT,
57  DENSE_REAL,
58  DENSE_SHORTREAL,
59  DENSE_SHORT,
60  DENSE_WORD,
61 
63  NDARRAY_BYTE,
64  NDARRAY_CHAR,
65  NDARRAY_INT,
66  NDARRAY_REAL,
67  NDARRAY_SHORTREAL,
68  NDARRAY_SHORT,
69  NDARRAY_WORD,
70 
72  SPARSE_BYTE,
73  SPARSE_CHAR,
74  SPARSE_INT,
75  SPARSE_REAL,
76  SPARSE_SHORT,
77  SPARSE_SHORTREAL,
78  SPARSE_WORD,
79 
81  STRING_BYTE,
82  STRING_CHAR,
83  STRING_INT,
84  STRING_SHORT,
85  STRING_WORD,
86 
88  ATTR_STRUCT
89 };
90 
92 enum E_WHICH_OBJ
93 {
95  SVM_PRIMAL,
97  SVM_DUAL,
99  MKL_PRIMAL,
101  MKL_DUAL,
103  MKL_RELATIVE_DUALITY_GAP,
105  MKL_ABSOLUTE_DUALITY_GAP
106 };
107 
109 class CSGInterface : public CSGObject
110 {
111  public:
115  CSGInterface(bool print_copyrights=true);
116 
118  ~CSGInterface();
119 
121  virtual void reset();
122 
124  void translate_arg(CSGInterface* source, CSGInterface* target);
125 
126  /* commands */
128  bool cmd_load_features();
130  bool cmd_save_features();
132  bool cmd_clean_features();
134  bool cmd_get_features();
136  bool cmd_add_features();
138  bool cmd_add_multiple_features();
140  bool cmd_add_dotfeatures();
142  bool cmd_set_features();
144  bool cmd_set_reference_features();
146  bool cmd_del_last_features();
148  bool cmd_convert();
150  bool cmd_reshape();
152  bool cmd_load_labels();
154  bool cmd_set_labels();
156  bool cmd_get_labels();
157 
159  bool cmd_set_kernel_normalization();
161  bool cmd_set_kernel();
163  bool cmd_add_kernel();
165  bool cmd_del_last_kernel();
167  bool cmd_init_kernel();
169  bool cmd_clean_kernel();
171  bool cmd_save_kernel();
173  bool cmd_load_kernel_init();
175  bool cmd_save_kernel_init();
177  bool cmd_get_kernel_matrix();
179  bool cmd_set_WD_position_weights();
181  bool cmd_get_subkernel_weights();
183  bool cmd_set_subkernel_weights();
185  bool cmd_set_subkernel_weights_combined();
187  bool cmd_get_dotfeature_weights_combined();
189  bool cmd_set_dotfeature_weights_combined();
191  bool cmd_set_last_subkernel_weights();
193  bool cmd_get_WD_position_weights();
195  bool cmd_get_last_subkernel_weights();
197  bool cmd_compute_by_subkernels();
199  bool cmd_init_kernel_optimization();
201  bool cmd_get_kernel_optimization();
203  bool cmd_delete_kernel_optimization();
205  bool cmd_use_diagonal_speedup();
207  bool cmd_set_kernel_optimization_type();
209  bool cmd_set_solver();
211  bool cmd_set_constraint_generator();
213  bool cmd_set_prior_probs();
215  bool cmd_set_prior_probs_from_labels();
216 #ifdef USE_SVMLIGHT
217 
218  bool cmd_resize_kernel_cache();
219 #endif //USE_SVMLIGHT
220 
221 
223  bool cmd_set_distance();
225  bool cmd_init_distance();
227  bool cmd_get_distance_matrix();
228 
230  bool cmd_get_SPEC_consensus();
232  bool cmd_get_SPEC_scoring();
234  bool cmd_get_WD_consensus();
236  bool cmd_compute_POIM_WD();
238  bool cmd_get_WD_scoring();
239 
241  bool cmd_new_classifier();
243  bool cmd_load_classifier();
245  bool cmd_save_classifier();
247  bool cmd_get_svm();
249  bool cmd_get_num_svms();
251  bool cmd_set_svm();
253  bool cmd_set_linear_classifier();
255  bool cmd_classify();
257  bool cmd_classify_example();
259  bool cmd_get_classifier();
261  bool cmd_get_svm_objective();
263  bool cmd_compute_svm_primal_objective();
265  bool cmd_compute_svm_dual_objective();
267  bool cmd_compute_mkl_dual_objective();
269  bool cmd_compute_relative_mkl_duality_gap();
271  bool cmd_compute_absolute_mkl_duality_gap();
273  bool cmd_train_classifier();
275  bool cmd_do_auc_maximization();
277  bool cmd_set_perceptron_parameters();
279  bool cmd_set_svm_qpsize();
281  bool cmd_set_svm_max_qpsize();
283  bool cmd_set_svm_bufsize();
285  bool cmd_set_svm_C();
287  bool cmd_set_svm_epsilon();
289  bool cmd_set_svr_tube_epsilon();
291  bool cmd_set_svm_nu();
293  bool cmd_set_svm_mkl_parameters();
295  bool cmd_set_elasticnet_lambda();
297  bool cmd_set_mkl_block_norm();
299  bool cmd_set_max_train_time();
301  bool cmd_set_svm_mkl_enabled();
303  bool cmd_set_svm_shrinking_enabled();
305  bool cmd_set_svm_batch_computation_enabled();
307  bool cmd_set_svm_linadd_enabled();
309  bool cmd_set_svm_bias_enabled();
311  bool cmd_set_mkl_interleaved_enabled();
313  bool cmd_set_krr_tau();
314 
316  bool cmd_add_preproc();
318  bool cmd_del_preproc();
320  bool cmd_attach_preproc();
322  bool cmd_clean_preproc();
323 
325  bool cmd_set_converter();
327  bool cmd_embed();
328 
330  bool cmd_new_hmm();
332  bool cmd_load_hmm();
334  bool cmd_save_hmm();
336  bool cmd_hmm_classify();
338  bool cmd_hmm_classify_example();
340  bool cmd_one_class_linear_hmm_classify();
342  bool cmd_one_class_hmm_classify();
344  bool cmd_one_class_hmm_classify_example();
346  bool cmd_output_hmm();
348  bool cmd_output_hmm_defined();
350  bool cmd_hmm_likelihood();
352  bool cmd_likelihood();
354  bool cmd_save_likelihood();
356  bool cmd_get_viterbi_path();
358  bool cmd_viterbi_train_defined();
360  bool cmd_viterbi_train();
362  bool cmd_baum_welch_train();
364  bool cmd_baum_welch_train_defined();
366  bool cmd_baum_welch_trans_train();
368  bool cmd_linear_train();
370  bool cmd_save_path();
372  bool cmd_append_hmm();
374  bool cmd_append_model();
376  bool cmd_set_hmm();
378  bool cmd_set_hmm_as();
380  bool cmd_get_hmm();
382  bool cmd_set_chop();
384  bool cmd_set_pseudo();
386  bool cmd_load_definitions();
388  bool cmd_convergence_criteria();
390  bool cmd_normalize();
392  bool cmd_add_states();
394  bool cmd_permutation_entropy();
396  bool cmd_relative_entropy();
398  bool cmd_entropy();
400  bool cmd_new_plugin_estimator();
402  bool cmd_train_estimator();
404  bool cmd_plugin_estimate_classify_example();
406  bool cmd_plugin_estimate_classify();
408  bool cmd_set_plugin_estimate();
410  bool cmd_get_plugin_estimate();
412  bool cmd_best_path();
414  bool cmd_best_path_2struct();
420  bool cmd_set_plif_struct();
427  bool cmd_get_plif_struct();
431  bool cmd_precompute_subkernels();
435  bool cmd_signals_set_model() { return false; };
439  bool cmd_signals_set_positions();
443  bool cmd_signals_set_labels();
447  bool cmd_signals_set_split();
451  bool cmd_signals_set_train_mask();
455  bool cmd_signals_add_feature();
459  bool cmd_signals_add_kernel();
463  bool cmd_signals_run();
476  bool cmd_precompute_content_svms();
480  bool cmd_get_lin_feat();
484  bool cmd_set_lin_feat();
485 
489  bool cmd_init_dyn_prog();
490 
494  bool cmd_clean_up_dyn_prog();
495 
499  bool cmd_init_intron_list();
500 
502  bool cmd_long_transition_settings();
503 
512  bool cmd_precompute_tiling_features();
524  bool cmd_set_model();
529  bool cmd_set_feature_matrix_sparse();
534  bool cmd_set_feature_matrix();
536  bool cmd_best_path_trans();
538  bool cmd_best_path_trans_deriv();
540  bool cmd_best_path_no_b();
542  bool cmd_best_path_no_b_trans();
543 
545  bool cmd_crc();
547  bool cmd_system();
549  bool cmd_exit();
551  bool cmd_exec();
553  bool cmd_set_output();
555  bool cmd_set_threshold();
557  bool cmd_init_random();
559  bool cmd_set_num_threads();
561  bool cmd_translate_string();
563  bool cmd_clear();
565  bool cmd_tic();
567  bool cmd_toc();
569  bool cmd_echo();
571  bool cmd_print();
573  bool cmd_loglevel();
575  bool cmd_progress();
577  bool cmd_syntax_highlight();
579  bool cmd_get_version();
581  bool cmd_help();
583  bool cmd_whos();
585  bool cmd_send_command();
587  virtual bool cmd_run_python();
589  virtual bool cmd_run_octave();
591  virtual bool cmd_run_r();
593  virtual bool cmd_pr_loqo();
594 
597 
598  virtual IFType get_argument_type()=0;
599 
601  virtual int32_t get_int()=0;
603  virtual float64_t get_real()=0;
605  virtual bool get_bool()=0;
606 
610  virtual char* get_string(int32_t& len)=0;
611 
616  virtual void get_vector(bool*& vector, int32_t& len);
621  virtual void get_vector(uint8_t*& vector, int32_t& len)=0;
626  virtual void get_vector(char*& vector, int32_t& len)=0;
631  virtual void get_vector(int32_t*& vector, int32_t& len)=0;
636  virtual void get_vector(float64_t*& vector, int32_t& len)=0;
641  virtual void get_vector(float32_t*& vector, int32_t& len)=0;
646  virtual void get_vector(int16_t*& vector, int32_t& len)=0;
651  virtual void get_vector(uint16_t*& vector, int32_t& len)=0;
652 
658  virtual void get_matrix(
659  uint8_t*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
665  virtual void get_matrix(
666  char*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
672  virtual void get_matrix(
673  int32_t*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
679  virtual void get_matrix(
680  float32_t*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
686  virtual void get_matrix(
687  float64_t*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
693  virtual void get_matrix(
694  int16_t*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
700  virtual void get_matrix(
701  uint16_t*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
702 
708  virtual void get_ndarray(
709  uint8_t*& array, int32_t*& dims, int32_t& num_dims)=0;
715  virtual void get_ndarray(
716  char*& array, int32_t*& dims, int32_t& num_dims)=0;
722  virtual void get_ndarray(
723  int32_t*& array, int32_t*& dims, int32_t& num_dims)=0;
729  virtual void get_ndarray(
730  float32_t*& array, int32_t*& dims, int32_t& num_dims)=0;
736  virtual void get_ndarray(
737  float64_t*& array, int32_t*& dims, int32_t& num_dims)=0;
743  virtual void get_ndarray(
744  int16_t*& array, int32_t*& dims, int32_t& num_dims)=0;
750  virtual void get_ndarray(
751  uint16_t*& array, int32_t*& dims, int32_t& num_dims)=0;
752 
758  virtual void get_sparse_matrix(
759  SGSparseVector<float64_t>*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
760 
761  /* future versions might support types other than float64_t
762 
763  virtual void get_sparse_matrix(SGSparseVector<uint8_t>*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
764  virtual void get_sparse_matrix(SGSparseVector<char>*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
765  virtual void get_sparse_matrix(SGSparseVector<int32_t>*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
766  virtual void get_sparse_matrix(SGSparseVector<float32_t>*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
767  virtual void get_sparse_matrix(SGSparseVector<int16_t>*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
768  virtual void get_sparse_matrix(SGSparseVector<uint16_t>*& matrix, int32_t& num_feat, int32_t& num_vec)=0; */
769 
775  virtual void get_string_list(
776  SGString<uint8_t>*& strings, int32_t& num_str,
777  int32_t& max_string_len)=0;
783  virtual void get_string_list(
784  SGString<char>*& strings, int32_t& num_str,
785  int32_t& max_string_len)=0;
791  virtual void get_string_list(
792  SGString<int32_t>*& strings, int32_t& num_str,
793  int32_t& max_string_len)=0;
799  virtual void get_string_list(
800  SGString<int16_t>*& strings, int32_t& num_str,
801  int32_t& max_string_len)=0;
807  virtual void get_string_list(
808  SGString<uint16_t>*& strings, int32_t& num_str,
809  int32_t& max_string_len)=0;
810 
814  virtual void get_attribute_struct(
815  const CDynamicArray<T_ATTRIBUTE>* &attrs)=0;
816 
817  // set functions - to pass data from shogun to the target interface
821  virtual bool create_return_values(int32_t num_val)=0;
822 
826  virtual void set_int(int32_t scalar)=0;
830  virtual void set_real(float64_t scalar)=0;
834  virtual void set_bool(bool scalar)=0;
835 
840  virtual void set_vector(const bool* vector, int32_t len);
845  virtual void set_vector(const uint8_t* vector, int32_t len)=0;
850  virtual void set_vector(const char* vector, int32_t len)=0;
855  virtual void set_vector(const int32_t* vector, int32_t len)=0;
860  virtual void set_vector(const float32_t* vector, int32_t len)=0;
865  virtual void set_vector(const float64_t* vector, int32_t len)=0;
870  virtual void set_vector(const int16_t* vector, int32_t len)=0;
875  virtual void set_vector(const uint16_t* vector, int32_t len)=0;
876 
882  virtual void set_matrix(
883  const uint8_t* matrix, int32_t num_feat, int32_t num_vec)=0;
889  virtual void set_matrix(
890  const char* matrix, int32_t num_feat, int32_t num_vec)=0;
896  virtual void set_matrix(
897  const int32_t* matrix, int32_t num_feat, int32_t num_vec)=0;
903  virtual void set_matrix(
904  const float32_t* matrix, int32_t num_feat, int32_t num_vec)=0;
910  virtual void set_matrix(
911  const float64_t* matrix, int32_t num_feat, int32_t num_vec)=0;
917  virtual void set_matrix(
918  const int16_t* matrix, int32_t num_feat, int32_t num_vec)=0;
924  virtual void set_matrix(
925  const uint16_t* matrix, int32_t num_feat, int32_t num_vec)=0;
926 
933  virtual void set_sparse_matrix(
934  const SGSparseVector<float64_t>* matrix, int32_t num_feat,
935  int32_t num_vec, int64_t nnz)=0;
936 
937  /* future versions might support types other than float64_t
938 
939  virtual void set_sparse_matrix(const SGSparseVector<uint8_t>* matrix, int32_t num_feat, int32_t num_vec)=0;
940  virtual void set_sparse_matrix(const SGSparseVector<char>* matrix, int32_t num_feat, int32_t num_vec)=0;
941  virtual void set_sparse_matrix(const SGSparseVector<int32_t>* matrix, int32_t num_feat, int32_t num_vec)=0;
942  virtual void set_sparse_matrix(const SGSparseVector<float32_t>* matrix, int32_t num_feat, int32_t num_vec)=0;
943  virtual void set_sparse_matrix(const SGSparseVector<int16_t>* matrix, int32_t num_feat, int32_t num_vec)=0;
944  virtual void set_sparse_matrix(const SGSparseVector<uint16_t>* matrix, int32_t num_feat, int32_t num_vec)=0; */
945 
950  virtual void set_string_list(
951  const SGString<uint8_t>* strings, int32_t num_str)=0;
956  virtual void set_string_list(
957  const SGString<char>* strings, int32_t num_str)=0;
962  virtual void set_string_list(
963  const SGString<int32_t>* strings, int32_t num_str)=0;
968  virtual void set_string_list(
969  const SGString<int16_t>* strings, int32_t num_str)=0;
974  virtual void set_string_list(
975  const SGString<uint16_t>* strings, int32_t num_str)=0;
976 
980  virtual void set_attribute_struct(
981  const CDynamicArray<T_ATTRIBUTE>* attrs)=0;
982 
984  bool handle();
985 
987  void print_prompt();
988 
990  int32_t get_nlhs() { return m_nlhs; }
991 
993  int32_t get_nrhs() { return m_nrhs; }
994 
995 
996  // ui lib
998  CGUIClassifier* ui_classifier;
1000  CGUIDistance* ui_distance;
1002  CGUIFeatures* ui_features;
1004  CGUIHMM* ui_hmm;
1006  CGUIKernel* ui_kernel;
1008  CGUILabels* ui_labels;
1010  CGUIMath* ui_math;
1012  CGUIPluginEstimate* ui_pluginestimate;
1014  CGUIPreprocessor* ui_preproc;
1016  CGUITime* ui_time;
1018  CGUIStructure* ui_structure;
1019  //CGUISignals* ui_signals;
1021  CGUIConverter* ui_converter;
1022 
1023  protected:
1031  static bool strmatch(const char* str, const char* cmd, int32_t len=-1)
1032  {
1033  if (len==-1)
1034  {
1035  len=strlen(cmd);
1036  if (strlen(str)!=(size_t) len) // match exact length
1037  return false;
1038  }
1039 
1040  return (strncmp(str, cmd, len)==0);
1041  }
1042 
1047  static bool strendswith(const char* str, const char* cmd)
1048  {
1049  size_t idx=strlen(str);
1050  size_t len=strlen(cmd);
1051 
1052  if (strlen(str) < len)
1053  return false;
1054 
1055  str=&str[idx-len];
1056 
1057  return (strncmp(str, cmd, len)==0);
1058  }
1060  char* get_command(int32_t &len)
1061  {
1062  ASSERT(m_rhs_counter==0);
1063  if (m_nrhs<=0)
1064  SG_SERROR("No input arguments supplied.\n");
1065 
1066  return get_string(len);
1067  }
1068  private:
1070  bool do_compute_objective(E_WHICH_OBJ obj);
1072  bool do_hmm_classify(bool linear=false, bool one_class=false);
1074  bool do_hmm_classify_example(bool one_class=false);
1076  bool do_set_features(bool add=false, bool check_dot=false, int32_t repetitions=1);
1077 
1079  void convert_to_bitembedding(CFeatures* &features, bool convert_to_word, bool convert_to_ulong);
1081  void obtain_from_single_string(CFeatures* features);
1083  bool obtain_from_position_list(CFeatures* features);
1085  bool obtain_by_sliding_window(CFeatures* features);
1087  CKernel* create_kernel();
1088 
1090  CFeatures* create_custom_string_features(CStringFeatures<uint8_t>* f);
1091 
1092  CFeatures* create_custom_real_features(CDenseFeatures<float64_t>* orig_feat);
1094  char* get_str_from_str_or_direct(int32_t& len);
1095  int32_t get_int_from_int_or_str();
1096  float64_t get_real_from_real_or_str();
1097  bool get_bool_from_bool_or_str();
1098  void get_vector_from_int_vector_or_str(
1099  int32_t*& vector, int32_t& len);
1100  void get_vector_from_real_vector_or_str(
1101  float64_t*& vector, int32_t& len);
1102  int32_t get_vector_len_from_str(int32_t expected_len=0);
1103  char* get_str_from_str(int32_t& len);
1104  int32_t get_num_args_in_str();
1105 
1108  char* get_line(FILE* infile=stdin, bool show_prompt=true);
1109 
1110  protected:
1112  int32_t m_lhs_counter;
1114  int32_t m_rhs_counter;
1116  int32_t m_nlhs;
1118  int32_t m_nrhs;
1119 
1120  // related to cmd_exec and cmd_echo
1122  FILE* file_out;
1124  char input[10000];
1126  bool echo;
1127 
1129  char* m_legacy_strptr;
1130 };
1131 
1133 typedef bool (CSGInterface::*CSGInterfacePtr)();
1134 
1135 #ifndef DOXYGEN_SHOULD_SKIP_THIS
1136 
1137 typedef struct {
1139  const char* command;
1141  CSGInterfacePtr method;
1143  const char* usage_prefix;
1145  const char* usage_suffix;
1146 } CSGInterfaceMethod;
1147 }
1148 #endif
1149 
1150 #endif // __SGINTERFACE__H_

SHOGUN Machine Learning Toolbox - Documentation