GUIDistance.cpp

Go to the documentation of this file.
00001 /*
00002  * This program is free software; you can redistribute it and/or modify
00003  * it under the terms of the GNU General Public License as published by
00004  * the Free Software Foundation; either version 3 of the License, or
00005  * (at your option) any later version.
00006  *
00007  * Written (W) 2006-2008 Christian Gehl
00008  * Written (W) 1999-2008 Soeren Sonnenburg
00009  * Copyright (C) 1999-2008 Fraunhofer Institute FIRST and Max-Planck-Society
00010  */
00011 
00012 #include <shogun/ui/GUIDistance.h>
00013 #include <shogun/ui/SGInterface.h>
00014 
00015 #include <string.h>
00016 
00017 #include <shogun/lib/config.h>
00018 #include <shogun/io/SGIO.h>
00019 #include <shogun/io/AsciiFile.h>
00020 
00021 #include <shogun/distance/Distance.h>
00022 #include <shogun/distance/SimpleDistance.h>
00023 #include <shogun/distance/CanberraMetric.h>
00024 #include <shogun/distance/ChebyshewMetric.h>
00025 #include <shogun/distance/GeodesicMetric.h>
00026 #include <shogun/distance/JensenMetric.h>
00027 #include <shogun/distance/ManhattanMetric.h>
00028 #include <shogun/distance/MinkowskiMetric.h>
00029 #include <shogun/distance/CanberraWordDistance.h>
00030 #include <shogun/distance/ManhattanWordDistance.h>
00031 #include <shogun/distance/HammingWordDistance.h>
00032 #include <shogun/distance/EuclidianDistance.h>
00033 #include <shogun/distance/SparseEuclidianDistance.h>
00034 #include <shogun/distance/TanimotoDistance.h>
00035 #include <shogun/distance/ChiSquareDistance.h>
00036 #include <shogun/distance/CosineDistance.h>
00037 #include <shogun/distance/BrayCurtisDistance.h>
00038 
00039 #include <shogun/features/RealFileFeatures.h>
00040 #include <shogun/features/TOPFeatures.h>
00041 #include <shogun/features/FKFeatures.h>
00042 #include <shogun/features/StringFeatures.h>
00043 #include <shogun/features/SimpleFeatures.h>
00044 #include <shogun/features/Features.h>
00045 
00046 using namespace shogun;
00047 
00048 CGUIDistance::CGUIDistance(CSGInterface* ui_)
00049 : CSGObject(), ui(ui_)
00050 {
00051     distance=NULL;
00052     initialized=false;
00053 }
00054 
00055 CGUIDistance::~CGUIDistance()
00056 {
00057     SG_UNREF(distance);
00058 }
00059 
00060 CDistance* CGUIDistance::get_distance()
00061 {
00062     return distance;
00063 }
00064 
00065 bool CGUIDistance::set_distance(CDistance* dist)
00066 {
00067     if (dist)
00068     {
00069         SG_UNREF(distance);
00070         SG_REF(dist);
00071         distance=dist;
00072         SG_DEBUG("set new distance (%p).\n", dist);
00073 
00074         return true;
00075     }
00076     else
00077         return false;
00078 }
00079 
00080 bool CGUIDistance::init_distance(const char* target)
00081 {
00082     SG_DEBUG("init_distance start\n.");
00083 
00084     if (!distance)
00085         SG_ERROR("No distance available.\n");
00086 
00087     distance->set_precompute_matrix(false);
00088     EFeatureClass d_fclass=distance->get_feature_class();
00089     EFeatureType d_ftype=distance->get_feature_type();
00090 
00091     if (!strncmp(target, "TRAIN", 5))
00092     {
00093         CFeatures* train=ui->ui_features->get_train_features();
00094         if (train)
00095         {
00096             EFeatureClass fclass=train->get_feature_class();
00097             EFeatureType ftype=train->get_feature_type();
00098             if ((d_fclass==fclass || d_fclass==C_ANY || fclass==C_ANY) &&
00099                 (d_ftype==ftype || d_ftype==F_ANY || ftype==F_ANY))
00100             
00101             {
00102                 distance->init(train, train);
00103                 initialized=true;
00104             }
00105             else
00106                 SG_ERROR("Distance can not process this train feature type: %d %d.\n", fclass, ftype);
00107         }
00108         else
00109             SG_ERROR("Assign train features first.\n");
00110     }
00111     else if (!strncmp(target, "TEST", 4))
00112     {
00113         CFeatures* train=ui->ui_features->get_train_features();
00114         CFeatures* test=ui->ui_features->get_test_features();
00115         if (test)
00116         {
00117             EFeatureClass fclass=test->get_feature_class();
00118             EFeatureType ftype=test->get_feature_type();
00119             if ((d_fclass==fclass || d_fclass==C_ANY || fclass==C_ANY) &&
00120                 (d_ftype==ftype || d_ftype==F_ANY || ftype==F_ANY))
00121             
00122             {
00123                 if (!initialized)
00124                     SG_ERROR("Distance not initialized with training examples.\n");
00125                 else
00126                 {
00127                     SG_INFO("Initialising distance with TEST DATA, train: %p test %p\n", train, test);
00128                     // lhs -> always train_features; rhs -> always test_features
00129                     distance->init(train, test);
00130                 }
00131             }
00132             else
00133                 SG_ERROR("Distance can not process this test feature type: %d %d.\n", fclass, ftype);
00134         }
00135         else
00136             SG_ERROR("Assign train and test features first.\n");
00137     }
00138     else
00139     {
00140         SG_NOTIMPLEMENTED;
00141         return false;
00142     }
00143 
00144     return true;
00145 
00146 }
00147 
00148 bool CGUIDistance::save_distance(char* param)
00149 {
00150     bool result=false;
00151     char filename[1024]="";
00152 
00153     if (distance && initialized)
00154     {
00155         if ((sscanf(param, "%s", filename))==1)
00156         {
00157             CAsciiFile* file=new CAsciiFile(filename);
00158             try
00159             {
00160                 distance->save(file);
00161             }
00162             catch (...)
00163             {
00164                 SG_ERROR( "writing to file %s failed!\n", filename);
00165             }
00166 
00167             SG_INFO( "successfully written distance to \"%s\" !\n", filename);
00168             result=true;
00169             SG_UNREF(file);
00170         }
00171         else
00172             SG_ERROR( "see help for params\n");
00173     }
00174     else
00175         SG_ERROR( "no distance set / distance not initialized!\n");
00176     return result;
00177 }
00178 
00179 CDistance* CGUIDistance::create_generic(EDistanceType type)
00180 {
00181     CDistance* dist=NULL;
00182 
00183     switch (type)
00184     {
00185         case D_MANHATTAN:
00186             dist=new CManhattanMetric(); break;
00187         case D_MANHATTANWORD:
00188             dist=new CManhattanWordDistance(); break;
00189         case D_CANBERRA:
00190             dist=new CCanberraMetric(); break;
00191         case D_CANBERRAWORD:
00192             dist=new CCanberraWordDistance(); break;
00193         case D_CHEBYSHEW:
00194             dist=new CChebyshewMetric(); break;
00195         case D_GEODESIC:
00196             dist=new CGeodesicMetric(); break;
00197         case D_JENSEN:
00198             dist=new CJensenMetric(); break;
00199         case D_EUCLIDIAN:
00200             dist=new CEuclidianDistance(); break;
00201         case D_SPARSEEUCLIDIAN:
00202             dist=new CSparseEuclidianDistance(); break;
00203         case D_CHISQUARE:
00204             dist=new CChiSquareDistance(); break;
00205         case D_TANIMOTO:
00206             dist=new CTanimotoDistance(); break;
00207         case D_COSINE:
00208             dist=new CCosineDistance(); break;
00209         case D_BRAYCURTIS:
00210             dist=new CBrayCurtisDistance(); break;
00211         default:
00212             SG_ERROR("Unknown metric/distance type %d given to create generic distance/metric.\n", type);
00213     }
00214 
00215     if (dist)
00216         SG_INFO("Metric/Distance of type %d created (%p).\n", type, dist);
00217     else
00218         SG_ERROR("Failed creating metric of type %d.\n", type);
00219 
00220     return dist;
00221 }
00222 
00223 CDistance* CGUIDistance::create_minkowski(float64_t k)
00224 {
00225     CDistance* dist=new CMinkowskiMetric(k);
00226     if (dist)
00227         SG_INFO("Minkowski Metric created (%p), k %f.\n", dist, k);
00228     else
00229         SG_ERROR("Failed Creating Minkowski Metric, k %f.\n", k);
00230 
00231     return dist;
00232 }
00233 
00234 CDistance* CGUIDistance::create_hammingword(bool use_sign)
00235 {
00236     CDistance* dist=new CHammingWordDistance(use_sign);
00237     if (dist)
00238     {
00239         SG_INFO("HammingWord distance created (%p), use sign %d.\n",
00240             dist, use_sign);
00241     }
00242     else
00243         SG_ERROR("Failed Creating HammingWord distance, use sign %d.\n", use_sign);
00244 
00245     return dist;
00246 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation