Changeset 3313


Ignore:
Timestamp:
Mar 9, 2018 1:08:30 PM (4 years ago)
Author:
toby
Message:

fix non-display of excluded regions; option to export plot as csv

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/GSASIIplot.py

    r3304 r3313  
    23952395        LimitId = 0
    23962396        if Pattern[1] is None: continue # skip over uncomputed simulations
    2397         xye = np.array(ma.getdata(Pattern[1]))
     2397        xye = np.array(ma.getdata(Pattern[1])) # strips mask
     2398        xye0 = Pattern[1][0]  # keeps mask
    23982399        bxye = G2pdG.GetFileBackground(G2frame,xye,Pattern)
    23992400        if PickId:
     
    24012402            LimitId = G2gd.GetGPXtreeItemId(G2frame,G2frame.PatternId,'Limits')
    24022403            limits = G2frame.GPXtree.GetItemPyData(LimitId)
     2404            # recompute mask from excluded regions, in case they have changed
    24032405            excls = limits[2:]
     2406            xye0.mask = False # resets mask for xye0 & Pattern[1][0](!)
    24042407            for excl in excls:
    2405                 xye[0] = ma.masked_inside(xye[0],excl[0],excl[1])
     2408                xye0 = ma.masked_inside(xye0,excl[0],excl[1]) # sets mask on xye0 but not Pattern[1][0] !
     2409            Pattern[1][0].mask = xye0.mask # transfer the mask
    24062410        if G2frame.plotStyle['qPlot'] and 'PWDR' in plottype:
    2407             X = 2.*np.pi/G2lat.Pos2dsp(Parms,xye[0])
     2411            X = 2.*np.pi/G2lat.Pos2dsp(Parms,xye0)
    24082412        elif G2frame.plotStyle['dPlot'] and 'PWDR' in plottype:
    2409             X = G2lat.Pos2dsp(Parms,xye[0])
     2413            X = G2lat.Pos2dsp(Parms,xye0)
    24102414        else:
    2411             X = xye[0]
     2415            X = xye0
    24122416        if not lenX:
    24132417            lenX = len(X)
     
    25152519                DX = xlim[1]-xlim[0]
    25162520                X += 0.002*offsetX*DX*N
    2517             Xum = ma.getdata(X)
     2521            Xum = ma.getdata(X) # unmasked version of X, use to plot data (only)
    25182522            if ifpicked:
    25192523                if G2frame.plotStyle['sqrtPlot']:
     
    27952799        figure.clear()
    27962800        plotOpt['fmtChoices'] = [fmtDict[j]+', '+j for j in sorted(fmtDict)]
     2801        plotOpt['fmtChoices'].append('Data file with plot elements, csv')
    27972802        if plotOpt['format'] is None:
    27982803            if 'pdf' in fmtDict:
     
    28342839        CopyRietveldPlot(G2frame,Pattern,Plot,Page,figure)
    28352840        figure.canvas.draw()
     2841       
     2842    def Write2csv(fil,dataItems,header=False):
     2843        '''Write a line to a CSV file
     2844
     2845        :param object fil: file object
     2846        :param list dataItems: items to write as row in file
     2847        :param bool header: True if all items should be written with quotes (default is False)
     2848        '''
     2849        line = ''
     2850        for item in dataItems:
     2851            if line: line += ','
     2852            item = str(item)
     2853            if header or ' ' in item:
     2854                line += '"'+item+'"'
     2855            else:
     2856                line += item
     2857        fil.write(line+'\n')
     2858
     2859    def CopyRietveld2csv(Pattern,Plot,Page,filename):
     2860        '''Copy the contents of the Rietveld graph from the plot window to
     2861        .csv file
     2862        '''
     2863        import itertools # delay this since not commonly called or needed
     2864
     2865        lblList = []
     2866        valueList = []
     2867
     2868        lblList.append('Axis-limits')
     2869        valueList.append(list(Plot.get_xlim())+list(Plot.get_ylim()))
     2870
     2871        tickpos = {}
     2872        for i,l in enumerate(Plot.lines):
     2873            lbl = l.get_label()
     2874            if 'mag' in lbl:
     2875                pass
     2876            elif lbl[1:] in ('obs','calc','bkg','zero','diff'):
     2877                if lbl[1:] == 'obs':
     2878                    lblList.append('x')
     2879                    valueList.append(l.get_xdata())
     2880                c = plotOpt['colors'].get(lbl[1:],l.get_color())
     2881                if sum(c) == 4.0: continue
     2882                lblList.append(lbl)
     2883                valueList.append(l.get_ydata())
     2884            elif l in Page.tickDict.values():
     2885                c = plotOpt['colors'].get(lbl,l.get_color())
     2886                if sum(c) == 4.0: continue
     2887                tickpos[lbl] = l.get_ydata()[0]
     2888                lblList.append(lbl)
     2889                valueList.append(l.get_xdata())
     2890        if tickpos:
     2891            lblList.append('tick-pos')
     2892            valueList.append([])
     2893            for i in tickpos:
     2894                valueList[-1].append(i)
     2895                valueList[-1].append(tickpos[i])
     2896        # add (obs-calc)/sigma [=(obs-calc)*sqrt(weight)]
     2897        lblList.append('diff/sigma')
     2898        valueList.append(Pattern[1][5]*np.sqrt(Pattern[1][2]))
     2899        if sum(Pattern[1][0].mask): # are there are excluded points? If so, add them too
     2900            lblList.append('excluded')
     2901            valueList.append(1*Pattern[1][0].mask)
     2902        # magnifcation values
     2903        for l in Plot.texts:
     2904            lbl = l.get_label()
     2905            if 'mag' not in lbl: continue
     2906            if l.xycoords == 'axes fraction':
     2907                lbl = 'initial-mag'
     2908                lblList.append(lbl)
     2909                valueList.append([l.get_text()])
     2910            else:
     2911                lbl = 'mag'
     2912                lblList.append(lbl)
     2913                valueList.append([l.get_text(),l.get_position()[0]])
     2914        # invert lists into columns, use iterator for all values
     2915        if hasattr(itertools,'zip_longest'): #Python 3+
     2916            invertIter = itertools.zip_longest(*valueList,fillvalue=' ')
     2917        else:
     2918            invertIter = itertools.izip_longest(*valueList,fillvalue=' ')
     2919        fp = open(filename,'w')
     2920        Write2csv(fp,lblList,header=True)
     2921        for row in invertIter:
     2922            Write2csv(fp,row)
     2923        fp.close()
     2924       
    28362925    def onSave(event):
    28372926        '''Write the current plot to a file
     
    28402929        hccanvas = hcCanvas(hcfigure)
    28412930        CopyRietveldPlot(G2frame,Pattern,Plot,Page,hcfigure)
    2842         longFormatName,type = plotOpt['format'].split(',')
    2843         fil = G2G.askSaveFile(G2frame,'','.'+type.strip(),longFormatName)
    2844         if fil:
    2845             hcfigure.savefig(fil,format=type.strip())
     2931        longFormatName,typ = plotOpt['format'].split(',')
     2932        fil = G2G.askSaveFile(G2frame,'','.'+typ.strip(),longFormatName)
     2933        if 'csv' in typ:
     2934            CopyRietveld2csv(Pattern,Plot,Page,fil)
    28462935            dlg.EndModal(wx.ID_OK)
     2936        elif fil:
     2937            hcfigure.savefig(fil,format=typ.strip())
     2938            dlg.EndModal(wx.ID_OK)
     2939           
    28472940    def onTextSize(event):
    28482941        '''Respond to a change in text size from the slider
     
    28512944            plotOpt['labelsize'] = event.GetInt()
    28522945            RefreshPlot()
     2946           
    28532947    def OnSelectColour(event):
    28542948        '''Respond to a change in color
Note: See TracChangeset for help on using the changeset viewer.