Repository: fitbenchmarking/fitbenchmarking

Issue:
Table key should use relative path
The table key should use a relative (not absolute) path otherwise it will not show up in certain cases, e.g. see below:

<img width="1278" alt="Screenshot 2021-08-23 at 9 53 55 am" src="https://user-images.githubusercontent.com/5586192/130419494-d974b470-ad9f-4931-9101-50b3bf7cf5a1.png">

(Here I am serving the results pages to another machine via a ssh tunnel).

Discussion:

Patch:
diff --git a/fitbenchmarking/results_processing/base_table.py b/fitbenchmarking/results_processing/base_table.py
index cbc0b505..1a4955b3 100644
--- a/fitbenchmarking/results_processing/base_table.py
+++ b/fitbenchmarking/results_processing/base_table.py
@@ -518,7 +518,7 @@ class Table:
 
         return hex_strs
 
-    def save_colourbar(self, fig_dir, n_divs=100, sz_in=(3, 0.8)):
+    def save_colourbar(self, fig_dir, n_divs=100, sz_in=(3, 0.8)) -> str:
         """
         Generates a png of a labelled colourbar using matplotlib.
 
@@ -528,6 +528,9 @@ class Table:
         :type n_divs: int
         :param sz_in: dimensions of png in inches [width, height]
         :type sz_in: list[float] - 2 elements
+
+        :return: The relative path to the colourbar image.
+        :rtype: str
         """
         fig_path = os.path.join(fig_dir, "{0}_cbar.png".format(self.name))
 
@@ -554,3 +557,5 @@ class Table:
         fig.set_size_inches(sz_in[0], sz_in[1])
 
         plt.savefig(fig_path, dpi=150)
+
+        return os.path.relpath(fig_path, self.group_dir)
diff --git a/fitbenchmarking/results_processing/local_min_table.py b/fitbenchmarking/results_processing/local_min_table.py
index 2cfe0eeb..044988fe 100644
--- a/fitbenchmarking/results_processing/local_min_table.py
+++ b/fitbenchmarking/results_processing/local_min_table.py
@@ -173,7 +173,7 @@ class LocalMinTable(Table):
         return f'{str(local_min)} ({template.format(norm_rel)})'
 
     # pylint: disable=unused-argument
-    def save_colourbar(self, fig_dir, n_divs=2, sz_in=None):
+    def save_colourbar(self, fig_dir, n_divs=2, sz_in=None) -> str:
         """
         Override default save_colourbar as there are only 2 possible divisions
         of the colour map (true or false).
@@ -184,9 +184,12 @@ class LocalMinTable(Table):
         :type n_divs: int, Fixed to 2
         :param sz_in: dimensions of png in inches [width, height]
         :type sz_in: list[float] - 2 elements
+
+        :return: The relative path to the colourbar image.
+        :rtype: str
         """
         if sz_in is not None:
-            super().save_colourbar(fig_dir, n_divs=2, sz_in=sz_in)
+            return super().save_colourbar(fig_dir, n_divs=2, sz_in=sz_in)
         else:
-            super().save_colourbar(fig_dir, n_divs=2)
+            return super().save_colourbar(fig_dir, n_divs=2)
     # pylint: enable=unused-argument


Test patch:
diff --git a/fitbenchmarking/results_processing/tests/test_base_table.py b/fitbenchmarking/results_processing/tests/test_base_table.py
index 47391999..9e7ea532 100644
--- a/fitbenchmarking/results_processing/tests/test_base_table.py
+++ b/fitbenchmarking/results_processing/tests/test_base_table.py
@@ -2,8 +2,9 @@
 Tests for functions in the base tables file.
 """
 
-from unittest import TestCase
+from unittest import mock, TestCase
 
+import os
 import numpy as np
 
 from fitbenchmarking.cost_func.weighted_nlls_cost_func import \
@@ -230,3 +231,37 @@ class DisplayStrTests(TestCase):
         self.table.options.comparison_mode = 'both'
         s = self.table.display_str([7, 9])
         self.assertEqual(s, '9 (7)')
+
+
+class SaveColourbarTests(TestCase):
+    """
+    Tests for the save_colourbar implementation.
+    """
+
+    def setUp(self):
+        results = generate_results()
+        self.root_directory = os.path.join(os.path.dirname(__file__),
+                                           os.pardir, os.pardir, os.pardir)
+        self.table = DummyTable(results=results,
+                                options=Options(),
+                                group_dir=self.root_directory,
+                                pp_locations=('no', 'pp'),
+                                table_name='A table!')
+
+    @mock.patch("fitbenchmarking.results_processing.base_table.plt.savefig")
+    def test_save_colourbar_returns_a_relative_path(self, savefig_mock):
+        """
+        Test the relative path to the colourbar figure is returned after saving
+        """
+        self.table.name = "fake_name"
+        colourbar_file = f"{self.table.name}_cbar.png"
+
+        figure_sub_directory = "fitbenchmarking_results"
+        figure_directory = os.path.join(self.root_directory,
+                                        figure_sub_directory)
+
+        relative_path = os.path.join(figure_sub_directory, colourbar_file)
+        self.assertEqual(self.table.save_colourbar(figure_directory),
+                         relative_path)
+
+        savefig_mock.assert_called_once()