mirror of
https://github.com/clearml/clearml
synced 2025-06-26 18:16:07 +00:00
Add report_matplotlib_figure(..., report_interactive=False) allowing to upload a matplotlib as a non-interactive (high quality png) plot
This commit is contained in:
parent
444e379e93
commit
83fb00ef8b
@ -264,7 +264,8 @@ class Reporter(InterfaceBase, AbstractContextManager, SetupUploadMixin, AsyncMan
|
|||||||
series=series,
|
series=series,
|
||||||
figure=figure,
|
figure=figure,
|
||||||
iter=iter,
|
iter=iter,
|
||||||
force_save_as_image=force_save_as_image,
|
force_save_as_image=force_save_as_image if not isinstance(force_save_as_image, str) else False,
|
||||||
|
report_as_debug_sample=force_save_as_image if isinstance(force_save_as_image, str) else False,
|
||||||
reporter=self,
|
reporter=self,
|
||||||
logger=logger,
|
logger=logger,
|
||||||
)
|
)
|
||||||
|
|||||||
@ -27,7 +27,7 @@ class PatchedMatplotlib:
|
|||||||
_global_image_counter_limit = None
|
_global_image_counter_limit = None
|
||||||
_last_iteration_plot_titles = {}
|
_last_iteration_plot_titles = {}
|
||||||
_current_task = None
|
_current_task = None
|
||||||
_support_image_plot = False
|
_support_image_plot = None
|
||||||
_matplotlylib = None
|
_matplotlylib = None
|
||||||
_plotly_renderer = None
|
_plotly_renderer = None
|
||||||
_lock_renderer = threading.RLock()
|
_lock_renderer = threading.RLock()
|
||||||
@ -124,13 +124,19 @@ class PatchedMatplotlib:
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
# update api version
|
# update api version
|
||||||
from ..backend_api import Session
|
PatchedMatplotlib._update_matplotlib_image_support()
|
||||||
PatchedMatplotlib._support_image_plot = Session.check_min_api_version('2.2')
|
|
||||||
# load plotly
|
# load plotly
|
||||||
PatchedMatplotlib._update_plotly_renderers()
|
PatchedMatplotlib._update_plotly_renderers()
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _update_matplotlib_image_support():
|
||||||
|
if PatchedMatplotlib._support_image_plot is None:
|
||||||
|
from ..backend_api import Session
|
||||||
|
PatchedMatplotlib._support_image_plot = Session.check_min_api_version('2.2')
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _update_matplotlib_version():
|
def _update_matplotlib_version():
|
||||||
if PatchedMatplotlib._matplot_major_version:
|
if PatchedMatplotlib._matplot_major_version:
|
||||||
@ -152,6 +158,9 @@ class PatchedMatplotlib:
|
|||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
# check backend support
|
||||||
|
PatchedMatplotlib._update_matplotlib_image_support()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _update_plotly_renderers():
|
def _update_plotly_renderers():
|
||||||
if PatchedMatplotlib._matplotlylib and PatchedMatplotlib._plotly_renderer:
|
if PatchedMatplotlib._matplotlylib and PatchedMatplotlib._plotly_renderer:
|
||||||
@ -280,9 +289,11 @@ class PatchedMatplotlib:
|
|||||||
return ret
|
return ret
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def report_figure(title, series, figure, iter, force_save_as_image=False, reporter=None, logger=None):
|
def report_figure(title, series, figure, iter,
|
||||||
|
force_save_as_image=False, report_as_debug_sample=False, reporter=None, logger=None):
|
||||||
PatchedMatplotlib._report_figure(
|
PatchedMatplotlib._report_figure(
|
||||||
force_save_as_image=force_save_as_image,
|
force_save_as_image=force_save_as_image,
|
||||||
|
report_as_debug_sample=report_as_debug_sample,
|
||||||
specific_fig=figure.gcf() if hasattr(figure, 'gcf') else figure,
|
specific_fig=figure.gcf() if hasattr(figure, 'gcf') else figure,
|
||||||
title=title,
|
title=title,
|
||||||
series=series,
|
series=series,
|
||||||
@ -294,6 +305,7 @@ class PatchedMatplotlib:
|
|||||||
@staticmethod
|
@staticmethod
|
||||||
def _report_figure(
|
def _report_figure(
|
||||||
force_save_as_image=False,
|
force_save_as_image=False,
|
||||||
|
report_as_debug_sample=False,
|
||||||
stored_figure=None,
|
stored_figure=None,
|
||||||
set_active=True,
|
set_active=True,
|
||||||
specific_fig=None,
|
specific_fig=None,
|
||||||
@ -336,7 +348,7 @@ class PatchedMatplotlib:
|
|||||||
if getattr(stored_figure, '_trains_is_imshow', None) is not None:
|
if getattr(stored_figure, '_trains_is_imshow', None) is not None:
|
||||||
# flag will be cleared when calling clf() (object will be replaced)
|
# flag will be cleared when calling clf() (object will be replaced)
|
||||||
stored_figure._trains_is_imshow = max(0, stored_figure._trains_is_imshow - 1)
|
stored_figure._trains_is_imshow = max(0, stored_figure._trains_is_imshow - 1)
|
||||||
force_save_as_image = True
|
report_as_debug_sample = True
|
||||||
# get current figure
|
# get current figure
|
||||||
mpl_fig = stored_figure.canvas.figure # plt.gcf()
|
mpl_fig = stored_figure.canvas.figure # plt.gcf()
|
||||||
else:
|
else:
|
||||||
@ -354,9 +366,11 @@ class PatchedMatplotlib:
|
|||||||
plotly_dict = None
|
plotly_dict = None
|
||||||
image_format = 'jpeg'
|
image_format = 'jpeg'
|
||||||
fig_dpi = 300
|
fig_dpi = 300
|
||||||
if force_save_as_image:
|
if force_save_as_image or report_as_debug_sample:
|
||||||
# if this is an image, store as is.
|
# if this is an image, store as is.
|
||||||
fig_dpi = None
|
fig_dpi = None if report_as_debug_sample else 300
|
||||||
|
force_save_as_image = force_save_as_image \
|
||||||
|
if force_save_as_image and isinstance(force_save_as_image, str) else 'png'
|
||||||
if isinstance(force_save_as_image, str):
|
if isinstance(force_save_as_image, str):
|
||||||
image_format = force_save_as_image
|
image_format = force_save_as_image
|
||||||
else:
|
else:
|
||||||
@ -487,8 +501,8 @@ 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_dict and (
|
report_as_debug_sample = report_as_debug_sample or (
|
||||||
force_save_as_image or not PatchedMatplotlib._support_image_plot)
|
not plotly_dict and not PatchedMatplotlib._support_image_plot)
|
||||||
|
|
||||||
if not title:
|
if not title:
|
||||||
if mpl_fig.texts:
|
if mpl_fig.texts:
|
||||||
|
|||||||
@ -987,6 +987,7 @@ class Logger(object):
|
|||||||
figure, # type: Union[MatplotlibFigure, pyplot]
|
figure, # type: Union[MatplotlibFigure, pyplot]
|
||||||
iteration=None, # type: Optional[int]
|
iteration=None, # type: Optional[int]
|
||||||
report_image=False, # type: bool
|
report_image=False, # type: bool
|
||||||
|
report_interactive=True, # type: bool
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
Report a ``matplotlib`` figure / plot directly
|
Report a ``matplotlib`` figure / plot directly
|
||||||
@ -999,6 +1000,9 @@ class Logger(object):
|
|||||||
:param MatplotlibFigure figure: A ``matplotlib`` Figure object
|
:param MatplotlibFigure figure: A ``matplotlib`` Figure object
|
||||||
:param report_image: Default False. If True the plot will be uploaded as a debug sample (png image),
|
:param report_image: Default False. If True the plot will be uploaded as a debug sample (png image),
|
||||||
and will appear under the debug samples tab (instead of the Plots tab).
|
and will appear under the debug samples tab (instead of the Plots tab).
|
||||||
|
:param report_interactive: If True (default) it will try to convert the matplotlib into interactive
|
||||||
|
plot in the UI. If False the matplotlib is saved as is and will
|
||||||
|
be non-interactive (with the exception of zooming in/out)
|
||||||
"""
|
"""
|
||||||
# if task was not started, we have to start it
|
# if task was not started, we have to start it
|
||||||
self._start_task_if_needed()
|
self._start_task_if_needed()
|
||||||
@ -1010,7 +1014,7 @@ class Logger(object):
|
|||||||
figure=figure,
|
figure=figure,
|
||||||
iter=iteration or 0,
|
iter=iteration or 0,
|
||||||
logger=self,
|
logger=self,
|
||||||
force_save_as_image='png' if report_image else False,
|
force_save_as_image=False if report_interactive else ('png' if report_image else True),
|
||||||
)
|
)
|
||||||
|
|
||||||
def set_default_upload_destination(self, uri):
|
def set_default_upload_destination(self, uri):
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user