@@ -1890,5 +1890,87 @@ def test_drawreal_seg_fault():
18901890 np .testing .assert_array_equal (image .array , 0 )
18911891
18921892
1893+ def _find_maxk (kim , thresh , count_thresh = True ):
1894+ No2 = kim .xmax
1895+ dk = np .pi / No2
1896+
1897+ thresh *= thresh
1898+ max_ix = No2
1899+ n_below_thresh = 0
1900+ maxk_ix = 0
1901+
1902+ ix = 0
1903+ while ix <= max_ix :
1904+
1905+ iy = 0
1906+ while iy <= ix :
1907+ # The bottom side of the square in the lower-right quadrant.
1908+ norm_kval = kim (iy , - ix )
1909+ norm_kval = (norm_kval * norm_kval .conjugate ()).real
1910+
1911+ if norm_kval <= thresh and iy != ix and ix != No2 :
1912+ # The top side of the square in the upper-right quadrant.
1913+ norm_kval = kim (iy , ix )
1914+ norm_kval = (norm_kval * norm_kval .conjugate ()).real
1915+
1916+ if norm_kval <= thresh and iy > 0 :
1917+ # The right side of the square in the lower-right quadrant.
1918+ norm_kval = kim (ix , - iy )
1919+ norm_kval = (norm_kval * norm_kval .conjugate ()).real
1920+
1921+ if norm_kval <= thresh and ix > 0 and iy != No2 :
1922+ # The right side of the square in the upper-right quadrant.
1923+ norm_kval = kim (ix , iy )
1924+ norm_kval = (norm_kval * norm_kval .conjugate ()).real
1925+
1926+ iy += 1
1927+
1928+ if norm_kval > thresh :
1929+ maxk_ix = ix
1930+ n_below_thresh = 0
1931+ break
1932+
1933+ if count_thresh :
1934+ if norm_kval <= thresh :
1935+ n_below_thresh += 1
1936+ else :
1937+ n_below_thresh += 1
1938+ if n_below_thresh == 5 :
1939+ break
1940+
1941+ ix += 1
1942+
1943+ maxk_ix += 1
1944+ return maxk_ix * dk
1945+
1946+
1947+ @timer
1948+ def test_interpolatedimage_maxk_python ():
1949+ # this code makes an image where there is a gap in the fourier
1950+ # space image of 4 pixels where pixels go above and below the
1951+ # maxk threshold. Four pixels is exactly the gap needed to trigger
1952+ # the bug in the maxk code we are testing for.
1953+ im = galsim .Gaussian (fwhm = 0.9 ).drawImage (scale = 0.2 )
1954+ iim = galsim .InterpolatedImage (im )
1955+ orig_maxk = iim .maxk
1956+ kim = iim ._xim .copy ()
1957+ kim .wcs = galsim .PixelScale (1.0 )
1958+ kim = kim .calculate_fft ()
1959+ kx , ky = kim .get_pixel_centers ()
1960+ kx *= kim .scale
1961+ ky *= kim .scale
1962+ thresh = iim .gsparams .maxk_threshold * kim (0 ,0 ).real
1963+ maxk_py = _find_maxk (kim , thresh , count_thresh = True )
1964+ maxk_ix = np .floor (maxk_py / kim .scale ).astype (int )
1965+ kim [maxk_ix , maxk_ix + 4 ] = kim [0 , 0 ].real * 0.1
1966+
1967+ maxk_py_false = _find_maxk (kim , thresh , count_thresh = False ) / im .wcs ._maxScale ()
1968+ maxk_py_true = _find_maxk (kim , thresh , count_thresh = True ) / im .wcs ._maxScale ()
1969+
1970+ print ("galsim|pybuggy|pyfixed:" , orig_maxk , maxk_py_false , maxk_py_true )
1971+ assert maxk_py_false != maxk_py_true
1972+ assert orig_maxk == maxk_py_true
1973+
1974+
18931975if __name__ == "__main__" :
18941976 runtests (__file__ )
0 commit comments