yt-project / yt

Main yt repository
http://yt-project.org
Other
463 stars 276 forks source link

yt.load_particles cannot find newly derived fields #1027

Closed yt-fido closed 7 years ago

yt-fido commented 9 years ago

Originally reported by: Benjamin Thompson (Bitbucket: cosmosquark, GitHub: cosmosquark)


This script.

#!python

import numpy as np
import yt
from yt.units.yt_array import YTArray
from yt.units import kiloparsec, Msun, kilometer, second, Gyr

def _do_magick(field, data):
    rv = data[("all","particle_birth_epoch")] * 10.0
    return rv 

n_data = 3000

sim_data_verb = {"particle_mass": YTArray(np.random.rand(n_data) * 1000,"Msun"),
            "particle_position_x": YTArray(np.random.rand(n_data),"kpc"),
            "particle_position_y": YTArray(np.random.rand(n_data),"kpc"),
            "particle_position_z": YTArray(np.random.rand(n_data),"kpc"),
            "particle_velocity_x": YTArray(np.random.rand(n_data), "kpc/s"),
            "particle_velocity_y": YTArray(np.random.rand(n_data), "kpc/s"),
            "particle_velocity_z": YTArray(np.random.rand(n_data), "kpc/s"),
            "particle_mass_birth_epoch": YTArray(np.random.rand(n_data), "Msun"),
            "particle_birth_epoch": YTArray(np.random.rand(n_data), "Gyr"),
            "particle_metallicity": YTArray(np.random.rand(n_data),"dimensionless")
        }

bbox = yt.YTArray(np.array([[0,1],[0,1],[0,1]]),"kpc")

ds = yt.load_particles(sim_data_verb, length_unit=kiloparsec, mass_unit=Msun, time_unit=second, bbox=bbox, n_ref=256)
ds.add_field(name = ('all','particle_birth_epoch_two'),function=_do_magick, units="Gyr")

ad = ds.all_data()

print ds.derived_field_list

print ad[('all','particle_birth_epoch_two')]

returns this error

#!

  File "yt_example2.py", line 35, in <module>
    print ad[('all','particle_birth_epoch_two')]
  File "/home/ben/.local/lib/python2.7/site-packages/yt-3.2dev-py2.7-linux-x86_64.egg/yt/data_objects/data_containers.py", line 246, in __getitem__
    f = self._determine_fields([key])[0]
  File "/home/ben/.local/lib/python2.7/site-packages/yt-3.2dev-py2.7-linux-x86_64.egg/yt/data_objects/data_containers.py", line 541, in _determine_fields
    raise YTFieldTypeNotFound(ftype)
yt.utilities.exceptions.YTFieldTypeNotFound: Could not find field 'all'.

It seems as though it cannot find the newly derived field


yt-fido commented 8 years ago

Original comment by Nathan Goldbaum (Bitbucket: ngoldbaum, GitHub: ngoldbaum):


Merged in brittonsmith/yt (pull request #2087)

Making YTFieldTypeNotFound exception more informative. (closes #1027)

yt-fido commented 8 years ago

Original comment by Nathan Goldbaum (Bitbucket: ngoldbaum, GitHub: ngoldbaum):


Merged in brittonsmith/yt (pull request #2087)

Making YTFieldTypeNotFound exception more informative. (closes #1027)

yt-fido commented 8 years ago

Original comment by Nathan Goldbaum (Bitbucket: ngoldbaum, GitHub: ngoldbaum):


Merged in brittonsmith/yt (pull request #2087)

Making YTFieldTypeNotFound exception more informative. (closes #1027)

yt-fido commented 8 years ago

Original comment by Nathan Goldbaum (Bitbucket: ngoldbaum, GitHub: ngoldbaum):


Merged in brittonsmith/yt (pull request #2087)

Making YTFieldTypeNotFound exception more informative. (closes #1027)

yt-fido commented 9 years ago

Original comment by Nathan Goldbaum (Bitbucket: ngoldbaum, GitHub: ngoldbaum):


It works if you do

ds.add_field(name = ('all','particle_birth_epoch_two'),function=_do_magick, units="Gyr", particle_type=True)

The error message could probably be improved here and we could probably do a better job of automatically detecting that a field should be particle_type=True.