Skip to content

Commit

Permalink
cleanup plot publish code;CSV now has all Q/d values and mask is incl…
Browse files Browse the repository at this point in the history
…uded; weight now included in (obs-calc)/sigma everywhere; (obs-calc)/sigma bug in #82 not present (anymore?); more work needed -- see #89
  • Loading branch information
briantoby committed Oct 25, 2024
1 parent 06945d3 commit f68b7ac
Showing 1 changed file with 59 additions and 79 deletions.
138 changes: 59 additions & 79 deletions GSASII/GSASIIpwdplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
plotOpt[key] = 0.0
plotOpt[key+'_use'] = False
partialOpts = {} # options for display of partials
savedX = None # contains the X values in plot units for "master" pattern, with mask

#### PlotPatterns ################################################################################
def ReplotPattern(G2frame,newPlot,plotType,PatternName=None,PickName=None):
Expand Down Expand Up @@ -176,7 +177,7 @@ def OnPlotKeyPress(event):
G2frame.G2plotNB.status.SetStatusText('Select '+plottype+' pattern first',1)
return
newPlot = False
if event.key == 'w': # and not Page.plotStyle['qPlot'] and not Page.plotStyle['dPlot']: #can't do weight plots when x-axis is different
if event.key == 'w':
G2frame.Weight = not G2frame.Weight
if not G2frame.Weight and not G2frame.Contour and 'PWDR' in plottype:
G2frame.SinglePlot = True
Expand Down Expand Up @@ -1453,6 +1454,7 @@ def onPartialConfig(event):
global DifLine # BHT: probably does not need to be global
global Ymax
global Pattern,mcolors,Plot,Page,imgAx,Temps
global savedX
plottype = plotType

# get powder pattern colors from config settings
Expand Down Expand Up @@ -1941,14 +1943,18 @@ def onPartialConfig(event):
Lmask = Emask = np.full(len(xye0),False)

if G2frame.Contour:
xye0 = xye[0] # drop mask

xye0 = xye[0] # drop mask for contouring

# convert all X values and then reapply mask
if Page.plotStyle['qPlot'] and 'PWDR' in plottype and not ifLimits:
X = 2.*np.pi/G2lat.Pos2dsp(Parms,xye0)
X = ma.array(2.*np.pi/G2lat.Pos2dsp(Parms,xye0.data),mask=xye0.mask)
elif Page.plotStyle['dPlot'] and 'PWDR' in plottype and not ifLimits:
X = G2lat.Pos2dsp(Parms,xye0)
X = ma.array(G2lat.Pos2dsp(Parms,xye0.data),mask=xye0.mask)
else:
X = copy.deepcopy(xye0)
if ifpicked and not G2frame.Contour:
savedX = copy.deepcopy(X)

if not lenX:
lenX = len(X)
# show plot magnification factors
Expand Down Expand Up @@ -2076,7 +2082,7 @@ def onPartialConfig(event):
elif ContourX is None:
ContourX = X
Nseq += 1
else:
else: # not contour plot
if not G2frame.plusPlot:
pP = ''
lW = 1.5
Expand All @@ -2096,7 +2102,7 @@ def onPartialConfig(event):
Xum = ma.getdata(X) # unmasked version of X, use for data limits (only)
else:
Xum = X[:]
if ifpicked:
if ifpicked: # plotting of "master" pattern (histogram selected in data tree)
ZI = copy.copy(xye[3]) #Yc
if Page.plotStyle['sqrtPlot']:
olderr = np.seterr(invalid='ignore') #get around sqrt(-ve) error
Expand Down Expand Up @@ -2875,7 +2881,6 @@ def ClosestColorNumber(color):
mkwid = l.get_mew()
glinetyp = 1
if lbl == 'obs':
obsartist = l
gsiz = float(plotOpt['markerSiz'])/8.
marker = plotOpt['markerSym']
gmw = float(plotOpt['markerWid'])
Expand Down Expand Up @@ -2933,11 +2938,9 @@ def ClosestColorNumber(color):
fp.write(linedef2.format("s"+str(datnum),'',gc,mkwid))
#======================================================================
# Start (obs-cal)/sigma plot
rsig = np.sqrt(Pattern[1][2])
rsig[rsig>1] = 1
wtFactor = Pattern[0]['wtFactor']
ysig = (Pattern[1][1]-Pattern[1][3])*np.sqrt(wtFactor*Pattern[1][2])
fp.write("@type xy\n")
l = obsartist
ysig = Pattern[1][5]*rsig
# scaling for bottom box
fp.write('@g{0} hidden false\n@with g{0}\n@legend {1}\n'.format(1,"off"))
fp.write('@world xmin {}\n@world xmax {}\n'.format(Plot.get_xlim()[0],Plot.get_xlim()[1]))
Expand Down Expand Up @@ -2973,16 +2976,9 @@ def ClosestColorNumber(color):
ytick = yti * 10**int(np.log10(ytick/yti)+.5)
fp.write('@{}axis tick major {}\n'.format('x',xticks[1]-xticks[0]))
fp.write('@{}axis tick major {}\n'.format('y',ytick))
rsig = np.sqrt(Pattern[1][2])
rsig[rsig>1] = 1
fp.write("@type xy\n")
l = obsartist
if ma.any(l.get_xdata().mask):
for x,y,m in zip(l.get_xdata(),Pattern[1][5]*rsig,l.get_xdata().mask):
if not m: fp.write("{} {}\n".format(x,y))
else:
for x,y in zip(l.get_xdata(),Pattern[1][5]*rsig):
fp.write("{} {}\n".format(x,y))
for x,y,m in zip(savedX,ysig,savedX.mask):
if not m: fp.write("{} {}\n".format(x,y))
fp.write("&\n")
fp.write(linedef3.format("s1",'',1,0,1.0,0,0,1))
fp.close()
Expand Down Expand Up @@ -3060,12 +3056,9 @@ def increase_line_width(plot):
tickpos = {}

for i,l in enumerate(Plot.lines):
if l.get_label() in ('obs','calc','bkg','zero','diff'):
lbl = l.get_label()
elif l.get_label()[1:] in ('obs','calc','bkg','zero','diff'):
lbl = l.get_label()[1:]
else:
lbl = l.get_label()
lbl = l.get_label()
if lbl[1:] in ('obs','calc','bkg','zero','diff'):
lbl = lbl[1:]
if 'magline' in lbl:
pass
elif lbl in ('obs','calc','bkg','zero','diff'):
Expand Down Expand Up @@ -3106,11 +3099,6 @@ def increase_line_width(plot):
lbl = 'mag'
lblList.append(lbl)
valueList.append([l.get_text(),l.get_position()[0]])
# invert lists into columns, use iterator for all values
#if hasattr(itertools,'zip_longest'): #Python 3+
# invertIter = itertools.zip_longest(*valueList,fillvalue=' ')
#else:
# invertIter = itertools.izip_longest(*valueList,fillvalue=' ')

# Start Origin instance
if op and op.oext:
Expand Down Expand Up @@ -3229,22 +3217,22 @@ def CopyRietveld2Igor(Pattern,Plot,Page,plotOpt,filename,G2frame):
igor_symbols = {"o":19, "s":18, "D":29, "^":17, "3":46, 'v':23,
"4":49, "+":1, "P":60, "x":0, "X":62, "*":2}
def Write2cols(fil,dataItems):
'''Write a line to a file in space-separated columns.
Skips masked items.
'''Write a line to a file in space-separated columns.
Skips masked items.
:param object fil: file object
:param list dataItems: items to write as row in file
'''
line = ''
for item in dataItems:
if ma.is_masked(item): return
if line: line += ' '
item = str(item)
if ' ' in item:
line += '"'+item+'"'
else:
line += item
fil.write(line+'\n')
:param object fil: file object
:param list dataItems: items to write as row in file
'''
line = ''
for item in dataItems:
if ma.is_masked(item): return
if line: line += ' '
item = str(item)
if ' ' in item:
line += '"'+item+'"'
else:
line += item
fil.write(line+'\n')
proj = os.path.splitext(G2frame.GSASprojectfile)[0]
if not proj: proj = 'GSASIIproject'
proj = proj.replace(' ','')
Expand Down Expand Up @@ -3285,11 +3273,6 @@ def Write2cols(fil,dataItems):
if lbl != 'zero':
valueList.append(l.get_ydata())
valueList.append(Pattern[1][5]*np.sqrt(Pattern[1][2]))
# invert lists into columns, use iterator for all values
if hasattr(itertools,'zip_longest'): #Python 3+
invertIter = itertools.zip_longest(*valueList,fillvalue=' ')
else:
invertIter = itertools.izip_longest(*valueList,fillvalue=' ')
fp = open(filename,'w')
fp.write('''IGOR
X setDataFolder root:
Expand All @@ -3301,7 +3284,8 @@ def Write2cols(fil,dataItems):
WAVES /D/O TwoTheta, Intensity, FitIntensity, Background, Difference, NormResidual
BEGIN
'''.format(proj))
for row in invertIter:
# invert lists into columns, use iterator for all values
for row in itertools.zip_longest(*valueList,fillvalue=' '):
Write2cols(fp,row)
fp.write('''END
X // *** static part of the code, NB reflection tickmarks later ****
Expand Down Expand Up @@ -3429,7 +3413,8 @@ def Write2cols(fil,dataItems):
s += r'\\s({}) {}'.format(nam,txt)
fp.write('X Legend/C/N=text0/J "{}"\n'.format(s))
fp.close()

print('file',filename,'written')

def CopyRietveld2csv(Pattern,Plot,Page,filename):
'''Copy the contents of the Rietveld graph from the plot window to
.csv file
Expand All @@ -3438,24 +3423,20 @@ def CopyRietveld2csv(Pattern,Plot,Page,filename):

lblList = []
valueList = []

lblList.append('Axis-limits')
valueList.append(list(Plot.get_xlim())+list(Plot.get_ylim()))

tickpos = {}
lblList.append('used')
valueList.append([0 if i else 1 for i in savedX.mask])
lblList.append('x')
valueList.append(savedX.data)
for i,l in enumerate(Plot.lines):
if l.get_label() in ('obs','calc','bkg','zero','diff'):
lbl = l.get_label()
elif l.get_label()[1:] in ('obs','calc','bkg','zero','diff'):
lbl = l.get_label()
if lbl[1:] in ('obs','calc','bkg','zero','diff'):
lbl = l.get_label()[1:]
else:
lbl = l.get_label()
if 'magline' in lbl:
pass
elif lbl in ('obs','calc','bkg','zero','diff'):
if lbl == 'obs':
lblList.append('x')
valueList.append(l.get_xdata())
continue
c = plotOpt['colors'].get(lbl,mpl.colors.to_rgba(l.get_color()))
if sum(c) == 4.0: continue
lblList.append(lbl)
Expand All @@ -3474,11 +3455,16 @@ def CopyRietveld2csv(Pattern,Plot,Page,filename):
valueList[-1].append(tickpos[i])
# add (obs-calc)/sigma [=(obs-calc)*sqrt(weight)]
lblList.append('diff/sigma')
valueList.append(Pattern[1][5]*np.sqrt(Pattern[1][2]))
wtFactor = Pattern[0]['wtFactor']
DZ = (Pattern[1][1]-Pattern[1][3])*np.sqrt(wtFactor*Pattern[1][2])
valueList.append(DZ)

if sum(Pattern[1][0].mask): # are there are excluded points? If so, add them too
lblList.append('excluded')
valueList.append(1*Pattern[1][0].mask)
# magnifcation values
lblList.append('Axis-limits')
valueList.append(list(Plot.get_xlim())+list(Plot.get_ylim()))
# magnification values
for l in Plot.texts:
lbl = l.get_label()
if 'magline' not in lbl: continue
Expand All @@ -3490,14 +3476,10 @@ def CopyRietveld2csv(Pattern,Plot,Page,filename):
lbl = 'mag'
lblList.append(lbl)
valueList.append([l.get_text(),l.get_position()[0]])
# invert lists into columns, use iterator for all values
if hasattr(itertools,'zip_longest'): #Python 3+
invertIter = itertools.zip_longest(*valueList,fillvalue=' ')
else:
invertIter = itertools.izip_longest(*valueList,fillvalue=' ')
fp = open(filename,'w')
G2plt.Write2csv(fp,lblList,header=True)
for row in invertIter:
# invert lists into columns, use iterator for all values
for row in itertools.zip_longest(*valueList,fillvalue=' '):
G2plt.Write2csv(fp,row)
fp.close()

Expand Down Expand Up @@ -3800,12 +3782,11 @@ def CopyRietveldPlot(G2frame,Pattern,Plot,Page,figure,phaseList):

legLbl = []
legLine = []
obsartist = None
# get the obs/calc... & magnification lines and xfer them
for i,l in enumerate(Plot.lines):
lbl = l.get_label()
if lbl[1:] in ('obs','calc','bkg','zero','diff'):
lbl = l.get_label()[1:]
lbl = lbl[1:]
if 'magline' in lbl: # magnification lines
ax0.axvline(l.get_data()[0][0],color='0.5',dashes=(1,1))
elif lbl in ('obs','calc','bkg','zero','diff'): # data segments
Expand All @@ -3815,7 +3796,6 @@ def CopyRietveldPlot(G2frame,Pattern,Plot,Page,figure,phaseList):
siz = l.get_markersize()
mew = l.get_mew()
if lbl == 'obs':
obsartist = l
siz = float(plotOpt['markerSiz'])
marker = plotOpt['markerSym']
mew = float(plotOpt['markerWid'])
Expand Down Expand Up @@ -3903,9 +3883,9 @@ def CopyRietveldPlot(G2frame,Pattern,Plot,Page,figure,phaseList):
# print('other text:',l.get_label())

# generate the (obs-calc)/sigma values and plot them
rsig = np.sqrt(Pattern[1][2])
if obsartist:
ax1.plot(obsartist.get_xdata(),Pattern[1][5]*rsig,color='k')
wtFactor = Pattern[0]['wtFactor']
DZ = (Pattern[1][1]-Pattern[1][3])*np.sqrt(wtFactor*Pattern[1][2])
ax1.plot(savedX,DZ,color='k')
# show the legend, if anything is in it (list legLine)
if legLine:
ax0.legend(legLine,legLbl,loc='best',prop={'size':plotOpt['labelSize']})
Expand Down

0 comments on commit f68b7ac

Please sign in to comment.