From 7b617574967dab833879c861d595e38b94a5a67c Mon Sep 17 00:00:00 2001 From: "Joachim Wuttke (l)" <j.wuttke@fz-juelich.de> Date: Sun, 7 Aug 2016 10:45:23 +0200 Subject: [PATCH] persistence test machinery now prepared to handle aribtrary file extensions --- .../SpecularSimulation.py | 5 +- .../PyCore/persistence/PyPersistenceTest.cpp | 28 ++++++---- Wrap/python/plot_utils.py | 51 +++++++++++++++---- 3 files changed, 61 insertions(+), 23 deletions(-) diff --git a/Examples/python/simulation/ex05_BeamAndDetector/SpecularSimulation.py b/Examples/python/simulation/ex05_BeamAndDetector/SpecularSimulation.py index 716ddb72628..93f2e075281 100644 --- a/Examples/python/simulation/ex05_BeamAndDetector/SpecularSimulation.py +++ b/Examples/python/simulation/ex05_BeamAndDetector/SpecularSimulation.py @@ -97,8 +97,9 @@ def plot(simulation): plt.show() -def save(simulation, filename): - print("Not yet saving anything") +def save(filename, simulation): + ba.yamlDump(filename, "Simulation passed") + if __name__ == '__main__': ba.simulateThenPlotOrSave(simulate, plot, save) diff --git a/Tests/Functional/PyCore/persistence/PyPersistenceTest.cpp b/Tests/Functional/PyCore/persistence/PyPersistenceTest.cpp index 3dfe9d0eeac..d274a6c9d11 100644 --- a/Tests/Functional/PyCore/persistence/PyPersistenceTest.cpp +++ b/Tests/Functional/PyCore/persistence/PyPersistenceTest.cpp @@ -39,7 +39,7 @@ void PyPersistenceTest::runTest() { // Set output data filename stem, and remove old output files std::string dat_stem = Utils::FileSystem::GetJoinPath(PYPERSIST_TMP_DIR, getName()); - for (const std::string& fname: Utils::FileSystem::glob(dat_stem+".*.int")) { + for (const std::string& fname: Utils::FileSystem::glob(dat_stem+".*.*")) { std::remove( fname.c_str() ); std::cout << "Removed old result " << fname.c_str() << "." << std::endl/*sic*/; } @@ -58,11 +58,12 @@ void PyPersistenceTest::runTest() } // Read back simulation results - std::map<const std::string, const OutputData<double>*> dat; - std::string dat_pattern = dat_stem + ".*.int"; + std::map<const std::string, const std::string> dat; + std::string dat_pattern = dat_stem + ".*.*"; for (const std::string& fname: Utils::FileSystem::glob(dat_pattern)) - dat.insert(make_pair(Utils::String::split(fname,".")[1], - IntensityDataIOFactory::readOutputData( fname ))); + dat.insert(make_pair(Utils::String::split(fname,".")[1]+"."+ + Utils::String::split(fname,".")[2], + fname)); if (dat.size()==0) { std::cerr << "There is no test output of form " << dat_pattern << "\n"; m_result = FAILED; @@ -72,23 +73,24 @@ void PyPersistenceTest::runTest() // Read reference files std::string ref_stem = Utils::FileSystem::GetJoinPath(PYPERSIST_REF_DIR, getName()); - std::map<const std::string, const OutputData<double>*> ref; - for (const std::string& fname: Utils::FileSystem::glob(ref_stem+".*.int.gz")) - ref.insert(make_pair(Utils::String::split(fname,".")[1], - IntensityDataIOFactory::readOutputData( fname ))); + std::map<const std::string, const std::string> ref; + for (const std::string& fname: Utils::FileSystem::glob(ref_stem+".*.*.gz")) + ref.insert(make_pair(Utils::String::split(fname,".")[1]+"."+ + Utils::String::split(fname,".")[2], + fname)); // Compare file lists m_result = SUCCESS; for( auto const& it: dat ) { if( ref.find(it.first)==ref.end() ) { - std::cerr << "For test output " << (dat_stem+"."+it.first+".int") + std::cerr << "For test output " << it.second << " there is no reference file in " << PYPERSIST_REF_DIR << "\n"; m_result = FAILED; } } for( auto const& it: ref ) { if( dat.find(it.first)==dat.end() ) { - std::cerr << "For reference file " << (ref_stem+"."+it.first+".int.gz") + std::cerr << "For reference file " << it.second << " there is no test output in " << PYPERSIST_TMP_DIR << "\n"; m_result = FAILED; } @@ -96,6 +98,10 @@ void PyPersistenceTest::runTest() if (m_result==FAILED) return; + +// const OutputData<double>* dat = IntensityDataIOFactory::readOutputData( fname ); + + /* const OutputData<double>* reference; try { diff --git a/Wrap/python/plot_utils.py b/Wrap/python/plot_utils.py index ea96b4ce57f..5cb22bdb91b 100644 --- a/Wrap/python/plot_utils.py +++ b/Wrap/python/plot_utils.py @@ -36,17 +36,41 @@ def standardIntensityPlot(result): plt.ylabel(r'$\alpha_f (^{\circ})$', fontsize=16) plt.show() -def standardIntensitySave(result, filename): + +def standardIntensitySave(filename, result): """ Saves simulation result, which must be in an intensity map, or a dictionary of such maps. """ - if type(result) is dict: - for name,data in result.iteritems(): - ba.IntensityDataIOFactory.writeIntensityData( - data, filename+"."+name+".int") - else: - ba.IntensityDataIOFactory.writeIntensityData(result, filename+".ref.int") + ba.IntensityDataIOFactory.writeIntensityData(result, filename+".int") + + +def yamlDump(filename, data): + """ + Saves an arbitrary hierarchical data set as YAML-formatted text file. + """ +# import yaml +# global yaml +# class ImprovedDumper(Dumper): +# pass +# def odict_representer(dumper, data): +# return dumper.represent_mapping( +# yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, +# data.items()) +# def flowseq_representer(dumper, data): +# return dumper.represent_sequence( +# yaml.resolver.BaseResolver.DEFAULT_SEQUENCE_TAG, +# data, +# flow_style=True ) +# ImprovedDumper.add_representer(OrderedDict, odict_representer) +# ImprovedDumper.add_representer(FlowSeq, flowseq_representer) + with open(filename+".yaml", "w") as f: + f.write(data) +# TODO +# f.write(yaml.dump(data, None, ImprovedDumper, +# allow_unicode=True, encoding='utf-8', +# default_flow_style=False, indent=4, width=70)) + def getFilenameOrPlotflag(): """ @@ -61,6 +85,7 @@ def getFilenameOrPlotflag(): sys.exit() return sys.argv[1] + def simulateThenPlotOrSave( simulate, plot=standardIntensityPlot, save=standardIntensitySave): """ @@ -69,10 +94,16 @@ def simulateThenPlotOrSave( """ arg = getFilenameOrPlotflag() result = simulate() - if arg != '-p': - save(result, arg) - else: + if arg == '-p': plot(result) + return + # save result + if type(result) is dict: + for name, subresult in result.iteritems(): + save(arg+"."+name, subresult) + else: + save(arg+".ref", result) + class DefaultFitObserver(IFitObserver): """ -- GitLab