SHOGUN  v3.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/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/GUISignals.h>
25 #include <shogun/ui/GUIConverter.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 #ifdef USE_SVMLIGHT
216 
217  bool cmd_resize_kernel_cache();
218 #endif //USE_SVMLIGHT
219 
220 
222  bool cmd_set_distance();
224  bool cmd_init_distance();
226  bool cmd_get_distance_matrix();
227 
229  bool cmd_get_SPEC_consensus();
231  bool cmd_get_SPEC_scoring();
233  bool cmd_get_WD_consensus();
235  bool cmd_compute_POIM_WD();
237  bool cmd_get_WD_scoring();
238 
240  bool cmd_new_classifier();
242  bool cmd_load_classifier();
244  bool cmd_save_classifier();
246  bool cmd_get_svm();
248  bool cmd_get_num_svms();
250  bool cmd_set_svm();
252  bool cmd_set_linear_classifier();
254  bool cmd_classify();
256  bool cmd_classify_example();
258  bool cmd_get_classifier();
260  bool cmd_get_svm_objective();
262  bool cmd_compute_svm_primal_objective();
264  bool cmd_compute_svm_dual_objective();
266  bool cmd_compute_mkl_dual_objective();
268  bool cmd_compute_relative_mkl_duality_gap();
270  bool cmd_compute_absolute_mkl_duality_gap();
272  bool cmd_train_classifier();
274  bool cmd_do_auc_maximization();
276  bool cmd_set_perceptron_parameters();
278  bool cmd_set_svm_qpsize();
280  bool cmd_set_svm_max_qpsize();
282  bool cmd_set_svm_bufsize();
284  bool cmd_set_svm_C();
286  bool cmd_set_svm_epsilon();
288  bool cmd_set_svr_tube_epsilon();
290  bool cmd_set_svm_nu();
292  bool cmd_set_svm_mkl_parameters();
294  bool cmd_set_elasticnet_lambda();
296  bool cmd_set_mkl_block_norm();
298  bool cmd_set_max_train_time();
300  bool cmd_set_svm_mkl_enabled();
302  bool cmd_set_svm_shrinking_enabled();
304  bool cmd_set_svm_batch_computation_enabled();
306  bool cmd_set_svm_linadd_enabled();
308  bool cmd_set_svm_bias_enabled();
310  bool cmd_set_mkl_interleaved_enabled();
312  bool cmd_set_krr_tau();
313 
315  bool cmd_add_preproc();
317  bool cmd_del_preproc();
319  bool cmd_attach_preproc();
321  bool cmd_clean_preproc();
322 
324  bool cmd_set_converter();
326  bool cmd_apply_converter();
328  bool cmd_embed();
329 
331  bool cmd_new_hmm();
333  bool cmd_load_hmm();
335  bool cmd_save_hmm();
337  bool cmd_hmm_classify();
339  bool cmd_hmm_classify_example();
341  bool cmd_one_class_linear_hmm_classify();
343  bool cmd_one_class_hmm_classify();
345  bool cmd_one_class_hmm_classify_example();
347  bool cmd_output_hmm();
349  bool cmd_output_hmm_defined();
351  bool cmd_hmm_likelihood();
353  bool cmd_likelihood();
355  bool cmd_save_likelihood();
357  bool cmd_get_viterbi_path();
359  bool cmd_viterbi_train_defined();
361  bool cmd_viterbi_train();
363  bool cmd_baum_welch_train();
365  bool cmd_baum_welch_train_defined();
367  bool cmd_baum_welch_trans_train();
369  bool cmd_linear_train();
371  bool cmd_save_path();
373  bool cmd_append_hmm();
375  bool cmd_append_model();
377  bool cmd_set_hmm();
379  bool cmd_set_hmm_as();
381  bool cmd_get_hmm();
383  bool cmd_set_chop();
385  bool cmd_set_pseudo();
387  bool cmd_load_definitions();
389  bool cmd_convergence_criteria();
391  bool cmd_normalize();
393  bool cmd_add_states();
395  bool cmd_permutation_entropy();
397  bool cmd_relative_entropy();
399  bool cmd_entropy();
401  bool cmd_new_plugin_estimator();
403  bool cmd_train_estimator();
405  bool cmd_plugin_estimate_classify_example();
407  bool cmd_plugin_estimate_classify();
409  bool cmd_set_plugin_estimate();
411  bool cmd_get_plugin_estimate();
413  bool cmd_best_path();
415  bool cmd_best_path_2struct();
421  bool cmd_set_plif_struct();
428  bool cmd_get_plif_struct();
432  bool cmd_precompute_subkernels();
436  bool cmd_signals_set_model() { return false; };
440  bool cmd_signals_set_positions();
444  bool cmd_signals_set_labels();
448  bool cmd_signals_set_split();
452  bool cmd_signals_set_train_mask();
456  bool cmd_signals_add_feature();
460  bool cmd_signals_add_kernel();
464  bool cmd_signals_run();
477  bool cmd_precompute_content_svms();
481  bool cmd_get_lin_feat();
485  bool cmd_set_lin_feat();
486 
490  bool cmd_init_dyn_prog();
491 
495  bool cmd_clean_up_dyn_prog();
496 
500  bool cmd_init_intron_list();
501 
503  bool cmd_long_transition_settings();
504 
513  bool cmd_precompute_tiling_features();
525  bool cmd_set_model();
530  bool cmd_set_feature_matrix_sparse();
535  bool cmd_set_feature_matrix();
537  bool cmd_best_path_trans();
539  bool cmd_best_path_trans_deriv();
541  bool cmd_best_path_no_b();
543  bool cmd_best_path_no_b_trans();
544 
546  bool cmd_crc();
548  bool cmd_system();
550  bool cmd_exit();
552  bool cmd_exec();
554  bool cmd_set_output();
556  bool cmd_set_threshold();
558  bool cmd_init_random();
560  bool cmd_set_num_threads();
562  bool cmd_translate_string();
564  bool cmd_clear();
566  bool cmd_tic();
568  bool cmd_toc();
570  bool cmd_echo();
572  bool cmd_print();
574  bool cmd_loglevel();
576  bool cmd_progress();
578  bool cmd_syntax_highlight();
580  bool cmd_get_version();
582  bool cmd_help();
584  bool cmd_whos();
586  bool cmd_send_command();
588  virtual bool cmd_run_python();
590  virtual bool cmd_run_octave();
592  virtual bool cmd_run_r();
594  virtual bool cmd_pr_loqo();
595 
598 
599  virtual IFType get_argument_type()=0;
600 
602  virtual int32_t get_int()=0;
604  virtual float64_t get_real()=0;
606  virtual bool get_bool()=0;
607 
611  virtual char* get_string(int32_t& len)=0;
612 
617  virtual void get_vector(bool*& vector, int32_t& len);
622  virtual void get_vector(uint8_t*& vector, int32_t& len)=0;
627  virtual void get_vector(char*& vector, int32_t& len)=0;
632  virtual void get_vector(int32_t*& vector, int32_t& len)=0;
637  virtual void get_vector(float64_t*& vector, int32_t& len)=0;
642  virtual void get_vector(float32_t*& vector, int32_t& len)=0;
647  virtual void get_vector(int16_t*& vector, int32_t& len)=0;
652  virtual void get_vector(uint16_t*& vector, int32_t& len)=0;
653 
659  virtual void get_matrix(
660  uint8_t*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
666  virtual void get_matrix(
667  char*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
673  virtual void get_matrix(
674  int32_t*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
680  virtual void get_matrix(
681  float32_t*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
687  virtual void get_matrix(
688  float64_t*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
694  virtual void get_matrix(
695  int16_t*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
701  virtual void get_matrix(
702  uint16_t*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
703 
709  virtual void get_ndarray(
710  uint8_t*& array, int32_t*& dims, int32_t& num_dims)=0;
716  virtual void get_ndarray(
717  char*& array, int32_t*& dims, int32_t& num_dims)=0;
723  virtual void get_ndarray(
724  int32_t*& array, int32_t*& dims, int32_t& num_dims)=0;
730  virtual void get_ndarray(
731  float32_t*& array, int32_t*& dims, int32_t& num_dims)=0;
737  virtual void get_ndarray(
738  float64_t*& array, int32_t*& dims, int32_t& num_dims)=0;
744  virtual void get_ndarray(
745  int16_t*& array, int32_t*& dims, int32_t& num_dims)=0;
751  virtual void get_ndarray(
752  uint16_t*& array, int32_t*& dims, int32_t& num_dims)=0;
753 
759  virtual void get_sparse_matrix(
760  SGSparseVector<float64_t>*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
761 
762  /* future versions might support types other than float64_t
763 
764  virtual void get_sparse_matrix(SGSparseVector<uint8_t>*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
765  virtual void get_sparse_matrix(SGSparseVector<char>*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
766  virtual void get_sparse_matrix(SGSparseVector<int32_t>*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
767  virtual void get_sparse_matrix(SGSparseVector<float32_t>*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
768  virtual void get_sparse_matrix(SGSparseVector<int16_t>*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
769  virtual void get_sparse_matrix(SGSparseVector<uint16_t>*& matrix, int32_t& num_feat, int32_t& num_vec)=0; */
770 
776  virtual void get_string_list(
777  SGString<uint8_t>*& strings, int32_t& num_str,
778  int32_t& max_string_len)=0;
784  virtual void get_string_list(
785  SGString<char>*& strings, int32_t& num_str,
786  int32_t& max_string_len)=0;
792  virtual void get_string_list(
793  SGString<int32_t>*& strings, int32_t& num_str,
794  int32_t& max_string_len)=0;
800  virtual void get_string_list(
801  SGString<int16_t>*& strings, int32_t& num_str,
802  int32_t& max_string_len)=0;
808  virtual void get_string_list(
809  SGString<uint16_t>*& strings, int32_t& num_str,
810  int32_t& max_string_len)=0;
811 
815  virtual void get_attribute_struct(
816  const CDynamicArray<T_ATTRIBUTE>* &attrs)=0;
817 
818  // set functions - to pass data from shogun to the target interface
822  virtual bool create_return_values(int32_t num_val)=0;
823 
827  virtual void set_int(int32_t scalar)=0;
831  virtual void set_real(float64_t scalar)=0;
835  virtual void set_bool(bool scalar)=0;
836 
841  virtual void set_vector(const bool* vector, int32_t len);
846  virtual void set_vector(const uint8_t* vector, int32_t len)=0;
851  virtual void set_vector(const char* vector, int32_t len)=0;
856  virtual void set_vector(const int32_t* vector, int32_t len)=0;
861  virtual void set_vector(const float32_t* vector, int32_t len)=0;
866  virtual void set_vector(const float64_t* vector, int32_t len)=0;
871  virtual void set_vector(const int16_t* vector, int32_t len)=0;
876  virtual void set_vector(const uint16_t* vector, int32_t len)=0;
877 
883  virtual void set_matrix(
884  const uint8_t* matrix, int32_t num_feat, int32_t num_vec)=0;
890  virtual void set_matrix(
891  const char* matrix, int32_t num_feat, int32_t num_vec)=0;
897  virtual void set_matrix(
898  const int32_t* matrix, int32_t num_feat, int32_t num_vec)=0;
904  virtual void set_matrix(
905  const float32_t* matrix, int32_t num_feat, int32_t num_vec)=0;
911  virtual void set_matrix(
912  const float64_t* matrix, int32_t num_feat, int32_t num_vec)=0;
918  virtual void set_matrix(
919  const int16_t* matrix, int32_t num_feat, int32_t num_vec)=0;
925  virtual void set_matrix(
926  const uint16_t* matrix, int32_t num_feat, int32_t num_vec)=0;
927 
934  virtual void set_sparse_matrix(
935  const SGSparseVector<float64_t>* matrix, int32_t num_feat,
936  int32_t num_vec, int64_t nnz)=0;
937 
938  /* future versions might support types other than float64_t
939 
940  virtual void set_sparse_matrix(const SGSparseVector<uint8_t>* matrix, int32_t num_feat, int32_t num_vec)=0;
941  virtual void set_sparse_matrix(const SGSparseVector<char>* matrix, int32_t num_feat, int32_t num_vec)=0;
942  virtual void set_sparse_matrix(const SGSparseVector<int32_t>* matrix, int32_t num_feat, int32_t num_vec)=0;
943  virtual void set_sparse_matrix(const SGSparseVector<float32_t>* matrix, int32_t num_feat, int32_t num_vec)=0;
944  virtual void set_sparse_matrix(const SGSparseVector<int16_t>* matrix, int32_t num_feat, int32_t num_vec)=0;
945  virtual void set_sparse_matrix(const SGSparseVector<uint16_t>* matrix, int32_t num_feat, int32_t num_vec)=0; */
946 
951  virtual void set_string_list(
952  const SGString<uint8_t>* strings, int32_t num_str)=0;
957  virtual void set_string_list(
958  const SGString<char>* strings, int32_t num_str)=0;
963  virtual void set_string_list(
964  const SGString<int32_t>* strings, int32_t num_str)=0;
969  virtual void set_string_list(
970  const SGString<int16_t>* strings, int32_t num_str)=0;
975  virtual void set_string_list(
976  const SGString<uint16_t>* strings, int32_t num_str)=0;
977 
981  virtual void set_attribute_struct(
982  const CDynamicArray<T_ATTRIBUTE>* attrs)=0;
983 
985  bool handle();
986 
988  void print_prompt();
989 
991  int32_t get_nlhs() { return m_nlhs; }
992 
994  int32_t get_nrhs() { return m_nrhs; }
995 
996 
997  // ui lib
999  CGUIClassifier* ui_classifier;
1001  CGUIDistance* ui_distance;
1003  CGUIFeatures* ui_features;
1005  CGUIHMM* ui_hmm;
1007  CGUIKernel* ui_kernel;
1009  CGUILabels* ui_labels;
1011  CGUIMath* ui_math;
1013  CGUIPluginEstimate* ui_pluginestimate;
1015  CGUIPreprocessor* ui_preproc;
1017  CGUITime* ui_time;
1019  CGUIStructure* ui_structure;
1020  //CGUISignals* ui_signals;
1022  CGUIConverter* ui_converter;
1023 
1024  protected:
1032  static bool strmatch(const char* str, const char* cmd, int32_t len=-1)
1033  {
1034  if (len==-1)
1035  {
1036  len=strlen(cmd);
1037  if (strlen(str)!=(size_t) len) // match exact length
1038  return false;
1039  }
1040 
1041  return (strncmp(str, cmd, len)==0);
1042  }
1043 
1048  static bool strendswith(const char* str, const char* cmd)
1049  {
1050  size_t idx=strlen(str);
1051  size_t len=strlen(cmd);
1052 
1053  if (strlen(str) < len)
1054  return false;
1055 
1056  str=&str[idx-len];
1057 
1058  return (strncmp(str, cmd, len)==0);
1059  }
1061  char* get_command(int32_t &len)
1062  {
1063  ASSERT(m_rhs_counter==0)
1064  if (m_nrhs<=0)
1065  SG_SERROR("No input arguments supplied.\n")
1066 
1067  return get_string(len);
1068  }
1069  private:
1071  bool do_compute_objective(E_WHICH_OBJ obj);
1073  bool do_hmm_classify(bool linear=false, bool one_class=false);
1075  bool do_hmm_classify_example(bool one_class=false);
1077  bool do_set_features(bool add=false, bool check_dot=false, int32_t repetitions=1);
1078 
1080  void convert_to_bitembedding(CFeatures* &features, bool convert_to_word, bool convert_to_ulong);
1082  void obtain_from_single_string(CFeatures* features);
1084  bool obtain_from_position_list(CFeatures* features);
1086  bool obtain_by_sliding_window(CFeatures* features);
1088  CKernel* create_kernel();
1089 
1091  CFeatures* create_custom_string_features(CStringFeatures<uint8_t>* f);
1092 
1093  CFeatures* create_custom_real_features(CDenseFeatures<float64_t>* orig_feat);
1095  char* get_str_from_str_or_direct(int32_t& len);
1096  int32_t get_int_from_int_or_str();
1097  float64_t get_real_from_real_or_str();
1098  bool get_bool_from_bool_or_str();
1099  void get_vector_from_int_vector_or_str(
1100  int32_t*& vector, int32_t& len);
1101  void get_vector_from_real_vector_or_str(
1102  float64_t*& vector, int32_t& len);
1103  int32_t get_vector_len_from_str(int32_t expected_len=0);
1104  char* get_str_from_str(int32_t& len);
1105  int32_t get_num_args_in_str();
1106 
1109  char* get_line(FILE* infile=stdin, bool show_prompt=true);
1110 
1111  protected:
1113  int32_t m_lhs_counter;
1115  int32_t m_rhs_counter;
1117  int32_t m_nlhs;
1119  int32_t m_nrhs;
1120 
1121  // related to cmd_exec and cmd_echo
1123  FILE* file_out;
1125  char input[10000];
1127  bool echo;
1128 
1130  char* m_legacy_strptr;
1131 };
1132 
1134 typedef bool (CSGInterface::*CSGInterfacePtr)();
1135 
1136 #ifndef DOXYGEN_SHOULD_SKIP_THIS
1137 
1138 typedef struct {
1140  const char* command;
1142  CSGInterfacePtr method;
1144  const char* usage_prefix;
1146  const char* usage_suffix;
1147 } CSGInterfaceMethod;
1148 }
1149 #endif
1150 
1151 #endif // __SGINTERFACE__H_

SHOGUN Machine Learning Toolbox - Documentation