From b0222df3057f49d14d8e732b5c06a8002679adb3 Mon Sep 17 00:00:00 2001
From: Martin Beseda <martin.beseda@vsb.cz>
Date: Wed, 23 Jan 2019 16:56:18 +0100
Subject: [PATCH] FIX: Fixed output formatting in MSE class

---
 src/ErrorFunction/ErrorFunctions.cpp | 123 ++++++++++++++++++---------
 1 file changed, 84 insertions(+), 39 deletions(-)

diff --git a/src/ErrorFunction/ErrorFunctions.cpp b/src/ErrorFunction/ErrorFunctions.cpp
index 59323bd8..e5bc62cf 100644
--- a/src/ErrorFunction/ErrorFunctions.cpp
+++ b/src/ErrorFunction/ErrorFunctions.cpp
@@ -114,11 +114,22 @@ namespace lib4neuro {
         std::vector<std::vector<double>> outputs(data->size());
         std::vector<double> output(dim_out);
 
-        COUT_DEBUG("Evaluation of the error function MSE on the given data-set (format 'data-set element index' 'input'"
-                   " 'real output' 'predicted output' 'absolute error' 'relative error'):"
+        COUT_DEBUG("Evaluation of the error function MSE on the given data-set" << std::endl);
+        COUT_DEBUG(R_ALIGN << "[Element index]" << " "
+                   << R_ALIGN << "[Input]" << " "
+                   << R_ALIGN << "[Real output]" << " "
+                   << R_ALIGN << "[Predicted output]" << " "
+                   << R_ALIGN << "[Absolute error]" << " "
+                   << R_ALIGN << "[Relative error]"
                    << std::endl);
 
-        *results_file_path << "[Data-set element index] [Input] [Real output] [Predicted output] [Abs. error] [Rel. error]" << std::endl;
+        *results_file_path << R_ALIGN << "[Element index]" << " "
+                           << R_ALIGN << "[Input]" << " "
+                           << R_ALIGN << "[Real output]" << " "
+                           << R_ALIGN << "[Predicted output]" << " "
+                           << R_ALIGN << "[Abs. error]" << " "
+                           << R_ALIGN << "[Rel. error]"
+                           << std::endl;
 
         for (auto i = 0; i < data->size(); i++) {  // Iterate through every element in the test set
             error = 0.0;
@@ -170,27 +181,32 @@ namespace lib4neuro {
             }
 
 #ifdef L4N_DEBUG
-            COUT_DEBUG("[" << i << "] "
-                           << ss_input.str() << " "
-                           << ss_real_output.str() << " "
-                           << ss_predicted_output.str() << " "
-                           << std::sqrt(error) << " "
-                           << 2 * std::sqrt(error) / (std::sqrt(error) + std::sqrt(output_norm))
-                           << std::endl);
-
-            *results_file_path << "["
-                               << i << "] "
-                               << ss_input.str() << " "
-                               << ss_real_output.str() << " "
-                               << ss_predicted_output.str() << " "
-                               << std::sqrt(error) << " "
-                               << 2 * std::sqrt(error) / (std::sqrt(error) + std::sqrt(output_norm)) <<
-                               std::endl;
+            std::stringstream ss_ind;
+            ss_ind << "[" << i << "]";
+
+            COUT_DEBUG(R_ALIGN << ss_ind.str() << " "
+                       << R_ALIGN << ss_input.str() << " "
+                       << R_ALIGN << ss_real_output.str() << " "
+                       << R_ALIGN << ss_predicted_output.str() << " "
+                       << R_ALIGN << std::sqrt(error) << " "
+                       << R_ALIGN << 2 * std::sqrt(error) / (std::sqrt(error) + std::sqrt(output_norm))
+                       << std::endl);
+
+            *results_file_path << R_ALIGN << ss_ind.str() << " "
+                               << R_ALIGN << ss_input.str() << " "
+                               << R_ALIGN << ss_real_output.str() << " "
+                               << R_ALIGN << ss_predicted_output.str() << " "
+                               << R_ALIGN << std::sqrt(error) << " "
+                               << R_ALIGN << 2 * std::sqrt(error) / (std::sqrt(error) + std::sqrt(output_norm))
+                               << std::endl;
 #endif
         }
 
         double result = std::sqrt(error) / n_elements;
+
+        COUT_DEBUG("MSE = " << result << std::endl);
         *results_file_path << "MSE = " << result << std::endl;
+
         return result;
     }
 
@@ -207,16 +223,22 @@ namespace lib4neuro {
         std::vector<std::vector<double>> outputs(data->size());
         std::vector<double> output(dim_out);
 
-        COUT_DEBUG(
-                "Evaluation of the error function MSE on the given data-set (format 'input' 'real output' 'predicted output'):"
-                        << std::endl);
+        COUT_DEBUG("Evaluation of the error function MSE on the given data-set" << std::endl);
+        COUT_DEBUG(R_ALIGN << "[Input]" << " "
+                   << R_ALIGN << "[Real output]" << " "
+                   << R_ALIGN << "[Predicted output]" << " "
+                   << std::endl);
 
         std::ofstream ofs(results_file_path);
         if (!ofs.is_open()) {
             THROW_RUNTIME_ERROR("File path: " + results_file_path + " was not successfully opened!");
         }
 
-        ofs << "[Index ][Input] [Real output] [Predicted output]" << std::endl;
+        ofs << R_ALIGN << "[Index]" << " "
+            << R_ALIGN << "[Input]" << " "
+            << R_ALIGN << "[Real output]" << " "
+            << R_ALIGN << "[Predicted output]"
+            << std::endl;
 
         for (auto i = 0; i < data->size(); i++) {  // Iterate through every element in the test set
 
@@ -243,14 +265,21 @@ namespace lib4neuro {
                 } else {
                     denormalized_output = outputs.at(i).at(j);
                 }
-                COUT_DEBUG("[" << i << "] "
-                               << data->at(i).first.at(j) << " "
-                               << data->at(i).second.at(j) << " "
-                               << denormalized_output << std::endl);
 
-                ofs << data->at(i).first.at(j) << " "
-                    << data->at(i).second.at(j) << " "
-                    << denormalized_output << std::endl;
+                std::stringstream ss_ind;
+                ss_ind << "[" << i << "]";
+
+                COUT_DEBUG(R_ALIGN << ss_ind.str() << " "
+                           << R_ALIGN << data->at(i).first.at(j) << " "
+                           << R_ALIGN << data->at(i).second.at(j) << " "
+                           << R_ALIGN << denormalized_output
+                           << std::endl);
+
+                ofs << R_ALIGN << ss_ind.str() << " "
+                    << R_ALIGN << data->at(i).first.at(j) << " "
+                    << R_ALIGN << data->at(i).second.at(j) << " "
+                    << R_ALIGN << denormalized_output
+                    << std::endl;
 
                 val = denormalized_output - data->at(i).second.at(j);
                 error += val * val;
@@ -259,9 +288,15 @@ namespace lib4neuro {
             ofs << std::endl;
         }
 
+
+        double result = std::sqrt(error) / n_elements;
+
+        ofs << "MSE = " << result << std::endl;
         ofs.close();
 
-        return error / n_elements;
+        COUT_DEBUG("MSE = " << result << std::endl);
+
+        return result;
     }
 
     double MSE::eval_on_data_set(DataSet* data_set, std::vector<double>* weights) {
@@ -275,9 +310,11 @@ namespace lib4neuro {
         std::vector<std::vector<double>> outputs(data->size());
         std::vector<double> output(dim_out);
 
-        COUT_DEBUG(
-                "Evaluation of the error function MSE on the given data-set (format 'input' 'real output' 'predicted output'):"
-                        << std::endl);
+        COUT_DEBUG("Evaluation of the error function MSE on the given data-set" << std::endl);
+        COUT_DEBUG(R_ALIGN << "[Input]" << " "
+                   << R_ALIGN << "[Real output]" << " "
+                   << R_ALIGN << "[Predicted output]" << " "
+                   << std::endl);
 
         /* Compute predicted outputs */
         for (auto i = 0; i < data->size(); i++) {  // Iterate through every element in the test set
@@ -308,17 +345,25 @@ namespace lib4neuro {
                 } else {
                     denormalized_output = outputs.at(i).at(j);
                 }
-                COUT_DEBUG("[" << i << "] "
-                               << data->at(i).first.at(j) << " "
-                               << data->at(i).second.at(j) << " "
-                               << denormalized_output << std::endl);
+
+                std::stringstream ss_ind;
+                ss_ind << "[" << i << "]";
+
+                COUT_DEBUG(R_ALIGN << ss_ind.str() << " "
+                           << R_ALIGN << data->at(i).first.at(j) << " "
+                           << R_ALIGN << data->at(i).second.at(j) << " "
+                           << R_ALIGN << denormalized_output
+                           << std::endl);
 
                 val = denormalized_output - data->at(i).second.at(j);
                 error += val * val;
             }
         }
 
-        return error / n_elements;
+        double result = std::sqrt(result)/n_elements;
+        COUT_DEBUG("MSE = " << result << std::endl);
+
+        return result;
     }
 
     double MSE::eval(std::vector<double>* weights) {
-- 
GitLab