1
0
mirror of https://github.com/aportelli/LatAnalyze.git synced 2024-11-10 00:45:36 +00:00

more general resampling tool

This commit is contained in:
Antonin Portelli 2014-06-04 14:40:19 +02:00
parent c9a008aaf7
commit 5d835b92e2

View File

@ -1,4 +1,24 @@
/*
* resample.cpp, part of LatAnalyze 3
*
* Copyright (C) 2013 - 2014 Antonin Portelli
*
* 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/>.
*/
#include <iostream> #include <iostream>
#include <map>
#include <libgen.h> #include <libgen.h>
#include <unistd.h> #include <unistd.h>
#include <LatAnalyze/AsciiFile.hpp> #include <LatAnalyze/AsciiFile.hpp>
@ -13,34 +33,35 @@ using namespace Latan;
static void usage(const string &cmdName) static void usage(const string &cmdName)
{ {
cerr << "usage: " << cmdName cerr << "usage: " << cmdName;
<< " [-n <nsample> -r <state> -o <output>] <manifest> <name> " cerr << " [-n <nsample> -b <bin size> -r <state>]";
<< endl; cerr << " <data list> <name list>";
cerr << endl;
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
// argument parsing ////////////////////////////////////////////////////////
int c; int c;
Index nSample = DEF_NSAMPLE; string manFileName, nameFileName, stateFileName, cmdName;
string manFileName, name, outFileName, stateFileName; Index binSize = 1, nSample = DEF_NSAMPLE;
char *cmdName;
opterr = 0; opterr = 0;
cmdName = basename(argv[0]); cmdName = basename(argv[0]);
while ((c = getopt(argc, argv, "n:r:o:")) != -1) while ((c = getopt(argc, argv, "b:n:r:")) != -1)
{ {
switch (c) switch (c)
{ {
case 'b':
binSize = strTo<Index>(optarg);
break;
case 'n': case 'n':
nSample = strTo<Index>(optarg); nSample = strTo<Index>(optarg);
break; break;
case 'r': case 'r':
stateFileName = optarg; stateFileName = optarg;
break; break;
case 'o':
outFileName = optarg;
break;
case '?': case '?':
cerr << "error parsing option -" << char(optopt) << endl; cerr << "error parsing option -" << char(optopt) << endl;
usage(cmdName); usage(cmdName);
@ -53,33 +74,73 @@ int main(int argc, char *argv[])
if (argc - optind == 2) if (argc - optind == 2)
{ {
manFileName = argv[optind]; manFileName = argv[optind];
name = argv[optind+1]; nameFileName = argv[optind+1];
} }
else else
{ {
usage(cmdName); usage(cmdName);
} }
Dataset<DMat> dataset; // parameter parsing ///////////////////////////////////////////////////////
DMatSample s; vector<string> dataFileName, name;
RandGen g;
dataFileName = readManifest(manFileName);
name = readManifest(nameFileName);
cout << "================================================" << endl;
cout << "Data resampler" << endl;
cout << "------------------------------------------------" << endl;
cout << " #file= " << dataFileName.size() << endl;
cout << " #name= " << name.size() << endl;
cout << " bin size= " << binSize << endl;
cout << " #sample= " << nSample << endl;
cout << "------------------------------------------------" << endl;
// 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)
{
AsciiFile dataFile;
cout << '\r' << ProgressBar(i + 1, dataFileName.size());
dataFile.open(dataFileName[i], AsciiFile::Mode::read);
for (const string &n: name)
{
data[n][i] = dataFile.read<DMat>(n);
}
dataFile.close();
}
cout << endl;
// data resampling /////////////////////////////////////////////////////////
DMatSample s(nSample);
RandGen g;
RandGenState state;
cout << "-- resampling data..." << endl;
if (!stateFileName.empty()) if (!stateFileName.empty())
{ {
AsciiFile f(stateFileName, File::Mode::read); state = Io::load<RandGenState, AsciiFile>(stateFileName);
g.setState(f.read<RandGenState>());
} }
cout << "-- loading data from manifest '" << manFileName << "'..." << endl; for (unsigned int i = 0; i < name.size(); ++i)
dataset.load<AsciiFile>(manFileName, name);
s = dataset.bootstrapMean(nSample, g);
cout << scientific;
cout << "central value:\n" << s[central] << endl;
cout << "standard deviation:\n" << s.variance().cwiseSqrt() << endl;
if (!outFileName.empty())
{ {
Io::save<DMatSample, AsciiFile>(s, outFileName, File::Mode::write, const string outFileName = name[i] + "_" + manFileName + ".boot";
manFileName + "_" + name);
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);
Io::save<DMatSample, AsciiFile>(s, outFileName);
}
cout << endl;
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }