comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: Anyway to avoid this last for loop
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Re: Anyway to avoid this last for loop [message #64572] Fri, 09 January 2009 00:46 Go to next message
hldevil is currently offline  hldevil
Messages: 6
Registered: September 2008
Junior Member
Okay, I tested it. Works perfectly. Thank you very much!

Cheers,

Steffen
Re: Anyway to avoid this last for loop [message #64579 is a reply to message #64572] Thu, 08 January 2009 09:31 Go to previous messageGo to next message
hldevil is currently offline  hldevil
Messages: 6
Registered: September 2008
Junior Member
On 8 Jan., 15:28, Tom McGlynn <t...@milkyway.gsfc.nasa.gov> wrote:
> On Jan 8, 4:59 am, hlde...@gmx.de wrote:
>
>> ;AClength is how many frames are thrown out after one AC Hit ->
>> multiple entries can fall into one bin
>
>> detHist=histogram(data,REVERSE_INDICES=ri, /L64, binSize=AClength)
>> acHist=histogram(ACdeletes, /L64, binSize=AClength)
>
>> ;renormalize to one
>>  dI=where(detHist GT 1, cntD)
>>  aI=where(acHist GT 1, cntA)
>>  IF cntD NE 0 THEN detHist[dI]=1
>>  IF cntA NE 0 THEN acHist[aI]=1
>
> Can't these last four lines be replaced by
>    detHist = detHist<1
>    acHist  = acHist<1
>
> The < and > operators are useful though not necessarily intuitive.
>
>
>
>
>
>>  ;subtract the two histograms. All detector frames which have
>> corresponding AC frame should now have value 0, if AC frame exists but
>> not detector frame then value is -1. If only detector frame exists
>> (the ones we want) value stays 1!!!
>> diffHist=detHist-acHist
>
>> ;keep the ones with one
>>  keep=where(diffHist EQ 1, cnt)
>
>> ;And now my problem. I need the indices of the keep-frames but there
>> can be more than one in a bin so I'm stuck with this loop:
>
>> FOR k=0L, cnt-1 DO BEGIN
>>                     keepIndices=[keepIndices,ri[ri[keep[k]]:ri[keep[k]+1]-1]]
>> ENDFOR
>
> To do it all at once, I think you can just go back to the data array
> as below:
>
>     keepHist = where (diffHist EQ 1, cnt)   ; From the original
>     if (cnt gt 0) then begin
>         keepers = intarr(n_elements(detHist))
>         keepers[keepHist] = 1;
>         keepIndices = where(keepers[data/ACLENGTH] eq 1)
>     endif else begin
>         keepIndices = [-1]
>     endelse
>
> Haven't tested it, but it seems like it or something like it should
> work reasonably efficiently.
>
>     Good luck,
>     Tom McGlynn

Seems reasonable. I'll try it and see if I get the same results as
with the for loop.

Thanks,

Steffen
Re: Anyway to avoid this last for loop [message #64588 is a reply to message #64579] Thu, 08 January 2009 06:40 Go to previous messageGo to next message
David Fanning is currently offline  David Fanning
Messages: 11724
Registered: August 2001
Senior Member
Tom McGlynn writes:

> After posting I realized that this means that one doesn't have to use
> histogram's reverse indices at all in the solution. That may be
> illegal in this news group and is a strong indicator that the solution
> must be wrong. :)

At the very least, it violates our charter! :-)

Cheers,

David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
Re: Anyway to avoid this last for loop [message #64589 is a reply to message #64588] Thu, 08 January 2009 06:36 Go to previous messageGo to next message
Tom McGlynn is currently offline  Tom McGlynn
Messages: 13
Registered: January 2008
Junior Member
After posting I realized that this means that one doesn't have to use
histogram's reverse indices at all in the solution. That may be
illegal in this news group and is a strong indicator that the solution
must be wrong. :)

Tom
Re: Anyway to avoid this last for loop [message #64594 is a reply to message #64589] Thu, 08 January 2009 06:28 Go to previous messageGo to next message
Tom McGlynn is currently offline  Tom McGlynn
Messages: 13
Registered: January 2008
Junior Member
On Jan 8, 4:59 am, hlde...@gmx.de wrote:
> ;AClength is how many frames are thrown out after one AC Hit ->
> multiple entries can fall into one bin
>
> detHist=histogram(data,REVERSE_INDICES=ri, /L64, binSize=AClength)
> acHist=histogram(ACdeletes, /L64, binSize=AClength)
>
> ;renormalize to one
> dI=where(detHist GT 1, cntD)
> aI=where(acHist GT 1, cntA)
> IF cntD NE 0 THEN detHist[dI]=1
> IF cntA NE 0 THEN acHist[aI]=1

Can't these last four lines be replaced by
detHist = detHist<1
acHist = acHist<1

The < and > operators are useful though not necessarily intuitive.

>
> ;subtract the two histograms. All detector frames which have
> corresponding AC frame should now have value 0, if AC frame exists but
> not detector frame then value is -1. If only detector frame exists
> (the ones we want) value stays 1!!!
> diffHist=detHist-acHist
>
> ;keep the ones with one
> keep=where(diffHist EQ 1, cnt)
>
> ;And now my problem. I need the indices of the keep-frames but there
> can be more than one in a bin so I'm stuck with this loop:
>
> FOR k=0L, cnt-1 DO BEGIN
> keepIndices=[keepIndices,ri[ri[keep[k]]:ri[keep[k]+1]-1]]
> ENDFOR
>

To do it all at once, I think you can just go back to the data array
as below:

keepHist = where (diffHist EQ 1, cnt) ; From the original
if (cnt gt 0) then begin
keepers = intarr(n_elements(detHist))
keepers[keepHist] = 1;
keepIndices = where(keepers[data/ACLENGTH] eq 1)
endif else begin
keepIndices = [-1]
endelse

Haven't tested it, but it seems like it or something like it should
work reasonably efficiently.

Good luck,
Tom McGlynn
Re: Anyway to avoid this last for loop [message #64598 is a reply to message #64594] Thu, 08 January 2009 05:54 Go to previous messageGo to next message
R.Bauer is currently offline  R.Bauer
Messages: 1424
Registered: November 1998
Senior Member
There is no PEP8 for idl somewhere defined, or isn't it. And if so I
probably have to clean also my code

The example is difficult to read.

Reimar



hldevil@gmx.de schrieb:
> Hi Everybody,
>
> after having worked my way through dfannings and jdsmiths histogram
> tips I still haven't found a way to avoid one last (and time
> consuming) FOR-loop. Maybe someone has an idea.
>
> I have to lists (Millions of entries): one containing detector hit-
> times, one containing anticoincidence hit-times. Only those detector
> hits which do not have a AC event at the same time are supposed to be
> kept.
>
> I used a histogram approach:
>
> ;AClength is how many frames are thrown out after one AC Hit ->
> multiple entries can fall into one bin
>
> detHist=histogram(data,REVERSE_INDICES=ri, /L64, binSize=AClength)
> acHist=histogram(ACdeletes, /L64, binSize=AClength)
>
>
> ;renormalize to one
> dI=where(detHist GT 1, cntD)
> aI=where(acHist GT 1, cntA)
> IF cntD NE 0 THEN detHist[dI]=1
> IF cntA NE 0 THEN acHist[aI]=1
>
> ;subtract the two histograms. All detector frames which have
> corresponding AC frame should now have value 0, if AC frame exists but
> not detector frame then value is -1. If only detector frame exists
> (the ones we want) value stays 1!!!
> diffHist=detHist-acHist
>
> ;keep the ones with one
> keep=where(diffHist EQ 1, cnt)
>
> ;And now my problem. I need the indices of the keep-frames but there
> can be more than one in a bin so I'm stuck with this loop:
>
> FOR k=0L, cnt-1 DO BEGIN
> keepIndices=[keepIndices,ri[ri[keep[k]]:ri[keep[k]+1]-1]]
> ENDFOR
>
> Is there any way I can avoid this, especially because it scales with
> the number of hits which are kept.
>
> Best Regards,
>
> Steffen
Re: Anyway to avoid this last for loop [message #64668 is a reply to message #64594] Fri, 09 January 2009 10:28 Go to previous message
Jeremy Bailin is currently offline  Jeremy Bailin
Messages: 618
Registered: April 2008
Senior Member
> Can't these last four lines be replaced by
>    detHist = detHist<1
>    acHist  = acHist<1
>
> The < and > operators are useful though not necessarily intuitive.

For that matter, those operators are valid for the op= construction,
so you can write the very efficient (in both performance and typing
time):

detHist <= 1

but which will confuse the hell out of people who normally think in
most other languages where <= is a comparison. ;-)

-Jeremy.
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Compile/Restore on Startup
Next Topic: How to write an array into a dateset as a band, not a dateset?

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Wed Oct 08 18:43:42 PDT 2025

Total time taken to generate the page: 0.43283 seconds