SHOGUN  v1.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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 
27 namespace shogun
28 {
33 enum IFType
34 {
36  UNDEFINED,
37 
39  SCALAR_INT,
40  SCALAR_REAL,
41  SCALAR_BOOL,
42  STANDARD_STRING,
43 
45  VECTOR_BOOL,
46  VECTOR_BYTE,
47  VECTOR_CHAR,
48  VECTOR_INT,
49  VECTOR_REAL,
50  VECTOR_SHORTREAL,
51  VECTOR_SHORT,
52  VECTOR_WORD,
53 
55  DENSE_INT,
56  DENSE_REAL,
57  DENSE_SHORTREAL,
58  DENSE_SHORT,
59  DENSE_WORD,
60 
62  NDARRAY_BYTE,
63  NDARRAY_CHAR,
64  NDARRAY_INT,
65  NDARRAY_REAL,
66  NDARRAY_SHORTREAL,
67  NDARRAY_SHORT,
68  NDARRAY_WORD,
69 
71  SPARSE_BYTE,
72  SPARSE_CHAR,
73  SPARSE_INT,
74  SPARSE_REAL,
75  SPARSE_SHORT,
76  SPARSE_SHORTREAL,
77  SPARSE_WORD,
78 
80  STRING_BYTE,
81  STRING_CHAR,
82  STRING_INT,
83  STRING_SHORT,
84  STRING_WORD,
85 
87  ATTR_STRUCT
88 };
89 
91 enum E_WHICH_OBJ
92 {
94  SVM_PRIMAL,
96  SVM_DUAL,
98  MKL_PRIMAL,
100  MKL_DUAL,
102  MKL_RELATIVE_DUALITY_GAP,
104  MKL_ABSOLUTE_DUALITY_GAP
105 };
106 
108 class CSGInterface : public CSGObject
109 {
110  public:
114  CSGInterface(bool print_copyrights=true);
115 
117  ~CSGInterface();
118 
120  virtual void reset();
121 
123  void translate_arg(CSGInterface* source, CSGInterface* target);
124 
125  /* commands */
127  bool cmd_load_features();
129  bool cmd_save_features();
131  bool cmd_clean_features();
133  bool cmd_get_features();
135  bool cmd_add_features();
137  bool cmd_add_multiple_features();
139  bool cmd_add_dotfeatures();
141  bool cmd_set_features();
143  bool cmd_set_reference_features();
145  bool cmd_del_last_features();
147  bool cmd_convert();
149  bool cmd_reshape();
151  bool cmd_load_labels();
153  bool cmd_set_labels();
155  bool cmd_get_labels();
156 
158  bool cmd_set_kernel_normalization();
160  bool cmd_set_kernel();
162  bool cmd_add_kernel();
164  bool cmd_del_last_kernel();
166  bool cmd_init_kernel();
168  bool cmd_clean_kernel();
170  bool cmd_save_kernel();
172  bool cmd_load_kernel_init();
174  bool cmd_save_kernel_init();
176  bool cmd_get_kernel_matrix();
178  bool cmd_set_WD_position_weights();
180  bool cmd_get_subkernel_weights();
182  bool cmd_set_subkernel_weights();
184  bool cmd_set_subkernel_weights_combined();
186  bool cmd_get_dotfeature_weights_combined();
188  bool cmd_set_dotfeature_weights_combined();
190  bool cmd_set_last_subkernel_weights();
192  bool cmd_get_WD_position_weights();
194  bool cmd_get_last_subkernel_weights();
196  bool cmd_compute_by_subkernels();
198  bool cmd_init_kernel_optimization();
200  bool cmd_get_kernel_optimization();
202  bool cmd_delete_kernel_optimization();
204  bool cmd_use_diagonal_speedup();
206  bool cmd_set_kernel_optimization_type();
208  bool cmd_set_solver();
210  bool cmd_set_constraint_generator();
212  bool cmd_set_prior_probs();
214  bool cmd_set_prior_probs_from_labels();
215 
216 
217 
219  bool cmd_set_distance();
221  bool cmd_init_distance();
223  bool cmd_get_distance_matrix();
224 
226  bool cmd_get_SPEC_consensus();
228  bool cmd_get_SPEC_scoring();
230  bool cmd_get_WD_consensus();
232  bool cmd_compute_POIM_WD();
234  bool cmd_get_WD_scoring();
235 
237  bool cmd_new_classifier();
239  bool cmd_load_classifier();
241  bool cmd_save_classifier();
243  bool cmd_get_svm();
245  bool cmd_get_num_svms();
247  bool cmd_set_svm();
249  bool cmd_set_linear_classifier();
251  bool cmd_classify();
253  bool cmd_classify_example();
255  bool cmd_get_classifier();
257  bool cmd_get_svm_objective();
259  bool cmd_compute_svm_primal_objective();
261  bool cmd_compute_svm_dual_objective();
263  bool cmd_compute_mkl_dual_objective();
265  bool cmd_compute_relative_mkl_duality_gap();
267  bool cmd_compute_absolute_mkl_duality_gap();
269  bool cmd_train_classifier();
271  bool cmd_do_auc_maximization();
273  bool cmd_set_perceptron_parameters();
275  bool cmd_set_svm_qpsize();
277  bool cmd_set_svm_max_qpsize();
279  bool cmd_set_svm_bufsize();
281  bool cmd_set_svm_C();
283  bool cmd_set_svm_epsilon();
285  bool cmd_set_svr_tube_epsilon();
287  bool cmd_set_svm_nu();
289  bool cmd_set_svm_mkl_parameters();
291  bool cmd_set_elasticnet_lambda();
293  bool cmd_set_mkl_block_norm();
295  bool cmd_set_max_train_time();
297  bool cmd_set_svm_mkl_enabled();
299  bool cmd_set_svm_shrinking_enabled();
301  bool cmd_set_svm_batch_computation_enabled();
303  bool cmd_set_svm_linadd_enabled();
305  bool cmd_set_svm_bias_enabled();
307  bool cmd_set_mkl_interleaved_enabled();
309  bool cmd_set_krr_tau();
310 
312  bool cmd_add_preproc();
314  bool cmd_del_preproc();
316  bool cmd_attach_preproc();
318  bool cmd_clean_preproc();
319 
321  bool cmd_new_hmm();
323  bool cmd_load_hmm();
325  bool cmd_save_hmm();
327  bool cmd_hmm_classify();
329  bool cmd_hmm_classify_example();
331  bool cmd_one_class_linear_hmm_classify();
333  bool cmd_one_class_hmm_classify();
335  bool cmd_one_class_hmm_classify_example();
337  bool cmd_output_hmm();
339  bool cmd_output_hmm_defined();
341  bool cmd_hmm_likelihood();
343  bool cmd_likelihood();
345  bool cmd_save_likelihood();
347  bool cmd_get_viterbi_path();
349  bool cmd_viterbi_train_defined();
351  bool cmd_viterbi_train();
353  bool cmd_baum_welch_train();
355  bool cmd_baum_welch_train_defined();
357  bool cmd_baum_welch_trans_train();
359  bool cmd_linear_train();
361  bool cmd_save_path();
363  bool cmd_append_hmm();
365  bool cmd_append_model();
367  bool cmd_set_hmm();
369  bool cmd_set_hmm_as();
371  bool cmd_get_hmm();
373  bool cmd_set_chop();
375  bool cmd_set_pseudo();
377  bool cmd_load_definitions();
379  bool cmd_convergence_criteria();
381  bool cmd_normalize();
383  bool cmd_add_states();
385  bool cmd_permutation_entropy();
387  bool cmd_relative_entropy();
389  bool cmd_entropy();
391  bool cmd_new_plugin_estimator();
393  bool cmd_train_estimator();
395  bool cmd_plugin_estimate_classify_example();
397  bool cmd_plugin_estimate_classify();
399  bool cmd_set_plugin_estimate();
401  bool cmd_get_plugin_estimate();
403  bool cmd_best_path();
405  bool cmd_best_path_2struct();
411  bool cmd_set_plif_struct();
418  bool cmd_get_plif_struct();
422  bool cmd_precompute_subkernels();
426  bool cmd_signals_set_model() { return false; };
430  bool cmd_signals_set_positions();
434  bool cmd_signals_set_labels();
438  bool cmd_signals_set_split();
442  bool cmd_signals_set_train_mask();
446  bool cmd_signals_add_feature();
450  bool cmd_signals_add_kernel();
454  bool cmd_signals_run();
467  bool cmd_precompute_content_svms();
471  bool cmd_get_lin_feat();
475  bool cmd_set_lin_feat();
476 
480  bool cmd_init_dyn_prog();
481 
485  bool cmd_clean_up_dyn_prog();
486 
490  bool cmd_init_intron_list();
491 
493  bool cmd_long_transition_settings();
494 
503  bool cmd_precompute_tiling_features();
515  bool cmd_set_model();
520  bool cmd_set_feature_matrix_sparse();
525  bool cmd_set_feature_matrix();
527  bool cmd_best_path_trans();
529  bool cmd_best_path_trans_deriv();
531  bool cmd_best_path_no_b();
533  bool cmd_best_path_no_b_trans();
534 
536  bool cmd_crc();
538  bool cmd_system();
540  bool cmd_exit();
542  bool cmd_exec();
544  bool cmd_set_output();
546  bool cmd_set_threshold();
548  bool cmd_init_random();
550  bool cmd_set_num_threads();
552  bool cmd_translate_string();
554  bool cmd_clear();
556  bool cmd_tic();
558  bool cmd_toc();
560  bool cmd_echo();
562  bool cmd_print();
564  bool cmd_loglevel();
566  bool cmd_progress();
568  bool cmd_syntax_highlight();
570  bool cmd_get_version();
572  bool cmd_help();
574  bool cmd_whos();
576  bool cmd_send_command();
578  virtual bool cmd_run_python();
580  virtual bool cmd_run_octave();
582  virtual bool cmd_run_r();
584  virtual bool cmd_pr_loqo();
585 
588 
589  virtual IFType get_argument_type()=0;
590 
592  virtual int32_t get_int()=0;
594  virtual float64_t get_real()=0;
596  virtual bool get_bool()=0;
597 
601  virtual char* get_string(int32_t& len)=0;
602 
607  virtual void get_vector(bool*& vector, int32_t& len);
612  virtual void get_vector(uint8_t*& vector, int32_t& len)=0;
617  virtual void get_vector(char*& vector, int32_t& len)=0;
622  virtual void get_vector(int32_t*& vector, int32_t& len)=0;
627  virtual void get_vector(float64_t*& vector, int32_t& len)=0;
632  virtual void get_vector(float32_t*& vector, int32_t& len)=0;
637  virtual void get_vector(int16_t*& vector, int32_t& len)=0;
642  virtual void get_vector(uint16_t*& vector, int32_t& len)=0;
643 
649  virtual void get_matrix(
650  uint8_t*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
656  virtual void get_matrix(
657  char*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
663  virtual void get_matrix(
664  int32_t*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
670  virtual void get_matrix(
671  float32_t*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
677  virtual void get_matrix(
678  float64_t*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
684  virtual void get_matrix(
685  int16_t*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
691  virtual void get_matrix(
692  uint16_t*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
693 
699  virtual void get_ndarray(
700  uint8_t*& array, int32_t*& dims, int32_t& num_dims)=0;
706  virtual void get_ndarray(
707  char*& array, int32_t*& dims, int32_t& num_dims)=0;
713  virtual void get_ndarray(
714  int32_t*& array, int32_t*& dims, int32_t& num_dims)=0;
720  virtual void get_ndarray(
721  float32_t*& array, int32_t*& dims, int32_t& num_dims)=0;
727  virtual void get_ndarray(
728  float64_t*& array, int32_t*& dims, int32_t& num_dims)=0;
734  virtual void get_ndarray(
735  int16_t*& array, int32_t*& dims, int32_t& num_dims)=0;
741  virtual void get_ndarray(
742  uint16_t*& array, int32_t*& dims, int32_t& num_dims)=0;
743 
749  virtual void get_sparse_matrix(
750  SGSparseVector<float64_t>*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
751 
752  /* future versions might support types other than float64_t
753 
754  virtual void get_sparse_matrix(SGSparseVector<uint8_t>*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
755  virtual void get_sparse_matrix(SGSparseVector<char>*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
756  virtual void get_sparse_matrix(SGSparseVector<int32_t>*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
757  virtual void get_sparse_matrix(SGSparseVector<float32_t>*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
758  virtual void get_sparse_matrix(SGSparseVector<int16_t>*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
759  virtual void get_sparse_matrix(SGSparseVector<uint16_t>*& matrix, int32_t& num_feat, int32_t& num_vec)=0; */
760 
766  virtual void get_string_list(
767  SGString<uint8_t>*& strings, int32_t& num_str,
768  int32_t& max_string_len)=0;
774  virtual void get_string_list(
775  SGString<char>*& strings, int32_t& num_str,
776  int32_t& max_string_len)=0;
782  virtual void get_string_list(
783  SGString<int32_t>*& strings, int32_t& num_str,
784  int32_t& max_string_len)=0;
790  virtual void get_string_list(
791  SGString<int16_t>*& strings, int32_t& num_str,
792  int32_t& max_string_len)=0;
798  virtual void get_string_list(
799  SGString<uint16_t>*& strings, int32_t& num_str,
800  int32_t& max_string_len)=0;
801 
805  virtual void get_attribute_struct(
806  const CDynamicArray<T_ATTRIBUTE>* &attrs)=0;
807 
808  // set functions - to pass data from shogun to the target interface
812  virtual bool create_return_values(int32_t num_val)=0;
813 
817  virtual void set_int(int32_t scalar)=0;
821  virtual void set_real(float64_t scalar)=0;
825  virtual void set_bool(bool scalar)=0;
826 
831  virtual void set_vector(const bool* vector, int32_t len);
836  virtual void set_vector(const uint8_t* vector, int32_t len)=0;
841  virtual void set_vector(const char* vector, int32_t len)=0;
846  virtual void set_vector(const int32_t* vector, int32_t len)=0;
851  virtual void set_vector(const float32_t* vector, int32_t len)=0;
856  virtual void set_vector(const float64_t* vector, int32_t len)=0;
861  virtual void set_vector(const int16_t* vector, int32_t len)=0;
866  virtual void set_vector(const uint16_t* vector, int32_t len)=0;
867 
873  virtual void set_matrix(
874  const uint8_t* matrix, int32_t num_feat, int32_t num_vec)=0;
880  virtual void set_matrix(
881  const char* matrix, int32_t num_feat, int32_t num_vec)=0;
887  virtual void set_matrix(
888  const int32_t* matrix, int32_t num_feat, int32_t num_vec)=0;
894  virtual void set_matrix(
895  const float32_t* matrix, int32_t num_feat, int32_t num_vec)=0;
901  virtual void set_matrix(
902  const float64_t* matrix, int32_t num_feat, int32_t num_vec)=0;
908  virtual void set_matrix(
909  const int16_t* matrix, int32_t num_feat, int32_t num_vec)=0;
915  virtual void set_matrix(
916  const uint16_t* matrix, int32_t num_feat, int32_t num_vec)=0;
917 
924  virtual void set_sparse_matrix(
925  const SGSparseVector<float64_t>* matrix, int32_t num_feat,
926  int32_t num_vec, int64_t nnz)=0;
927 
928  /* future versions might support types other than float64_t
929 
930  virtual void set_sparse_matrix(const SGSparseVector<uint8_t>* matrix, int32_t num_feat, int32_t num_vec)=0;
931  virtual void set_sparse_matrix(const SGSparseVector<char>* matrix, int32_t num_feat, int32_t num_vec)=0;
932  virtual void set_sparse_matrix(const SGSparseVector<int32_t>* matrix, int32_t num_feat, int32_t num_vec)=0;
933  virtual void set_sparse_matrix(const SGSparseVector<float32_t>* matrix, int32_t num_feat, int32_t num_vec)=0;
934  virtual void set_sparse_matrix(const SGSparseVector<int16_t>* matrix, int32_t num_feat, int32_t num_vec)=0;
935  virtual void set_sparse_matrix(const SGSparseVector<uint16_t>* matrix, int32_t num_feat, int32_t num_vec)=0; */
936 
941  virtual void set_string_list(
942  const SGString<uint8_t>* strings, int32_t num_str)=0;
947  virtual void set_string_list(
948  const SGString<char>* strings, int32_t num_str)=0;
953  virtual void set_string_list(
954  const SGString<int32_t>* strings, int32_t num_str)=0;
959  virtual void set_string_list(
960  const SGString<int16_t>* strings, int32_t num_str)=0;
965  virtual void set_string_list(
966  const SGString<uint16_t>* strings, int32_t num_str)=0;
967 
971  virtual void set_attribute_struct(
972  const CDynamicArray<T_ATTRIBUTE>* attrs)=0;
973 
975  bool handle();
976 
978  void print_prompt();
979 
981  int32_t get_nlhs() { return m_nlhs; }
982 
984  int32_t get_nrhs() { return m_nrhs; }
985 
986 
987  // ui lib
989  CGUIClassifier* ui_classifier;
991  CGUIDistance* ui_distance;
993  CGUIFeatures* ui_features;
995  CGUIHMM* ui_hmm;
997  CGUIKernel* ui_kernel;
999  CGUILabels* ui_labels;
1001  CGUIMath* ui_math;
1003  CGUIPluginEstimate* ui_pluginestimate;
1005  CGUIPreprocessor* ui_preproc;
1007  CGUITime* ui_time;
1009  CGUIStructure* ui_structure;
1010  //CGUISignals* ui_signals;
1011 
1012  protected:
1020  static bool strmatch(const char* str, const char* cmd, int32_t len=-1)
1021  {
1022  if (len==-1)
1023  {
1024  len=strlen(cmd);
1025  if (strlen(str)!=(size_t) len) // match exact length
1026  return false;
1027  }
1028 
1029  return (strncmp(str, cmd, len)==0);
1030  }
1031 
1036  static bool strendswith(const char* str, const char* cmd)
1037  {
1038  size_t idx=strlen(str);
1039  size_t len=strlen(cmd);
1040 
1041  if (strlen(str) < len)
1042  return false;
1043 
1044  str=&str[idx-len];
1045 
1046  return (strncmp(str, cmd, len)==0);
1047  }
1049  char* get_command(int32_t &len)
1050  {
1051  ASSERT(m_rhs_counter==0);
1052  if (m_nrhs<=0)
1053  SG_SERROR("No input arguments supplied.\n");
1054 
1055  return get_string(len);
1056  }
1057  private:
1059  bool do_compute_objective(E_WHICH_OBJ obj);
1061  bool do_hmm_classify(bool linear=false, bool one_class=false);
1063  bool do_hmm_classify_example(bool one_class=false);
1065  bool do_set_features(bool add=false, bool check_dot=false, int32_t repetitions=1);
1066 
1068  void convert_to_bitembedding(CFeatures* &features, bool convert_to_word, bool convert_to_ulong);
1070  void obtain_from_single_string(CFeatures* features);
1072  bool obtain_from_position_list(CFeatures* features);
1074  bool obtain_by_sliding_window(CFeatures* features);
1076  CKernel* create_kernel();
1077 
1079  CFeatures* create_custom_string_features(CStringFeatures<uint8_t>* f);
1080 
1081  CFeatures* create_custom_real_features(CSimpleFeatures<float64_t>* orig_feat);
1083  char* get_str_from_str_or_direct(int32_t& len);
1084  int32_t get_int_from_int_or_str();
1085  float64_t get_real_from_real_or_str();
1086  bool get_bool_from_bool_or_str();
1087  void get_vector_from_int_vector_or_str(
1088  int32_t*& vector, int32_t& len);
1089  void get_vector_from_real_vector_or_str(
1090  float64_t*& vector, int32_t& len);
1091  int32_t get_vector_len_from_str(int32_t expected_len=0);
1092  char* get_str_from_str(int32_t& len);
1093  int32_t get_num_args_in_str();
1094 
1097  char* get_line(FILE* infile=stdin, bool show_prompt=true);
1098 
1099  protected:
1101  int32_t m_lhs_counter;
1103  int32_t m_rhs_counter;
1105  int32_t m_nlhs;
1107  int32_t m_nrhs;
1108 
1109  // related to cmd_exec and cmd_echo
1111  FILE* file_out;
1113  char input[10000];
1115  bool echo;
1116 
1118  char* m_legacy_strptr;
1119 };
1120 
1122 typedef bool (CSGInterface::*CSGInterfacePtr)();
1123 
1124 #ifndef DOXYGEN_SHOULD_SKIP_THIS
1125 
1126 typedef struct {
1128  const char* command;
1130  CSGInterfacePtr method;
1132  const char* usage_prefix;
1134  const char* usage_suffix;
1135 } CSGInterfaceMethod;
1136 }
1137 #endif
1138 
1139 #endif // __SGINTERFACE__H_

SHOGUN Machine Learning Toolbox - Documentation