SHOGUN  5.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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/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 #ifdef USE_SVMLIGHT
215 
216  bool cmd_resize_kernel_cache();
217 #endif //USE_SVMLIGHT
218 
219 
221  bool cmd_set_distance();
223  bool cmd_init_distance();
225  bool cmd_get_distance_matrix();
226 
228  bool cmd_get_SPEC_consensus();
230  bool cmd_get_SPEC_scoring();
232  bool cmd_get_WD_consensus();
234  bool cmd_compute_POIM_WD();
236  bool cmd_get_WD_scoring();
237 
239  bool cmd_new_classifier();
241  bool cmd_load_classifier();
243  bool cmd_save_classifier();
245  bool cmd_get_svm();
247  bool cmd_get_num_svms();
249  bool cmd_set_svm();
251  bool cmd_set_linear_classifier();
253  bool cmd_classify();
255  bool cmd_classify_example();
257  bool cmd_get_classifier();
259  bool cmd_get_svm_objective();
261  bool cmd_compute_svm_primal_objective();
263  bool cmd_compute_svm_dual_objective();
265  bool cmd_compute_mkl_dual_objective();
267  bool cmd_compute_relative_mkl_duality_gap();
269  bool cmd_compute_absolute_mkl_duality_gap();
271  bool cmd_train_classifier();
273  bool cmd_do_auc_maximization();
275  bool cmd_set_perceptron_parameters();
277  bool cmd_set_svm_qpsize();
279  bool cmd_set_svm_max_qpsize();
281  bool cmd_set_svm_bufsize();
283  bool cmd_set_svm_C();
285  bool cmd_set_svm_epsilon();
287  bool cmd_set_svr_tube_epsilon();
289  bool cmd_set_svm_nu();
291  bool cmd_set_svm_mkl_parameters();
293  bool cmd_set_elasticnet_lambda();
295  bool cmd_set_mkl_block_norm();
297  bool cmd_set_max_train_time();
299  bool cmd_set_svm_mkl_enabled();
301  bool cmd_set_svm_shrinking_enabled();
303  bool cmd_set_svm_batch_computation_enabled();
305  bool cmd_set_svm_linadd_enabled();
307  bool cmd_set_svm_bias_enabled();
309  bool cmd_set_mkl_interleaved_enabled();
311  bool cmd_set_krr_tau();
312 
314  bool cmd_add_preproc();
316  bool cmd_del_preproc();
318  bool cmd_attach_preproc();
320  bool cmd_clean_preproc();
321 
323  bool cmd_set_converter();
325  bool cmd_apply_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();
592 #ifdef USE_GPL_SHOGUN
593 
594  virtual bool cmd_pr_loqo();
595 #endif //USE_GPL_SHOGUN
596 
599  virtual IFType get_argument_type()=0;
601 
603  virtual int32_t get_int()=0;
605  virtual float64_t get_real()=0;
607  virtual bool get_bool()=0;
608 
612  virtual char* get_string(int32_t& len)=0;
613 
618  virtual void get_vector(bool*& vector, int32_t& len);
623  virtual void get_vector(uint8_t*& vector, int32_t& len)=0;
628  virtual void get_vector(char*& vector, int32_t& len)=0;
633  virtual void get_vector(int32_t*& vector, int32_t& len)=0;
638  virtual void get_vector(float64_t*& vector, int32_t& len)=0;
643  virtual void get_vector(float32_t*& vector, int32_t& len)=0;
648  virtual void get_vector(int16_t*& vector, int32_t& len)=0;
653  virtual void get_vector(uint16_t*& vector, int32_t& len)=0;
654 
660  virtual void get_matrix(
661  uint8_t*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
667  virtual void get_matrix(
668  char*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
674  virtual void get_matrix(
675  int32_t*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
681  virtual void get_matrix(
682  float32_t*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
688  virtual void get_matrix(
689  float64_t*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
695  virtual void get_matrix(
696  int16_t*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
702  virtual void get_matrix(
703  uint16_t*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
704 
710  virtual void get_ndarray(
711  uint8_t*& array, int32_t*& dims, int32_t& num_dims)=0;
717  virtual void get_ndarray(
718  char*& array, int32_t*& dims, int32_t& num_dims)=0;
724  virtual void get_ndarray(
725  int32_t*& array, int32_t*& dims, int32_t& num_dims)=0;
731  virtual void get_ndarray(
732  float32_t*& array, int32_t*& dims, int32_t& num_dims)=0;
738  virtual void get_ndarray(
739  float64_t*& array, int32_t*& dims, int32_t& num_dims)=0;
745  virtual void get_ndarray(
746  int16_t*& array, int32_t*& dims, int32_t& num_dims)=0;
752  virtual void get_ndarray(
753  uint16_t*& array, int32_t*& dims, int32_t& num_dims)=0;
754 
760  virtual void get_sparse_matrix(
761  SGSparseVector<float64_t>*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
762 
763  /* future versions might support types other than float64_t
764 
765  virtual void get_sparse_matrix(SGSparseVector<uint8_t>*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
766  virtual void get_sparse_matrix(SGSparseVector<char>*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
767  virtual void get_sparse_matrix(SGSparseVector<int32_t>*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
768  virtual void get_sparse_matrix(SGSparseVector<float32_t>*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
769  virtual void get_sparse_matrix(SGSparseVector<int16_t>*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
770  virtual void get_sparse_matrix(SGSparseVector<uint16_t>*& matrix, int32_t& num_feat, int32_t& num_vec)=0; */
771 
777  virtual void get_string_list(
778  SGString<uint8_t>*& strings, int32_t& num_str,
779  int32_t& max_string_len)=0;
785  virtual void get_string_list(
786  SGString<char>*& strings, int32_t& num_str,
787  int32_t& max_string_len)=0;
793  virtual void get_string_list(
794  SGString<int32_t>*& strings, int32_t& num_str,
795  int32_t& max_string_len)=0;
801  virtual void get_string_list(
802  SGString<int16_t>*& strings, int32_t& num_str,
803  int32_t& max_string_len)=0;
809  virtual void get_string_list(
810  SGString<uint16_t>*& strings, int32_t& num_str,
811  int32_t& max_string_len)=0;
812 
816  virtual void get_attribute_struct(
817  const CDynamicArray<T_ATTRIBUTE>* &attrs)=0;
818 
819  // set functions - to pass data from shogun to the target interface
823  virtual bool create_return_values(int32_t num_val)=0;
824 
828  virtual void set_int(int32_t scalar)=0;
832  virtual void set_real(float64_t scalar)=0;
836  virtual void set_bool(bool scalar)=0;
837 
842  virtual void set_vector(const bool* vector, int32_t len);
847  virtual void set_vector(const uint8_t* vector, int32_t len)=0;
852  virtual void set_vector(const char* vector, int32_t len)=0;
857  virtual void set_vector(const int32_t* vector, int32_t len)=0;
862  virtual void set_vector(const float32_t* vector, int32_t len)=0;
867  virtual void set_vector(const float64_t* vector, int32_t len)=0;
872  virtual void set_vector(const int16_t* vector, int32_t len)=0;
877  virtual void set_vector(const uint16_t* vector, int32_t len)=0;
878 
884  virtual void set_matrix(
885  const uint8_t* matrix, int32_t num_feat, int32_t num_vec)=0;
891  virtual void set_matrix(
892  const char* matrix, int32_t num_feat, int32_t num_vec)=0;
898  virtual void set_matrix(
899  const int32_t* matrix, int32_t num_feat, int32_t num_vec)=0;
905  virtual void set_matrix(
906  const float32_t* matrix, int32_t num_feat, int32_t num_vec)=0;
912  virtual void set_matrix(
913  const float64_t* matrix, int32_t num_feat, int32_t num_vec)=0;
919  virtual void set_matrix(
920  const int16_t* matrix, int32_t num_feat, int32_t num_vec)=0;
926  virtual void set_matrix(
927  const uint16_t* matrix, int32_t num_feat, int32_t num_vec)=0;
928 
935  virtual void set_sparse_matrix(
936  const SGSparseVector<float64_t>* matrix, int32_t num_feat,
937  int32_t num_vec, int64_t nnz)=0;
938 
939  /* future versions might support types other than float64_t
940 
941  virtual void set_sparse_matrix(const SGSparseVector<uint8_t>* matrix, int32_t num_feat, int32_t num_vec)=0;
942  virtual void set_sparse_matrix(const SGSparseVector<char>* matrix, int32_t num_feat, int32_t num_vec)=0;
943  virtual void set_sparse_matrix(const SGSparseVector<int32_t>* matrix, int32_t num_feat, int32_t num_vec)=0;
944  virtual void set_sparse_matrix(const SGSparseVector<float32_t>* matrix, int32_t num_feat, int32_t num_vec)=0;
945  virtual void set_sparse_matrix(const SGSparseVector<int16_t>* matrix, int32_t num_feat, int32_t num_vec)=0;
946  virtual void set_sparse_matrix(const SGSparseVector<uint16_t>* matrix, int32_t num_feat, int32_t num_vec)=0; */
947 
952  virtual void set_string_list(
953  const SGString<uint8_t>* strings, int32_t num_str)=0;
958  virtual void set_string_list(
959  const SGString<char>* strings, int32_t num_str)=0;
964  virtual void set_string_list(
965  const SGString<int32_t>* strings, int32_t num_str)=0;
970  virtual void set_string_list(
971  const SGString<int16_t>* strings, int32_t num_str)=0;
976  virtual void set_string_list(
977  const SGString<uint16_t>* strings, int32_t num_str)=0;
978 
982  virtual void set_attribute_struct(
983  const CDynamicArray<T_ATTRIBUTE>* attrs)=0;
984 
986  bool handle();
987 
989  void print_prompt();
990 
992  int32_t get_nlhs() { return m_nlhs; }
993 
995  int32_t get_nrhs() { return m_nrhs; }
996 
997 
998  // ui lib
1000  CGUIClassifier* ui_classifier;
1002  CGUIDistance* ui_distance;
1004  CGUIFeatures* ui_features;
1006  CGUIHMM* ui_hmm;
1008  CGUIKernel* ui_kernel;
1010  CGUILabels* ui_labels;
1012  CGUIMath* ui_math;
1014  CGUIPluginEstimate* ui_pluginestimate;
1016  CGUIPreprocessor* ui_preproc;
1018  CGUITime* ui_time;
1020  CGUIStructure* ui_structure;
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_
#define UNDEFINED(function, type)
Definition: SGVector.cpp:936
#define ASSERT(x)
Definition: SGIO.h:201
void add(Matrix A, Matrix B, Matrix C, typename Matrix::Scalar alpha=1.0, typename Matrix::Scalar beta=1.0)
Definition: Core.h:66
double float64_t
Definition: common.h:50
float float32_t
Definition: common.h:49
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
#define SG_SERROR(...)
Definition: SGIO.h:179

SHOGUN Machine Learning Toolbox - Documentation