How to use the trains.binding.matplotlib_bind.PatchedMatplotlib function in trains

To help you get started, we’ve selected a few trains examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github allegroai / trains / trains / binding / matplotlib_bind.py View on Github external
def patched_show(*args, **kw):
        tid = threading._get_ident() if six.PY2 else threading.get_ident()
        PatchedMatplotlib._recursion_guard[tid] = True
        # noinspection PyBroadException
        try:
            figures = PatchedMatplotlib._get_output_figures(None, all_figures=True)
            for figure in figures:
                # if this is a stale figure (just updated) we should send it, the rest will not be stale
                if figure.canvas.figure.stale or (hasattr(figure, '_trains_is_imshow') and figure._trains_is_imshow):
                    PatchedMatplotlib._report_figure(stored_figure=figure)
        except Exception:
            pass
        ret = PatchedMatplotlib._patched_original_plot(*args, **kw)
        if PatchedMatplotlib._current_task and sys.modules['matplotlib'].rcParams['backend'] == 'agg':
            # clear the current plot, because no one else will
            # noinspection PyBroadException
            try:
                if sys.modules['matplotlib'].rcParams['backend'] == 'agg':
                    import matplotlib.pyplot as plt
                    plt.clf()
            except Exception:
                pass
        PatchedMatplotlib._recursion_guard[tid] = False
        return ret
github allegroai / trains / trains / binding / matplotlib_bind.py View on Github external
matplotlib.pyplot.switch_backend('agg')
            import matplotlib.pyplot as plt
            import matplotlib.figure as figure
            from matplotlib import _pylab_helpers
            if six.PY2:
                PatchedMatplotlib._patched_original_plot = staticmethod(plt.show)
                PatchedMatplotlib._patched_original_imshow = staticmethod(plt.imshow)
                PatchedMatplotlib._patched_original_figure = staticmethod(figure.Figure.show)
            else:
                PatchedMatplotlib._patched_original_plot = plt.show
                PatchedMatplotlib._patched_original_imshow = plt.imshow
                PatchedMatplotlib._patched_original_figure = figure.Figure.show

            plt.show = PatchedMatplotlib.patched_show
            figure.Figure.show = PatchedMatplotlib.patched_figure_show
            sys.modules['matplotlib'].pyplot.imshow = PatchedMatplotlib.patched_imshow
            # patch plotly so we know it failed us.
            from plotly.matplotlylib import renderer
            renderer.warnings = PatchedMatplotlib._PatchWarnings()

            # ignore deprecation warnings from plotly to matplotlib
            try:
                import warnings
                warnings.filterwarnings(action='ignore', category=matplotlib.MatplotlibDeprecationWarning,
                                        module='plotly')
            except Exception:
                pass

        except Exception:
            return False

        # patch IPython matplotlib inline mode
github allegroai / trains / trains / binding / matplotlib_bind.py View on Github external
def update_current_task(task):
        # make sure we have a default value
        if PatchedMatplotlib._global_image_counter_limit is None:
            from ..config import config
            PatchedMatplotlib._global_image_counter_limit = config.get('metric.matplotlib_untitled_history_size', 100)

        # if we already patched it, just update the current task
        if PatchedMatplotlib._patched_original_plot is not None:
            PatchedMatplotlib._current_task = task
        # if matplotlib is not loaded yet, get a callback hook
        elif not running_remotely() and 'matplotlib.pyplot' not in sys.modules:
            PatchedMatplotlib._current_task = task
            PostImportHookPatching.add_on_import('matplotlib.pyplot', PatchedMatplotlib.patch_matplotlib)
        elif PatchedMatplotlib.patch_matplotlib():
            PatchedMatplotlib._current_task = task
github allegroai / trains / trains / binding / matplotlib_bind.py View on Github external
def patched_imshow(*args, **kw):
        ret = PatchedMatplotlib._patched_original_imshow(*args, **kw)
        try:
            from matplotlib import _pylab_helpers
            # store on the plot that this is an imshow plot
            stored_figure = _pylab_helpers.Gcf.get_active()
            if stored_figure:
                stored_figure._trains_is_imshow = 1 if not hasattr(stored_figure, '_trains_is_imshow') \
                    else stored_figure._trains_is_imshow + 1
        except Exception:
            pass
        return ret
github allegroai / trains / trains / binding / matplotlib_bind.py View on Github external
# instead of hooking ipython, we should hook the matplotlib
                    import matplotlib.pyplot as plt
                    PatchedMatplotlib.__patched_original_draw_all = plt.draw_all
                    plt.draw_all = PatchedMatplotlib.__patched_draw_all
                    # ip.events.register('post_execute', PatchedMatplotlib.ipython_post_execute_hook)
        except Exception:
            pass

        # update api version
        from ..backend_api import Session
        PatchedMatplotlib._support_image_plot = Session.check_min_api_version('2.2')

        # create plotly renderer
        try:
            from plotly import optional_imports
            PatchedMatplotlib._matplotlylib = optional_imports.get_module('plotly.matplotlylib')
            PatchedMatplotlib._plotly_renderer = PatchedMatplotlib._matplotlylib.PlotlyRenderer()
        except Exception:
            pass

        return True
github allegroai / trains / trains / binding / matplotlib_bind.py View on Github external
def _enforce_unique_title_per_iteration(title, last_iteration):
        if last_iteration != PatchedMatplotlib._last_iteration_plot_titles[0]:
            PatchedMatplotlib._last_iteration_plot_titles = (last_iteration, [title])
        elif title not in PatchedMatplotlib._last_iteration_plot_titles[1]:
            PatchedMatplotlib._last_iteration_plot_titles[1].append(title)
        else:
            base_title = title
            counter = 1
            while title in PatchedMatplotlib._last_iteration_plot_titles[1]:
                # we already used this title in this iteration, we should change the title
                title = base_title + ' %d' % counter
                counter += 1
            # store the new title
            PatchedMatplotlib._last_iteration_plot_titles[1].append(title)
        return title
github allegroai / trains / trains / binding / matplotlib_bind.py View on Github external
plotly_dict['layout']['title'] = title
                    reporter.report_plot(title=title, series='plot', plot=plotly_dict, iter=last_iteration)
                else:
                    logger = PatchedMatplotlib._current_task.get_logger()

                    # this is actually a failed plot, we should put it under plots:
                    # currently disabled
                    if force_save_as_image or not PatchedMatplotlib._support_image_plot:
                        last_iteration = PatchedMatplotlib._current_task.get_last_iteration()
                        # send the plot as image
                        if plot_title:
                            title = PatchedMatplotlib._enforce_unique_title_per_iteration(plot_title, last_iteration)
                        else:
                            PatchedMatplotlib._global_image_counter += 1
                            title = 'untitled %d' % (PatchedMatplotlib._global_image_counter %
                                                     PatchedMatplotlib._global_image_counter_limit)

                        logger.report_image(title=title, series='plot image', local_path=image,
                                            delete_after_upload=True, iteration=last_iteration)
                    else:
                        # send the plot as plotly with embedded image
                        last_iteration = PatchedMatplotlib._current_task.get_last_iteration()
                        if plot_title:
                            title = PatchedMatplotlib._enforce_unique_title_per_iteration(plot_title, last_iteration)
                        else:
                            PatchedMatplotlib._global_plot_counter += 1
                            title = 'untitled %d' % (PatchedMatplotlib._global_plot_counter %
                                                     PatchedMatplotlib._global_image_counter_limit)

                        logger._report_image_plot_and_upload(title=title, series='plot image', path=image,
                                                             delete_after_upload=True, iteration=last_iteration)
        except Exception:
github allegroai / trains / trains / binding / matplotlib_bind.py View on Github external
def patched_show(*args, **kw):
        tid = threading._get_ident() if six.PY2 else threading.get_ident()
        PatchedMatplotlib._recursion_guard[tid] = True
        # noinspection PyBroadException
        try:
            figures = PatchedMatplotlib._get_output_figures(None, all_figures=True)
            for figure in figures:
                # if this is a stale figure (just updated) we should send it, the rest will not be stale
                if figure.canvas.figure.stale or (hasattr(figure, '_trains_is_imshow') and figure._trains_is_imshow):
                    PatchedMatplotlib._report_figure(stored_figure=figure)
        except Exception:
            pass
        ret = PatchedMatplotlib._patched_original_plot(*args, **kw)
        if PatchedMatplotlib._current_task and sys.modules['matplotlib'].rcParams['backend'] == 'agg':
            # clear the current plot, because no one else will
            # noinspection PyBroadException
            try:
                if sys.modules['matplotlib'].rcParams['backend'] == 'agg':
                    import matplotlib.pyplot as plt
                    plt.clf()
            except Exception:
                pass
github allegroai / trains / trains / binding / matplotlib_bind.py View on Github external
# currently disabled
                    if force_save_as_image or not PatchedMatplotlib._support_image_plot:
                        last_iteration = PatchedMatplotlib._current_task.get_last_iteration()
                        # send the plot as image
                        if plot_title:
                            title = PatchedMatplotlib._enforce_unique_title_per_iteration(plot_title, last_iteration)
                        else:
                            PatchedMatplotlib._global_image_counter += 1
                            title = 'untitled %d' % (PatchedMatplotlib._global_image_counter %
                                                     PatchedMatplotlib._global_image_counter_limit)

                        logger.report_image(title=title, series='plot image', local_path=image,
                                            delete_after_upload=True, iteration=last_iteration)
                    else:
                        # send the plot as plotly with embedded image
                        last_iteration = PatchedMatplotlib._current_task.get_last_iteration()
                        if plot_title:
                            title = PatchedMatplotlib._enforce_unique_title_per_iteration(plot_title, last_iteration)
                        else:
                            PatchedMatplotlib._global_plot_counter += 1
                            title = 'untitled %d' % (PatchedMatplotlib._global_plot_counter %
                                                     PatchedMatplotlib._global_image_counter_limit)

                        logger._report_image_plot_and_upload(title=title, series='plot image', path=image,
                                                             delete_after_upload=True, iteration=last_iteration)
        except Exception:
            # plotly failed
            pass

        return
github allegroai / trains / trains / binding / matplotlib_bind.py View on Github external
pass

        except Exception:
            return False

        # patch IPython matplotlib inline mode
        # noinspection PyBroadException
        try:
            if 'IPython' in sys.modules:
                from IPython import get_ipython
                ip = get_ipython()
                if ip and matplotlib.is_interactive():
                    # instead of hooking ipython, we should hook the matplotlib
                    import matplotlib.pyplot as plt
                    PatchedMatplotlib.__patched_original_draw_all = plt.draw_all
                    plt.draw_all = PatchedMatplotlib.__patched_draw_all
                    # ip.events.register('post_execute', PatchedMatplotlib.ipython_post_execute_hook)
        except Exception:
            pass

        # update api version
        from ..backend_api import Session
        PatchedMatplotlib._support_image_plot = Session.check_min_api_version('2.2')

        # create plotly renderer
        try:
            from plotly import optional_imports
            PatchedMatplotlib._matplotlylib = optional_imports.get_module('plotly.matplotlylib')
            PatchedMatplotlib._plotly_renderer = PatchedMatplotlib._matplotlylib.PlotlyRenderer()
        except Exception:
            pass