KSquare Utilities
FeatureFileIORoberts.cpp
Go to the documentation of this file.
1 #include "FirstIncludes.h"
2 
3 #include <stdio.h>
4 #include <math.h>
5 #include <ctype.h>
6 #include <time.h>
7 
8 #include <string>
9 #include <iostream>
10 #include <fstream>
11 #include <vector>
12 
13 #include "MemoryDebug.h"
14 
15 using namespace std;
16 
17 
18 #include "KKBaseTypes.h"
19 #include "DateTime.h"
20 #include "OSservices.h"
21 #include "RunLog.h"
22 #include "KKStr.h"
23 using namespace KKB;
24 
25 
27 #include "FileDesc.h"
28 #include "MLClass.h"
29 using namespace KKMLL;
30 
31 
33 
34 
35 
37  FeatureFileIO ("Roberts", false, true)
38 {
39 }
40 
42 {
43 }
44 
45 
46 
48  istream& _in,
49  MLClassListPtr _classes,
50  kkint32& _estSize,
51  KKStr& _errorMessage,
52  RunLog& _log
53  )
54 {
55  _log.Level (10) << endl << endl
56  << "FeatureFileIORoberts::LoadFile ***ERROR*** ARFF read Functionality not implemented." << endl
57  << endl;
58 
59  _errorMessage = "ROBERTS read functionality not implemented.";
60  return NULL;
61 }
62 
63 
64 
65 
66 
67 FeatureVectorListPtr FeatureFileIORoberts::LoadFile (const KKStr& _fileName,
68  const FileDescPtr _fileDesc,
69  MLClassList& _classes,
70  istream& _in,
71  kkint32 _maxCount, // Maximum # images to load.
72  VolConstBool& _cancelFlag,
73  bool& _changesMade,
74  KKStr& _errorMessage,
75  RunLog& _log
76  )
77 {
78  _log.Level (10) << endl << endl
79  << "FeatureFileIORoberts::LoadFile ***ERROR*** ARFF read Functionality not implemented." << endl
80  << endl;
81 
82  _errorMessage = "ROBERTS read functionality not implemented.";
83  return NULL;
84 } /* LoadFile */
85 
86 
87 
89  const KKStr& _fileName,
90  FeatureNumListConst& _selFeatures,
91  ostream& _out,
92  kkuint32& _numExamplesWritten,
93  VolConstBool& _cancelFlag,
94  bool& _successful,
95  KKStr& _errorMessage,
96  RunLog& _log
97  )
98 {
99  _log.Level (20) << "FeatureFileIORoberts::SaveFile FileName[" << _fileName << "]" << endl;
100 
101  _numExamplesWritten = 0;
102 
103  FileDescPtr fileDesc = _data.FileDesc ();
104 
105  const AttributePtr* attrTable = fileDesc->CreateAAttributeTable ();
106 
107  kkint32 x;
108  {
109  KKStr namesFileName = _fileName + ".names";
110  // Write _out names file
111  ofstream nf (namesFileName.Str ());
112 
113  MLClassListPtr classes = _data.ExtractListOfClasses ();
114  classes->SortByName ();
115  for (x = 0; x < classes->QueueSize (); x++)
116  {
117  if (x > 0) nf << " ";
118  nf << classes->IdxToPtr (x)->Name ();
119  }
120  delete classes;
121  classes = NULL;
122  nf << endl << endl;
123 
124  for (x = 0; x < _selFeatures.NumOfFeatures (); x++)
125  {
126  kkint32 featureNum = _selFeatures[x];
127  AttributePtr attr = attrTable[featureNum];
128  if ((attr->Type () == AttributeType::Nominal) ||
130  )
131  {
132  kkint32 y;
133  nf << "discrete";
134  for (y = 0; y < attr->Cardinality (); y++)
135  nf << " " << attr->GetNominalValue (y);
136  }
137  else
138  {
139  nf << "continuous";
140  }
141  nf << endl;
142  }
143  nf.close ();
144  }
145 
146 
147  FeatureVectorPtr example = NULL;
148 
149  kkint32 idx;
150  for (idx = 0; (idx < _data.QueueSize ()) && (!_cancelFlag); idx++)
151  {
152  example = _data.IdxToPtr (idx);
153 
154  for (x = 0; x < _selFeatures.NumOfFeatures (); x++)
155  {
156  kkint32 featureNum = _selFeatures[x];
157  AttributePtr attr = attrTable[featureNum];
158 
159  if ((attr->Type () == AttributeType::Nominal) ||
161  )
162  _out << attr->GetNominalValue ((kkint32)(example->FeatureData (featureNum)));
163  else
164  _out << example->FeatureData (featureNum);
165  _out << " ";
166  }
167  _out << example->ClassName ();
168  _out << endl;
169  _numExamplesWritten++;
170  }
171 
172  if (!_cancelFlag)
173  _successful = true;
174 
175  delete attrTable;
176  return;
177 } /* WriteRobertsFile */
virtual FileDescPtr GetFileDesc(const KKStr &_fileName, istream &_in, MLClassListPtr _classList, kkint32 &_estSize, KKStr &_errorMessage, RunLog &_log)
virtual void SaveFile(FeatureVectorList &_data, const KKStr &_fileName, FeatureNumListConst &_selFeatures, ostream &_out, kkuint32 &_numExamplesWritten, VolConstBool &_cancelFlag, bool &_successful, KKStr &_errorMessage, RunLog &_log)
__int32 kkint32
Definition: KKBaseTypes.h:88
const KKStr & GetNominalValue(kkint32 code) const
Returns the nominal value for the given ordinal value.
Definition: Attribute.cpp:143
float FeatureData(kkint32 featureNum) const
MLClassListPtr ExtractListOfClasses() const
KKStr & operator=(const char *src)
Definition: KKStr.cpp:1442
Supports the writing of Feature Data to a file that can then be read by OpenDT.
const FileDescPtr FileDesc() const
FeatureNumList const FeatureNumListConst
KKStr operator+(const char *right) const
Definition: KKStr.cpp:3986
unsigned __int32 kkuint32
Definition: KKBaseTypes.h:89
Container class for FeatureVector derived objects.
Attribute * AttributePtr
Definition: Attribute.h:156
KKTHread * KKTHreadPtr
kkuint16 operator[](kkint32 idx) const
Returns back the selected feature.
kkint32 NumOfFeatures() const
Base class for all FeatureFileIO classes.
Definition: FeatureFileIO.h:48
virtual FeatureVectorListPtr LoadFile(const KKStr &_fileName, const FileDescPtr _fileDesc, MLClassList &_classes, istream &_in, kkint32 _maxCount, VolConstBool &_cancelFlag, bool &_changesMade, KKStr &_errorMessage, RunLog &_log)
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of &#39;std::string&#39; strings.
Definition: KKStr.cpp:1082
AttributeType
Definition: Attribute.h:36
FileDesc * FileDescPtr
AttributeType Type() const
Definition: Attribute.h:133
std::ostream &__cdecl operator<<(std::ostream &os, const KKStr &str)
const KKMLL::AttributePtr * CreateAAttributeTable() const
Definition: FileDesc.cpp:408
Used for logging messages.
Definition: RunLog.h:49
void EncodeProblem(const struct svm_paramater &param, struct svm_problem &prob_in, struct svm_problem &prob_out)
MLClassList * MLClassListPtr
Definition: MLClass.h:49
const KKStr & ClassName() const
Name of class that this example is assigned to.
Maintains a list of MLClass instances.
Definition: MLClass.h:233
FeatureFileIO(const KKStr &_driverName, bool _canRead, bool _canWrite)
volatile const bool VolConstBool
Definition: KKBaseTypes.h:163