KSquare Utilities
FeatureFileIOArff.cpp
Go to the documentation of this file.
1 #include "FirstIncludes.h"
2 #include <stdio.h>
3 #include <math.h>
4 #include <ctype.h>
5 #include <time.h>
6 #include <string>
7 #include <iostream>
8 #include <fstream>
9 #include <vector>
10 #include "MemoryDebug.h"
11 using namespace std;
12 
13 
14 #include "KKBaseTypes.h"
15 #include "DateTime.h"
16 #include "OSservices.h"
17 #include "RunLog.h"
18 #include "KKStr.h"
19 using namespace KKB;
20 
21 
22 #include "FeatureFileIOArff.h"
23 #include "FileDesc.h"
24 #include "MLClass.h"
25 using namespace KKMLL;
26 
27 
28 
30 
31 
32 
33 
35  FeatureFileIO ("ARFF", false, true)
36 {
37 }
38 
39 
41 {
42 }
43 
44 
45 
46 FileDescPtr FeatureFileIOArff::GetFileDesc (const KKStr& _fileName,
47  istream& _in,
48  MLClassListPtr _classes,
49  kkint32& _estSize,
50  KKStr& _errorMessage,
51  RunLog& _log
52  )
53 {
54  _log.Level (10) << endl << endl
55  << "FeatureFileIOArff::LoadFile ***ERROR*** ARFF read Functionality not implemented." << endl
56  << endl;
57 
58  _errorMessage = "ARFF read functionality not implemented.";
59  return NULL;
60 }
61 
62 
63 
64 
65 
66 FeatureVectorListPtr FeatureFileIOArff::LoadFile (const KKStr& _fileName,
67  const FileDescPtr _fileDesc,
68  MLClassList& _classes,
69  istream& _in,
70  kkint32 _maxCount, // Maximum # images to load.
71  VolConstBool& _cancelFlag,
72  bool& _changesMade,
73  KKStr& _errorMessage,
74  RunLog& _log
75  )
76 {
77  _log.Level (10) << endl << endl
78  << "FeatureFileIOArff::LoadFile ***ERROR*** ARFF read Functionality not implemented." << endl
79  << endl;
80 
81  _errorMessage = "ARFF read functionality not implemented.";
82  return NULL;
83 } /* LoadFile */
84 
85 
86 
87 
88 void FeatureFileIOArff::SaveFile (FeatureVectorList& _data,
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) << "FeatureFileIOArff::SaveFile FileName[" << _fileName << "]" << endl;
100 
101  _numExamplesWritten = 0;
102 
103  FileDescPtr fileDesc = _data.FileDesc ();
104  const AttributePtr* attrTable = fileDesc->CreateAAttributeTable ();
105 
106  kkint32 x;
107  {
108  _out << "% ARFF Format Definition: http://www.cs.waikato.ac.nz/~ml/weka/arff.html" << endl
109  << "%" << endl
110  << "% FileName [" << _fileName << "]" << endl
111  << "% DateWritten [" << osGetLocalDateTime () << "]" << endl
112  << "% SelectedFeatures [" << _selFeatures.ToString () << "]" << endl
113  << "% TotalRecords [" << _data.QueueSize () << "]" << endl
114  << "% NumAttributes [" << _selFeatures.NumOfFeatures () << "]" << endl
115  << "%" << endl
116  << "% ClassName" << "\t" << "Count" << endl;
117 
118  ClassStatisticListPtr classStatistics = _data.GetClassStatistics ();
119  KKStr classListStr (classStatistics->QueueSize () * 15);
120  for (x = 0; x < classStatistics->QueueSize (); x++)
121  {
122  ClassStatisticPtr classStatistic = classStatistics->IdxToPtr (x);
123  _out << "% " << classStatistic->Name () << "\t" << classStatistic->Count () << endl;
124  if (x > 0)
125  classListStr << ", ";
126  classListStr << classStatistic->Name ();
127  }
128 
129  _out << "%" << endl
130  << "% Total" << "\t" << _data.QueueSize () << endl
131  << "%" << endl
132  << "%" << endl
133  << "@relation image_features" << endl
134  << "%" << endl;
135 
136 
137  for (kkint32 fnIDX = 0; fnIDX < _selFeatures.NumOfFeatures (); fnIDX++)
138  {
139  kkint32 featureNum = _selFeatures[fnIDX];
140  AttributePtr attr = attrTable[featureNum];
141  _out << "@attribute "
142  << attr->Name ()
143  << " ";
144 
145  if ((attr->Type () == AttributeType::Nominal) ||
147  )
148  {
149  _out << "(";
150  for (x = 0; x < attr->Cardinality (); x++)
151  {
152  if (x > 0)
153  _out << ",";
154  _out << attr->GetNominalValue (x);
155  }
156  _out << ")" << endl;
157  }
158  else
159  {
160  _out << "NUMERIC";
161  }
162 
163  _out << endl;
164  }
165 
166  _out << "@attribute ExampleFileName " << "string" << endl;
167 
168  _out << "@attribute Classes? { " << classListStr << " }" << endl;
169 
170  _out << endl
171  << endl
172  << "@data" << endl
173  << "%" << endl
174  << "% " << _data.QueueSize () << " Instances" << endl
175  << "%" << endl;
176  delete classStatistics;
177  }
178 
179 
180  kkint32 numOfDigistsNeededInRowMask = Min (1, kkint32 (log10 (float (_data.QueueSize ()))) + 1);
181 
182  KKStr rowMask = "0";
183  rowMask.RightPad (numOfDigistsNeededInRowMask, '0');
184 
185  FeatureVectorPtr example = NULL;
186 
187  kkint32 idx;
188  for (idx = 0; idx < _data.QueueSize (); idx++)
189  {
190  example = _data.IdxToPtr (idx);
191 
192  for (x = 0; x < _selFeatures.NumOfFeatures (); x++)
193  {
194  kkint32 featureNum = _selFeatures[x];
195 
196  if ((attrTable[featureNum]->Type () == AttributeType::Nominal) ||
197  (attrTable[featureNum]->Type () == AttributeType::Symbolic)
198  )
199  _out << attrTable[featureNum]->GetNominalValue ((kkint32)(example->FeatureData (featureNum)));
200  else
201  _out << example->FeatureData (featureNum);
202  _out << ",";
203  }
204 
206  if (imageFileName.Empty ())
207  imageFileName == "Row_" + StrFormatInt (idx, rowMask.Str ());
208  _out << imageFileName << ",";
209  _out << example->ClassName ();
210  _out << endl;
211  _numExamplesWritten++;
212  }
213 
214  _successful = true;
215  delete attrTable;
216  return;
217 } /* SaveFile */
const KKStr & Name() const
__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
Support the writing of ARFF Formatted Feature Files.
KKStr & operator=(const char *src)
Definition: KKStr.cpp:1442
const FileDescPtr FileDesc() const
FeatureNumList const FeatureNumListConst
ClassStatistic * ClassStatisticPtr
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.
KKStr operator+(const char *left, const KKStr &right)
Definition: KKStr.cpp:3976
kkint32 NumOfFeatures() const
Base class for all FeatureFileIO classes.
Definition: FeatureFileIO.h:48
bool Empty() const
Definition: KKStr.h:241
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of &#39;std::string&#39; strings.
Definition: KKStr.cpp:1082
ClassStatisticListPtr GetClassStatistics() const
Returns the number of FeatureVectors per class.
AttributeType
Definition: Attribute.h:36
FileDesc * FileDescPtr
bool operator==(const KKStr &right) const
Definition: KKStr.cpp:1550
KKStr StrFormatInt(kkint32 val, const char *mask)
Definition: KKStr.cpp:5004
ClassStatisticList * ClassStatisticListPtr
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
const char * Str() const
Returns a pointer to a ascii string.
Definition: KKStr.h:422
void RightPad(kkint32 width, char ch= ' ')
Pads string on the right side with specified character so that the string will be of specified length...
Definition: KKStr.cpp:2239
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
KKStr osGetRootNameWithExtension(const KKStr &fullFileName)
const KKStr & ClassName() const
Name of class that this example is assigned to.
Maintains a list of MLClass instances.
Definition: MLClass.h:233
kkint32 Cardinality() const
Returns back the cardinality of the attribute; the number of possible values it can take...
Definition: Attribute.cpp:173
FeatureFileIO(const KKStr &_driverName, bool _canRead, bool _canWrite)
const KKStr & ExampleFileName() const
Name of file that this FeatureVector was computed from.
volatile const bool VolConstBool
Definition: KKBaseTypes.h:163