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 #ifdef USE_SVMLIGHT
00216 
00217         bool cmd_resize_kernel_cache();
00218 #endif //USE_SVMLIGHT
00219 
00220 
00222         bool cmd_set_distance();
00224         bool cmd_init_distance();
00226         bool cmd_get_distance_matrix();
00227 
00229         bool cmd_get_SPEC_consensus();
00231         bool cmd_get_SPEC_scoring();
00233         bool cmd_get_WD_consensus();
00235         bool cmd_compute_POIM_WD();
00237         bool cmd_get_WD_scoring();
00238 
00240         bool cmd_new_classifier();
00242         bool cmd_load_classifier();
00244         bool cmd_save_classifier();
00246         bool cmd_get_svm();
00248         bool cmd_get_num_svms();
00250         bool cmd_set_svm();
00252         bool cmd_set_linear_classifier();
00254         bool cmd_classify();
00256         bool cmd_classify_example();
00258         bool cmd_get_classifier();
00260         bool cmd_get_svm_objective();
00262         bool cmd_compute_svm_primal_objective();
00264         bool cmd_compute_svm_dual_objective();
00266         bool cmd_compute_mkl_dual_objective();
00268         bool cmd_compute_relative_mkl_duality_gap();
00270         bool cmd_compute_absolute_mkl_duality_gap();
00272         bool cmd_train_classifier();
00274         bool cmd_do_auc_maximization();
00276         bool cmd_set_perceptron_parameters();
00278         bool cmd_set_svm_qpsize();
00280         bool cmd_set_svm_max_qpsize();
00282         bool cmd_set_svm_bufsize();
00284         bool cmd_set_svm_C();
00286         bool cmd_set_svm_epsilon();
00288         bool cmd_set_svr_tube_epsilon();
00290         bool cmd_set_svm_nu();
00292         bool cmd_set_svm_mkl_parameters();
00294         bool cmd_set_elasticnet_lambda();
00296         bool cmd_set_mkl_block_norm();
00298         bool cmd_set_max_train_time();
00300         bool cmd_set_svm_mkl_enabled();
00302         bool cmd_set_svm_shrinking_enabled();
00304         bool cmd_set_svm_batch_computation_enabled();
00306         bool cmd_set_svm_linadd_enabled();
00308         bool cmd_set_svm_bias_enabled();
00310         bool cmd_set_mkl_interleaved_enabled();
00312         bool cmd_set_krr_tau();
00313 
00315         bool cmd_add_preproc();
00317         bool cmd_del_preproc();
00319         bool cmd_attach_preproc();
00321         bool cmd_clean_preproc();
00322 
00324         bool cmd_new_hmm();
00326         bool cmd_load_hmm();
00328         bool cmd_save_hmm();
00330         bool cmd_hmm_classify();
00332         bool cmd_hmm_classify_example();
00334         bool cmd_one_class_linear_hmm_classify();
00336         bool cmd_one_class_hmm_classify();
00338         bool cmd_one_class_hmm_classify_example();
00340         bool cmd_output_hmm();
00342         bool cmd_output_hmm_defined();
00344         bool cmd_hmm_likelihood();
00346         bool cmd_likelihood();
00348         bool cmd_save_likelihood();
00350         bool cmd_get_viterbi_path();
00352         bool cmd_viterbi_train_defined();
00354         bool cmd_viterbi_train();
00356         bool cmd_baum_welch_train();
00358         bool cmd_baum_welch_train_defined();
00360         bool cmd_baum_welch_trans_train();
00362         bool cmd_linear_train();
00364         bool cmd_save_path();
00366         bool cmd_append_hmm();
00368         bool cmd_append_model();
00370         bool cmd_set_hmm();
00372         bool cmd_set_hmm_as();
00374         bool cmd_get_hmm();
00376         bool cmd_set_chop();
00378         bool cmd_set_pseudo();
00380         bool cmd_load_definitions();
00382         bool cmd_convergence_criteria();
00384         bool cmd_normalize();
00386         bool cmd_add_states();
00388         bool cmd_permutation_entropy();
00390         bool cmd_relative_entropy();
00392         bool cmd_entropy();
00394         bool cmd_new_plugin_estimator();
00396         bool cmd_train_estimator();
00398         bool cmd_plugin_estimate_classify_example();
00400         bool cmd_plugin_estimate_classify();
00402         bool cmd_set_plugin_estimate();
00404         bool cmd_get_plugin_estimate();
00406         bool cmd_best_path();
00408         bool cmd_best_path_2struct();
00414         bool cmd_set_plif_struct();
00421         bool cmd_get_plif_struct();
00425         bool cmd_precompute_subkernels();
00429         bool cmd_signals_set_model() { return false; };
00433         bool cmd_signals_set_positions();
00437         bool cmd_signals_set_labels();
00441         bool cmd_signals_set_split();
00445         bool cmd_signals_set_train_mask();
00449         bool cmd_signals_add_feature();
00453         bool cmd_signals_add_kernel();
00457         bool cmd_signals_run();
00470         bool cmd_precompute_content_svms();
00474         bool cmd_get_lin_feat();
00478         bool cmd_set_lin_feat();
00479 
00483         bool cmd_init_dyn_prog();
00484 
00488         bool cmd_clean_up_dyn_prog();
00489 
00493         bool cmd_init_intron_list();
00494 
00496         bool cmd_long_transition_settings();
00497 
00506         bool cmd_precompute_tiling_features();
00518         bool cmd_set_model();
00523         bool cmd_set_feature_matrix_sparse();
00528         bool cmd_set_feature_matrix();
00530         bool cmd_best_path_trans();
00532         bool cmd_best_path_trans_deriv();
00534         bool cmd_best_path_no_b();
00536         bool cmd_best_path_no_b_trans();
00537 
00539         bool cmd_crc();
00541         bool cmd_system();
00543         bool cmd_exit();
00545         bool cmd_exec();
00547         bool cmd_set_output();
00549         bool cmd_set_threshold();
00551         bool cmd_init_random();
00553         bool cmd_set_num_threads();
00555         bool cmd_translate_string();
00557         bool cmd_clear();
00559         bool cmd_tic();
00561         bool cmd_toc();
00563         bool cmd_echo();
00565         bool cmd_print();
00567         bool cmd_loglevel();
00569         bool cmd_progress();
00571         bool cmd_syntax_highlight();
00573         bool cmd_get_version();
00575         bool cmd_help();
00577         bool cmd_whos();
00579         bool cmd_send_command();
00581         virtual bool cmd_run_python();
00583         virtual bool cmd_run_octave();
00585         virtual bool cmd_run_r();
00587         virtual bool cmd_pr_loqo();
00588 
00591 
00592         virtual IFType get_argument_type()=0;
00593 
00595         virtual int32_t get_int()=0;
00597         virtual float64_t get_real()=0;
00599         virtual bool get_bool()=0;
00600 
00604         virtual char* get_string(int32_t& len)=0;
00605 
00610         virtual void get_vector(bool*& vector, int32_t& len);
00615         virtual void get_vector(uint8_t*& vector, int32_t& len)=0;
00620         virtual void get_vector(char*& vector, int32_t& len)=0;
00625         virtual void get_vector(int32_t*& vector, int32_t& len)=0;
00630         virtual void get_vector(float64_t*& vector, int32_t& len)=0;
00635         virtual void get_vector(float32_t*& vector, int32_t& len)=0;
00640         virtual void get_vector(int16_t*& vector, int32_t& len)=0;
00645         virtual void get_vector(uint16_t*& vector, int32_t& len)=0;
00646 
00652         virtual void get_matrix(
00653             uint8_t*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
00659         virtual void get_matrix(
00660             char*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
00666         virtual void get_matrix(
00667             int32_t*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
00673         virtual void get_matrix(
00674             float32_t*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
00680         virtual void get_matrix(
00681             float64_t*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
00687         virtual void get_matrix(
00688             int16_t*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
00694         virtual void get_matrix(
00695             uint16_t*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
00696 
00702         virtual void get_ndarray(
00703             uint8_t*& array, int32_t*& dims, int32_t& num_dims)=0;
00709         virtual void get_ndarray(
00710             char*& array, int32_t*& dims, int32_t& num_dims)=0;
00716         virtual void get_ndarray(
00717             int32_t*& array, int32_t*& dims, int32_t& num_dims)=0;
00723         virtual void get_ndarray(
00724             float32_t*& array, int32_t*& dims, int32_t& num_dims)=0;
00730         virtual void get_ndarray(
00731             float64_t*& array, int32_t*& dims, int32_t& num_dims)=0;
00737         virtual void get_ndarray(
00738             int16_t*& array, int32_t*& dims, int32_t& num_dims)=0;
00744         virtual void get_ndarray(
00745             uint16_t*& array, int32_t*& dims, int32_t& num_dims)=0;
00746 
00752         virtual void get_sparse_matrix(
00753             SGSparseVector<float64_t>*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
00754 
00755         /*  future versions might support types other than float64_t
00756         
00757         virtual void get_sparse_matrix(SGSparseVector<uint8_t>*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
00758         virtual void get_sparse_matrix(SGSparseVector<char>*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
00759         virtual void get_sparse_matrix(SGSparseVector<int32_t>*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
00760         virtual void get_sparse_matrix(SGSparseVector<float32_t>*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
00761         virtual void get_sparse_matrix(SGSparseVector<int16_t>*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
00762         virtual void get_sparse_matrix(SGSparseVector<uint16_t>*& matrix, int32_t& num_feat, int32_t& num_vec)=0; */
00763 
00769         virtual void get_string_list(
00770             SGString<uint8_t>*& strings, int32_t& num_str,
00771             int32_t& max_string_len)=0;
00777         virtual void get_string_list(
00778             SGString<char>*& strings, int32_t& num_str,
00779             int32_t& max_string_len)=0;
00785         virtual void get_string_list(
00786             SGString<int32_t>*& strings, int32_t& num_str,
00787             int32_t& max_string_len)=0;
00793         virtual void get_string_list(
00794             SGString<int16_t>*& strings, int32_t& num_str,
00795             int32_t& max_string_len)=0;
00801         virtual void get_string_list(
00802             SGString<uint16_t>*& strings, int32_t& num_str,
00803             int32_t& max_string_len)=0;
00804 
00808         virtual void get_attribute_struct(
00809             const CDynamicArray<T_ATTRIBUTE>* &attrs)=0;
00810 
00811         // set functions - to pass data from shogun to the target interface
00815         virtual bool create_return_values(int32_t num_val)=0;
00816 
00820         virtual void set_int(int32_t scalar)=0;
00824         virtual void set_real(float64_t scalar)=0;
00828         virtual void set_bool(bool scalar)=0;
00829 
00834         virtual void set_vector(const bool* vector, int32_t len);
00839         virtual void set_vector(const uint8_t* vector, int32_t len)=0;
00844         virtual void set_vector(const char* vector, int32_t len)=0;
00849         virtual void set_vector(const int32_t* vector, int32_t len)=0;
00854         virtual void set_vector(const float32_t* vector, int32_t len)=0;
00859         virtual void set_vector(const float64_t* vector, int32_t len)=0;
00864         virtual void set_vector(const int16_t* vector, int32_t len)=0;
00869         virtual void set_vector(const uint16_t* vector, int32_t len)=0;
00870 
00876         virtual void set_matrix(
00877             const uint8_t* matrix, int32_t num_feat, int32_t num_vec)=0;
00883         virtual void set_matrix(
00884             const char* matrix, int32_t num_feat, int32_t num_vec)=0;
00890         virtual void set_matrix(
00891             const int32_t* matrix, int32_t num_feat, int32_t num_vec)=0;
00897         virtual void set_matrix(
00898             const float32_t* matrix, int32_t num_feat, int32_t num_vec)=0;
00904         virtual void set_matrix(
00905             const float64_t* matrix, int32_t num_feat, int32_t num_vec)=0;
00911         virtual void set_matrix(
00912             const int16_t* matrix, int32_t num_feat, int32_t num_vec)=0;
00918         virtual void set_matrix(
00919             const uint16_t* matrix, int32_t num_feat, int32_t num_vec)=0;
00920 
00927         virtual void set_sparse_matrix(
00928             const SGSparseVector<float64_t>* matrix, int32_t num_feat,
00929             int32_t num_vec, int64_t nnz)=0;
00930 
00931         /*  future versions might support types other than float64_t
00932         
00933         virtual void set_sparse_matrix(const SGSparseVector<uint8_t>* matrix, int32_t num_feat, int32_t num_vec)=0;
00934         virtual void set_sparse_matrix(const SGSparseVector<char>* matrix, int32_t num_feat, int32_t num_vec)=0;
00935         virtual void set_sparse_matrix(const SGSparseVector<int32_t>* matrix, int32_t num_feat, int32_t num_vec)=0;
00936         virtual void set_sparse_matrix(const SGSparseVector<float32_t>* matrix, int32_t num_feat, int32_t num_vec)=0;
00937         virtual void set_sparse_matrix(const SGSparseVector<int16_t>* matrix, int32_t num_feat, int32_t num_vec)=0;
00938         virtual void set_sparse_matrix(const SGSparseVector<uint16_t>* matrix, int32_t num_feat, int32_t num_vec)=0; */
00939 
00944         virtual void set_string_list(
00945             const SGString<uint8_t>* strings, int32_t num_str)=0;
00950         virtual void set_string_list(
00951             const SGString<char>* strings, int32_t num_str)=0;
00956         virtual void set_string_list(
00957             const SGString<int32_t>* strings, int32_t num_str)=0;
00962         virtual void set_string_list(
00963             const SGString<int16_t>* strings, int32_t num_str)=0;
00968         virtual void set_string_list(
00969             const SGString<uint16_t>* strings, int32_t num_str)=0;
00970 
00974         virtual void set_attribute_struct(
00975             const CDynamicArray<T_ATTRIBUTE>* attrs)=0;
00976 
00978         bool handle();
00979 
00981         void print_prompt();
00982 
00984         int32_t get_nlhs() { return m_nlhs; }
00985 
00987         int32_t get_nrhs() { return m_nrhs; }
00988 
00989 
00990         // ui lib 
00992         CGUIClassifier* ui_classifier;
00994         CGUIDistance* ui_distance;
00996         CGUIFeatures* ui_features;
00998         CGUIHMM* ui_hmm;
01000         CGUIKernel* ui_kernel;
01002         CGUILabels* ui_labels;
01004         CGUIMath* ui_math;
01006         CGUIPluginEstimate* ui_pluginestimate;
01008         CGUIPreprocessor* ui_preproc;
01010         CGUITime* ui_time;
01012         CGUIStructure* ui_structure;
01013         //CGUISignals* ui_signals;
01014 
01015     protected:
01023         static bool strmatch(const char* str, const char* cmd, int32_t len=-1)
01024         {
01025             if (len==-1)
01026             {
01027                 len=strlen(cmd);
01028                 if (strlen(str)!=(size_t) len) // match exact length
01029                     return false;
01030             }
01031 
01032             return (strncmp(str, cmd, len)==0);
01033         }
01034 
01039         static bool strendswith(const char* str, const char* cmd)
01040         {
01041             size_t idx=strlen(str);
01042             size_t len=strlen(cmd);
01043 
01044             if (strlen(str) < len)
01045                 return false;
01046 
01047             str=&str[idx-len];
01048 
01049             return (strncmp(str, cmd, len)==0);
01050         }
01052         char* get_command(int32_t &len)
01053         {
01054             ASSERT(m_rhs_counter==0);
01055             if (m_nrhs<=0)
01056                 SG_SERROR("No input arguments supplied.\n");
01057 
01058             return get_string(len);
01059         }
01060     private:
01062         bool do_compute_objective(E_WHICH_OBJ obj);
01064         bool do_hmm_classify(bool linear=false, bool one_class=false);
01066         bool do_hmm_classify_example(bool one_class=false);
01068         bool do_set_features(bool add=false, bool check_dot=false, int32_t repetitions=1);
01069 
01071         void convert_to_bitembedding(CFeatures* &features, bool convert_to_word, bool convert_to_ulong);
01073         void obtain_from_single_string(CFeatures* features);
01075         bool obtain_from_position_list(CFeatures* features);
01077         bool obtain_by_sliding_window(CFeatures* features);
01079         CKernel* create_kernel();
01080 
01082         CFeatures* create_custom_string_features(CStringFeatures<uint8_t>* f);
01083 
01084         CFeatures* create_custom_real_features(CSimpleFeatures<float64_t>* orig_feat);
01086         char* get_str_from_str_or_direct(int32_t& len);
01087         int32_t get_int_from_int_or_str();
01088         float64_t get_real_from_real_or_str();
01089         bool get_bool_from_bool_or_str();
01090         void get_vector_from_int_vector_or_str(
01091             int32_t*& vector, int32_t& len);
01092         void get_vector_from_real_vector_or_str(
01093             float64_t*& vector, int32_t& len);
01094         int32_t get_vector_len_from_str(int32_t expected_len=0);
01095         char* get_str_from_str(int32_t& len);
01096         int32_t get_num_args_in_str();
01097 
01100         char* get_line(FILE* infile=stdin, bool show_prompt=true);
01101 
01102     protected:
01104         int32_t m_lhs_counter;
01106         int32_t m_rhs_counter;
01108         int32_t m_nlhs;
01110         int32_t m_nrhs;
01111 
01112         // related to cmd_exec and cmd_echo
01114         FILE* file_out;
01116         char input[10000];
01118         bool echo;
01119 
01121         char* m_legacy_strptr;
01122 };
01123 
01125 typedef bool (CSGInterface::*CSGInterfacePtr)();
01126 
01127 #ifndef DOXYGEN_SHOULD_SKIP_THIS
01128 
01129 typedef struct {
01131     const char* command;
01133     CSGInterfacePtr method;
01135     const char* usage_prefix;
01137     const char* usage_suffix;
01138 } CSGInterfaceMethod;
01139 }
01140 #endif
01141 
01142 #endif // __SGINTERFACE__H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation