[yt-users] Custom units.

Nathan Goldbaum nathan12343 at gmail.com
Tue Nov 1 13:30:06 PDT 2016


Oh oops, I'm sorry my suggested test script had a typo, try this test
script:

http://paste.yt-project.org/show/6910/

I haven't actually tried to create an annotated volume rendering using a
field with this kB unit yet, maybe there's a deeper issue.

Unfortunately I need to work on a presentation for a conference right now
and don't have time to dive in deeply on this. If you can create a test
script that triggers this issue, using a publicly available dataset (e.g.
one of the datasets on yt-project.org/data), that would be helpful.

It might also be a lot easier to avoid this workaround and just specify the
label by hand. You'll need to build yt from source and then modify the
save_annotated function in yt/visualization/volume_rendering/scene.py to
optionally accept a label instead of calculating one based on the plotted
field.

-Nathan


On Tue, Nov 1, 2016 at 3:25 PM, Kuo-Chuan Pan <pankuoch at msu.edu> wrote:

> Dear Nathan,
>
> It gives a different error message.
> http://paste.yt-project.org/show/6909/
>
> I am using yt 3.3.2 installed from Anaconda on a Mac.
>
> Many thanks,
> Kuo-Chuan
>
> yt : [INFO     ] 2016-11-01 16:23:01,279 integer runtime parameter
> checkpointfilenumber overwrites a simulation scalar of the same name
> yt : [INFO     ] 2016-11-01 16:23:01,279 integer runtime parameter
> forcedplotfilenumber overwrites a simulation scalar of the same name
> yt : [INFO     ] 2016-11-01 16:23:01,279 integer runtime parameter nbegin
> overwrites a simulation scalar of the same name
> yt : [INFO     ] 2016-11-01 16:23:01,280 integer runtime parameter
> plotfilenumber overwrites a simulation scalar of the same name
> yt : [INFO     ] 2016-11-01 16:23:01,302 Parameters: current_time
>      = 0.298820349993
> yt : [INFO     ] 2016-11-01 16:23:01,302 Parameters: domain_dimensions
>     = [80 80 80]
> yt : [INFO     ] 2016-11-01 16:23:01,303 Parameters: domain_left_edge
>      = [ -1.00000000e+09  -1.00000000e+09  -1.00000000e+09]
> yt : [INFO     ] 2016-11-01 16:23:01,303 Parameters: domain_right_edge
>     = [  1.00000000e+09   1.00000000e+09   1.00000000e+09]
> yt : [INFO     ] 2016-11-01 16:23:01,304 Parameters:
> cosmological_simulation   = 0.0
> Traceback (most recent call last):
>   File "test.py", line 15, in <module>
>     print ad['Entropy']
>   File "/Users/pan/anaconda2/lib/python2.7/site-packages/yt/
> data_objects/data_containers.py", line 272, in __getitem__
>     self.get_data(f)
>   File "/Users/pan/anaconda2/lib/python2.7/site-packages/yt/
> data_objects/data_containers.py", line 1192, in get_data
>     self._generate_fields(fields_to_generate)
>   File "/Users/pan/anaconda2/lib/python2.7/site-packages/yt/
> data_objects/data_containers.py", line 1243, in _generate_fields
>     raise YTFieldUnitError(fi, fd.units)
> yt.utilities.exceptions.YTFieldUnitError: The field function associated
> with the field '('gas', 'Entropy')' returned data with units
> 'dimensionless' but was defined with units 'kB'.
>
>
> On Nov 1, 2016, at 4:16 PM, Nathan Goldbaum <nathan12343 at gmail.com> wrote:
>
> Which yt version is this? Can you reproduce it on yt 3.3.2?
>
> Does the following simple test script work?
>
>     from yt.units import kb
>     from yt.units.dimensions import energy, temperature
>
>     # load your dataset
>     ds = yt.load(...)
>
>     ds.unit_registry.add('kB', 1.3806488e-16,
> dimensions=energy/temperature, tex_repr='k_{B}')
>
>     def _entrdens(field,data):
>          """
>          create a new derived field to show both entropy and density
>          """
>          return data['ones']
>     ds.add_field("Entropy",function=_entrdens, units="kB",
> display_name="Entropy per Baryon")
>
>     ad = ds.all_data()
>
>     print ad['Entropy']
>
> On Tue, Nov 1, 2016 at 3:13 PM, Kuo-Chuan Pan <pankuoch at msu.edu> wrote:
>
>> Dear Nathan,
>>
>> Thanks very much for your help.
>> That was a stupid mistake and the error message is different with the
>> previous one I said.
>> If I changed to kb, the error message is:
>>
>> P014 yt : [ERROR    ] 2016-11-01 16:11:53,531 YTFieldUnitParseError: The
>> field '('gas', 'Entropy')' has unparseable units 'kB'.
>>   File "volume_rendering.py", line 164, in <module>
>>     plot_a_vr(fn,cycle,rotate=False)
>>   File "volume_rendering.py", line 101, in plot_a_vr
>>     tfh.set_bounds()
>>   File "/Users/pan/anaconda2/lib/python2.7/site-packages/yt/visuali
>> zation/volume_rendering/transfer_function_helper.py", line 66, in
>> set_bounds
>>     bounds = self.ds.h.all_data().quantities['Extrema'](self.field,
>> non_zero=True)
>>   File "/Users/pan/anaconda2/lib/python2.7/site-packages/yt/data_
>> objects/derived_quantities.py", line 510, in __call__
>>     rv = super(Extrema, self).__call__(fields, non_zero)
>>   File "/Users/pan/anaconda2/lib/python2.7/site-packages/yt/data_
>> objects/derived_quantities.py", line 67, in __call__
>>     sto.result = self.process_chunk(ds, *args, **kwargs)
>>   File "/Users/pan/anaconda2/lib/python2.7/site-packages/yt/data_
>> objects/derived_quantities.py", line 518, in process_chunk
>>     fd = data[field]
>>   File "/Users/pan/anaconda2/lib/python2.7/site-packages/yt/data_objects/data_containers.py",
>> line 272, in __getitem__
>>     self.get_data(f)
>>   File "/Users/pan/anaconda2/lib/python2.7/site-packages/yt/data_objects/data_containers.py",
>> line 1192, in get_data
>>     self._generate_fields(fields_to_generate)
>>   File "/Users/pan/anaconda2/lib/python2.7/site-packages/yt/data_objects/data_containers.py",
>> line 1245, in _generate_fields
>>     raise YTFieldUnitParseError(fi)
>> P015 yt : [ERROR    ] 2016-11-01 16:11:53,572 YTFieldUnitParseError: The
>> field '('gas', 'Entropy')' has unparseable units 'kB'.
>> ------------------------------------------------------------
>> --------------
>>
>> Many thanks,
>> Kuo-Chuan
>>
>> On Nov 1, 2016, at 4:03 PM, Nathan Goldbaum <nathan12343 at gmail.com>
>> wrote:
>>
>> At the top of your script, you have:
>>
>> from yt.units import kb
>>
>> but later you have:
>>
>> return kB*entrdens
>>
>> It should be:
>>
>> return kb*entrdens
>>
>> On Tue, Nov 1, 2016 at 3:00 PM, Kuo-Chuan Pan <pankuoch at msu.edu> wrote:
>>
>>> Dear Nathan,
>>>
>>> Thank you very much for your help.
>>> I was using ds.add_field()
>>>
>>> I have pasted my script and error message here:
>>> http://paste.yt-project.org/show/wToLggLnTYVpMxeqJtln/
>>>
>>> Many thanks,
>>> Kuo-Chuan
>>>
>>>
>>> On Nov 1, 2016, at 3:50 PM, Nathan Goldbaum <nathan12343 at gmail.com>
>>> wrote:
>>>
>>>
>>>
>>> On Tue, Nov 1, 2016 at 2:40 PM, Kuo-Chuan Pan <pankuoch at msu.edu> wrote:
>>>
>>>> Dear Nathan,
>>>>
>>>> Thanks very much for your email and suggestions.
>>>> I followed your suggested code but it is not working.
>>>> yt gives an error message:
>>>> yt : [ERROR ] 2016-11-01 15:25:27,650 YTFieldUnitParseError: The field
>>>> '('gas', 'Entropy')' has unparseable units 'kB’.
>>>>
>>>
>>> Did you use ds.add_field as I showed in my previous e-mail or
>>> yt.add_field as you originally had in your last e-mail? You need to use
>>> ds.add_field as I showed in my code snippet for my workaround to work
>>> properly.
>>>
>>> If you *did* use ds.add_field, can you pastebin the full script, along
>>> with any error messages and tracebacks you received upon running the script?
>>>
>>> We have paste.yt-project.org set up for this purpose.
>>>
>>> -Nathan
>>>
>>>
>>>>
>>>> The vales of my “entr” field is already in “kB/baryon” (specific
>>>> entropy) but the unit is incorrect.
>>>> Following your suggestions, I want to create two new units called “kB”
>>>> and “baryon” (1.674e-24 g)
>>>> and override the “entr” and “Entropy” units to “kB/baryon” (from
>>>> dimensionless).
>>>> Is it possible to do that?
>>>>
>>>> Many thanks,
>>>> Kuo-Chuan
>>>>
>>>>
>>>> On Nov 1, 2016, at 2:52 PM, Nathan Goldbaum <nathan12343 at gmail.com>
>>>> wrote:
>>>>
>>>> One could argue that it would be useful for save_annotated to
>>>> optionally take a fully user-specified colorbar label. That's not the case
>>>> now but it would be a relatively easy modification.
>>>>
>>>> Your 'entr' field isn't being recognized by yt with units because it's
>>>> not a field that is "known" by yt in the FLASH frontend:
>>>>
>>>> https://bitbucket.org/yt_analysis/yt/src/334cceb2c6e197acf1a
>>>> 8c7f273ee9dd0bd592f58/yt/frontends/flash/fields.py?at=yt&fil
>>>> eviewer=file-view-default#fields.py-43
>>>>
>>>> Specifically, it does not appear in the known_other_fields tuple in the
>>>> flash fields.py file. If entr is not specific to your version of FLASH it
>>>> may be worthwhile to add an entry there so that the entr field is read in
>>>> with the correct units.
>>>>
>>>> Similarly, kB isn't a unit that the yt unit system understands out of
>>>> the box (although I think one could make a case that it's worth including).
>>>> There is a way to add new units to the unit system though:
>>>>
>>>>     from yt.units import kb
>>>>     from yt.units.dimensions import energy, temperature
>>>>
>>>>     # load your dataset
>>>>     ds = yt.load(...)
>>>>
>>>>     ds.unit_registry.add('kB', 1.3806488e-16,
>>>> dimensions=energy/temperature, tex_repr='k_{B}')
>>>>
>>>>     def _entrdens(field,data):
>>>>          """
>>>>          create a new derived field to show both entropy and density
>>>>          """
>>>>          dens = data["dens"]
>>>>          entr = data["entr"]
>>>>          entrdens = entr*(np.exp(-(dens.in_cgs()/P
>>>> NS_DENSITY)**5))+PNS_ENTR
>>>>          return kb*entrdens
>>>>     ds.add_field("Entropy",function=_entrdens, units="kB",
>>>> display_name="Entropy per Baryon")
>>>>
>>>> And then make your volume rendering as you were doing before. It should
>>>> show up in the label as "Entropy per baryon" and with units of "kB".
>>>>
>>>> Please feel free to open issues or pull requests about any of the
>>>> things I raised above.
>>>>
>>>> -Nathan
>>>>
>>>>
>>>> On Tue, Nov 1, 2016 at 1:25 PM, Kuo-Chuan Pan <pankuoch at msu.edu> wrote:
>>>>
>>>>> Dear Nathan,
>>>>>
>>>>> Thanks very much for your help.
>>>>> entropy is a filed in my FLASH dataset. The unit of entropy is
>>>>> kB/baryon in the code but it is recognised as dimensionless in yt.
>>>>>
>>>>> The field “entr" is the entropy in my dataset. The derived field
>>>>> “Entropy” is the one I want to visualise.
>>>>> PNS_DENSITY and PNS_ENTR are just constants.
>>>>>
>>>>> Many thanks,
>>>>> Kuo-Chuan
>>>>>
>>>>>   11 def _entrdens(field,data):
>>>>>   12     """
>>>>>   13     create a new derived field to show both entropy and density
>>>>>   14     """
>>>>>   15     dens = data["dens"]
>>>>>   16     entr = data["entr"]
>>>>>   17     entrdens = entr*(np.exp(-(dens.in_cgs()/P
>>>>> NS_DENSITY)**5))+PNS_ENTR
>>>>>   18     return entrdens
>>>>>   19 yt.add_field("Entropy",function=_entrdens,units="dimensionless")
>>>>>
>>>>>
>>>>> On Nov 1, 2016, at 2:20 PM, Nathan Goldbaum <nathan12343 at gmail.com>
>>>>> wrote:
>>>>>
>>>>> Hi Kuo-Chuan,
>>>>>
>>>>> Can you show us how you've defined your derived field?
>>>>>
>>>>> Is entropy a derived field as well or is it a field that's included in
>>>>> your dataset? This is the entropy per baryon, right?
>>>>>
>>>>> -Nathan
>>>>>
>>>>> On Tue, Nov 1, 2016 at 1:09 PM, Kuo-Chuan Pan <pankuoch at msu.edu>
>>>>> wrote:
>>>>>
>>>>>> Dear all,
>>>>>>
>>>>>> I have a custom new derived field based on density (g/cm^3) and
>>>>>> entropy (kB/baryon but it is recognised as dimensionless).
>>>>>> Currently, this new derived field is set to dimensionless (it should
>>>>>> be kB/baryon).
>>>>>>
>>>>>> Could anyone teach me how to set this new derived filed to its
>>>>>> correct unit (which should be kB/baryon)?
>>>>>> Or is it possible to simply override the field label in my volume
>>>>>> rendering plot before using save_annotated()?
>>>>>>
>>>>>> Many thanks,
>>>>>> Kuo-Chuan
>>>>>> _______________________________________________
>>>>>> yt-users mailing list
>>>>>> yt-users at lists.spacepope.org
>>>>>> http://lists.spacepope.org/listinfo.cgi/yt-users-spacepope.org
>>>>>>
>>>>>
>>>>> _______________________________________________
>>>>> yt-users mailing list
>>>>> yt-users at lists.spacepope.org
>>>>> http://lists.spacepope.org/listinfo.cgi/yt-users-spacepope.org
>>>>>
>>>>>
>>>>>
>>>>> _______________________________________________
>>>>> yt-users mailing list
>>>>> yt-users at lists.spacepope.org
>>>>> http://lists.spacepope.org/listinfo.cgi/yt-users-spacepope.org
>>>>>
>>>>>
>>>> _______________________________________________
>>>> yt-users mailing list
>>>> yt-users at lists.spacepope.org
>>>> http://lists.spacepope.org/listinfo.cgi/yt-users-spacepope.org
>>>>
>>>>
>>>>
>>>> _______________________________________________
>>>> yt-users mailing list
>>>> yt-users at lists.spacepope.org
>>>> http://lists.spacepope.org/listinfo.cgi/yt-users-spacepope.org
>>>>
>>>>
>>> _______________________________________________
>>> yt-users mailing list
>>> yt-users at lists.spacepope.org
>>> http://lists.spacepope.org/listinfo.cgi/yt-users-spacepope.org
>>>
>>>
>>>
>>> _______________________________________________
>>> yt-users mailing list
>>> yt-users at lists.spacepope.org
>>> http://lists.spacepope.org/listinfo.cgi/yt-users-spacepope.org
>>>
>>>
>> _______________________________________________
>> yt-users mailing list
>> yt-users at lists.spacepope.org
>> http://lists.spacepope.org/listinfo.cgi/yt-users-spacepope.org
>>
>>
>>
>> _______________________________________________
>> yt-users mailing list
>> yt-users at lists.spacepope.org
>> http://lists.spacepope.org/listinfo.cgi/yt-users-spacepope.org
>>
>>
> _______________________________________________
> yt-users mailing list
> yt-users at lists.spacepope.org
> http://lists.spacepope.org/listinfo.cgi/yt-users-spacepope.org
>
>
>
> _______________________________________________
> yt-users mailing list
> yt-users at lists.spacepope.org
> http://lists.spacepope.org/listinfo.cgi/yt-users-spacepope.org
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.spacepope.org/pipermail/yt-users-spacepope.org/attachments/20161101/d4e06a96/attachment-0001.htm>


More information about the yt-users mailing list