Closed
Description
Hi!
I have a code using matplotlib.path to create a mask of points inside a polygon. Lately we moved from version 1.4 to 1.51 and at first I thought that our program started to hang but it turned out that it just took about 30x more time to create masks.
Profiler showed that the problem is with path.contains_points(pts).
I've extracted the problem to a unit test and did tests 1.4 vs 1.51:
1.4 - 70 ms
1.51 - 2215 ms
import unittest
import numpy as np
import math
import matplotlib
from matplotlib.path import Path
print matplotlib.__version__
class MyTestCase(unittest.TestCase):
def get_polygon_path(self, polygon_x, polygon_y):
vertices = zip(polygon_x, polygon_y)
p = Path(vertices, None, closed=True, readonly=True)
return p
def get_in_polygon(self, x1, x2, y1, y2, path):
x, y = np.meshgrid(np.arange(x1, x2), np.arange(y1, y2))
x, y = x.flatten(), y.flatten()
pts = np.vstack((x, y)).T
# Find points that belong to snake in minimal rectangle
grid = path.contains_points(pts)
def test_matplotlib(self):
for k in range(10):
angles = np.linspace(0, 2 * math.pi, 90)
px = 150 + 100 * np.cos(angles)
py = 150 + 100 * np.sin(angles)
poly = self.get_polygon_path(px, py)
self.get_in_polygon(50, 250, 50, 250, poly)
self.assertEqual(True, True)
if __name__ == '__main__':
unittest.main()
Am I doing or setting something wrong or is it indeed a problem with a new version?
Regards
Filip Mróz