Skip to content

Commit

Permalink
added errorbars (#1279)
Browse files Browse the repository at this point in the history
Co-authored-by: Jessica Farmer <79837359+jess-farmer@users.noreply.github.com>
  • Loading branch information
RabiyaF and jess-farmer committed May 20, 2024
1 parent 0b3ea8a commit cf9d1f1
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 30 deletions.
56 changes: 32 additions & 24 deletions fitbenchmarking/results_processing/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,36 @@ class Plot:
"""
Class providing plotting functionality.
"""
data_marker = {"symbol": "x",
"color": "black"}
best_fit_line = {"dash": "dot",
"color": '#6699ff'}
legend_options = {"yanchor": "top",
"y": 0.99,
"xanchor": "left",
"x": 0.01,
"bgcolor": 'rgba(0,0,0,0.1)'}
summary_best_plot_line = {"width": 2}
summary_plot_line = {"width": 1}
_data_marker = {"symbol": "x",
"color": "black"}
_best_fit_line = {"dash": "dot",
"color": '#6699ff'}
_legend_options = {"yanchor": "top",
"y": 0.99,
"xanchor": "left",
"x": 0.01,
"bgcolor": 'rgba(0,0,0,0.1)'}
_summary_best_plot_line = {"width": 2}
_summary_plot_line = {"width": 1}
_error_dict = dict(type='data',
array=None,
thickness=1,
width=4)

def __init__(self, best_result, options, figures_dir):
self.result = best_result
self.plots_failed = False

self.plots_failed = True
if self.result.multivariate:
self.plots_failed = True
raise PlottingError(
'Plots cannot be generated for multivariate problems')
if self.result.problem_format == 'horace':
self.plots_failed = True
raise PlottingError(
'Plots cannot be generated for Horace problems')
self.plots_failed = False

self.options = options

self.figures_dir = figures_dir

@staticmethod
Expand Down Expand Up @@ -85,14 +89,16 @@ def plot_initial_guess(self, df):
color="minimizer",
title=self.result.name,
markers=True)
self._error_dict['array'] = df["e"][df["minimizer"] == 'Data']
# add the raw data as a scatter plot
fig.add_trace(go.Scatter(
x=df["x"][df["minimizer"] == 'Data'],
y=df["y"][df["minimizer"] == 'Data'],
error_y=self._error_dict,
mode='markers',
name='Data',
marker=self.data_marker))
fig.update_layout(legend=self.legend_options)
marker=self._data_marker))
fig.update_layout(legend=self._legend_options)

if self.result.plot_scale in ["loglog", "logx"]:
fig.update_xaxes(type="log")
Expand Down Expand Up @@ -139,6 +145,7 @@ def plotly_fit(self, df):
y_best = df["y"][df["best"]]
x_data = df["x"][df["minimizer"] == 'Data']
y_data = df["y"][df["minimizer"] == 'Data']
self._error_dict['array'] = df["e"][df["minimizer"] == 'Data']

for minimizer in df['minimizer'].unique():
if minimizer not in ["Data", "Starting Guess"]:
Expand All @@ -157,16 +164,17 @@ def plotly_fit(self, df):
y=y_best,
mode='lines',
name=name,
line=self.best_fit_line))
line=self._best_fit_line))
# add the raw data as a scatter plot
fig.add_trace(
go.Scatter(
x=x_data,
y=y_data,
error_y=self._error_dict,
mode='markers',
name='Data',
marker=self.data_marker))
fig.update_layout(legend=self.legend_options)
marker=self._data_marker))
fig.update_layout(legend=self._legend_options)

if self.result.plot_scale in ["loglog", "logx"]:
fig.update_xaxes(type="log")
Expand Down Expand Up @@ -266,22 +274,22 @@ def plot_summary(cls, categories, title, options, figures_dir):
error_y=error_y,
mode='markers',
name='Data',
marker=cls.data_marker))
marker=cls._data_marker))

for (key, results), colour in zip(categories.items(), plotly_colours):
# Plot category
for result in results:
if result.params is not None:
line = cls.summary_best_plot_line \
if result.is_best_fit else cls.summary_plot_line
line = cls._summary_best_plot_line \
if result.is_best_fit else cls._summary_plot_line

line["color"] = colour
label = key if result.is_best_fit else ''
if result.is_best_fit:
line = cls.summary_best_plot_line
line = cls._summary_best_plot_line
line["color"] = colour
else:
line = cls.summary_plot_line
line = cls._summary_plot_line
transparency = 0.5
line["color"] = 'rgba' + colour[3:-1] + ', ' + \
str(transparency) + ')'
Expand Down
32 changes: 26 additions & 6 deletions fitbenchmarking/results_processing/tests/test_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ def load_mock_result():
return results


def find_error_bar_count(path):
"""
Reads an html file and counts the error_y count
"""
with open(path, 'r', encoding='utf-8') as file:
html_content = file.read()
return html_content.count("error_y")


class PlotTests(unittest.TestCase):
"""
Test the plot object is correct.
Expand Down Expand Up @@ -108,14 +117,16 @@ def setUp(self):

def test_plot_initial_guess_create_files(self):
"""
Test that initial plot creates a file.
Test that initial plot creates a file and errorbars are
added to the plot.
"""
file_name = self.plot.plot_initial_guess(
self.df[('Fake_Test_Data', 'prob_1')])

self.assertEqual(file_name, 'start_for_prob_1.html')
path = os.path.join(self.figures_dir, file_name)
self.assertTrue(os.path.exists(path))
self.assertEqual(find_error_bar_count(path), 2)

def test_best_filename_return(self):
"""
Expand All @@ -127,15 +138,24 @@ def test_best_filename_return(self):

def test_plotly_fit_create_files(self):
"""
Test that plotly_fit creates a file.
Test that plotly_fit creates a file and errorbars are
added to the plot.
"""
file_names = self.plot.plotly_fit(
self.df[('Fake_Test_Data', 'prob_1')])

self.assertEqual(file_names['m10_[s1]_jj0'],
'm10_[s1]_jj0_fit_for_cf1_prob_1.html')
path = os.path.join(self.figures_dir, file_names['m10_[s1]_jj0'])
self.assertTrue(os.path.exists(path))
for m, s, j in zip(['m10', 'm11', 'm01', 'm00',
'm10', 'm11', 'm01', 'm00'],
['s1', 's1', 's0', 's0',
's1', 's1', 's0', 's0'],
['jj0', 'jj0', 'jj0', 'jj0',
'jj1', 'jj1', 'jj1', 'jj1']):
file_name_prefix = f'{m}_[{s}]_{j}'
self.assertEqual(file_names[file_name_prefix],
file_name_prefix+'_fit_for_cf1_prob_1.html')
path = os.path.join(self.figures_dir, file_names[file_name_prefix])
self.assertTrue(os.path.exists(path))
self.assertEqual(find_error_bar_count(path), 2)

def test_multivariate_plot(self):
"""
Expand Down

0 comments on commit cf9d1f1

Please sign in to comment.