[yt-svn] commit/yt: MatthewTurk: Merged in jzuhone/yt-3.x (pull request #1593)

commits-noreply at bitbucket.org commits-noreply at bitbucket.org
Mon Jun 1 12:26:51 PDT 2015


1 new commit in yt:

https://bitbucket.org/yt_analysis/yt/commits/ac0abcc06c42/
Changeset:   ac0abcc06c42
Branch:      yt
User:        MatthewTurk
Date:        2015-06-01 19:26:42+00:00
Summary:     Merged in jzuhone/yt-3.x (pull request #1593)

Export FixedResolutionBuffers as datasets
Affected #:  6 files

diff -r 239abe7c09fe6680691383eddb189965fe6522fe -r ac0abcc06c420713e8e4dba44178a0c5e3854267 doc/source/analyzing/generating_processed_data.rst
--- a/doc/source/analyzing/generating_processed_data.rst
+++ b/doc/source/analyzing/generating_processed_data.rst
@@ -47,10 +47,30 @@
    frb = FixedResolutionBuffer(sl, (0.3, 0.5, 0.6, 0.8), (512, 512))
    my_image = frb["density"]
 
-This resultant array can be saved out to disk or visualized using a
-hand-constructed Matplotlib image, for instance using
+This image may then be used in a hand-constructed Matplotlib image, for instance using
 :func:`~matplotlib.pyplot.imshow`.
 
+The buffer arrays can be saved out to disk in either HDF5 or FITS format:
+ 
+.. code-block:: python
+
+   frb.export_hdf5("my_images.h5", fields=["density","temperature"])
+   frb.export_fits("my_images.fits", fields=["density","temperature"],
+                   clobber=True, units="kpc")
+
+In the FITS case, there is an option for setting the ``units`` of the coordinate system in
+the file. If you want to overwrite a file with the same name, set ``clobber=True``. 
+
+The :class:`~yt.visualization.fixed_resolution.FixedResolutionBuffer` can even be exported
+as a 2D dataset itself, which may be operated on in the same way as any other dataset in yt:
+
+.. code-block:: python
+
+   ds_frb = frb.export_dataset(fields=["density","temperature"], nprocs=8)
+   sp = ds_frb.sphere("c", (100.,"kpc"))
+
+where the ``nprocs`` parameter can be used to decompose the image into ``nprocs`` number of grids.
+
 .. _generating-profiles-and-histograms:
 
 Profiles and Histograms

diff -r 239abe7c09fe6680691383eddb189965fe6522fe -r ac0abcc06c420713e8e4dba44178a0c5e3854267 yt/frontends/stream/data_structures.py
--- a/yt/frontends/stream/data_structures.py
+++ b/yt/frontends/stream/data_structures.py
@@ -709,7 +709,7 @@
             pdata = pdata_ftype
         # This will update the stream handler too
         assign_particle_data(sds, pdata)
-    
+
     return sds
 
 def load_amr_grids(grid_data, domain_dimensions,

diff -r 239abe7c09fe6680691383eddb189965fe6522fe -r ac0abcc06c420713e8e4dba44178a0c5e3854267 yt/visualization/fixed_resolution.py
--- a/yt/visualization/fixed_resolution.py
+++ b/yt/visualization/fixed_resolution.py
@@ -20,6 +20,7 @@
 from yt.utilities.lib.pixelization_routines import \
     pixelize_cylinder
 from yt.utilities.lib.api import add_points_to_greyscale_image
+from yt.frontends.stream.api import load_uniform_grid
 
 from . import _MPL
 import numpy as np
@@ -73,13 +74,13 @@
     To make a projection and then several images, you can generate a
     single FRB and then access multiple fields:
 
-    >>> proj = ds.proj(0, "Density")
+    >>> proj = ds.proj(0, "density")
     >>> frb1 = FixedResolutionBuffer(proj, (0.2, 0.3, 0.4, 0.5),
-                    (1024, 1024))
-    >>> print frb1["Density"].max()
-    1.0914e-9
-    >>> print frb1["Temperature"].max()
-    104923.1
+    ...                              (1024, 1024))
+    >>> print frb1["density"].max()
+    1.0914e-9 g/cm**3
+    >>> print frb1["temperature"].max()
+    104923.1 K
     """
     _exclude_fields = ('pz','pdz','dx','x','y','z',
         'r', 'dr', 'phi', 'dphi', 'theta', 'dtheta',
@@ -289,7 +290,7 @@
             These fields will be pixelized and output.
         """
         import h5py
-        if fields is None: fields = self.data.keys()
+        if fields is None: fields = list(self.data.keys())
         output = h5py.File(filename, "a")
         for field in fields:
             output.create_dataset(field,data=self[field])
@@ -307,30 +308,68 @@
         filename : string
             The name of the FITS file to be written.
         fields : list of strings
-            These fields will be pixelized and output.
+            These fields will be pixelized and output. If "None", the keys of the
+            FRB will be used. 
         clobber : boolean
             If the file exists, this governs whether we will overwrite.
         other_keys : dictionary, optional
             A set of header keys and values to write into the FITS header.
         units : string, optional
-            the length units that the coordinates are written in, default 'cm'
-            If units are set to "deg" then assume that sky coordinates are
-            requested.
+            the length units that the coordinates are written in, default 'cm'.
         """
 
         from yt.utilities.fits_image import FITSImageBuffer
 
-        extra_fields = ['x','y','z','px','py','pz','pdx','pdy','pdz','weight_field']
-        if fields is None: 
-            fields = [field[-1] for field in self.data_source.field_data
-                      if field not in extra_fields]
+        if fields is None: fields = list(self.data.keys())
 
         fib = FITSImageBuffer(self, fields=fields, units=units)
         if other_keys is not None:
             for k,v in other_keys.items():
                 fib.update_all_headers(k,v)
         fib.writeto(filename, clobber=clobber)
-        
+
+    def export_dataset(self, fields=None, nprocs=1):
+        r"""Export a set of pixelized fields to an in-memory dataset that can be
+        analyzed as any other in yt. Unit information and other parameters (e.g., 
+        geometry, current_time, etc.) will be taken from the parent dataset. 
+
+        Parameters
+        ----------
+        fields : list of strings, optional
+            These fields will be pixelized and output. If "None", the keys of the
+            FRB will be used. 
+        nprocs: integer, optional
+            If greater than 1, will create this number of subarrays out of data
+
+        Examples
+        --------
+        >>> import yt
+        >>> ds = yt.load("GasSloshing/sloshing_nomag2_hdf5_plt_cnt_0150")
+        >>> slc = ds.slice(2, 0.0)
+        >>> frb = slc.to_frb((500.,"kpc"), 500)
+        >>> ds2 = frb.export_dataset(fields=["density","temperature"], nprocs=32)
+        """
+        nx, ny = self.buff_size
+        data = {}
+        if fields is None:
+            fields = list(self.keys())
+        for field in fields:
+            arr = self[field]
+            data[field] = (arr.d.T.reshape(nx,ny,1), str(arr.units))
+        bounds = [b.in_units("code_length").v for b in self.bounds]
+        bbox = np.array([[bounds[0],bounds[1]],[bounds[2],bounds[3]],[0.,1.]])
+        return load_uniform_grid(data, [nx,ny,1],
+                                 length_unit=self.ds.length_unit,
+                                 bbox=bbox,
+                                 sim_time=self.ds.current_time.in_units("s").v,
+                                 mass_unit=self.ds.mass_unit,
+                                 time_unit=self.ds.time_unit,
+                                 velocity_unit=self.ds.velocity_unit,
+                                 magnetic_unit=self.ds.magnetic_unit,
+                                 periodicity=(False,False,False),
+                                 geometry=self.ds.geometry,
+                                 nprocs=nprocs)
+
     @property
     def limits(self):
         rv = dict(x = None, y = None, z = None)

diff -r 239abe7c09fe6680691383eddb189965fe6522fe -r ac0abcc06c420713e8e4dba44178a0c5e3854267 yt/visualization/tests/test_export_frb.py
--- /dev/null
+++ b/yt/visualization/tests/test_export_frb.py
@@ -0,0 +1,39 @@
+"""
+Tests for exporting an FRB as a dataset
+
+
+
+"""
+from __future__ import absolute_import
+
+#-----------------------------------------------------------------------------
+# Copyright (c) 2013, yt Development Team.
+#
+# Distributed under the terms of the Modified BSD License.
+#
+# The full license is in the file COPYING.txt, distributed with this software.
+#-----------------------------------------------------------------------------
+import numpy as np
+from yt.testing import \
+    fake_random_ds, assert_equal, \
+    assert_allclose
+
+def setup():
+    """Test specific setup."""
+    from yt.config import ytcfg
+    ytcfg["yt", "__withintesting"] = "True"
+
+
+def test_export_frb():
+    test_ds = fake_random_ds(128)
+    slc = test_ds.slice(0,0.5)
+    frb = slc.to_frb((0.5,"unitary"), 64)
+    frb_ds = frb.export_dataset(fields=["density"], nprocs=8)
+    dd_frb = frb_ds.all_data()
+
+    yield assert_equal, frb_ds.domain_left_edge.v, np.array([0.25,0.25,0.0])
+    yield assert_equal, frb_ds.domain_right_edge.v, np.array([0.75,0.75,1.0])
+    yield assert_equal, frb_ds.domain_width.v, np.array([0.5,0.5,1.0])
+    yield assert_equal, frb_ds.domain_dimensions, np.array([64,64,1], dtype="int64")
+    yield assert_allclose, frb["density"].sum(), dd_frb.quantities.total_quantity("density")
+    yield assert_equal, frb_ds.index.num_grids, 8

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