Error in Setting Particle Filter in EAGLE simulation

Hi, I tried to set a particle filter with EAGLE and got an error. Here is a simple example, which uses the EAGLE dataset from yt sample data. import yt from yt.data_objects.particle_filters import add_particle_filter eagle_fpath = './data/snapshot_028_z000p000/snap_028_z000p000.0.hdf5' unit_base = {'UnitLength_in_cm' : 3.085678e+24, 'UnitMass_in_g' : 1.989e+43, 'UnitVelocity_in_cm_per_s' : 100000.0} ds = yt.load(eagle_fpath,unit_base=unit_base) ad = ds.all_data() def cold_gas(pfilter, data): temperature = data[pfilter.filtered_type, "Temperature"] filter = (temperature.in_units('K') <= 1e5) return filter add_particle_filter("gas_cold", function=cold_gas, filtered_type='PartType0', requires=["Temperature"]) ds.add_particle_filter('gas_cold') Below shows the error. UnboundLocalError Traceback (most recent call last) /Users/stephaniehho/EAGLE_sim/sho/test_with_yt/test_z000p000/particle_filter_simple.py in <module>() 20 21 add_particle_filter("gas_cold", function=cold_gas, filtered_type='PartType0', requires=["Temperature"]) ---> 22 ds.add_particle_filter('gas_cold') /Users/stephaniehho/yt-git/yt/data_objects/static_output.pyc in add_particle_filter(self, filter) 673 if f is None: 674 return False --> 675 used = self._setup_filtered_type(f) 676 if used: 677 filter = f /Users/stephaniehho/yt-git/yt/data_objects/static_output.pyc in _setup_filtered_type(self, filter) 714 if filter.name not in self.filtered_particle_types: 715 self.filtered_particle_types.append(filter.name) --> 716 new_fields = self._setup_particle_types([filter.name]) 717 deps, _ = self.field_info.check_derived_fields(new_fields) 718 self.field_dependencies.update(deps) /Users/stephaniehho/yt-git/yt/data_objects/static_output.pyc in _setup_particle_types(self, ptypes) 723 if ptypes is None: ptypes = self.ds.particle_types_raw 724 for ptype in set(ptypes): --> 725 df += self._setup_particle_type(ptype) 726 return df 727 /Users/stephaniehho/yt-git/yt/data_objects/static_output.pyc in _setup_particle_type(self, ptype) 889 def _setup_particle_type(self, ptype): 890 orig = set(self.field_info.items()) --> 891 self.field_info.setup_particle_fields(ptype) 892 return [n for n, v in set(self.field_info.items()).difference(orig)] 893 /Users/stephaniehho/yt-git/yt/frontends/owls/fields.pyc in setup_particle_fields(self, ptype) 111 112 super(OWLSFieldInfo,self).setup_particle_fields( --> 113 ptype, num_neighbors=self._num_neighbors, ftype=ftype) 114 115 UnboundLocalError: local variable 'ftype' referenced before assignment What I find is that, this problem seems to be temporarily solved if I add en else case at line 111 of the code: https://github.com/yt-project/yt/blob/master/yt/frontends/owls/fields.py, else: ftype=ptype But this will raise the questions of, what are ftype and ptype designed for at the first place, or will this temporary fix cause other errors that I have not yet encountered. Thanks, Stephanie

Hi Stephanie, Thanks for including the script to reproduce the issue you're having. That made it much easier for me to see what was going on. I think just adding the else statement is fine. I suspect what happened is that the original author of the OWLS/EAGLE frontends didn't consider that those functions might be called when a user defines a particle filter. In that case, ftype should be the name of the particle filter ('gas_cold' for your example). Just adding the else statement as you've done should be sufficient to fix the issue. Would you like to make a pull request to the main yt repository making that fix? You could also add a test to the owls frontend (I'd do it in yt/frontends/owls/tests/test_outputs.py) that just makes sure your example script does something sensible. If you don't want to make the pull request I'm happy to do so, just let me know. -Nathan On Tue, Mar 13, 2018 at 3:36 PM, Stephanie Ho <stephaniehkho@gmail.com> wrote:
Hi,
I tried to set a particle filter with EAGLE and got an error. Here is a simple example, which uses the EAGLE dataset from yt sample data.
import yt from yt.data_objects.particle_filters import add_particle_filter eagle_fpath = './data/snapshot_028_z000p000/snap_028_z000p000.0.hdf5' unit_base = {'UnitLength_in_cm' : 3.085678e+24, 'UnitMass_in_g' : 1.989e+43, 'UnitVelocity_in_cm_per_s' : 100000.0} ds = yt.load(eagle_fpath,unit_base=unit_base) ad = ds.all_data()
def cold_gas(pfilter, data): temperature = data[pfilter.filtered_type, "Temperature"] filter = (temperature.in_units('K') <= 1e5) return filter
add_particle_filter("gas_cold", function=cold_gas, filtered_type='PartType0', requires=["Temperature"]) ds.add_particle_filter('gas_cold')
Below shows the error.
UnboundLocalError Traceback (most recent call last) /Users/stephaniehho/EAGLE_sim/sho/test_with_yt/test_ z000p000/particle_filter_simple.py in <module>() 20 21 add_particle_filter("gas_cold", function=cold_gas, filtered_type='PartType0', requires=["Temperature"]) ---> 22 ds.add_particle_filter('gas_cold')
/Users/stephaniehho/yt-git/yt/data_objects/static_output.pyc in add_particle_filter(self, filter) 673 if f is None: 674 return False --> 675 used = self._setup_filtered_type(f) 676 if used: 677 filter = f
/Users/stephaniehho/yt-git/yt/data_objects/static_output.pyc in _setup_filtered_type(self, filter) 714 if filter.name not in self.filtered_particle_types: 715 self.filtered_particle_types.append(filter.name) --> 716 new_fields = self._setup_particle_types([filter.name]) 717 deps, _ = self.field_info.check_derived_ fields(new_fields) 718 self.field_dependencies.update(deps)
/Users/stephaniehho/yt-git/yt/data_objects/static_output.pyc in _setup_particle_types(self, ptypes) 723 if ptypes is None: ptypes = self.ds.particle_types_raw 724 for ptype in set(ptypes): --> 725 df += self._setup_particle_type(ptype) 726 return df 727
/Users/stephaniehho/yt-git/yt/data_objects/static_output.pyc in _setup_particle_type(self, ptype) 889 def _setup_particle_type(self, ptype): 890 orig = set(self.field_info.items()) --> 891 self.field_info.setup_particle_fields(ptype) 892 return [n for n, v in set(self.field_info.items()). difference(orig)] 893
/Users/stephaniehho/yt-git/yt/frontends/owls/fields.pyc in setup_particle_fields(self, ptype) 111 112 super(OWLSFieldInfo,self).setup_particle_fields( --> 113 ptype, num_neighbors=self._num_neighbors, ftype=ftype) 114 115
UnboundLocalError: local variable 'ftype' referenced before assignment
What I find is that, this problem seems to be temporarily solved if I add en else case at line 111 of the code: https://github.com/yt-project/ yt/blob/master/yt/frontends/owls/fields.py, else: ftype=ptype
But this will raise the questions of, what are ftype and ptype designed for at the first place, or will this temporary fix cause other errors that I have not yet encountered.
Thanks, Stephanie _______________________________________________ yt-users mailing list -- yt-users@python.org To unsubscribe send an email to yt-users-leave@python.org
participants (2)
-
Nathan Goldbaum
-
Stephanie Ho