-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Fixed error in plot_histogram when number_to_keep is smaller that the number of keys #7481
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
Changes from all commits
a7d973a
05a9767
877322f
ea6321a
44f5daa
94ae6d2
8ab7d91
5a1abe7
88dd944
b997c9a
4dd9589
2705650
fcf7025
30583fe
993d686
f636ac5
dbbc4f2
b8fb075
6337b6c
25545c8
847c6a6
79b7e01
e4ade96
bfef156
235a9d6
d54af7b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| --- | ||
| fixes: | ||
| - | | ||
| Fixed a bug in :func:`~qiskit.visualization.plot_histogram` when the | ||
| ``number_to_keep`` argument was smaller that the number of keys. The | ||
| following code will not throw errors and will be properly aligned:: | ||
|
|
||
| from qiskit.visualization import plot_histogram | ||
| data = {'00': 3, '01': 5, '11': 8, '10': 11} | ||
| plot_histogram(data, number_to_keep=2) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,15 +13,21 @@ | |
| """Tests for plot_histogram.""" | ||
|
|
||
| import unittest | ||
| from io import BytesIO | ||
| from collections import Counter | ||
|
|
||
| import matplotlib as mpl | ||
| from PIL import Image | ||
|
|
||
| from qiskit.test import QiskitTestCase | ||
| from qiskit.tools.visualization import plot_histogram | ||
| from qiskit.utils import optionals | ||
| from .visualization import QiskitVisualizationTestCase | ||
|
|
||
|
|
||
| class TestPlotHistogram(QiskitTestCase): | ||
| class TestPlotHistogram(QiskitVisualizationTestCase): | ||
| """Qiskit plot_histogram tests.""" | ||
|
|
||
| @unittest.skipUnless(optionals.HAS_MATPLOTLIB, "matplotlib not available.") | ||
| def test_different_counts_lengths(self): | ||
| """Test plotting two different length dists works""" | ||
| exact_dist = { | ||
|
|
@@ -107,6 +113,107 @@ def test_different_counts_lengths(self): | |
| fig = plot_histogram([raw_dist, exact_dist]) | ||
| self.assertIsInstance(fig, mpl.figure.Figure) | ||
|
|
||
| @unittest.skipUnless(optionals.HAS_MATPLOTLIB, "matplotlib not available.") | ||
| def test_with_number_to_keep(self): | ||
| """Test plotting using number_to_keep""" | ||
| dist = {"00": 3, "01": 5, "11": 8, "10": 11} | ||
| fig = plot_histogram(dist, number_to_keep=2) | ||
| self.assertIsInstance(fig, mpl.figure.Figure) | ||
|
|
||
| @unittest.skipUnless(optionals.HAS_MATPLOTLIB, "matplotlib not available.") | ||
| def test_with_number_to_keep_multiple_executions(self): | ||
| """Test plotting using number_to_keep with multiple executions""" | ||
| dist = [{"00": 3, "01": 5, "11": 8, "10": 11}, {"00": 3, "01": 7, "10": 11}] | ||
| fig = plot_histogram(dist, number_to_keep=2) | ||
| self.assertIsInstance(fig, mpl.figure.Figure) | ||
|
|
||
| @unittest.skipUnless(optionals.HAS_MATPLOTLIB, "matplotlib not available.") | ||
| def test_with_number_to_keep_multiple_executions_correct_image(self): | ||
| """Test plotting using number_to_keep with multiple executions""" | ||
| data_noisy = { | ||
| "00000": 0.22, | ||
| "00001": 0.003, | ||
| "00010": 0.005, | ||
| "00011": 0.0, | ||
| "00100": 0.004, | ||
| "00101": 0.001, | ||
| "00110": 0.004, | ||
| "00111": 0.001, | ||
| "01000": 0.005, | ||
| "01001": 0.0, | ||
| "01010": 0.002, | ||
| "01011": 0.0, | ||
| "01100": 0.225, | ||
| "01101": 0.001, | ||
| "01110": 0.003, | ||
| "01111": 0.003, | ||
| "10000": 0.012, | ||
| "10001": 0.002, | ||
| "10010": 0.001, | ||
| "10011": 0.001, | ||
| "10100": 0.247, | ||
| "10101": 0.004, | ||
| "10110": 0.003, | ||
| "10111": 0.001, | ||
| "11000": 0.225, | ||
| "11001": 0.005, | ||
| "11010": 0.002, | ||
| "11011": 0.0, | ||
| "11100": 0.015, | ||
| "11101": 0.004, | ||
| "11110": 0.001, | ||
| "11111": 0.0, | ||
| } | ||
| data_ideal = { | ||
| "00000": 0.25, | ||
| "00001": 0, | ||
| "00010": 0, | ||
| "00011": 0, | ||
| "00100": 0, | ||
| "00101": 0, | ||
| "00110": 0, | ||
| "00111": 0.0, | ||
| "01000": 0.0, | ||
| "01001": 0, | ||
| "01010": 0.0, | ||
| "01011": 0.0, | ||
| "01100": 0.25, | ||
| "01101": 0, | ||
| "01110": 0, | ||
| "01111": 0, | ||
| "10000": 0, | ||
| "10001": 0, | ||
| "10010": 0.0, | ||
| "10011": 0.0, | ||
| "10100": 0.25, | ||
| "10101": 0, | ||
| "10110": 0, | ||
| "10111": 0, | ||
| "11000": 0.25, | ||
| "11001": 0, | ||
| "11010": 0, | ||
| "11011": 0, | ||
| "11100": 0.0, | ||
| "11101": 0, | ||
| "11110": 0, | ||
| "11111": 0.0, | ||
| } | ||
| data_ref_noisy = dict(Counter(data_noisy).most_common(5)) | ||
| data_ref_noisy["rest"] = sum(data_noisy.values()) - sum(data_ref_noisy.values()) | ||
| data_ref_ideal = dict(Counter(data_ideal).most_common(4)) # do not add 0 values | ||
| data_ref_ideal["rest"] = 0 | ||
| figure_ref = plot_histogram([data_ref_ideal, data_ref_noisy]) | ||
| figure_truncated = plot_histogram([data_ideal, data_noisy], number_to_keep=5) | ||
| with BytesIO() as img_buffer_ref: | ||
| figure_ref.savefig(img_buffer_ref, format="png") | ||
| img_buffer_ref.seek(0) | ||
|
Comment on lines
+207
to
+209
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Someone from the qiskit team will need to check how saving reference images is supposed to work (is it supposed to be saved to a file?). I didn't figure that out on my previous pass at roughly this issue. Looks good otherwise, took me a second to figure out what this test is actually doing but looks good now.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah.. that. We use snapshot testing for this kind of things (introduced in #4544). However, it seems like |
||
| with BytesIO() as img_buffer: | ||
| figure_truncated.savefig(img_buffer, format="png") | ||
| img_buffer.seek(0) | ||
| self.assertImagesAreEqual(Image.open(img_buffer_ref), Image.open(img_buffer), 0.2) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure about this 0.2 threshold. I don't have a good reference for how big this should be. |
||
| mpl.pyplot.close(figure_ref) | ||
| mpl.pyplot.close(figure_truncated) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main(verbosity=2) | ||
Uh oh!
There was an error while loading. Please reload this page.