guix-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

16/17: gnu: python-matplotlib: Fix flaky Legend loc test.


From: guix-commits
Subject: 16/17: gnu: python-matplotlib: Fix flaky Legend loc test.
Date: Thu, 18 Jan 2024 07:58:12 -0500 (EST)

jpoiret pushed a commit to branch core-updates
in repository guix.

commit f8a9dbe74b97acf2a7a6e5d1961cacc694c78d9a
Author: Josselin Poiret <dev@jpoiret.xyz>
AuthorDate: Tue Jan 16 17:38:00 2024 +0100

    gnu: python-matplotlib: Fix flaky Legend loc test.
    
    * gnu/packages/patches/python-matplotlib-fix-legend-loc-best-test.patch: 
New patch.
    * gnu/local.mk (dist_patch_DATA): Register it.
    * gnu/packages/python-xyz.scm (python-matplotlib): Use it.
    
    Change-Id: I0b488844d7b34a718b7294134b8c954492c9b697
---
 gnu/local.mk                                       |  1 +
 ...ython-matplotlib-fix-legend-loc-best-test.patch | 84 ++++++++++++++++++++++
 gnu/packages/python-xyz.scm                        |  3 +-
 3 files changed, 87 insertions(+), 1 deletion(-)

diff --git a/gnu/local.mk b/gnu/local.mk
index 27697c14e2..ca2fdff8df 100644
--- a/gnu/local.mk
+++ b/gnu/local.mk
@@ -1813,6 +1813,7 @@ dist_patch_DATA =                                         
\
   %D%/packages/patches/python-docrepr-fix-tests.patch          \
   %D%/packages/patches/python-feedparser-missing-import.patch  \
   %D%/packages/patches/python-louvain-fix-test.patch           \
+  %D%/packages/patches/python-matplotlib-fix-legend-loc-best-test.patch        
\
   %D%/packages/patches/python-random2-getrandbits-test.patch           \
   %D%/packages/patches/python-pillow-use-zlib-1.3.patch        \
   %D%/packages/patches/python-poppler-qt5-fix-build.patch      \
diff --git 
a/gnu/packages/patches/python-matplotlib-fix-legend-loc-best-test.patch 
b/gnu/packages/patches/python-matplotlib-fix-legend-loc-best-test.patch
new file mode 100644
index 0000000000..9046eb2b4c
--- /dev/null
+++ b/gnu/packages/patches/python-matplotlib-fix-legend-loc-best-test.patch
@@ -0,0 +1,84 @@
+From 3cc6610597ee16a0cce39f7b033ae529972177e7 Mon Sep 17 00:00:00 2001
+From: Elliott Sales de Andrade <quantum.analyst@gmail.com>
+Date: Thu, 10 Aug 2023 00:09:53 -0400
+Subject: [PATCH] TST: Improve test for Legend(loc='best') warning
+
+By patching the timer instead of using actually large data, we can both
+a) speed up these tests (~7.5s vs <0.2s for both), and b) consistently
+trigger the warning even on systems which are fast (such as the M1
+systems on Cirrus.)
+
+Also, copy the test data from `test_legend_auto3`, which correctly hits
+all candidate locations for the 'best' legend locator without having to
+fill up the entire Axes with data.
+---
+ lib/matplotlib/tests/test_legend.py | 38 ++++++++++++++++++++---------
+ 1 file changed, 27 insertions(+), 11 deletions(-)
+
+diff --git a/lib/matplotlib/tests/test_legend.py 
b/lib/matplotlib/tests/test_legend.py
+index 759ac6aadaff..1549354ba56b 100644
+--- a/lib/matplotlib/tests/test_legend.py
++++ b/lib/matplotlib/tests/test_legend.py
+@@ -1,5 +1,7 @@
+ import collections
++import itertools
+ import platform
++import time
+ from unittest import mock
+ import warnings
+ 
+@@ -1109,29 +1111,43 @@ def test_usetex_no_warn(caplog):
+     assert "Font family ['serif'] not found." not in caplog.text
+ 
+ 
+-def test_warn_big_data_best_loc():
++def test_warn_big_data_best_loc(monkeypatch):
++    # Force _find_best_position to think it took a long time.
++    counter = itertools.count(0, step=1.5)
++    monkeypatch.setattr(time, 'perf_counter', lambda: next(counter))
++
+     fig, ax = plt.subplots()
+     fig.canvas.draw()  # So that we can call draw_artist later.
+-    for idx in range(1000):
+-        ax.plot(np.arange(5000), label=idx)
++
++    # Place line across all possible legend locations.
++    x = [0.9, 0.1, 0.1, 0.9, 0.9, 0.5]
++    y = [0.95, 0.95, 0.05, 0.05, 0.5, 0.5]
++    ax.plot(x, y, 'o-', label='line')
++
+     with rc_context({'legend.loc': 'best'}):
+         legend = ax.legend()
+-    with pytest.warns(UserWarning) as records:
++    with pytest.warns(UserWarning,
++                      match='Creating legend with loc="best" can be slow with 
large '
++                      'amounts of data.') as records:
+         fig.draw_artist(legend)  # Don't bother drawing the lines -- it's 
slow.
+     # The _find_best_position method of Legend is called twice, duplicating
+     # the warning message.
+     assert len(records) == 2
+-    for record in records:
+-        assert str(record.message) == (
+-            'Creating legend with loc="best" can be slow with large '
+-            'amounts of data.')
+ 
+ 
+-def test_no_warn_big_data_when_loc_specified():
++def test_no_warn_big_data_when_loc_specified(monkeypatch):
++    # Force _find_best_position to think it took a long time.
++    counter = itertools.count(0, step=1.5)
++    monkeypatch.setattr(time, 'perf_counter', lambda: next(counter))
++
+     fig, ax = plt.subplots()
+     fig.canvas.draw()
+-    for idx in range(1000):
+-        ax.plot(np.arange(5000), label=idx)
++
++    # Place line across all possible legend locations.
++    x = [0.9, 0.1, 0.1, 0.9, 0.9, 0.5]
++    y = [0.95, 0.95, 0.05, 0.05, 0.5, 0.5]
++    ax.plot(x, y, 'o-', label='line')
++
+     legend = ax.legend('best')
+     fig.draw_artist(legend)  # Check that no warning is emitted.
+ 
diff --git a/gnu/packages/python-xyz.scm b/gnu/packages/python-xyz.scm
index 020b2a330f..d362b4bbe9 100644
--- a/gnu/packages/python-xyz.scm
+++ b/gnu/packages/python-xyz.scm
@@ -8442,7 +8442,8 @@ comparison.
        (method url-fetch)
        (uri (pypi-uri "matplotlib" version))
        (sha256
-        (base32 "18amhxyxa6yzy1nwky4ggdgvvxnbl3qz2lki05vfx0dqf6w7ia81"))))
+        (base32 "18h78s5ld1i6mz00w258hy29909nfr3ddq6ry9kq18agw468bks8"))
+       (patches (search-patches 
"python-matplotlib-fix-legend-loc-best-test.patch"))))
     (build-system pyproject-build-system)
     (arguments
      (list



reply via email to

[Prev in Thread] Current Thread [Next in Thread]