Fix report_histogram does not show "horizontal" orientation (#699)

This commit is contained in:
allegroai 2022-09-02 23:40:34 +03:00
parent bae2108d13
commit 86586fbf35
3 changed files with 22 additions and 7 deletions

View File

@ -543,7 +543,7 @@ class Reporter(InterfaceBase, AbstractContextManager, SetupUploadMixin, AsyncMan
self._report(ev)
def report_histogram(self, title, series, histogram, iter, labels=None, xlabels=None,
xtitle=None, ytitle=None, comment=None, mode='group', layout_config=None):
xtitle=None, ytitle=None, comment=None, mode='group', data_args=None, layout_config=None):
"""
Report an histogram bar plot
:param title: Title (AKA metric)
@ -565,6 +565,8 @@ class Reporter(InterfaceBase, AbstractContextManager, SetupUploadMixin, AsyncMan
:type comment: str
:param mode: multiple histograms mode. valid options are: stack / group / relative. Default is 'group'.
:type mode: str
:param data_args: optional dictionary for data configuration, passed directly to plotly
:type data_args: dict or None
:param layout_config: optional dictionary for layout configuration, passed directly to plotly
:type layout_config: dict or None
"""
@ -580,6 +582,7 @@ class Reporter(InterfaceBase, AbstractContextManager, SetupUploadMixin, AsyncMan
xlabels=xlabels,
comment=comment,
mode=mode,
data_args=data_args,
layout_config=layout_config,
)

View File

@ -221,8 +221,8 @@ class Logger(object):
:param yaxis: The y-axis title. (Optional)
:param mode: Multiple histograms mode, stack / group / relative. Default is 'group'.
:param extra_layout: optional dictionary for layout configuration, passed directly to plotly
See full details on the supported configuration: https://plotly.com/javascript/reference/bar/
example: extra_layout={'xaxis': {'type': 'date', 'range': ['2020-01-01', '2020-01-31']}}
See full details on the supported configuration: https://plotly.com/javascript/reference/layout/
example: extra_layout={'showlegend': False, 'plot_bgcolor': 'yellow'}
"""
self._touch_title_series(title, series)
return self.report_histogram(title, series, values, iteration or 0, labels=labels, xlabels=xlabels,
@ -239,6 +239,7 @@ class Logger(object):
xaxis=None, # type: Optional[str]
yaxis=None, # type: Optional[str]
mode=None, # type: Optional[str]
data_args=None, # type: Optional[dict]
extra_layout=None, # type: Optional[dict]
):
"""
@ -267,6 +268,9 @@ class Logger(object):
:param xaxis: The x-axis title. (Optional)
:param yaxis: The y-axis title. (Optional)
:param mode: Multiple histograms mode, stack / group / relative. Default is 'group'.
:param data_args: optional dictionary for data configuration, passed directly to plotly
See full details on the supported configuration: https://plotly.com/javascript/reference/bar/
example: data_args={'orientation': 'h', 'marker': {'color': 'blue'}}
:param extra_layout: optional dictionary for layout configuration, passed directly to plotly
See full details on the supported configuration: https://plotly.com/javascript/reference/bar/
example: extra_layout={'xaxis': {'type': 'date', 'range': ['2020-01-01', '2020-01-31']}}
@ -289,6 +293,7 @@ class Logger(object):
xtitle=xaxis,
ytitle=yaxis,
mode=mode or 'group',
data_args=data_args,
layout_config=extra_layout,
)

View File

@ -12,7 +12,7 @@ from attr import attrs, attrib
def create_2d_histogram_plot(np_row_wise, labels, title=None, xtitle=None, ytitle=None, series=None, xlabels=None,
comment=None, mode='group', layout_config=None):
comment=None, mode='group', data_args=None, layout_config=None):
"""
Create a 2D Plotly histogram chart from a 2D numpy array
:param np_row_wise: 2D numpy data array
@ -20,8 +20,10 @@ def create_2d_histogram_plot(np_row_wise, labels, title=None, xtitle=None, ytitl
:param title: Chart title
:param xtitle: X-Series title
:param ytitle: Y-Series title
:param xlabels: The labels of the x axis.
:param comment: comment underneath the title
:param mode: multiple histograms mode. valid options are: stack / group / relative. Default is 'group'.
:param data_args: optional extra data configuration
:param layout_config: optional extra layout configuration
:return: Plotly chart dict.
"""
@ -44,8 +46,9 @@ def create_2d_histogram_plot(np_row_wise, labels, title=None, xtitle=None, ytitl
elif not labels and xlabels:
labels = [series]
data = [_np_row_to_plotly_data_item(np_row=np_row_wise[i, :], label=labels[i] if labels else None, xlabels=xlabels)
for i in range(np_row_wise.shape[0])]
data = [_np_row_to_plotly_data_item(
np_row=np_row_wise[i, :], label=labels[i] if labels else None, xlabels=xlabels, data_args=data_args
) for i in range(np_row_wise.shape[0])]
return _plotly_hist_dict(title=series if use_series else title,
xtitle=xtitle, ytitle=ytitle, mode=mode, data=data, comment=comment,
layout_config=layout_config)
@ -410,11 +413,13 @@ def _plotly_hist_dict(title, xtitle, ytitle, mode='group', data=None, comment=No
return plotly_object
def _np_row_to_plotly_data_item(np_row, label, xlabels=None):
def _np_row_to_plotly_data_item(np_row, label, xlabels=None, data_args=None):
"""
Convert a numpy data row into a Plotly chart data item
:param np_row: numpy 1D data row
:param label: Item label
:param xlabels: The labels of the x axis.
:param dict data_args: Plotly data arguments
:return: A plotly data item dict.
"""
bins = list(range(np_row.shape[0])) if xlabels is None else list(xlabels)
@ -426,6 +431,8 @@ def _np_row_to_plotly_data_item(np_row, label, xlabels=None):
# "text": mylabels,
"type": "bar"
}
if data_args:
this_trace_data.update(data_args)
return this_trace_data