def testCopyItems(self): if isjava: import jarray print "test copy items" da = np.array(self.db, np.float) jda = da._jdataset() ta = np.zeros(da.shape) jda.copyItemsFromAxes(None, None, ta._jdataset()) print ta.data ta = np.array(self.m) jda.setItemsOnAxes(None, None, ta.data) print da.data print '2' da = np.array(self.db, np.float) jda = da._jdataset() ta = np.zeros((2,)) axes = [ True, False ] jda.copyItemsFromAxes(None, axes, ta._jdataset()) print ta.data ta = jarray.array([ -2, -3.4 ], 'd') jda.setItemsOnAxes(None, axes, ta) print da.data print '3' da = np.array(self.db, np.float) jda = da._jdataset() ta = np.zeros((2,)) axes = [ False, True ] jda.copyItemsFromAxes(None, axes, ta._jdataset()) print ta.data ta = jarray.array([ -2.5, -3.4 ], 'd') jda.setItemsOnAxes(None, axes, ta) print da.data
def testZeros(self): # make new datasets with zeros print("test zeros") dds = np.zeros(3, dtype=np.float) if isjava: self.assertEquals(1, dds.dtype.elements) self.assertEquals(1, dds.ndim) self.assertEquals(3, dds.shape[0]) self.assertEquals(0, dds[0]) dds = np.zeros((2,3), dtype=np.float) if isjava: self.assertEquals(1, dds.dtype.elements) self.assertEquals(2, dds.ndim) self.assertEquals(2, dds.shape[0]) self.assertEquals(3, dds.shape[1]) self.assertEquals(0, dds[0,0]) dds = np.zeros_like(dds) if isjava: self.assertEquals(1, dds.dtype.elements) self.assertEquals(2, dds.ndim) self.assertEquals(2, dds.shape[0]) self.assertEquals(3, dds.shape[1]) self.assertEquals(0, dds[0,0]) dds = np.zeros(np.array([1,2]), dtype=np.float) self.assertEqual((1,2), dds.shape) self.assertEquals(0, dds[0,0]) dds = np.zeros_like(np.array([1,2]), dtype=np.float) self.assertEquals((2,), dds.shape) self.assertEquals(0, dds[0])
def testCopyItems(self): if isjava: import jarray print "test copy items" da = np.array(self.db, np.float) jda = da._jdataset() ta = np.zeros(da.shape) jda.copyItemsFromAxes(None, None, ta._jdataset()) print ta.data ta = np.array(self.m) jda.setItemsOnAxes(None, None, ta.data) print da.data print '2' da = np.array(self.db, np.float) jda = da._jdataset() ta = np.zeros((2, )) axes = [True, False] jda.copyItemsFromAxes(None, axes, ta._jdataset()) print ta.data ta = jarray.array([-2, -3.4], 'd') jda.setItemsOnAxes(None, axes, ta) print da.data print '3' da = np.array(self.db, np.float) jda = da._jdataset() ta = np.zeros((2, )) axes = [False, True] jda.copyItemsFromAxes(None, axes, ta._jdataset()) print ta.data ta = jarray.array([-2.5, -3.4], 'd') jda.setItemsOnAxes(None, axes, ta) print da.data
def testCopyItems(self): if isjava: import jarray print "test copy items" da = np.array(self.db, np.float) ta = np.zeros(da.getShape()) da.copyItemsFromAxes(None, None, ta) print ta.getBuffer() ta = np.array(self.m) da.setItemsOnAxes(None, None, ta.getBuffer()) print da.getBuffer() print '2' da = np.array(self.db, np.float) ta = np.zeros((2,)) axes = [ True, False ] da.copyItemsFromAxes(None, axes, ta) print ta.getBuffer() ta = jarray.array([ -2, -3.4 ], 'd') da.setItemsOnAxes(None, axes, ta) print da.getBuffer() print '3' da = np.array(self.db, np.float) ta = np.zeros((2,)) axes = [ False, True ] da.copyItemsFromAxes(None, axes, ta) print ta.getBuffer() ta = jarray.array([ -2.5, -3.4 ], 'd') da.setItemsOnAxes(None, axes, ta) print da.getBuffer()
def testZeros(self): # make new datasets with zeros print("test zeros") dds = np.zeros(3, dtype=np.float) if isjava: self.assertEqual(1, dds.dtype.elements) self.assertEqual(1, dds.ndim) self.assertEqual(3, dds.shape[0]) self.assertEqual(0, dds[0]) dds = np.zeros((2, 3), dtype=np.float) if isjava: self.assertEqual(1, dds.dtype.elements) self.assertEqual(2, dds.ndim) self.assertEqual(2, dds.shape[0]) self.assertEqual(3, dds.shape[1]) self.assertEqual(0, dds[0, 0]) dds = np.zeros_like(dds) if isjava: self.assertEqual(1, dds.dtype.elements) self.assertEqual(2, dds.ndim) self.assertEqual(2, dds.shape[0]) self.assertEqual(3, dds.shape[1]) self.assertEqual(0, dds[0, 0]) dds = np.zeros(np.array([1, 2]), dtype=np.float) self.assertEqual((1, 2), dds.shape) self.assertEqual(0, dds[0, 0]) dds = np.zeros_like(np.array([1, 2]), dtype=np.float) self.assertEqual((2, ), dds.shape) self.assertEqual(0, dds[0])
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 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 plot_results(plotName, function): d = function.coords d1 = dnp.zeros(d[0].shape[0]+1) d1[1:] = d[0] d2 = dnp.toList(d1) data = function.data data2 = dnp.zeros(data.shape[0]+1) data2[0] = function.func.getParameterValue(1) data2[1:] = data func = dnp.fit.fitcore.fitresult(function.func, d2, data2) func.plot(plotName)
def testCorrelate2D(self): print 'test correlate2' da = np.zeros([8,8], np.float) da[3,3] = 20 db = da.copy() ada = sig.correlate(da,db) print ada da += rnd.poisson(2, size=da.shape) db = np.zeros([8,8], np.float) db[5,6] = 20 db += rnd.poisson(2, size=db.shape) ada = sig.correlate(da,db) print ada
def tryImageShift(self, dx=2, dy=3, cx=23, cy=13): da = np.zeros([40,40]) da[cy,cx] = 50 da += rnd.poisson(2, da.shape) db = np.zeros([40,40]) db[cy+dy,cx+dx] = 65 db += rnd.poisson(2, db.shape) sx = max(dx,5) sy = max(dy,5) r = roi.rectangle(point=[cx-2*sx,cy-2*sy],lengths=[4*sx,4*sy],angle=0) shift = img.findshift(da, db, r) print shift self.assertAlmostEquals(-dy, shift[0], places=0) self.assertAlmostEquals(-dx, shift[1], places=0)
def tryImageShiftGaussian(self, dx=2, dy=3, cx=23, cy=13): da = np.zeros([40,40]) da[cy,cx] = 50 da += rnd.poisson(2, da.shape) db = np.zeros([40,40]) db[cy+dy,cx+dx] = 65 db += rnd.poisson(2, db.shape) sx = max(dx,10) sy = max(dy,10) r = roi.rect(cx-2*sx,cy-2*sy,4*sx,4*sy,0) shift = img.findshift(da, db, r) print shift self.assertAlmostEquals(-dy, shift[0], places=0) self.assertAlmostEquals(-dx, shift[1], places=0)
def testCorrelate2(self): print 'test correlate2' da = np.zeros([10], np.float) da[3] = 1 db = da.copy() ada = sig.correlate(da,db) self.checkitems(None, ada) db = np.zeros([10], np.float) db[5] = 1 ada = sig.correlate(da,db) self.checkitems(None, ada) ada = sig.phasecorrelate(da,db) self.checkitems(None, ada)
def __init__(self): self.ccWaveFormHead = CAClient(pvWaveFormHead) self.ccSubArraySize = CAClient(pvSubArraySize) self.ccSubArrayIndex = CAClient(pvSubArrayIndex) self.ccUpdate = CAClient(pvUpdate) self.ccCh01 = CAClient(pvCh01) self.ccCh02 = CAClient(pvCh02) self.ccCh03 = CAClient(pvCh03) self.ccCh04 = CAClient(pvCh04) self.ccCh05 = CAClient(pvCh05) self.ccCh06 = CAClient(pvCh06) self.ccWaveFormHead.configure() self.ccSubArraySize.configure() self.ccSubArrayIndex.configure() self.ccUpdate.configure() self.ccCh01.configure() self.ccCh02.configure() self.ccCh03.configure() self.ccCh04.configure() self.ccCh05.configure() self.ccCh06.configure() self.length = 0 self.head = 0 self.dataSetCh01 = dnp.zeros([maxLength]) self.dataSetCh01.setName("Channel 1") self.js = Finder.find("command_server")
def testCompound(self): # make new datasets with ones print "test compound" dds = np.ndarrayCB(3, (3,4)) self.assertEquals(3, dds.itemsize, msg="itemsize incorrect") dds = np.ndarrayRGB((3,5)) print type(dds), dds.dtype, dds.shape self.assertEquals(6, dds.itemsize, msg="itemsize incorrect") dds = dds.get_red() print type(dds), dds.dtype, dds.shape self.assertEquals(2, dds.itemsize, msg="itemsize incorrect") dds = np.zeros((3,4), np.cint32, 2) print type(dds), dds.dtype, dds.shape self.assertEquals(8, dds.itemsize, msg="itemsize incorrect") dds = np.zeros((3,4), np.cint32(2)) print type(dds), dds.dtype, dds.shape self.assertEquals(8, dds.itemsize, msg="itemsize incorrect")
def scan_start_temp(): temps = dnp.arange(10, 200, 10) p0 = dnp.zeros(temps.shape) p1 = dnp.zeros(temps.shape) p2 = dnp.zeros(temps.shape) count = 0 for temp in temps: (fwhm, values) = scan_fwhm(0, 0.2, 0.01, temp) res = dnp.fit.polyfit(values, fwhm, 2) p0[count] = res[0] p1[count] = res[1] p2[count] = res[2] count += 1 dnp.plot.line(temps, [p0, p1, p2], name="Plot 3") return (dnp.fit.polyfit(temps, p0, 2), dnp.fit.polyfit(temps, p1, 2), dnp.fit.polyfit(temps, p2, 2))
def testCompound(self): # make new datasets with ones print "test compound" # dds = np.ndarrayCB(3, (3,4)) # self.assertEquals(3, dds.itemsize, msg="itemsize incorrect") dds = np.zeros((3,5), np.rgb) print type(dds), dds.dtype, dds.shape self.assertEquals(6, dds.itemsize, msg="itemsize incorrect") dds = dds.red print type(dds), dds.dtype, dds.shape self.assertEquals(2, dds.itemsize, msg="itemsize incorrect") dds = np.zeros((3,4), np.cint32, elements=2) print type(dds), dds.dtype, dds.shape self.assertEquals(8, dds.itemsize, msg="itemsize incorrect") dds = np.zeros((3,4), np.cint32(2)) print type(dds), dds.dtype, dds.shape self.assertEquals(8, dds.itemsize, msg="itemsize incorrect")
def testCompound(self): # make new datasets with ones print "test compound" # dds = np.ndarrayCB(3, (3,4)) # self.assertEquals(3, dds.itemsize, msg="itemsize incorrect") dds = np.zeros((3, 5), np.rgb) print type(dds), dds.dtype, dds.shape self.assertEquals(6, dds.itemsize, msg="itemsize incorrect") dds = dds.red print type(dds), dds.dtype, dds.shape self.assertEquals(2, dds.itemsize, msg="itemsize incorrect") dds = np.zeros((3, 4), np.cint32, elements=2) print type(dds), dds.dtype, dds.shape self.assertEquals(8, dds.itemsize, msg="itemsize incorrect") dds = np.zeros((3, 4), np.cint32(2)) print type(dds), dds.dtype, dds.shape self.assertEquals(8, dds.itemsize, msg="itemsize incorrect")
def tryImageShiftGaussian(self, dx=2, dy=3, cx=23, cy=13): da = np.zeros([40, 40]) da[cy, cx] = 50 da += rnd.poisson(2, da.shape) db = np.zeros([40, 40]) db[cy + dy, cx + dx] = 65 db += rnd.poisson(2, db.shape) sx = max(dx, 10) sy = max(dy, 10) r = roi.rectangle(point=[cx - 2 * sx, cy - 2 * sy], lengths=[4 * sx, 4 * sy], angle=0) shift = img.findshift(da, db, r) print shift self.assertAlmostEquals(-dy, shift[0], places=0) self.assertAlmostEquals(-dx, shift[1], places=0)
def scan_start_temp(): temps = dnp.arange(10,200,10) p0 = dnp.zeros(temps.shape) p1 = dnp.zeros(temps.shape) p2 = dnp.zeros(temps.shape) count = 0 for temp in temps: (fwhm,values) = scan_fwhm(0,0.2,0.01,temp) res = dnp.fit.polyfit(values,fwhm,2) p0[count] = res[0] p1[count] = res[1] p2[count] = res[2] count+=1 dnp.plot.line(temps,[p0,p1,p2],name="Plot 3") return(dnp.fit.polyfit(temps,p0,2), dnp.fit.polyfit(temps,p1,2), dnp.fit.polyfit(temps,p2,2))
def testCorrelate2D(self): import os if os.name != 'java': return print 'test correlate2' da = np.zeros([8, 8], np.float) da[3, 3] = 20 db = da.copy() ada = np.correlate(da, db) print ada da += rnd.poisson(2, size=da.shape) db = np.zeros([8, 8], np.float) db[5, 6] = 20 db += rnd.poisson(2, size=db.shape) ada = np.correlate(da, db) print ada
def testCorrelate2(self): print 'test correlate2' da = np.zeros([10], np.float) da[3] = 1 db = da.copy() ada = np.correlate(da, db) self.checkitems(None, ada) db = np.zeros([10], np.float) db[5] = 1 ada = np.correlate(da, db) self.checkitems(None, ada) import os if os.name == 'java': ada = sig.phasecorrelate(da, db) self.checkitems(None, ada)
def testCorrelate2D(self): import os if os.name != 'java': return print 'test correlate2' da = np.zeros([8,8], np.float) da[3,3] = 20 db = da.copy() ada = np.correlate(da,db) print ada da += rnd.poisson(2, size=da.shape) db = np.zeros([8,8], np.float) db[5,6] = 20 db += rnd.poisson(2, size=db.shape) ada = np.correlate(da,db) print ada
def testCorrelate2(self): print('test correlate2') da = np.zeros([10], np.float) da[3] = 1 db = da.copy() ada = np.correlate(da,db) self.checkitems(None, ada) db = np.zeros([10], np.float) db[5] = 1 ada = np.correlate(da,db) self.checkitems(None, ada) import os if os.name == 'java': ada = sig.phasecorrelate(da,db) self.checkitems(None, ada)
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)
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)
def testZeros(self): # make new datasets with zeros print "test zeros" dds = np.zeros(3, dtype=np.float) if isjava: self.assertEquals(1, dds.dtype.elements) self.assertEquals(1, dds.ndim) self.assertEquals(3, dds.shape[0]) self.assertEquals(0, dds[0]) dds = np.zeros((2, 3), dtype=np.float) if isjava: self.assertEquals(1, dds.dtype.elements) self.assertEquals(2, dds.ndim) self.assertEquals(2, dds.shape[0]) self.assertEquals(3, dds.shape[1]) self.assertEquals(0, dds[0, 0]) dds = np.zeros_like(dds) if isjava: self.assertEquals(1, dds.dtype.elements) self.assertEquals(2, dds.ndim) self.assertEquals(2, dds.shape[0]) self.assertEquals(3, dds.shape[1]) self.assertEquals(0, dds[0, 0])
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_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 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])
def plotData(self): newHead = self.getDataNumbers() if self.arrayHead >= newHead: print "No new data added for plotting" return self.arrayHead = newHead #to get new data arrayEnergyPGM = self.energyPGM.cagetArrayDouble() arrayEnergyIDGAP = self.energyIDGAP.cagetArrayDouble() arrayChannel01 = self.channel01.cagetArrayDouble() arrayChannel02 = self.channel02.cagetArrayDouble() arrayChannel03 = self.channel03.cagetArrayDouble() arrayChannel04 = self.channel04.cagetArrayDouble() dataSetEnergyPGM = dnp.zeros([newHead]) dataSetEnergyPGM.setName("PGM Energy") dataSetEnergyIDGAP = dnp.zeros([newHead]) dataSetEnergyIDGAP.setName("ID Gap Energy") dataSetChannel01 = dnp.zeros([newHead]) dataSetChannel01.setName("Channel 1") dataSetChannel02 = dnp.zeros([newHead]) dataSetChannel02.setName("Channel 2") dataSetChannel03 = dnp.zeros([newHead]) dataSetChannel03.setName("Channel 3") dataSetChannel04 = dnp.zeros([newHead]) dataSetChannel04.setName("Channel 4") for i in range(0, newHead): #print i, arrayEnergyPGM[i], arrayEnergyIDGAP[i], arrayChannel01[i], arrayChannel02[i], arrayChannel03[i], arrayChannel04[i]; dataSetEnergyPGM[i] = arrayEnergyPGM[i] dataSetEnergyIDGAP[i] = arrayEnergyIDGAP[i] dataSetChannel01[i] = arrayChannel01[i] dataSetChannel02[i] = arrayChannel02[i] dataSetChannel03[i] = arrayChannel03[i] dataSetChannel04[i] = arrayChannel04[i] #print i, arrayEnergyPGM[i], arrayEnergyIDGAP[i], arrayChannel01[i], arrayChannel02[i], arrayChannel03[i], arrayChannel04[i]; dvp = Plotter() indexDataSet = dataSetEnergyPGM.getIndexDataSet() #dvp.plot("Data Vector", indexDataSet, [dataSetChannel01, dataSetChannel02, dataSetChannel03, dataSetChannel04]); dvp.plot("Fast Scan Panel", dataSetEnergyPGM, [ dataSetChannel01, dataSetChannel02, dataSetChannel03, dataSetChannel04 ])
def testDatasetSlice(self): print('test slicing with start and stop') ds = np.arange(16) dr = ds[2:10] self.checkitems(list(range(2,10)), dr) print('test slicing with steps') dr = ds[::2] self.checkitems(list(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(list(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 refl(runfiles, pathtofiles, outputpath, scalar, beamheight, footprint, angularfudgefactor, wl, back=()): # scalar - scale factor to divide the data by # beamheight FWHM in microns # footprint in mm # angular offset correction in degrees # wavelength in wl # back is an optional variable to subtract a background, set back=1 to do a background subtraction qq = [] RR = [] dR = [] ii = -1 for filename in runfiles: data = dnp.io.load(pathtofiles + "/" + str(filename) + ".dat") ii += 1 theta = data.alpha # work out the q vector qqtemp = 4 * dnp.pi * dnp.sin( (theta + angularfudgefactor) * dnp.pi / 180) / wl #qqtemp = data.qdcd # this section is to allow users to set limits on the q range used from each file if not 'qmin' + str(ii) in refl.__dict__: qmin = qqtemp.min() else: print "USER SET", qmin = refl.__getattribute__('qmin' + str(ii)) print 'refl.qmin' + str(ii) + " = " + str(qmin) + " ;", if not 'qmax' + str(ii) in refl.__dict__: qmax = qqtemp.max() else: print "USER SET", qmax = refl.__getattribute__('qmax' + str(ii)) print 'refl.qmax' + str(ii) + " = " + str(qmax) + " ;", roi1_sum = data.roi1_sum roi1_sum = roi1_sum[dnp.where((qqtemp >= qmin) & (qqtemp <= qmax))] roi1dr = dnp.sqrt(roi1_sum) theta = theta[dnp.where((qqtemp >= qmin) & (qqtemp <= qmax))] qqtemp = qqtemp[dnp.where((qqtemp >= qmin) & (qqtemp <= qmax))] bg_sum = dnp.zeros(len(roi1_sum)) bg_dr = dnp.zeros(len(roi1dr)) # if background ROI number given as int, convert to a single-item tuple if type(back) == int: back = (back, ) # subtract any background ROIs from the data if len(back) > 0 and back[0] > 0: if ii == 0: print "Using background from " + str( len(back)) + " ROIs: " + str(back) for bg in back: if ('roi' + str(bg) + '_sum' in data.keys()): bg_cur = data[data.keys().index('roi' + str(bg) + '_sum')] dr_cur = dnp.sqrt(bg_cur) (bg_sum, bg_dr) = ep.EPadd(bg_sum, bg_dr, bg_cur, dr_cur) (bg_sum, bg_dr) = ep.EPmulk(bg_sum, bg_dr, 1.0 / len(back)) else: if ii == 0: print "Not subtracting a background" (RRtemp, drtemp) = ep.EPsub(roi1_sum, roi1dr, bg_sum, bg_dr) # do a footprint correction. # assumes that the beam is gaussian in profile, with a FWHM of "beamheight". # footprint of sample is measured in mm. areamultiplier = 2 * (norm.cdf( footprint * dnp.sin( (theta + angularfudgefactor) / 180 * dnp.pi) / 2, 0, 1e-3 * beamheight / (2 * dnp.sqrt(2 * dnp.log(2)))) - 0.5) RRtemp /= areamultiplier drtemp /= areamultiplier # for the 2nd, 3rd, 4th q ranges have to splice the data on the end of the preexisting data if (ii > 0): # splice (scalingfactor, sferror) = nsplice.getScalingInOverlap(qq, RR, dR, qqtemp, RRtemp, drtemp) RRtemp *= scalingfactor drtemp *= scalingfactor print "Error in scaling factor: %2.3f %%" % (sferror / scalingfactor * 100) # now concatenate the data. qq = dnp.concatenate((qq, qqtemp)) RR = dnp.concatenate((RR, RRtemp)) dR = dnp.concatenate((dR, drtemp)) # end of per-file loop RR /= scalar dR /= scalar RR = RR[np.argsort(qq)] dR = dR[np.argsort(qq)] qq = np.sort(qq) # write out the data. np.savetxt( outputpath + "/" + str(runfiles[0]) + "_refl.dat", dnp.concatenate((qq, RR, dR)).reshape(3, qq.shape[0]).transpose())
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 refl(runfiles, pathtofiles, outputpath, scalar, beamheight, footprint, angularfudgefactor, wl, back=()): # scalar - scale factor to divide the data by # beamheight FWHM in microns # footprint in mm # angular offset correction in degrees # wavelength in wl # back is an optional variable to subtract a background, set back=1 to do a background subtraction qq = [] RR = [] dR = [] ii = -1 for filename in runfiles: data = dnp.io.load(pathtofiles + "/" + str(filename) + ".dat") ii += 1 theta = data.alpha # work out the q vector qqtemp = 4 * dnp.pi * dnp.sin((theta + angularfudgefactor) * dnp.pi /180) / wl #qqtemp = data.qdcd # this section is to allow users to set limits on the q range used from each file if not 'qmin' + str(ii) in refl.__dict__: qmin = qqtemp.min() else: print "USER SET", qmin = refl.__getattribute__('qmin' + str(ii)) print 'refl.qmin' + str(ii) + " = " + str(qmin) + " ;", if not 'qmax' + str(ii) in refl.__dict__: qmax = qqtemp.max() else: print "USER SET", qmax = refl.__getattribute__('qmax' + str(ii)) print 'refl.qmax' + str(ii) + " = " + str(qmax) + " ;", roi1_sum = data.roi1_sum roi1_sum = roi1_sum[dnp.where((qqtemp >= qmin) & (qqtemp <= qmax))] roi1dr = dnp.sqrt(roi1_sum) theta = theta[dnp.where((qqtemp >= qmin) & (qqtemp <= qmax))] qqtemp = qqtemp[dnp.where((qqtemp >= qmin) & (qqtemp <= qmax))] bg_sum = dnp.zeros(len(roi1_sum)) bg_dr = dnp.zeros(len(roi1dr)) # if background ROI number given as int, convert to a single-item tuple if type(back) == int: back = (back,) # subtract any background ROIs from the data if len(back) > 0 and back[0] > 0: if ii==0: print "Using background from " + str(len(back)) + " ROIs: " + str(back) for bg in back: if ('roi' + str(bg) + '_sum' in data.keys()): bg_cur = data[data.keys().index('roi' +str(bg) + '_sum')] dr_cur = dnp.sqrt(bg_cur) (bg_sum, bg_dr) = ep.EPadd(bg_sum, bg_dr, bg_cur, dr_cur) (bg_sum, bg_dr) = ep.EPmulk(bg_sum, bg_dr, 1.0/len(back)) else: if ii==0: print "Not subtracting a background" (RRtemp, drtemp) = ep.EPsub(roi1_sum, roi1dr, bg_sum, bg_dr) # do a footprint correction. # assumes that the beam is gaussian in profile, with a FWHM of "beamheight". # footprint of sample is measured in mm. areamultiplier = 2*(norm.cdf(footprint * dnp.sin((theta + angularfudgefactor) / 180 * dnp.pi) / 2, 0, 1e-3 * beamheight/ (2*dnp.sqrt(2*dnp.log(2)))) - 0.5) RRtemp /= areamultiplier drtemp /= areamultiplier # for the 2nd, 3rd, 4th q ranges have to splice the data on the end of the preexisting data if(ii > 0): # splice (scalingfactor, sferror) = nsplice.getScalingInOverlap(qq, RR, dR, qqtemp, RRtemp, drtemp) RRtemp *= scalingfactor drtemp *= scalingfactor print "Error in scaling factor: %2.3f %%" % (sferror/scalingfactor*100) # now concatenate the data. qq = dnp.concatenate((qq, qqtemp)) RR = dnp.concatenate((RR, RRtemp)) dR = dnp.concatenate((dR, drtemp)) # end of per-file loop RR /= scalar dR /= scalar RR = RR[np.argsort(qq)] dR = dR[np.argsort(qq)] qq = np.sort(qq) # write out the data. np.savetxt(outputpath+"/"+str(runfiles[0])+"_refl.dat",dnp.concatenate((qq,RR,dR)).reshape(3,qq.shape[0]).transpose())
def process_peem(prefix=None, displayname=IMAGEDPYNAME, explorerview=IMAGEEXPNAME): '''Process PEEM images selection from ImageExplorer view after defining rectangular regions of interest process_peem(prefix=None, displayname="PEEM Image", explorerview="Image Explorer") Results are returned as the CD map, positive and negative images and a list of averaged images. They are also saved as prefix+ave-[0-3].png and prefix+pmap.png. The shifts are saved in prefix+shift.dat ''' start = time.time() files = get_peem_files_from_selection(explorerview) if files == None: print "No images selected" if _PEEMDIR: print "Using images in ", _PEEMDIR import glob, os.path names = glob.glob(os.path.join(_PEEMDIR, '*.png')) omit = glob.glob(os.path.join(_PEEMDIR, '*ave-[0-3].png')) omit += glob.glob(os.path.join(_PEEMDIR, '*pmap.png')) files = [n for n in names if not n in omit] else: return None num = len(files) nsets = num // NPEEM # number of sets of images print "%d sets of PEEM images" % nsets if (num % NPEEM) != 0: print "Need to select multiples of %d images" % NPEEM return # divide up images to (s+,la), (s+,lb), (s-,la) and (s-,lb) ifiles = [] ifiles.append(files[0:nsets]) ifiles.append(files[nsets:2 * nsets]) ifiles.append(files[2 * nsets:3 * nsets]) ifiles.append(files[3 * nsets:4 * nsets]) crs = get_peem_rrois(displayname) if crs == None: print "WARNING, no rectangular region of interest selected" aimages = [] da = peem_load_dataset(ifiles[0][0]) shape = da.shape for n in range(NPEEM): aimage = np.zeros(shape) #@UndefinedVariable # images are organised to groups of nsets lfiles = ifiles[n] for s, im in enumerate(lfiles): print " %s" % basename(im) db = peem_load_dataset(im) #@UndefinedVariable aimage += db aimage /= nsets aimage.name = "average %d" % n peem_save_dataset(prefix + "ave-%d" % n + ".png", aimage) aimages.append(aimage) else: rois = [] if len(crs) == 1: rois.append(crs[0]) rois.append(crs[0]) rois.append(crs[0]) rois.append(crs[0]) elif len(crs) < 4: rois.append(crs[0]) rois.append(crs[0]) rois.append(crs[1]) rois.append(crs[1]) elif len(crs) >= 4: rois.append(crs[0]) rois.append(crs[1]) rois.append(crs[2]) rois.append(crs[3]) shifts = [] fromtop = False for n in range(NPEEM): shift, da = align_peem_images(ifiles[0], rois[0], fromtop) shifts.append(shift) fromtop = not fromtop save_peem_shifts(prefix + "shifts.dat", shifts) shape = da.shape # work out average (save averaged set) print "working out average" aimages = [] for n in range(NPEEM): aimage = np.zeros(shape) #@UndefinedVariable lfiles = ifiles[n] shift = shifts[n] print "average %d" % n for s, im in enumerate(lfiles): print " %s" % basename(im) db = peem_load_dataset(im) #@UndefinedVariable sim = img.shiftimage(db, shift[s]) aimage += sim aimage /= nsets aimage.name = "average %d" % n peem_save_dataset(prefix + "ave-%d" % n + ".png", aimage) aimages.append(aimage) # calculate degree of polarization map pmap, pos, neg = xmcd(aimages) pmap.name = "poln map" peem_save_dataset(prefix + "pmap.png", pmap, scaled=True) # pl.image(pmap) print 'Aligned and processed images: %.2fs' % (time.time() - start) return pmap, pos, neg, aimages