Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conform iplot_histogram and plot_histogram #866

Merged
merged 6 commits into from
Sep 7, 2018

Conversation

mtreinish
Copy link
Member

Summary

This commit fixes the inconsistencies between the 2 histogram plot
functions iplot_histogram (for an interactive js plot) and
plot_histogram (for a matplotlib generated plot). Both functions perform
the same function just using different tools. To make them
interchangeable this commit addresses the differences between the 2 and
makes the interface the same between the 2. However, because of
backwards compat concerns with plot_histogram (which has been present in
several releases already) there is a some duplication of number_to_keep
kwarg and the options dict. This deprecates the number_to_keep parameter
so users can migrate off of it and then we can remove it from both
plot_histogram and iplot_histogram.

With the interfaces to both functions being the same this commit also
updates the init module to leverage iplot_histogram by default if
qiskit.tools.visualization.plot_histogram() is called and we're running
under jupyter and have a network connection to download the necessary
js.

Details and comments

Fixes #864

This commit fixes the inconsistencies between the 2 histogram plot
functions iplot_histogram (for an interactive js plot) and
plot_histogram (for a matplotlib generated plot). Both functions perform
the same function just using different tools. To make them
interchangeable this commit addresses the differences between the 2 and
makes the interface the same between the 2. However, because of
backwards compat concerns with plot_histogram (which has been present in
several releases already) there is a some duplication of number_to_keep
kwarg and the options dict. This deprecates the number_to_keep parameter
so users can migrate off of it and then we can remove it from both
plot_histogram and iplot_histogram.

With the interfaces to both functions being the same this commit also
updates the __init__ module to leverage iplot_histogram by default if
qiskit.tools.visualization.plot_histogram() is called and we're running
under jupyter and have a network connection to download the necessary
js.

Fixes Qiskit#864
@mtreinish mtreinish force-pushed the plot-histogram-conform branch from bea039d to 39ca802 Compare September 5, 2018 23:18
jaygambetta
jaygambetta previously approved these changes Sep 5, 2018
@jaygambetta
Copy link
Member

this was fast :-)

Copy link
Contributor

@delapuente delapuente left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this addition, I think it's great to have a homogeneous API for visualizations.

Can you also add a couple of entries in the changelog to refer to this change and the other introduced in #862?

@@ -17,35 +17,68 @@
import matplotlib.pyplot as plt


def plot_histogram(data, number_to_keep=False):
def plot_histogram(data, number_to_keep=False, legend=None, options=None):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we emit deprecation warnings for number_to_keep? Change the signature of the function to:

def plot_histogram(data, legend=None, options=None, **kwargs):

And check if number_to_keep has been passed. If so, emit a deprecation warning:

if 'numbers_to_keep' in kwargs:
  warnings.warn('number_to_keep has been deprecated, use ...' DeprecationWarning)

numbers_to_keep = kwargs.get('numbers_to_keep', False)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't want to change the signature here, because the order of named kwargs actually matters. For example someone could be using this like: plot_histogram(data_input, 5) and that would work fine but if I move numbers_to_keep those users will be broken. But, I'll add the deprecation warning

if isinstance(data, dict):
data = [data]
if legend and len(legend) != 1:
raise Exception("Length of legendL %s doesn't match number of "
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We try to use functionality-specific errors, subclassing QiskitError. I think a VisualizationError class would make sense here. Do you mind to create this new class of errors and pass through other raise statements and change them accordingly?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

ax.text(rect.get_x() + rect.get_width() / 2., 1.05 * height,
'%f' % float(height),
ha='center', va='bottom')
for item, execution in enumerate(data):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking: do you think is it feasible or even useful to allow customization of graph width, graph height and sort to equate the options dictionary passed to this function to that supported by the iplot_function?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be, and I thought about that when I was refactoring things. But I didn't want to change things too much here. After this merges I'll work on updating this to add the missing options.

single bar called other values
Args:
data (list or dict): This is either a list of dictionaries containing:
values to represent (ex {'001': 130})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you complete this sentence: "either ... or"?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

This commit fixes several issue related to the plot_histogram function.
Specifically it creates a new VisualizationError Exception class for
errors raised during execution, now properly emits a DeprecationWarning
when the deprecated number_to_keep kwarg is used, and updateds unclear
docstrings.
This commit updates the changelog to document the recent changes to the
visualization module.
This comimt clenas up the pyllint errors in the newly added
visualization._error and visualization._counts_visualization modules.
@mtreinish mtreinish force-pushed the plot-histogram-conform branch from 1ffeb0e to 4a1ddb6 Compare September 6, 2018 20:46
ajavadia
ajavadia previously approved these changes Sep 7, 2018
Copy link
Contributor

@delapuente delapuente left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good! Thank you, @mtreinish.

@delapuente delapuente merged commit 9fb88b0 into Qiskit:master Sep 7, 2018
@mtreinish mtreinish deleted the plot-histogram-conform branch September 7, 2018 11:37
lia-approves pushed a commit to edasgupta/qiskit-terra that referenced this pull request Jul 30, 2019
This commit fixes the inconsistencies between the 2 histogram plot
functions iplot_histogram (for an interactive js plot) and
plot_histogram (for a matplotlib generated plot). Both functions perform
the same function just using different tools. To make them
interchangeable this commit addresses the differences between the 2 and
makes the interface the same between the 2. However, because of
backwards compat concerns with plot_histogram (which has been present in
several releases already) there is a some duplication of number_to_keep
kwarg and the options dict. This deprecates the number_to_keep parameter
so users can migrate off of it and then we can remove it from both
plot_histogram and iplot_histogram.

With the interfaces to both functions being the same this commit also
updates the __init__ module to leverage iplot_histogram by default if
qiskit.tools.visualization.plot_histogram() is called and we're running
under jupyter and have a network connection to download the necessary
js.

It also creates a new VisualizationError Exception class for
errors raised during execution, now properly emits a DeprecationWarning
when the deprecated number_to_keep kwarg is used, and updateds unclear
docstrings.

Fixes Qiskit#864
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make plot_histogram & iplot_histogram similar
4 participants