def plotPeaks(x, y, peaks, showPeaks=True, plotLabels=False, fig=10, plotScore=False, plotsmooth=True, plothalf=False, plotbottom=False, plotmarker='.-b'): """ Plot detected peaks Arguments --------- x,y : numpy arrays scandata peaks : list list of peaks to plot showPeaks, plotLabels, plotScore, plothalf : boolean plotting options Returns ------- handles : dictionary graphics handles """ kk = np.ones(3) / 3. ys = scipy.ndimage.filters.correlate1d(y, kk, mode='nearest') stdY = np.std(y) pmatlab.cfigure(fig) plt.clf() h = plt.plot(x, y, plotmarker) # , label='data points') if plotsmooth: plt.plot(x, ys, 'g') # plt.plot(x,w) labelh = [] for jj, peak in enumerate(peaks): if peak['valid']: p = peak['p'] xpos = peak['x'] ypos = peak['y'] if showPeaks: plt.plot(xpos, ypos, '.r', markersize=15, label='peaks') if plotLabels: tp = peak.get('type', 'peak') lbl = '%s %d' % (tp, jj) if plotScore: lbl += ': score %.1f ' % peak['score'] # print('plot label!') lh = plt.text(xpos, ypos + .05 * stdY, lbl) labelh += [lh] halfhandle = [] if plothalf: for p in peaks: hh = plt.plot(p['xhalfl'], p['yhalfl'], '.m', markersize=12, label='peak half height') halfhandle += [hh[0]] if plotbottom: for p in peaks: if p['valid']: plt.plot( p['xbottoml'], p['ybottoml'], '.', color=[.5, 1, .5], markersize=12, label='peak bottom left') th = plt.title('Local maxima') return dict({'linehandle': h, 'title': th, 'labelh': labelh, 'halfhandle': halfhandle})
def show2Dimage(im, dd, **kwargs): """ Show image in window Arguments --------- im : array image to show dd : dict with scan data data is used to scale the image to measurement resolution """ _ = show2D(dd, im=im, **kwargs) return None try: extentImage, xdata, ydata, imdummy = get2Ddata(dd, fastscan=False, verbose=0, fig=None, midx=midx) mdata = dd except: extentscan, g0, g2, vstep, vsweep, arrayname = dataset2Dmetadata( dd, arrayname=None) extentImage = [vsweep[0], vsweep[-1], vstep[-1], vstep[0]] # matplotlib extent style mdata = dd.metadata pgeometry.cfigure(fig, facecolor=facecolor) plt.clf() if verbose >= 2: print('show2D: show image') imh = plt.imshow(im, extent=extent2fullextent(extentImage, im), interpolation='nearest') # imh=plt.imshow(im, extent=xx, interpolation='nearest') if units is not None: if 'stepdata' in mdata: plt.xlabel('%s (%s)' % (dd['sweepdata']['gates'][0], units)) plt.ylabel('%s (%s)' % (dd['stepdata']['gates'][0], units)) else: if 'stepdata' in mdata: plt.xlabel('%s' % dd['sweepdata']['gates'][0]) plt.ylabel('%s' % dd['stepdata']['gates'][0]) if not title is None: plt.title(title) if colorbar: plt.colorbar() if verbose >= 2: print('show2D: at show') try: plt.show(block=False) except: # ipython backend does not know about block keyword... plt.show() return extentImage
def plotBarrierFit(imq, imextent, pp, fig=400, title='Fitted model'): """ Plot the fitted model of a V-shape """ pmatlab.cfigure(fig) plt.clf() plt.imshow(imq, extent=extent2fullextent(imextent, imq), interpolation='nearest') plt.axis('tight') plt.title(title) plt.ylabel('Frequency') plt.ylabel('Frequency / [mV]') plt.axis('tight') x0 = np.linspace(imextent[0], imextent[1], 1000) yfit = barrierModel(x0, pp) plt.plot(x0, yfit, '-g', label='Model') ppx = copy.deepcopy(pp) ppx[2] = 0 yfit = barrierModel(x0, ppx) plt.plot(x0, yfit, '--g')
def show2D(dd, impixel=None, im=None, fig=101, verbose=1, dy=None, sigma=None, colorbar=False, title=None, midx=2, units=None): """ Show result of a 2D scan Args: dd (DataSet) impixel (array or None) im (array or None) """ if dd is None: return None extent, g0, g1, vstep, vsweep, arrayname = dataset2Dmetadata(dd) tr = image_transform(dd, mode='pixel') array = getattr(dd, arrayname) if impixel is None: if im is None: im = np.array(array) impixel = tr._transform(im) else: pass else: pass labels = [s.name for s in array.set_arrays] xx = extent xx = tr.matplotlib_image_extent() ny = vstep.size nx = vsweep.size im = qtt.utilities.tools.diffImageSmooth(impixel, dy=dy, sigma=sigma) if verbose: print('show2D: nx %d, ny %d' % ( nx, ny, )) if verbose >= 2: print('extent: %s' % xx) if units is None: unitstr = '' else: unitstr = ' (%s)' % units if fig is not None: scanjob = dd.metadata.get('scanjob', dict()) pgeometry.cfigure(fig) plt.clf() if impixel is None: if verbose >= 2: print('show2D: show raw image') plt.pcolormesh(vstep, vsweep, im) else: if verbose >= 2: print('show2D: show image') plt.imshow(impixel, extent=xx, interpolation='nearest') labelx = labels[1] labely = labels[0] if scanjob.get('sweepdata', None) is not None: labelx = sweepgate(scanjob) plt.xlabel('%s' % labelx + unitstr) else: pass if scanjob.get('stepdata', None) is not None: if units is None: plt.ylabel('%s' % stepgate(scanjob)) else: plt.ylabel('%s (%s)' % (stepgate(scanjob), units)) if not title is None: plt.title(title) if colorbar: plt.colorbar() if verbose >= 2: print('show2D: at show') try: plt.show(block=False) except: # ipython backend does not know about block keyword... plt.show() return xx, vstep, vsweep
def costFunctionLine(pp, imx, istep, maxshift=12, verbose=0, fig=None, maxangle=np.deg2rad(70), ksizemv=12, dthr=8, dwidth=3, alldata=None, px=None): """ Cost function for line fitting pp (list or array): line parameters imx (numpy array): image to fit to istep (float) px (array): translational offset to operate from """ istepmodel = .5 samplesize = [ int(imx.shape[1] * istep / istepmodel), int(imx.shape[0] * istep / istepmodel) ] LW = 2 # [mV] LL = 15 # [mV] H = costFunctionLine.scaling0.copy() H[0, 0] = istep / istepmodel H[1, 1] = istep / istepmodel #patch=linetools.sampleImage(im, pp, samplesize, fig=11, clearfig=True, nrsub=1) dsize = (samplesize[0], samplesize[1]) patch = cv2.warpPerspective(imx.astype(np.float32), H, dsize, None, (cv2.INTER_LINEAR), cv2.BORDER_CONSTANT, -1) pm0 = np.array(pp[0:2]).reshape((1, 2)) / istepmodel # [pixel] if px is None: pxpatch = [patch.shape[1] / 2, patch.shape[0] / 2] else: pxpatch = (float(istep) / istepmodel) * np.array(px) pm = pm0 + pxpatch #modelpatch, cdata=createCross(param, samplesize, centermodel=False, istep=istepmodel, verbose=0) lowv = np.percentile(imx, 1) highv = np.percentile(imx, 95) theta = pp[2] if verbose: print('costFunctionLine: sample line patch: lowv %.1f, highv %.1f' % (lowv, highv)) # print(px) linepatch = lowv + np.zeros((samplesize[1], samplesize[0])) lineSegment(linepatch, pm, theta=pp[2], w=LW / istepmodel, l=LL / istepmodel, H=highv - lowv, ml=-6 / istepmodel) #plt.figure(99); plt.clf(); plt.imshow(lineseg, interpolation='nearest'); plt.colorbar() #plt.figure(99); plt.clf(); plt.imshow(linepatch-lineseg, interpolation='nearest'); plt.colorbar() #plt.figure(99); plt.clf(); plt.imshow(linepatch, interpolation='nearest'); plt.colorbar() dd = patch - (linepatch) cost = np.linalg.norm(dd) cost0 = cost if 1: ddx0 = np.linalg.norm(pm0) # [pixel] ddx = np.linalg.norm(pm0) # [pixel] if verbose: print( 'costFunctionLine: calculate additonal costs: dist %.1f [mV]' % (ddx * istepmodel)) ddx = pmatlab.smoothstep(ddx, dthr / istepmodel, dwidth / istepmodel) if verbose >= 2: print(' ddx: %.3f, thr %.3f' % (ddx, dthr / istepmodel)) cost += 100000 * ddx #cost = sLimits(cost, plocal, pm, maxshift, maxangle) if fig is not None: pmatlab.cfigure(fig) plt.clf() plt.imshow(patch, interpolation='nearest') plt.title('patch: cost %.2f, dist %.1f' % (cost, ddx0 * istep)) plt.colorbar() pm = pm.flatten() #plt.plot(pm0.flatten()[0], pm0.flatten()[1], 'dk', markersize=12, label='initial starting point?') plt.plot(pm[0], pm[1], '.g', markersize=24, label='fitted point') plt.plot(pxpatch[0], pxpatch[1], '.m', markersize=18, label='offset for parameters') qq = np.array( pm.reshape(2, 1) + (LL / istepmodel) * pmatlab.rot2D(theta).dot(np.array([[1, -1], [0, 0]]))) plt.plot(qq[0, :], qq[1, :], '--k', markersize=24, linewidth=2) # print(pm) plt.axis('image') # plt.colorbar() pmatlab.cfigure(fig + 1) plt.clf() plt.imshow(linepatch, interpolation='nearest') plt.title('line patch') plt.plot(px[0], px[1], '.m', markersize=24) plt.axis('image') plt.colorbar() pmatlab.tilefigs([fig, fig + 1]) if verbose >= 2: pmatlab.cfigure(fig + 2) plt.clf() xx = np.arange(0, 20, .1) xxstep = istepmodel * pmatlab.smoothstep( xx / istepmodel, dthr / istepmodel, (1 / dwidth) / istepmodel) plt.plot(xx, xxstep, '.-b', label='distance step') plt.xlabel('Distance [mV]') plt.legend() if verbose: print('costFucntion: cost: base %.2f -> final %.2f' % (cost0, cost)) if verbose >= 2: ww = np.abs(dd).mean(axis=0) print('costFunction: dd %s ' % ww) return cost
def plotPeaks(x, y, peaks, showPeaks=True, plotLabels=False, fig=10, plotScore=False, plotsmooth=True, plothalf=False, plotbottom=False, plotmarker='.-b'): """ Plot detected peaks Args: x (array): independent variable data y (array): dependent variable data peaks (list): list of peaks to plot showPeaks, plotLabels, plotScore, plothalf (bool): plotting options Returns: dictionary: graphics handles """ kk = np.ones(3) / 3. ys = scipy.ndimage.filters.correlate1d(y, kk, mode='nearest') stdY = np.std(y) pgeometry.cfigure(fig) plt.clf() h = plt.plot(x, y, plotmarker) if plotsmooth: plt.plot(x, ys, 'g') labelh = [] first_label = True for jj, peak in enumerate(peaks): if peak['valid']: xpos = peak['x'] ypos = peak['y'] if showPeaks: label = 'peaks' if first_label else None first_label = False plt.plot(xpos, ypos, '.r', markersize=15, label=label) if plotLabels: tp = peak.get('type', 'peak') lbl = '%s %d' % (tp, jj) if plotScore: lbl += ': score %.1f ' % peak['score'] lh = plt.text(xpos, ypos + .05 * stdY, lbl) labelh += [lh] halfhandle = [] if plothalf: first_label = True for peak in peaks: if 'xhalfl' in peak: label = 'peak at half height' if first_label else None first_label = False hh = plt.plot(peak['xhalfl'], peak['yhalfl'], '.m', markersize=12, label=label) halfhandle += [hh[0]] if plotbottom: for peak in peaks: if peak['valid']: plt.plot(peak['xbottoml'], peak['ybottoml'], '.', color=[.5, 1, .5], markersize=12, label='peak bottom left') th = plt.title('Fitted peaks') return dict({ 'linehandle': h, 'title': th, 'labelh': labelh, 'halfhandle': halfhandle })
def analyseGateSweep(dd, fig=None, minthr=None, maxthr=None, verbose=1, drawsmoothed=True, drawmidpoints=True): """ Analyse sweep of a gate for pinch value, low value and high value Args: dd (1D qcodes DataSet): structure containing the scan data minthr, maxthr : float parameters for the algorithm (default: None) Returns: result (dict): dictionary with analysis results """ goodgate = True data = dd XX = None # should be made generic setpoint_name = [x for x in list(data.arrays.keys()) if not x.endswith('amplitude') and getattr(data, x).is_setpoint][0] value_parameter_name = data.default_parameter_name() # e.g. 'amplitude' x = data.arrays[setpoint_name] value = np.array(data.arrays[value_parameter_name]) # detect direction of scan scandirection = np.sign(x[-1] - x[0]) if scandirection < 0: scandirection = 1 x = x[::-1] value = value[::-1] # crude estimate of noise noise = np.nanpercentile(np.abs(np.diff(value)), 50) lowvalue = np.nanpercentile(value, 1) highvalue = np.nanpercentile(value, 90) # sometimes a channel is almost completely closed, then the percentile # approach does not function well ww = value[value >= (lowvalue + highvalue) / 2] highvalue = np.nanpercentile(ww, 90) if verbose >= 2: print('analyseGateSweep: lowvalue %.1f highvalue %.1f' % (lowvalue, highvalue)) d = highvalue - lowvalue vv1 = value > (lowvalue + .2 * d) vv2 = value < (lowvalue + .8 * d) midpoint1 = vv1 * vv2 ww = midpoint1.nonzero()[0] midpoint1 = np.mean(x[ww]) # smooth signal kk = np.ones(3) / 3. ww = value for ii in range(4): ww = scipy.ndimage.filters.correlate1d(ww, kk, mode='nearest') # ww=scipy.signal.convolve(ww, kk, mode='same') # ww=scipy.signal.convolve2d(ww, kk, mode='same', boundary='symm') midvalue = .7 * lowvalue + .3 * highvalue if scandirection >= 0: mp = (ww >= (.7 * lowvalue + .3 * highvalue)).nonzero()[0][0] else: mp = (ww >= (.7 * lowvalue + .3 * highvalue)).nonzero()[0][-1] mp = max(mp, 2) # fix for case with zero data signal midpoint2 = x[mp] if verbose >= 2: print('analyseGateSweep: midpoint2 %.1f midpoint1 %.1f' % (midpoint2, midpoint1)) if minthr is not None and np.abs(lowvalue) > minthr: if verbose: print('analyseGateSweep: gate not good: gate is not closed') print(' minthr %s' % minthr) midpoint1 = np.percentile(x, .5) midpoint2 = np.percentile(x, .5) goodgate = False # check for gates that are fully open or closed if scandirection > 0: xleft = x[0:mp] leftval = ww[0:mp] rightval = ww[mp] else: xleft = x[mp:] leftval = ww[mp:] rightval = ww[0:mp] st = ww.std() if verbose>=2: print('analyseGateSweep: leftval %.2f, rightval %.2f' % (leftval.mean(), rightval.mean())) if goodgate and (rightval.mean() - leftval.mean() < .3 * st): if verbose: print( 'analyseGateSweep: gate not good: gate is not closed (or fully closed)') midpoint1 = np.percentile(x, .5) midpoint2 = np.percentile(x, .5) goodgate = False # fit a polynomial to the left side if goodgate and leftval.size > 5: # TODO: make this a robust fit fit = np.polyfit(xleft, leftval, 1) pp = np.polyval(fit, xleft) # pmid = np.polyval(fit, midpoint2) p0 = np.polyval(fit, xleft[-1]) pmid = np.polyval(fit, xleft[0]) if verbose>=2: print('analyseGateSweep: p0 %.1f, pmid %.1f, leftval[0] %.1f' % (p0, pmid, leftval[0])) if pmid + (pmid - p0) * .25 > leftval[0]: midpoint1 = np.percentile(x, .5) midpoint2 = np.percentile(x, .5) goodgate = False if verbose: print( 'analyseGateSweep: gate not good: gate is not closed (or fully closed) (line fit check)') # another check on closed gates if scandirection > 0: leftidx = range(0, mp) fitleft = np.polyfit( [x[leftidx[0]], x[leftidx[-1]]], [lowvalue, value[leftidx[-1]]], 1) else: leftidx = range(mp, value.shape[0]) fitleft = np.polyfit( [x[leftidx[-1]], x[leftidx[0]]], [lowvalue, value[leftidx[0]]], 1) leftval = value[leftidx] leftpred = np.polyval(fitleft, x[leftidx]) if np.abs(scandirection * (xleft[1] - xleft[0])) > 150 and xleft.size > 15: xleft0 = x[mp + 6:] leftval0 = ww[mp + 6:] fitL = np.polyfit(xleft0, leftval0, 1) pp = np.polyval(fitL, xleft0) nd = fitL[0] / (highvalue - lowvalue) if goodgate and (nd * 750 > 1): midpoint1 = np.percentile(x, .5) midpoint2 = np.percentile(x, .5) goodgate = False if verbose: print('analyseGateSweep: gate not good: gate is not closed (or fully closed) (slope check)') pass if np.mean(leftval - leftpred) > noise: midpoint1 = np.percentile(x, .5) midpoint2 = np.percentile(x, .5) goodgate = False if verbose: print( 'analyseGateSweep: gate not good: gate is not closed (or fully closed) (left region check)') pass if fig is not None: cfigure(fig) plt.clf() plt.plot(x, value, '.-b', linewidth=2) plt.xlabel('Sweep %s [mV]' % setpoint_name, fontsize=14) plt.ylabel('keithley [pA]', fontsize=14) if drawsmoothed: plt.plot(x, ww, '-g', linewidth=1) plot2Dline([0, -1, lowvalue], '--m', label='low value') plot2Dline([0, -1, highvalue], '--m', label='high value') if drawmidpoints: if verbose >= 2: plot2Dline([-1, 0, midpoint1], '--g', linewidth=1) plot2Dline([-1, 0, midpoint2], '--m', linewidth=2) if verbose >= 2: plt.plot(x[leftidx], leftpred, '--r', markersize=15, linewidth=1, label='leftpred') plt.plot(x[leftidx], leftval, '--m', markersize=15, linewidth=1, label='leftval') adata = dict({'description': 'pinchoff analysis', 'pinchvalue': 'use pinchoff_point instead', '_pinchvalueX': midpoint1 - 50, 'goodgate': goodgate}) adata['lowvalue'] = lowvalue adata['highvalue'] = highvalue adata['xlabel'] = 'Sweep %s [mV]' % setpoint_name adata['pinchoff_point'] = midpoint2 - 50 pinchoff_index = np.interp(-70.5, x, np.arange(x.size) ) adata['pinchoff_value'] = value[int(pinchoff_index)] adata['midpoint'] = float(midpoint2) adata['midvalue'] = midvalue adata['dataset']=dd.location adata['type']='gatesweep' if verbose>=1: print('analyseGateSweep: pinch-off point %.3f, value %.3f' % (adata['midpoint'], adata['midvalue']) ) if verbose >= 2: print('analyseGateSweep: gate status %d: pinchvalue %.1f' % (goodgate, adata['pinchoff_point'])) adata['Xsmooth'] = ww adata['XX'] = XX adata['X'] = value adata['x'] = x adata['ww'] = ww adata['_mp'] = mp return adata