示例#1
0
    def __getitem__(self, item):
        if item in self.data:
            return self.data[item]

        mylog.info("Splatting (%s) onto a %d by %d mesh" %
                (item, self.buff_size[0], self.buff_size[1]))

        bounds = []
        for b in self.bounds:
            if hasattr(b, "in_units"):
                b = float(b.in_units("code_length"))
            bounds.append(b)

        x_data = self.data_source.dd[self.x_field]
        y_data = self.data_source.dd[self.y_field]
        data = self.data_source.dd[item]

        # convert to pixels
        px = (x_data - self.bounds[0]) / (self.bounds[1] - self.bounds[0])
        py = (y_data - self.bounds[2]) / (self.bounds[3] - self.bounds[2])

        # select only the particles that will actually show up in the image
        mask = np.logical_and(np.logical_and(px >= 0.0, px <= 1.0),
                              np.logical_and(py >= 0.0, py <= 1.0))

        weight_field = self.data_source.weight_field
        if weight_field is None:
            weight_data = np.ones_like(data.v)
        else:
            weight_data = self.data_source.dd[weight_field]
        splat_vals = weight_data[mask]*data[mask]

        # splat particles
        buff = np.zeros(self.buff_size)
        add_points_to_greyscale_image(buff,
                                      px[mask],
                                      py[mask],
                                      splat_vals)
        ia = ImageArray(buff, input_units=data.units,
                        info=self._get_info(item))

        # divide by the weight_field, if needed
        if weight_field is not None:
            weight_buff = np.zeros(self.buff_size)
            add_points_to_greyscale_image(weight_buff,
                                          px[mask],
                                          py[mask],
                                          weight_data[mask])
            weight_array = ImageArray(weight_buff,
                                      input_units=weight_data.units,
                                      info=self._get_info(item))

            locs = np.where(weight_array > 0)
            ia[locs] /= weight_array[locs]

        self.data[item] = ia
        return self.data[item]
示例#2
0
    def __getitem__(self, item):
        if item in self.data:
            return self.data[item]

        mylog.info(
            "Splatting (%s) onto a %d by %d mesh",
            item,
            self.buff_size[0],
            self.buff_size[1],
        )

        bounds = []
        for b in self.bounds:
            if hasattr(b, "in_units"):
                b = float(b.in_units("code_length"))
            bounds.append(b)

        ftype = item[0]
        x_data = self.data_source.dd[ftype, self.x_field]
        y_data = self.data_source.dd[ftype, self.y_field]
        data = self.data_source.dd[item]

        # handle periodicity
        dx = x_data.in_units("code_length").d - bounds[0]
        dy = y_data.in_units("code_length").d - bounds[2]
        if self.periodic:
            dx %= float(self._period[0].in_units("code_length"))
            dy %= float(self._period[1].in_units("code_length"))

        # convert to pixels
        px = dx / (bounds[1] - bounds[0])
        py = dy / (bounds[3] - bounds[2])

        # select only the particles that will actually show up in the image
        mask = np.logical_and(np.logical_and(px >= 0.0, px <= 1.0),
                              np.logical_and(py >= 0.0, py <= 1.0))

        weight_field = self.data_source.weight_field
        if weight_field is None:
            weight_data = np.ones_like(data.v)
        else:
            weight_data = self.data_source.dd[weight_field]
        splat_vals = weight_data[mask] * data[mask]

        # splat particles
        buff = np.zeros(self.buff_size)
        buff_mask = np.zeros(self.buff_size).astype("int")
        add_points_to_greyscale_image(buff, buff_mask, px[mask], py[mask],
                                      splat_vals)
        # remove values in no-particle region
        buff[buff_mask == 0] = np.nan
        ia = ImageArray(buff, units=data.units, info=self._get_info(item))

        # divide by the weight_field, if needed
        if weight_field is not None:
            weight_buff = np.zeros(self.buff_size)
            weight_buff_mask = np.zeros(self.buff_size).astype("int")
            add_points_to_greyscale_image(weight_buff, weight_buff_mask,
                                          px[mask], py[mask],
                                          weight_data[mask])
            weight_array = ImageArray(weight_buff,
                                      units=weight_data.units,
                                      info=self._get_info(item))
            # remove values in no-particle region
            weight_buff[weight_buff_mask == 0] = np.nan
            locs = np.where(weight_array > 0)
            ia[locs] /= weight_array[locs]

        self.data[item] = ia
        return self.data[item]
示例#3
0
    def __getitem__(self, item):
        if item in self.data:
            return self.data[item]

        deposition = self.data_source.deposition
        density = self.data_source.density

        mylog.info(
            "Splatting (%s) onto a %d by %d mesh using method '%s'",
            item,
            self.buff_size[0],
            self.buff_size[1],
            deposition,
        )

        bounds = []
        for b in self.bounds:
            if hasattr(b, "in_units"):
                b = float(b.in_units("code_length"))
            bounds.append(b)

        ftype = item[0]
        x_data = self.data_source.dd[ftype, self.x_field]
        y_data = self.data_source.dd[ftype, self.y_field]
        data = self.data_source.dd[item]

        # handle periodicity
        dx = x_data.in_units("code_length").d - bounds[0]
        dy = y_data.in_units("code_length").d - bounds[2]
        if self.periodic:
            dx %= float(self._period[0].in_units("code_length"))
            dy %= float(self._period[1].in_units("code_length"))

        # convert to pixels
        px = dx / (bounds[1] - bounds[0])
        py = dy / (bounds[3] - bounds[2])

        # select only the particles that will actually show up in the image
        mask = np.logical_and(np.logical_and(px >= 0.0, px <= 1.0),
                              np.logical_and(py >= 0.0, py <= 1.0))

        weight_field = self.data_source.weight_field
        if weight_field is None:
            weight_data = np.ones_like(data.v)
        else:
            weight_data = self.data_source.dd[weight_field]
        splat_vals = weight_data[mask] * data[mask]

        x_bin_edges = np.linspace(0.0, 1.0, self.buff_size[0] + 1)
        y_bin_edges = np.linspace(0.0, 1.0, self.buff_size[1] + 1)

        # splat particles
        buff = np.zeros(self.buff_size)
        buff_mask = np.zeros_like(buff, dtype="uint8")
        if deposition == "ngp":
            add_points_to_greyscale_image(buff, buff_mask, px[mask], py[mask],
                                          splat_vals)
        elif deposition == "cic":
            CICDeposit_2(
                py[mask],
                px[mask],
                splat_vals,
                mask.sum(),
                buff,
                buff_mask,
                x_bin_edges,
                y_bin_edges,
            )
        else:
            raise ValueError(
                f"Received unknown deposition method '{deposition}'")

        # remove values in no-particle region
        buff[buff_mask == 0] = np.nan

        # Normalize by the surface area of the pixel or volume of pencil if
        # requested
        info = self._get_info(item)
        if density:
            width = self.data_source.width
            norm = width[self.xax] * width[self.yax] / np.prod(self.buff_size)
            norm = norm.in_base()
            buff /= norm.v
            units = data.units / norm.units
            info["label"] = "%s $\\rm{Density}$" % info["label"]
        else:
            units = data.units

        ia = ImageArray(buff, units=units, info=info)

        # divide by the weight_field, if needed
        if weight_field is not None:
            weight_buff = np.zeros(self.buff_size)
            weight_buff_mask = np.zeros(self.buff_size, dtype="uint8")
            if deposition == "ngp":
                add_points_to_greyscale_image(weight_buff, weight_buff_mask,
                                              px[mask], py[mask],
                                              weight_data[mask])
            elif deposition == "cic":
                CICDeposit_2(
                    py[mask],
                    px[mask],
                    weight_data[mask],
                    mask.sum(),
                    weight_buff,
                    weight_buff_mask,
                    y_bin_edges,
                    x_bin_edges,
                )
            weight_array = ImageArray(weight_buff,
                                      units=weight_data.units,
                                      info=self._get_info(item))
            # remove values in no-particle region
            weight_buff[weight_buff_mask == 0] = np.nan
            locs = np.where(weight_array > 0)
            ia[locs] /= weight_array[locs]

        self.data[item] = ia
        return self.data[item]