1
0
Fork 0
mirror of https://github.com/gwm17/Specter.git synced 2024-11-22 18:28:52 -05:00

Add Variable to serialization. Fix some Serializer jank. Change save file extension to .yaml to reflect actual file type.

This commit is contained in:
Gordon McCann 2022-11-15 21:15:15 -05:00
parent 74e6c1d122
commit 6c5b7d17a7
5 changed files with 50 additions and 8 deletions

View File

@ -390,6 +390,18 @@ namespace Specter {
var.m_pdata = m_varMap[var.GetName()]; var.m_pdata = m_varMap[var.GetName()];
} }
//Only for use with SpectrumSerializer
double SpectrumManager::GetVariableData(const std::string& variable)
{
std::scoped_lock<std::mutex> guard(m_managerMutex);
auto iter = m_varMap.find(variable);
if (iter != m_varMap.end())
{
return iter->second->value;
}
return 0.0;
}
std::vector<std::string> SpectrumManager::GetListOfVariables() std::vector<std::string> SpectrumManager::GetListOfVariables()
{ {
std::scoped_lock<std::mutex> guard(m_managerMutex); std::scoped_lock<std::mutex> guard(m_managerMutex);

View File

@ -81,6 +81,7 @@ namespace Specter {
/*Variable Functions*/ /*Variable Functions*/
void BindVariable(Variable& var); void BindVariable(Variable& var);
double GetVariableData(const std::string& variableName); //Only for use with SpectrumSerializer
std::vector<std::string> GetListOfVariables(); std::vector<std::string> GetListOfVariables();
/********************/ /********************/

View File

@ -18,7 +18,7 @@
namespace Specter { namespace Specter {
static void SerializeCut(YAML::Emitter& output, const std::shared_ptr<SpectrumManager>& manager, const CutArgs& args) static void SerializeCut(YAML::Emitter& output, const SpectrumManager::Ref& manager, const CutArgs& args)
{ {
output << YAML::BeginMap; output << YAML::BeginMap;
output << YAML::Key << "Cut" << YAML::Value << args.name; output << YAML::Key << "Cut" << YAML::Value << args.name;
@ -49,7 +49,7 @@ namespace Specter {
output << YAML::EndMap; output << YAML::EndMap;
} }
static void SerializeHistogram(YAML::Emitter& output, const std::shared_ptr<SpectrumManager>& manager, const HistogramArgs& args) static void SerializeHistogram(YAML::Emitter& output, const SpectrumManager::Ref& manager, const HistogramArgs& args)
{ {
output << YAML::BeginMap; output << YAML::BeginMap;
output << YAML::Key << "Histogram" << YAML::Value << args.name; output << YAML::Key << "Histogram" << YAML::Value << args.name;
@ -73,6 +73,14 @@ namespace Specter {
} }
static void SerializeVariable(YAML::Emitter& output, const SpectrumManager::Ref& manager, const std::string& variable)
{
output << YAML::BeginMap;
output << YAML::Key << "Variable" << YAML::Value << variable;
output << YAML::Key << "Value" << YAML::Value << manager->GetVariableData(variable);
output << YAML::EndMap;
}
SpectrumSerializer::SpectrumSerializer(const std::string& filepath) : SpectrumSerializer::SpectrumSerializer(const std::string& filepath) :
m_filename(filepath) m_filename(filepath)
{ {
@ -80,7 +88,7 @@ namespace Specter {
SpectrumSerializer::~SpectrumSerializer() {} SpectrumSerializer::~SpectrumSerializer() {}
void SpectrumSerializer::SerializeData(const SpectrumManager::Ref& manager, const std::vector<HistogramArgs>& histoList, const std::vector<CutArgs>& cutList) void SpectrumSerializer::SerializeData(const SpectrumManager::Ref& manager)
{ {
std::ofstream output(m_filename); std::ofstream output(m_filename);
if (!output.is_open()) if (!output.is_open())
@ -89,8 +97,13 @@ namespace Specter {
return; return;
} }
auto cutList = manager->GetListOfCuts();
auto histoList = manager->GetListOfHistograms();
auto varList = manager->GetListOfVariables();
YAML::Emitter yamlStream; YAML::Emitter yamlStream;
yamlStream << YAML::BeginMap; yamlStream << YAML::BeginMap;
yamlStream << YAML::Key << "Cuts" << YAML::Value << YAML::BeginSeq; yamlStream << YAML::Key << "Cuts" << YAML::Value << YAML::BeginSeq;
for (auto& cut : cutList) for (auto& cut : cutList)
{ {
@ -102,6 +115,12 @@ namespace Specter {
{ {
SerializeHistogram(yamlStream, manager, histo); SerializeHistogram(yamlStream, manager, histo);
} }
yamlStream << YAML::EndSeq;
yamlStream << YAML::Key << "Variables" << YAML::Value << YAML::BeginSeq;
for (auto& var : varList)
{
SerializeVariable(yamlStream, manager, var);
}
yamlStream << YAML::EndSeq << YAML::EndMap; yamlStream << YAML::EndSeq << YAML::EndMap;
output << yamlStream.c_str(); output << yamlStream.c_str();
@ -109,7 +128,7 @@ namespace Specter {
output.close(); output.close();
} }
void SpectrumSerializer::DeserializeData(const SpectrumManager::Ref& manager) void SpectrumSerializer::DeserializeData(SpectrumManager::Ref& manager)
{ {
manager->RemoveAllSpectra(); //When loading in, we remove all extant data, to avoid any potential collisions. manager->RemoveAllSpectra(); //When loading in, we remove all extant data, to avoid any potential collisions.
@ -177,6 +196,16 @@ namespace Specter {
} }
} }
} }
auto vars = data["Variables"];
if (vars)
{
for (const auto& var : vars)
{
Variable tempVar(var["Variable"].as<std::string>());
manager->BindVariable(tempVar);
tempVar.SetValue(var["Value"].as<double>());
}
}
SPEC_INFO("Successfully loaded data from {0}", m_filename); SPEC_INFO("Successfully loaded data from {0}", m_filename);
} }

View File

@ -26,8 +26,8 @@ namespace Specter {
SpectrumSerializer(const std::string& filepath); SpectrumSerializer(const std::string& filepath);
~SpectrumSerializer(); ~SpectrumSerializer();
void SerializeData(const SpectrumManager::Ref& manager, const std::vector<HistogramArgs>& histoList, const std::vector<CutArgs>& cutList); void SerializeData(const SpectrumManager::Ref& manager);
void DeserializeData(const SpectrumManager::Ref& manager); void DeserializeData(SpectrumManager::Ref& manager);
inline const std::string& GetFilename() { return m_filename; } inline const std::string& GetFilename() { return m_filename; }

View File

@ -197,7 +197,7 @@ namespace Specter {
} }
//Render all of our sub-windows, dialogs, panels, etc //Render all of our sub-windows, dialogs, panels, etc
auto fd_result = m_fileDialog.RenderFileDialog(".spec"); auto fd_result = m_fileDialog.RenderFileDialog(".yaml");
if (!fd_result.first.empty()) if (!fd_result.first.empty())
{ {
switch (fd_result.second) switch (fd_result.second)
@ -214,7 +214,7 @@ namespace Specter {
{ {
SPEC_INFO("Found a Save File! {0}", fd_result.first); SPEC_INFO("Found a Save File! {0}", fd_result.first);
SpectrumSerializer serializer(fd_result.first); SpectrumSerializer serializer(fd_result.first);
serializer.SerializeData(m_manager, m_histoList, m_cutList); serializer.SerializeData(m_manager);
break; break;
} }
} }