[yt-users] How to use if, elif, else statements in creating a derived field

Britton Smith brittonsmith at gmail.com
Sun Oct 27 01:23:10 PDT 2013


Is the image actually being made in a linear scale now?  If not, this is
most likely the problem as your field does have the potential to be
negative.
If the image is being made in a linear scale and you are still seeing white
patches, then I think you need to inspect the values of this field
throughout your data by hand.
Do something like:
ad = pf.h.all_data()
print ad["My_Field"]
You can use this to search for NaNs, zeros, or any sort of numbers that you
don't think you should be getting.

Also, in the future, please use yt_lodgeit.py to upload images to imgur,
then just post the link in your email rather than including the image as an
attachment.  The syntax is:
yt_lodgeit.py some_image.png


On Sun, Oct 27, 2013 at 9:02 AM, Reju Sam John <rejusamjohn at gmail.com>wrote:

>
> Still white patches are coming. I don't know what's the problem.
>
>
> On Sun, Oct 27, 2013 at 11:46 AM, Reju Sam John <rejusamjohn at gmail.com>wrote:
>
>> Thanks nathan and suoqing..... Let me try with your suggestions....
>>
>>
>> On Sun, Oct 27, 2013 at 11:37 AM, Nathan Goldbaum <nathan12343 at gmail.com>wrote:
>>
>>> And using suoqing's example, I can control whether or not we should take
>>> the logarithm of a variable in plots:
>>>
>>> @derived_field(name = "MyField", take_log=False)
>>> def my_new_field(field, data):
>>>     tmp_data = (data["MachNumber"] <= 2) * (.5 * data["MachNumber"]**2.
>>> - 1)
>>>     tmp_data = tmp_data + (data["MachNumber"] > 2) * (data["MachNumber"]
>>> <= 100) * ((.5* (data["MachNumber"])**4) + (.5* (data["MachNumber"])**3))
>>>     tmp_data = tmp_data + (data["MachNumber"] > 100) * 5.446361E-01
>>>     return tmp_data
>>>
>>> If you've already created a plot, you can also disable field logging
>>> like so:
>>>
>>> slc = SlicePlot(pf, 0, 'Density')
>>> slc.set_log('Density', False)
>>>
>>> -Nathan
>>>
>>>
>>> On Sat, Oct 26, 2013 at 11:02 PM, Suoqing JI <jisuoqing at gmail.com>wrote:
>>>
>>>> Hi Reju,
>>>>
>>>> The first thing occurs to me is that the area of white patch contains
>>>> no positive field value, so it can not be plotted in log scale.
>>>>
>>>> Also, about the way of defining a derived field, I might prefer the
>>>> following way, for example:
>>>>
>>>> @derived_field(name = "MyField")
>>>> def my_new_field(field, data):
>>>>     tmp_data = (data["MachNumber"] <= 2) * (.5 * data["MachNumber"]**2.
>>>> - 1)
>>>>     tmp_data = tmp_data + (data["MachNumber"] > 2) *
>>>> (data["MachNumber"] <= 100) * ((.5* (data["MachNumber"])**4) + (.5*
>>>> (data["MachNumber"])**3))
>>>>     tmp_data = tmp_data + (data["MachNumber"] > 100) * 5.446361E-01
>>>>     return tmp_data
>>>>
>>>> Best wishes,
>>>> Suoqing
>>>>
>>>> On Oct 26, 2013, at 10:27 PM, Reju Sam John <rejusamjohn at gmail.com>
>>>> wrote:
>>>>
>>>> Dear Matt,
>>>> Thanks for the help. Now my problem with the code solved. But the slice
>>>> plot of the new field has some problem -- white patch are coming as shown
>>>> below. What may be the reason?
>>>> <output.png>
>>>>
>>>>
>>>> On Sat, Oct 26, 2013 at 7:59 PM, Matthew Turk <matthewturk at gmail.com>wrote:
>>>>
>>>>> Hi Reju,
>>>>>
>>>>> On Sat, Oct 26, 2013 at 2:50 AM, Reju Sam John <rejusamjohn at gmail.com>
>>>>> wrote:
>>>>> > Dear Kacper,
>>>>> >
>>>>> > My logic was wrong, and yours is right. But with the following code
>>>>> >
>>>>> > @derived_field(name = "MyField")
>>>>> > def my_new_field(field, data):
>>>>> >     temp = data["MachNumber"].copy().fill(5.446361E-01)
>>>>> >     #print temp
>>>>> >
>>>>> >     ind = np.where(data["MachNumber"] < 2)
>>>>> >     temp[ind] = 1.9564E-3*((data["MachNumber"][ind]**2) - 1)
>>>>> >     ind = np.where((data["MachNumber"] >= 2) and
>>>>> (data["MachNumber"]) < 100)
>>>>> >     temp[ind] =
>>>>> >
>>>>> (5.699327E-1*((data["MachNumber"][ind]-1)**4))/(data["MachNumber"][ind]**4)
>>>>> > - (3.337557E-1*
>>>>> > ((data["MachNumber"][ind]-1)**3))/((data["MachNumber"][ind])**4) +
>>>>> > (4.173271E+0*
>>>>> > ((data["MachNumber"][ind]-1)**2))/((data["MachNumber"][ind])**4) -
>>>>> > (9.775620E+0*
>>>>> ((data["MachNumber"][ind]-1)))/((data["MachNumber"][ind])**4)+
>>>>> > 5.455605E+0/(data["MachNumber"][ind])**4
>>>>> >     return temp
>>>>> >
>>>>> > I am getting this  Traceback
>>>>> >
>>>>> >  line 41, in my_new_field
>>>>> >     temp[ind] = 1.9564E-3*((data["MachNumber"][ind]**2) - 1)
>>>>> > TypeError: 'NoneType' object does not support item assignment
>>>>> >
>>>>> > Would you suggest any solution?
>>>>>
>>>>> I believe the problem is in your .fill() statement.  That doesn't
>>>>> return an array, so you need to do it after the assignment.  For
>>>>> instance:
>>>>>
>>>>> temp = data["MachNumber"].copy()
>>>>> temp.fill(5.446361E-01)
>>>>>
>>>>> -Matt
>>>>>
>>>>> >
>>>>> >
>>>>> >
>>>>> > On Mon, Oct 21, 2013 at 2:58 PM, Kacper Kowalik <
>>>>> xarthisius.kk at gmail.com>
>>>>> > wrote:
>>>>> >>
>>>>> >> On 10/21/2013 11:10 AM, Reju Sam John wrote:
>>>>> >> > Dear all,
>>>>> >> >
>>>>> >> > I would like to create a derived field which should return a value
>>>>> >> > according to specified conditions. My definition of new field is
>>>>> shown
>>>>> >> > below.. But it is giving error.
>>>>> >> >
>>>>> >> >
>>>>> >> > @derived_field(name = "MyField")
>>>>> >> > def my_new_field(field, data):
>>>>> >> >     if data.pf["MachNumber"] < 2 :
>>>>> >> >         return (.5* (data["MachNumber"])**2) - 1
>>>>> >> >     elif data.pf["MachNumber"] < 100 :
>>>>> >> >         return (.5* (data["MachNumber"])**4) + (.5*
>>>>> >> > (data["MachNumber"])**3)
>>>>> >> >     else:
>>>>> >> >        return 5.446361E-01
>>>>> >> >
>>>>> >> >
>>>>> >> >
>>>>> >> > Please suggest me how to implement if, elif, else statements in
>>>>> creating
>>>>> >> > a
>>>>> >> > derived field.
>>>>> >>
>>>>> >> Hi,
>>>>> >> your derived fields looks fine (except for missing space in the
>>>>> >> indentation of the last statement). Could you attach the backtrace?
>>>>> >>
>>>>> >> One thing that's confusing me is 'data.pf["MachNumber"]'. Do you
>>>>> have a
>>>>> >> global parameter with such name defined or you'd rather create a
>>>>> MyField
>>>>> >> based on local value of "MachNumber" field?
>>>>> >> If the latter you could try something like this:
>>>>> >>
>>>>> >> @derived_field(name = "MyField")
>>>>> >> def my_new_field(field, data):
>>>>> >>     temp = data["MachNumber"].copy().fill(5.446361E-01)
>>>>> >>     ind = np.where(data["MachNumber"] < 2)
>>>>> >>     temp[ind] = 0.5 * data["MachNumber"][ind] ** 2 - 1.0
>>>>> >>     ind = np.where((data["MachNumber"] >= 2) &
>>>>> >>                    (data["MachNumber"]) < 100))
>>>>> >>     temp[ind] = 0.5 * data["MachNumber"][ind] ** 4 + \
>>>>> >>                 0.5 * data["MachNumber"][ind] ** 3
>>>>> >>     return temp
>>>>> >>
>>>>> >>
>>>>> >> Cheers,
>>>>> >> Kacper
>>>>> >>
>>>>> >>
>>>>> >>
>>>>> >> _______________________________________________
>>>>> >> yt-users mailing list
>>>>> >> yt-users at lists.spacepope.org
>>>>> >> http://lists.spacepope.org/listinfo.cgi/yt-users-spacepope.org
>>>>> >>
>>>>> >
>>>>> >
>>>>> >
>>>>> > --
>>>>> > Reju Sam John
>>>>> >
>>>>> > _______________________________________________
>>>>> > 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
>>>>>
>>>>
>>>>
>>>>
>>>> --
>>>> Reju Sam John
>>>>  _______________________________________________
>>>> 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
>>>
>>>
>>
>>
>> --
>> Reju Sam John
>>
>
>
>
> --
> Reju Sam John
>
> _______________________________________________
> 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/20131027/6d4221b5/attachment.htm>


More information about the yt-users mailing list