-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
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
base: main
Are you sure you want to change the base?
Changes from all commits
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 |
---|---|---|
|
@@ -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) | ||
|
||
bs_index = np.random.randint(M, size=(N, M)) | ||
bsData = data[bs_index] | ||
|
@@ -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)) | ||
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 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 | ||
|
||
|
@@ -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) | ||
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. ditto, leave as before. |
||
|
||
# interquartile range | ||
stats['iqr'] = q3 - q1 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
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 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:
and that function |
||
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]]) | ||
|
@@ -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([ | ||
|
@@ -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]]) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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], | ||
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 guess I would just write this is as |
||
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 | ||
|
@@ -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 | ||
|
There was a problem hiding this comment.
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.)