def arraystats(A, verbose=1): """ Return statistics of an array """ Af = A.transpose().flatten() al = oalib.array_link(A.shape[0], A.shape[1], 0) alist = oalib.arraylist_t(1) alist[0] = al # al.array ia = oalib.intArray.frompointer(al.array) for x in range(0, A.size): ia[x] = int(Af[x]) # al.showarray() jresults = oalib.analyseArrays(alist, 0) js = jresults[0] vals = pointer2np(js.vals, js.nc) jvals = getposjvals(A, 3, verbose=0) jv = np.zeros(len(jvals)) c = Counter(abs(vals)) for ii, xx in enumerate(jvals): if c.has_key(xx): jv[ii] = c.get(xx) if verbose: print('Possible j-values: %s' % jvals, end="") print(' values: %s' % jv.astype(int)) N = A.shape[0] Ak = (1 / N ^ 2) * sum(vals**2) print('Ak: %s' % Ak) return Ak
def makearraylink(al): """ Convert array to array_link object """ if isinstance(al, np.ndarray): tmp = oalib.array_link() tmp.setarray(al) al = tmp return al
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 makearraylink(array): """ Convert array to array_link object Args: array (numpy array): array to convert Returns: array_link """ if isinstance(array, np.ndarray): tmp = oalib.array_link() tmp.setarray(array) array = tmp return array
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
def unittest(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) #test_numpy_interface() 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) # print(arrayclass) if verbose >= 2: print('unittest: calculate efficiencies') Deff = al.Defficiency() aa = oalib.Aefficiencies(al) 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, 1) 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 at = oalib.reductionDOP(al) check = at.apply(al) == al.reduceDOP() if not check: print('error: DOP reduction transformation is invalid') # test graphtools if verbose >= 2: print('unittest: test graphtools') # import . graphtools from .graphtools import oa2graph arrayclass = oalib.arraylink2arraydata(al) _ = oa2graph(al, arrayclass) test_numpy_interface() test_nauty() return True