Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Improve speed in some modules #22678

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions lib/matplotlib/cbook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1150,7 +1150,7 @@ def boxplot_stats(X, whis=1.5, bootstrap=None, labels=None,
def _bootstrap_median(data, N=5000):
# determine 95% confidence intervals of the median
M = len(data)
percentiles = [2.5, 97.5]
percentiles = (2.5, 97.5)
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this matters wrt speed (does it?); also I prefer a list here as this matches "more" the return type of np.percentile below (a ndarray is closer to a list than to a tuple.)


bs_index = np.random.randint(M, size=(N, M))
bsData = data[bs_index]
Expand All @@ -1169,8 +1169,9 @@ def _compute_conf_interval(data, med, iqr, bootstrap):
else:

N = len(data)
notch_min = med - 1.57 * iqr / np.sqrt(N)
notch_max = med + 1.57 * iqr / np.sqrt(N)
half_height = 1.57 * iqr / (N ** (1 / 2))
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess you could inline len(data) into the computation here.

notch_min = med - half_height
notch_max = med + half_height

return notch_min, notch_max

Expand Down Expand Up @@ -1221,7 +1222,8 @@ def _compute_conf_interval(data, med, iqr, bootstrap):
stats['mean'] = np.mean(x)

# medians and quartiles
q1, med, q3 = np.percentile(x, [25, 50, 75])
percentiles = (25, 50, 75)
q1, med, q3 = np.percentile(x, percentiles)
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto, leave as before.


# interquartile range
stats['iqr'] = q3 - q1
Expand Down
6 changes: 3 additions & 3 deletions lib/matplotlib/markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ def _set_pentagon(self):
self._path = polypath
else:
verts = polypath.vertices
y = (1 + np.sqrt(5)) / 4.
y = (1 + 5 ** (1 / 2)) / 4.
Copy link
Member

Choose a reason for hiding this comment

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

I'm not convinced of all these sqrt micro-optimizations. IMHO it makes the code less readable, and the performance gain is minimal. For example here

The sqrt is less than 2% of the the time of the immediate surrounding code in the function:

In [21]: %%timeit
    ...: Affine2D().scale(0.5)
    ...: polypath = Path.unit_regular_polygon(5)
    ...: verts = polypath.vertices
    ...: top = Path(verts[[0, 1, 4, 0]])
    ...: bottom = Path(verts[[1, 2, 3, 4, 1]])
    ...: left = Path([verts[0], verts[1], verts[2], [0, -y], verts[0]])
    ...: right = Path([verts[0], verts[4], verts[3], [0, -y], verts[0]])
    ...: 
61.1 µs ± 228 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)

In [22]: %timeit np.sqrt(5)
2.36 µs ± 35 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)

and that function _set_pentagon() again is only used to create a new marker object, which in itself is quite rare. So I bet the performance gain is not measurable in any real example.

top = Path(verts[[0, 1, 4, 0]])
bottom = Path(verts[[1, 2, 3, 4, 1]])
left = Path([verts[0], verts[1], verts[2], [0, -y], verts[0]])
Expand Down Expand Up @@ -747,7 +747,7 @@ def _set_hexagon2(self):
else:
verts = polypath.vertices
# not drawing inside lines
x, y = np.sqrt(3) / 4, 3 / 4.
x, y = 3 ** (1 / 2) / 4, 3 / 4.
top = Path(verts[[1, 0, 5, 4, 1]])
bottom = Path(verts[1:5])
left = Path(np.concatenate([
Expand All @@ -772,7 +772,7 @@ def _set_octagon(self):
self._transform.rotate_deg(22.5)
self._path = polypath
else:
x = np.sqrt(2.) / 4.
x = 2 ** (1 / 2) / 4.
self._path = self._alt_path = Path(
[[0, -1], [0, 1], [-x, 1], [-1, x],
[-1, -x], [-x, -1], [0, -1]])
Expand Down
94 changes: 49 additions & 45 deletions lib/matplotlib/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -822,45 +822,48 @@ def circle(cls, center=(0., 0.), radius=1., readonly=False):
Bezier Cubic Splines <https://www.tinaja.com/glib/ellipse4.pdf>`_.
"""
MAGIC = 0.2652031
SQRTHALF = np.sqrt(0.5)
SQRTHALF = (1 / 2) ** (1 / 2)
MAGIC45 = SQRTHALF * MAGIC
MAGICSUM = SQRTHALF + MAGIC45
MAGICDIFF = SQRTHALF - MAGIC45

vertices = np.array([[0.0, -1.0],
Copy link
Contributor

@anntzer anntzer Apr 24, 2022

Choose a reason for hiding this comment

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

I guess I would just write this is as vertices = np.array([...]) without even bothering with the dtype=float which is unneeded; this also avoids a temporary variable (mostly avoids having to keep track of it mentally). Ditto below.

vertices = ((0.0, -1.0),

[MAGIC, -1.0],
[SQRTHALF-MAGIC45, -SQRTHALF-MAGIC45],
[SQRTHALF, -SQRTHALF],
(MAGIC, -1.0),
(MAGICDIFF, -MAGICSUM),
(SQRTHALF, -SQRTHALF),

[SQRTHALF+MAGIC45, -SQRTHALF+MAGIC45],
[1.0, -MAGIC],
[1.0, 0.0],
(MAGICSUM, -MAGICDIFF),
(1.0, -MAGIC),
(1.0, 0.0),

[1.0, MAGIC],
[SQRTHALF+MAGIC45, SQRTHALF-MAGIC45],
[SQRTHALF, SQRTHALF],
(1.0, MAGIC),
(MAGICSUM, MAGICDIFF),
(SQRTHALF, SQRTHALF),

[SQRTHALF-MAGIC45, SQRTHALF+MAGIC45],
[MAGIC, 1.0],
[0.0, 1.0],
(MAGICDIFF, MAGICSUM),
(MAGIC, 1.0),
(0.0, 1.0),

[-MAGIC, 1.0],
[-SQRTHALF+MAGIC45, SQRTHALF+MAGIC45],
[-SQRTHALF, SQRTHALF],
(-MAGIC, 1.0),
(-MAGICDIFF, MAGICSUM),
(-SQRTHALF, SQRTHALF),

[-SQRTHALF-MAGIC45, SQRTHALF-MAGIC45],
[-1.0, MAGIC],
[-1.0, 0.0],
(-MAGICSUM, MAGICDIFF),
(-1.0, MAGIC),
(-1.0, 0.0),

[-1.0, -MAGIC],
[-SQRTHALF-MAGIC45, -SQRTHALF+MAGIC45],
[-SQRTHALF, -SQRTHALF],
(-1.0, -MAGIC),
(-MAGICSUM, -MAGICDIFF),
(-SQRTHALF, -SQRTHALF),

[-SQRTHALF+MAGIC45, -SQRTHALF-MAGIC45],
[-MAGIC, -1.0],
[0.0, -1.0],
(-MAGICDIFF, -MAGICSUM),
(-MAGIC, -1.0),
(0.0, -1.0),

[0.0, -1.0]],
dtype=float)
(0.0, -1.0))

vertices = np.array(vertices, dtype=float)

codes = [cls.CURVE4] * 26
codes[0] = cls.MOVETO
Expand All @@ -878,31 +881,32 @@ def unit_circle_righthalf(cls):
"""
if cls._unit_circle_righthalf is None:
MAGIC = 0.2652031
SQRTHALF = np.sqrt(0.5)
SQRTHALF = (1 / 2) ** (1 / 2)
MAGIC45 = SQRTHALF * MAGIC
MAGICSUM = SQRTHALF + MAGIC45
MAGICDIFF = SQRTHALF - MAGIC45

vertices = np.array(
[[0.0, -1.0],
vertices = ((0.0, -1.0),

[MAGIC, -1.0],
[SQRTHALF-MAGIC45, -SQRTHALF-MAGIC45],
[SQRTHALF, -SQRTHALF],
(MAGIC, -1.0),
(MAGICDIFF, -MAGICSUM),
(SQRTHALF, -SQRTHALF),

[SQRTHALF+MAGIC45, -SQRTHALF+MAGIC45],
[1.0, -MAGIC],
[1.0, 0.0],
(MAGICSUM, -MAGICDIFF),
(1.0, -MAGIC),
(1.0, 0.0),

[1.0, MAGIC],
[SQRTHALF+MAGIC45, SQRTHALF-MAGIC45],
[SQRTHALF, SQRTHALF],
(1.0, MAGIC),
(MAGICSUM, MAGICDIFF),
(SQRTHALF, SQRTHALF),

[SQRTHALF-MAGIC45, SQRTHALF+MAGIC45],
[MAGIC, 1.0],
[0.0, 1.0],
(MAGICDIFF, MAGICSUM),
(MAGIC, 1.0),
(0.0, 1.0),

[0.0, -1.0]],
(0.0, -1.0))

float)
vertices = np.array(vertices, dtype=float)

codes = np.full(14, cls.CURVE4, dtype=cls.code_type)
codes[0] = cls.MOVETO
Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/patheffects.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,8 +411,8 @@ def __init__(self, offset=(0, 0),
to your right, and 180 behind you.
length : float, default: 1.414
The length of the tick relative to spacing.
Recommended length = 1.414 (sqrt(2)) when angle=45, length=1.0
when angle=90 and length=2.0 when angle=60.
Recommended length = 1.414 (:math:`\\sqrt{2}`) when angle=45,
length=1.0 when angle=90 and length=2.0 when angle=60.
**kwargs
Extra keywords are stored and passed through to
:meth:`AbstractPathEffect._update_gc`.
Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/projections/polar.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def transform_non_affine(self, xy):
# docstring inherited
x, y = xy.T
r = np.hypot(x, y)
theta = (np.arctan2(y, x) + 2 * np.pi) % (2 * np.pi)
theta = np.arctan2(y, x) % (2 * np.pi)
# PolarAxes does not use the theta transforms here, but apply them for
# backwards-compatibility if not being used by it.
if self._apply_theta_transforms and self._axis is not None:
Expand Down Expand Up @@ -1076,7 +1076,7 @@ def set_theta_zero_location(self, loc, offset=0.0):
'SW': np.pi * 1.25,
'S': np.pi * 1.5,
'SE': np.pi * 1.75,
'E': 0,
'E': 0.,
'NE': np.pi * 0.25}
return self.set_theta_offset(mapping[loc] + np.deg2rad(offset))

Expand Down