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

commits-noreply at bitbucket.org commits-noreply at bitbucket.org
Wed Aug 24 11:07:13 PDT 2016


1 new commit in yt:

https://bitbucket.org/yt_analysis/yt/commits/8e0cb2ce536d/
Changeset:   8e0cb2ce536d
Branch:      yt
User:        ngoldbaum
Date:        2016-08-24 18:06:42+00:00
Summary:     Merged in ngoldbaum/yt (pull request #2338)

Print a nicer error message when trying to create a profile of mixed particle/mesh fields. Closes #1261
Affected #:  3 files

diff -r c2ff880b5787cfe955d8871bc548a8d18b68303c -r 8e0cb2ce536dafacc0f073335d86295ac7650b38 yt/data_objects/profiles.py
--- a/yt/data_objects/profiles.py
+++ b/yt/data_objects/profiles.py
@@ -27,6 +27,8 @@
     YTQuantity
 from yt.units.unit_object import Unit
 from yt.data_objects.data_containers import YTFieldData
+from yt.utilities.exceptions import \
+    YTIllDefinedProfile
 from yt.utilities.lib.misc_utilities import \
     new_bin_profile1d, \
     new_bin_profile2d, \
@@ -940,10 +942,18 @@
     fields = ensure_list(fields)
     is_pfield = [data_source.ds._get_field_info(f).particle_type
                  for f in bin_fields + fields]
+    wf = None
+    if weight_field is not None:
+        wf = data_source.ds._get_field_info(weight_field)
+        is_pfield.append(wf.particle_type)
+        wf = wf.name
 
-    if len(bin_fields) == 1:
+    if any(is_pfield) and not all(is_pfield):
+        raise YTIllDefinedProfile(
+            bin_fields, data_source._determine_fields(fields), wf, is_pfield)
+    elif len(bin_fields) == 1:
         cls = Profile1D
-    elif len(bin_fields) == 2 and np.all(is_pfield):
+    elif len(bin_fields) == 2 and all(is_pfield):
         # log bin_fields set to False for Particle Profiles.
         # doesn't make much sense for CIC deposition.
         # accumulation and fractional set to False as well.

diff -r c2ff880b5787cfe955d8871bc548a8d18b68303c -r 8e0cb2ce536dafacc0f073335d86295ac7650b38 yt/data_objects/tests/test_profiles.py
--- a/yt/data_objects/tests/test_profiles.py
+++ b/yt/data_objects/tests/test_profiles.py
@@ -8,7 +8,13 @@
 from yt.testing import \
     fake_random_ds, \
     assert_equal, \
+    assert_raises, \
     assert_rel_equal
+from yt.utilities.exceptions import \
+    YTIllDefinedProfile
+from yt.visualization.profile_plotter import \
+    ProfilePlot, \
+    PhasePlot
 
 _fields = ("density", "temperature", "dinosaurs", "tribbles")
 _units = ("g/cm**3", "K", "dyne", "erg")
@@ -158,3 +164,34 @@
                         weight_field = None)
         p3d.add_fields(["particle_ones"])
         yield assert_equal, p3d["particle_ones"].sum(), 32**3
+
+def test_mixed_particle_mesh_profiles():
+    ds = fake_random_ds(32, particles=10)
+    ad = ds.all_data()
+    assert_raises(
+        YTIllDefinedProfile, ProfilePlot, ad, 'radius', 'particle_mass')
+    assert_raises(
+        YTIllDefinedProfile, ProfilePlot, ad, 'radius',
+        ['particle_mass', 'particle_ones'])
+    assert_raises(
+        YTIllDefinedProfile, ProfilePlot, ad, 'radius',
+        ['particle_mass', 'ones'])
+    assert_raises(
+        YTIllDefinedProfile, ProfilePlot, ad, 'particle_radius', 'particle_mass',
+        'cell_mass')
+    assert_raises(
+        YTIllDefinedProfile, ProfilePlot, ad, 'radius', 'cell_mass',
+        'particle_ones')
+
+    assert_raises(
+        YTIllDefinedProfile, PhasePlot, ad, 'radius', 'particle_mass',
+        'velocity_x')
+    assert_raises(
+        YTIllDefinedProfile, PhasePlot, ad, 'particle_radius', 'particle_mass',
+        'cell_mass')
+    assert_raises(
+        YTIllDefinedProfile, PhasePlot, ad, 'radius', 'cell_mass',
+        'particle_ones')
+    assert_raises(
+        YTIllDefinedProfile, PhasePlot, ad, 'particle_radius', 'particle_mass',
+        'particle_ones')

diff -r c2ff880b5787cfe955d8871bc548a8d18b68303c -r 8e0cb2ce536dafacc0f073335d86295ac7650b38 yt/utilities/exceptions.py
--- a/yt/utilities/exceptions.py
+++ b/yt/utilities/exceptions.py
@@ -593,3 +593,43 @@
         v += self.message
         v += " Specified bounds are %s" % self.bounds
         return v
+
+def screen_one_element_list(lis):
+    if len(lis) == 1:
+        return lis[0]
+    return lis
+
+class YTIllDefinedProfile(YTException):
+    def __init__(self, bin_fields, fields, weight_field, is_pfield):
+        nbin = len(bin_fields)
+        nfields = len(fields)
+        self.bin_fields = screen_one_element_list(bin_fields)
+        self.bin_fields_ptype = screen_one_element_list(is_pfield[:nbin])
+        self.fields = screen_one_element_list(fields)
+        self.fields_ptype = screen_one_element_list(is_pfield[nbin:nbin+nfields])
+        self.weight_field = weight_field
+        if self.weight_field is not None:
+            self.weight_field_ptype = is_pfield[-1]
+
+    def __str__(self):
+        msg = (
+            "\nCannot create a profile object that mixes particle and mesh "
+            "fields.\n\n"
+            "Received the following bin_fields:\n\n"
+            "   %s, particle_type = %s\n\n"
+            "Profile fields:\n\n"
+            "   %s, particle_type = %s\n"
+        )
+        msg = msg % (
+            self.bin_fields, self.bin_fields_ptype,
+            self.fields, self.fields_ptype
+        )
+
+        if self.weight_field is not None:
+            weight_msg = "\nAnd weight field:\n\n   %s, particle_type = %s\n"
+            weight_msg = weight_msg % (
+                self.weight_field, self.weight_field_ptype)
+        else:
+            weight_msg = ""
+
+        return msg + weight_msg

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