def addfactors(Dcat, naddedfac, sumtable): # Find all the lower generators for nfac in range(0, naddedfac - 1): newcat = [] for i in Dcat: cols = [ x for x in candiCols if candiCols.index(x) > candiCols.index(i['word']) ] for j in cols: newcat.append({ "design": np.c_[i['design'], fullD[:, [fn.n2fn(nbf).index(j)]]], "cols": i['cols'] + [j], 'word': j }) #Fill the summary table Dcat = fn.isoSelection(newcat) sumtable = sumtable.append(pd.DataFrame( [[nfac + 5, len(newcat), len(Dcat)]], columns=['NFac', 'NDesigns', 'NFinal']), ignore_index=True) return Dcat, sumtable
def addfactor(cat,candicols,nruns): nbf = np.log2(nruns).astype(int); fullD = (fracfact(' '.join(fn.n2fn(nbf)))+1)//2; # Find all the lower generators fullcat = []; for i in cat: cols = [x for x in candicols if candicols.index(x) > candicols.index(i['word']) ]; for j in cols: fullcat.append({"design" : np.c_[i['design'],fullD[:,[fn.n2fn(nbf).index(j)]]], "cols" : i['cols'] +[j], 'word' : j}); # Select non-isomorphic designs isocat = fn.isoSelection(fullcat); return fullcat,isocat
def addfirstcol(D,Dcols,n4lvl): # Get 4lvl generators nbf = np.log2(D.shape[0]).astype(int); fourlvlgens = np.array(fn.generators(nbf)); fourlvlgen = list(chain(*fourlvlgens[0:n4lvl])); # Enumerate 2lvl generators candiCols = [x for x in fn.n2fn(nbf) if x not in fourlvlgen and len(x) > 1]; val,ind = np.unique( [getIntType(i,fourlvlgens[0:n4lvl]) for i in candiCols],return_index=True); # Retrieve corresponding columns ind.sort(); nisoCols = [candiCols[x] for x in ind]; # Add to the design fullD = (fracfact(' '.join(fn.n2fn(nbf)))+1)//2; cat = []; for i in nisoCols: cat.append({"design" : np.c_[D,fullD[:,[fn.n2fn(nbf).index(i)]]], "cols" : Dcols + [i], "word" : i}); fullcat = []; for i in candiCols: fullcat.append({"design" : np.c_[D,fullD[:,[fn.n2fn(nbf).index(i)]]], "cols" : Dcols + [i], "word" : i}); return cat, candiCols
def getstartD(nruns,n4lvl): # Starting two-level design nbf = np.log2(nruns).astype(int); fourlvlgens = np.array(fn.generators(nbf)); fourlvlgen = list(chain(*fourlvlgens[0:n4lvl])); startDcols = [i for i in fn.n2fn(nbf) if len(i) == 1 or i in fourlvlgen]; startD = (fracfact(' '.join(startDcols))+1)//2; # Make four-level variable varnamechr = 65 D = startD.copy(); Dcols = startDcols.copy(); for gen in fourlvlgens[0:n4lvl]: D,Dcols = fn.make4lvl(D,Dcols,[Dcols.index(i) for i in gen], chr(varnamechr),zerocoding=True); varnamechr+=1; return D, Dcols;
l -= 1 return (2 * l + val) def intTypeSort(collist, fourlvlgen=['a', 'b', 'ab']): out = [pseudoword(x, fourlvlgen) for x in collist] out = sorted(out) out = [normalword(x) for x in out] return sorted(out, key=getIntType) nruns = 16 nbf = np.log2(nruns).astype(int) # Get starting Design fourlvlgen = ['a', 'b', 'ab'] startDcols = [i for i in fn.n2fn(nbf) if len(i) == 1 or i in fourlvlgen] startD = (fracfact(' '.join(startDcols)) + 1) // 2 # Make four-level variable D, Dcols = fn.make4lvl(array=startD, collist=startDcols, colindex=[startDcols.index(i) for i in fourlvlgen], fourlvlvarname='A', zerocoding=True) #Dcols = ['A'] + [i for i in startDcols if i not in fourlvlgen]; # Select remaining columns and sort the generators candiCols = [i for i in fn.n2fn(nbf) if i not in startDcols] #candiCols = intTypeSort(candiCols); # Compute interaction type
def bruteForceD(nruns,n4lvl,n2lvl,catType='iso'): if catType not in ['full','iso']: raise ValueError('Only "full" "iso" catalogs available'); if not np.log2(nruns).is_integer() or nruns < 4: raise ValueError('Nruns must be a positive power of 2'); if n4lvl not in range(0,int((nruns-1)/3)): raise ValueError('N4lvl must be between 0 and (N-1)/3'); if n2lvl not in range(0,int(nruns-1)): raise ValueError('N2lvl must be between 0 and (N-1)'); if n4lvl*3+n2lvl > nruns-1: raise ValueError('Too many factors') l = iter([nruns,n4lvl,n2lvl]); if any(l) < 0: raise ValueError('Nruns, n4lvl and n2lvl must all be positive integer'); nbf = np.log2(nruns).astype(int) # Get starting Design fourlvlgens = fn.generators(nbf)[:n4lvl]; fourlvlgen = list(chain(*fourlvlgens)); startDcols = [i for i in fn.n2fn(nbf) if len(i) == 1 or i in fourlvlgen] ; startD = (fracfact(' '.join(startDcols))+1)//2; # Make four-level variable D=startD.copy(); Dcols = startDcols.copy(); varnamechr = 65 for gen in fourlvlgens: D,Dcols = fn.make4lvl(D,Dcols,[Dcols.index(i) for i in gen],chr(varnamechr),zerocoding=True) varnamechr+=1; # Add added factors if n4lvl == 1: if n2lvl <= 2: Dcatalog =[{'design' : D[:,:n2lvl+1], 'cols' : Dcols[:n2lvl+1]}]; return Dcatalog else: fullD = (fracfact(' '.join(fn.n2fn(nbf)))+1)//2; candiCols = [i for i in fn.n2fn(nbf) if i not in startDcols]; enum = combinations(candiCols,n2lvl-2); Dcatalog = []; for i in list(enum): Dcatalog.append( {"design" : np.c_[D,fullD[:,[fn.n2fn(nbf).index(x) for x in [y for y in i]]]], "cols" : Dcols + [y for y in i], "name" : 'D_4(' + str(n4lvl) + ')2(' + str(n2lvl) + ')'} ); else: fullD = (fracfact(' '.join(fn.n2fn(nbf)))+1)//2; candiCols = [i for i in fn.n2fn(nbf) if i not in startDcols]; enum = combinations(candiCols,n2lvl); Dcatalog = []; for i in list(enum): Dcatalog.append( {"design" : np.c_[D,fullD[:,[fn.n2fn(nbf).index(x) for x in [y for y in i]]]], "cols" : Dcols + [y for y in i], "name" : 'D_4(' + str(n4lvl) + ')2(' + str(n2lvl) + ')'} ); if catType == 'full': return Dcatalog elif catType == 'iso': return fn.isoSelection(Dcatalog);