def new_image(self, camera): self.current_image = ImageArray( np.zeros((camera.resolution[0], camera.resolution[1], 4), dtype='float64', order='C'), info={'imtype': 'rendering'}) return self.current_image
transfer_function=transfer_function.tf, ) print "ROTATION SET 3",mol images3 = ytcubes[mol].quick_render_movie('ChemMovie/', size=size, nframes=nframes, start_index=nframes*2, camera_angle=(0,0,1), rot_vector=(0.5,0.5,0), image_prefix=mol, transfer_function=transfer_function.tf, ) all_images[mol] = images1+images2+images3 molecules = ('HNC','HCOp','HCN','HC3N') from matplotlib import image from yt.data_objects.image_array import ImageArray combined = [np.sum([image.imread("ChemMovie/{mol}{ii:04d}.png".format(mol=mol, ii=ii))[:,:,:3] for mol in molecules], axis=0) for ii in range(nframes*3)] cmax = max(np.percentile(img[:, :, :3].sum(axis=2), 99.5) for img in combined) for ii, img in enumerate(combined): img = ImageArray(img) img = img.rescale(cmax=cmax).swapaxes(0,1) img.write_png("ChemMovie/combined_%04i.png" % (ii), rescale=False) import spectral_cube.ytcube spectral_cube.ytcube._make_movie('ChemMovie', prefix='combined_', filename='combined_%i.mp4' % size)
def test_write_image(self): im_arr = ImageArray(dummy_image(10.0, 4)) im_arr.write_image("with_cmap", cmap_name="hot") im_arr.write_image("channel_1.png", channel=1)
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]
def test_image_array_rgba_png(self): im_arr = ImageArray(dummy_image(10.0, 4)) im_arr.write_png("standard") im_arr.write_png("non-scaled.png", rescale=False) im_arr.write_png("black_bg.png", background="black") im_arr.write_png("white_bg.png", background="white") im_arr.write_png("green_bg.png", background=[0.0, 1.0, 0.0, 1.0]) im_arr.write_png("transparent_bg.png", background=None) with warnings.catch_warnings(record=True) as w: # Cause all warnings to always be triggered. warnings.simplefilter("always") # Trigger a warning. im_arr.write_png("clipped.png", clip_ratio=0.5) assert str(w[0].message) == ( "'clip_ratio' keyword is deprecated. Use 'sigma_clip' instead" )
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, 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) 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, input_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]
def test_write_image(self): im_arr = ImageArray(dummy_image(10.0, 4)) im_arr.write_image('with_cmap', cmap_name='hot') im_arr.write_image('channel_1.png', channel=1)
def test_image_array_rgba_png(self): im_arr = ImageArray(dummy_image(10.0, 4)) im_arr.write_png('standard.png') im_arr.write_png('non-scaled.png', rescale=False) im_arr.write_png('black_bg.png', background='black') im_arr.write_png('white_bg.png', background='white') im_arr.write_png('green_bg.png', background=[0., 1., 0., 1.]) im_arr.write_png('transparent_bg.png', background=None)
def test_image_array_rgb_png(self): im_arr = ImageArray(dummy_image(10.0, 3)) im_arr.write_png('standard.png')