2014-06-04 13:40:19 +01:00
|
|
|
/*
|
|
|
|
* resample.cpp, part of LatAnalyze 3
|
|
|
|
*
|
2015-06-11 14:04:54 +01:00
|
|
|
* Copyright (C) 2013 - 2015 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/>.
|
|
|
|
*/
|
|
|
|
|
2014-02-17 18:50:26 +00:00
|
|
|
#include <iostream>
|
2014-06-04 13:40:19 +01:00
|
|
|
#include <map>
|
2014-02-17 18:50:26 +00:00
|
|
|
#include <libgen.h>
|
|
|
|
#include <unistd.h>
|
2014-03-13 18:51:01 +00:00
|
|
|
#include <LatAnalyze/Dataset.hpp>
|
2015-10-01 00:13:34 +01:00
|
|
|
#include <LatAnalyze/Io.hpp>
|
2014-02-17 18:50:26 +00:00
|
|
|
|
|
|
|
#ifndef DEF_NSAMPLE
|
2014-02-26 18:36:29 +00:00
|
|
|
#define DEF_NSAMPLE 100
|
2014-02-17 18:50:26 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace Latan;
|
|
|
|
|
|
|
|
static void usage(const string &cmdName)
|
|
|
|
{
|
2014-06-04 13:40:19 +01:00
|
|
|
cerr << "usage: " << cmdName;
|
2015-11-18 19:47:29 +00:00
|
|
|
cerr << " [-n <nsample> -b <bin size> -r <state> -o <output dir> -f {h5|sample}]";
|
2014-06-04 13:40:19 +01:00
|
|
|
cerr << " <data list> <name list>";
|
|
|
|
cerr << endl;
|
2014-02-17 18:50:26 +00:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2014-06-04 13:40:19 +01:00
|
|
|
// argument parsing ////////////////////////////////////////////////////////
|
|
|
|
int c;
|
2014-10-21 17:36:02 +01:00
|
|
|
string manFileName, nameFileName, stateFileName, cmdName, outDirName = ".";
|
2015-11-18 19:47:29 +00:00
|
|
|
string ext = "h5";
|
2014-06-04 13:40:19 +01:00
|
|
|
Index binSize = 1, nSample = DEF_NSAMPLE;
|
2014-02-17 18:50:26 +00:00
|
|
|
|
|
|
|
opterr = 0;
|
|
|
|
cmdName = basename(argv[0]);
|
2015-11-18 19:47:29 +00:00
|
|
|
while ((c = getopt(argc, argv, "b:n:r:o:f:")) != -1)
|
2014-02-17 18:50:26 +00:00
|
|
|
{
|
|
|
|
switch (c)
|
|
|
|
{
|
2014-06-04 13:40:19 +01:00
|
|
|
case 'b':
|
|
|
|
binSize = strTo<Index>(optarg);
|
|
|
|
break;
|
2014-02-17 18:50:26 +00:00
|
|
|
case 'n':
|
2014-02-26 18:36:29 +00:00
|
|
|
nSample = strTo<Index>(optarg);
|
2014-02-17 18:50:26 +00:00
|
|
|
break;
|
2014-10-21 17:36:02 +01:00
|
|
|
case 'o':
|
|
|
|
outDirName = optarg;
|
|
|
|
break;
|
2014-02-17 18:50:26 +00:00
|
|
|
case 'r':
|
|
|
|
stateFileName = optarg;
|
|
|
|
break;
|
2015-11-18 19:47:29 +00:00
|
|
|
case 'f':
|
|
|
|
ext = optarg;
|
|
|
|
break;
|
2014-02-17 18:50:26 +00:00
|
|
|
case '?':
|
|
|
|
cerr << "error parsing option -" << char(optopt) << endl;
|
|
|
|
usage(cmdName);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage(cmdName);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (argc - optind == 2)
|
|
|
|
{
|
2014-06-04 13:40:19 +01:00
|
|
|
manFileName = argv[optind];
|
|
|
|
nameFileName = argv[optind+1];
|
2014-02-17 18:50:26 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
usage(cmdName);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
RandGen g;
|
|
|
|
RandGenState state;
|
|
|
|
|
|
|
|
cout << "-- resampling data..." << endl;
|
2014-02-17 18:50:26 +00:00
|
|
|
if (!stateFileName.empty())
|
|
|
|
{
|
2015-10-01 00:13:34 +01:00
|
|
|
state = Io::load<RandGenState>(stateFileName);
|
2014-02-17 18:50:26 +00:00
|
|
|
}
|
2014-06-04 13:40:19 +01:00
|
|
|
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);
|
|
|
|
if (!stateFileName.empty())
|
|
|
|
{
|
|
|
|
g.setState(state);
|
|
|
|
}
|
|
|
|
s = data[name[i]].bootstrapMean(nSample, g);
|
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;
|
|
|
|
}
|