2014-06-04 13:40:19 +01:00
|
|
|
/*
|
|
|
|
* resample.cpp, part of LatAnalyze 3
|
|
|
|
*
|
2016-04-06 20:11:23 +01:00
|
|
|
* Copyright (C) 2013 - 2016 Antonin Portelli
|
2014-06-04 13:40:19 +01:00
|
|
|
*
|
|
|
|
* LatAnalyze 3 is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* LatAnalyze 3 is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with LatAnalyze 3. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2016-06-14 14:06:59 +01:00
|
|
|
#include <LatCore/OptParser.hpp>
|
2014-03-13 18:51:01 +00:00
|
|
|
#include <LatAnalyze/Dataset.hpp>
|
2015-10-01 00:13:34 +01:00
|
|
|
#include <LatAnalyze/Io.hpp>
|
2016-06-14 14:06:59 +01:00
|
|
|
#include <LatAnalyze/includes.hpp>
|
2014-02-17 18:50:26 +00:00
|
|
|
|
|
|
|
#ifndef DEF_NSAMPLE
|
2016-06-14 14:06:59 +01:00
|
|
|
#define DEF_NSAMPLE "100"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_HDF5
|
|
|
|
#define DEF_FMT "h5"
|
|
|
|
#else
|
|
|
|
#define DEF_FMT "sample"
|
2014-02-17 18:50:26 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace Latan;
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2014-06-04 13:40:19 +01:00
|
|
|
// argument parsing ////////////////////////////////////////////////////////
|
2016-06-14 14:06:59 +01:00
|
|
|
OptParser opt;
|
|
|
|
bool parsed;
|
2016-04-06 20:04:23 +01:00
|
|
|
random_device rd;
|
|
|
|
SeedType seed = rd();
|
2016-06-14 14:06:59 +01:00
|
|
|
string manFileName, nameFileName, outDirName;
|
|
|
|
string ext;
|
|
|
|
Index binSize, nSample;
|
2014-02-17 18:50:26 +00:00
|
|
|
|
2016-06-14 14:06:59 +01:00
|
|
|
opt.addOption("n", "nsample" , OptParser::OptType::value, true,
|
|
|
|
"number of samples", DEF_NSAMPLE);
|
|
|
|
opt.addOption("b", "bin" , OptParser::OptType::value, true,
|
|
|
|
"bin size", "1");
|
|
|
|
opt.addOption("r", "seed" , OptParser::OptType::value, true,
|
|
|
|
"random generator seed (default: random)");
|
|
|
|
opt.addOption("o", "output-dir", OptParser::OptType::value, true,
|
|
|
|
"output directory", ".");
|
|
|
|
opt.addOption("f", "format" , OptParser::OptType::value, true,
|
|
|
|
"output file format", DEF_FMT);
|
|
|
|
opt.addOption("" , "help" , OptParser::OptType::trigger, true,
|
|
|
|
"show this help message and exit");
|
|
|
|
parsed = opt.parse(argc, argv);
|
2016-06-15 19:22:24 +01:00
|
|
|
if (!parsed or (opt.getArgs().size() != 2) or opt.gotOption("help"))
|
2014-02-17 18:50:26 +00:00
|
|
|
{
|
2016-06-14 14:06:59 +01:00
|
|
|
cerr << "usage: " << argv[0];
|
|
|
|
cerr << " <datafile list> <name list> <options>" << endl;
|
|
|
|
cerr << endl << "Possible options:" << endl << opt << endl;
|
|
|
|
|
|
|
|
return EXIT_FAILURE;
|
2014-02-17 18:50:26 +00:00
|
|
|
}
|
2016-06-14 14:06:59 +01:00
|
|
|
nSample = opt.optionValue<Index>("n");
|
|
|
|
binSize = opt.optionValue<Index>("b");
|
|
|
|
if (opt.gotOption("r"))
|
2014-02-17 18:50:26 +00:00
|
|
|
{
|
2016-06-14 14:06:59 +01:00
|
|
|
seed = opt.optionValue<SeedType>("r");
|
2014-02-17 18:50:26 +00:00
|
|
|
}
|
2016-06-14 14:06:59 +01:00
|
|
|
ext = opt.optionValue("f");
|
|
|
|
outDirName = opt.optionValue("o");
|
|
|
|
manFileName = opt.getArgs()[0];
|
|
|
|
nameFileName = opt.getArgs()[1];
|
2014-02-17 18:50:26 +00:00
|
|
|
|
2014-06-04 13:40:19 +01:00
|
|
|
// parameter parsing ///////////////////////////////////////////////////////
|
|
|
|
vector<string> dataFileName, name;
|
|
|
|
|
|
|
|
dataFileName = readManifest(manFileName);
|
|
|
|
name = readManifest(nameFileName);
|
|
|
|
cout << "================================================" << endl;
|
|
|
|
cout << "Data resampler" << endl;
|
|
|
|
cout << "------------------------------------------------" << endl;
|
2015-11-18 19:47:29 +00:00
|
|
|
cout << " #file= " << dataFileName.size() << endl;
|
|
|
|
cout << " #name= " << name.size() << endl;
|
|
|
|
cout << " bin size= " << binSize << endl;
|
|
|
|
cout << " #sample= " << nSample << endl;
|
|
|
|
cout << " output dir: " << outDirName << endl;
|
|
|
|
cout << "output format: " << ext << endl;
|
2014-06-04 13:40:19 +01:00
|
|
|
cout << "------------------------------------------------" << endl;
|
2014-02-17 18:50:26 +00:00
|
|
|
|
2014-06-04 13:40:19 +01:00
|
|
|
// data loading ////////////////////////////////////////////////////////////
|
|
|
|
map<string, Dataset<DMat>> data;
|
|
|
|
|
|
|
|
cout << "-- loading data..." << endl;
|
|
|
|
for (const string &n: name)
|
|
|
|
{
|
|
|
|
data[n].resize(static_cast<Index>(dataFileName.size()));
|
|
|
|
}
|
|
|
|
for (unsigned int i = 0; i < dataFileName.size(); ++i)
|
|
|
|
{
|
2015-11-18 19:47:29 +00:00
|
|
|
std::unique_ptr<File> dataFile = Io::open(dataFileName[i]);
|
2015-10-01 00:13:34 +01:00
|
|
|
|
2014-06-04 13:40:19 +01:00
|
|
|
cout << '\r' << ProgressBar(i + 1, dataFileName.size());
|
|
|
|
for (const string &n: name)
|
|
|
|
{
|
2015-10-01 00:13:34 +01:00
|
|
|
data[n][i] = dataFile->read<DMat>(n);
|
2014-06-04 13:40:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
cout << endl;
|
|
|
|
|
|
|
|
// data resampling /////////////////////////////////////////////////////////
|
|
|
|
DMatSample s(nSample);
|
|
|
|
|
|
|
|
cout << "-- resampling data..." << endl;
|
|
|
|
for (unsigned int i = 0; i < name.size(); ++i)
|
2014-02-17 18:50:26 +00:00
|
|
|
{
|
2015-11-18 19:47:29 +00:00
|
|
|
const string outFileName = name[i] + "_" + manFileName + "." + ext;
|
2014-06-04 13:40:19 +01:00
|
|
|
|
|
|
|
cout << '\r' << ProgressBar(i + 1, name.size());
|
|
|
|
data[name[i]].bin(binSize);
|
2016-04-06 20:04:23 +01:00
|
|
|
s = data[name[i]].bootstrapMean(nSample, seed);
|
2015-10-01 00:13:34 +01:00
|
|
|
Io::save<DMatSample>(s, outDirName + "/" + outFileName,
|
|
|
|
File::Mode::write, outFileName);
|
2014-02-17 18:50:26 +00:00
|
|
|
}
|
2014-06-04 13:40:19 +01:00
|
|
|
cout << endl;
|
2014-02-17 18:50:26 +00:00
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|