1
0
mirror of https://github.com/aportelli/LatAnalyze.git synced 2025-04-11 03:20:46 +01:00

More general sample-element util

This commit is contained in:
Antonin Portelli 2017-10-10 16:56:13 +01:00
parent 98cf39efda
commit 839159831f

View File

@ -1,39 +1,103 @@
#include <iostream> /*
#include <string> * sample-element.cpp, part of LatAnalyze 3
*
* Copyright (C) 2013 - 2016 Antonin Portelli, Matt Spraggs
*
* 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 <LatCore/OptParser.hpp>
#include <LatAnalyze/Io.hpp> #include <LatAnalyze/Io.hpp>
#include <LatAnalyze/MatSample.hpp> #include <LatAnalyze/MatSample.hpp>
using namespace std;
using namespace Latan;
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
using namespace std; // argument parsing ////////////////////////////////////////////////////////
using namespace Latan; OptParser opt;
bool parsed;
if (argc != 4 and argc != 5) { string inFilename, outFilename;
cout << "Usage: " << argv[0] << " <input filename> <row> <column> "; Index r, c, nr, nc;
cout << "[output filename]" << endl;
return -1; opt.addOption("r", "row", OptParser::OptType::value , false,
"row");
opt.addOption("c", "col", OptParser::OptType::value , false,
"column");
opt.addOption("" , "nrow", OptParser::OptType::value , true,
"number of rows (default: 1)", "1");
opt.addOption("" , "ncol", OptParser::OptType::value , true,
"number of columns (default: 1)", "1");
opt.addOption("o", "output", OptParser::OptType::value , true,
"output file name (default: result not saved)", "");
opt.addOption("" , "help" , OptParser::OptType::trigger, true,
"show this help message and exit");
parsed = opt.parse(argc, argv);
if (!parsed or (opt.getArgs().size() != 1) or opt.gotOption("help"))
{
cerr << "usage: " << argv[0];
cerr << " <options> <input file>" << endl;
cerr << endl << "Possible options:" << endl << opt << endl;
return EXIT_FAILURE;
} }
inFilename = opt.getArgs()[0];
string inFileName = argv[1]; outFilename = opt.optionValue("o");
auto row = strTo<Index>(argv[2]); r = opt.optionValue<Index>("r");
auto col = strTo<Index>(argv[3]); c = opt.optionValue<Index>("c");
string outFileName = (argc == 5) ? argv[4] : ""; nr = opt.optionValue<Index>("nrow");
nc = opt.optionValue<Index>("ncol");
auto inputData = Io::load<DMatSample>(inFileName);
cout << scientific; // Data extraction /////////////////////////////////////////////////////////
cout << "central value:\n" << inputData[central](row, col) << endl; auto inputData = Io::load<DMatSample>(inFilename);
cout << "standard deviation:\n";
cout << inputData.variance().cwiseSqrt()(row, col) << endl; if ((nr == 1) and (nc == 1))
if (not outFileName.empty())
{ {
DSample outputData(inputData.size()); DSample outputData(inputData.size());
FOR_STAT_ARRAY(inputData, s) {
outputData[s] = inputData[s](row, col); FOR_STAT_ARRAY(inputData, s)
{
outputData[s] = inputData[s](r, c);
}
cout << scientific;
cout << "central value:\n" << outputData[central] << endl;
cout << "standard deviation:\n";
cout << sqrt(outputData.variance()) << endl;
if (not outFilename.empty())
{
Io::save(outputData, outFilename);
} }
Io::save(outputData, outFileName);
} }
else
{
DMatSample outputData(inputData.size(), nr, nc);
FOR_STAT_ARRAY(inputData, s)
{
outputData[s] = inputData[s].block(r, c, nr, nc);
}
cout << scientific;
cout << "central value:\n" << outputData[central] << endl;
cout << "standard deviation:\n";
cout << outputData.variance().cwiseSqrt() << endl;
if (not outFilename.empty())
{
Io::save(outputData, outFilename);
}
}
return EXIT_SUCCESS;
} }