[yt-svn] commit/yt: 18 new changesets
Bitbucket
commits-noreply at bitbucket.org
Tue Feb 19 07:56:45 PST 2013
18 new commits in yt:
https://bitbucket.org/yt_analysis/yt/commits/c3411ea9e100/
changeset: c3411ea9e100
branch: yt
user: samskillman
date: 2013-02-17 22:41:05
summary: Adding rescaling and the ability to write_png to an arbitrary background color.
Also adding test of the rescaling, and image writing. I think the image saving
part needs to be moved to some other location, perhaps the docstrings for
write_png itself.
affected #: 2 files
diff -r 56c9e8e1d0920d28c93e6b03d39097079466c17c -r c3411ea9e10031ed35b6d4b8765ee84176a8b65a yt/data_objects/image_array.py
--- a/yt/data_objects/image_array.py
+++ b/yt/data_objects/image_array.py
@@ -133,13 +133,88 @@
d.attrs.create(k, v)
f.close()
- def write_png(self, filename, clip_ratio=None):
+ def rescale(self, cmax=None, amax=None, inline=True):
+ r"""Rescales the image to be in [0,1] range.
+
+ Parameters
+ ----------
+ cmax: float, optional
+ Normalization value to use for rgb channels. Defaults to None,
+ corresponding to using the maximum value in the rgb channels.
+ amax: float, optional
+ Normalization value to use for alpha channel. Defaults to None,
+ corresponding to using the maximum value in the alpha channel.
+ inline: boolean, optional
+ Specifies whether or not the rescaling is done inline. If false,
+ a new copy of the ImageArray will be created, returned.
+ Default:True.
+
+ Returns
+ -------
+ out: ImageArray
+ The rescaled ImageArray, clipped to the [0,1] range.
+
+ Notes
+ -----
+ This requires that the shape of the ImageArray to have a length of 3,
+ and for the third dimension to be >= 3. If the third dimension has
+ a shape of 4, the alpha channel will also be rescaled.
+
+ Examples
+ --------
+ >>> im = np.zeros([64,128,4])
+ >>> for i in xrange(im.shape[0]):
+ >>> for k in xrange(im.shape[2]):
+ >>> im[i,:,k] = np.linspace(0.,0.3*k, im.shape[1])
+
+ >>> im_arr.write_png('original.png')
+ >>> im_arr.rescale()
+ >>> im_arr.write_png('normalized.png')
+
+ """
+
+ assert(len(self.shape) == 3)
+ assert(self.shape[2] >= 3)
+ if inline:
+ out = self
+ else:
+ out = self.copy()
+ if cmax is None:
+ cmax = self[:,:,:3].sum(axis=2).max()
+ if amax is None:
+ amax = self[:,:,3].max()
+
+ np.multiply(self[:,:,:3], 1./cmax, out[:,:,:3])
+
+ if self.shape[2] == 4:
+ np.multiply(self[:,:,3], 1./amax, out[:,:,3])
+
+ np.clip(out, 0.0, 1.0, out)
+ return out
+
+ def write_png(self, filename, clip_ratio=None, background='black',
+ rescale=True):
r"""Writes ImageArray to png file.
Parameters
----------
filename: string
Note filename not be modified.
+ clip_ratio: float, optional
+ Image will be clipped before saving to the standard deviation
+ of the image multiplied by this value. Useful for enhancing
+ images. Default: None
+ background:
+ This can be used to set a background color for the image, and can
+ take several types of values:
+ 'white': white background, opaque
+ 'black': black background, opaque
+ None: transparent background
+ 4-element array [r,g,b,a]: arbitrary rgba setting.
+ Default: 'black'
+ rescale: boolean, optional
+ If True, will write out a rescaled image (without modifying the
+ original image). Default: True
Examples
--------
@@ -157,14 +232,36 @@
>>> im_arr.write_png('test_ImageArray.png')
"""
+ if background == None:
+ background = (0., 0., 0., 0.)
+ elif background == 'white':
+ background = (1., 1., 1., 1.)
+ elif background == 'black':
+ background = (0., 0., 0., 1.)
+
+ if rescale:
+ scaled = self.rescale(inline=False)
+ else:
+ scaled = self
+
+ # Alpha blending to background
+ if self.shape[2] == 4:
+ out = np.zeros_like(self)
+ out[:,:,3] = scaled[:,:,3] + background[3]*(1.0-scaled[:,:,3])
+ for i in range(3):
+ out[:,:,i] = scaled[:,:,i]*scaled[:,:,3] + \
+ background[i]*background[3]*(1.0-scaled[:,:,3])
+ else:
+ out = scaled
+
if filename[-4:] != '.png':
filename += '.png'
if clip_ratio is not None:
- return write_bitmap(self.swapaxes(0, 1), filename,
- clip_ratio * self.std())
+ return write_bitmap(out.swapaxes(0, 1), filename,
+ clip_ratio * out.std())
else:
- return write_bitmap(self.swapaxes(0, 1), filename)
+ return write_bitmap(out.swapaxes(0, 1), filename)
def write_image(self, filename, color_bounds=None, channel=None, cmap_name="algae", func=lambda x: x):
r"""Writes a single channel of the ImageArray to a png file.
diff -r 56c9e8e1d0920d28c93e6b03d39097079466c17c -r c3411ea9e10031ed35b6d4b8765ee84176a8b65a yt/data_objects/tests/test_image_array.py
--- /dev/null
+++ b/yt/data_objects/tests/test_image_array.py
@@ -0,0 +1,40 @@
+from yt.testing import *
+from yt.data_objects.image_array import ImageArray
+import numpy as np
+
+def setup():
+ from yt.config import ytcfg
+ ytcfg["yt","__withintesting"] = "True"
+ np.seterr(all = 'ignore')
+
+def test_rgba_rescale():
+ im = np.zeros([64,128,4])
+ for i in xrange(im.shape[0]):
+ for k in xrange(im.shape[2]):
+ im[i,:,k] = np.linspace(0.,10.*k, im.shape[1])
+ im_arr = ImageArray(im)
+
+ new_im = im_arr.rescale(inline=False)
+ yield assert_equal, im_arr[:,:,:3].max(), 2*10.
+ yield assert_equal, im_arr[:,:,3].max(), 3*10.
+ yield assert_equal, new_im[:,:,:3].sum(axis=2).max(), 1.0
+ yield assert_equal, new_im[:,:,3].max(), 1.0
+
+ im_arr.rescale()
+ yield assert_equal, im_arr[:,:,:3].sum(axis=2).max(), 1.0
+ yield assert_equal, im_arr[:,:,3].max(), 1.0
+
+def test_imarr_writepng():
+ im = np.zeros([64,128,4])
+ for i in xrange(im.shape[0]):
+ for k in xrange(im.shape[2]):
+ im[i,:,k] = np.linspace(0.,10.*k, im.shape[1])
+
+ im_arr = ImageArray(im)
+ 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)
+
https://bitbucket.org/yt_analysis/yt/commits/ac79cacd8ccf/
changeset: ac79cacd8ccf
branch: yt
user: samskillman
date: 2013-02-17 22:43:48
summary: Moving rescaling test into docstrings for write_png.
affected #: 2 files
diff -r c3411ea9e10031ed35b6d4b8765ee84176a8b65a -r ac79cacd8ccfa19defbd183a8f82fcfe14fd7aae yt/data_objects/image_array.py
--- a/yt/data_objects/image_array.py
+++ b/yt/data_objects/image_array.py
@@ -218,18 +218,18 @@
Examples
--------
-
- >>> im = np.zeros([64,128,3])
+ >>> im = np.zeros([64,128,4])
>>> for i in xrange(im.shape[0]):
>>> for k in xrange(im.shape[2]):
- >>> im[i,:,k] = np.linspace(0.,0.3*k, im.shape[1])
+ >>> im[i,:,k] = np.linspace(0.,10.*k, im.shape[1])
- >>> myinfo = {'field':'dinosaurs', 'east_vector':np.array([1.,0.,0.]),
- >>> 'north_vector':np.array([0.,0.,1.]), 'normal_vector':np.array([0.,1.,0.]),
- >>> 'width':0.245, 'units':'cm', 'type':'rendering'}
-
- >>> im_arr = ImageArray(im, info=myinfo)
- >>> im_arr.write_png('test_ImageArray.png')
+ >>> im_arr = ImageArray(im)
+ >>> 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)
"""
if background == None:
diff -r c3411ea9e10031ed35b6d4b8765ee84176a8b65a -r ac79cacd8ccfa19defbd183a8f82fcfe14fd7aae yt/data_objects/tests/test_image_array.py
--- a/yt/data_objects/tests/test_image_array.py
+++ b/yt/data_objects/tests/test_image_array.py
@@ -24,17 +24,3 @@
yield assert_equal, im_arr[:,:,:3].sum(axis=2).max(), 1.0
yield assert_equal, im_arr[:,:,3].max(), 1.0
-def test_imarr_writepng():
- im = np.zeros([64,128,4])
- for i in xrange(im.shape[0]):
- for k in xrange(im.shape[2]):
- im[i,:,k] = np.linspace(0.,10.*k, im.shape[1])
-
- im_arr = ImageArray(im)
- 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)
-
https://bitbucket.org/yt_analysis/yt/commits/4e7b13877d5f/
changeset: 4e7b13877d5f
branch: yt
user: samskillman
date: 2013-02-17 22:44:20
summary: Removing pdb from amr_kdtree.
affected #: 1 file
diff -r ac79cacd8ccfa19defbd183a8f82fcfe14fd7aae -r 4e7b13877d5f73b09e906131a2edeb48c05628dc yt/utilities/amr_kdtree/amr_kdtree.py
--- a/yt/utilities/amr_kdtree/amr_kdtree.py
+++ b/yt/utilities/amr_kdtree/amr_kdtree.py
@@ -35,12 +35,6 @@
from yt.utilities.lib.grid_traversal import PartitionedGrid
from yt.utilities.math_utils import periodic_position
-import pdb
-
-def my_break():
- my_debug = False
- if my_debug: pdb.set_trace()
-
steps = np.array([[-1, -1, -1], [-1, -1, 0], [-1, -1, 1],
[-1, 0, -1], [-1, 0, 0], [-1, 0, 1],
[-1, 1, -1], [-1, 1, 0], [-1, 1, 1],
https://bitbucket.org/yt_analysis/yt/commits/9023fb0001c1/
changeset: 9023fb0001c1
branch: yt
user: samskillman
date: 2013-02-17 22:46:10
summary: Cleaning up camera.py image saving internal api. Adding transparent=False call
to snapshot. Making lines() drawing respect alpha channels. replacing
enhance() with a call to ImageArray.rescale().
affected #: 3 files
diff -r 4e7b13877d5f73b09e906131a2edeb48c05628dc -r 9023fb0001c16cccf00399389adc2da35e2b2d14 yt/utilities/lib/misc_utilities.pyx
--- a/yt/utilities/lib/misc_utilities.pyx
+++ b/yt/utilities/lib/misc_utilities.pyx
@@ -155,6 +155,8 @@
if (x0 >=0 and x0 < nx and y0 >= 0 and y0 < ny):
for i in range(3):
image[x0,y0,i] = (1.-alpha[i])*image[x0,y0,i] + alpha[i]
+ if image.shape[2] == 4:
+ image[x0,y0,3] = (1.-alpha[3])*image[x0,y0,3] + alpha[3]
if (x0 == x1 and y0 == y1):
break
e2 = 2*err
diff -r 4e7b13877d5f73b09e906131a2edeb48c05628dc -r 9023fb0001c16cccf00399389adc2da35e2b2d14 yt/visualization/volume_rendering/blenders.py
--- a/yt/visualization/volume_rendering/blenders.py
+++ b/yt/visualization/volume_rendering/blenders.py
@@ -11,3 +11,17 @@
del nz
np.clip(im, 0.0, 1.0, im)
+def enhance_rgba(im, stdval=6.0):
+ nzc = im[:,:,:3][im[:,:,:3]>0.0]
+ cmax = nzc.mean()+stdval*np.std(nzc)
+
+ nza = im[:,:,3][im[:,:,3]>0.0]
+ if len(nza) == 0:
+ im[:,:,3]=1.0
+ amax = 1.0
+ else:
+ amax = nza.mean()+stdval*np.std(nza)
+
+ im.rescale(amax=amax, cmax=cmax, inline=True)
+ np.clip(im, 0.0, 1.0, im)
+
diff -r 4e7b13877d5f73b09e906131a2edeb48c05628dc -r 9023fb0001c16cccf00399389adc2da35e2b2d14 yt/visualization/volume_rendering/camera.py
--- a/yt/visualization/volume_rendering/camera.py
+++ b/yt/visualization/volume_rendering/camera.py
@@ -49,7 +49,7 @@
from yt.utilities.parallel_tools.parallel_analysis_interface import \
ParallelAnalysisInterface, ProcessorPool, parallel_objects
from yt.utilities.amr_kdtree.api import AMRKDTree
-from .blenders import enhance
+from .blenders import enhance_rgba
from numpy import pi
def get_corners(le, re):
@@ -317,7 +317,7 @@
# Must normalize the image
ma = im.max()
if ma > 0.0:
- enhance(im)
+ enhance_rgba(im)
lines(im, px, py, colors, 24)
@@ -391,7 +391,7 @@
ma = im.max()
if ma > 0.0:
- enhance(im)
+ enhance_rgba(im)
self.draw_box(im, self.pf.domain_left_edge, self.pf.domain_right_edge,
color=np.array([1.0,1.0,1.0,alpha]))
@@ -529,6 +529,8 @@
def finalize_image(self, image):
view_pos = self.front_center + self.orienter.unit_vectors[2] * 1.0e6 * self.width[2]
image = self.volume.reduce_tree_images(image, view_pos)
+ if self.transfer_function.grey_opacity is False:
+ image[:,:,3]=1.0
return image
def _render(self, double_check, num_threads, image, sampler):
@@ -598,12 +600,13 @@
self.annotate(ax.axes, enhance)
self._pylab.savefig(fn, bbox_inches='tight', facecolor='black', dpi=dpi)
- def save_image(self, fn, clip_ratio, image, transparent=False):
- if self.comm.rank is 0 and fn is not None:
+ def save_image(self, image, fn=None, clip_ratio=None, transparent=False):
+ if self.comm.rank == 0 and fn is not None:
if transparent:
- image.write_png(fn, clip_ratio=clip_ratio)
+ image.write_png(fn, clip_ratio=clip_ratio, rescale=False)
else:
- image[:,:,:3].write_png(fn, clip_ratio=clip_ratio)
+ image.write_png(fn, clip_ratio=clip_ratio, rescale=False,
+ background='black')
def initialize_source(self):
return self.volume.initialize_source()
@@ -619,7 +622,7 @@
return info_dict
def snapshot(self, fn = None, clip_ratio = None, double_check = False,
- num_threads = 0):
+ num_threads = 0, transparent=False):
r"""Ray-cast the camera.
This method instructs the camera to take a snapshot -- i.e., call the ray
@@ -640,6 +643,9 @@
If supplied, will use 'num_threads' number of OpenMP threads during
the rendering. Defaults to 0, which uses the environment variable
OMP_NUM_THREADS.
+ transparent: bool, optional
+ Optionally saves out the 4-channel rgba image, which can appear
+ empty if the alpha channel is low everywhere. Default: False
Returns
-------
@@ -655,7 +661,7 @@
image = ImageArray(self._render(double_check, num_threads,
image, sampler),
info=self.get_information())
- self.save_image(fn, clip_ratio, image)
+ self.save_image(image, fn=fn, clip_ratio=clip_ratio)
return image
def show(self, clip_ratio = None):
@@ -1191,11 +1197,11 @@
image = ImageArray(self._render(double_check, num_threads,
image, sampler),
info=self.get_information())
- self.save_image(fn, clim, image, label = label)
+ self.save_image(image, fn=fn, clim=clim, label = label)
return image
- def save_image(self, fn, clim, image, label = None):
- if self.comm.rank is 0 and fn is not None:
+ def save_image(self, image, fn=None, clim=None, label = None):
+ if self.comm.rank == 0 and fn is not None:
# This assumes Density; this is a relatively safe assumption.
import matplotlib.figure
import matplotlib.backends.backend_agg
@@ -1509,7 +1515,7 @@
sto.id = self.imj*self.nimx + self.imi
sto.result = image
image = self.reduce_images(my_storage)
- self.save_image(fn, clip_ratio, image)
+ self.save_image(image, fn=fn, clip_ratio=clip_ratio)
return image
def reduce_images(self,im_dict):
@@ -2165,12 +2171,12 @@
image = self.finalize_image(sampler.aimage)
return image
- def save_image(self, fn, clip_ratio, image):
+ def save_image(self, image, fn=None, clip_ratio=None):
if self.pf.field_info[self.field].take_log:
im = np.log10(image)
else:
im = image
- if self.comm.rank is 0 and fn is not None:
+ if self.comm.rank == 0 and fn is not None:
if clip_ratio is not None:
write_image(im, fn)
else:
@@ -2197,7 +2203,7 @@
image, sampler),
info=self.get_information())
- self.save_image(fn, clip_ratio, image)
+ self.save_image(image, fn=fn, clip_ratio=clip_ratio)
return image
snapshot.__doc__ = Camera.snapshot.__doc__
https://bitbucket.org/yt_analysis/yt/commits/6a12037c5194/
changeset: 6a12037c5194
branch: yt
user: samskillman
date: 2013-02-17 23:54:52
summary: Hit two missing my_break calls.
affected #: 1 file
Diff not available.
https://bitbucket.org/yt_analysis/yt/commits/6c695977df16/
changeset: 6c695977df16
branch: yt
user: samskillman
date: 2013-02-18 02:27:47
summary: cam.save_image should rescale by default. Otherwise images are very transparent.
affected #: 1 file
Diff not available.
https://bitbucket.org/yt_analysis/yt/commits/b40286284ba4/
changeset: b40286284ba4
branch: yt
user: samskillman
date: 2013-02-18 04:29:25
summary: Fixing up clip_ratio usage in the image_array. Somewhat sneaky, but produces
good results based on the opaque rendering cookbook.
affected #: 1 file
Diff not available.
https://bitbucket.org/yt_analysis/yt/commits/1d0763085546/
changeset: 1d0763085546
branch: yt
user: samskillman
date: 2013-02-18 04:30:25
summary: Setting up the image_writer to only clip the rgb channels. The alpha channel
should either be left alone if it was given to it, or it should be set to
zeros.
affected #: 1 file
Diff not available.
https://bitbucket.org/yt_analysis/yt/commits/ac10654fa997/
changeset: ac10654fa997
branch: yt
user: samskillman
date: 2013-02-18 04:31:48
summary: Fixing up how the alpha channel is handled for grey_opacity=False renders. It
should be set to the sum of rgb, to later be enhanced or clipped.
affected #: 2 files
Diff not available.
https://bitbucket.org/yt_analysis/yt/commits/66c8c11fedac/
changeset: 66c8c11fedac
branch: yt
user: samskillman
date: 2013-02-18 18:02:24
summary: Pulling out the background image adding to its own member function.
affected #: 1 file
Diff not available.
https://bitbucket.org/yt_analysis/yt/commits/bfe01ea7e3d0/
changeset: bfe01ea7e3d0
branch: yt
user: samskillman
date: 2013-02-18 18:03:44
summary: Fixing the lines function to work correctly with 4 channel images. Passing the
correct argument for transparent image saving in camera.
affected #: 2 files
Diff not available.
https://bitbucket.org/yt_analysis/yt/commits/505f3337e398/
changeset: 505f3337e398
branch: yt
user: samskillman
date: 2013-02-18 18:04:06
summary: Bugfix and docstring update to image_writer.
affected #: 1 file
Diff not available.
https://bitbucket.org/yt_analysis/yt/commits/cb807e4295aa/
changeset: cb807e4295aa
branch: yt
user: samskillman
date: 2013-02-18 18:05:36
summary: Using a better enhance function for the rgb channels. For the drawing of lines
and boxes, we now create a new image and modify that. That way the image isn't
permanently damaged by drawing the domain/grids/lines. Requires updating of
the example scripts.
affected #: 2 files
Diff not available.
https://bitbucket.org/yt_analysis/yt/commits/b4056ecf7c13/
changeset: b4056ecf7c13
branch: yt
user: samskillman
date: 2013-02-18 18:28:01
summary: Whoops, needed inverse here.
affected #: 1 file
Diff not available.
https://bitbucket.org/yt_analysis/yt/commits/7d4f9a3dce41/
changeset: 7d4f9a3dce41
branch: yt
user: samskillman
date: 2013-02-18 18:38:58
summary: This is the simplest way at the moment to preserve the usage of write_bitmap()
for volume renderings, even though I really don't like it. This should be
addressed in the future with a discussion of grey_opacity and a scene object.
affected #: 1 file
Diff not available.
https://bitbucket.org/yt_analysis/yt/commits/2669c41d4f19/
changeset: 2669c41d4f19
branch: yt
user: samskillman
date: 2013-02-18 19:16:57
summary: Removing __main__ call, putting functionality into data_objects/tests
affected #: 2 files
Diff not available.
https://bitbucket.org/yt_analysis/yt/commits/0d2a983d8924/
changeset: 0d2a983d8924
branch: yt
user: samskillman
date: 2013-02-19 16:52:56
summary: Fixing up docstrings to obey magic Sphinx power, adding display_in_notebook
as a helper function for displaying images inline in an IPython notebook.
affected #: 2 files
Diff not available.
https://bitbucket.org/yt_analysis/yt/commits/7f49529e1bb6/
changeset: 7f49529e1bb6
branch: yt
user: MatthewTurk
date: 2013-02-19 16:56:37
summary: Merged in samskillman/yt (pull request #435)
Upgrades to ImageArray, Camera image saving, handling arbitrary background colors.
affected #: 7 files
Diff not available.
Repository URL: https://bitbucket.org/yt_analysis/yt/
--
This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.
More information about the yt-svn
mailing list