Skip to content

Commit

Permalink
Fix Python 3 bug with integer division
Browse files Browse the repository at this point in the history
The midPointAccessor variable, which is used various places in the code
(mainly but not exclusively in verbose prints), is supposed to be a
tuple of integers to index the array.  But the form used for the
division:

(tp-1)/2

produces a floating point result in Python 3.  And a tuple of floats is
no longer allowed as an index to a numpy array.  The fix is to force
integer division (which is clearly the intent here):

(tp-1)//2

This fix was made 3 places in the code.  It fixes an error in the print
statements when beVerbose=True is specified.
  • Loading branch information
rlwastro committed Jul 26, 2022
1 parent 42e4384 commit ceeb16f
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions fastkde/fastKDE.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ def applyBernacchiaFilter(self,doFlushArrays=True):
if(self.doSaveTransformedKernel):
self.kappaSC = kappaSC

midPointAccessor = tuple([(tp-1)/2 for tp in self.numTPoints])
midPointAccessor = tuple([(tp-1)//2 for tp in self.numTPoints])
#Calculate the transform of the self-consistent density estimate
self.phiSC[iCalcPhi] = self.ECF[iCalcPhi]*kappaSC[iCalcPhi]

Expand Down Expand Up @@ -549,7 +549,7 @@ def normFunc(delta):

if(self.beVerbose):
normConst = sum(pdf*prod(self.deltaX))
midPointAccessor = tuple([(tp-1)/2 for tp in self.numTPoints])
midPointAccessor = tuple([(tp-1)//2 for tp in self.numTPoints])
print("Normalization of pdf = {}. phiSC[0] = {}".format(normConst,self.phiSC[midPointAccessor]))


Expand Down Expand Up @@ -839,7 +839,7 @@ def reApplyFilter(self,pdf):
#Transform the PDF to fourier space
phiTilde_tmp = fft.fftshift(fft.ifftn(fft.ifftshift(ma.filled(pdf,0.0))))
#Normalize the transform
midPointAccessor = tuple([(tp-1)/2 for tp in self.numTPoints])
midPointAccessor = tuple([(tp-1)//2 for tp in self.numTPoints])
phiTilde_tmp /= phiTilde_tmp[midPointAccessor]

#Reapply the filter
Expand Down

0 comments on commit ceeb16f

Please sign in to comment.