2014-02-12 18:39:17 +00:00
|
|
|
/*
|
|
|
|
* Dataset.hpp, 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef Latan_Dataset_hpp_
|
|
|
|
#define Latan_Dataset_hpp_
|
|
|
|
|
2014-03-13 18:51:01 +00:00
|
|
|
#include <LatAnalyze/Global.hpp>
|
|
|
|
#include <LatAnalyze/File.hpp>
|
|
|
|
#include <LatAnalyze/StatArray.hpp>
|
|
|
|
#include <LatAnalyze/RandGen.hpp>
|
2014-02-12 18:39:17 +00:00
|
|
|
#include <fstream>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
BEGIN_NAMESPACE
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* Dataset class *
|
|
|
|
******************************************************************************/
|
2014-02-20 23:52:45 +00:00
|
|
|
template <typename T>
|
2014-02-12 18:39:17 +00:00
|
|
|
class Dataset: public StatArray<T>
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
typedef StatArray<T> Base;
|
|
|
|
public:
|
2014-02-20 22:54:11 +00:00
|
|
|
// constructors
|
2014-03-03 14:21:37 +00:00
|
|
|
Dataset(void) = default;
|
2014-02-20 23:52:45 +00:00
|
|
|
template <typename FileType>
|
2014-02-12 18:39:17 +00:00
|
|
|
Dataset(const std::string &listFileName, const std::string &dataName);
|
2014-03-03 14:21:37 +00:00
|
|
|
using Base::Base;
|
2014-02-12 18:39:17 +00:00
|
|
|
// destructor
|
2014-02-20 22:54:11 +00:00
|
|
|
virtual ~Dataset(void) = default;
|
2014-02-12 18:39:17 +00:00
|
|
|
// IO
|
2014-02-20 23:52:45 +00:00
|
|
|
template <typename FileType>
|
2014-02-12 18:39:17 +00:00
|
|
|
void load(const std::string &listFileName, const std::string &dataName);
|
|
|
|
// resampling
|
2014-02-26 18:36:29 +00:00
|
|
|
Sample<T> bootstrapMean(const Index nSample, RandGen& generator);
|
2014-02-12 18:39:17 +00:00
|
|
|
private:
|
|
|
|
// mean from pointer vector for resampling
|
|
|
|
void ptVectorMean(T &m, const std::vector<const T *> &v);
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* Dataset template implementation *
|
|
|
|
******************************************************************************/
|
|
|
|
// constructor /////////////////////////////////////////////////////////////////
|
2014-02-20 23:52:45 +00:00
|
|
|
template <typename T>
|
|
|
|
template <typename FileType>
|
|
|
|
Dataset<T>::Dataset(const std::string &listFileName,
|
|
|
|
const std::string &dataName)
|
2014-02-12 18:39:17 +00:00
|
|
|
{
|
2014-02-20 23:52:45 +00:00
|
|
|
load<FileType>(listFileName, dataName);
|
2014-02-12 18:39:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IO //////////////////////////////////////////////////////////////////////////
|
2014-02-20 23:52:45 +00:00
|
|
|
template <typename T>
|
|
|
|
template <typename FileType>
|
|
|
|
void Dataset<T>::load(const std::string &listFileName,
|
|
|
|
const std::string &dataName)
|
2014-02-12 18:39:17 +00:00
|
|
|
{
|
2014-02-20 23:52:45 +00:00
|
|
|
FileType file;
|
2014-02-12 18:39:17 +00:00
|
|
|
std::ifstream listFile;
|
|
|
|
char dataFileNameBuf[MAX_PATH_LENGTH];
|
|
|
|
std::vector<std::string> dataFileName;
|
|
|
|
|
|
|
|
listFile.open(listFileName, std::ios::in);
|
|
|
|
while (!listFile.eof())
|
|
|
|
{
|
|
|
|
listFile.getline(dataFileNameBuf, MAX_PATH_LENGTH);
|
|
|
|
if (!std::string(dataFileNameBuf).empty())
|
|
|
|
{
|
|
|
|
dataFileName.push_back(dataFileNameBuf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
listFile.close();
|
|
|
|
this->resize(dataFileName.size());
|
2014-02-26 18:36:29 +00:00
|
|
|
for (Index i = 0; i < static_cast<Index>(dataFileName.size()); ++i)
|
2014-02-12 18:39:17 +00:00
|
|
|
{
|
2014-02-20 23:52:45 +00:00
|
|
|
file.open(dataFileName[i], File::Mode::read);
|
|
|
|
(*this)[i] = file.template read<T>(dataName);
|
|
|
|
file.close();
|
2014-02-12 18:39:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// resampling //////////////////////////////////////////////////////////////////
|
2014-02-20 23:52:45 +00:00
|
|
|
template <typename T>
|
2014-02-26 18:36:29 +00:00
|
|
|
Sample<T> Dataset<T>::bootstrapMean(const Index nSample, RandGen& generator)
|
2014-02-12 18:39:17 +00:00
|
|
|
{
|
2014-02-26 18:36:29 +00:00
|
|
|
Index nData = this->size();
|
2014-02-12 18:39:17 +00:00
|
|
|
std::vector<const T *> data(nData);
|
|
|
|
Sample<T> s(nSample);
|
|
|
|
|
2014-02-26 18:36:29 +00:00
|
|
|
for (Index j = 0; j < nData; ++j)
|
2014-02-12 18:39:17 +00:00
|
|
|
{
|
|
|
|
data[j] = &((*this)[j]);
|
|
|
|
}
|
|
|
|
ptVectorMean(s[central], data);
|
2014-02-26 18:36:29 +00:00
|
|
|
for (Index i = 0; i < nSample; ++i)
|
2014-02-12 18:39:17 +00:00
|
|
|
{
|
2014-02-26 18:36:29 +00:00
|
|
|
for (Index j = 0; j < nData; ++j)
|
2014-02-12 18:39:17 +00:00
|
|
|
{
|
|
|
|
data[j] = &((*this)[generator.discreteUniform(nData)]);
|
|
|
|
}
|
|
|
|
ptVectorMean(s[i], data);
|
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2014-02-20 23:52:45 +00:00
|
|
|
template <typename T>
|
|
|
|
void Dataset<T>::ptVectorMean(T &m, const std::vector<const T *> &v)
|
2014-02-12 18:39:17 +00:00
|
|
|
{
|
|
|
|
if (v.size())
|
|
|
|
{
|
|
|
|
m = *(v[0]);
|
|
|
|
for (unsigned int i = 1; i < v.size(); ++i)
|
|
|
|
{
|
|
|
|
m += *(v[i]);
|
|
|
|
}
|
|
|
|
m /= static_cast<double>(v.size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
END_NAMESPACE
|
|
|
|
|
|
|
|
#endif // Latan_Dataset_hpp_
|