Example #1
0
 def map(self, L, x, y):
     dd = 1.0 / (2.0**(int(L)))
     relx = int(x) * dd
     rely = int(y) * dd
     DW = (self.ds.domain_right_edge - self.ds.domain_left_edge)
     xl = self.ds.domain_left_edge[0] + relx * DW[0]
     yl = self.ds.domain_left_edge[1] + rely * DW[1]
     xr = xl + dd * DW[0]
     yr = yl + dd * DW[1]
     frb = FixedResolutionBuffer(self.data, (xl, xr, yl, yr), (256, 256))
     cmi, cma = get_color_bounds(
         self.data['px'], self.data['py'], self.data['pdx'],
         self.data['pdy'], self.data[self.field],
         self.ds.domain_left_edge[0], self.ds.domain_right_edge[0],
         self.ds.domain_left_edge[1], self.ds.domain_right_edge[1],
         dd * DW[0] / (64 * 256), dd * DW[0])
     if self.ds._get_field_info(self.field).take_log:
         cmi = np.log10(cmi)
         cma = np.log10(cma)
         to_plot = apply_colormap(np.log10(frb[self.field]),
                                  color_bounds=(cmi, cma))
     else:
         to_plot = apply_colormap(frb[self.field], color_bounds=(cmi, cma))
     rv = write_png_to_string(to_plot)
     return rv
 def map(self, L, x, y):
     dd = 1.0 / (2.0**(int(L)))
     relx = int(x) * dd
     rely = int(y) * dd
     DW = (self.ds.domain_right_edge - self.ds.domain_left_edge)
     xl = self.ds.domain_left_edge[0] + relx * DW[0]
     yl = self.ds.domain_left_edge[1] + rely * DW[1]
     xr = xl + dd*DW[0]
     yr = yl + dd*DW[1]
     frb = FixedResolutionBuffer(self.data, (xl, xr, yl, yr), (256, 256))
     cmi, cma = get_color_bounds(self.data['px'], self.data['py'],
                                 self.data['pdx'], self.data['pdy'],
                                 self.data[self.field],
                                 self.ds.domain_left_edge[0],
                                 self.ds.domain_right_edge[0],
                                 self.ds.domain_left_edge[1],
                                 self.ds.domain_right_edge[1],
                                 dd*DW[0] / (64*256),
                                 dd*DW[0])
     if self.ds._get_field_info(self.field).take_log:
         cmi = np.log10(cmi)
         cma = np.log10(cma)
         to_plot = apply_colormap(np.log10(frb[self.field]), color_bounds = (cmi, cma))
     else:
         to_plot = apply_colormap(frb[self.field], color_bounds = (cmi, cma))
     rv = write_png_to_string(to_plot)
     return rv
Example #3
0
    def apply_colormap(self):
        '''

        Applies a colormap to the current image without re-rendering.

        Parameters
        ----------
        cmap_name : string, optional
            An acceptable colormap.  See either yt.visualization.color_maps or
            http://www.scipy.org/Cookbook/Matplotlib/Show_colormaps .
        color_bounds : tuple of floats, optional
            The min and max to scale between.  Outlying values will be clipped.

        Returns
        -------
        current_image : A new image with the specified color scale applied to
            the underlying data.


        '''

        image = apply_colormap(self.data,
                               color_bounds=self._color_bounds,
                               cmap_name=self._cmap) / 255.
        alpha = image[:, :, 3]
        alpha[self.sampler.aimage_used == -1] = 0.0
        image[:, :, 3] = alpha
        return image
Example #4
0
    def map(self, field, L, x, y):
        if "," in field:
            field = tuple(field.split(","))
        cmap = self.cmap
        dd = 1.0 / (2.0**(int(L)))
        relx = int(x) * dd
        rely = int(y) * dd
        DW = self.ds.domain_right_edge - self.ds.domain_left_edge
        xl = self.ds.domain_left_edge[0] + relx * DW[0]
        yl = self.ds.domain_left_edge[1] + rely * DW[1]
        xr = xl + dd * DW[0]
        yr = yl + dd * DW[1]
        try:
            self.lock()
            w = 256  # pixels
            data = self.data[field]
            frb = FixedResolutionBuffer(self.data, (xl, xr, yl, yr), (w, w))
            cmi, cma = get_color_bounds(
                self.data["px"],
                self.data["py"],
                self.data["pdx"],
                self.data["pdy"],
                data,
                self.ds.domain_left_edge[0],
                self.ds.domain_right_edge[0],
                self.ds.domain_left_edge[1],
                self.ds.domain_right_edge[1],
                dd * DW[0] / (64 * 256),
                dd * DW[0],
            )
        finally:
            self.unlock()

        if self.takelog:
            cmi = np.log10(cmi)
            cma = np.log10(cma)
            to_plot = apply_colormap(np.log10(frb[field]),
                                     color_bounds=(cmi, cma),
                                     cmap_name=cmap)
        else:
            to_plot = apply_colormap(frb[field],
                                     color_bounds=(cmi, cma),
                                     cmap_name=cmap)

        rv = write_png_to_string(to_plot)
        return rv
Example #5
0
    def __init__(
        self, data_source, alpha=0.3, cmap=None, min_level=None, max_level=None
    ):
        self.data_source = data_source_or_all(data_source)
        corners = []
        levels = []
        for block, _mask in self.data_source.blocks:
            block_corners = np.array(
                [
                    [block.LeftEdge[0], block.LeftEdge[1], block.LeftEdge[2]],
                    [block.RightEdge[0], block.LeftEdge[1], block.LeftEdge[2]],
                    [block.RightEdge[0], block.RightEdge[1], block.LeftEdge[2]],
                    [block.LeftEdge[0], block.RightEdge[1], block.LeftEdge[2]],
                    [block.LeftEdge[0], block.LeftEdge[1], block.RightEdge[2]],
                    [block.RightEdge[0], block.LeftEdge[1], block.RightEdge[2]],
                    [block.RightEdge[0], block.RightEdge[1], block.RightEdge[2]],
                    [block.LeftEdge[0], block.RightEdge[1], block.RightEdge[2]],
                ],
                dtype="float64",
            )
            corners.append(block_corners)
            levels.append(block.Level)
        corners = np.dstack(corners)
        levels = np.array(levels)
        if cmap is None:
            cmap = ytcfg.get("yt", "default_colormap")

        if max_level is not None:
            subset = levels <= max_level
            levels = levels[subset]
            corners = corners[:, :, subset]
        if min_level is not None:
            subset = levels >= min_level
            levels = levels[subset]
            corners = corners[:, :, subset]

        colors = (
            apply_colormap(
                levels * 1.0,
                color_bounds=[0, self.data_source.ds.index.max_level],
                cmap_name=cmap,
            )[0, :, :]
            / 255.0
        )
        colors[:, 3] = alpha

        order = [0, 1, 1, 2, 2, 3, 3, 0]
        order += [4, 5, 5, 6, 6, 7, 7, 4]
        order += [0, 4, 1, 5, 2, 6, 3, 7]

        vertices = np.empty([corners.shape[2] * 2 * 12, 3])
        for i in range(3):
            vertices[:, i] = corners[order, i, ...].ravel(order="F")
        vertices = vertices.reshape((corners.shape[2] * 12, 2, 3))

        super(GridSource, self).__init__(vertices, colors, color_stride=24)
Example #6
0
    def map(self, field, L, x, y):
        if ',' in field:
            field = tuple(field.split(','))
        dd = 1.0 / (2.0**(int(L)))
        relx = int(x) * dd
        rely = int(y) * dd
        DW = (self.ds.domain_right_edge - self.ds.domain_left_edge)
        xl = self.ds.domain_left_edge[0] + relx * DW[0]
        yl = self.ds.domain_left_edge[1] + rely * DW[1]
        xr = xl + dd*DW[0]
        yr = yl + dd*DW[1]
        try:
            self.lock()
            data = self.data[field]
            frb = FixedResolutionBuffer(self.data, (xl, xr, yl, yr), (256, 256))
            cmi, cma = get_color_bounds(self.data['px'], self.data['py'],
                                        self.data['pdx'], self.data['pdy'],
                                        data,
                                        self.ds.domain_left_edge[0],
                                        self.ds.domain_right_edge[0],
                                        self.ds.domain_left_edge[1],
                                        self.ds.domain_right_edge[1],
                                        dd*DW[0] / (64*256),
                                        dd*DW[0])
        finally:
            self.unlock()

        if self.takelog:
            cmi = np.log10(cmi)
            cma = np.log10(cma)
            to_plot = apply_colormap(np.log10(frb[field]), color_bounds = (cmi, cma))
        else:
            to_plot = apply_colormap(frb[field], color_bounds = (cmi, cma))

        rv = write_png_to_string(to_plot)
        return rv
Example #7
0
    def apply_colormap(self):
        """

        Applies a colormap to the current image without re-rendering.

        Returns
        -------
        current_image : A new image with the specified color scale applied to
            the underlying data.


        """

        image = (apply_colormap(
            self.data, color_bounds=self._color_bounds, cmap_name=self._cmap) /
                 255.0)
        alpha = image[:, :, 3]
        alpha[self.sampler.aimage_used == -1] = 0.0
        image[:, :, 3] = alpha
        return image
Example #8
0
 def test_apply_colormap(self):
     x = np.array(np.random.randint(0, 256, size=(10, 10)), dtype="uint8")
     apply_colormap(x,
                    color_bounds=None,
                    cmap_name=None,
                    func=lambda x: x**2)
Example #9
0
    def __call__(self, plot):
        x0, x1 = plot.xlim
        y0, y1 = plot.ylim
        xx0, xx1 = plot._axes.get_xlim()
        yy0, yy1 = plot._axes.get_ylim()
        (dx, dy) = self.pixel_scale(plot)
        (xpix, ypix) = plot.image._A.shape
        ax = plot.data.axis
        px_index = plot.data.ds.coordinates.x_axis[ax]
        py_index = plot.data.ds.coordinates.y_axis[ax]
        DW = plot.data.ds.domain_width
        if self.periodic:
            pxs, pys = np.mgrid[-1:1:3j, -1:1:3j]
        else:
            pxs, pys = np.mgrid[0:0:1j, 0:0:1j]
        GLE, GRE, levels = [], [], []
        for block, mask in plot.data.blocks:
            GLE.append(block.LeftEdge.in_units("code_length"))
            GRE.append(block.RightEdge.in_units("code_length"))
            levels.append(block.Level)
        if len(GLE) == 0: return
        # Retain both units and registry
        GLE = YTArray(GLE, input_units=GLE[0].units)
        GRE = YTArray(GRE, input_units=GRE[0].units)
        levels = np.array(levels)
        min_level = self.min_level or 0
        max_level = self.max_level or levels.max()

        # sorts the three arrays in order of ascending level - this makes images look nicer
        new_indices = np.argsort(levels)
        levels = levels[new_indices]
        GLE = GLE[new_indices]
        GRE = GRE[new_indices]

        for px_off, py_off in zip(pxs.ravel(), pys.ravel()):
            pxo = px_off * DW[px_index]
            pyo = py_off * DW[py_index]
            left_edge_x = np.array((GLE[:, px_index] + pxo - x0) * dx) + xx0
            left_edge_y = np.array((GLE[:, py_index] + pyo - y0) * dy) + yy0
            right_edge_x = np.array((GRE[:, px_index] + pxo - x0) * dx) + xx0
            right_edge_y = np.array((GRE[:, py_index] + pyo - y0) * dy) + yy0
            xwidth = xpix * (right_edge_x - left_edge_x) / (xx1 - xx0)
            ywidth = ypix * (right_edge_y - left_edge_y) / (yy1 - yy0)
            visible = np.logical_and(
                np.logical_and(xwidth > self.min_pix, ywidth > self.min_pix),
                np.logical_and(levels >= min_level, levels <= max_level))

            # Grids can either be set by edgecolors OR a colormap.
            if self.edgecolors is not None:
                edgecolors = colorConverter.to_rgba(self.edgecolors,
                                                    alpha=self.alpha)
            else:  # use colormap if not explicity overridden by edgecolors
                if self.cmap is not None:
                    color_bounds = [0, plot.data.ds.index.max_level]
                    edgecolors = apply_colormap(
                        levels[visible] * 1.0,
                        color_bounds=color_bounds,
                        cmap_name=self.cmap)[0, :, :] * 1.0 / 255.
                    edgecolors[:, 3] = self.alpha
                else:
                    edgecolors = (0.0, 0.0, 0.0, self.alpha)

            if visible.nonzero()[0].size == 0: continue
            verts = np.array([
                (left_edge_x, left_edge_x, right_edge_x, right_edge_x),
                (left_edge_y, right_edge_y, right_edge_y, left_edge_y)
            ])
            verts = verts.transpose()[visible, :, :]
            grid_collection = matplotlib.collections.PolyCollection(
                verts,
                facecolors="none",
                edgecolors=edgecolors,
                linewidth=self.linewidth)
            plot._axes.hold(True)
            plot._axes.add_collection(grid_collection)

            if self.draw_ids:
                visible_ids = np.logical_and(
                    np.logical_and(xwidth > self.min_pix_ids,
                                   ywidth > self.min_pix_ids),
                    np.logical_and(levels >= min_level, levels <= max_level))
                active_ids = np.unique(plot.data['grid_indices'])
                for i in np.where(visible_ids)[0]:
                    plot._axes.text(left_edge_x[i] + (2 * (xx1 - xx0) / xpix),
                                    left_edge_y[i] + (2 * (yy1 - yy0) / ypix),
                                    "%d" % active_ids[i],
                                    clip_on=True)
            plot._axes.hold(False)