4 #include <boost/regex.hpp>
6 #define BOOST_FILESYSTEM_VERSION 2
7 #include <boost/filesystem/operations.hpp>
8 #include <boost/filesystem/convenience.hpp>
9 #include <boost/filesystem/fstream.hpp>
10 #include <boost/filesystem/path.hpp>
11 #include <boost/lexical_cast.hpp>
14 namespace fs = boost::filesystem;
16 #include "../Exceptions.hpp"
29 full_path = fs::system_complete(fs::path(temp_dir,
33 if(fs::exists(full_path))
35 fs::remove_all(full_path);
40 cerr <<
"Could not delete temp directory...\n";
53 char buffer[] =
"/tmp/CEBL-XXXXXX";
55 string temp_dir = buffer;
60 full_path = fs::system_complete(fs::path(TEMP, fs::native));
63 if(fs::exists(full_path))
65 full_path = fs::system_complete(fs::path(temp_dir.c_str(), fs::native));
67 if(!fs::exists(full_path))
71 fs::create_directory(full_path);
75 cerr <<
"Failed to create temporary directory.\n";
76 throw FileException(
"DataIO: Failed to create temp directory.");
83 throw FileException(
"DataIO: Failed to create temp directory.");
98 std::vector<int> components;
99 ublas::matrix<double>
filter;
110 std::vector<int> filter_removed_components,
111 ublas::matrix<double> filter_matrix)
122 for(
int cls = 0; cls<unfiltered_data.
numClasses(); cls++)
124 for(
int seq=0; seq<unfiltered_data.
numSequences(cls); seq++)
127 fname << current_temp_dir <<
"/class-" << cls <<
"_seq-" << seq;
134 string filter_matrix_filename = current_temp_dir +
"/filter";
137 for(
int cls = 0; cls<filtered_data.
numClasses(); cls++)
139 for(
int seq=0; seq<filtered_data.
numSequences(cls); seq++)
142 fname << current_temp_dir <<
"/filtered_class-"
143 << cls <<
"_seq-" << seq;
150 filt.
saveToFile(filter_matrix_filename.c_str());
157 fname << current_temp_dir <<
"/Description.txt";
159 desc_file.open(fname.str().c_str());
163 struct tm * current_tm;
165 current_tm = localtime(&rawtime);
166 desc_file <<
"# Date: " << asctime(current_tm);
169 desc_file <<
"# Session\n";
170 desc_file <<
"num_classes = " << unfiltered_data.
numClasses() <<
"\n";
171 desc_file <<
"num_sequences = " << unfiltered_data.
numSequences() <<
"\n";
175 desc_file <<
"class_sequence_ordering = ";
176 for(
unsigned i=0;i<ord.size();i++)
179 if(i != ord.size()-1)
186 desc_file <<
"# Class Labels\n";
188 if(labels.size() > 0)
190 for(
int i=0; i < unfiltered_data.
numClasses(); i++)
192 desc_file <<
"class" << i <<
" = \"" << labels.at(i) <<
"\"\n";
197 desc_file <<
"# Channel Names\n";
198 if(channel_names.size() > 0)
200 for(
unsigned i=0; i < channel_names.size(); i++)
202 desc_file <<
"channel" << i <<
" = \""
203 << channel_names.at(i) <<
"\"\n";
207 desc_file <<
"# Data Files Size - CHANNELS x SAMPLES\n";
208 for(
int cls = 0; cls<unfiltered_data.
numClasses(); cls++)
210 for(
int seq=0; seq<unfiltered_data.
numSequences(cls); seq++)
212 desc_file <<
"class-" << cls <<
"_seq-" << seq <<
" = "
220 desc_file <<
"# Filter\n";
221 desc_file <<
"filtered_data_included = " << filtered <<
"\n";
224 desc_file <<
"filter_matrix_file = filter\n";
225 desc_file <<
"filter_matrix_size = " << filter_matrix.size1()
226 <<
"x" << filter_matrix.size2() <<
"\n";
227 desc_file <<
"filter_num_lags = " << filter_lags <<
"\n";
228 desc_file <<
"filter_removed_components = ";
229 for(
unsigned i=0;i<filter_removed_components.size();i++)
231 desc_file << filter_removed_components[i];
232 if(i != filter_removed_components.size()-1)
244 string command =
"cd " + current_temp_dir +
"; tar -cjf "
245 +
static_cast<string>(filename) +
" ./*";
246 cout << command << endl;
247 system(command.c_str());
263 string file = filename;
264 full_path = fs::system_complete(fs::path(file,fs::native));
265 if(!fs::exists(full_path))
267 cerr <<
"File does not exist.\n";
271 string command =
"cd " + current_temp_dir +
"; cp "+file+
" "
272 + current_temp_dir +
"; tar -xjf *.bz2;rm *.bz2";
273 cout << command << endl;
274 system(command.c_str());
277 full_path = fs::system_complete(fs::path(current_temp_dir,
280 fs::directory_iterator end_iter;
281 for (fs::directory_iterator dir_itr( full_path );
291 string file = dir_itr->leaf();
293 boost::regex filename_regexp(
"^class-(\\d*)_seq-(\\d*)");
295 if(!regex_search(file,filename_regexp))
300 cout <<
"reading " << file <<
"\n";
303 regex_match(file.c_str(), what, filename_regexp);
309 c = boost::lexical_cast<
int>(what[1]);
310 s = boost::lexical_cast<
int>(what[2]);
314 cerr <<
"failed to load class " << c <<
" seq " << s <<
"\n";
320 tdata.
set(c,s,new_data);
324 catch(
const std::exception & ex)
326 cerr <<
"Exception: " << ex.what() <<
"\n";
329 cout << tdata <<
"\n";