SHOGUN  v3.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
SGInterface.h
Go to the documentation of this file.
1 #ifndef __SGINTERFACE__H_
2 #define __SGINTERFACE__H_
3 
4 #include <shogun/lib/config.h>
5 #include <shogun/lib/common.h>
6 #include <shogun/base/SGObject.h>
11 #include <shogun/kernel/Kernel.h>
12 
14 #include <shogun/ui/GUIDistance.h>
15 #include <shogun/ui/GUIFeatures.h>
16 #include <shogun/ui/GUIHMM.h>
17 #include <shogun/ui/GUIKernel.h>
18 #include <shogun/ui/GUILabels.h>
19 #include <shogun/ui/GUIMath.h>
22 #include <shogun/ui/GUITime.h>
23 #include <shogun/ui/GUIStructure.h>
24 #include <shogun/ui/GUIConverter.h>
25 
26 namespace shogun
27 {
32 enum IFType
33 {
35  UNDEFINED,
36 
38  SCALAR_INT,
39  SCALAR_REAL,
40  SCALAR_BOOL,
41  STANDARD_STRING,
42 
44  VECTOR_BOOL,
45  VECTOR_BYTE,
46  VECTOR_CHAR,
47  VECTOR_INT,
48  VECTOR_REAL,
49  VECTOR_SHORTREAL,
50  VECTOR_SHORT,
51  VECTOR_WORD,
52 
54  DENSE_INT,
55  DENSE_REAL,
56  DENSE_SHORTREAL,
57  DENSE_SHORT,
58  DENSE_WORD,
59 
61  NDARRAY_BYTE,
62  NDARRAY_CHAR,
63  NDARRAY_INT,
64  NDARRAY_REAL,
65  NDARRAY_SHORTREAL,
66  NDARRAY_SHORT,
67  NDARRAY_WORD,
68 
70  SPARSE_BYTE,
71  SPARSE_CHAR,
72  SPARSE_INT,
73  SPARSE_REAL,
74  SPARSE_SHORT,
75  SPARSE_SHORTREAL,
76  SPARSE_WORD,
77 
79  STRING_BYTE,
80  STRING_CHAR,
81  STRING_INT,
82  STRING_SHORT,
83  STRING_WORD,
84 
86  ATTR_STRUCT
87 };
88 
90 enum E_WHICH_OBJ
91 {
93  SVM_PRIMAL,
95  SVM_DUAL,
97  MKL_PRIMAL,
99  MKL_DUAL,
101  MKL_RELATIVE_DUALITY_GAP,
103  MKL_ABSOLUTE_DUALITY_GAP
104 };
105 
107 class CSGInterface : public CSGObject
108 {
109  public:
113  CSGInterface(bool print_copyrights=true);
114 
116  ~CSGInterface();
117 
119  virtual void reset();
120 
122  void translate_arg(CSGInterface* source, CSGInterface* target);
123 
124  /* commands */
126  bool cmd_load_features();
128  bool cmd_save_features();
130  bool cmd_clean_features();
132  bool cmd_get_features();
134  bool cmd_add_features();
136  bool cmd_add_multiple_features();
138  bool cmd_add_dotfeatures();
140  bool cmd_set_features();
142  bool cmd_set_reference_features();
144  bool cmd_del_last_features();
146  bool cmd_convert();
148  bool cmd_reshape();
150  bool cmd_load_labels();
152  bool cmd_set_labels();
154  bool cmd_get_labels();
155 
157  bool cmd_set_kernel_normalization();
159  bool cmd_set_kernel();
161  bool cmd_add_kernel();
163  bool cmd_del_last_kernel();
165  bool cmd_init_kernel();
167  bool cmd_clean_kernel();
169  bool cmd_save_kernel();
171  bool cmd_load_kernel_init();
173  bool cmd_save_kernel_init();
175  bool cmd_get_kernel_matrix();
177  bool cmd_set_WD_position_weights();
179  bool cmd_get_subkernel_weights();
181  bool cmd_set_subkernel_weights();
183  bool cmd_set_subkernel_weights_combined();
185  bool cmd_get_dotfeature_weights_combined();
187  bool cmd_set_dotfeature_weights_combined();
189  bool cmd_set_last_subkernel_weights();
191  bool cmd_get_WD_position_weights();
193  bool cmd_get_last_subkernel_weights();
195  bool cmd_compute_by_subkernels();
197  bool cmd_init_kernel_optimization();
199  bool cmd_get_kernel_optimization();
201  bool cmd_delete_kernel_optimization();
203  bool cmd_use_diagonal_speedup();
205  bool cmd_set_kernel_optimization_type();
207  bool cmd_set_solver();
209  bool cmd_set_constraint_generator();
211  bool cmd_set_prior_probs();
213  bool cmd_set_prior_probs_from_labels();
214 
215 
216 
218  bool cmd_set_distance();
220  bool cmd_init_distance();
222  bool cmd_get_distance_matrix();
223 
225  bool cmd_get_SPEC_consensus();
227  bool cmd_get_SPEC_scoring();
229  bool cmd_get_WD_consensus();
231  bool cmd_compute_POIM_WD();
233  bool cmd_get_WD_scoring();
234 
236  bool cmd_new_classifier();
238  bool cmd_load_classifier();
240  bool cmd_save_classifier();
242  bool cmd_get_svm();
244  bool cmd_get_num_svms();
246  bool cmd_set_svm();
248  bool cmd_set_linear_classifier();
250  bool cmd_classify();
252  bool cmd_classify_example();
254  bool cmd_get_classifier();
256  bool cmd_get_svm_objective();
258  bool cmd_compute_svm_primal_objective();
260  bool cmd_compute_svm_dual_objective();
262  bool cmd_compute_mkl_dual_objective();
264  bool cmd_compute_relative_mkl_duality_gap();
266  bool cmd_compute_absolute_mkl_duality_gap();
268  bool cmd_train_classifier();
270  bool cmd_do_auc_maximization();
272  bool cmd_set_perceptron_parameters();
274  bool cmd_set_svm_qpsize();
276  bool cmd_set_svm_max_qpsize();
278  bool cmd_set_svm_bufsize();
280  bool cmd_set_svm_C();
282  bool cmd_set_svm_epsilon();
284  bool cmd_set_svr_tube_epsilon();
286  bool cmd_set_svm_nu();
288  bool cmd_set_svm_mkl_parameters();
290  bool cmd_set_elasticnet_lambda();
292  bool cmd_set_mkl_block_norm();
294  bool cmd_set_max_train_time();
296  bool cmd_set_svm_mkl_enabled();
298  bool cmd_set_svm_shrinking_enabled();
300  bool cmd_set_svm_batch_computation_enabled();
302  bool cmd_set_svm_linadd_enabled();
304  bool cmd_set_svm_bias_enabled();
306  bool cmd_set_mkl_interleaved_enabled();
308  bool cmd_set_krr_tau();
309 
311  bool cmd_add_preproc();
313  bool cmd_del_preproc();
315  bool cmd_attach_preproc();
317  bool cmd_clean_preproc();
318 
320  bool cmd_set_converter();
322  bool cmd_apply_converter();
324  bool cmd_embed();
325 
327  bool cmd_new_hmm();
329  bool cmd_load_hmm();
331  bool cmd_save_hmm();
333  bool cmd_hmm_classify();
335  bool cmd_hmm_classify_example();
337  bool cmd_one_class_linear_hmm_classify();
339  bool cmd_one_class_hmm_classify();
341  bool cmd_one_class_hmm_classify_example();
343  bool cmd_output_hmm();
345  bool cmd_output_hmm_defined();
347  bool cmd_hmm_likelihood();
349  bool cmd_likelihood();
351  bool cmd_save_likelihood();
353  bool cmd_get_viterbi_path();
355  bool cmd_viterbi_train_defined();
357  bool cmd_viterbi_train();
359  bool cmd_baum_welch_train();
361  bool cmd_baum_welch_train_defined();
363  bool cmd_baum_welch_trans_train();
365  bool cmd_linear_train();
367  bool cmd_save_path();
369  bool cmd_append_hmm();
371  bool cmd_append_model();
373  bool cmd_set_hmm();
375  bool cmd_set_hmm_as();
377  bool cmd_get_hmm();
379  bool cmd_set_chop();
381  bool cmd_set_pseudo();
383  bool cmd_load_definitions();
385  bool cmd_convergence_criteria();
387  bool cmd_normalize();
389  bool cmd_add_states();
391  bool cmd_permutation_entropy();
393  bool cmd_relative_entropy();
395  bool cmd_entropy();
397  bool cmd_new_plugin_estimator();
399  bool cmd_train_estimator();
401  bool cmd_plugin_estimate_classify_example();
403  bool cmd_plugin_estimate_classify();
405  bool cmd_set_plugin_estimate();
407  bool cmd_get_plugin_estimate();
409  bool cmd_best_path();
411  bool cmd_best_path_2struct();
417  bool cmd_set_plif_struct();
424  bool cmd_get_plif_struct();
428  bool cmd_precompute_subkernels();
432  bool cmd_signals_set_model() { return false; };
436  bool cmd_signals_set_positions();
440  bool cmd_signals_set_labels();
444  bool cmd_signals_set_split();
448  bool cmd_signals_set_train_mask();
452  bool cmd_signals_add_feature();
456  bool cmd_signals_add_kernel();
460  bool cmd_signals_run();
473  bool cmd_precompute_content_svms();
477  bool cmd_get_lin_feat();
481  bool cmd_set_lin_feat();
482 
486  bool cmd_init_dyn_prog();
487 
491  bool cmd_clean_up_dyn_prog();
492 
496  bool cmd_init_intron_list();
497 
499  bool cmd_long_transition_settings();
500 
509  bool cmd_precompute_tiling_features();
521  bool cmd_set_model();
526  bool cmd_set_feature_matrix_sparse();
531  bool cmd_set_feature_matrix();
533  bool cmd_best_path_trans();
535  bool cmd_best_path_trans_deriv();
537  bool cmd_best_path_no_b();
539  bool cmd_best_path_no_b_trans();
540 
542  bool cmd_crc();
544  bool cmd_system();
546  bool cmd_exit();
548  bool cmd_exec();
550  bool cmd_set_output();
552  bool cmd_set_threshold();
554  bool cmd_init_random();
556  bool cmd_set_num_threads();
558  bool cmd_translate_string();
560  bool cmd_clear();
562  bool cmd_tic();
564  bool cmd_toc();
566  bool cmd_echo();
568  bool cmd_print();
570  bool cmd_loglevel();
572  bool cmd_progress();
574  bool cmd_syntax_highlight();
576  bool cmd_get_version();
578  bool cmd_help();
580  bool cmd_whos();
582  bool cmd_send_command();
584  virtual bool cmd_run_python();
586  virtual bool cmd_run_octave();
588  virtual bool cmd_run_r();
590  virtual bool cmd_pr_loqo();
591 
594  virtual IFType get_argument_type()=0;
596 
598  virtual int32_t get_int()=0;
600  virtual float64_t get_real()=0;
602  virtual bool get_bool()=0;
603 
607  virtual char* get_string(int32_t& len)=0;
608 
613  virtual void get_vector(bool*& vector, int32_t& len);
618  virtual void get_vector(uint8_t*& vector, int32_t& len)=0;
623  virtual void get_vector(char*& vector, int32_t& len)=0;
628  virtual void get_vector(int32_t*& vector, int32_t& len)=0;
633  virtual void get_vector(float64_t*& vector, int32_t& len)=0;
638  virtual void get_vector(float32_t*& vector, int32_t& len)=0;
643  virtual void get_vector(int16_t*& vector, int32_t& len)=0;
648  virtual void get_vector(uint16_t*& vector, int32_t& len)=0;
649 
655  virtual void get_matrix(
656  uint8_t*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
662  virtual void get_matrix(
663  char*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
669  virtual void get_matrix(
670  int32_t*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
676  virtual void get_matrix(
677  float32_t*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
683  virtual void get_matrix(
684  float64_t*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
690  virtual void get_matrix(
691  int16_t*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
697  virtual void get_matrix(
698  uint16_t*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
699 
705  virtual void get_ndarray(
706  uint8_t*& array, int32_t*& dims, int32_t& num_dims)=0;
712  virtual void get_ndarray(
713  char*& array, int32_t*& dims, int32_t& num_dims)=0;
719  virtual void get_ndarray(
720  int32_t*& array, int32_t*& dims, int32_t& num_dims)=0;
726  virtual void get_ndarray(
727  float32_t*& array, int32_t*& dims, int32_t& num_dims)=0;
733  virtual void get_ndarray(
734  float64_t*& array, int32_t*& dims, int32_t& num_dims)=0;
740  virtual void get_ndarray(
741  int16_t*& array, int32_t*& dims, int32_t& num_dims)=0;
747  virtual void get_ndarray(
748  uint16_t*& array, int32_t*& dims, int32_t& num_dims)=0;
749 
755  virtual void get_sparse_matrix(
756  SGSparseVector<float64_t>*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
757 
758  /* future versions might support types other than float64_t
759 
760  virtual void get_sparse_matrix(SGSparseVector<uint8_t>*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
761  virtual void get_sparse_matrix(SGSparseVector<char>*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
762  virtual void get_sparse_matrix(SGSparseVector<int32_t>*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
763  virtual void get_sparse_matrix(SGSparseVector<float32_t>*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
764  virtual void get_sparse_matrix(SGSparseVector<int16_t>*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
765  virtual void get_sparse_matrix(SGSparseVector<uint16_t>*& matrix, int32_t& num_feat, int32_t& num_vec)=0; */
766 
772  virtual void get_string_list(
773  SGString<uint8_t>*& strings, int32_t& num_str,
774  int32_t& max_string_len)=0;
780  virtual void get_string_list(
781  SGString<char>*& strings, int32_t& num_str,
782  int32_t& max_string_len)=0;
788  virtual void get_string_list(
789  SGString<int32_t>*& strings, int32_t& num_str,
790  int32_t& max_string_len)=0;
796  virtual void get_string_list(
797  SGString<int16_t>*& strings, int32_t& num_str,
798  int32_t& max_string_len)=0;
804  virtual void get_string_list(
805  SGString<uint16_t>*& strings, int32_t& num_str,
806  int32_t& max_string_len)=0;
807 
811  virtual void get_attribute_struct(
812  const CDynamicArray<T_ATTRIBUTE>* &attrs)=0;
813 
814  // set functions - to pass data from shogun to the target interface
818  virtual bool create_return_values(int32_t num_val)=0;
819 
823  virtual void set_int(int32_t scalar)=0;
827  virtual void set_real(float64_t scalar)=0;
831  virtual void set_bool(bool scalar)=0;
832 
837  virtual void set_vector(const bool* vector, int32_t len);
842  virtual void set_vector(const uint8_t* vector, int32_t len)=0;
847  virtual void set_vector(const char* vector, int32_t len)=0;
852  virtual void set_vector(const int32_t* vector, int32_t len)=0;
857  virtual void set_vector(const float32_t* vector, int32_t len)=0;
862  virtual void set_vector(const float64_t* vector, int32_t len)=0;
867  virtual void set_vector(const int16_t* vector, int32_t len)=0;
872  virtual void set_vector(const uint16_t* vector, int32_t len)=0;
873 
879  virtual void set_matrix(
880  const uint8_t* matrix, int32_t num_feat, int32_t num_vec)=0;
886  virtual void set_matrix(
887  const char* matrix, int32_t num_feat, int32_t num_vec)=0;
893  virtual void set_matrix(
894  const int32_t* matrix, int32_t num_feat, int32_t num_vec)=0;
900  virtual void set_matrix(
901  const float32_t* matrix, int32_t num_feat, int32_t num_vec)=0;
907  virtual void set_matrix(
908  const float64_t* matrix, int32_t num_feat, int32_t num_vec)=0;
914  virtual void set_matrix(
915  const int16_t* matrix, int32_t num_feat, int32_t num_vec)=0;
921  virtual void set_matrix(
922  const uint16_t* matrix, int32_t num_feat, int32_t num_vec)=0;
923 
930  virtual void set_sparse_matrix(
931  const SGSparseVector<float64_t>* matrix, int32_t num_feat,
932  int32_t num_vec, int64_t nnz)=0;
933 
934  /* future versions might support types other than float64_t
935 
936  virtual void set_sparse_matrix(const SGSparseVector<uint8_t>* matrix, int32_t num_feat, int32_t num_vec)=0;
937  virtual void set_sparse_matrix(const SGSparseVector<char>* matrix, int32_t num_feat, int32_t num_vec)=0;
938  virtual void set_sparse_matrix(const SGSparseVector<int32_t>* matrix, int32_t num_feat, int32_t num_vec)=0;
939  virtual void set_sparse_matrix(const SGSparseVector<float32_t>* matrix, int32_t num_feat, int32_t num_vec)=0;
940  virtual void set_sparse_matrix(const SGSparseVector<int16_t>* matrix, int32_t num_feat, int32_t num_vec)=0;
941  virtual void set_sparse_matrix(const SGSparseVector<uint16_t>* matrix, int32_t num_feat, int32_t num_vec)=0; */
942 
947  virtual void set_string_list(
948  const SGString<uint8_t>* strings, int32_t num_str)=0;
953  virtual void set_string_list(
954  const SGString<char>* strings, int32_t num_str)=0;
959  virtual void set_string_list(
960  const SGString<int32_t>* strings, int32_t num_str)=0;
965  virtual void set_string_list(
966  const SGString<int16_t>* strings, int32_t num_str)=0;
971  virtual void set_string_list(
972  const SGString<uint16_t>* strings, int32_t num_str)=0;
973 
977  virtual void set_attribute_struct(
978  const CDynamicArray<T_ATTRIBUTE>* attrs)=0;
979 
981  bool handle();
982 
984  void print_prompt();
985 
987  int32_t get_nlhs() { return m_nlhs; }
988 
990  int32_t get_nrhs() { return m_nrhs; }
991 
992 
993  // ui lib
995  CGUIClassifier* ui_classifier;
997  CGUIDistance* ui_distance;
999  CGUIFeatures* ui_features;
1001  CGUIHMM* ui_hmm;
1003  CGUIKernel* ui_kernel;
1005  CGUILabels* ui_labels;
1007  CGUIMath* ui_math;
1009  CGUIPluginEstimate* ui_pluginestimate;
1011  CGUIPreprocessor* ui_preproc;
1013  CGUITime* ui_time;
1015  CGUIStructure* ui_structure;
1017  CGUIConverter* ui_converter;
1018 
1019  protected:
1027  static bool strmatch(const char* str, const char* cmd, int32_t len=-1)
1028  {
1029  if (len==-1)
1030  {
1031  len=strlen(cmd);
1032  if (strlen(str)!=(size_t) len) // match exact length
1033  return false;
1034  }
1035 
1036  return (strncmp(str, cmd, len)==0);
1037  }
1038 
1043  static bool strendswith(const char* str, const char* cmd)
1044  {
1045  size_t idx=strlen(str);
1046  size_t len=strlen(cmd);
1047 
1048  if (strlen(str) < len)
1049  return false;
1050 
1051  str=&str[idx-len];
1052 
1053  return (strncmp(str, cmd, len)==0);
1054  }
1056  char* get_command(int32_t &len)
1057  {
1058  ASSERT(m_rhs_counter==0)
1059  if (m_nrhs<=0)
1060  SG_SERROR("No input arguments supplied.\n")
1061 
1062  return get_string(len);
1063  }
1064  private:
1066  bool do_compute_objective(E_WHICH_OBJ obj);
1068  bool do_hmm_classify(bool linear=false, bool one_class=false);
1070  bool do_hmm_classify_example(bool one_class=false);
1072  bool do_set_features(bool add=false, bool check_dot=false, int32_t repetitions=1);
1073 
1075  void convert_to_bitembedding(CFeatures* &features, bool convert_to_word, bool convert_to_ulong);
1077  void obtain_from_single_string(CFeatures* features);
1079  bool obtain_from_position_list(CFeatures* features);
1081  bool obtain_by_sliding_window(CFeatures* features);
1083  CKernel* create_kernel();
1084 
1086  CFeatures* create_custom_string_features(CStringFeatures<uint8_t>* f);
1087 
1088  CFeatures* create_custom_real_features(CDenseFeatures<float64_t>* orig_feat);
1090  char* get_str_from_str_or_direct(int32_t& len);
1091  int32_t get_int_from_int_or_str();
1092  float64_t get_real_from_real_or_str();
1093  bool get_bool_from_bool_or_str();
1094  void get_vector_from_int_vector_or_str(
1095  int32_t*& vector, int32_t& len);
1096  void get_vector_from_real_vector_or_str(
1097  float64_t*& vector, int32_t& len);
1098  int32_t get_vector_len_from_str(int32_t expected_len=0);
1099  char* get_str_from_str(int32_t& len);
1100  int32_t get_num_args_in_str();
1101 
1104  char* get_line(FILE* infile=stdin, bool show_prompt=true);
1105 
1106  protected:
1108  int32_t m_lhs_counter;
1110  int32_t m_rhs_counter;
1112  int32_t m_nlhs;
1114  int32_t m_nrhs;
1115 
1116  // related to cmd_exec and cmd_echo
1118  FILE* file_out;
1120  char input[10000];
1122  bool echo;
1123 
1125  char* m_legacy_strptr;
1126 };
1127 
1129 typedef bool (CSGInterface::*CSGInterfacePtr)();
1130 
1131 #ifndef DOXYGEN_SHOULD_SKIP_THIS
1132 
1133 typedef struct {
1135  const char* command;
1137  CSGInterfacePtr method;
1139  const char* usage_prefix;
1141  const char* usage_suffix;
1142 } CSGInterfaceMethod;
1143 }
1144 #endif
1145 
1146 #endif // __SGINTERFACE__H_
#define UNDEFINED(function, type)
Definition: SGVector.cpp:1414
#define ASSERT(x)
Definition: SGIO.h:203
double float64_t
Definition: common.h:48
float float32_t
Definition: common.h:47
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:16
#define SG_SERROR(...)
Definition: SGIO.h:181

SHOGUN Machine Learning Toolbox - Documentation