[yt-svn] commit/yt: bwkeller: Merged in MatthewTurk/yt (pull request #1760)
commits-noreply at bitbucket.org
commits-noreply at bitbucket.org
Mon Nov 23 11:13:46 PST 2015
1 new commit in yt:
https://bitbucket.org/yt_analysis/yt/commits/b949b80ab094/
Changeset: b949b80ab094
Branch: yt
User: bwkeller
Date: 2015-11-23 19:13:38+00:00
Summary: Merged in MatthewTurk/yt (pull request #1760)
Add "fields" attribute to datasets
Affected #: 4 files
diff -r 903268eb8d717ff3af3fc5acd7634f5db854a1b4 -r b949b80ab0941331195e1653ce85e836909f20a9 doc/source/analyzing/fields.rst
--- a/doc/source/analyzing/fields.rst
+++ b/doc/source/analyzing/fields.rst
@@ -195,10 +195,32 @@
:ref:`field-list`. If you want to create additional custom derived fields,
see :ref:`creating-derived-fields`.
-The full list of fields available for a dataset can be found as
-the attribute ``field_list`` for native, on-disk fields and ``derived_field_list``
-for derived fields (``derived_field_list`` is a superset of ``field_list``).
-You can view these lists by examining a dataset like this:
+Every dataset has an attribute, ``ds.fields``. This attribute possesses
+attributes itself, each of which is a "field type," and each field type has as
+its attributes the fields themselves. When one of these is printed, it returns
+information about the field and things like units and so on. You can use this
+for tab-completing as well as easier access to information.
+
+As an example, you might browse the available fields like so:::
+
+ print(dir(ds.fields))
+ print(dir(ds.fields.gas))
+ print(ds.fields.gas.density)
+
+On an Enzo dataset, the result from the final command would look something like
+this:::
+
+ Alias Field for "('enzo', 'Density')" (gas, density): (units: g/cm**3)
+
+You can use this to easily explore available fields, particularly through
+tab-completion in Jupyter/IPython.
+
+For a more programmatic method of accessing fields, you can utilize the
+``ds.field_list``, ``ds.derived_field_list`` and some accessor methods to gain
+information about fields. The full list of fields available for a dataset can
+be found as the attribute ``field_list`` for native, on-disk fields and
+``derived_field_list`` for derived fields (``derived_field_list`` is a superset
+of ``field_list``). You can view these lists by examining a dataset like this:
.. code-block:: python
diff -r 903268eb8d717ff3af3fc5acd7634f5db854a1b4 -r b949b80ab0941331195e1653ce85e836909f20a9 yt/data_objects/static_output.py
--- a/yt/data_objects/static_output.py
+++ b/yt/data_objects/static_output.py
@@ -87,6 +87,36 @@
output_type_registry[name] = cls
mylog.debug("Registering: %s as %s", name, cls)
+class FieldTypeContainer(object):
+ def __init__(self, ds):
+ self.ds = weakref.proxy(ds)
+
+ def __getattr__(self, attr):
+ ds = self.__getattribute__('ds')
+ fnc = FieldNameContainer(ds, attr)
+ if len(dir(fnc)) == 0:
+ return self.__getattribute__(attr)
+ return fnc
+
+ def __dir__(self):
+ return list(set(t for t, n in self.ds.field_info))
+
+class FieldNameContainer(object):
+ def __init__(self, ds, field_type):
+ self.ds = ds
+ self.field_type = field_type
+
+ def __getattr__(self, attr):
+ ft = self.__getattribute__("field_type")
+ ds = self.__getattribute__("ds")
+ if (ft, attr) not in ds.field_info:
+ return self.__getattribute__(attr)
+ return ds.field_info[ft, attr]
+
+ def __dir__(self):
+ return [n for t, n in self.ds.field_info
+ if t == self.field_type]
+
class IndexProxy(object):
# This is a simple proxy for Index objects. It enables backwards
# compatibility so that operations like .h.sphere, .h.print_stats and
@@ -136,6 +166,7 @@
_index_class = None
field_units = None
derived_field_list = requires_index("derived_field_list")
+ fields = requires_index("fields")
_instantiated = False
def __new__(cls, filename=None, *args, **kwargs):
@@ -391,6 +422,7 @@
self.field_info.load_all_plugins()
deps, unloaded = self.field_info.check_derived_fields()
self.field_dependencies.update(deps)
+ self.fields = FieldTypeContainer(self)
def setup_deprecated_fields(self):
from yt.fields.field_aliases import _field_name_aliases
diff -r 903268eb8d717ff3af3fc5acd7634f5db854a1b4 -r b949b80ab0941331195e1653ce85e836909f20a9 yt/fields/derived_field.py
--- a/yt/fields/derived_field.py
+++ b/yt/fields/derived_field.py
@@ -36,6 +36,7 @@
def _TranslationFunc(field, data):
# We do a bunch of in-place modifications, so we will copy this.
return data[field_name].copy()
+ _TranslationFunc.alias_name = field_name
return _TranslationFunc
def NullFunc(field, data):
@@ -212,6 +213,25 @@
data_label += r"$"
return data_label
+ def __repr__(self):
+ if self._function == NullFunc:
+ s = "On-Disk Field "
+ elif self._function.func_name == "_TranslationFunc":
+ s = "Alias Field for \"%s\" " % (self._function.alias_name,)
+ else:
+ s = "Derived Field "
+ if isinstance(self.name, tuple):
+ s += "(%s, %s): " % self.name
+ else:
+ s += "%s: " % (self.name)
+ s += "(units: %s" % self.units
+ if self.display_name is not None:
+ s += ", display_name: '%s'" % (self.display_name)
+ if self.particle_type:
+ s += ", particle field"
+ s += ")"
+ return s
+
class FieldValidator(object):
pass
diff -r 903268eb8d717ff3af3fc5acd7634f5db854a1b4 -r b949b80ab0941331195e1653ce85e836909f20a9 yt/fields/tests/test_field_name_container.py
--- /dev/null
+++ b/yt/fields/tests/test_field_name_container.py
@@ -0,0 +1,13 @@
+from yt.utilities.answer_testing.framework import \
+ requires_ds, \
+ data_dir_load
+
+enzotiny = "enzo_tiny_cosmology/DD0046/DD0046"
+ at requires_ds(enzotiny)
+def test_simulated_halo_mass_function():
+ ds = data_dir_load(enzotiny)
+ for field_type in dir(ds.fields):
+ ft = getattr(ds.fields, field_type)
+ for field_name in dir(ft):
+ f = getattr(ft, field_name)
+ assert ((field_type, field_name) == f.name)
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