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

Nathan Goldbaum nathan12343 at gmail.com
Sat Oct 26 23:07:38 PDT 2013


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
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.spacepope.org/pipermail/yt-users-spacepope.org/attachments/20131026/98eca488/attachment.htm>


More information about the yt-users mailing list