def testMethods(self): print('Methods testing') print(np.arange(6, dtype=np.int32)) print(np.arange(6, dtype=np.float)) a = np.array([4,3.]) print(type(a)) print(np.sort(a, None)) self.checkitems([3.,4], np.sort(a, None)) print(a.sort()) self.checkitems([3.,4], a) a = np.array([4,3.]) self.checkitems([1,0], a.argsort()) self.checkitems([1,0], np.argsort(a, None)) a = np.arange(6, dtype=np.float) print(a.take([0, 2, 4])) print(a.take([0, 2, 4], 0)) d = np.take(a, [0, 2, 4], 0) print(type(d), d) d = np.diag([0, 2, 3]) print(type(d), d) a.shape = 2,3 self.checkitems([1,2], a.take([1,2])) self.checkitems([[1,2], [4,5]], a.take([1,2],1)) self.checkitems([0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5], a.repeat(2)) self.checkitems([[0, 1, 2], [0, 1, 2], [3, 4, 5], [3, 4, 5]], a.repeat(2, axis=0)) self.checkitems([[0, 0, 1, 1, 2, 2], [3, 3, 4, 4, 5, 5]], a.repeat(2, axis=1))
def testSaveArgs(self): self.checkArgs(None) self.checkArgs(([None, None], [None,])) self.checkArgs(([None, 1, 1.5], [None,])) self.checkArgs(([None, 1, 1.5], (None,))) self.checkArgs(([None, 1, 1.5], dnp.arange(12))) self.checkArgs(([None, 1, 1.5], {'blah':dnp.arange(12), 'foo': dnp.ones((3,4)), 'boo': -2.345}))
def testStrAndRepr(self): print 'String and repr testing' a = np.arange(6, dtype=np.int32) print str(a), repr(a) a = np.arange(6, dtype=np.float) print str(a), repr(a) a = np.array([4,3.]) print str(a), repr(a)
def testTake(self): print 'test take' ds = np.arange(16) self.checkitems([2, 5], ds.take([2, 5])) self.checkitems([2, 5], ds.take(np.array([2, 5]))) ds = np.arange(16).reshape(4,4) self.checkitems([2, 5], ds.take([2, 5])) self.checkitems([[4, 5, 6, 7], [12, 13, 14, 15]], ds.take([1, 3], 0)) self.checkitems([[1, 3], [5, 7], [9, 11], [13, 15]], ds.take([1, 3], 1))
def testDot(self): a = np.arange(1,9).reshape(2, 2, 2) b = np.array([-2, -3, 5, 7]).reshape(2, 2) self.checkitems([35, 63], np.tensordot(a, b)) self.checkitems([63, 70], np.tensordot(b, a)) a = np.arange(60.).reshape(3,4,5) b = np.arange(24.).reshape(4,3,2) self.checkitems([[ 4400., 4730.], [ 4532., 4874.], [ 4664., 5018.], [ 4796., 5162.], [ 4928., 5306.]], np.tensordot(a,b, axes=([1,0],[0,1])))
def testArange(self): print "test arange" dds = np.arange(12, dtype=np.float) self.checkitems(range(12), dds) dds = np.arange(2,12, dtype=np.int) self.checkitems(range(2,12), dds) dds = np.arange(2,12,3) self.checkitems(range(2,12,3), dds) dds = np.arange(12,2,-3) self.checkitems(range(12,2,-3), dds)
def testDatasetSlice(self): print 'test slicing with start and stop' ds = np.arange(16) dr = ds[2:10] self.checkitems(range(2,10), dr) print 'test slicing with steps' dr = ds[::2] self.checkitems(range(0,16,2), dr) print 'test slicing 3D' dr = self.dda[1:,::2,1::2] self.checkitems(self.t, dr) print 'test putting in a 3D slice' dr = self.dda.copy() dr[:,1::2,::2] = 7. self.checkitems(self.u, dr) print 'test putting a list in a 3D slice' dr = self.dda.copy() print dr[:,1::2,::2].shape dr[:,1::2,::2] = np.array([7., 7]).reshape(2,1,1) self.checkitems(self.u, dr) print 'test slicing with ellipse' dr = ds[...] self.checkitems(range(16), dr) print 'test slicing 3D with ellipse' dr = self.dda[...,1::2] self.checkitems(self.t, dr[1:, ::2]) print 'test putting in a 3D slice with ellipsis' dr = self.dda.copy() dr[...,1::2,::2] = 7. self.checkitems(self.u, dr) print 'test putting a list in a 3D slice with ellipsis' dr = self.dda.copy() print dr[...,1::2,::2].shape dr[...,1::2,::2] = np.array([7., 7]).reshape(2,1,1) self.checkitems(self.u, dr) print 'test slice shape reduction' dr = np.arange(120).reshape(4,3,5,2) s = dr[:2,...,1:].shape print s self.assertEquals(s, (2,3,5,1)) s = dr[:2,...,1].shape print s self.assertEquals(s, (2,3,5)) s = dr[:2,...,...,1].shape print s self.assertEquals(s, (2,3,5))
def testExternal(self): from external_functions import fun, funadd, fundec, funexception a = fun() efun = dnp.external.create_function(fun, dls_module=True) print a, self.equals(efun(), a) efun = dnp.external.create_function("fun", "external_functions", dls_module=True) print a, self.equals(efun(), a) a = funadd(dnp.arange(3.), 1.5) efun = dnp.external.create_function(funadd, dls_module=True) print a, self.equals(efun(dnp.arange(3.), 1.5), a) efun = dnp.external.create_function("funadd", "external_functions", dls_module=True) print a, self.equals(efun(dnp.arange(3.), 1.5), a) a = fundec(dnp.arange(3.)) efun = dnp.external.create_function(fundec, dls_module=True) print a, self.equals(efun(dnp.arange(3.)), a) efun = dnp.external.create_function("fundec", "external_functions", dls_module=True) print a, self.equals(efun(dnp.arange(3.)), a) a = fundec(dnp.arange(3.), b=2.5) efun = dnp.external.create_function(fundec, dls_module=True) print a, self.equals(efun(dnp.arange(3.), b=2.5), a) efun = dnp.external.create_function("fundec", "external_functions", dls_module=True) print a, self.equals(efun(dnp.arange(3.), b=2.5), a)
def testHistogram(self): print 'Histogram testing' h, v = np.histogram([1, 2, 1], bins=[0, 1, 2, 3]) self.checkitems([0, 2, 1], h) self.checkitems([0, 1, 2, 3], v) h, v = np.histogram([0, 1, 2, 1], bins=2) self.checkitems([1, 3], h) self.checkitems([0, 1, 2], v) h, v = np.histogram(np.arange(4), bins=np.arange(5)) self.checkitems([1, 1, 1, 1], h) self.checkitems([0, 1, 2, 3, 4, 5], v)
def testMeshGrid(self): print 'Meshgrid testing' x = np.arange(0, 6, 1) y = np.arange(0, 4, 1) xy = np.meshgrid(x,y) self.checkitems([[0,1,2,3,4,5],[0,1,2,3,4,5],[0,1,2,3,4,5],[0,1,2,3,4,5]], xy[0]) self.checkitems([[0,0,0,0,0,0], [1,1,1,1,1,1], [2,2,2,2,2,2], [3,3,3,3,3,3]], xy[1]) xy = np.meshgrid(x,y, indexing='ij') self.checkitems([[0,1,2,3],[0,1,2,3],[0,1,2,3],[0,1,2,3],[0,1,2,3],[0,1,2,3]], xy[1]) self.checkitems([[0,0,0,0], [1,1,1,1], [2,2,2,2], [3,3,3,3], [4,4,4,4], [5,5,5,5]], xy[0])
def testPut(self): print 'test put' ds = np.arange(6.) ds.put([2, 5], [-2, -5.5]) self.checkitems([0, 1, -2, 3, 4, -5.5], ds) ds.put(np.array([0, 4]), [-2, -5.5]) self.checkitems([-2, 1, -2, 3, -5.5, -5.5], ds) ds = np.arange(6.).reshape(2,3) ds.put([2, 5], [-2, -5.5]) self.checkitems([[0, 1, -2], [3, 4, -5.5]], ds) ds.put(np.array([0, 4]), [-2, -5.5]) self.checkitems([[-2, 1, -2], [3, -5.5, -5.5]], ds)
def testSlicing(self): a = np.array([], dtype=np.float_) self.assertEquals(len(a), 0) a = np.zeros((2,)) self.assertEquals(len(a), 2) self.checkitems([0,0], a[:]) self.assertEquals(a[1], 0) a[:] = 0.5 self.checkitems([0.5,0.5], a[:]) a = np.zeros((2,3)) self.assertEquals(len(a), 2) self.checkitems([0,0,0], a[0]) a[1] = 0.2 self.checkitems([0.2,0.2,0.2], a[1]) a = np.zeros((2,3,4)) self.assertEquals(len(a), 2) a = np.arange(10).reshape((5,2)) a[3,:] = np.array([0,0]) self.checkitems([0,0], a[3]) a[3,:] = np.array([1,1]).reshape(1,2) self.checkitems([1,1], a[3]) a[:2,1] = np.array([2,2]) self.checkitems([2,2], a[:2,1]) a = np.arange(7) c = range(7) self.checkitems(c[::2], a[::2]) self.checkitems(c[0::2], a[0::2]) self.checkitems(c[3::2], a[3::2]) self.checkitems(c[6::2], a[6::2]) self.checkitems(c[6:6:2], a[6:6:2]) self.checkitems(c[7::2], a[7::2]) self.checkitems(c[-1::2], a[-1::2]) self.checkitems(c[-2::2], a[-2::2]) self.checkitems(c[::-2], a[::-2]) self.checkitems(c[0::-2], a[0::-2]) self.checkitems(c[3::-2], a[3::-2]) self.checkitems(c[6::-2], a[6::-2]) self.checkitems(c[6:6:-2], a[6:6:-2]) self.checkitems(c[7::-2], a[7::-2]) self.checkitems(c[-1::-2], a[-1::-2]) self.checkitems(c[-2::-2], a[-2::-2]) self.checkitems(a[::-2], a[:-8:-2]) a = np.arange(24).reshape(6,4) self.assertEqual((6,4), a[:].shape) self.assertEqual((6,4), a[:,].shape) self.assertEqual((6,3), a[:,:3].shape) self.assertEqual((0,3), a[4:2,:3].shape) self.assertEqual((6,), a[:,3].shape) self.assertEqual((0,), a[4:2,3].shape)
def testMethods(self): print np.arange(6, dtype=np.int32) print np.arange(6, dtype=np.float) a = np.array([4,3.]) print type(a) print np.sort(a, None) print a.sort() a = np.arange(6, dtype=np.float) print a.take([0, 2, 4], 0) d = np.take(a, [0, 2, 4], 0) print type(d), d d = np.diag([0, 2, 3]) print type(d), d
def testSums(self): print 'test sum' ds = np.arange(12).reshape((3,4)) self.assertEquals(np.sum(ds), 66) self.checkitems([12, 15, 18, 21], np.sum(ds, 0)) self.checkitems([ 6, 22, 38], np.sum(ds, 1)) lds = np.arange(1024*1024, dtype=np.int32) self.assertEquals(np.sum(lds, dtype=np.int32), -524288) self.assertEquals(np.sum(lds, dtype=np.int64), 549755289600) print 'test cumsum' self.checkitems([0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66], np.cumsum(ds)) self.checkitems([[0, 1, 2, 3], [4, 6, 8, 10], [12, 15, 18, 21]], np.cumsum(ds, 0)) self.checkitems([[0, 1, 3, 6], [4, 9, 15, 22], [8, 17, 27, 38]], np.cumsum(ds, 1))
def testGDA2270(self): print 'test negative indices' ds = np.arange(10.) self.assertEquals(8, ds[-2]) ds[-2] = 0 self.assertEquals(0, ds[8]) ds = np.arange(10.).reshape(2,5) du = np.arange(5.,10) dt = ds[-1] for i in range(du.shape[0]): self.assertEquals(dt[i], du[i]) ds[-1] = -2 for i in range(ds.shape[1]): self.assertEquals(ds[-1,i], -2)
def testAtleast(self): print 'Atleast testing' self.checkitems([1.], np.atleast_1d(1.)) self.checkitems([[1.]], np.atleast_2d(1.)) self.checkitems([[[1.]]], np.atleast_3d(1.)) a = np.atleast_1d(1., [2, 3]) self.checkitems([1.], a[0]) self.checkitems([2,3], a[1]) a = np.atleast_2d(np.arange(2)) self.checkitems([[0, 1]], a) a = np.atleast_3d(np.arange(2)) self.checkitems([[[0], [1]]], a) a = np.atleast_3d(np.arange(6).reshape(3,2)) self.checkitems([[[0], [1]], [[2], [3]], [[4], [5]]], a)
def testBitwise(self): print 'Bitwise testing' a = np.arange(-4,4, dtype=np.int8) b = np.arange(8, dtype=np.int8) self.checkitems([0, 1, 2, 3, 0, 1, 2, 3], np.bitwise_and(a, b)) self.checkitems([-4, -3, -2, -1, 4, 5, 6, 7], np.bitwise_or(a, b)) self.checkitems([-4, -4, -4, -4, 4, 4, 4, 4], np.bitwise_xor(a, b)) self.checkitems([3, 2, 1, 0, -1, -2, -3, -4], np.invert(a)) self.checkitems([-1, -2, -3, -4, -5, -6, -7, -8], np.invert(b)) self.checkitems([-4, -6, -8, -8, 0, 32, -128, -128], np.left_shift(a, b)) self.checkitems([0, 0, 0, 0, 4, 10, 24, 56], np.left_shift(b, a)) self.checkitems([0, 0, 0, 0, 0, 2, 8, 24], np.left_shift(a, a)) self.checkitems([-4, -2, -1, -1, 0, 0, 0, 0], np.right_shift(a, b)) self.checkitems([0, 0, 0, 0, 4, 2, 1, 0], np.right_shift(b, a)) self.checkitems([-1, -1, -1, -1, 0, 0, 0, 0], np.right_shift(a, a))
def testHistogram(self): print('Histogram testing') h, v = np.histogram([1, 2, 1], bins=[0, 1, 2, 3]) self.checkitems([0, 2, 1], h) self.checkitems([0, 1, 2, 3], v) h, v = np.histogram([0, 1, 2, 1], bins=2) self.checkitems([1, 3], h) self.checkitems([0, 1, 2], v) h, v = np.histogram(np.arange(4), bins=4, range=[0,4]) self.checkitems([1, 1, 1, 1], h) self.checkitems([0, 1, 2, 3, 4], v) h, v = np.histogram(np.arange(4), bins=4, range=[0,4], weights=np.full(4, 0.25)) self.checkitems([0.25, 0.25, 0.25, 0.25], h) self.checkitems([0, 1, 2, 3, 4], v)
def testGradient(self): print "Gradient testing" x = np.array([1, 2, 4, 7, 11, 16], dtype=np.float) g = np.gradient(x) self.checkitems([1.0, 1.5, 2.5, 3.5, 4.5, 5.0], g) g = np.gradient(x, 2) self.checkitems([0.5, 0.75, 1.25, 1.75, 2.25, 2.5], g) a = np.arange(6, dtype=np.float) * 2 g = np.gradient(x, a) self.checkitems([0.5, 0.75, 1.25, 1.75, 2.25, 2.5], g) g = np.gradient(np.array([[1, 2, 6], [3, 4, 5]], dtype=np.float)) self.checkitems([[2.0, 2.0, -1.0], [2.0, 2.0, -1.0]], g[0]) self.checkitems([[1.0, 2.5, 4.0], [1.0, 1.0, 1.0]], g[1]) g = np.gradient(np.array([[1, 2, 6], [3, 4, 5]], dtype=np.float), 2) self.checkitems([[1.0, 1.0, -0.5], [1.0, 1.0, -0.5]], g[0]) self.checkitems([[0.5, 1.25, 2.0], [0.5, 0.5, 0.5]], g[1]) g = np.gradient(np.array([[1, 2, 6], [3, 4, 5]], dtype=np.float), 2, 1) self.checkitems([[1.0, 1.0, -0.5], [1.0, 1.0, -0.5]], g[0]) self.checkitems([[1.0, 2.5, 4.0], [1.0, 1.0, 1.0]], g[1]) g = np.gradient(np.array([[1, 2, 6], [3, 4, 5]], dtype=np.float), 1, np.array([1.0, 2.0, 5.0])) self.checkitems([[2.0, 2.0, -1.0], [2.0, 2.0, -1.0]], g[0]) self.checkitems([[1.0, 1.25, 4.0 / 3], [1.0, 0.5, 1.0 / 3]], g[1]) # test slice views x = np.array([1, 2, 4, 7, 11, 16], dtype=np.float) g = np.gradient(x[2:]) self.checkitems([3, 3.5, 4.5, 5.0], g)
def testSqueeze(self): print('Squeeze testing') a = np.arange(10.).reshape(2,5) self.assertTrue(a is a.squeeze()) a.shape = 2,1,5 self.assertFalse(a is a.squeeze()) self.assertEqual((2,5), a.squeeze().shape)
def testGradient(self): print "Gradient testing" z = np.arange(200.0) dz = np.gradient(z) self.assertEquals(1, len(dz.shape)) self.assertEquals(200, dz.size) self.assertEquals(1, dz[0])
def testSaving(self): d = dnp.arange(100).reshape(10,10) % 3 self.save("chequered.png", d) im = self.load("chequered.png", testfolder=OutTestFolder) im = self.load("test.png") self.save("grey.png", im) im2 = self.load("grey.png", testfolder=OutTestFolder)
def load_data_element(self, element_number): identifier = "/entry1/EDXD_Element_%02i/data" % element_number element = self.data[identifier][:] # energy = self.data.getLazyDataset("/entry1/EDXD_Element_%02i/edxd_energy_approx" % element_number).getSlice(None,None) counts = element.sum(0) energy = dnp.arange(counts.shape[0]) return (counts, energy)
def testStats(self): print('test stats') a = np.arange(12.).reshape(4,3) self.assertEquals(11, a.argmax()) self.assertEquals(0, a.argmin()) self.assertEquals(11., a.max()) self.assertEquals(0., a.min()) self.assertEquals(5.5, a.mean()) self.assertAlmostEquals(11.9166666667, a.var()) self.assertAlmostEquals(3.45205252953, a.std()) self.assertAlmostEquals(66., a.sum()) self.assertAlmostEquals(0., a.prod()) self.checkitems([2, 2, 2, 2], a.argmax(1)) self.checkitems([0, 0, 0, 0], a.argmin(1)) self.checkitems([2.,5.,8.,11.], a.max(1)) self.checkitems([2.,5.,8.,11.], a.max(-1)) self.checkitems([0., 3., 6., 9.], a.min(1)) self.checkitems([0., 3., 6., 9.], a.min(-1)) self.checkitems([1., 4., 7., 10.], a.mean(1)) self.checkitems([1., 4., 7., 10.], a.mean(-1)) b = 2./3 self.checkitems([b, b, b, b], a.var(1)) self.checkitems([b, b, b, b], a.var(-1)) self.checkitems([1., 1., 1., 1.], a.var(1, ddof=1)) self.checkitems([1., 1., 1., 1.], a.var(-1, ddof=1)) b = np.sqrt(b) self.checkitems([b, b, b, b], a.std(1)) self.checkitems([b, b, b, b], a.std(-1)) self.checkitems([1., 1., 1., 1.], a.std(1, ddof=1)) self.checkitems([1., 1., 1., 1.], a.std(-1, ddof=1)) self.checkitems([3., 12., 21., 30.], a.sum(1)) self.checkitems([3., 12., 21., 30.], a.sum(-1)) self.checkitems([0., 60., 336., 990.], a.prod(1)) self.checkitems([0., 60., 336., 990.], a.prod(-1))
def testScisoft(self): a = np.ones([3,4]) print a.shape a.shape = [2,6] print a.shape a.shape = 12 print a.shape a.shape = (2,6) print a.shape print a print a*2 b = np.arange(12) print b print b[0] b[2] = 2.3 print b[1:8:3] b[6:2:-1] = -2.1 b.shape = [2,6] print b + 2 print 2 + b b += 2. print b[1,3] b[0,5] = -2.3 print b[0,2:5] b[:,1] = -7.1 print b try: c = np.add(a, b) print c except: print "Failed with an IAE as expected"
def testScisoft(self): a = np.ones([3,4]) print(a.shape) a.shape = [2,6] print(a.shape) a.shape = 12 print(a.shape) a.shape = (2,6) print(a.shape) print(a) print(a*2) b = np.arange(12) print(b) print(b[0]) b[2] = 2.3 print(b[1:8:3]) b[6:2:-1] = -2.1 b.shape = [2,6] print(b + 2) print(2 + b) b += 2 print(b[1,3]) b[0,5] = -2.3 print(b[0,2:5]) b[:,1] = -7.1 print(b) try: c = np.add(a, b) print(c) except: print("Failed with an IAE as expected")
def testMean(self): print('test mean') a = np.arange(10).reshape(2,5) self.assertEqual(4.5, a.mean()) self.checkitems([2.5, 3.5, 4.5, 5.5, 6.5], a.mean(0)) self.checkitems([2., 7.], a.mean(1)) self.checkitems([2., 7.], a.mean(-1))
def testMaxMin(self): print 'test max/min' a = np.arange(10, dtype=np.float).reshape(2,5) self.assertEqual(0., a.min()) self.checkitems([0., 1., 2., 3., 4.], a.min(0)) self.checkitems([0., 5.], a.min(1)) self.assertEqual(0, a.argmin()) self.checkitems([0, 0, 0, 0, 0], a.argmin(0)) self.checkitems([0, 0], a.argmin(1)) if isjava: self.checkitems([0., 1., 2., 3., 4.], a.min(0, True)) self.checkitems([0., 5.], a.min(1, True)) self.checkitems([0, 0, 0, 0, 0], a.argmin(0, True)) self.checkitems([0, 0], a.argmin(1, True)) self.assertEqual(9., a.max()) self.checkitems([5., 6., 7., 8., 9.], a.max(0)) self.checkitems([4., 9.], a.max(1)) self.assertEqual(9, a.argmax()) self.checkitems([1, 1, 1, 1, 1], a.argmax(0)) self.checkitems([4, 4], a.argmax(1)) if isjava: self.checkitems([5., 6., 7., 8., 9.], a.max(0, True)) self.checkitems([4., 9.], a.max(1, True)) self.checkitems([1, 1, 1, 1, 1], a.argmax(0, True)) self.checkitems([4, 4], a.argmax(1, True))
def testDiv(self): print("test div") da = np.array(self.db, np.int) dr = da // (da+1) self.checkitemsdiv(self.db, self.db, dr, convert=toInt) dr = da.copy() dr //= da + 1 self.checkitemsdiv(self.db, self.db, dr, convert=toInt) dr = da / 1.2 self.checkitemsdivconst(self.db, 1.2, dr, convert=toInt) # dr = da.copy() # dr /= 1.2 # self.checkitemsdivconst2(self.db, 1.2, dr, convert=toInt) da = np.array(self.db, np.float) dr = da / (da+1) self.checkitemsdiv(self.db, self.db, dr) dr = da.copy() dr /= da + 1 self.checkitemsdiv(self.db, self.db, dr) dr = da / 1.2 self.checkitemsdivconst(self.db, 1.2, dr) dr = 1.2 / (da+1) self.checkitemsdivconst(1.2, self.db, dr) dr = da.copy() dr /= 1.2 self.checkitemsdivconst(self.db, 1.2, dr) za = np.array(self.zb, np.complex) zr = za / (za + 1) self.checkitemsdiv(self.zb, self.zb, zr, iscomplex=True) zr = za.copy() zr /= za + 1 self.checkitemsdiv(self.zb, self.zb, zr, iscomplex=True) zr = za / (1.3 + 0.2j) self.checkitemsdivconst(self.zb, (1.3 + 0.2j), zr, iscomplex=True) zr = za.copy() zr /= (1.3 + 0.2j) self.checkitemsdivconst(self.zb, (1.3 + 0.2j), zr, iscomplex=True) a = np.arange(-4, 4, dtype=np.int8) import sys py3 = sys.hexversion >= 0x03000000 if py3: self.checkitems([-2, -1.5, -1, -0.5, 0, 0.5, 1, 1.5], a / 2) self.checkitems([2, 1.5, 1, 0.5, 0, -0.5, -1, -1.5], a / -2) else: self.checkitems([-2, -2, -1, -1, 0, 0, 1, 1], a / 2) self.checkitems([2, 1, 1, 0, 0, -1, -1, -2], a / -2) self.checkitems([-2, -2, -1, -1, 0, 0, 1, 1], a // 2) self.checkitems([0, 1, 0, 1, 0, 1, 0, 1], a % 2) self.checkitems([2, 1, 1, 0, 0, -1, -1, -2], a // -2) self.checkitems([0, -1, 0, -1, 0, -1, 0, -1], a % -2) self.checkitems([-1.6, -1.2, -0.8, -0.4, 0. , 0.4, 0.8, 1.2], a / 2.5) self.checkitems([-2., -2., -1., -1., 0., 0., 0., 1.], a // 2.5) self.checkitems([1., 2., 0.5, 1.5, 0., 1., 2., 0.5], a % 2.5) self.checkitems([1.6, 1.2, 0.8, 0.4, -0., -0.4, -0.8, -1.2], a / -2.5) self.checkitems([1., 1., 0., 0., -0., -1., -1., -2.], a // -2.5) self.checkitems([-1.5, -0.5, -2., -1., 0., -1.5, -0.5, -2.], a % -2.5)
def testCentroid(self): print 'Centroid testing' a = np.arange(12.) x = np.arange(12.) + 2 ca = np.centroid(a) self.assertEquals(ca[0], 539./66) ca = np.centroid(a, x) self.assertEquals(ca[0], 539./66 + 1.5) a.shape = (3,4) ca = np.centroid(a) self.assertEquals(ca[0], 131./66) self.assertEquals(ca[1], 147./66) x = np.arange(3.) + 2 y = np.arange(4.) + 3 ca = np.centroid(a, [x,y]) self.assertEquals(ca[0], 131./66 + 1.5) self.assertEquals(ca[1], 312./66) #147./66 + 2.5)
def plotLine(): dnp.plot.setdefname('Plot 1') x = dnp.arange(10) y = dnp.arange(10) dnp.plot.line(x, y)
import math import scisoftpy as dnp #To create a mesh grid #def grid(xList, yList): xList = range(5) yList = range(3) x = dnp.arange(len(xList) * len(yList)) x = x.reshape([len(xList), len(yList)])
def sphere_alignment(threshold=0.55, x_stage=ss1_x, flat_field_position=-50, projection_position=2.0, theta_stage=ss1_theta, exposure_time=0.06, max_tries=15, z_tilt_stage=ss1_rz, x_tilt_stage=ss1_rx, doscan=True, working_directory='/dls/i12/data/2014/cm4963-2/rawdata', flatfield_file=36466, projection_file=36467): print "Running sphere alignment" # scan flat field # Run the scan, then call sphere_alignmet_processing(flatfilename, projectionfilename) if (doscan == True): print " Moving to flatfield position" # this should be an optional step as it may not be possible to do this move without disrupting user equipment #pos ss1.x -200 pos(x_stage, flat_field_position) print "Collecting flat field image" scan(ix, 1, 1, 1, pco4000_dio_hdf, exposure_time) time.sleep(5) working_dir = wd() current_file = cfn() filename = '%s/%d.nxs' % (working_dir, current_file) print "Loading flat field image to data processing pipeline" ff = None try_number = 0 while (ff == None and try_number < max_tries): try_number += 1 try: data = dnp.io.load(filename) ff = data['entry1']['pco4000_dio_hdf']['data'][...].mean(0) except: print("Failed to load %i, will try again in 5 seconds" % (try_number)) time.sleep(5) dnp.plot.image(ff) print "Moving to sample position" pos(x_stage, projection_position) print "Collecting projections" scan(theta_stage, 0, 360, 20, pco4000_dio_hdf, exposure_time) working_dir = wd() current_file = cfn() filename = '%s/%d.nxs' % (working_dir, current_file) time.sleep(5) print "Loading projections to data processing pipeline" #data = dnp.io.load(filename) dd = None try_number = 0 while (dd == None and try_number < max_tries): try_number += 1 try: data = dnp.io.load(filename) dd = data['entry1']['pco4000_dio_hdf']['data'] except: print("Failed to load %i, will try again in 5 seconds" % (try_number)) time.sleep(5) else: print "Not Moving to flatfield position" # this should be an optional step as it may not be possible to do this move without disrupting user equipment print "Not Collecting flat field image" time.sleep(5) working_dir = working_directory current_file = flatfield_file filename = '%s/%d.nxs' % (working_dir, current_file) print "Loading flat field image to data processing pipeline" ff = None try_number = 0 while (ff == None and try_number < max_tries): try_number += 1 try: data = dnp.io.load(filename) ff = data['entry1']['pco4000_dio_hdf']['data'][...].mean(0) except: print("Failed to load %i, will try again in 2 seconds" % (try_number)) time.sleep(2) dnp.plot.image(ff) print "Not Moving to sample position" print "Not Collecting projections" working_dir = working_directory current_file = projection_file filename = '%s/%d.nxs' % (working_dir, current_file) time.sleep(5) print "Loading projections into data processing pipeline" data = dnp.io.load(filename) dd = data['entry1']['pco4000_dio_hdf']['data'] print "Processing data" # apply any filtering # exclude spheres which are not completely in the field of view print "slow look at all the data" preview = dd[:, :, :].mean(0) * 1.0 / ff dnp.plot.image(preview[10:-1, :]) time.sleep(2) print "Generating centroid data" thresholdOK = False xs = [] ys = [] while not thresholdOK: xs = [] ys = [] for i in range(dd.shape[0]): cor = dd[i, :, :] * 1.0 / ff cor = cor[10:-1, :] < threshold dnp.plot.clear() #time.sleep(0.1) dnp.plot.image(cor) #time.sleep(.1) x, y = dnp.centroid(cor) xs.append(x) ys.append(y) xxx = dnp.array(ys) yyy = dnp.array(xs) responce = requestInput( "Current threshold is %f, y for ok, otherwise enter a new threshold value " % (threshold)) if responce == 'y': thresholdOK = True else: try: threshold = float(responce) except: print "could not interpret %s as a float" % responce print "Fitting ellipse to centroid data" ellipseFitValues = dnp.fit.ellipsefit(xxx, yyy) t = dnp.arange(100) * dnp.pi / 50. fitplot = dnp.fit.makeellipse(ellipseFitValues, t._jdataset()) dnp.plot.line(fitplot[0], fitplot[1]) time.sleep(1) dnp.plot.addline(xxx, yyy) #print("XXX is:") #print (type(xxx)) #print(xxx) print("ys is:") type(xs) print(xs) print "Results of ellipse fitting routine:" print " Major: %f" % (ellipseFitValues[0]) print " Minor semi-axis: %f" % (ellipseFitValues[1]) print " Major axis angle: %f" % (ellipseFitValues[2]) print " Centre co-ord 1: %f" % (ellipseFitValues[3]) print " Centre co-ord 2: %f" % (ellipseFitValues[4]) print "Calculating stage tilts" xangle = math.fabs(math.atan2(ellipseFitValues[1], ellipseFitValues[0])) #find out which direction npoints = len(xs) firstquarter = int(math.floor(npoints / 4.0)) lastquarter = int(math.floor(3.0 * npoints / 4.0)) print(" firstquarter %i %f lastquarter %i %f" % (firstquarter, xs[firstquarter], lastquarter, xs[lastquarter])) if (xs[firstquarter] > xs[lastquarter]): xangle = -math.degrees(xangle) print(" negative xangle %f" % xangle) else: xangle = math.degrees(xangle) print(" positive xangle %f" % xangle) #xangle = math.degrees(xangle) * -1.0 # This needs to be calculated correctly zangle = math.degrees(ellipseFitValues[2]) if zangle > 90.0: zangle -= 180.0 print xangle print zangle responce = requestInput("Would you like to rotate the X tilt stage by %f" % (xangle)) if responce == 'y': print "Moving x tilt stage" pos(x_tilt_stage) inc(x_tilt_stage, xangle) pos(x_tilt_stage) print "In position." responce = requestInput("Would you like to rotate the Z tilt stage by %f" % (zangle)) if responce == 'y': print "Moving z tilt stage" pos(z_tilt_stage) inc(z_tilt_stage, zangle) pos(z_tilt_stage) print "In position." print "sphere_alignment finished"
def testUnpack(self): print 'Unpacking testing' print tuple(np.arange(6)) print tuple(np.arange(6).reshape(2, 3)) print tuple(np.arange(6).reshape(3, 2))
def testReshape(self): print 'Reshape testing' a = np.arange(10.) a.reshape(2, 5) a.reshape((2, 5))
def testCompounds(self): a = np.arange(24).reshape(3, 4, 2) oa = np.compoundarray(a) ca = oa.copy() self.assertEqual(ca.shape[0], 3) self.assertEqual(ca.shape[1], 4) la = ca[1, 2] print(la) self.assertEqual(la[0], 12) self.assertEqual(la[1], 13) ca[2, 2] = (0, 0) self.assertEqual(ca[2, 2][0], 0) self.assertEqual(ca[2, 2][1], 0) ca[2, 2] = (-2, 1) self.assertEqual(ca[2, 2][0], -2) self.assertEqual(ca[2, 2][1], 1) ca[1:, 3:] = (-1, -1) self.assertEqual(ca[2, 3][0], -1) self.assertEqual(ca[2, 3][1], -1) ca[1:, 3:] = (3, -4) self.assertEqual(ca[2, 3][0], 3) self.assertEqual(ca[2, 3][1], -4) ia = np.array([2, -3]) print('Integer index testing') ca = oa.copy() print(ca) cb = ca[ia] print(cb) self.assertEqual(cb.shape[0], 2) self.assertEqual(cb[0, 0][0], 16) self.assertEqual(cb[0, 0][1], 17) self.assertEqual(cb[1, 0][0], 0) self.assertEqual(cb[1, 0][1], 1) print(np.compoundarray(np.array([1, 2]))) ca[ia] = np.compoundarray(np.array([1, 2])) self.assertEqual(ca[0, 2][0], 1) self.assertEqual(ca[0, 2][1], 2) self.assertEqual(ca[1, 1][0], 10) self.assertEqual(ca[1, 1][1], 11) ca[ia] = (1, 2) # this works too print('Boolean index testing') ba = np.array([[0, 0, 1, 0], [1, 0, 0, 0], [0, 1, 0, 1]], dtype=np.bool) ca = oa.copy() cc = ca[ba] # index dataset does not work # test boolean too print(cc) self.assertEqual(cc.shape[0], 4) self.assertEqual(cc[0][0], 4) self.assertEqual(cc[0][1], 5) self.assertEqual(cc[1][0], 8) self.assertEqual(cc[1][1], 9) self.assertEqual(cc[2][0], 18) self.assertEqual(cc[2][1], 19) self.assertEqual(cc[3][0], 22) self.assertEqual(cc[3][1], 23) ca[ba] = (1, 2) self.assertEqual(ca[0, 2][0], 1) self.assertEqual(ca[0, 2][1], 2) self.assertEqual(ca[1, 0][0], 1) self.assertEqual(ca[1, 0][1], 2) self.assertEqual(ca[2, 1][0], 1) self.assertEqual(ca[2, 1][1], 2) self.assertEqual(ca[2, 3][0], 1) self.assertEqual(ca[2, 3][1], 2)
def testArray(self): # make new datasets print "test array" dds = np.array(self.dz, np.float) self.assertEquals(self.dz, dds) self.assertEquals(dds, self.dz) self.assertEquals(dds.item(), self.dz) if isjava: self.assertEquals(1, dds.dtype.elements) self.assertEquals(8, dds.itemsize) dds = np.array(self.da, np.float) self.checkitems(self.da, dds) dds = np.array(self.db, np.float) self.checkitems(self.db, dds) dds = np.array(self.dc, np.float) self.checkitems(self.dc, dds) mds = np.array(self.m) self.checkitems(self.db, mds) zds = np.array(self.zz, np.complex) self.assertEquals(self.zz, zds) self.assertEquals(zds, self.zz) self.assertEquals(zds.item(), self.zz) self.assertEquals(zds.real.item(), 1.) self.assertEquals(zds.imag.item(), .5) self.assertEquals(np.real(zds), 1.) self.assertEquals(np.imag(zds), .5) zds = np.array([self.zz, self.zz], np.complex) self.assertEquals(self.zz, zds[0]) self.assertRaises(ValueError, zds.item) if isjava: self.assertEquals(1, zds.dtype.elements) self.assertEquals(16, zds.itemsize) self.checkitems([1., 1.], zds.real) self.checkitems([.5, .5], zds.imag) self.checkitems([1., 1.], np.real(zds)) self.checkitems([.5, .5], np.imag(zds)) a = np.array([1 + 2j, 3 + 4j, 5 + 6j]) a.real = np.array([2, 4, 6]) self.checkitems([2, 4, 6], np.real(a)) a.imag = np.array([8, 10, 12]) self.checkitems([8, 10, 12], np.imag(a)) zds = np.array(self.za, np.complex) self.checkitems(self.za, zds) zds = np.array(self.zb, np.complex) self.checkitems(self.zb, zds) zds = np.array(self.zc, np.complex) self.checkitems(self.zc, zds) ids = np.array(self.s, np.int) self.checkitems(self.s, ids) ids = np.array(self.a, np.int) self.checkitems(self.a, ids) ids = np.array(self.s, np.int8) self.checkitems(self.s, ids) ids = np.array(self.a, np.int8) self.checkitems(self.a, ids) ids = np.array(self.s, np.int16) self.checkitems(self.s, ids) ids = np.array(self.a, np.int16) self.checkitems(self.a, ids) ids = np.array(self.s, np.int64) self.checkitems(self.s, ids) ids = np.array(self.a, np.int64) self.checkitems(self.a, ids) tds = np.array(self.a) self.assertEquals(tds.dtype, np.int_) tds = np.array(self.m) self.assertEquals(tds.dtype, np.float64) tds = np.array(self.q) self.assertEquals(tds.dtype, np.float64) tds = np.array(self.r) self.assertEquals(tds.dtype, np.float64) tds = np.array(self.s) self.assertEquals(tds.dtype, np.int_) tds = np.array(self.t) self.assertEquals(tds.dtype, np.float64) tds = np.array(self.u) self.assertEquals(tds.dtype, np.float64) tds = np.array([np.arange(5), np.arange(5) + 5]) self.checkitems(np.arange(10).reshape(2, 5), tds)
def testDatasetSlice(self): print 'test slicing with start and stop' ds = np.arange(16) dr = ds[2:10] self.checkitems(range(2, 10), dr) print 'test slicing with steps' dr = ds[::2] self.checkitems(range(0, 16, 2), dr) print 'test slicing 3D' dr = self.dda[1:, ::2, 1::2] self.checkitems(self.t, dr) print 'test putting in a 3D slice' dr = self.dda.copy() dr[:, 1::2, ::2] = 7. self.checkitems(self.u, dr) print 'test putting a list in a 3D slice' dr = self.dda.copy() print dr[:, 1::2, ::2].shape dr[:, 1::2, ::2] = np.array([7., 7]).reshape(2, 1, 1) self.checkitems(self.u, dr) print 'test slicing with ellipse' dr = ds[...] self.checkitems(range(16), dr) print 'test slicing 3D with ellipse' dr = self.dda[..., 1::2] self.checkitems(self.t, dr[1:, ::2]) print 'test putting in a 3D slice with ellipsis' dr = self.dda.copy() dr[..., 1::2, ::2] = 7. self.checkitems(self.u, dr) print 'test putting a list in a 3D slice with ellipsis' dr = self.dda.copy() print dr[..., 1::2, ::2].shape dr[..., 1::2, ::2] = np.array([7., 7]).reshape(2, 1, 1) self.checkitems(self.u, dr) print 'test slice shape reduction' dr = np.arange(120).reshape(4, 3, 5, 2) s = dr[:2, ..., 1:].shape print s self.assertEquals(s, (2, 3, 5, 1)) s = dr[:2, ..., 1].shape print s self.assertEquals(s, (2, 3, 5)) try: s = dr[:2, ..., ..., 1].shape raise AssertionError, 'Should have raised an error' except: pass print 'test newaxis' dr = np.arange(15).reshape(3, 5) s = dr[np.newaxis, :, 1:].shape print s self.assertEquals(s, (1, 3, 4)) s = dr[np.newaxis, ...].shape print s self.assertEquals(s, (1, 3, 5)) s = dr[np.newaxis, ..., np.newaxis].shape print s self.assertEquals(s, (1, 3, 5, 1)) s = dr[..., np.newaxis].shape print s self.assertEquals(s, (3, 5, 1)) s = dr[:, np.newaxis, ..., np.newaxis].shape print s self.assertEquals(s, (3, 1, 5, 1)) a = np.zeros((5, 4)) b = np.arange(5.) a[:, 2] = b self.checkitems(b, a[:, 2]) a.fill(0) try: b.shape = (5, 1) a[:, 2] = b raise AssertionError, 'Should have raised an error' except: pass try: b.shape = (1, 5, 1) a[:, 2] = b raise AssertionError, 'Should have raised an error' except: pass
def testFFT1(self): az = [ 66. + 0.j, -6. + 22.39230485j, -6. + 10.39230485j, -6. + 6.j, -6. + 3.46410162j, -6. + 1.60769515j, -6. + 0.j, -6. - 1.60769515j, -6. - 3.46410162j, -6. - 6.j, -6. - 10.39230485j, -6. - 22.39230485j ] dsa = np.arange(12.) ada = fft.fft(dsa) self.checkcomplexitems(az, ada) ida = fft.ifft(ada) self.checkrealitems(dsa, ida) dsa = np.arange(12, dtype=np.float32) ada = fft.fft(dsa) self.checkcomplexitems(az, ada, places=5) ida = fft.ifft(ada) self.checkrealitems(dsa, ida, places=5) dsa = np.arange(12.) dsa.shape = (3, 4) bz = [[6. + 0.j, -2. + 2.j, -2. + 0.j, -2. - 2.j], [22. + 0.j, -2. + 2.j, -2. + 0.j, -2. - 2.j], [38. + 0.j, -2. + 2.j, -2. + 0.j, -2. - 2.j]] ada = fft.fft(dsa) self.checkcomplexitems(bz, ada) ida = fft.ifft(ada) self.checkrealitems(dsa, ida) dsa = np.arange(12, dtype=np.float32) dsa.shape = (3, 4) ada = fft.fft(dsa) self.checkcomplexitems(bz, ada, places=5) ida = fft.ifft(ada) self.checkrealitems(dsa, ida, places=5) dsa = np.arange(12.) dsa.shape = (3, 4) cz = [[12. + 0.j, 15. + 0.j, 18. + 0.j, 21. + 0.j], [ -6. + 3.46410162j, -6. + 3.46410162j, -6. + 3.46410162j, -6. + 3.46410162j ], [ -6. - 3.46410162j, -6. - 3.46410162j, -6. - 3.46410162j, -6. - 3.46410162j ]] ada = fft.fft(dsa, axis=0) self.checkcomplexitems(cz, ada) ida = fft.ifft(ada, axis=0) self.checkrealitems(dsa, ida) dsa = np.arange(12, dtype=np.float32) dsa.shape = (3, 4) ada = fft.fft(dsa, axis=0) self.checkcomplexitems(cz, ada, places=5) ida = fft.ifft(ada, axis=0) self.checkrealitems(dsa, ida, places=5) dsa = np.arange(12.) dsa.shape = (3, 4) dz = [[ 6.00000000 + 0.j, -3.73606798 - 0.36327126j, 0.73606798 - 1.53884177j, 0.73606798 + 1.53884177j, -3.73606798 + 0.36327126j ], [ 22.00000000 + 0.j, -4.97213595 - 4.16749733j, 3.97213595 - 3.88998278j, 3.97213595 + 3.88998278j, -4.97213595 + 4.16749733j ], [ 38.00000000 + 0.j, -6.20820393 - 7.97172339j, 7.20820393 - 6.24112379j, 7.20820393 + 6.24112379j, -6.20820393 + 7.97172339j ]] ada = fft.fft(dsa, n=5) self.checkcomplexitems(dz, ada) ida = fft.ifft(ada, n=5) self.checkrealitems(dsa, ida[:, :4]) dsa = np.arange(12, dtype=np.float32) dsa.shape = (3, 4) ada = fft.fft(dsa, n=5) self.checkcomplexitems(dz, ada, places=5) ida = fft.ifft(ada, n=5) self.checkrealitems(dsa, ida[:, :4], places=5) dsa = np.arange(12, dtype=np.complex)
from uk.ac.diamond.scisoft.analysis.fitting.functions import FermiGauss import scisoftpy as dnp fg = FermiGauss() x = dnp.arange(0, 0.2, 0.001) def scan_temps(start, stop, step, fwhm): fg.setParameterValues(0.1, start, 0, 1, 0, fwhm) test = fg.calculateValues([x]) temps = dnp.arange(start, stop, step) result = dnp.zeros(temps.shape) count = 0 for temp in temps: fg.setParameterValues(0.1, temp, 0, 1, 0, 0.0) comp = fg.calculateValues([x]) result[count] = dnp.sum(dnp.square(dnp.array(test) - dnp.array(comp))) count += 1 dnp.plot.line(temps, result) return temps[result.minPos().tolist()] def scan_fwhm(start, stop, step, t_start, t_stop=500, t_step=1): fwhms = dnp.arange(start, stop, step) result = dnp.zeros(fwhms.shape) count = 0 for fwhm in fwhms: result[count] = scan_temps(t_start, t_stop, t_step, fwhm) count += 1 dnp.plot.line(result, fwhms, name="Plot 2") return (fwhms, result)
''' Created on 24 Sep 2013 @author: vdp96513 ''' import math import scisoftpy as dnp #dnp.plot.clear() im = dnp.arange(100*100.).reshape(100,100) % 7 dnp.plot.image(im) rl = dnp.plot.roi.line_list() n = 2 da = math.pi/n for a in range(n): l = dnp.plot.roi.line(length = 50, angle = a*da) l.point = 50.,50. l.name = 'Line %d' % a rl.add(l) bean = dnp.plot.getbean() dnp.plot.setrois(bean, rl) dnp.plot.setbean(bean)
def peakdet(v, delta, x = None): """ Converted from MATLAB script at http://billauer.co.il/peakdet.html Currently returns two lists of tuples, but maybe arrays would be better function [maxtab, mintab]=peakdet(v, delta, x) %PEAKDET Detect peaks in a vector % [MAXTAB, MINTAB] = PEAKDET(V, DELTA) finds the local % maxima and minima ("peaks") in the vector V. % MAXTAB and MINTAB consists of two columns. Column 1 % contains indices in V, and column 2 the found values. % % With [MAXTAB, MINTAB] = PEAKDET(V, DELTA, X) the indices % in MAXTAB and MINTAB are replaced with the corresponding % X-values. % % A point is considered a maximum peak if it has the maximal % value, and was preceded (to the left) by a value lower by % DELTA. % Eli Billauer, 3.4.05 (Explicitly not copyrighted). % This function is released to the public domain; Any use is allowed. """ maxtab = [] mintab = [] if x is None: x = dnp.arange(len(v)) v = dnp.asarray(v) if len(v) != len(x): sys.exit('Input vectors v and x must have same length') if not isinstance(delta, (type(None),int,float,str,bool)): sys.exit('Input argument delta must be a number') if delta <= 0: sys.exit('Input argument delta must be positive') mn, mx = float('inf'), float('-inf') mnpos, mxpos = float('NaN'), float('NaN') lookformax = True for i in dnp.arange(len(v)): this = v[i] if this > mx: mx = this mxpos = x[i] if this < mn: mn = this mnpos = x[i] if lookformax: if this < mx-delta: maxtab.append((mxpos, mx)) mn = this mnpos = x[i] lookformax = False else: if this > mn+delta: mintab.append((mnpos, mn)) mx = this mxpos = x[i] lookformax = True return maxtab, mintab
res = res > 0.5 dnp.plot.image(res) print "Calculting coms" points = [] for i in range(res.shape[1]): a = res[:,i:i+1].mean(1) points.append(dnp.centroid(a)) coms = dnp.array(points) coms.squeeze() dnp.plot.line(coms) p = dnp.fit.polyfit(dnp.arange(coms.shape[0]), coms, 1, full=True) p[1].plot() rotation = math.degrees(math.atan2(p[0][0],1.)) responce = requestInput("Would you like to rotate the camera stage by %f" % (rotation)) if responce == 'y' : print "Moving cam1.roll" pos cam1.roll inc cam1.roll rotation pos cam1.roll print "In position."
def testReshape(self): print('Reshape testing') a = np.arange(10.) self.assertEqual((2, 5), a.reshape(2, 5).shape) self.assertEqual((2, 5), a.reshape((2, 5)).shape) self.assertEqual((5, 2), a.reshape(5, -1).shape)
def testIterate(self): print 'test iterate' l = [v for v in np.arange(3)] self.assertEqual([0, 1, 2], l)
# # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. ### ''' Run this test manually to sanity check plotting in a running DAWN when this script is launched from the host Eclipse Dev. If launched as jython, will use RMI to access plotserver If launched as python, will use AnalysisRpc to access plotserver, via SDAPlotter ''' # Note: manual add of path is needed when this is run as an automated test by: # AutomatedManualPlottingPluginTest.java import os, sys sys.path.append(sys.argv[1]) import scisoftpy as dnp # When manually testing you may want to check alternate temp locations # dnp.rpc.settemplocation('d:/temp/bug') a = dnp.arange(100) a.shape = (10, 10) if os.name == "java": plotName = "Plot 1 DNP Java" else: plotName = "Plot 1 DNP Python" dnp.plot.image(a, name=plotName)
def testPrint(self): print 'test print' a = np.arange(10) print type(a) print a
def testCompounds(self): a = np.arange(24).reshape(3, 4, 2) oa = np.compoundarray(a) ca = oa.copy() self.assertEquals(ca.shape[0], 3) self.assertEquals(ca.shape[1], 4) la = ca[1, 2] print la self.assertEquals(la[0], 12) self.assertEquals(la[1], 13) ca[2, 2] = (0, 0) self.assertEquals(ca[2, 2][0], 0) self.assertEquals(ca[2, 2][1], 0) ca[2, 2] = (-2, 1) self.assertEquals(ca[2, 2][0], -2) self.assertEquals(ca[2, 2][1], 1) ca[1:, 3:] = (-1, -1) self.assertEquals(ca[2, 3][0], -1) self.assertEquals(ca[2, 3][1], -1) ca[1:, 3:] = (3, -4) self.assertEquals(ca[2, 3][0], 3) self.assertEquals(ca[2, 3][1], -4) if isjava: ia = np.array([2, -7]) print 'Integer index testing' ca = oa.copy() cb = ca[ia] print cb self.assertEquals(cb.shape[0], 2) self.assertEquals(cb[0][0], 4) self.assertEquals(cb[0][1], 5) self.assertEquals(cb[1][0], 10) self.assertEquals(cb[1][1], 11) ca[ia] = [1, 2] self.assertEquals(ca[0, 2][0], 1) self.assertEquals(ca[0, 2][1], 2) self.assertEquals(ca[1, 1][0], 1) self.assertEquals(ca[1, 1][1], 2) print 'Boolean index testing' ba = np.array([[0, 0, 1, 0], [1, 0, 0, 0], [0, 1, 0, 1]], dtype=np.bool) ca = oa.copy() cc = ca[ba] # index dataset does not work # test boolean too print cc self.assertEquals(cc.shape[0], 4) self.assertEquals(cc[0][0], 4) self.assertEquals(cc[0][1], 5) self.assertEquals(cc[1][0], 8) self.assertEquals(cc[1][1], 9) self.assertEquals(cc[2][0], 18) self.assertEquals(cc[2][1], 19) self.assertEquals(cc[3][0], 22) self.assertEquals(cc[3][1], 23) ca[ba] = (1, 2) self.assertEquals(ca[0, 2][0], 1) self.assertEquals(ca[0, 2][1], 2) self.assertEquals(ca[1, 0][0], 1) self.assertEquals(ca[1, 0][1], 2) self.assertEquals(ca[2, 1][0], 1) self.assertEquals(ca[2, 1][1], 2) self.assertEquals(ca[2, 3][0], 1) self.assertEquals(ca[2, 3][1], 2)
arg -- tuple of additional arguments ''' return a*dnp.exp( -(x[0]-mu)*(x[0]-mu)/(2*sigma*sigma) ); ######################## m6Data=dnp.io.load("/dls/i06-1/data/2012/si7816-1/68917.dat", formats=['srs'], asdict=True); x, y=m6Data['m6qg'], m6Data['ca62sr']; #Normalize data yn=y/y.max(); xn=dnp.arange(yn.size); # [mu, sigma, peak, gf] = fineGaussianFitting(y, "Plot 2"); [mu0, sigma0, peak, gf] = fineGaussianFitting(y); n=x.size; a=(x[n-1] - x[0])/(n-1); b=x[0]; mu=a*mu0+b; sigma=a*sigma0; #One vertical line on the peak point: #xx, yy=dnp.array([mu-1, mu+1]), dnp.array([0, peak]); xx, yy=dnp.array([mu-1, mu, mu+1]), dnp.array([0, peak, 0]); xxx, yyy=dnp.array([mu]), dnp.array([peak]); #To find the closest data point to the peak;
def testTranspose(self): print 'Transpose testing' a = np.arange(20).reshape(4, 5) print a.T
def testPrint(self): print('test print') a = np.arange(10) print(type(a)) print(a)
def _createNumpyArray(self): a = dnp.arange(100) a.shape = (10, 10) return a
def sphere_alignment_processing(previewmean=False,visit_directory='/dls/i12/data/2014/cm4963-2/',flatfile=37384, projectionfile=37385, threshold=0.4, max_tries=15,prompt=False,testdata=False, request_input_command=request_DAWN_Input): results_directory="%s/tmp/orbit_test"%visit_directory if not os.path.exists(results_directory) : os.makedirs(results_directory) #The acquisition of the data is separated into another fuction, returning the lists of x and y data if not testdata: #the image processing step might fail. try: xs,ys=get_image_data(visit_directory=visit_directory, flatfile=flatfile ,projectionfile=projectionfile, threshold=threshold,max_tries=max_tries,prompt=prompt,request_input_command=request_input_command) except: raise xarray=dnp.array(xs) yarray=dnp.array(ys) else: #Lists of numbers can be read in to compare results xarray,yarray,idealarray=readTestInputs(printres=False) #is there a less annoying way? #return print "Fitting ellipse to centroid data" ellipseFitValues = dnp.fit.ellipsefit(xarray, yarray) t = dnp.arange(100)*dnp.pi/50. fitplot = dnp.fit.makeellipse(ellipseFitValues, t) print("fitplot is: %s element is %s "%(type(fitplot), type(fitplot[0]))) dnp.plot.line(fitplot[0], fitplot[1]) if previewmean: time.sleep(1) dnp.plot.addline(xarray, yarray) print "Results of ellipse fitting routine:" print " Major: %f" % (ellipseFitValues[0]) print " Minor semi-axis: %f" % (ellipseFitValues[1]) print " Major axis angle: %f" % (ellipseFitValues[2]) print " Centre co-ord 1: %f" % (ellipseFitValues[3]) print " Centre co-ord 2: %f" % (ellipseFitValues[4]) print "Calculating stage tilts" xangle = math.fabs( math.atan2(ellipseFitValues[1], ellipseFitValues[0])) #find out which direction #find the extremes in horizontal direction npoints=len(xarray) xmax=dnp.argmax(xarray) xmin=dnp.argmin(xarray) #index 1/4 of the way around firstquarter=(int(math.floor(npoints/4.0))+xmax)%npoints lastquarter=(int(math.floor(3.0*npoints/4.0))+xmax)%npoints #Show which points are used for finding the front and back: dnp.plot.addline(dnp.array([xarray[firstquarter],xarray[lastquarter]]),dnp.array([yarray[firstquarter],yarray[lastquarter]])) dnp.plot.addline(dnp.array([xarray[xmin],xarray[xmax]]),dnp.array([yarray[xmin],yarray[xmax]])) #check whether the 90 deg or 270 deg point is higher and use the corresponding sign for the angle if (yarray[firstquarter] > yarray[lastquarter]): xangle=-math.degrees(xangle) print(" negative xangle %f"%xangle) else: xangle=math.degrees(xangle) print(" positive xangle %f"%xangle) # This needs to be calculated correctly zangle = math.degrees(ellipseFitValues[2]) if zangle > 90.0: zangle -= 180.0 print "efitter Xangle (degrees)",xangle print "efitter Zangle (degrees)",zangle print "Now trying direct fourier integral method" f_ht,f_mag,f_delta,f_calcres=dofourier(xarray,yarray,printres=False) #dnp.plot.addline(xdata,f_calcres) dnp.plot.addline(xarray,f_calcres) if(testdata): dnp.plot.addline(xarray,idealarray) #nfitpoints=len(dnp.array(t)) nfitpoints=t.size fitplotarray=dnp.array(fitplot) resultsfilename="%s/efitter_%s.dat"%(results_directory,projectionfile) fout=open(resultsfilename,"w") for i in range(0,nfitpoints): fout.write("%i,%f,%f\n"%(i,fitplotarray[0][i],fitplotarray[1][i])) fout.close() resultsfilename="%s/input_%s.dat"%(results_directory,projectionfile) fout=open(resultsfilename,"w") for i in range(0,npoints): fout.write("%i,%f,%f\n"%(i,xarray[i],yarray[i])) fout.close() resultsfilename="%s/fcalc_%s.dat"%(results_directory,projectionfile) fout=open(resultsfilename,"w") for i in range(0,npoints): fout.write("%i,%f,%f\n"%(i,xarray[i],f_calcres[i])) fout.close() return (xangle,zangle,xarray,yarray)
include('workspace://Utilities/dawnplotting.py') import scisoftpy as dnp t = dnp.arange(0.0, 2.0, 0.01) s = dnp.sin(2 * dnp.pi * t) dnp.plot.line(t, s) ps = dnp.plot.getPlottingSystem() ps.setTitle("EclipseCon France Demo")
fr.parameters fr[0] fr.residual d3x = data3['entry1']['default']['x'][...] d3y = data3['entry1']['default']['y'][...] ds = dnp.fft.fft(d3y) dnp.plot.line(d3x,[ds.real,ds.imag]) dnp.plot.line(d3x,[dnp.fft.fftshift(ds.real),dnp.fft.fftshift(ds.imag)]) scan sg -1 1 0.001 data4 = dnp.io.load("<File name from the beginning of the scan>”) d4y = data4['entry1']['default']['y'][...] d4x = data4['entry1']['default']['x'][...] ds = dnp.fft.fft(d4y) dnp.plot.line(d4x,[d4y,dnp.fft.ifft(ds).real]) ds[500:1500] = 0 dnp.plot.line(d4x,[d4y,dnp.fft.ifft(ds).real]) ds[250:1750] = 0 dnp.plot.line(d4x,[d4y,dnp.fft.ifft(ds).real]) ds[50:1950] = 0 filter = dnp.arange(1,-1,(-2./2001.)) dnp.plot.line(d4x,filter) filter = dnp.abs(filter) dnp.plot.line(d4x,filter) dnp.plot.line(d4x,[d4y,dnp.fft.ifft(ds*dnp.power(filter,1)).real]) dnp.plot.line(d4x,[d4y,dnp.fft.ifft(ds*dnp.power(filter,2)).real])