Hey there! I have a relatively extensive plugins file that adds lots of fields. I changed how one of the fields is calculated, but now I am getting an error about an undefined variable. I have the following function: def _get_scalar_func(_ptype, field_name, unit_dict): def _scalar(_ptype, field, data): if field_name in only_gas_fields: _ptype='gas' return data[_ptype, field_name] elif 'velocity_los' in field_name and data.ds.head('RAY')==0: pass else: old_data=data[_ptype, field_name] x_old_units=unyt.unyt_array(old_data.value*unit_dict[field_name][0], units=unit_dict[field_name][1], registry=data.ds.unit_registry) return x_old_units.to(unit_dict[field_name][2]) return _scalar it converts some scalar fields for me based on "unit_dict", which holds what units I want for each property. It also adds the field ('PartType0', 'entropy'), which is just a copy of ('gas', 'entropy') with the first "if" statement. However, it says that the variable "_ptype" is referenced before assignment, and none of my plugins stuff is added. When I try to add a field manually outside the plugins file like so: ds.add_field(('PartType0', 'Density_msun_pc3'), function=_get_scalar_func('PartType0', 'Density', gas_scalar_conversions), sampling_type='particle', units='Msun/pc**3') I get the following long error: Traceback (most recent call last): File "file_loader_tester.py", line 35, in <module> new_yt.add_field(('PartType0', 'Density_msun_pc3'), function=_get_scalar_func('PartType0', 'Density', gas_scalar_conversions), sampling_type='particle', units='Msun/pc**3') File "/home/jacob/.local/lib/python3.8/site-packages/yt/data_objects/static_output.py", line 1593, in add_field self.index File "/home/jacob/.local/lib/python3.8/site-packages/yt/data_objects/static_output.py", line 539, in index self.create_field_info() File "/home/jacob/.local/lib/python3.8/site-packages/yt/data_objects/static_output.py", line 613, in create_field_info self.field_info.load_all_plugins(self.default_fluid_type) File "/home/jacob/.local/lib/python3.8/site-packages/yt/fields/field_info_container.py", line 402, in load_all_plugins self.find_dependencies(loaded) File "/home/jacob/.local/lib/python3.8/site-packages/yt/fields/field_info_container.py", line 415, in find_dependencies deps, unavailable = self.check_derived_fields(loaded) File "/home/jacob/.local/lib/python3.8/site-packages/yt/fields/field_info_container.py", line 612, in check_derived_fields fd = fi.get_dependencies(ds=self.ds) File "/home/jacob/.local/lib/python3.8/site-packages/yt/fields/derived_field.py", line 253, in get_dependencies e[self.name] File "/home/jacob/.local/lib/python3.8/site-packages/yt/fields/field_detector.py", line 125, in __missing__ vv = finfo(self) File "/home/jacob/.local/lib/python3.8/site-packages/yt/fields/derived_field.py", line 294, in __call__ dd = self._function(self, data) File "/home/jacob/.config/yt/my_plugins.py", line 69, in _scalar old_data=data[_ptype, field_name] UnboundLocalError: local variable '_ptype' referenced before assignment This has been working for literally years, and I only made minor changes to the inner _scalar function recently. But this almost seems like an issue with the add_field() function, which I don't think I have ever touched. Any ideas?
This is my old function, which still works: def _get_scalar_func(_ptype, field_name, unit_dict): def _scalar(field, data): old_data=data[_ptype, field_name] x_old_units=unyt.unyt_array(old_data.value*unit_dict[field_name][0], units=unit_dict[field_name][1], registry=data.ds.unit_registry) return x_old_units.to(unit_dict[field_name][2]) return _scalar
participants (1)
-
Jacob Morgan