def pc_plot_extcenwid(tempcen, cenwid, binval, plotsdir="Plots", pcatype="<unknown>", maxp=25, prefix=""): """ Saves a few output png files of the PCA analysis for the target centroid/width definition """ npc = tempcen.shape[1] pages, npp = get_dimen(npc,maxp=maxp) x0=binval*np.arange(cenwid.shape[0]) # First calculate the min and max values for the plotting axes, to make sure they are all the same ymin, ymax = None, None """ for i in xrange(npc): w = np.where(tempcen[:,i]!=-999999.9) if np.size(w[0]) == 0: continue medv = np.median(tempcen[:,i][w]) madv = 1.4826*np.median(np.abs(medv-tempcen[:,i][w])) vmin, vmax = medv-3.0*madv, medv+3.0*madv tymin = min(vmin,np.min(cenwid[:,i])) tymax = max(vmax,np.max(cenwid[:,i])) if ymin is None: ymin = tymin else: if tymin < ymin: ymin = tymin if ymax is None: ymax = tymax else: if tymax > ymax: ymax = tymax """ w = np.where(tempcen!=-999999.9) if np.size(w[0]) != 0: medv = np.median(tempcen[w]) madv = 1.4826*np.median(np.abs(medv-tempcen[w])) vmin, vmax = medv-3.0*madv, medv+3.0*madv tymin = min(vmin,np.min(cenwid)) tymax = max(vmax,np.max(cenwid)) if ymin is None: ymin = tymin else: if tymin < ymin: ymin = tymin if ymax is None: ymax = tymax else: if tymax > ymax: ymax = tymax # Check that ymin and ymax are set, if not, return without plotting if ymin is None or ymax is None: msgs.warn("{0:s} fits were not plotted".format(pcatype)) return # Generate the plots ndone=0 for i in xrange(len(pages)): f, axes = plt.subplots(pages[i][1], pages[i][0]) ipx, ipy = 0, 0 for j in xrange(npp[i]): if pages[i][1] == 1: ind = (ipx) elif pages[i][0] == 1: ind = (ipy) else: ind = (ipy,ipx) w = np.where(tempcen[:,ndone]!=-999999.9) if np.size(w[0]) != 0: rowplt = binval*(0.5 + np.arange(tempcen.shape[0])) axes[ind].plot(rowplt[w],tempcen[:,ndone][w],'bx') axes[ind].plot(x0,cenwid[:,ndone],'r-') axes[ind].axis([0,binval*tempcen.shape[0],ymin,ymax]) axes[ind].set_title("Order {0:d}".format(1+ndone)) ipx += 1 if ipx == pages[i][0]: ipx = 0 ipy += 1 ndone += 1 # Delete the unnecessary axes for j in xrange(npp[i],axes.size): if pages[i][1] == 1: ind = (ipx) elif pages[i][0] == 1: ind = (ipy) else: ind = (ipy,ipx) f.delaxes(axes[ind]) if ipx == pages[i][0]: ipx = 0 ipy += 1 # Save the figure if pages[i][1] == 1 or pages[i][0] == 1: ypngsiz = 11.0/axes.size else: ypngsiz = 11.0*axes.shape[0]/axes.shape[1] f.set_size_inches(11.0, ypngsiz) f.tight_layout() if prefix != "": f.savefig("{0:s}/{1:s}_PCA_{2:s}_page-{3:d}.png".format(plotsdir,prefix,pcatype,i+1), dpi=200, orientation='landscape') else: f.savefig("{0:s}/PCA_{1:s}_page-{2:d}.png".format(plotsdir,pcatype,i+1), dpi=200, orientation='landscape') f.clf() del f return
def pc_plot_extcenwid(tempcen, cenwid, binval, plotsdir="Plots", pcatype="<unknown>", maxp=25, prefix=""): """ Saves a few output png files of the PCA analysis for the target centroid/width definition """ npc = tempcen.shape[1] pages, npp = get_dimen(npc, maxp=maxp) x0 = binval * np.arange(cenwid.shape[0]) # First calculate the min and max values for the plotting axes, to make sure they are all the same ymin, ymax = None, None """ for i in xrange(npc): w = np.where(tempcen[:,i]!=-999999.9) if np.size(w[0]) == 0: continue medv = np.median(tempcen[:,i][w]) madv = 1.4826*np.median(np.abs(medv-tempcen[:,i][w])) vmin, vmax = medv-3.0*madv, medv+3.0*madv tymin = min(vmin,np.min(cenwid[:,i])) tymax = max(vmax,np.max(cenwid[:,i])) if ymin is None: ymin = tymin else: if tymin < ymin: ymin = tymin if ymax is None: ymax = tymax else: if tymax > ymax: ymax = tymax """ w = np.where(tempcen != -999999.9) if np.size(w[0]) != 0: medv = np.median(tempcen[w]) madv = 1.4826 * np.median(np.abs(medv - tempcen[w])) vmin, vmax = medv - 3.0 * madv, medv + 3.0 * madv tymin = min(vmin, np.min(cenwid)) tymax = max(vmax, np.max(cenwid)) if ymin is None: ymin = tymin else: if tymin < ymin: ymin = tymin if ymax is None: ymax = tymax else: if tymax > ymax: ymax = tymax # Check that ymin and ymax are set, if not, return without plotting if ymin is None or ymax is None: msgs.warn("{0:s} fits were not plotted".format(pcatype)) return # Generate the plots ndone = 0 for i in xrange(len(pages)): f, axes = plt.subplots(pages[i][1], pages[i][0]) ipx, ipy = 0, 0 for j in xrange(npp[i]): if pages[i][1] == 1: ind = (ipx) elif pages[i][0] == 1: ind = (ipy) else: ind = (ipy, ipx) w = np.where(tempcen[:, ndone] != -999999.9) if np.size(w[0]) != 0: rowplt = binval * (0.5 + np.arange(tempcen.shape[0])) axes[ind].plot(rowplt[w], tempcen[:, ndone][w], 'bx') axes[ind].plot(x0, cenwid[:, ndone], 'r-') axes[ind].axis([0, binval * tempcen.shape[0], ymin, ymax]) axes[ind].set_title("Order {0:d}".format(1 + ndone)) ipx += 1 if ipx == pages[i][0]: ipx = 0 ipy += 1 ndone += 1 # Delete the unnecessary axes for j in xrange(npp[i], axes.size): if pages[i][1] == 1: ind = (ipx) elif pages[i][0] == 1: ind = (ipy) else: ind = (ipy, ipx) f.delaxes(axes[ind]) if ipx == pages[i][0]: ipx = 0 ipy += 1 # Save the figure if pages[i][1] == 1 or pages[i][0] == 1: ypngsiz = 11.0 / axes.size else: ypngsiz = 11.0 * axes.shape[0] / axes.shape[1] f.set_size_inches(11.0, ypngsiz) f.tight_layout() if prefix != "": f.savefig("{0:s}/{1:s}_PCA_{2:s}_page-{3:d}.png".format( plotsdir, prefix, pcatype, i + 1), dpi=200, orientation='landscape') else: f.savefig("{0:s}/PCA_{1:s}_page-{2:d}.png".format( plotsdir, pcatype, i + 1), dpi=200, orientation='landscape') f.clf() del f return
def pc_plot(slf, inpar, ofit, maxp=25, pcadesc="", addOne=True): """ Saves quality control plots for a PCA analysis """ npc = inpar['npc']+1 pages, npp = get_dimen(npc, maxp=maxp) x0 = inpar['x0'] ordernum = inpar['x0in'] x0fit = inpar['x0fit'] usetrc = inpar['usetrc'] hidden = inpar['hidden'] high_fit = inpar['high_fit'] nc = np.max(ordernum[usetrc]) # Loop through all pages and plot the results ndone = 0 for i in xrange(len(pages)): plt.clf() f, axes = plt.subplots(pages[i][1], pages[i][0]) ipx, ipy = 0, 0 if i == 0: if pages[i][1] == 1: ind = (0,) elif pages[i][0] == 1: ind = (0,) else: ind = (0,0) axes[ind].plot(ordernum[usetrc], x0[usetrc], 'bx') axes[ind].plot(ordernum, x0fit, 'k-') amn, amx = np.min(x0fit), np.max(x0fit) diff = x0[usetrc]-x0fit[usetrc] tdiffv = np.median(diff) mdiffv = 1.4826*np.median(np.abs(tdiffv-diff)) amn -= 2.0*mdiffv amx += 2.0*mdiffv mval = amn-0.15*(amx-amn) dmin, dmax = tdiffv-2.0*mdiffv, tdiffv+2.0*mdiffv diff = mval + diff*0.20*(amx-amn)/(dmax-dmin) wign = np.where(np.abs(diff-np.median(diff))<4.0*1.4826*np.median(np.abs(diff-np.median(diff))))[0] dmin, dmax = np.min(diff[wign]), np.max(diff[wign]) axes[ind].plot(ordernum[usetrc], diff, 'rx') if addOne: axes[ind].plot([0, nc+1], [mval,mval], 'k-') axes[ind].axis([0, nc+1, dmin-0.5*(dmax-dmin), amx + 0.05*(amx-amn)]) else: axes[ind].plot([0, nc], [mval, mval], 'k-') axes[ind].axis([0, nc, dmin-0.5*(dmax-dmin), amx + 0.05*(amx-amn)]) axes[ind].set_title("Mean Value") ipx += 1 if ipx == pages[i][0]: ipx = 0 ipy += 1 npp[0] -= 1 for j in xrange(npp[i]): if pages[i][1] == 1: ind = (ipx,) elif pages[i][0] == 1: ind = (ipy,) else: ind = (ipy, ipx) axes[ind].plot(ordernum[usetrc], hidden[j+ndone,:], 'bx') axes[ind].plot(ordernum, high_fit[:,j+ndone], 'k-') vmin, vmax = np.min(hidden[j+ndone,:]), np.max(hidden[j+ndone,:]) if ofit[1+j+ndone] != -1: cmn, cmx = np.min(high_fit[:,j+ndone]), np.max(high_fit[:,j+ndone]) diff = hidden[j+ndone,:]-high_fit[:,j+ndone][usetrc] tdiffv = np.median(diff) mdiffv = 1.4826*np.median(np.abs(tdiffv-diff)) cmn -= 2.0*mdiffv cmx += 2.0*mdiffv mval = cmn-0.15*(cmx-cmn) dmin, dmax = tdiffv-2.0*mdiffv, tdiffv+2.0*mdiffv #dmin, dmax = np.min(diff), np.max(diff) diff = mval + diff*0.20*(cmx-cmn)/(dmax-dmin) wign = np.where(np.abs(diff-np.median(diff))<4.0*1.4826*np.median(np.abs(diff-np.median(diff))))[0] dmin, dmax = np.min(diff[wign]), np.max(diff[wign]) #vmin, vmax = np.min(hidden[j+ndone,:][wign]), np.max(hidden[j+ndone,:][wign]) axes[ind].plot(ordernum[usetrc], diff, 'rx') axes[ind].plot([0, 1+nc], [mval, mval], 'k-') # ymin = np.min([(3.0*dmin-dmax)/2.0,vmin-0.1*(vmax-dmin),dmin-0.1*(vmax-dmin)]) # ymax = np.max([np.max(high_fit[:,j+ndone]),vmax+0.1*(vmax-dmin),dmax+0.1*(vmax-dmin)]) ymin = dmin-0.5*(dmax-dmin) ymax = cmx + 0.05*(cmx-cmn) if addOne: axes[ind].axis([0, nc+1, ymin, ymax]) else: axes[ind].axis([0, nc, ymin, ymax]) else: if addOne: axes[ind].axis([0, nc+1, vmin-0.1*(vmax-vmin), vmax+0.1*(vmax-vmin)]) else: axes[ind].axis([0, nc, vmin-0.1*(vmax-vmin), vmax+0.1*(vmax-vmin)]) axes[ind].set_title("PC {0:d}".format(j+ndone)) ipx += 1 if ipx == pages[i][0]: ipx = 0 ipy += 1 if i == 0: npp[0] = npp[0] + 1 # Delete the unnecessary axes for j in xrange(npp[i], axes.size): if pages[i][1] == 1: ind = (ipx,) elif pages[i][0] == 1: ind = (ipy,) else: ind = (ipy, ipx) f.delaxes(axes[ind]) if ipx == pages[i][0]: ipx = 0 ipy += 1 ndone += npp[i] # Save the figure if pages[i][1] == 1 or pages[i][0] == 1: ypngsiz = 11.0/axes.size else: ypngsiz = 11.0*axes.shape[0]/axes.shape[1] f.set_size_inches(11.0, ypngsiz) if pcadesc != "": pgtxt = "" if len(pages) != 1: pgtxt = ", page {0:d}/{1:d}".format(i+1, len(pages)) f.suptitle(pcadesc + pgtxt, y=1.02, size=16) f.tight_layout() slf._qa.savefig(dpi=200, orientation='landscape', bbox_inches='tight') plt.close() f.clf() del f return
def pc_plot(slf, inpar, ofit, maxp=25, pcadesc="", addOne=True): """ Saves quality control plots for a PCA analysis """ npc = inpar['npc'] + 1 pages, npp = get_dimen(npc, maxp=maxp) x0 = inpar['x0'] ordernum = inpar['x0in'] x0fit = inpar['x0fit'] usetrc = inpar['usetrc'] hidden = inpar['hidden'] high_fit = inpar['high_fit'] nc = np.max(ordernum[usetrc]) # Loop through all pages and plot the results ndone = 0 for i in xrange(len(pages)): plt.clf() f, axes = plt.subplots(pages[i][1], pages[i][0]) ipx, ipy = 0, 0 if i == 0: if pages[i][1] == 1: ind = (0, ) elif pages[i][0] == 1: ind = (0, ) else: ind = (0, 0) axes[ind].plot(ordernum[usetrc], x0[usetrc], 'bx') axes[ind].plot(ordernum, x0fit, 'k-') amn, amx = np.min(x0fit), np.max(x0fit) diff = x0[usetrc] - x0fit[usetrc] tdiffv = np.median(diff) mdiffv = 1.4826 * np.median(np.abs(tdiffv - diff)) amn -= 2.0 * mdiffv amx += 2.0 * mdiffv mval = amn - 0.15 * (amx - amn) dmin, dmax = tdiffv - 2.0 * mdiffv, tdiffv + 2.0 * mdiffv diff = mval + diff * 0.20 * (amx - amn) / (dmax - dmin) wign = np.where( np.abs(diff - np.median(diff)) < 4.0 * 1.4826 * np.median(np.abs(diff - np.median(diff))))[0] dmin, dmax = np.min(diff[wign]), np.max(diff[wign]) axes[ind].plot(ordernum[usetrc], diff, 'rx') if addOne: axes[ind].plot([0, nc + 1], [mval, mval], 'k-') axes[ind].axis([ 0, nc + 1, dmin - 0.5 * (dmax - dmin), amx + 0.05 * (amx - amn) ]) else: axes[ind].plot([0, nc], [mval, mval], 'k-') axes[ind].axis([ 0, nc, dmin - 0.5 * (dmax - dmin), amx + 0.05 * (amx - amn) ]) axes[ind].set_title("Mean Value") ipx += 1 if ipx == pages[i][0]: ipx = 0 ipy += 1 npp[0] -= 1 for j in xrange(npp[i]): if pages[i][1] == 1: ind = (ipx, ) elif pages[i][0] == 1: ind = (ipy, ) else: ind = (ipy, ipx) axes[ind].plot(ordernum[usetrc], hidden[j + ndone, :], 'bx') axes[ind].plot(ordernum, high_fit[:, j + ndone], 'k-') vmin, vmax = np.min(hidden[j + ndone, :]), np.max(hidden[j + ndone, :]) if ofit[1 + j + ndone] != -1: cmn, cmx = np.min(high_fit[:, j + ndone]), np.max( high_fit[:, j + ndone]) diff = hidden[j + ndone, :] - high_fit[:, j + ndone][usetrc] tdiffv = np.median(diff) mdiffv = 1.4826 * np.median(np.abs(tdiffv - diff)) cmn -= 2.0 * mdiffv cmx += 2.0 * mdiffv mval = cmn - 0.15 * (cmx - cmn) dmin, dmax = tdiffv - 2.0 * mdiffv, tdiffv + 2.0 * mdiffv #dmin, dmax = np.min(diff), np.max(diff) diff = mval + diff * 0.20 * (cmx - cmn) / (dmax - dmin) wign = np.where( np.abs(diff - np.median(diff)) < 4.0 * 1.4826 * np.median(np.abs(diff - np.median(diff))))[0] dmin, dmax = np.min(diff[wign]), np.max(diff[wign]) #vmin, vmax = np.min(hidden[j+ndone,:][wign]), np.max(hidden[j+ndone,:][wign]) axes[ind].plot(ordernum[usetrc], diff, 'rx') axes[ind].plot([0, 1 + nc], [mval, mval], 'k-') # ymin = np.min([(3.0*dmin-dmax)/2.0,vmin-0.1*(vmax-dmin),dmin-0.1*(vmax-dmin)]) # ymax = np.max([np.max(high_fit[:,j+ndone]),vmax+0.1*(vmax-dmin),dmax+0.1*(vmax-dmin)]) ymin = dmin - 0.5 * (dmax - dmin) ymax = cmx + 0.05 * (cmx - cmn) if addOne: axes[ind].axis([0, nc + 1, ymin, ymax]) else: axes[ind].axis([0, nc, ymin, ymax]) else: if addOne: axes[ind].axis([ 0, nc + 1, vmin - 0.1 * (vmax - vmin), vmax + 0.1 * (vmax - vmin) ]) else: axes[ind].axis([ 0, nc, vmin - 0.1 * (vmax - vmin), vmax + 0.1 * (vmax - vmin) ]) axes[ind].set_title("PC {0:d}".format(j + ndone)) ipx += 1 if ipx == pages[i][0]: ipx = 0 ipy += 1 if i == 0: npp[0] = npp[0] + 1 # Delete the unnecessary axes for j in xrange(npp[i], axes.size): if pages[i][1] == 1: ind = (ipx, ) elif pages[i][0] == 1: ind = (ipy, ) else: ind = (ipy, ipx) f.delaxes(axes[ind]) if ipx == pages[i][0]: ipx = 0 ipy += 1 ndone += npp[i] # Save the figure if pages[i][1] == 1 or pages[i][0] == 1: ypngsiz = 11.0 / axes.size else: ypngsiz = 11.0 * axes.shape[0] / axes.shape[1] f.set_size_inches(11.0, ypngsiz) if pcadesc != "": pgtxt = "" if len(pages) != 1: pgtxt = ", page {0:d}/{1:d}".format(i + 1, len(pages)) f.suptitle(pcadesc + pgtxt, y=1.02, size=16) f.tight_layout() slf._qa.savefig(dpi=200, orientation='landscape', bbox_inches='tight') plt.close() f.clf() del f return