Re: [Numpy-discussion] applying function under certain conditions From: Alexandre - 2005-05-20 12:50 ```On Fri, May 20, 2005 at 02:22:25PM +0200, Christian Meesters wrote: > Hi >=20 > I have a large 1D numarray array with some huge values and lots of=20 > zeros. Now, for better representation I wanted to plot the logarithm.=20 > Well, calculating the logarithm is not such a good idea if you have=20 > zeros in your array ... > However, how do you implement such cases like "only apply a function if= =20 > a certain condition is true", I'd like to ask whether there are nice=20 > short snippets which could show such cases. My own code is=20 > (unnecessarily?) long and ugly. I tried to combine 'where' and=20 > 'greater', but didn't succeed. I guess I'm just don't see the wood for=20 > the trees. How would you write this in my case? Assuming your data contains only positive or null values,you could try replacing zeros by a very small value before computing the log: log_scaled =3D log(where(data, data, 1e-50))=20 This uses the fact that 0 is false and non-zero is true. If you have negative values, you can use something like log_scaled =3D log(where(data>0, data, 1e-50)) since data>0 will return a boolean array suitable for where.=20 --=20 Alexandre Fayolle LOGILAB, Paris (France). http://www.logilab.com http://www.logilab.fr http://www.logilab.org ```

 Re: [Numpy-discussion] applying function under certain conditions From: Alexandre - 2005-05-20 12:50 Attachments: Message as HTML ```On Fri, May 20, 2005 at 02:22:25PM +0200, Christian Meesters wrote: > Hi >=20 > I have a large 1D numarray array with some huge values and lots of=20 > zeros. Now, for better representation I wanted to plot the logarithm.=20 > Well, calculating the logarithm is not such a good idea if you have=20 > zeros in your array ... > However, how do you implement such cases like "only apply a function if= =20 > a certain condition is true", I'd like to ask whether there are nice=20 > short snippets which could show such cases. My own code is=20 > (unnecessarily?) long and ugly. I tried to combine 'where' and=20 > 'greater', but didn't succeed. I guess I'm just don't see the wood for=20 > the trees. How would you write this in my case? Assuming your data contains only positive or null values,you could try replacing zeros by a very small value before computing the log: log_scaled =3D log(where(data, data, 1e-50))=20 This uses the fact that 0 is false and non-zero is true. If you have negative values, you can use something like log_scaled =3D log(where(data>0, data, 1e-50)) since data>0 will return a boolean array suitable for where.=20 --=20 Alexandre Fayolle LOGILAB, Paris (France). http://www.logilab.com http://www.logilab.fr http://www.logilab.org ```
 Re: [Numpy-discussion] applying function under certain conditions From: Perry Greenfield - 2005-05-20 13:26 ```On May 20, 2005, at 8:49 AM, Alexandre wrote: > On Fri, May 20, 2005 at 02:22:25PM +0200, Christian Meesters wrote: >> Hi >> >> I have a large 1D numarray array with some huge values and lots of >> zeros. Now, for better representation I wanted to plot the logarithm. >> Well, calculating the logarithm is not such a good idea if you have >> zeros in your array ... >> However, how do you implement such cases like "only apply a function >> if >> a certain condition is true", I'd like to ask whether there are nice >> short snippets which could show such cases. My own code is >> (unnecessarily?) long and ugly. I tried to combine 'where' and >> 'greater', but didn't succeed. I guess I'm just don't see the wood for >> the trees. How would you write this in my case? > > Assuming your data contains only positive or null values,you could try > replacing zeros by a very small value before computing the log: > > log_scaled = log(where(data, data, 1e-50)) > > This uses the fact that 0 is false and non-zero is true. > > If you have negative values, you can use something like > > log_scaled = log(where(data>0, data, 1e-50)) > > since data>0 will return a boolean array suitable for where. > > -- > Alexandre Fayolle LOGILAB, Paris (France). > http://www.logilab.com http://www.logilab.fr http://www.logilab.org Another alternative that takes two lines but perhaps is more readable is: data[data<=0] = 1e-50 # if you don't mind modifying the original array log_scaled = log(data) ```
 Re: [Numpy-discussion] applying function under certain conditions From: Christian Meesters - 2005-05-20 13:43 ```Hi Thanks. So, it is as easy as I thought ... Since I do not want to modify the original array in this case (it's only for plotting purposes), I will go for Alexandre's solution here. Thanks a lot, Christian ```
 [Numpy-discussion] Re: applying function under certain conditions From: Russell E. Owen - 2005-05-20 22:38 ```In article , Christian Meesters wrote: > Thanks. So, it is as easy as I thought ... > Since I do not want to modify the original array in this case (it's > only for plotting purposes), I will go for Alexandre's solution here. One more thing to consider...do you really want log? If you are looking for log-like behavior for positive values but graceful handling of 0 and negative values, consider arcsinh, e.g. arcsinh(scale * (x-offset)). -- Russell *Due to the properties I just mentioned, arcsinh is a very useful function for stretching astronomical images. Thanks to Robert Lupton for pointing this out to me. ```
 Re: [Numpy-discussion] Re: applying function under certain conditions From: Christian Meesters - 2005-05-21 06:56 ```Hi Oh yes, that's an idea. I actually really wanted log, though more for historical display reasons than for mathematical reasons. In my particular case it hardly makes a difference for the eye whether I use arcsinh or log, but in the near future I might want to use it. I will keep it in mind. Thanks for pointing this out to me. Have a nice weekend, Christian On 21 May 2005, at 00:36, Russell E. Owen wrote: > In article , > Christian Meesters wrote: > >> Thanks. So, it is as easy as I thought ... >> Since I do not want to modify the original array in this case (it's >> only for plotting purposes), I will go for Alexandre's solution here. > > One more thing to consider...do you really want log? If you are looking > for log-like behavior for positive values but graceful handling of 0 > and > negative values, consider arcsinh, e.g. arcsinh(scale * (x-offset)). > > -- Russell > > *Due to the properties I just mentioned, arcsinh is a very useful > function for stretching astronomical images. Thanks to Robert Lupton > for > pointing this out to me. > > > ```