Fix global matplotlib counting

Fix typo in matplotlib bind class property
Fix Matplotlib binding without a title reported a single plot (untitled 00) instead of increasing the counter
This commit is contained in:
allegroai 2020-11-17 23:11:41 +02:00
parent f35e927951
commit 8b4b7098e0

View File

@ -19,7 +19,7 @@ class PatchedMatplotlib:
_patched_original_plot = None _patched_original_plot = None
_patched_original_figure = None _patched_original_figure = None
_patched_original_savefig = None _patched_original_savefig = None
__patched_original_imshow = None _patched_original_imshow = None
__patched_original_draw_all = None __patched_original_draw_all = None
__patched_draw_all_recursion_guard = False __patched_draw_all_recursion_guard = False
_global_plot_counter = -1 _global_plot_counter = -1
@ -46,7 +46,7 @@ class PatchedMatplotlib:
raise ValueError(text) raise ValueError(text)
def __getattr__(self, item): def __getattr__(self, item):
def bypass(*args, **kwargs): def bypass(*_, **__):
pass pass
return bypass return bypass
@ -459,6 +459,9 @@ class PatchedMatplotlib:
last_iteration = iter if iter is not None else PatchedMatplotlib._get_last_iteration() last_iteration = iter if iter is not None else PatchedMatplotlib._get_last_iteration()
report_as_debug_sample = not plotly_fig and (
force_save_as_image or not PatchedMatplotlib._support_image_plot)
if not title: if not title:
if mpl_fig.texts: if mpl_fig.texts:
plot_title = mpl_fig.texts[0].get_text() plot_title = mpl_fig.texts[0].get_text()
@ -468,10 +471,19 @@ class PatchedMatplotlib:
if plot_title: if plot_title:
title = PatchedMatplotlib._enforce_unique_title_per_iteration(plot_title, last_iteration) title = PatchedMatplotlib._enforce_unique_title_per_iteration(plot_title, last_iteration)
elif report_as_debug_sample:
PatchedMatplotlib._global_image_counter += 1
title = 'untitled {:02d}'.format(
PatchedMatplotlib._global_image_counter % PatchedMatplotlib._global_image_counter_limit)
else: else:
PatchedMatplotlib._global_plot_counter += 1 PatchedMatplotlib._global_plot_counter += 1
mod_ = 1 if plotly_fig else PatchedMatplotlib._global_image_counter_limit title = 'untitled {:02d}'.format(
title = 'untitled %02d' % (PatchedMatplotlib._global_plot_counter % mod_) PatchedMatplotlib._global_plot_counter % PatchedMatplotlib._global_image_counter_limit)
# by now we should have a title, if the iteration was known list us as globally reported.
# we later use it to check if externally someone was actually reporting iterations
if iter is None:
PatchedMatplotlib._matplotlib_reported_titles.add(title)
# remove borders and size, we should let the web take care of that # remove borders and size, we should let the web take care of that
if plotly_fig: if plotly_fig:
@ -485,17 +497,14 @@ class PatchedMatplotlib:
plotly_dict['layout'] = {} plotly_dict['layout'] = {}
plotly_dict['layout']['title'] = series or title plotly_dict['layout']['title'] = series or title
PatchedMatplotlib._matplotlib_reported_titles.add(title)
reporter.report_plot(title=title, series=series or 'plot', plot=plotly_dict, iter=last_iteration) reporter.report_plot(title=title, series=series or 'plot', plot=plotly_dict, iter=last_iteration)
else: else:
# this is actually a failed plot, we should put it under plots: # this is actually a failed plot, we should put it under plots:
# currently disabled # currently disabled
if force_save_as_image or not PatchedMatplotlib._support_image_plot: if report_as_debug_sample:
PatchedMatplotlib._matplotlib_reported_titles.add(title)
logger.report_image(title=title, series=series or 'plot image', local_path=image, logger.report_image(title=title, series=series or 'plot image', local_path=image,
delete_after_upload=True, iteration=last_iteration) delete_after_upload=True, iteration=last_iteration)
else: else:
PatchedMatplotlib._matplotlib_reported_titles.add(title)
# noinspection PyProtectedMember # noinspection PyProtectedMember
logger._report_image_plot_and_upload( logger._report_image_plot_and_upload(
title=title, series=series or 'plot image', path=image, title=title, series=series or 'plot image', path=image,