def testIntegers(self): print 'test integers get and set' a = np.array([8, 9, 10, 11, 12, 13]) d = a[np.where(a > 10)] self.checkitems([11, 12, 13], d) a.shape = 3, 2 d = a[np.where(a > 10)] self.checkitems([11, 12, 13], d) a[np.where(a > 10)] = -1 self.checkitems([[8, 9], [10, -1], [-1, -1]], a) # fancy indexing a = np.array([8, 9, 10, 11, 12, 13]) a.shape = 3, 2 self.assertEquals(13, a[(2, 1)]) d = a[(2, 1), ] self.checkitems([[12, 13], [10, 11]], d) tm = np.array(self.mm) d = tm[[1]] self.checkitems([[[20., 30.], [42., 56.]]], d) d = tm[np.array([0, 1]), np.array([1, 1])] self.checkitems([[6., 12.], [42., 56.]], d) d = tm[np.array([0, 1]), np.array([1, 1]), :1] self.checkitems([[6.], [42.]], d) d = tm[np.array([[0, 1], [1, 1]]), np.array([[1, 1], [1, 0]]), :1] self.checkitems([[[6.], [42.]], [[42.], [20.]]], d) d = tm[[[0, 1], [1, 1]], [[1, 1], [1, 0]], :1] self.checkitems([[[6.], [42.]], [[42.], [20.]]], d) tm[np.array([[0, 1], [1, 1]]), np.array([[1, 1], [1, 0]]), :1] = -2.3 self.checkitems([[[0., 2.], [-2.3, 12.]], [[-2.3, 30.], [-2.3, 56.]]], tm) # fancy with booleans too tm = np.array(self.mm) d = tm[np.array([0, 1]), np.array([False, True]), :1] self.checkitems([[6.], [42.]], d) # TODO broadcasting with lower ranked booleam d = np.arange(8).reshape(4, 2) m = np.array([False, True, True, False]) print 'broadcast', d[m] self.checkitems([[2, 3], [4, 5]], d[m])
def testIntegers(self): print('test integers get and set') a = np.array([8,9,10,11,12,13]) d = a[np.where(a > 10)] self.checkitems([11, 12, 13], d) a.shape = 3,2 d = a[np.where(a > 10)] self.checkitems([11, 12, 13], d) a[np.where(a > 10)] = -1 self.checkitems([[8,9],[10,-1], [-1, -1]], a) # fancy indexing a = np.array([8,9,10,11,12,13]) a.shape = 3,2 self.assertEquals(13, a[(2,1)]) d = a[(2,1),] self.checkitems([[12, 13], [10, 11]], d) tm = np.array(self.mm) d = tm[[1]] self.checkitems([[[20., 30.], [42., 56.]]], d) d = tm[np.array([0, 1]), np.array([1, 1])] self.checkitems([[6., 12.], [42., 56.]], d) d = tm[np.array([0, 1]), np.array([1, 1]), :1] self.checkitems([[6.], [42.]], d) d = tm[np.array([[0, 1], [1, 1]]), np.array([[1, 1], [1, 0]]), :1] self.checkitems([[[6.], [42.]], [[42.], [20.]]], d) d = tm[[[0, 1], [1, 1]], [[1, 1], [1, 0]], :1] self.checkitems([[[6.], [42.]], [[42.], [20.]]], d) tm[np.array([[0, 1], [1, 1]]), np.array([[1, 1], [1, 0]]), :1] = -2.3 self.checkitems([[[0., 2.], [-2.3, 12.]], [[-2.3, 30.], [-2.3, 56.]]], tm) # fancy with booleans too tm = np.array(self.mm) d = tm[np.array([0, 1]), np.array([False, True]), :1] self.checkitems([[6.], [42.]], d) # TODO broadcasting with lower ranked booleam d = np.arange(8).reshape(4,2) m = np.array([False, True, True, False]) print('broadcast', d[m]) self.checkitems([[2, 3], [4, 5]], d[m])
def find_peak_coords(peak_image): data = peak_image[0].transpose() roi = data[centre[0]-width/2:centre[0]+width/2][:] peak_value = np.max(roi) coords = dnp.where(peak_image[0]==peak_value) coords = zip(coords[0], coords[1]) return coords[0]
def testIntegers(self): print 'test integers get and set' a = np.array([8,9,10,11,12,13]) d = a[np.where(a > 10)] self.checkitems([11, 12, 13], d) a.shape = 3,2 d = a[np.where(a > 10)] self.checkitems([11, 12, 13], d) a[np.where(a > 10)] = -1 self.checkitems([[8,9],[10,-1], [-1, -1]], a) # fancy indexing a = np.array([8,9,10,11,12,13]) a.shape = 3,2 self.assertEquals(13, a[(2,1)]) d = a[(2,1),] self.checkitems([[12, 13], [10, 11]], d) tm = np.array(self.mm) d = tm[[1]] self.checkitems([[[20., 30.], [42., 56.]]], d) d = tm[np.array([0, 1]), np.array([1, 1])] self.checkitems([[6., 12.], [42., 56.]], d) d = tm[np.array([0, 1]), np.array([1, 1]), :1] self.checkitems([[6.], [42.]], d) d = tm[np.array([[0, 1], [1, 1]]), np.array([[1, 1], [1, 0]]), :1] self.checkitems([[[6.], [42.]], [[42.], [20.]]], d) d = tm[[[0, 1], [1, 1]], [[1, 1], [1, 0]], :1] self.checkitems([[[6.], [42.]], [[42.], [20.]]], d) tm[np.array([[0, 1], [1, 1]]), np.array([[1, 1], [1, 0]]), :1] = -2.3 self.checkitems([[[0., 2.], [-2.3, 12.]], [[-2.3, 30.], [-2.3, 56.]]], tm) # fancy with booleans too tm = np.array(self.mm) d = tm[np.array([0, 1]), np.array([False, True]), :1] self.checkitems([[6.], [42.]], d)
def onUpdatePlot(self, event=None): if self.paused or not self.needs_refresh: return tnow = time.time() # set timescale sec/min/hour timescale = 1.0 if self.time_choice.GetSelection() == 1: timescale = 1. / 60 elif self.time_choice.GetSelection() == 2: timescale = 1. / 3600 ylabelset, y2labelset = False, False xlabel = 'Elapsed Time (%s)' % self.timelabel itrace = -1 update_failed = False hasplot = False span1 = (1, 0) did_update = False left_axes = self.plotpanel.axes right_axes = self.plotpanel.get_right_axes() for irow, pname, uselog, color, ymin, ymax in self.get_current_traces( ): if pname not in self.pvdata: continue itrace += 1 if len(self.plots_drawn) < itrace: self.plots_drawn.extend([False] * 3) side = 'left' if itrace == 1: side = 'right' data = self.pvdata[pname][:] if len(data) < 2: update_failed = True continue tdat = timescale * (array([i[0] for i in data]) - tnow) mask = where(tdat > self.tmin) if (len(mask[0]) < 2 or ((abs(min(tdat)) / abs(1 - self.tmin)) > 0.1)): data.append((time.time(), data[0][-1])) tdat = timescale * (array([i[0] for i in data]) - tnow) mask = where(tdat > self.tmin) i0 = mask[0][0] if i0 > 0: i0 = i0 - 1 i1 = mask[0][-1] + 1 tdat = timescale * (array([i[0] for i in data[i0:i1]]) - tnow) ydat = array([i[1] for i in data[i0:i1]]) if len(ydat) < 2: update_failed = True continue if ymin is None: ymin = min(ydat) if ymax is None: ymax = max(ydat) # for more that 2 plots, scale to left hand axis if itrace == 0: span1 = (ymax - ymin, ymin) if span1[0] * ymax < 1.e-6: update_failed = True continue elif itrace > 1: yr = abs(ymax - ymin) if yr > 1.e-9: ydat = span1[1] + 0.99 * (ydat - ymin) * span1[0] / yr ymin, ymax = min(ydat), max(ydat) if self.needs_refresh: if itrace == 0: self.plotpanel.set_ylabel(pname) elif itrace == 1: self.plotpanel.set_y2label(pname) if not self.plots_drawn[itrace]: plot = self.plotpanel.oplot if itrace == 0: plot = self.plotpanel.plot try: plot(tdat, ydat, drawstyle='steps-post', side=side, ylog_scale=uselog, color=color, xmin=self.tmin, xmax=0, xlabel=xlabel, label=pname, autoscale=False) self.plots_drawn[itrace] = True except: update_failed = True else: try: self.plotpanel.update_line(itrace, tdat, ydat, draw=False) self.plotpanel.set_xylims((self.tmin, 0, ymin, ymax), side=side, autoscale=False) did_update = True except: update_failed = True axes = left_axes if itrace == 1: axes = right_axes if uselog and min(ydat) > 0: axes.set_yscale('log', basey=10) else: axes.set_yscale('linear') self.plotpanel.set_title( time.strftime("%Y-%b-%d %H:%M:%S", time.localtime())) if did_update: self.plotpanel.canvas.draw() self.needs_refresh = update_failed return
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 testWhere(self): print 'test where' t = np.array([[[-0.5, 0.1], [2, 0]], [[1.3, 1.4], [-10, -20]]]) tm = np.where(t > 0, np.array(self.mm), -2.3) self.checkitems([ [[-2.3, 2.], [6., -2.3]], [[20., 30.], [-2.3, -2.3]] ], tm)
def testWhere(self): print 'test where' t = np.array([[[-0.5, 0.1], [2, 0]], [[1.3, 1.4], [-10, -20]]]) tm = np.where(t > 0, np.array(self.mm), -2.3) self.checkitems([[[-2.3, 2.], [6., -2.3]], [[20., 30.], [-2.3, -2.3]]], tm)
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())