def filterPareto(scores, dds, sols, verbose=0): """ From a list of designs select only the pareto optimal designs """ pp = oahelper.createPareto(dds, verbose=0) paretoidx = np.array(pp.allindices()) pscores = scores[paretoidx] pdds = dds[paretoidx] psols = [sols[i] for i in paretoidx] return pscores, pdds, psols
def filterPareto(scores, dds, arrays, verbose=0): """ From a list of designs select only the pareto optimal designs Args: scores (array): array of scores dds (array): array with D-efficiency values arrays (list): list of designs Returns: pareto_scores (list) : list of scores of pareto optimal designs pareto_efficiencies (list): list of D-efficiency values of pareto optimal designs pareto_designs (list) : list of selected designs """ pp = oahelper.createPareto(dds, verbose=0) paretoidx = np.array(pp.allindices()) pareto_scores = scores[paretoidx] pareto_efficiencies = dds[paretoidx] pareto_designs = [arrays[i] for i in paretoidx] return pareto_scores, pareto_efficiencies, pareto_designs
def generateDscatter(dds, si=0, fi=1, lbls=None, ndata=3, nofig=False, fig=20, scatterarea=80): """ Generate scatter plot for D and Ds efficiencies """ data = dds.T pp = oahelper.createPareto(dds) paretoidx = np.array(pp.allindices()) nn = dds.shape[0] area = scatterarea * np.ones(nn, ) / 2 area[np.array(pp.allindices())] = scatterarea alpha = 1.0 if dds.shape[1] > ndata: colors = dds[:, ndata] else: colors = np.zeros((nn, 1)) idx = np.unique(colors).astype(int) try: mycmap = brewer2mpl.get_map('Set1', 'qualitative', idx.size).mpl_colors except: mycmap = [matplotlib.cm.jet(ii) for ii in range(4)] pass # For remaining spines, thin out their line and change the black to a # slightly off-black dark grey almost_black = '#202020' figh = plt.figure(fig) # ,facecolor='red') plt.clf() figh.set_facecolor('w') ax = plt.subplot(111) nonparetoidx = np.setdiff1d(range(nn), paretoidx) ax.scatter(data[fi, nonparetoidx], data[si, nonparetoidx], s=.33 * scatterarea, c=(.5, .5, .5), linewidths=0, alpha=alpha, label='Non-pareto design') for jj, ii in enumerate(idx): gidx = (colors == ii).nonzero()[0] gp = np.intersect1d(paretoidx, gidx) color = mycmap[jj] cc = [color] * len(gp) print('index %d: %d points' % (ii, gidx.size)) ax.scatter(data[fi, gp], data[si, gp], s=scatterarea, c=cc, linewidths=0, alpha=alpha, label=lbls[jj]) # , zorder=4) plt.draw() if data[si, :].std() < 1e-3: y_formatter = matplotlib.ticker.ScalarFormatter(useOffset=False) ax.yaxis.set_major_formatter(y_formatter) if 0: for xi, al in enumerate(sols): D, Ds, D1 = al.Defficiencies() print('D1 %f Ds %f D %f' % (D1, Ds, D)) tmp = plt.scatter(Ds, D, s=60, color='r') if xi == 0: tmp = plt.scatter(Ds, D, s=60, color='r', label='Strength 3') plt.draw() xlabelhandle = plt.xlabel('$D_s$-efficiency', fontsize=16) plt.ylabel('D-efficiency', fontsize=16) try: oahelper.setWindowRectangle(10, 10, 860, 600) except Exception as e: print('generateDscatter: setWindowRectangle failed') pass plt.axis('image') pltlegend = ax.legend(loc=3, scatterpoints=1) # , fontcolor=almost_black) if not nofig: plt.show() ax.grid(b=True, which='both', color='0.85', linestyle='-') ax.set_axisbelow(True) if not nofig: plt.draw() plt.pause(1e-3) hh = dict({'ax': ax, 'xlabelhandle': xlabelhandle, 'pltlegend': pltlegend}) return hh
def generateDpage(outputdir, arrayclass, dds, allarrays, fig=20, optimfunc=[1, 0, 0], nofig=False, urlprefix='', makeheader=True, verbose=1, lbls=None): """ Helper function to generate web page with D-optimal design results """ if verbose: print('generateDpage: dds %s' % str(dds.shape)) pp = oahelper.createPareto(dds) narrays = dds.shape[0] npareto = pp.number() if verbose: print('generateDpage: narrays %d' % narrays) xstr = oahelper.series2htmlstr(arrayclass, case=1) xstrplain = oahelper.series2htmlstr(arrayclass, html=0, case=1) if verbose: print('generateDpage: selectParetoArrays ') paretoarrays = oahelper.selectParetoArrays(allarrays, pp) at = array2Dtable(paretoarrays, verbose=1) if verbose: print('generateDpage: write file with Pareto arrays') pfile0 = 'paretoarrays.oa' pfile = os.path.join(outputdir, pfile0) oalib.writearrayfile(pfile, paretoarrays) istrlnk = markup.oneliner.a('paretoarrays.oa', href=urlprefix + pfile0) if lbls is None: lbls = ['Optimization of $D$'] hh = generateDscatter(dds, lbls=lbls, fig=fig, nofig=nofig) oahelper.niceplot(hh.get('ax', None), despine=True, legend=hh['pltlegend']) scatterfile = os.path.join(outputdir, 'scatterplot.png') if verbose: print('generateDpage: writen scatterplot to %s' % scatterfile) plt.savefig(scatterfile, bbox_inches='tight', pad_inches=0.25, dpi=160) #%% Create page page = markup.page() if makeheader: page.init( title="Class %s" % xstrplain, css=('../oastyle.css'), lang='en', htmlattrs=dict({ 'xmlns': 'http://www.w3.org/1999/xhtml', 'xml:lang': 'en' }), header="<!-- Start of page -->", bodyattrs=dict({'style': 'padding-left: 3px;'}), # doctype=markup.doctype.strict, doctype= '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">', metainfo=({ 'text/html': 'charset=utf-8', 'keywords': 'orthogonal arrays designs', 'robots': 'index, follow', 'description': 'Even-Odd arrays' }), footer="<!-- End of page -->") page.h1('Results for array class %s ' % xstr) # mathjax is not updated properly... ss = 'The Pareto optimaly was calculated according to the statistics \(D\), \(D1\) and \(Ds\).' ss = 'The Pareto optimaly was calculated according to the statistics D, D<sub>1</sub> and D<sub>s</sub>.' if npareto == 1: page.p('Generated %d arrays, %d is Pareto optimal. %s' % (narrays, npareto, ss)) else: page.p('Generated %d arrays, %d are Pareto optimal. %s' % (narrays, npareto, ss)) if narrays > 0: scores = calcScore(dds, optimfunc) tmp, dd, sols = selectDn(scores, dds, allarrays, nout=1) A = sols[0] bestdesignfile = os.path.join(outputdir, 'best-design.oa') oalib.writearrayfile(bestdesignfile, A) page.h2('Best design') page.p('The best design: %s.' % e.a( 'best-design.oa', href=os.path.join(urlprefix, 'best-design.oa'))) page.p() dd = dd[0] page.span('D-efficiency: %.4f, ' % A.Defficiency()) # page.br() page.span('D<sub>s</sub>-efficiency: %.4f, ' % dd[1]) # page.br() page.span('D<sub>1</sub>-efficiency: %.4f' % dd[2]) page.br() page.span('A-efficiency: %.3f' % A.Aefficiency()) page.br() gwlp = A.GWLP() # gwlp=','.join(['%.3f' % xq for xq in gwlp]) gwlp = oahelper.gwlp2str(gwlp, jstr=', ') page.span('Generalized wordlength pattern: %s' % gwlp) page.br() # page.p('D-efficiency: %.3f' % A.Defficiency() ) pec = oalib.PECsequence(A) pec = ','.join(['%.3f' % xq for xq in pec]) page.span('PEC-sequence: %s' % pec) page.br() page.h2('Table of Pareto optimal arrays ') page.span(str(at)) page.p('All Pareto optimal arrays: %s' % istrlnk) page.img( src=urlprefix + 'scatterplot.png', style= "margin: 10px; width:95%; min-width: 300px; max-width:1100px; height: auto; " ) citationstr = markup.oneliner.a( 'Complete Enumeration of Pure-Level and Mixed-Level Orthogonal Arrays', href='http://dx.doi.org/10.1002/jcd.20236') page.br(clear='both') page.p( 'Citation notice: if you make use of the results on this page, please cite the following paper:' ) page.p( '%s, Journal of Combinatorial Designs, Volume 18, Issue 2, pages 123-140, 2010.' % citationstr) page.p('Generated with oapackage %s, date %s.' % (oalib.version(), oahelper.timeString())) outfile = os.path.join(outputdir, 'Dresults.html') fid = open(outfile, 'wt') fid.write(str(page)) fid.close() print('written to file %s' % outfile) return outfile
def generateDscatter(dds, second_index=0, first_index=1, lbls=None, ndata=3, nofig=False, fig=20, scatterarea=80, verbose=0, setWindowRectangle=False): """ Generate scatter plot for D and Ds efficiencies Args: dds (array): array with D-efficiencies Returns: dict: contains handles to plotting elements """ if matplotlib is None: raise MissingMatplotLibException data = dds.T pp = oahelper.createPareto(dds) paretoidx = np.array(pp.allindices()) nn = dds.shape[0] area = scatterarea * np.ones(nn,) / 2 area[np.array(pp.allindices())] = scatterarea alpha = 1.0 if dds.shape[1] > ndata: colors = dds[:, ndata] else: colors = np.zeros((nn, 1)) idx = np.unique(colors).astype(int) if verbose: print('generateDscatter: unique colors: %s' % (idx, )) ncolors = idx.size try: import brewer2mpl ncolors = max(ncolors, 4) mycmap = brewer2mpl.get_map('Set1', 'qualitative', ncolors).mpl_colors except BaseException: mycmap = [matplotlib.cm.jet(ii) for ii in np.linspace(0, 1, ncolors)] nonparetoidx = np.setdiff1d(range(nn), paretoidx) if lbls is None: lbls = ['%d' % i for i in range(len(idx))] if fig is not None: figh = plt.figure(fig) plt.clf() figh.set_facecolor('w') ax = plt.subplot(111) ax.scatter(data[first_index, nonparetoidx], data[second_index, nonparetoidx], s=.33 * scatterarea, c=(.5, .5, .5), linewidths=0, alpha=alpha, label='Non-pareto design') for jj, ii in enumerate(idx): gidx = (colors == ii).nonzero()[0] gp = np.intersect1d(paretoidx, gidx) color = mycmap[jj] cc = [color] * len(gp) if verbose: print('index %d: %d points' % (ii, gidx.size)) ax.scatter(data[first_index, gp], data[second_index, gp], s=scatterarea, c=cc, linewidths=0, alpha=alpha, label=lbls[jj]) plt.draw() if data[second_index, :].std() < 1e-3: y_formatter = matplotlib.ticker.ScalarFormatter(useOffset=False) ax.yaxis.set_major_formatter(y_formatter) xlabelhandle = plt.xlabel('$D_s$-efficiency', fontsize=16) plt.ylabel('D-efficiency', fontsize=16) if setWindowRectangle: try: oahelper.setWindowRectangle(10, 10, 860, 600) except Exception as ex: print('generateDscatter: setWindowRectangle failed') logging.exception(ex) plt.axis('image') pltlegend = ax.legend(loc=3, scatterpoints=1) # , fontcolor=almost_black) if not nofig: plt.show() ax.grid(b=True, which='both', color='0.85', linestyle='-') ax.set_axisbelow(True) if nofig: plt.close(figh.number) else: plt.draw() plt.pause(1e-3) else: ax = None xlabelhandle = None pltlegend = None hh = dict({'ax': ax, 'xlabelhandle': xlabelhandle, 'pltlegend': pltlegend}) return hh
def generateDpage(outputdir, arrayclass, dds, allarrays, fig=20, optimfunc=[1, 0, 0], nofig=False, urlprefix='', makeheader=True, verbose=1, lbls=None): """ Helper function to generate web page with D-optimal design results """ if verbose: print('generateDpage: dds %s' % str(dds.shape)) pp = oahelper.createPareto(dds) narrays = dds.shape[0] npareto = pp.number() if verbose: print('generateDpage: narrays %d' % narrays) xstr = oahelper.series2htmlstr(arrayclass, case=1) xstrplain = oahelper.series2htmlstr(arrayclass, html=0, case=1) if verbose: print('generateDpage: selectParetoArrays ') paretoarrays = oahelper.selectParetoArrays(allarrays, pp) at = array2Dtable(paretoarrays, verbose=1) if verbose: print('generateDpage: write file with Pareto arrays') pfile0 = 'paretoarrays.oa' pfile = os.path.join(outputdir, pfile0) oalib.writearrayfile(pfile, paretoarrays) istrlnk = markup.oneliner.a('paretoarrays.oa', href=urlprefix + pfile0) if lbls is None: lbls = ['Optimization of $D$'] if fig is not None: hh = generateDscatter(dds, lbls=lbls, fig=fig, nofig=nofig) oahelper.niceplot(hh.get('ax', None), despine=True, legend=hh['pltlegend']) scatterfile = os.path.join(outputdir, 'scatterplot.png') if verbose: print('generateDpage: writen scatterplot to %s' % scatterfile) plt.savefig(scatterfile, bbox_inches='tight', pad_inches=0.25, dpi=160) page = markup.page() if makeheader: page.init(title="Class %s" % xstrplain, css=('../oastyle.css'), lang='en', htmlattrs=dict({'xmlns': 'http://www.w3.org/1999/xhtml', 'xml:lang': 'en'}), header="<!-- Start of page -->", bodyattrs=dict({'style': 'padding-left: 3px;'}), doctype='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">', metainfo=({'text/html': 'charset=utf-8', 'keywords': 'orthogonal arrays designs', 'robots': 'index, follow', 'description': 'Even-Odd arrays'}), footer="<!-- End of page -->") page.h1('Results for array class %s ' % xstr) ss = r'The Pareto optimaly was calculated according to the statistics D, D<sub>1</sub> and D<sub>s</sub>.' if npareto == 1: page.p('Generated %d arrays, %d is Pareto optimal. %s' % (narrays, npareto, ss)) else: page.p('Generated %d arrays, %d are Pareto optimal. %s' % (narrays, npareto, ss)) if narrays > 0: scores = calcScore(dds, optimfunc) _, dd, sols = selectDn(scores, dds, allarrays, nout=1) A = sols[0] bestdesignfile = os.path.join(outputdir, 'best-design.oa') oalib.writearrayfile(bestdesignfile, A) page.h2('Best design') page.p('The best design: %s.' % e.a('best-design.oa', href=os.path.join(urlprefix, 'best-design.oa'))) page.p() dd = dd[0] page.span('D-efficiency: %.4f, ' % A.Defficiency()) # page.br() page.span('D<sub>s</sub>-efficiency: %.4f, ' % dd[1]) # page.br() page.span('D<sub>1</sub>-efficiency: %.4f' % dd[2]) page.br() page.span('A-efficiency: %.3f' % A.Aefficiency()) page.br() gwlp = A.GWLP() # gwlp=','.join(['%.3f' % xq for xq in gwlp]) gwlp = oahelper.gwlp2str(gwlp, jstr=', ') page.span('Generalized wordlength pattern: %s' % gwlp) page.br() # page.p('D-efficiency: %.3f' % A.Defficiency() ) pec = oalib.PECsequence(A) pec = ','.join(['%.3f' % xq for xq in pec]) page.span('PEC-sequence: %s' % pec) page.br() page.h2('Table of Pareto optimal arrays ') page.span(str(at)) page.p('All Pareto optimal arrays: %s' % istrlnk) page.img(src=urlprefix + 'scatterplot.png', style="margin: 10px; width:95%; min-width: 300px; max-width:1100px; height: auto; ") citationstr = markup.oneliner.a( 'Complete Enumeration of Pure-Level and Mixed-Level Orthogonal Arrays', href='https://doi.org/10.1002/jcd.20236') page.br(clear='both') page.p( 'Citation notice: if you make use of the results on this page, please cite the following paper:') page.p('%s, Journal of Combinatorial Designs, Volume 18, Issue 2, pages 123-140, 2010.' % citationstr) page.p('Generated with oapackage %s, date %s.' % (oalib.version(), oahelper.timeString())) outfile = os.path.join(outputdir, 'Dresults.html') fid = open(outfile, 'wt') fid.write(str(page)) fid.close() print('written to file %s' % outfile) return outfile
def generateDscatter(dds, second_index=0, first_index=1, lbls=None, ndata=3, nofig=False, fig=20, scatterarea=80, verbose=0, setWindowRectangle=False): """ Generate scatter plot for D and Ds efficiencies Args: dds (array): array with D-efficiencies Returns: dict: contains handles to plotting elements """ if matplotlib is None: raise MissingMatplotLibException data = dds.T pp = oahelper.createPareto(dds) paretoidx = np.array(pp.allindices()) nn = dds.shape[0] area = scatterarea * np.ones(nn, ) / 2 area[np.array(pp.allindices())] = scatterarea alpha = 1.0 if dds.shape[1] > ndata: colors = dds[:, ndata] else: colors = np.zeros((nn, 1)) idx = np.unique(colors).astype(int) if verbose: print('generateDscatter: unique colors: %s' % (idx, )) ncolors = idx.size try: import brewer2mpl ncolors = max(ncolors, 4) mycmap = brewer2mpl.get_map('Set1', 'qualitative', ncolors).mpl_colors except BaseException: mycmap = [matplotlib.cm.jet(ii) for ii in np.linspace(0, 1, ncolors)] nonparetoidx = np.setdiff1d(range(nn), paretoidx) if lbls is None: lbls = ['%d' % i for i in range(len(idx))] if fig is not None: figh = plt.figure(fig) plt.clf() figh.set_facecolor('w') ax = plt.subplot(111) ax.scatter(data[first_index, nonparetoidx], data[second_index, nonparetoidx], s=.33 * scatterarea, c=[(.5, .5, .5)], linewidths=0, alpha=alpha, label='Non-pareto design') for jj, ii in enumerate(idx): gidx = (colors == ii).nonzero()[0] gp = np.intersect1d(paretoidx, gidx) color = mycmap[jj] cc = [color] * len(gp) if verbose: print('index %d: %d points' % (ii, gidx.size)) ax.scatter(data[first_index, gp], data[second_index, gp], s=scatterarea, c=cc, linewidths=0, alpha=alpha, label=lbls[jj]) plt.draw() if data[second_index, :].std() < 1e-3: y_formatter = matplotlib.ticker.ScalarFormatter(useOffset=False) ax.yaxis.set_major_formatter(y_formatter) xlabelhandle = plt.xlabel('$D_s$-efficiency', fontsize=16) plt.ylabel('D-efficiency', fontsize=16) if setWindowRectangle: try: oahelper.setWindowRectangle(10, 10, 860, 600) except Exception as ex: print('generateDscatter: setWindowRectangle failed') logging.exception(ex) plt.axis('image') pltlegend = ax.legend(loc=3, scatterpoints=1) # , fontcolor=almost_black) if not nofig: plt.show() ax.grid(b=True, which='both', color='0.85', linestyle='-') ax.set_axisbelow(True) if nofig: plt.close(figh.number) else: plt.draw() plt.pause(1e-3) else: ax = None xlabelhandle = None pltlegend = None hh = dict({'ax': ax, 'xlabelhandle': xlabelhandle, 'pltlegend': pltlegend}) return hh