Skip to content
Snippets Groups Projects
Commit c933b28f authored by Carl Philipp Klemm's avatar Carl Philipp Klemm
Browse files

eistype: support writeing to and reading from streams instead of just files

parent 0db20026
Branches
No related tags found
No related merge requests found
...@@ -173,6 +173,7 @@ public: ...@@ -173,6 +173,7 @@ public:
EisSpectra(const std::filesystem::path& path){*this = loadFromDisk(path);} EisSpectra(const std::filesystem::path& path){*this = loadFromDisk(path);}
EisSpectra(){} EisSpectra(){}
static EisSpectra loadFromDisk(const std::filesystem::path& path); static EisSpectra loadFromDisk(const std::filesystem::path& path);
static EisSpectra loadFromStream(std::istream& path);
void setLabel(size_t label, size_t maxLabel); void setLabel(size_t label, size_t maxLabel);
size_t getLabel(); size_t getLabel();
void setSzLabels(std::vector<size_t> label); void setSzLabels(std::vector<size_t> label);
...@@ -182,6 +183,7 @@ public: ...@@ -182,6 +183,7 @@ public:
bool isMulticlass(); bool isMulticlass();
std::vector<fvalue> getFvalueLabels(); std::vector<fvalue> getFvalueLabels();
bool saveToDisk(const std::filesystem::path& path) const; bool saveToDisk(const std::filesystem::path& path) const;
void saveToStream(std::ostream& stream) const;
}; };
bool saveToDisk(const EisSpectra& data, const std::filesystem::path& path); bool saveToDisk(const EisSpectra& data, const std::filesystem::path& path);
......
...@@ -250,35 +250,28 @@ std::vector<fvalue> EisSpectra::getFvalueLabels() ...@@ -250,35 +250,28 @@ std::vector<fvalue> EisSpectra::getFvalueLabels()
} }
} }
bool EisSpectra::saveToDisk(const std::filesystem::path& path) const void EisSpectra::saveToStream(std::ostream& stream) const
{
std::fstream file;
file.open(path, std::ios_base::out | std::ios_base::trunc);
if(!file.is_open())
{ {
Log(Log::ERROR)<<"can not open "<<path<<" for writing\n"; stream<<std::scientific;
return false; stream<<F_MAGIC<<", "<<std::to_string(F_VERSION_MAJOR)<<'.'
}
file<<std::scientific;
file<<F_MAGIC<<", "<<std::to_string(F_VERSION_MAJOR)<<'.'
<<std::to_string(F_VERSION_MINOR)<<'.'<<std::to_string(F_VERSION_PATCH)<<'\n'; <<std::to_string(F_VERSION_MINOR)<<'.'<<std::to_string(F_VERSION_PATCH)<<'\n';
file<<'"'<<model<<'"'<<(!header.empty() ? ", " : ""); stream<<'"'<<model<<'"'<<(!header.empty() ? ", " : "");
file<<header; stream<<header;
if(!labels.empty()) if(!labels.empty())
{ {
if(!labelNames.empty()) if(!labelNames.empty())
{ {
file<<"\nlabelsNames\n"; stream<<"\nlabelsNames\n";
std::string labelLine; std::string labelLine;
for(const std::string& name : labelNames) for(const std::string& name : labelNames)
labelLine += "\"" + name + "\", "; labelLine += "\"" + name + "\", ";
labelLine.pop_back(); labelLine.pop_back();
labelLine.pop_back(); labelLine.pop_back();
file<<labelLine; stream<<labelLine;
} }
file<<"\nlabels\n"; stream<<"\nlabels\n";
std::stringstream labelSs; std::stringstream labelSs;
for(double label : labels) for(double label : labels)
...@@ -286,59 +279,68 @@ bool EisSpectra::saveToDisk(const std::filesystem::path& path) const ...@@ -286,59 +279,68 @@ bool EisSpectra::saveToDisk(const std::filesystem::path& path) const
std::string labelLine = labelSs.str(); std::string labelLine = labelSs.str();
labelLine.pop_back(); labelLine.pop_back();
labelLine.pop_back(); labelLine.pop_back();
file<<labelLine; stream<<labelLine;
} }
file<<"\nomega, real, im\n"; stream<<"\nomega, real, im\n";
for(const eis::DataPoint& point : data) for(const eis::DataPoint& point : data)
file<<point.omega<<", "<<point.im.real()<<", "<<point.im.imag()<<'\n'; stream<<point.omega<<", "<<point.im.real()<<", "<<point.im.imag()<<'\n';
file.close();
return true;
} }
EisSpectra EisSpectra::loadFromDisk(const std::filesystem::path& path) bool EisSpectra::saveToDisk(const std::filesystem::path& path) const
{ {
EisSpectra out;
std::fstream file; std::fstream file;
file.open(path, std::ios_base::in); file.open(path, std::ios_base::out | std::ios_base::trunc);
if(!file.is_open()) if(!file.is_open())
throw file_error("can not open " + path.string() + " for reading\n"); {
Log(Log::ERROR)<<"can not open "<<path<<" for writing\n";
return false;
}
saveToStream(file);
file.close();
return true;
}
EisSpectra EisSpectra::loadFromStream(std::istream& stream)
{
EisSpectra out;
std::string line; std::string line;
std::getline(file, line); std::getline(stream, line);
std::vector<std::string> tokens = tokenizeBinaryIgnore(line, ',', '"', '\\'); std::vector<std::string> tokens = tokenizeBinaryIgnore(line, ',', '"', '\\');
if(tokens.size() < 2 || tokens[0] != F_MAGIC) if(tokens.size() < 2 || tokens[0] != F_MAGIC)
{ {
throw file_error(path.string() + " is not a valid EISGenerator file"); throw file_error("not a valid EISGenerator file or stream");
} }
else else
{ {
std::vector<std::string> versionTokens = tokenize(tokens[1], '.'); std::vector<std::string> versionTokens = tokenize(tokens[1], '.');
if(versionTokens.size() != 3 || std::stoi(versionTokens[0]) > F_VERSION_MAJOR || std::stoi(versionTokens[1]) > F_VERSION_MINOR) if(versionTokens.size() != 3 || std::stoi(versionTokens[0]) > F_VERSION_MAJOR || std::stoi(versionTokens[1]) > F_VERSION_MINOR)
throw file_error(path.string() + " was saved by a newer version of EISGenerator, can not open"); throw file_error("saved by a newer version of EISGenerator, can not open");
} }
std::getline(file, line); std::getline(stream, line);
tokens = tokenizeBinaryIgnore(line, ',', '"', '\\'); tokens = tokenizeBinaryIgnore(line, ',', '"', '\\');
stripQuotes(tokens[0]); stripQuotes(tokens[0]);
out.model = tokens[0]; out.model = tokens[0];
line.erase(line.begin(), line.begin()+tokens.size()); line.erase(line.begin(), line.begin()+tokens.size());
out.header = line; out.header = line;
while(file.good()) while(stream.good())
{ {
std::getline(file, line); std::getline(stream, line);
if(line.starts_with("labelsNames")) if(line.starts_with("labelsNames"))
{ {
std::getline(file, line); std::getline(stream, line);
out.labelNames = tokenizeBinaryIgnore(line, ',', '"', '\\'); out.labelNames = tokenizeBinaryIgnore(line, ',', '"', '\\');
continue; continue;
} }
else if(line.starts_with("labels")) else if(line.starts_with("labels"))
{ {
std::getline(file, line); std::getline(stream, line);
std::vector<std::string> tokens = tokenizeBinaryIgnore(line, ',', '"', '\\'); std::vector<std::string> tokens = tokenizeBinaryIgnore(line, ',', '"', '\\');
for(const std::string& token : tokens) for(const std::string& token : tokens)
out.labels.push_back(std::stod(token)); out.labels.push_back(std::stod(token));
...@@ -350,7 +352,7 @@ EisSpectra EisSpectra::loadFromDisk(const std::filesystem::path& path) ...@@ -350,7 +352,7 @@ EisSpectra EisSpectra::loadFromDisk(const std::filesystem::path& path)
} }
tokens = tokenize(line, ','); tokens = tokenize(line, ',');
if(tokens.size() != 3) if(tokens.size() != 3)
throw file_error("invalid line in " + path.string() + ": " + line); throw file_error("invalid line: " + line);
#pragma GCC diagnostic push #pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wnarrowing" #pragma GCC diagnostic ignored "-Wnarrowing"
...@@ -363,6 +365,23 @@ EisSpectra EisSpectra::loadFromDisk(const std::filesystem::path& path) ...@@ -363,6 +365,23 @@ EisSpectra EisSpectra::loadFromDisk(const std::filesystem::path& path)
eis::removeDuplicates(out.data); eis::removeDuplicates(out.data);
} }
file.close();
return out; return out;
} }
EisSpectra EisSpectra::loadFromDisk(const std::filesystem::path& path)
{
std::fstream file;
file.open(path, std::ios_base::in);
if(!file.is_open())
throw file_error("can not open " + path.string() + " for reading\n");
try
{
EisSpectra out = loadFromStream(file);
return out;
}
catch(const file_error& err)
{
throw file_error(path.string() + std::string(": ") + err.what());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment