def pytest6(verbose=1): """ Check generation of OA(32, t, 2^a) """ N = 32 k = 8 t = 3 l = [2] * 8 rr = [] gtrr = [3, 5, 10, 17, 33] s = oalib.intVector(l) adata = oalib.arraydata_t(s, N, t, k) if verbose: print('pytest6: run different algorithms on the same case') algs = [oalib.MODE_ORIGINAL, oalib.MODE_J4] for ii, alg in enumerate(algs): algname = oalib.algnames(alg) if verbose >= 2: print('pytest6: running %s, alg %s' % (adata.fullidstr(), algname)) rr = [] tmp = oahelper.runExtend(N, k, t, l, verbose=verbose, nums=rr, algorithm=alg) if not rr == gtrr: print('pytest6: case %s' % adata.fullidstr()) print( ' algorithm %s: error: incorrect number of arrays! %s -> %s' % (algname, rr, gtrr))
def pytest2level(verbose=1): """ Check generation of OA(16,2, 4^2 2^a) """ N = 32 k = 10 t = 3 l = [2] rr = [] t0 = time.time() oahelper.runExtend(N, k, t, l, verbose=1, nums=rr, algorithm=oalib.MODE_ORIGINAL) dt = time.time() - t0 t0 = time.time() oahelper.runExtend(N, k, t, l, verbose=1, nums=rr, algorithm=oalib.MODE_LMC_2LEVEL) dt2level = time.time() - t0 ll = l * k s = oalib.intVector(ll) adata = oalib.arraydata_t(s, N, t, k) if verbose: print('case %s: 2-level method %.2f [s] -> %.2f [s]' % (adata.idstr(), dt, dt2level))
def runExtend(N, k, t=3, l=2, verbose=1, initsols=None, nums=[], algorithm=None): """ Run extension algorithm and return arrays Args: N (int): number of rows k (int): number of columns to extend to t (int): strength of the arrays l (int): factors of the designs initsols (None or list): list of arrays to extend, None to start with root Returns: list: list of generated designs Example: >>> import oapackage >>> designs = oapackage.oahelper.runExtend(16, 5, 3, verbose=0) """ if verbose: print('runExtend: N=%d, k=%d, t=%d' % (N, k, t)) if isinstance(l, list): ll = l else: ll = [l] ll = ll + [ll[-1]] * (k - len(ll)) s = oalib.intVector(ll) adata = oalib.arraydata_t(s, N, t, k) al = oalib.array_link(adata.N, adata.strength, 1) al.create_root(adata) if initsols is None: sols0 = oalib.arraylist_t() sols0.append(al) tstart = t else: sols0 = initsols tstart = sols0[0].n_columns oaoptions = oalib.OAextend() if algorithm is None: oaoptions.setAlgorithmAuto(adata) else: oaoptions.setAlgorithm(algorithm, adata) solsx = sols0 for ii in range(tstart, k): solsx = oalib.arraylist_t() oalib.extend_arraylist(sols0, adata, oaoptions, ii, solsx) if verbose >= 2: print(' ii %d: %d' % (ii, solsx.size())) sols0 = solsx nums.append(solsx.size()) sys.stdout.flush() return solsx
def runExtend(N, k, t=3, l=2, verbose=1, initsols=None, nums=[], algorithm=None): """ Run extension algorithm and return arrays Arguments --------- N : integer number of rows k: integer number of columns t: integer strength of the arrays >>> r = runExtend(16, 5, 3, verbose=0) """ if verbose: print('runExtend: N=%d, k=%d, t=%d' % (N, k, t)) if isinstance(l, list): # types.ListType): ll = l else: ll = [l] ll = ll + [ll[-1]] * (k - len(ll)) s = oalib.intVector(ll) adata = oalib.arraydata_t(s, N, t, k) al = oalib.array_link(adata.N, adata.strength, 1) al.create_root(adata) if initsols is None: sols0 = oalib.arraylist_t() sols0.append(al) tstart = t else: sols0 = initsols tstart = sols0[0].n_columns oaoptions = oalib.OAextend() if algorithm is None: oaoptions.setAlgorithmAuto(adata) else: oaoptions.setAlgorithm(algorithm, adata) solsx = sols0 for ii in range(tstart, k): solsx = oalib.arraylist_t() oalib.extend_arraylist(sols0, adata, oaoptions, ii, solsx) if verbose >= 2: print(' ii %d: %d' % (ii, solsx.size())) sols0 = solsx nums.append(solsx.size()) sys.stdout.flush() return solsx
def _optimDeffhelper(classdata): """ Helper function that is suitable for the multi-processing framework """ N = classdata[0] k = classdata[1] alpha = classdata[2] method = classdata[3] p = classdata[4] nabort = p.get('nabort', 2500) niter = p.get('nabort', 12000) arrayclass = oalib.arraydata_t(2, N, 1, k) al = arrayclass.randomarray(1) vv = optimDeffPython(al, niter=niter, nabort=nabort, verbose=0, alpha=alpha, method=method) return vv[0], vv[1].getarray()
def miscunittest(verbose=1): """ Perform some unit testing, return True if succesfull """ if verbose: print('oapackage: unittest: oalib version %s' % oalib.version()) al = oalib.array_link() ii = 0 al = oalib.exampleArray(ii, 0) arrayclass = oalib.arraydata_t(2, 16, 2, 6) # fine alist = [arrayclass.create_root()] r = oalib.extend_arraylist(alist, arrayclass) if len(r) != 3: raise Exception('extension generation for %s failed' % (arrayclass, )) if not isinstance(al.getarray(), np.ndarray): print( 'oapackage: unittest: error: array interface not working properly') else: if not al[2, 0] == al.getarray()[2, 0]: print( 'oapackage: unittest: error: array interface not working properly' ) arrayclass = oalib.arraylink2arraydata(al) if verbose >= 2: print('unittest: calculate efficiencies') Deff = al.Defficiency() aa = oalib.Aefficiencies(al) assert (aa[0] == 1.0) assert (aa[1] == 1.0) assert (aa[2] == 1.0) if verbose >= 2: print('## oapackage test: example array %d: Deff %.3f' % (ii, Deff)) # DOP reduction if verbose >= 2: print('unittest: test delete-one-factor GWLP reduction') al = oalib.exampleArray(5, verbose) al2 = al.randomperm() alr = al.reduceDOP() al2r = al2.reduceDOP() if not alr == al2r: print('error: DOP reduced arrays unequal!: %d' % (alr == al2r)) print('alr') alr.showarraycompact() print('al2r') al2r.showarraycompact() return False transformation = oalib.reductionDOP(al) check = transformation.apply(al) == al.reduceDOP() if not check: print('error: DOP reduction transformation is invalid') # test graphtools if verbose >= 2: print('unittest: test graphtools') from oapackage.graphtools import oa2graph arrayclass = oalib.arraylink2arraydata(al) _ = oa2graph(al, arrayclass) test_numpy_interface() test_nauty() return True
> pygmentize -f html example_python_testing.py @author: Pieter Eendebak """ #%% Load packages import sys, os import numpy as np print('Python OA Interface Check') import oalib import oapackage.oahelper as oahelper #%% print('-- Create design configuration --') adata = oalib.arraydata_t(2, 32, 2, 7) oaoptions = oalib.OAextend() oaoptions.setAlgorithmAuto(adata) adata.show() print('-- Create root element --') al = adata.create_root() al.show() al.showarray() # Extend print('-- Extend arrays --') print('Extend to 3 columns') newsols = oalib.extend_array(al, adata, oaoptions) print(newsols)