From 49c021fe0202ed0f54a208c52e3dd1d809821dc1 Mon Sep 17 00:00:00 2001
From: "Joachim Wuttke (o)" <j.wuttke@fz-juelich.de>
Date: Mon, 14 Dec 2020 17:54:17 +0100
Subject: [PATCH] Proper error msg from Python/Std/Check

---
 Device/Data/DataUtils.cpp               |  9 +++++++--
 Device/Histo/IntensityDataIOFactory.cpp |  7 +++----
 Tests/Functional/Python/Std/Check.cpp   | 25 +++++++++++++++----------
 Wrap/Python/plot_utils.py               |  6 +++---
 4 files changed, 28 insertions(+), 19 deletions(-)

diff --git a/Device/Data/DataUtils.cpp b/Device/Data/DataUtils.cpp
index 57947965f60..bc48648cac0 100644
--- a/Device/Data/DataUtils.cpp
+++ b/Device/Data/DataUtils.cpp
@@ -33,6 +33,9 @@ std::vector<std::vector<double>> FT2DArray(const std::vector<std::vector<double>
 //! Returns relative difference between two data sets sum(dat[i] - ref[i])/ref[i]).
 double DataUtils::relativeDataDifference(const OutputData<double>& dat,
                                          const OutputData<double>& ref) {
+    std::cerr << "DEBUG rDD" << std::endl;
+    std::cerr << "DEBUG rDD ref rank=" << ref.rank() << ", size=" << ref.getAllocatedSize() << std::endl;
+    std::cerr << "DEBUG rDD dat rank=" << dat.rank() << ", size=" << dat.getAllocatedSize() << std::endl;
     if (!dat.hasSameDimensions(ref))
         throw std::runtime_error("OutputData dimension differs from reference");
 
@@ -48,15 +51,17 @@ double DataUtils::relativeDataDifference(const OutputData<double>& dat,
 //! Returns true is relative difference is below threshold; prints informative output
 bool DataUtils::checkRelativeDifference(const OutputData<double>& dat,
                                         const OutputData<double>& ref, const double threshold) {
+    std::cerr << "DEBUG cRD" << std::endl;
     const double diff = relativeDataDifference(dat, ref);
+    std::cerr << "DEBUG cRD -> " << diff << std::endl;
     if (diff > threshold) {
         std::cerr << "FAILED: relative deviation of dat from ref is " << diff
-                  << ", above given threshold " << threshold << "\n";
+                  << ", above given threshold " << threshold << std::endl;
         return false;
     }
     if (diff)
         std::cerr << "- OK: relative deviation of dat from ref is " << diff
-                  << ", within given threshold " << threshold << "\n";
+                  << ", within given threshold " << threshold << std::endl;
     else
         std::cout << "- OK: dat = ref\n";
     return true;
diff --git a/Device/Histo/IntensityDataIOFactory.cpp b/Device/Histo/IntensityDataIOFactory.cpp
index 0694e2a1aa2..e0054c25ab2 100644
--- a/Device/Histo/IntensityDataIOFactory.cpp
+++ b/Device/Histo/IntensityDataIOFactory.cpp
@@ -40,11 +40,10 @@ OutputData<double>* IntensityDataIOFactory::readOutputData(const std::string& fi
         return readOutputData(
             file_name, [](std::istream& s) { return OutputDataReadWriteINT().readOutputData(s); });
 #ifdef BORNAGAIN_TIFF_SUPPORT
-    else if (DataFormatUtils::isTiffFile(file_name))
+    if (DataFormatUtils::isTiffFile(file_name))
         return readOutputData(
             file_name, [](std::istream& s) { return OutputDataReadWriteTiff().readOutputData(s); });
-#endif // BORNAGAIN_TIFF_SUPPORT
-
+#endif
     // Try to read ASCII by default. Binary maps to ASCII.
     // If the file is not actually a matrix of numbers,
     // the error will be thrown during the reading.
@@ -74,7 +73,7 @@ void IntensityDataIOFactory::writeOutputData(const OutputData<double>& data,
         writeOutputData(file_name, [&](std::ostream& s) {
             OutputDataReadWriteTiff().writeOutputData(data, s);
         });
-#endif // BORNAGAIN_TIFF_SUPPORT
+#endif
     else
         writeOutputData(file_name, [&](std::ostream& s) {
             OutputDataReadWriteNumpyTXT().writeOutputData(data, s);
diff --git a/Tests/Functional/Python/Std/Check.cpp b/Tests/Functional/Python/Std/Check.cpp
index 4ca0553dd91..f593da72305 100644
--- a/Tests/Functional/Python/Std/Check.cpp
+++ b/Tests/Functional/Python/Std/Check.cpp
@@ -35,9 +35,10 @@ std::unique_ptr<OutputData<double>> domainData(const std::string& test_name,
     // Generate Python script
     const std::string pyscript_filename =
         FileSystemUtils::jointPath(BATesting::TestOutDir_PyStd(), test_name + ".py");
-    std::ofstream pythonFile(pyscript_filename);
-    pythonFile << ExportToPython::generateSimulationCode(direct_simulation);
-    pythonFile.close();
+    std::ofstream f(pyscript_filename);
+    f << ExportToPython::generateSimulationCode(direct_simulation);
+    f.close();
+    std::cout << "- wrote Python script " << pyscript_filename << std::endl;
 
     // Run Python script
     const std::string py_command = pyscript_filename + " " + output_path;
@@ -51,14 +52,16 @@ std::unique_ptr<OutputData<double>> domainData(const std::string& test_name,
                                     + BABuild::pythonExecutable() + "\" -B " + py_command;
 #endif
     std::cout << "- system call: " << sys_command << std::endl;
-    int ret = std::system(sys_command.c_str());
-    if (ret != 0) {
-        std::stringstream msg;
-        msg << "System call returned non-zero value " << ret;
-        throw std::runtime_error(msg.str());
-    }
+    int err = std::system(sys_command.c_str());
+    std::cout << "- system call returned " << err << std::endl;
+    if (err)
+        throw std::runtime_error("Exported Python script did not execute properly");
 
-    return std::unique_ptr<OutputData<double>>(IntensityDataIOFactory::readOutputData(output_path));
+    auto ret = std::unique_ptr<OutputData<double>>(
+        IntensityDataIOFactory::readOutputData(output_path));
+    if (!ret)
+        throw std::runtime_error("Could not read back simulation output from file " + output_path);
+    return ret;
 }
 
 } // namespace
@@ -70,7 +73,9 @@ bool checkSimulation(const std::string& name, const ISimulation& direct_simulati
     std::cout << "PyStd test: checkSimulation(" << name << ")" << std::endl;
 
     const std::unique_ptr<OutputData<double>> domain_data = domainData(name, direct_simulation);
+    std::cout << "- got domain data" << std::endl;
     const std::unique_ptr<OutputData<double>> ref_data = direct_simulation.result().data();
+    std::cout << "- ran simulation" << std::endl;
 
     return DataUtils::checkRelativeDifference(*domain_data, *ref_data, limit);
 }
diff --git a/Wrap/Python/plot_utils.py b/Wrap/Python/plot_utils.py
index c30624191d4..dab5dcabc32 100644
--- a/Wrap/Python/plot_utils.py
+++ b/Wrap/Python/plot_utils.py
@@ -204,9 +204,7 @@ def plot_simulation_result(result, **kwargs):
     :param units: units for plot axes
     :param noshow: don't plot to interactive device
     """
-    ns = "NOSHOW" in os.environ
-    noshow = kwargs.pop('noshow', ns)
-    print(f'DEBUG ns={ns}, noshow={noshow}, kwargs={kwargs}')
+    noshow = kwargs.pop('noshow', "NOSHOW" in os.environ)
 
     if len(result.array().shape) == 1:  # 1D data, specular simulation assumed
         plot_specular_simulation_result(result, **kwargs)
@@ -215,6 +213,8 @@ def plot_simulation_result(result, **kwargs):
     plt.tight_layout()
     if not (noshow):
         plt.show()
+    else:
+        print("plot_simulation_result: noshow")
 
 def run_and_plot(simulation, **kwargs):
     simulation.runSimulation()
-- 
GitLab