From 79799d3efd720a7accdd89bafb52d428709a70a0 Mon Sep 17 00:00:00 2001 From: allegroai <> Date: Sat, 11 Jul 2020 01:37:02 +0300 Subject: [PATCH] Add logger.report_confusion_matrix arg yaxis_reversed (when True flip the confusion matrix, default False). Issue #165 --- .../reporting/scatter_hist_confusion_mat_reporting.py | 11 +++++++++++ trains/backend_interface/metrics/reporter.py | 4 +++- trains/logger.py | 6 ++++++ trains/utilities/plotly_reporter.py | 4 +++- 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/examples/reporting/scatter_hist_confusion_mat_reporting.py b/examples/reporting/scatter_hist_confusion_mat_reporting.py index 91f0f6dc..184f5c8f 100644 --- a/examples/reporting/scatter_hist_confusion_mat_reporting.py +++ b/examples/reporting/scatter_hist_confusion_mat_reporting.py @@ -55,6 +55,17 @@ def report_plots(logger, iteration=0): yaxis="title Y", ) + # report confusion matrix with 0,0 is at the top left + logger.report_matrix( + "example_confusion_0_0_at_top", + "ignored", + iteration=iteration, + matrix=confusion, + xaxis="title X", + yaxis="title Y", + yaxis_reversed=True, + ) + scatter2d = np.hstack( (np.atleast_2d(np.arange(0, 10)).T, np.random.randint(10, size=(10, 1))) ) diff --git a/trains/backend_interface/metrics/reporter.py b/trains/backend_interface/metrics/reporter.py index 3646b088..2ab976b3 100644 --- a/trains/backend_interface/metrics/reporter.py +++ b/trains/backend_interface/metrics/reporter.py @@ -523,7 +523,7 @@ class Reporter(InterfaceBase, AbstractContextManager, SetupUploadMixin, AsyncMan ) def report_value_matrix(self, title, series, data, iter, xtitle=None, ytitle=None, xlabels=None, ylabels=None, - comment=None, layout_config=None): + yaxis_reversed=False, comment=None, layout_config=None): """ Report a heat-map matrix @@ -539,6 +539,7 @@ class Reporter(InterfaceBase, AbstractContextManager, SetupUploadMixin, AsyncMan :param str ytitle: optional y-axis title :param xlabels: optional label per column of the matrix :param ylabels: optional label per row of the matrix + :param bool yaxis_reversed: If False 0,0 is at the bottom left corner. If True 0,0 is at the Top left corner :param comment: comment underneath the title :param layout_config: optional dictionary for layout configuration, passed directly to plotly :type layout_config: dict or None @@ -553,6 +554,7 @@ class Reporter(InterfaceBase, AbstractContextManager, SetupUploadMixin, AsyncMan comment=comment, xtitle=xtitle, ytitle=ytitle, + yaxis_reversed=yaxis_reversed, layout_config=layout_config, ) diff --git a/trains/logger.py b/trains/logger.py index d7b14038..296a737a 100644 --- a/trains/logger.py +++ b/trains/logger.py @@ -572,6 +572,7 @@ class Logger(object): yaxis=None, # type: Optional[str] xlabels=None, # type: Optional[List[str]] ylabels=None, # type: Optional[List[str]] + yaxis_reversed=False, # type: bool comment=None, # type: Optional[str] extra_layout=None, # type: Optional[dict] ): @@ -594,6 +595,7 @@ class Logger(object): :param str yaxis: The y-axis title. (Optional) :param list(str) xlabels: Labels for each column of the matrix. (Optional) :param list(str) ylabels: Labels for each row of the matrix. (Optional) + :param bool yaxis_reversed: If False 0,0 is at the bottom left corner. If True 0,0 is at the Top left corner :param str comment: A comment displayed with the plot, underneath the title. :param dict extra_layout: optional dictionary for layout configuration, passed directly to plotly example: extra_layout={'xaxis': {'type': 'date', 'range': ['2020-01-01', '2020-01-31']}} @@ -614,6 +616,7 @@ class Logger(object): ytitle=yaxis, xlabels=xlabels, ylabels=ylabels, + yaxis_reversed=yaxis_reversed, comment=comment, layout_config=extra_layout, ) @@ -628,6 +631,7 @@ class Logger(object): yaxis=None, # type: Optional[str] xlabels=None, # type: Optional[List[str]] ylabels=None, # type: Optional[List[str]] + yaxis_reversed=False, # type: bool extra_layout=None, # type: Optional[dict] ): """ @@ -644,12 +648,14 @@ class Logger(object): :param str yaxis: The y-axis title. (Optional) :param list(str) xlabels: Labels for each column of the matrix. (Optional) :param list(str) ylabels: Labels for each row of the matrix. (Optional) + :param bool yaxis_reversed: If False 0,0 is at the bottom left corner. If True 0,0 is at the Top left corner :param dict extra_layout: optional dictionary for layout configuration, passed directly to plotly example: extra_layout={'xaxis': {'type': 'date', 'range': ['2020-01-01', '2020-01-31']}} """ self._touch_title_series(title, series) return self.report_confusion_matrix(title, series, matrix, iteration, xaxis=xaxis, yaxis=yaxis, xlabels=xlabels, ylabels=ylabels, + yaxis_reversed=yaxis_reversed, extra_layout=extra_layout) def report_surface( diff --git a/trains/utilities/plotly_reporter.py b/trains/utilities/plotly_reporter.py index e78708ef..af4d4a85 100644 --- a/trains/utilities/plotly_reporter.py +++ b/trains/utilities/plotly_reporter.py @@ -217,7 +217,7 @@ def create_3d_scatter_series(np_row_wise, title="Scatter", series_name="Series", def create_value_matrix(np_value_matrix, title="Heatmap Matrix", xlabels=None, ylabels=None, xtitle="X", ytitle="Y", - custom_colors=True, series=None, comment=None, layout_config=None): + custom_colors=True, series=None, comment=None, yaxis_reversed=False, layout_config=None): conf_matrix_plot = { "data": [ { @@ -240,6 +240,8 @@ def create_value_matrix(np_value_matrix, title="Heatmap Matrix", xlabels=None, y "name": series, } } + if yaxis_reversed: + conf_matrix_plot['layout']['yaxis']['autorange'] = "reversed" if custom_colors: scale, bar = _get_z_colorbar_data()