def testsmall(self): ''' sse / avx issue for tiny? ''' # print("testsmall, fixme, segfault/infinite loop so no CI") im = np.array([[0, 0, 0, 0, 0], [0, 2, 0, 0, 0], [0, 1, 1, 0, 0], [0, 0, 0, 0, 0]], np.float32) la = np.empty(im.shape, 'i') wk = np.empty(im.shape, 'b') localmaxlabel(im, la, wk) self.assertEqual(la[1, 1], 1)
def testbug(self): ''' case caught with image from Mariana Mar Lucas, caused a segfault''' im = np.array([[0, 0, 0, 0, 0, 0, 0, 0], [0, 2, 0, 0, 0, 0, 0, 0], [0, 1, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]], np.float32) la = np.empty(im.shape, 'i') wk = np.empty(im.shape, 'b') localmaxlabel(im, la, wk) self.assertEqual(la[1, 1], 1)
def test3(self): # print("in test 2") im = make_test_image(N=2048) l1 = np.zeros(im.shape, np.int32) l2 = np.zeros(im.shape, np.int32) l3 = np.zeros(im.shape, np.int32) wk = np.zeros(im.shape, np.int8) start = time.time() for i in range(10): npks = localmaxlabel(im, l1, wk) end = time.time() npks = localmaxlabel(im, l1, wk, 10) ctime = (end - start) * 100. start = time.time() for i in range(10): npks = localmaxlabel(im, l2, wk, 1) end = time.time() npks = localmaxlabel(im, l2, wk, 11) stime = (end - start) * 100. start = time.time() for i in range(10): npks = localmaxlabel(im, l3, wk, 2) end = time.time() npks = localmaxlabel(im, l3, wk, 12) atime = (end - start) * 100. if not (l2 == l3).all(): import pylab as pl pl.imshow(l2) pl.imshow(l3) pl.show() self.assertEqual((l2 == l3).all(), True) self.assertEqual((l1 == l3).all(), True) print("Timing 2048 c %.3f ms, sse %.3f, avx %.3f" % (ctime, stime, atime))
def test1( self): im = make_test_image() start = time.time() l1 = localmax(im) end = time.time() pytime = (end-start)*1000. l2 = np.zeros(im.shape,np.int32) wk = np.zeros(im.shape,np.int8) start = time.time() npks = localmaxlabel(im, l2 , wk ) end=time.time() ctime = (end-start)*1000. self.assertEqual( (l2==l1).all(), True ) print("Timing python %.3f ms, c %.3f ms"%(pytime,ctime))
def test3(self): # print("in test 2") im = make_test_image(N=2048) l0 = np.zeros(im.shape, np.int32) l1 = np.zeros(im.shape, np.int32) wp = np.zeros(im.shape, np.int32) wk = np.zeros(im.shape, np.int8) N = 4 if GOTNUMBA: npks = localmax(im, l0, wp) start = timer() for i in range(N): npks = localmax(im, l0, wp) end = timer() ptime = (end - start) * 1000. / N else: ptime = np.nan npks = localmaxlabel(im, l1, wk) start = timer() for i in range(N): npks = localmaxlabel(im, l1, wk) end = timer() ctime = (end - start) * 1000. / N print("Timing 2048 numba %.3f ms c %.3f ms" % (ptime, ctime))
def demo(): import pylab as pl im = make_test_image() l1 = localmax(im) l2 = np.zeros(im.shape, np.int32) wk = np.zeros(im.shape, np.int8) npks = localmaxlabel(im, l2, wk, cpu=0) ax1 = pl.subplot(2, 2, 1) pl.imshow(im) pl.xlim(0, im.shape[0]) pl.ylim(0, im.shape[1]) ax1 = pl.subplot(2, 2, 2, sharex=ax1, sharey=ax1) pl.imshow((l1 * 13.0) % 37) pl.xlim(0, im.shape[0]) pl.ylim(0, im.shape[1]) ax1 = pl.subplot(2, 2, 3, sharex=ax1, sharey=ax1) pl.imshow((l2 * 13.0) % 37) pl.xlim(0, im.shape[0]) pl.ylim(0, im.shape[1]) pl.show()
def test1(self): im = self.im if self.dopy: l1 = np.zeros(im.shape, np.int32) wp = np.zeros(im.shape, np.int32) start = timer() localmax(im, l1, wp) end = timer() pytime = (end - start) * 1000. l = [ l1, ] wk = [ 0, ] times = [ pytime, ] names = [ "python", ] for cpu, name in [(0, "ansi"), (1, "sse2"), (2, "avx2")]: li = np.zeros(im.shape, np.int32) wi = np.zeros(im.shape, np.int8) start = timer() # print("cpu",cpu) npks = localmaxlabel(im.copy(), li, wi, cpu=cpu) end = timer() ctime = (end - start) * 1000. l.append(li) times.append(ctime) names.append(name) lref = l[0] print("Timing", end=" ") for i in range(len(names)): print("%s %.3f ms" % (names[i], times[i]), end=" ") print() for li in l[1:]: self.assertTrue((lref == l1).all())
def bench(int_im): im = int_im float_im = int_im.astype(np.float32) labelc = np.zeros(im.shape, np.int32) labelm = np.zeros(im.shape, np.int32) work = np.zeros(im.shape, np.int8) end0 = time.time() nc = cImageD11.connectedpixels(float_im, labelc, 1000) end1 = time.time() rc = cImageD11.blobproperties(float_im, labelc, nc, 0) cImageD11.blob_moments(rc) end2 = time.time() print("cptime", nc, "%.3f %.3f" % ((end1 - end0) * 1000, (end2 - end1) * 1000)) wfloatim = np.where(float_im > 1000, float_im, 0) #import scipy.ndimage as ndi #wfloatim= (float_im - ndi.gaussian_filter( float_im , 32)).clip(0,1e9) end0 = time.time() nw = cImageD11.localmaxlabel(wfloatim, labelm, work) end1 = time.time() rw = cImageD11.blobproperties(wfloatim, labelm, nw, 0) cImageD11.blob_moments(rw) end2 = time.time() print("lmtime", nw, "%.3f %.3f" % ((end1 - end0) * 1000, (end2 - end1) * 1000)) if 0: pks = np.array(pks).T import pylab as pl pl.imshow(pl.log(float_im), origin='lower', interpolation='nearest', aspect='auto') pl.plot(pks[1], pks[0], "r+", label="ideal") pl.plot(rc[:, cImageD11.f_raw], rc[:, cImageD11.s_raw], "wx", label="connect") pl.plot(rw[:, cImageD11.f_raw], rw[:, cImageD11.s_raw], "ko", label="watershed", markerfacecolor='None') pl.legend() pl.figure() dci = [ np.sqrt(np.min((pks[1] - x)**2 + (pks[0] - y)**2)) for x, y in zip(rc[:, cImageD11.f_raw], rc[:, cImageD11.s_raw]) ] pl.subplot(121) pl.plot(rc[:, cImageD11.s_raw], dci, '.', label="sconnected") pl.plot(rc[:, cImageD11.f_raw], dci, '.', label="fconnected") dwi = [ np.sqrt(np.min((pks[1] - x)**2 + (pks[0] - y)**2)) for x, y in zip(rw[:, cImageD11.f_raw], rw[:, cImageD11.s_raw]) ] pl.subplot(122) pl.plot(rw[:, cImageD11.s_raw], dwi, '.', label="slocalmax") pl.plot(rw[:, cImageD11.f_raw], dwi, '.', label="flocalmax") pl.legend() pl.show()
float_im=int_im.astype(np.float32) labelc = np.zeros( im.shape, np.int32 ) labelm = np.zeros( im.shape, np.int32 ) work = np.zeros( im.shape, np.int8 ) end0 = time.time() nc = cImageD11.connectedpixels( float_im, labelc, 1000 ) end1 = time.time() rc = cImageD11.blobproperties( float_im, labelc, nc, 0 ) cImageD11.blob_moments(rc) end2 = time.time() print(nc,"%.3f %.3f"%((end1-end0)*1000,(end2-end1)*1000)) wfloatim = np.where( float_im > 1000, float_im, float_im) end0 = time.time() nw = cImageD11.localmaxlabel( wfloatim, labelm, work ) end1 = time.time() rw = cImageD11.blobproperties( wfloatim, labelm, nw, 0 ) cImageD11.blob_moments(rw) end2 = time.time() print(nw,"%.3f %.3f"%((end1-end0)*1000,(end2-end1)*1000)) if 0: pks=np.array(pks).T import pylab as pl pl.imshow( pl.log(float_im), origin='lower', interpolation='nearest', aspect='auto') pl.plot( pks[1], pks[0],"+",label="ideal" ) pl.plot( rc[:,cImageD11.f_raw],rc[:,cImageD11.s_raw],"x", label="connect") pl.plot( rw[:,cImageD11.f_raw],rw[:,cImageD11.s_raw],"o", label="watershed",
[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]], np.float32) la = np.empty(im.shape, 'i') wk = np.empty(im.shape, 'b') localmaxlabel(im, la, wk, cpu) self.assertEqual(la[1, 1], 1) def testsmall(self): ''' sse / avx issue for tiny? ''' # print("testsmall, fixme, segfault/infinite loop so no CI") for cpu in [0, 1, 2]: #11,12]: im = np.array([[0, 0, 0, 0, 0], [0, 2, 0, 0, 0], [0, 1, 1, 0, 0], [0, 0, 0, 0, 0]], np.float32) la = np.empty(im.shape, 'i') wk = np.empty(im.shape, 'b') localmaxlabel(im, la, wk, cpu) self.assertEqual(la[1, 1], 1) if __name__ == "__main__": import sys if len(sys.argv) > 1: demo() unittest.main() else: im = make_test_image(256) l2 = np.zeros(im.shape, 'i') wk = np.zeros(im.shape, np.uint8) npks = localmaxlabel(im, l2, wk)