def chunk_limit_setup(): N = 100_000 dpi = 500 w = 5 * dpi h = 6 * dpi # just fit in the width x = np.linspace(0, w, N) # and go top-to-bottom y = np.ones(N) * h y[::2] = 0 idt = IdentityTransform() # make a renderer ra = RendererAgg(w, h, dpi) # setup the minimal gc to draw a line gc = ra.new_gc() gc.set_linewidth(1) gc.set_foreground('r') # make a Path p = Path(np.vstack((x, y)).T) # effectively disable path simplification (but leaving it "on") p.simplify_threshold = 0 return ra, gc, p, idt
def onselect(vertices): path = Path( vertices, closed = True) # fraction of a pixel difference below which vertices will be simplified out path.simplify_threshold = 1.0 path.should_simplify = True # path.contains_points(points = ) vertfilename = "maskselection.npz" savez_dict = dict(vertices = vertices) np.savez_compressed(vertfilename, **savez_dict)
def draw_path(self, gc, path, transform, rgbFace=None): # docstring inherited nmax = mpl.rcParams['agg.path.chunksize'] # here at least for testing npts = path.vertices.shape[0] if (npts > nmax > 100 and path.should_simplify and rgbFace is None and gc.get_hatch() is None): nch = np.ceil(npts / nmax) chsize = int(np.ceil(npts / nch)) i0 = np.arange(0, npts, chsize) i1 = np.zeros_like(i0) i1[:-1] = i0[1:] - 1 i1[-1] = npts for ii0, ii1 in zip(i0, i1): v = path.vertices[ii0:ii1, :] c = path.codes if c is not None: c = c[ii0:ii1] c[0] = Path.MOVETO # move to end of last chunk p = Path(v, c) p.simplify_threshold = path.simplify_threshold try: self._renderer.draw_path(gc, p, transform, rgbFace) except OverflowError: msg = ( "Exceeded cell block limit in Agg.\n\n" "Please reduce the value of " f"rcParams['agg.path.chunksize'] (currently {nmax}) " "or increase the path simplification threshold" "(rcParams['path.simplify_threshold'] = " f"{mpl.rcParams['path.simplify_threshold']:.2f} by " "default and path.simplify_threshold = " f"{path.simplify_threshold:.2f} on the input).") raise OverflowError(msg) from None else: try: self._renderer.draw_path(gc, path, transform, rgbFace) except OverflowError: cant_chunk = '' if rgbFace is not None: cant_chunk += "- can not split filled path\n" if gc.get_hatch() is not None: cant_chunk += "- can not split hatched path\n" if not path.should_simplify: cant_chunk += "- path.should_simplify is False\n" if len(cant_chunk): msg = ( "Exceeded cell block limit in Agg, however for the " "following reasons:\n\n" f"{cant_chunk}\n" "we can not automatically split up this path to draw." "\n\nPlease manually simplify your path.") else: inc_threshold = ( "or increase the path simplification threshold" "(rcParams['path.simplify_threshold'] = " f"{mpl.rcParams['path.simplify_threshold']} " "by default and path.simplify_threshold " f"= {path.simplify_threshold} " "on the input).") if nmax > 100: msg = ( "Exceeded cell block limit in Agg. Please reduce " "the value of rcParams['agg.path.chunksize'] " f"(currently {nmax}) {inc_threshold}") else: msg = ("Exceeded cell block limit in Agg. Please set " "the value of rcParams['agg.path.chunksize'], " f"(currently {nmax}) to be greater than 100 " + inc_threshold) raise OverflowError(msg) from None
# <codecell> import matplotlib.path as mpath from matplotlib.path import Path vertfilename = "maskselection.npz" data = np.load(vertfilename) verts = data['vertices'] path = Path(verts, closed = True) print len(path) path.should_simplify = True path.simplify_threshold = 10.0 # <codecell> vertices = [] codes = [] for (vertex, code) in path.iter_segments(simplify = True): vertices.append(vertex.tolist()) codes.append(code) # <codecell> cleanpath = Path(vertices, codes) len(cleanpath) # <codecell>