SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
GUIDistance.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 2006-2008 Christian Gehl
8  * Written (W) 1999-2008 Soeren Sonnenburg
9  * Copyright (C) 1999-2008 Fraunhofer Institute FIRST and Max-Planck-Society
10  */
11 
12 #include <shogun/ui/GUIDistance.h>
13 #include <shogun/ui/SGInterface.h>
14 
15 #include <string.h>
16 
17 #include <shogun/lib/config.h>
18 #include <shogun/io/SGIO.h>
19 #include <shogun/io/CSVFile.h>
20 
38 
45 
46 using namespace shogun;
47 
48 CGUIDistance::CGUIDistance(CSGInterface* ui_)
49 : CSGObject(), ui(ui_)
50 {
51  distance=NULL;
52  initialized=false;
53 }
54 
56 {
58 }
59 
61 {
62  return distance;
63 }
64 
66 {
67  if (dist)
68  {
69  SG_REF(dist);
71  distance=dist;
72  SG_DEBUG("set new distance (%p).\n", dist)
73 
74  return true;
75  }
76  else
77  return false;
78 }
79 
80 bool CGUIDistance::init_distance(const char* target)
81 {
82  SG_DEBUG("init_distance start\n.")
83 
84  if (!distance)
85  SG_ERROR("No distance available.\n")
86 
90 
91  if (!strncmp(target, "TRAIN", 5))
92  {
93  CFeatures* train=ui->ui_features->get_train_features();
94  if (train)
95  {
96  EFeatureClass fclass=train->get_feature_class();
97  EFeatureType ftype=train->get_feature_type();
98  if ((d_fclass==fclass || d_fclass==C_ANY || fclass==C_ANY) &&
99  (d_ftype==ftype || d_ftype==F_ANY || ftype==F_ANY))
100 
101  {
102  distance->init(train, train);
103  initialized=true;
104  }
105  else
106  SG_ERROR("Distance can not process this train feature type: %d %d.\n", fclass, ftype)
107  }
108  else
109  SG_ERROR("Assign train features first.\n")
110  }
111  else if (!strncmp(target, "TEST", 4))
112  {
113  CFeatures* train=ui->ui_features->get_train_features();
114  CFeatures* test=ui->ui_features->get_test_features();
115  if (test)
116  {
117  EFeatureClass fclass=test->get_feature_class();
118  EFeatureType ftype=test->get_feature_type();
119  if ((d_fclass==fclass || d_fclass==C_ANY || fclass==C_ANY) &&
120  (d_ftype==ftype || d_ftype==F_ANY || ftype==F_ANY))
121 
122  {
123  if (!initialized)
124  SG_ERROR("Distance not initialized with training examples.\n")
125  else
126  {
127  SG_INFO("Initialising distance with TEST DATA, train: %p test %p\n", train, test)
128  // lhs -> always train_features; rhs -> always test_features
129  distance->init(train, test);
130  }
131  }
132  else
133  SG_ERROR("Distance can not process this test feature type: %d %d.\n", fclass, ftype)
134  }
135  else
136  SG_ERROR("Assign train and test features first.\n")
137  }
138  else
139  {
141  return false;
142  }
143 
144  return true;
145 
146 }
147 
149 {
150  bool result=false;
151  char filename[1024]="";
152 
153  if (distance && initialized)
154  {
155  if ((sscanf(param, "%s", filename))==1)
156  {
157  CCSVFile* file=new CCSVFile(filename);
158  try
159  {
160  distance->save(file);
161  }
162  catch (...)
163  {
164  SG_ERROR("writing to file %s failed!\n", filename)
165  }
166 
167  SG_INFO("successfully written distance to \"%s\" !\n", filename)
168  result=true;
169  SG_UNREF(file);
170  }
171  else
172  SG_ERROR("see help for params\n")
173  }
174  else
175  SG_ERROR("no distance set / distance not initialized!\n")
176  return result;
177 }
178 
180 {
181  CDistance* dist=NULL;
182 
183  switch (type)
184  {
185  case D_MANHATTAN:
186  dist=new CManhattanMetric(); break;
187  case D_MANHATTANWORD:
188  dist=new CManhattanWordDistance(); break;
189  case D_CANBERRA:
190  dist=new CCanberraMetric(); break;
191  case D_CANBERRAWORD:
192  dist=new CCanberraWordDistance(); break;
193  case D_CHEBYSHEW:
194  dist=new CChebyshewMetric(); break;
195  case D_GEODESIC:
196  dist=new CGeodesicMetric(); break;
197  case D_JENSEN:
198  dist=new CJensenMetric(); break;
199  case D_EUCLIDEAN:
200  dist=new CEuclideanDistance(); break;
201  case D_SPARSEEUCLIDEAN:
202  dist=new CSparseEuclideanDistance(); break;
203  case D_CHISQUARE:
204  dist=new CChiSquareDistance(); break;
205  case D_TANIMOTO:
206  dist=new CTanimotoDistance(); break;
207  case D_COSINE:
208  dist=new CCosineDistance(); break;
209  case D_BRAYCURTIS:
210  dist=new CBrayCurtisDistance(); break;
211  default:
212  SG_ERROR("Unknown metric/distance type %d given to create generic distance/metric.\n", type)
213  }
214 
215  if (dist)
216  SG_INFO("Metric/Distance of type %d created (%p).\n", type, dist)
217  else
218  SG_ERROR("Failed creating metric of type %d.\n", type)
219 
220  return dist;
221 }
222 
224 {
225  CDistance* dist=new CMinkowskiMetric(k);
226  if (dist)
227  SG_INFO("Minkowski Metric created (%p), k %f.\n", dist, k)
228  else
229  SG_ERROR("Failed Creating Minkowski Metric, k %f.\n", k)
230 
231  return dist;
232 }
233 
235 {
236  CDistance* dist=new CHammingWordDistance(use_sign);
237  if (dist)
238  {
239  SG_INFO("HammingWord distance created (%p), use sign %d.\n",
240  dist, use_sign);
241  }
242  else
243  SG_ERROR("Failed Creating HammingWord distance, use sign %d.\n", use_sign)
244 
245  return dist;
246 }
class CosineDistance
class HammingWordDistance
#define SG_INFO(...)
Definition: SGIO.h:118
virtual EFeatureClass get_feature_class()=0
Class Distance, a base class for all the distances used in the Shogun toolbox.
Definition: Distance.h:81
class SparseEucldeanDistance
bool save_distance(char *param)
CDistance * create_hammingword(bool use_sign=false)
class MinkowskiMetric
#define SG_ERROR(...)
Definition: SGIO.h:129
#define SG_NOTIMPLEMENTED
Definition: SGIO.h:139
class Tanimoto coefficient
class ManhattanMetric
class ChiSquareDistance
class ManhattanWordDistance
CDistance * get_distance()
Definition: GUIDistance.cpp:60
#define SG_REF(x)
Definition: SGObject.h:51
EFeatureClass
shogun feature class
Definition: FeatureTypes.h:38
class ChebyshewMetric
Class CSVFile used to read data from comma-separated values (CSV) files. See http://en.wikipedia.org/wiki/Comma-separated_values.
Definition: CSVFile.h:29
virtual void set_precompute_matrix(bool flag)
Definition: Distance.h:268
EDistanceType
Definition: Distance.h:32
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:112
double float64_t
Definition: common.h:50
class JensenMetric
Definition: JensenMetric.h:36
void save(CFile *writer)
Definition: Distance.cpp:113
CDistance * create_generic(EDistanceType type)
virtual EFeatureClass get_feature_class() const =0
CSGInterface * ui
Definition: GUIDistance.h:66
EFeatureType
shogun feature type
Definition: FeatureTypes.h:19
CDistance * distance
Definition: GUIDistance.h:64
class Bray-Curtis distance
bool set_distance(CDistance *dist)
Definition: GUIDistance.cpp:65
#define SG_UNREF(x)
Definition: SGObject.h:52
#define SG_DEBUG(...)
Definition: SGIO.h:107
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
bool init_distance(const char *target)
Definition: GUIDistance.cpp:80
The class Features is the base class of all feature objects.
Definition: Features.h:68
virtual EFeatureType get_feature_type()=0
class GeodesicMetric
class CanberraMetric
class CanberraWordDistance
virtual bool init(CFeatures *lhs, CFeatures *rhs)
Definition: Distance.cpp:78
class EuclideanDistance
virtual EFeatureType get_feature_type() const =0
CDistance * create_minkowski(float64_t k=3)

SHOGUN Machine Learning Toolbox - Documentation