00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "common.h"
00020 #include "Collection.h"
00021 #include <algorithm>
00022 #include <iterator>
00023 #include <time.h>
00024
00025
00026
00027 using std::ostringstream;
00028 using std::vector;
00029 using std::ifstream;
00030 using std::ofstream;
00031 using std::ostream_iterator;
00032 using std::endl;
00033 using std::ostream;
00034 using std::istream;
00035
00036
00037
00038
00039 namespace Marsyas
00040 {
00041
00042
00043 mrs_string join(const vector<mrs_string>& v, const mrs_string delim)
00044 {
00045 ostringstream os;
00046 copy(v.begin(), v.end(), ostream_iterator<mrs_string>(os, delim.c_str()));
00047
00048 return os.str();
00049 }
00050
00051 Collection::Collection()
00052 {
00053 collectionList_.reserve(1024);
00054 hasLabels_ = false;
00055 store_labels_ = true;
00056
00057 srand( time( NULL) );
00058 }
00059
00060 Collection::~Collection()
00061 {
00062 }
00063
00064 void
00065 Collection::setName(mrs_string name)
00066 {
00067 name_ = name;
00068 }
00069
00070 void
00071 Collection::store_labels(mrs_bool store)
00072 {
00073 store_labels_ = store;
00074 }
00075
00076
00077 void
00078 Collection::read(mrs_string filename)
00079 {
00080 ifstream is(filename.c_str());
00081 name_ = filename.substr(0, filename.rfind(".", filename.length()));
00082
00083 is >> (*this);
00084 }
00085
00086
00087 void
00088 Collection::write(mrs_string filename)
00089 {
00090 ofstream os(filename.c_str());
00091 os << (*this) << endl;
00092 }
00093
00094 void
00095 Collection::labelAll(mrs_string label)
00096 {
00097 if (hasLabels_ == false)
00098 {
00099 hasLabels_ = true;
00100 labelList_.reserve(collectionList_.size());
00101 for (unsigned int i = 0; i < collectionList_.size(); ++i)
00102 labelList_.push_back(label);
00103 }
00104 else
00105 {
00106 for (unsigned int i=0; i < collectionList_.size(); ++i)
00107 labelList_[i] = label;
00108 }
00109 }
00110
00111 ostream&
00112 operator<<(ostream& o, const Collection& l)
00113 {
00114
00115
00116 for (unsigned int i=0; i < l.collectionList_.size(); ++i)
00117 {
00118 o << l.collectionList_[i];
00119 if (l.hasLabels_)
00120 o << "\t" << l.labelList_[i];
00121 o << endl;
00122 }
00123
00124 return o;
00125 }
00126
00127
00128
00129 size_t
00130 Collection::size()
00131 {
00132 return collectionList_.size();
00133 }
00135 size_t
00136 Collection::getSize()
00137 {
00138 return collectionList_.size();
00139 }
00140
00141
00142 mrs_string
00143 Collection::name()
00144 {
00145 return name_;
00146 }
00147
00148 void
00149 Collection::add(mrs_string entry)
00150 {
00151 collectionList_.push_back(entry);
00152 hasLabels_ = false;
00153 }
00154
00155
00156
00157 void
00158 Collection::clear()
00159 {
00160 collectionList_.clear();
00161 labelList_.clear();
00162
00163
00164
00165
00166 }
00167
00168
00169 void
00170 Collection::add(mrs_string entry, mrs_string label)
00171 {
00172
00173 collectionList_.push_back(entry);
00174 hasLabels_ = true;
00175 labelList_.push_back(label);
00176
00177 if (store_labels_) {
00178 if (find(labelNames_.begin(), labelNames_.end(), label) == labelNames_.end()) {
00179 labelNames_.push_back(label);
00180 }
00181 sort(labelNames_.begin(), labelNames_.end());
00182 }
00183
00184 }
00185
00186
00187
00188
00189 mrs_natural
00190 Collection::getNumLabels()
00191 {
00192 return labelNames_.size();
00193 }
00194
00195 mrs_string
00196 Collection::labelName(mrs_natural i)
00197 {
00198 if ((unsigned)i < labelNames_.size())
00199 return labelNames_[i];
00200
00201 return EMPTYSTRING;
00202 }
00203
00204 mrs_string
00205 Collection::getLabelNames()
00206 {
00207 return join(labelNames_, ",");
00208 }
00209
00210 mrs_bool
00211 Collection::hasLabels()
00212 {
00213 return hasLabels_;
00214 }
00215
00216 void
00217 Collection::shuffle()
00218 {
00219
00220
00221 unsigned int n = collectionList_.size();
00222 while (n > 1)
00223 {
00224
00225 int k = (unsigned int)(n * ((mrs_real)rand() / ((mrs_real)(RAND_MAX) + (mrs_real)1)));
00226
00227 n--;
00228 swap(collectionList_[n], collectionList_[k]);
00229 if (hasLabels_)
00230 swap(labelList_[n], labelList_[k]);
00231 }
00232 }
00233
00234 mrs_string
00235 Collection::toLongString()
00236 {
00237 return join(collectionList_, ",");
00238 }
00239
00240 mrs_natural
00241 Collection::labelNum(mrs_string label)
00242 {
00243
00244 vector<mrs_string>::iterator it = find(labelNames_.begin(), labelNames_.end(), label);
00245 if (it == labelNames_.end())
00246 return -1;
00247
00248 mrs_natural l = distance(labelNames_.begin(), it);
00249 return l ;
00250
00251 }
00252 mrs_real
00253 Collection::regression_label(mrs_natural cindex)
00254 {
00255 if (hasLabels_) {
00256 return (mrs_real) atof(labelList_[cindex].c_str());
00257 }
00258 return 0.0;
00259 }
00260
00261 mrs_string
00262 Collection::labelEntry(unsigned int i)
00263 {
00264 if (hasLabels_)
00265 return labelList_[i];
00266 return "No label";
00267 }
00268
00269 mrs_string
00270 Collection::entry(unsigned int i)
00271 {
00272 return collectionList_[i];
00273 }
00274
00275
00276 void
00277 Collection::concatenate(vector<Collection> cls)
00278 {
00279 for (size_t cj = 0; cj < cls.size(); cj++)
00280 {
00281 Collection l = cls[cj];
00282 if (l.hasLabels_)
00283 hasLabels_ = true;
00284
00285 for (size_t i = 0; i < l.size(); ++i)
00286 add(l.entry(i), l.labelEntry(i));
00287 }
00288 }
00289
00290
00291
00292 istream&
00293 operator>>(istream& i, Collection& l)
00294 {
00295 MRSDIAG("Collection.cpp - operator>>");
00296
00297 mrs_string fileEntry;
00298 while (getline(i, fileEntry))
00299 {
00300
00301 if (fileEntry.empty())
00302 continue;
00303
00304
00305 if (fileEntry[0] == '#')
00306 continue;
00307
00308
00309
00310 mrs_string::size_type loc = fileEntry.find('\t', 0);
00311 if (loc != mrs_string::npos)
00312 {
00313 mrs_string file = fileEntry.substr(0, loc);
00314 mrs_string label = fileEntry.substr(loc+1, fileEntry.size());
00315 l.add(file, label);
00316 }
00317 else
00318 l.add(fileEntry);
00319 }
00320
00321 return i;
00322 }
00323 }