Nathan, Thank you. If you care for a comment, I do not think this is a good idea. I sent you a snippet, my real field function looks like this: def StellarLuminosity(field,data): ts = data[('STAR','age')].in_units("yr").v zs = (data[('STAR', 'METALLICITY_SNII')]+data[('STAR', 'METALLICITY_SNIa')]).v return sps.GetLnu(ts,zs)*data[('STAR','mass')].in_units("Msun") The fake data caused an error in the statement ts = data[('STAR','age')].in_units("yr") because dimensionless data cannot be converted into years, and that was how I discovered that the incorrect data were sent to the field function. I can add a check "if 'FieldDetector' in str(type(data))" to the function, but think about what is going on in the actual calculation: a string comparison is going to be taking place every time that function is called, if it is called frequently, that will be a huge performance penalty. n On 5/27/2018 12:08 PM, Nathan Goldbaum wrote:
Hi Nick,
What you're seeing in those print statements is how yt's field detection system works. Before we actually select and return data, we pass your field function fake data to make sure it doesn't raise errors or it doesn't select fields that aren't available for your dataset.
Here's a script I made based on yours that helps explain what's going on:
https://gist.github.com/ngoldbaum/4d27987bda8ecbce5c9bd9b98bbbbf49
And here's the output of that script:
https://gist.github.com/ngoldbaum/475fcd827a53e4d02e76cb25711ff5ea
I made a couple modifications compared to your script. First, I added a line at the end that selects your new field from a data object, which causes real data to be fed into the field function after field detection completes. I also specified "units='auto'" in the call to add_field. The dimensions keyword is only used when "units='auto'" is set.
You can see that your field function is called twice. First with a `FieldDetector` instance and then the second time with a real yt data object. In general the field function might be called several times before real data is supplied to it. One way I commonly deal with this is the following hack to only see output when the field definition is called with real data:
def NewField(field, data): q = data[('STAR', 'age')] if 'FieldDetector' in str(type(data)): print("BBB",data) return q
Although more often I use python's debugger via "import pdb; pdb.set_trace()" instead of print statements.
Hope that helps,
Nathan
On Sun, May 27, 2018 at 10:51 AM, Nick Gnedin <ngnedin@gmail.com <mailto:ngnedin@gmail.com>> wrote:
Guys,
I am trying to add a new particle field, and apparently I am doing something wrong, but I could find the documentation for this:
import yt def NewField(field,data): q = data[('STAR','age')] print("BBB",data) return q d = yt.load("rei10_a0.1001/rei10_a0.1001.art") print('AAA',d.all_data()[("STAR","age")]) d.add_field(("STAR","new-field"),function=NewField,dimensions="time",sampling_type="particle")
The result is here:
AAA [ 1.35689106e+08 1.29578798e+08 1.15241829e+08 ..., 2.97175878e+07 2.04648908e+07 1.35365980e+07] yr BBB defaultdict(<function FieldDetector.__init__.<locals>.<lambda> at 0x2b9e698e3598>, {('STAR', 'BIRTH_TIME'): YTArray([ 1.]) (dimensionless), ('STAR', 'creation_time'): YTArray([ 1.]) (dimensionless), ('STAR', 'age'): YTArray([ 1.]) (dimensionless)})
It appears that the data object sent to NewField(field,data) has all its fields set to dimensionless.
I tried omitting sampling_type="particle", but it does not help, just generates an incomprehensible warning.
n _______________________________________________ yt-users mailing list -- yt-users@python.org <mailto:yt-users@python.org> To unsubscribe send an email to yt-users-leave@python.org <mailto:yt-users-leave@python.org>
participants (1)
-
Nick Gnedin