SHOGUN  v1.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
SGInterface.h
Go to the documentation of this file.
00001 #ifndef __SGINTERFACE__H_
00002 #define __SGINTERFACE__H_
00003 
00004 #include <shogun/lib/memory.h>
00005 #include <shogun/lib/config.h>
00006 #include <shogun/lib/common.h>
00007 #include <shogun/base/SGObject.h>
00008 #include <shogun/features/Features.h>
00009 #include <shogun/features/StringFeatures.h>
00010 #include <shogun/features/SparseFeatures.h>
00011 #include <shogun/features/AttributeFeatures.h>
00012 #include <shogun/kernel/Kernel.h>
00013 
00014 #include <shogun/ui/GUIClassifier.h>
00015 #include <shogun/ui/GUIDistance.h>
00016 #include <shogun/ui/GUIFeatures.h>
00017 #include <shogun/ui/GUIHMM.h>
00018 #include <shogun/ui/GUIKernel.h>
00019 #include <shogun/ui/GUILabels.h>
00020 #include <shogun/ui/GUIMath.h>
00021 #include <shogun/ui/GUIPluginEstimate.h>
00022 #include <shogun/ui/GUIPreprocessor.h>
00023 #include <shogun/ui/GUITime.h>
00024 #include <shogun/ui/GUIStructure.h>
00025 #include <shogun/ui/GUISignals.h>
00026 
00027 namespace shogun
00028 {
00033 enum IFType
00034 {
00036     UNDEFINED,
00037 
00039     SCALAR_INT,
00040     SCALAR_REAL,
00041     SCALAR_BOOL,
00042     STANDARD_STRING,
00043 
00045     VECTOR_BOOL,
00046     VECTOR_BYTE,
00047     VECTOR_CHAR,
00048     VECTOR_INT,
00049     VECTOR_REAL,
00050     VECTOR_SHORTREAL,
00051     VECTOR_SHORT,
00052     VECTOR_WORD,
00053 
00055     DENSE_INT,
00056     DENSE_REAL,
00057     DENSE_SHORTREAL,
00058     DENSE_SHORT,
00059     DENSE_WORD,
00060 
00062     NDARRAY_BYTE,
00063     NDARRAY_CHAR,
00064     NDARRAY_INT,
00065     NDARRAY_REAL,
00066     NDARRAY_SHORTREAL,
00067     NDARRAY_SHORT,
00068     NDARRAY_WORD,
00069 
00071     SPARSE_BYTE,
00072     SPARSE_CHAR,
00073     SPARSE_INT,
00074     SPARSE_REAL,
00075     SPARSE_SHORT,
00076     SPARSE_SHORTREAL,
00077     SPARSE_WORD,
00078 
00080     STRING_BYTE,
00081     STRING_CHAR,
00082     STRING_INT,
00083     STRING_SHORT,
00084     STRING_WORD,
00085 
00087     ATTR_STRUCT
00088 };
00089 
00091 enum E_WHICH_OBJ
00092 {
00094     SVM_PRIMAL,
00096     SVM_DUAL,
00098     MKL_PRIMAL,
00100     MKL_DUAL,
00102     MKL_RELATIVE_DUALITY_GAP,
00104     MKL_ABSOLUTE_DUALITY_GAP
00105 };
00106 
00108 class CSGInterface : public CSGObject
00109 {
00110     public:
00114         CSGInterface(bool print_copyrights=true);
00115 
00117         ~CSGInterface();
00118 
00120         virtual void reset();
00121 
00123         void translate_arg(CSGInterface* source, CSGInterface* target);
00124 
00125         /* commands */
00127         bool cmd_load_features();
00129         bool cmd_save_features();
00131         bool cmd_clean_features();
00133         bool cmd_get_features();
00135         bool cmd_add_features();
00137         bool cmd_add_multiple_features();
00139         bool cmd_add_dotfeatures();
00141         bool cmd_set_features();
00143         bool cmd_set_reference_features();
00145         bool cmd_del_last_features();
00147         bool cmd_convert();
00149         bool cmd_reshape();
00151         bool cmd_load_labels();
00153         bool cmd_set_labels();
00155         bool cmd_get_labels();
00156 
00158         bool cmd_set_kernel_normalization();
00160         bool cmd_set_kernel();
00162         bool cmd_add_kernel();
00164         bool cmd_del_last_kernel();
00166         bool cmd_init_kernel();
00168         bool cmd_clean_kernel();
00170         bool cmd_save_kernel();
00172         bool cmd_load_kernel_init();
00174         bool cmd_save_kernel_init();
00176         bool cmd_get_kernel_matrix();
00178         bool cmd_set_WD_position_weights();
00180         bool cmd_get_subkernel_weights();
00182         bool cmd_set_subkernel_weights();
00184         bool cmd_set_subkernel_weights_combined();
00186         bool cmd_get_dotfeature_weights_combined();
00188         bool cmd_set_dotfeature_weights_combined();
00190         bool cmd_set_last_subkernel_weights();
00192         bool cmd_get_WD_position_weights();
00194         bool cmd_get_last_subkernel_weights();
00196         bool cmd_compute_by_subkernels();
00198         bool cmd_init_kernel_optimization();
00200         bool cmd_get_kernel_optimization();
00202         bool cmd_delete_kernel_optimization();
00204         bool cmd_use_diagonal_speedup();
00206         bool cmd_set_kernel_optimization_type();
00208         bool cmd_set_solver();
00210         bool cmd_set_constraint_generator();
00212         bool cmd_set_prior_probs();
00214         bool cmd_set_prior_probs_from_labels();
00215 
00216 
00217 
00219         bool cmd_set_distance();
00221         bool cmd_init_distance();
00223         bool cmd_get_distance_matrix();
00224 
00226         bool cmd_get_SPEC_consensus();
00228         bool cmd_get_SPEC_scoring();
00230         bool cmd_get_WD_consensus();
00232         bool cmd_compute_POIM_WD();
00234         bool cmd_get_WD_scoring();
00235 
00237         bool cmd_new_classifier();
00239         bool cmd_load_classifier();
00241         bool cmd_save_classifier();
00243         bool cmd_get_svm();
00245         bool cmd_get_num_svms();
00247         bool cmd_set_svm();
00249         bool cmd_set_linear_classifier();
00251         bool cmd_classify();
00253         bool cmd_classify_example();
00255         bool cmd_get_classifier();
00257         bool cmd_get_svm_objective();
00259         bool cmd_compute_svm_primal_objective();
00261         bool cmd_compute_svm_dual_objective();
00263         bool cmd_compute_mkl_dual_objective();
00265         bool cmd_compute_relative_mkl_duality_gap();
00267         bool cmd_compute_absolute_mkl_duality_gap();
00269         bool cmd_train_classifier();
00271         bool cmd_do_auc_maximization();
00273         bool cmd_set_perceptron_parameters();
00275         bool cmd_set_svm_qpsize();
00277         bool cmd_set_svm_max_qpsize();
00279         bool cmd_set_svm_bufsize();
00281         bool cmd_set_svm_C();
00283         bool cmd_set_svm_epsilon();
00285         bool cmd_set_svr_tube_epsilon();
00287         bool cmd_set_svm_nu();
00289         bool cmd_set_svm_mkl_parameters();
00291         bool cmd_set_elasticnet_lambda();
00293         bool cmd_set_mkl_block_norm();
00295         bool cmd_set_max_train_time();
00297         bool cmd_set_svm_mkl_enabled();
00299         bool cmd_set_svm_shrinking_enabled();
00301         bool cmd_set_svm_batch_computation_enabled();
00303         bool cmd_set_svm_linadd_enabled();
00305         bool cmd_set_svm_bias_enabled();
00307         bool cmd_set_mkl_interleaved_enabled();
00309         bool cmd_set_krr_tau();
00310 
00312         bool cmd_add_preproc();
00314         bool cmd_del_preproc();
00316         bool cmd_attach_preproc();
00318         bool cmd_clean_preproc();
00319 
00321         bool cmd_new_hmm();
00323         bool cmd_load_hmm();
00325         bool cmd_save_hmm();
00327         bool cmd_hmm_classify();
00329         bool cmd_hmm_classify_example();
00331         bool cmd_one_class_linear_hmm_classify();
00333         bool cmd_one_class_hmm_classify();
00335         bool cmd_one_class_hmm_classify_example();
00337         bool cmd_output_hmm();
00339         bool cmd_output_hmm_defined();
00341         bool cmd_hmm_likelihood();
00343         bool cmd_likelihood();
00345         bool cmd_save_likelihood();
00347         bool cmd_get_viterbi_path();
00349         bool cmd_viterbi_train_defined();
00351         bool cmd_viterbi_train();
00353         bool cmd_baum_welch_train();
00355         bool cmd_baum_welch_train_defined();
00357         bool cmd_baum_welch_trans_train();
00359         bool cmd_linear_train();
00361         bool cmd_save_path();
00363         bool cmd_append_hmm();
00365         bool cmd_append_model();
00367         bool cmd_set_hmm();
00369         bool cmd_set_hmm_as();
00371         bool cmd_get_hmm();
00373         bool cmd_set_chop();
00375         bool cmd_set_pseudo();
00377         bool cmd_load_definitions();
00379         bool cmd_convergence_criteria();
00381         bool cmd_normalize();
00383         bool cmd_add_states();
00385         bool cmd_permutation_entropy();
00387         bool cmd_relative_entropy();
00389         bool cmd_entropy();
00391         bool cmd_new_plugin_estimator();
00393         bool cmd_train_estimator();
00395         bool cmd_plugin_estimate_classify_example();
00397         bool cmd_plugin_estimate_classify();
00399         bool cmd_set_plugin_estimate();
00401         bool cmd_get_plugin_estimate();
00403         bool cmd_best_path();
00405         bool cmd_best_path_2struct();
00411         bool cmd_set_plif_struct();
00418         bool cmd_get_plif_struct();
00422         bool cmd_precompute_subkernels();
00426         bool cmd_signals_set_model() { return false; };
00430         bool cmd_signals_set_positions();
00434         bool cmd_signals_set_labels();
00438         bool cmd_signals_set_split();
00442         bool cmd_signals_set_train_mask();
00446         bool cmd_signals_add_feature();
00450         bool cmd_signals_add_kernel();
00454         bool cmd_signals_run();
00467         bool cmd_precompute_content_svms();
00471         bool cmd_get_lin_feat();
00475         bool cmd_set_lin_feat();
00476 
00480         bool cmd_init_dyn_prog();
00481 
00485         bool cmd_clean_up_dyn_prog();
00486 
00490         bool cmd_init_intron_list();
00491 
00493         bool cmd_long_transition_settings();
00494 
00503         bool cmd_precompute_tiling_features();
00515         bool cmd_set_model();
00520         bool cmd_set_feature_matrix_sparse();
00525         bool cmd_set_feature_matrix();
00527         bool cmd_best_path_trans();
00529         bool cmd_best_path_trans_deriv();
00531         bool cmd_best_path_no_b();
00533         bool cmd_best_path_no_b_trans();
00534 
00536         bool cmd_crc();
00538         bool cmd_system();
00540         bool cmd_exit();
00542         bool cmd_exec();
00544         bool cmd_set_output();
00546         bool cmd_set_threshold();
00548         bool cmd_init_random();
00550         bool cmd_set_num_threads();
00552         bool cmd_translate_string();
00554         bool cmd_clear();
00556         bool cmd_tic();
00558         bool cmd_toc();
00560         bool cmd_echo();
00562         bool cmd_print();
00564         bool cmd_loglevel();
00566         bool cmd_progress();
00568         bool cmd_syntax_highlight();
00570         bool cmd_get_version();
00572         bool cmd_help();
00574         bool cmd_whos();
00576         bool cmd_send_command();
00578         virtual bool cmd_run_python();
00580         virtual bool cmd_run_octave();
00582         virtual bool cmd_run_r();
00584         virtual bool cmd_pr_loqo();
00585 
00588 
00589         virtual IFType get_argument_type()=0;
00590 
00592         virtual int32_t get_int()=0;
00594         virtual float64_t get_real()=0;
00596         virtual bool get_bool()=0;
00597 
00601         virtual char* get_string(int32_t& len)=0;
00602 
00607         virtual void get_vector(bool*& vector, int32_t& len);
00612         virtual void get_vector(uint8_t*& vector, int32_t& len)=0;
00617         virtual void get_vector(char*& vector, int32_t& len)=0;
00622         virtual void get_vector(int32_t*& vector, int32_t& len)=0;
00627         virtual void get_vector(float64_t*& vector, int32_t& len)=0;
00632         virtual void get_vector(float32_t*& vector, int32_t& len)=0;
00637         virtual void get_vector(int16_t*& vector, int32_t& len)=0;
00642         virtual void get_vector(uint16_t*& vector, int32_t& len)=0;
00643 
00649         virtual void get_matrix(
00650             uint8_t*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
00656         virtual void get_matrix(
00657             char*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
00663         virtual void get_matrix(
00664             int32_t*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
00670         virtual void get_matrix(
00671             float32_t*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
00677         virtual void get_matrix(
00678             float64_t*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
00684         virtual void get_matrix(
00685             int16_t*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
00691         virtual void get_matrix(
00692             uint16_t*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
00693 
00699         virtual void get_ndarray(
00700             uint8_t*& array, int32_t*& dims, int32_t& num_dims)=0;
00706         virtual void get_ndarray(
00707             char*& array, int32_t*& dims, int32_t& num_dims)=0;
00713         virtual void get_ndarray(
00714             int32_t*& array, int32_t*& dims, int32_t& num_dims)=0;
00720         virtual void get_ndarray(
00721             float32_t*& array, int32_t*& dims, int32_t& num_dims)=0;
00727         virtual void get_ndarray(
00728             float64_t*& array, int32_t*& dims, int32_t& num_dims)=0;
00734         virtual void get_ndarray(
00735             int16_t*& array, int32_t*& dims, int32_t& num_dims)=0;
00741         virtual void get_ndarray(
00742             uint16_t*& array, int32_t*& dims, int32_t& num_dims)=0;
00743 
00749         virtual void get_sparse_matrix(
00750             SGSparseVector<float64_t>*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
00751 
00752         /*  future versions might support types other than float64_t
00753         
00754         virtual void get_sparse_matrix(SGSparseVector<uint8_t>*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
00755         virtual void get_sparse_matrix(SGSparseVector<char>*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
00756         virtual void get_sparse_matrix(SGSparseVector<int32_t>*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
00757         virtual void get_sparse_matrix(SGSparseVector<float32_t>*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
00758         virtual void get_sparse_matrix(SGSparseVector<int16_t>*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
00759         virtual void get_sparse_matrix(SGSparseVector<uint16_t>*& matrix, int32_t& num_feat, int32_t& num_vec)=0; */
00760 
00766         virtual void get_string_list(
00767             SGString<uint8_t>*& strings, int32_t& num_str,
00768             int32_t& max_string_len)=0;
00774         virtual void get_string_list(
00775             SGString<char>*& strings, int32_t& num_str,
00776             int32_t& max_string_len)=0;
00782         virtual void get_string_list(
00783             SGString<int32_t>*& strings, int32_t& num_str,
00784             int32_t& max_string_len)=0;
00790         virtual void get_string_list(
00791             SGString<int16_t>*& strings, int32_t& num_str,
00792             int32_t& max_string_len)=0;
00798         virtual void get_string_list(
00799             SGString<uint16_t>*& strings, int32_t& num_str,
00800             int32_t& max_string_len)=0;
00801 
00805         virtual void get_attribute_struct(
00806             const CDynamicArray<T_ATTRIBUTE>* &attrs)=0;
00807 
00808         // set functions - to pass data from shogun to the target interface
00812         virtual bool create_return_values(int32_t num_val)=0;
00813 
00817         virtual void set_int(int32_t scalar)=0;
00821         virtual void set_real(float64_t scalar)=0;
00825         virtual void set_bool(bool scalar)=0;
00826 
00831         virtual void set_vector(const bool* vector, int32_t len);
00836         virtual void set_vector(const uint8_t* vector, int32_t len)=0;
00841         virtual void set_vector(const char* vector, int32_t len)=0;
00846         virtual void set_vector(const int32_t* vector, int32_t len)=0;
00851         virtual void set_vector(const float32_t* vector, int32_t len)=0;
00856         virtual void set_vector(const float64_t* vector, int32_t len)=0;
00861         virtual void set_vector(const int16_t* vector, int32_t len)=0;
00866         virtual void set_vector(const uint16_t* vector, int32_t len)=0;
00867 
00873         virtual void set_matrix(
00874             const uint8_t* matrix, int32_t num_feat, int32_t num_vec)=0;
00880         virtual void set_matrix(
00881             const char* matrix, int32_t num_feat, int32_t num_vec)=0;
00887         virtual void set_matrix(
00888             const int32_t* matrix, int32_t num_feat, int32_t num_vec)=0;
00894         virtual void set_matrix(
00895             const float32_t* matrix, int32_t num_feat, int32_t num_vec)=0;
00901         virtual void set_matrix(
00902             const float64_t* matrix, int32_t num_feat, int32_t num_vec)=0;
00908         virtual void set_matrix(
00909             const int16_t* matrix, int32_t num_feat, int32_t num_vec)=0;
00915         virtual void set_matrix(
00916             const uint16_t* matrix, int32_t num_feat, int32_t num_vec)=0;
00917 
00924         virtual void set_sparse_matrix(
00925             const SGSparseVector<float64_t>* matrix, int32_t num_feat,
00926             int32_t num_vec, int64_t nnz)=0;
00927 
00928         /*  future versions might support types other than float64_t
00929         
00930         virtual void set_sparse_matrix(const SGSparseVector<uint8_t>* matrix, int32_t num_feat, int32_t num_vec)=0;
00931         virtual void set_sparse_matrix(const SGSparseVector<char>* matrix, int32_t num_feat, int32_t num_vec)=0;
00932         virtual void set_sparse_matrix(const SGSparseVector<int32_t>* matrix, int32_t num_feat, int32_t num_vec)=0;
00933         virtual void set_sparse_matrix(const SGSparseVector<float32_t>* matrix, int32_t num_feat, int32_t num_vec)=0;
00934         virtual void set_sparse_matrix(const SGSparseVector<int16_t>* matrix, int32_t num_feat, int32_t num_vec)=0;
00935         virtual void set_sparse_matrix(const SGSparseVector<uint16_t>* matrix, int32_t num_feat, int32_t num_vec)=0; */
00936 
00941         virtual void set_string_list(
00942             const SGString<uint8_t>* strings, int32_t num_str)=0;
00947         virtual void set_string_list(
00948             const SGString<char>* strings, int32_t num_str)=0;
00953         virtual void set_string_list(
00954             const SGString<int32_t>* strings, int32_t num_str)=0;
00959         virtual void set_string_list(
00960             const SGString<int16_t>* strings, int32_t num_str)=0;
00965         virtual void set_string_list(
00966             const SGString<uint16_t>* strings, int32_t num_str)=0;
00967 
00971         virtual void set_attribute_struct(
00972             const CDynamicArray<T_ATTRIBUTE>* attrs)=0;
00973 
00975         bool handle();
00976 
00978         void print_prompt();
00979 
00981         int32_t get_nlhs() { return m_nlhs; }
00982 
00984         int32_t get_nrhs() { return m_nrhs; }
00985 
00986 
00987         // ui lib 
00989         CGUIClassifier* ui_classifier;
00991         CGUIDistance* ui_distance;
00993         CGUIFeatures* ui_features;
00995         CGUIHMM* ui_hmm;
00997         CGUIKernel* ui_kernel;
00999         CGUILabels* ui_labels;
01001         CGUIMath* ui_math;
01003         CGUIPluginEstimate* ui_pluginestimate;
01005         CGUIPreprocessor* ui_preproc;
01007         CGUITime* ui_time;
01009         CGUIStructure* ui_structure;
01010         //CGUISignals* ui_signals;
01011 
01012     protected:
01020         static bool strmatch(const char* str, const char* cmd, int32_t len=-1)
01021         {
01022             if (len==-1)
01023             {
01024                 len=strlen(cmd);
01025                 if (strlen(str)!=(size_t) len) // match exact length
01026                     return false;
01027             }
01028 
01029             return (strncmp(str, cmd, len)==0);
01030         }
01031 
01036         static bool strendswith(const char* str, const char* cmd)
01037         {
01038             size_t idx=strlen(str);
01039             size_t len=strlen(cmd);
01040 
01041             if (strlen(str) < len)
01042                 return false;
01043 
01044             str=&str[idx-len];
01045 
01046             return (strncmp(str, cmd, len)==0);
01047         }
01049         char* get_command(int32_t &len)
01050         {
01051             ASSERT(m_rhs_counter==0);
01052             if (m_nrhs<=0)
01053                 SG_SERROR("No input arguments supplied.\n");
01054 
01055             return get_string(len);
01056         }
01057     private:
01059         bool do_compute_objective(E_WHICH_OBJ obj);
01061         bool do_hmm_classify(bool linear=false, bool one_class=false);
01063         bool do_hmm_classify_example(bool one_class=false);
01065         bool do_set_features(bool add=false, bool check_dot=false, int32_t repetitions=1);
01066 
01068         void convert_to_bitembedding(CFeatures* &features, bool convert_to_word, bool convert_to_ulong);
01070         void obtain_from_single_string(CFeatures* features);
01072         bool obtain_from_position_list(CFeatures* features);
01074         bool obtain_by_sliding_window(CFeatures* features);
01076         CKernel* create_kernel();
01077 
01079         CFeatures* create_custom_string_features(CStringFeatures<uint8_t>* f);
01080 
01081         CFeatures* create_custom_real_features(CSimpleFeatures<float64_t>* orig_feat);
01083         char* get_str_from_str_or_direct(int32_t& len);
01084         int32_t get_int_from_int_or_str();
01085         float64_t get_real_from_real_or_str();
01086         bool get_bool_from_bool_or_str();
01087         void get_vector_from_int_vector_or_str(
01088             int32_t*& vector, int32_t& len);
01089         void get_vector_from_real_vector_or_str(
01090             float64_t*& vector, int32_t& len);
01091         int32_t get_vector_len_from_str(int32_t expected_len=0);
01092         char* get_str_from_str(int32_t& len);
01093         int32_t get_num_args_in_str();
01094 
01097         char* get_line(FILE* infile=stdin, bool show_prompt=true);
01098 
01099     protected:
01101         int32_t m_lhs_counter;
01103         int32_t m_rhs_counter;
01105         int32_t m_nlhs;
01107         int32_t m_nrhs;
01108 
01109         // related to cmd_exec and cmd_echo
01111         FILE* file_out;
01113         char input[10000];
01115         bool echo;
01116 
01118         char* m_legacy_strptr;
01119 };
01120 
01122 typedef bool (CSGInterface::*CSGInterfacePtr)();
01123 
01124 #ifndef DOXYGEN_SHOULD_SKIP_THIS
01125 
01126 typedef struct {
01128     const char* command;
01130     CSGInterfacePtr method;
01132     const char* usage_prefix;
01134     const char* usage_suffix;
01135 } CSGInterfaceMethod;
01136 }
01137 #endif
01138 
01139 #endif // __SGINTERFACE__H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation