[yt-svn] commit/yt: ngoldbaum: Merged in chummels/yt (pull request #2479)

commits-noreply at bitbucket.org commits-noreply at bitbucket.org
Tue Dec 20 10:47:40 PST 2016


1 new commit in yt:

https://bitbucket.org/yt_analysis/yt/commits/9f15c5506c63/
Changeset:   9f15c5506c63
Branch:      yt
User:        ngoldbaum
Date:        2016-12-20 18:47:14+00:00
Summary:     Merged in chummels/yt (pull request #2479)

Making boolean data objects function for 0D, 1D, 2D, and 3D Selection Containers (Closes #1303)
Affected #:  3 files

diff -r 9337d37e3dabed61eea5bb7321917a695bedc072 -r 9f15c5506c6304da38dd7338b4870f1e4113ccbc doc/source/analyzing/objects.rst
--- a/doc/source/analyzing/objects.rst
+++ b/doc/source/analyzing/objects.rst
@@ -630,7 +630,7 @@
 ---------------------------------------
 
 A special type of data object is the *boolean* data object, which works with
-three-dimensional data selection.  It is built by relating already existing
+data selection objects of any dimension.  It is built by relating already existing
 data objects with the bitwise operators for AND, OR and XOR, as well as the
 subtraction operator.  These are created by using the operators ``&`` for an
 intersection ("AND"), ``|`` for a union ("OR"), ``^`` for an exclusive or

diff -r 9337d37e3dabed61eea5bb7321917a695bedc072 -r 9f15c5506c6304da38dd7338b4870f1e4113ccbc yt/data_objects/data_containers.py
--- a/yt/data_objects/data_containers.py
+++ b/yt/data_objects/data_containers.py
@@ -1390,6 +1390,43 @@
                         if f not in fields_to_generate:
                             fields_to_generate.append(f)
 
+    def __or__(self, other):
+        if not isinstance(other, YTSelectionContainer):
+            raise YTBooleanObjectError(other)
+        if self.ds is not other.ds:
+            raise YTBooleanObjectsWrongDataset()
+        # Should maybe do something with field parameters here
+        return YTBooleanContainer("OR", self, other, ds = self.ds)
+
+    def __invert__(self):
+        # ~obj
+        asel = yt.geometry.selection_routines.AlwaysSelector(self.ds)
+        return YTBooleanContainer("NOT", self, asel, ds = self.ds)
+
+    def __xor__(self, other):
+        if not isinstance(other, YTSelectionContainer):
+            raise YTBooleanObjectError(other)
+        if self.ds is not other.ds:
+            raise YTBooleanObjectsWrongDataset()
+        return YTBooleanContainer("XOR", self, other, ds = self.ds)
+
+    def __and__(self, other):
+        if not isinstance(other, YTSelectionContainer):
+            raise YTBooleanObjectError(other)
+        if self.ds is not other.ds:
+            raise YTBooleanObjectsWrongDataset()
+        return YTBooleanContainer("AND", self, other, ds = self.ds)
+
+    def __add__(self, other):
+        return self.__or__(other)
+
+    def __sub__(self, other):
+        if not isinstance(other, YTSelectionContainer):
+            raise YTBooleanObjectError(other)
+        if self.ds is not other.ds:
+            raise YTBooleanObjectsWrongDataset()
+        return YTBooleanContainer("NEG", self, other, ds = self.ds)
+
     @contextmanager
     def _field_lock(self):
         self._locked = True
@@ -1913,42 +1950,6 @@
         """
         return self.quantities.total_quantity(("index", "cell_volume"))
 
-    def __or__(self, other):
-        if not isinstance(other, YTSelectionContainer3D):
-            raise YTBooleanObjectError(other)
-        if self.ds is not other.ds:
-            raise YTBooleanObjectsWrongDataset()
-        # Should maybe do something with field parameters here
-        return YTBooleanContainer("OR", self, other, ds = self.ds)
-
-    def __invert__(self):
-        # ~obj
-        asel = yt.geometry.selection_routines.AlwaysSelector(self.ds)
-        return YTBooleanContainer("NOT", self, asel, ds = self.ds)
-
-    def __xor__(self, other):
-        if not isinstance(other, YTSelectionContainer3D):
-            raise YTBooleanObjectError(other)
-        if self.ds is not other.ds:
-            raise YTBooleanObjectsWrongDataset()
-        return YTBooleanContainer("XOR", self, other, ds = self.ds)
-
-    def __and__(self, other):
-        if not isinstance(other, YTSelectionContainer3D):
-            raise YTBooleanObjectError(other)
-        if self.ds is not other.ds:
-            raise YTBooleanObjectsWrongDataset()
-        return YTBooleanContainer("AND", self, other, ds = self.ds)
-
-    def __add__(self, other):
-        return self.__or__(other)
-
-    def __sub__(self, other):
-        if not isinstance(other, YTSelectionContainer3D):
-            raise YTBooleanObjectError(other)
-        if self.ds is not other.ds:
-            raise YTBooleanObjectsWrongDataset()
-        return YTBooleanContainer("NEG", self, other, ds = self.ds)
 
 class YTBooleanContainer(YTSelectionContainer3D):
     """
@@ -1964,9 +1965,9 @@
     ----------
     op : string
         Can be AND, OR, XOR, NOT or NEG.
-    dobj1 : YTSelectionContainer3D
+    dobj1 : YTSelectionContainer
         The first selection object
-    dobj2 : YTSelectionContainer3D
+    dobj2 : YTSelectionContainer
         The second object
 
     Examples

diff -r 9337d37e3dabed61eea5bb7321917a695bedc072 -r 9f15c5506c6304da38dd7338b4870f1e4113ccbc yt/data_objects/tests/test_boolean_regions.py
--- a/yt/data_objects/tests/test_boolean_regions.py
+++ b/yt/data_objects/tests/test_boolean_regions.py
@@ -427,3 +427,380 @@
     b6.sort()
     assert_array_equal(b6, np.setxor1d(np.setxor1d(rei, spi), cyli))
 
+def test_boolean_ray_region_no_overlap():
+    r"""Test to make sure that boolean objects (ray, region, no overlap)
+    behave the way we expect.
+
+    Test non-overlapping ray and region. This also checks that the original 
+    objects don't change as part of constructing the booleans.
+    """
+    ds = fake_amr_ds()
+    re = ds.box([0.25]*3, [0.75]*3)
+    ra = ds.ray([0.1]*3, [0.1, 0.1, 0.9])
+    # Store the original indices
+    i1 = re["index","morton_index"]
+    i1.sort()
+    i2 = ra["index","morton_index"]
+    i2.sort()
+    ii = np.concatenate((i1, i2))
+    ii.sort()
+    # Make some booleans
+    bo1 = re & ra
+    bo2 = re - ra
+    bo3 = re | ra
+    bo4 = ds.union([re, ra])
+    bo5 = ds.intersection([re, ra])
+    # This makes sure the original containers didn't change.
+    new_i1 = re["index","morton_index"]
+    new_i1.sort()
+    new_i2 = ra["index","morton_index"]
+    new_i2.sort()
+    assert_array_equal(new_i1, i1)
+    assert_array_equal(new_i2, i2)
+    # Now make sure the indices also behave as we expect.
+    empty = np.array([])
+    assert_array_equal(bo1["index","morton_index"], empty)
+    assert_array_equal(bo5["index","morton_index"], empty)
+    b2 = bo2["index","morton_index"]
+    b2.sort()
+    assert_array_equal(b2, i1 )
+    b3 = bo3["index","morton_index"]
+    b3.sort()
+    assert_array_equal(b3, ii)
+    b4 = bo4["index","morton_index"]
+    b4.sort()
+    b5 = bo5["index","morton_index"]
+    b5.sort()
+    assert_array_equal(b3, b4)
+    bo6 = re ^ ra
+    b6 = bo6["index", "morton_index"]
+    b6.sort()
+    assert_array_equal(b6, np.setxor1d(i1, i2))
+
+def test_boolean_ray_region_overlap():
+    r"""Test to make sure that boolean objects (ray, region, overlap)
+    behave the way we expect.
+
+    Test overlapping ray and region. This also checks that the original 
+    objects don't change as part of constructing the booleans.
+    """
+    ds = fake_amr_ds()
+    re = ds.box([0.25]*3, [0.75]*3)
+    ra = ds.ray([0]*3, [1]*3)
+    # Get indices of both.
+    i1 = re["index","morton_index"]
+    i2 = ra["index","morton_index"]
+    # Make some booleans
+    bo1 = re & ra
+    bo2 = re - ra
+    bo3 = re | ra
+    bo4 = ds.union([re, ra])
+    bo5 = ds.intersection([re, ra])
+    # Now make sure the indices also behave as we expect.
+    short_line = np.intersect1d(i1, i2)
+    cube_minus_line = np.setdiff1d(i1, i2)
+    both = np.union1d(i1, i2)
+    b1 = bo1["index","morton_index"]
+    b1.sort()
+    b2 = bo2["index","morton_index"]
+    b2.sort()
+    b3 = bo3["index","morton_index"]
+    b3.sort()
+    assert_array_equal(b1, short_line)
+    assert_array_equal(b2, cube_minus_line)
+    assert_array_equal(b3, both)
+    b4 = bo4["index","morton_index"]
+    b4.sort()
+    b5 = bo5["index","morton_index"]
+    b5.sort()
+    assert_array_equal(b3, b4)
+    assert_array_equal(b1, b5)
+    bo6 = re ^ ra
+    b6 = bo6["index", "morton_index"]
+    b6.sort()
+    assert_array_equal(b6, np.setxor1d(i1, i2))
+
+def test_boolean_rays_no_overlap():
+    r"""Test to make sure that boolean objects (rays, no overlap)
+    behave the way we expect.
+
+    Test non-overlapping rays.
+    """
+    ds = fake_amr_ds()
+    ra1 = ds.ray([0, 0, 0], [0, 0, 1])
+    ra2 = ds.ray([1, 0, 0], [1, 0, 1])
+    # Store the original indices
+    i1 = ra1["index","morton_index"]
+    i1.sort()
+    i2 = ra2["index","morton_index"]
+    i2.sort()
+    ii = np.concatenate((i1, i2))
+    ii.sort()
+    # Make some booleans
+    bo1 = ra1 & ra2
+    bo2 = ra1 - ra2
+    bo3 = ra1 | ra2
+    bo4 = ds.union([ra1, ra2])
+    bo5 = ds.intersection([ra1, ra2])
+    # This makes sure the original containers didn't change.
+    new_i1 = ra1["index","morton_index"]
+    new_i1.sort()
+    new_i2 = ra2["index","morton_index"]
+    new_i2.sort()
+    assert_array_equal(new_i1, i1)
+    assert_array_equal(new_i2, i2)
+    # Now make sure the indices also behave as we expect.
+    empty = np.array([])
+    assert_array_equal(bo1["index","morton_index"], empty)
+    assert_array_equal(bo5["index","morton_index"], empty)
+    b2 = bo2["index","morton_index"]
+    b2.sort()
+    assert_array_equal(b2, i1 )
+    b3 = bo3["index","morton_index"]
+    b3.sort()
+    assert_array_equal(b3, ii)
+    b4 = bo4["index","morton_index"]
+    b4.sort()
+    b5 = bo5["index","morton_index"]
+    b5.sort()
+    assert_array_equal(b3, b4)
+    bo6 = ra1 ^ ra2
+    b6 = bo6["index", "morton_index"]
+    b6.sort()
+    assert_array_equal(b6, np.setxor1d(i1, i2))
+
+def test_boolean_rays_overlap():
+    r"""Test to make sure that boolean objects (rays, overlap)
+    behave the way we expect.
+
+    Test non-overlapping rays.
+    """
+    ds = fake_amr_ds()
+    ra1 = ds.ray([0]*3, [1]*3)
+    ra2 = ds.ray([0]*3, [0.5]*3)
+    # Get indices of both.
+    i1 = ra1["index","morton_index"]
+    i1.sort()
+    i2 = ra2["index","morton_index"]
+    i2.sort()
+    ii = np.concatenate((i1, i2))
+    ii.sort()
+    # Make some booleans
+    bo1 = ra1 & ra2
+    bo2 = ra1 - ra2
+    bo3 = ra1 | ra2
+    bo4 = ds.union([ra1, ra2])
+    bo5 = ds.intersection([ra1, ra2])
+    # Now make sure the indices also behave as we expect.
+    short_line = np.intersect1d(i1, i2)
+    short_line_b = np.setdiff1d(i1, i2)
+    full_line = np.union1d(i1, i2)
+    b1 = bo1["index","morton_index"]
+    b1.sort()
+    b2 = bo2["index","morton_index"]
+    b2.sort()
+    b3 = bo3["index","morton_index"]
+    b3.sort()
+    assert_array_equal(b1, short_line)
+    assert_array_equal(b2, short_line_b)
+    assert_array_equal(b3, full_line)
+    b4 = bo4["index","morton_index"]
+    b4.sort()
+    b5 = bo5["index","morton_index"]
+    b5.sort()
+    assert_array_equal(b3, i1)
+    assert_array_equal(b3, b4)
+    assert_array_equal(b1, b5)
+    bo6 = ra1 ^ ra2
+    b6 = bo6["index", "morton_index"]
+    b6.sort()
+    assert_array_equal(b6, np.setxor1d(i1, i2))
+
+def test_boolean_slices_no_overlap():
+    r"""Test to make sure that boolean objects (slices, no overlap)
+    behave the way we expect.
+
+    Test non-overlapping slices. This also checks that the original regions
+    don't change as part of constructing the booleans.
+    """
+    ds = fake_amr_ds()
+    sl1 = ds.r[:,:,0.25]
+    sl2 = ds.r[:,:,0.75]
+    # Store the original indices
+    i1 = sl1["index","morton_index"]
+    i1.sort()
+    i2 = sl2["index","morton_index"]
+    i2.sort()
+    ii = np.concatenate((i1, i2))
+    ii.sort()
+    # Make some booleans
+    bo1 = sl1 & sl2
+    bo2 = sl1 - sl2
+    bo3 = sl1 | sl2
+    bo4 = ds.union([sl1, sl2])
+    bo5 = ds.intersection([sl1, sl2])
+    # This makes sure the original containers didn't change.
+    new_i1 = sl1["index","morton_index"]
+    new_i1.sort()
+    new_i2 = sl2["index","morton_index"]
+    new_i2.sort()
+    assert_array_equal(new_i1, i1)
+    assert_array_equal(new_i2, i2)
+    # Now make sure the indices also behave as we expect.
+    empty = np.array([])
+    assert_array_equal(bo1["index","morton_index"], empty)
+    assert_array_equal(bo5["index","morton_index"], empty)
+    b2 = bo2["index","morton_index"]
+    b2.sort()
+    assert_array_equal(b2, i1 )
+    b3 = bo3["index","morton_index"]
+    b3.sort()
+    assert_array_equal(b3, ii)
+    b4 = bo4["index","morton_index"]
+    b4.sort()
+    b5 = bo5["index","morton_index"]
+    b5.sort()
+    assert_array_equal(b3, b4)
+    bo6 = sl1 ^ sl2
+    b6 = bo6["index", "morton_index"]
+    b6.sort()
+    assert_array_equal(b6, np.setxor1d(i1, i2))
+
+def test_boolean_slices_overlap():
+    r"""Test to make sure that boolean objects (slices, overlap)
+    behave the way we expect.
+
+    Test overlapping slices.
+    """
+    ds = fake_amr_ds()
+    sl1 = ds.r[:,:,0.25]
+    sl2 = ds.r[:,0.75,:]
+    # Get indices of both.
+    i1 = sl1["index","morton_index"]
+    i2 = sl2["index","morton_index"]
+    # Make some booleans
+    bo1 = sl1 & sl2
+    bo2 = sl1 - sl2
+    bo3 = sl1 | sl2
+    bo4 = ds.union([sl1, sl2])
+    bo5 = ds.intersection([sl1, sl2])
+    # Now make sure the indices also behave as we expect.
+    line = np.intersect1d(i1, i2)
+    orig = np.setdiff1d(i1, i2)
+    both = np.union1d(i1, i2)
+    b1 = bo1["index","morton_index"]
+    b1.sort()
+    b2 = bo2["index","morton_index"]
+    b2.sort()
+    b3 = bo3["index","morton_index"]
+    b3.sort()
+    assert_array_equal(b1, line)
+    assert_array_equal(b2, orig)
+    assert_array_equal(b3, both)
+    b4 = bo4["index","morton_index"]
+    b4.sort()
+    b5 = bo5["index","morton_index"]
+    b5.sort()
+    assert_array_equal(b3, b4)
+    assert_array_equal(b1, b5)
+    bo6 = sl1 ^ sl2
+    b6 = bo6["index", "morton_index"]
+    b6.sort()
+    assert_array_equal(b6, np.setxor1d(i1, i2))
+
+def test_boolean_ray_slice_no_overlap():
+    r"""Test to make sure that boolean objects (ray, slice, no overlap)
+    behave the way we expect.
+
+    Test non-overlapping ray and slice. This also checks that the original 
+    regions don't change as part of constructing the booleans.
+    """
+    ds = fake_amr_ds()
+    sl = ds.r[:,:,0.25]
+    ra = ds.ray([0]*3, [0, 1, 0])
+    # Store the original indices
+    i1 = sl["index","morton_index"]
+    i1.sort()
+    i2 = ra["index","morton_index"]
+    i2.sort()
+    ii = np.concatenate((i1, i2))
+    ii.sort()
+    # Make some booleans
+    bo1 = sl & ra
+    bo2 = sl - ra
+    bo3 = sl | ra
+    bo4 = ds.union([sl, ra])
+    bo5 = ds.intersection([sl, ra])
+    # This makes sure the original containers didn't change.
+    new_i1 = sl["index","morton_index"]
+    new_i1.sort()
+    new_i2 = ra["index","morton_index"]
+    new_i2.sort()
+    assert_array_equal(new_i1, i1)
+    assert_array_equal(new_i2, i2)
+    # Now make sure the indices also behave as we expect.
+    empty = np.array([])
+    assert_array_equal(bo1["index","morton_index"], empty)
+    assert_array_equal(bo5["index","morton_index"], empty)
+    b2 = bo2["index","morton_index"]
+    b2.sort()
+    assert_array_equal(b2, i1 )
+    b3 = bo3["index","morton_index"]
+    b3.sort()
+    assert_array_equal(b3, ii)
+    b4 = bo4["index","morton_index"]
+    b4.sort()
+    b5 = bo5["index","morton_index"]
+    b5.sort()
+    assert_array_equal(b3, b4)
+    bo6 = sl ^ ra
+    b6 = bo6["index", "morton_index"]
+    b6.sort()
+    assert_array_equal(b6, np.setxor1d(i1, i2))
+
+def test_boolean_ray_slice_overlap():
+    r"""Test to make sure that boolean objects (rays and slices, overlap)
+    behave the way we expect.
+
+    Test overlapping rays and slices.
+    """
+    ds = fake_amr_ds()
+    sl = ds.r[:,:,0.25]
+    ra = ds.ray([0, 0, 0.25], [0, 1, 0.25])
+    # Get indices of both.
+    i1 = sl["index","morton_index"]
+    i1.sort()
+    i2 = ra["index","morton_index"]
+    i1.sort()
+    ii = np.concatenate((i1, i2))
+    ii.sort()
+    # Make some booleans
+    bo1 = sl & ra
+    bo2 = sl - ra
+    bo3 = sl | ra
+    bo4 = ds.union([sl, ra])
+    bo5 = ds.intersection([sl, ra])
+    # Now make sure the indices also behave as we expect.
+    line = np.intersect1d(i1, i2)
+    sheet_minus_line = np.setdiff1d(i1, i2)
+    sheet = np.union1d(i1, i2)
+    b1 = bo1["index","morton_index"]
+    b1.sort()
+    b2 = bo2["index","morton_index"]
+    b2.sort()
+    b3 = bo3["index","morton_index"]
+    b3.sort()
+    assert_array_equal(b1, line)
+    assert_array_equal(b2, sheet_minus_line)
+    assert_array_equal(b3, sheet)
+    b4 = bo4["index","morton_index"]
+    b4.sort()
+    b5 = bo5["index","morton_index"]
+    b5.sort()
+    assert_array_equal(b3, i1)
+    assert_array_equal(b3, b4)
+    assert_array_equal(b1, b5)
+    bo6 = sl ^ ra
+    b6 = bo6["index", "morton_index"]
+    b6.sort()
+    assert_array_equal(b6, np.setxor1d(i1, i2))

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