SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Features.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) 1999-2009 Soeren Sonnenburg
8  * Written (W) 1999-2008 Gunnar Raetsch
9  * Written (W) 2011-2012 Heiko Strathmann
10  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
11  */
12 
15 #include <shogun/io/SGIO.h>
16 #include <shogun/base/Parameter.h>
18 
19 #include <string.h>
20 
21 using namespace shogun;
22 
23 CFeatures::CFeatures(int32_t size)
24 : CSGObject()
25 {
26  init();
27  cache_size = size;
28 }
29 
31 : CSGObject(orig)
32 {
33  init();
34 
35  // Call to init creates new preproc and preprocessed arrays.
36  SG_UNREF(preproc);
37  SG_UNREF(preprocessed);
38  preproc = orig.preproc;
39  preprocessed = orig.preprocessed;
40  SG_REF(preproc);
41  SG_REF(preprocessed);
42 }
43 
45 : CSGObject()
46 {
47  init();
48 
49  load(loader);
50  SG_INFO("Feature object loaded (%p)\n",this)
51 }
52 
54 {
57  SG_UNREF(preproc);
58  SG_UNREF(preprocessed);
59 }
60 
61 void CFeatures::init()
62 {
63  SG_ADD(&properties, "properties", "Feature properties", MS_NOT_AVAILABLE);
64  SG_ADD(&cache_size, "cache_size", "Size of cache in MB", MS_NOT_AVAILABLE);
65 
66  SG_ADD((CSGObject**) &preproc, "preproc", "Array of preprocessors.",
68  SG_ADD((CSGObject**) &preprocessed, "preprocessed", "Array of preprocessed.",
70 
71  SG_ADD((CSGObject**)&m_subset_stack, "subset_stack", "Stack of subsets",
73 
76 
77  properties = FP_NONE;
78  cache_size = 0;
79  preproc = new CDynamicObjectArray();
80  preprocessed = new CDynamicArray<bool>();
81  SG_REF(preproc);
82  SG_REF(preprocessed);
83 }
84 
86 {
87  ASSERT(p)
88 
89  preproc->push_back(p);
90  preprocessed->push_back(false);
91 }
92 
94 {
95  if (num<preproc->get_num_elements() && num>=0)
96  {
97  return (CPreprocessor*) preproc->get_element(num);
98  }
99  else
100  return NULL;
101 }
102 
104 {
105  int32_t num=0;
106 
107  for (int32_t i=0; i<preproc->get_num_elements(); i++)
108  {
109  if ((*preprocessed)[i])
110  num++;
111  }
112 
113  return num;
114 }
115 
117 {
118  preproc->reset_array();
119  preprocessed->reset_array();
120 }
121 
123 {
124  if (num<preproc->get_num_elements() && num>=0)
125  {
126  preproc->delete_element(num);
127  preprocessed->delete_element(num);
128  }
129 }
130 
132 {
133  int32_t num_preproc = preproc->get_num_elements();
134 
135  for (int32_t i=0; i<num_preproc; i++)
136  {
137  SG_INFO("preproc[%d]=%s applied=%s\n",i,
138  preproc->get_element(i)->get_name(),
139  preprocessed->get_element(i) ? "true" : "false");
140  }
141 }
142 
144 {
145  ASSERT(num<preprocessed->get_num_elements() && num>=0);
146  (*preprocessed)[num]=true;
147 }
148 
149 bool CFeatures::is_preprocessed(int32_t num) const
150 {
151  ASSERT(num<preprocessed->get_num_elements() && num>=0);
152  return (*preprocessed)[num];
153 }
154 
156 {
157  return preproc->get_num_elements();
158 }
159 
161 {
162  return cache_size;
163 }
164 
165 bool CFeatures::reshape(int32_t num_features, int32_t num_vectors)
166 {
168  return false;
169 }
170 
172 {
173  SG_INFO("%p - ", this)
174  switch (get_feature_class())
175  {
176  case C_UNKNOWN:
177  SG_INFO("C_UNKNOWN ")
178  break;
179  case C_DENSE:
180  SG_INFO("C_DENSE ")
181  break;
182  case C_SPARSE:
183  SG_INFO("C_SPARSE ")
184  break;
185  case C_STRING:
186  SG_INFO("C_STRING ")
187  break;
188  case C_COMBINED:
189  SG_INFO("C_COMBINED ")
190  break;
191  case C_COMBINED_DOT:
192  SG_INFO("C_COMBINED_DOT ")
193  break;
194  case C_WD:
195  SG_INFO("C_WD ")
196  break;
197  case C_SPEC:
198  SG_INFO("C_SPEC ")
199  break;
200  case C_WEIGHTEDSPEC:
201  SG_INFO("C_WEIGHTEDSPEC ")
202  break;
203  case C_STREAMING_DENSE:
204  SG_INFO("C_STREAMING_DENSE ")
205  break;
206  case C_STREAMING_SPARSE:
207  SG_INFO("C_STREAMING_SPARSE ")
208  break;
209  case C_STREAMING_STRING:
210  SG_INFO("C_STREAMING_STRING ")
211  break;
212  case C_STREAMING_VW:
213  SG_INFO("C_STREAMING_VW ")
214  break;
215  case C_ANY:
216  SG_INFO("C_ANY ")
217  break;
218  default:
219  SG_ERROR("ERROR UNKNOWN FEATURE CLASS")
220  }
221 
222  switch (get_feature_type())
223  {
224  case F_UNKNOWN:
225  SG_INFO("F_UNKNOWN \n")
226  break;
227  case F_CHAR:
228  SG_INFO("F_CHAR \n")
229  break;
230  case F_BYTE:
231  SG_INFO("F_BYTE \n")
232  break;
233  case F_SHORT:
234  SG_INFO("F_SHORT \n")
235  break;
236  case F_WORD:
237  SG_INFO("F_WORD \n")
238  break;
239  case F_INT:
240  SG_INFO("F_INT \n")
241  break;
242  case F_UINT:
243  SG_INFO("F_UINT \n")
244  break;
245  case F_LONG:
246  SG_INFO("F_LONG \n")
247  break;
248  case F_ULONG:
249  SG_INFO("F_ULONG \n")
250  break;
251  case F_SHORTREAL:
252  SG_INFO("F_SHORTEAL \n")
253  break;
254  case F_DREAL:
255  SG_INFO("F_DREAL \n")
256  break;
257  case F_LONGREAL:
258  SG_INFO("F_LONGREAL \n")
259  break;
260  case F_ANY:
261  SG_INFO("F_ANY \n")
262  break;
263  default:
264  SG_ERROR("ERROR UNKNOWN FEATURE TYPE\n")
265  }
266 }
267 
268 
269 void CFeatures::load(CFile* loader)
270 {
274 }
275 
276 void CFeatures::save(CFile* writer)
277 {
281 }
282 
284 {
285  bool result=false;
286 
287  if (f)
288  {
289  result= ( (this->get_feature_class() == f->get_feature_class()) &&
290  (this->get_feature_type() == f->get_feature_type()));
291  }
292  return result;
293 }
294 
296 {
297  return (properties & p) != 0;
298 }
299 
301 {
302  properties |= p;
303 }
304 
306 {
307  properties &= (properties | p) ^ p;
308 }
309 
311 {
312  m_subset_stack->add_subset(subset);
314 }
315 
317 {
320 }
321 
323 {
326 }
327 
329 {
332 }
333 
335 {
337  return m_subset_stack;
338 }
339 
341 {
342  SG_ERROR("%s::copy_subset(): copy_subset and therefore model storage of "
343  "CMachine (required for cross-validation and model-selection is "
344  "not yet implemented yet. Ask developers!\n", get_name());
345  return NULL;
346 }
347 
349 {
350  SG_WARNING("%s::copy_dimension_subset():: Is not yet implemented!\n",
351  get_name());
352  return NULL;
353 }
354 
356 {
357  if (this->get_feature_class()==rhs)
358  return true;
359  return false;
360 }
virtual const char * get_name() const =0
CSubsetStack * m_subset_stack
Definition: Features.h:361
virtual void save(CFile *writer)
Definition: Features.cpp:276
#define SG_INFO(...)
Definition: SGIO.h:118
virtual void add_subset_in_place(SGVector< index_t > subset)
Definition: Features.cpp:316
#define SG_RESET_LOCALE
Definition: SGIO.h:86
virtual bool reshape(int32_t num_features, int32_t num_vectors)
Definition: Features.cpp:165
virtual bool get_feature_class_compatibility(EFeatureClass rhs) const
Definition: Features.cpp:355
void set_preprocessed(int32_t num)
Definition: Features.cpp:143
CFeatures(int32_t size=0)
Definition: Features.cpp:23
void set_property(EFeatureProperty p)
Definition: Features.cpp:300
#define SG_ERROR(...)
Definition: SGIO.h:129
#define SG_NOTIMPLEMENTED
Definition: SGIO.h:139
CPreprocessor * get_preprocessor(int32_t num) const
Definition: Features.cpp:93
#define SG_REF(x)
Definition: SGObject.h:54
#define SG_SET_LOCALE_C
Definition: SGIO.h:85
EFeatureClass
shogun feature class
Definition: FeatureTypes.h:38
class to add subset support to another class. A CSubsetStackStack instance should be added and wrappe...
Definition: SubsetStack.h:37
int32_t get_num_preprocessors() const
Definition: Features.cpp:155
virtual void del_preprocessor(int32_t num)
Definition: Features.cpp:122
virtual void subset_changed_post()
Definition: Features.h:293
bool check_feature_compatibility(CFeatures *f) const
Definition: Features.cpp:283
void list_feature_obj() const
Definition: Features.cpp:171
virtual CSubsetStack * get_subset_stack()
Definition: Features.cpp:334
virtual void add_subset(SGVector< index_t > subset)
Definition: SubsetStack.cpp:80
#define ASSERT(x)
Definition: SGIO.h:201
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:115
virtual void remove_all_subsets()
Definition: SubsetStack.cpp:59
void unset_property(EFeatureProperty p)
Definition: Features.cpp:305
bool delete_element(int32_t idx)
Definition: DynamicArray.h:375
virtual void add_subset_in_place(SGVector< index_t > subset)
bool is_preprocessed(int32_t num) const
Definition: Features.cpp:149
A File access base class.
Definition: File.h:34
virtual EFeatureClass get_feature_class() const =0
Dynamic array class for CSGObject pointers that creates an array that can be used like a list or an a...
virtual void load(CFile *loader)
Definition: Features.cpp:269
virtual void add_preprocessor(CPreprocessor *p)
Definition: Features.cpp:85
#define SG_UNREF(x)
Definition: SGObject.h:55
virtual CFeatures * copy_dimension_subset(SGVector< index_t > dims)
Definition: Features.cpp:348
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
virtual void remove_subset()
Definition: Features.cpp:322
int32_t get_num_preprocessed() const
Definition: Features.cpp:103
int32_t get_cache_size() const
Definition: Features.cpp:160
virtual CFeatures * copy_subset(SGVector< index_t > indices)
Definition: Features.cpp:340
The class Features is the base class of all feature objects.
Definition: Features.h:68
Class Preprocessor defines a preprocessor interface.
Definition: Preprocessor.h:75
CSGObject * get_element(int32_t index) const
virtual void remove_subset()
virtual ~CFeatures()
Definition: Features.cpp:53
void clean_preprocessors()
Definition: Features.cpp:116
#define SG_WARNING(...)
Definition: SGIO.h:128
#define SG_ADD(...)
Definition: SGObject.h:84
bool has_property(EFeatureProperty p) const
Definition: Features.cpp:295
virtual void remove_all_subsets()
Definition: Features.cpp:328
EFeatureProperty
shogun feature properties
Definition: FeatureTypes.h:65
void list_preprocessors()
Definition: Features.cpp:131
const T & get_element(int32_t idx1, int32_t idx2=0, int32_t idx3=0) const
Definition: DynamicArray.h:212
virtual void add_subset(SGVector< index_t > subset)
Definition: Features.cpp:310
virtual EFeatureType get_feature_type() const =0

SHOGUN Machine Learning Toolbox - Documentation