def selectIsomorphismClasses(sols, verbose=1): """ Select isomorphism classes from a list of designs Args: sols (list of arrays) verbose (int) Return: indices (list of integers): indices of the isomorphism classes mm (list of arrays): the arrays in normal form Example: >>> sols=[oapackage.exampleArray(i) for i in [26,26,27,26]] >>> idx, mm = selectIsomorphismClasses(sols) To select one representative array from each isomorphism class one can use: >>> _, ui = np.unique(idx, return_index=True) >>> representatives = [sols[i] for i in ui] """ # perform check on array data type mm = [] for ii, al in enumerate(sols): if verbose: oapackage.tprint('selectIsomorphismClasses: process aray %d/%d' % (ii, len(sols))) al = oapackage.makearraylink(al) tt = oapackage.reduceOAnauty(al, verbose >= 2) #pp, tt = reduceBliss(al, arrayclass, verbose >= 2) #tt = graph2arrayTransformation(pp, arrayclass) alx = tt.apply(al) mm.append(np.array(alx)) pass # perform uniqueness check nn = len(mm) qq = np.array([None] * nn, dtype=object) for ii in range(nn): qq[ii] = mm[ii].flatten() # Trick to make unique work... nx = qq[0].size dt = qq[0].dtype.descr * nx qqq = np.array(qq, dtype=dt) a, indices = np.unique(qqq, return_inverse=True) if verbose >= 1: print('selectIsomorphismClasses: reduce %d to %d' % (len(sols), np.unique(indices).size)) return indices, mm
def selectIsomorphismClasses(sols, verbose=1): """ Select isomorphism classes from a list of designs Args: sols (list of arrays) verbose (int) Return: indices (list of integers): indices of the isomorphism classes mm (list of arrays): the arrays in normal form """ # perform check on array data type mm = [] for ii, al in enumerate(sols): if verbose: oapackage.tprint('selectIsomorphismClasses: process aray %d/%d' % (ii, len(sols)), dt=4) al = oapackage.makearraylink(al) tt = oapackage.reduceOAnauty(al, verbose >= 2) #pp, tt = reduceBliss(al, arrayclass, verbose >= 2) #tt = graph2arrayTransformation(pp, arrayclass) alx = tt.apply(al) mm.append(np.array(alx)) pass # perform uniqueness check nn = len(mm) qq = np.array([None] * nn, dtype=object) for ii in range(nn): qq[ii] = mm[ii].flatten() if 0: # Trick to make unique work... # old code nx = qq[0].size dt = qq[0].dtype.descr * nx qqq = [np.array(q, dtype=dt) for q in qq] qqq = np.array(qq, dtype=dt) a, indices = np.unique(qqq, return_inverse=True) # Trick to make unique work... a, indices = np.unique(np.vstack(qq), axis=0, return_inverse=True) if verbose >= 1: print('selectIsomorphismClasses: %d reduced to %d' % (len(sols), np.unique(indices).size)) return indices, mm
def optimize_arrays(sols, alpha=None, verbose=1, niter=400): """ Optimize set of designs """ vv2 = [] ll = oapackage.arraylist_t() for ii, A in enumerate(sols): Dinit = A.Defficiency() Df, Ax = oapackage.optimDeffPython( A, niter=niter, verbose=0, alpha=alpha) D, Ds, D1 = Ax.Defficiencies() ll.push_back(Ax) oapackage.tprint('optimize array %d: %f -> D %f Ds %f D1 %f' % (ii, Dinit, D, Ds, D1)) vv2.append(np.array([D, Ds, D1])) return np.array(vv2).T, ll
def selectIsomorphismClasses(sols, verbose=1): """ Select isomorphism classes from a list of designs Args: sols (list of arrays): list of arrays from which to determine the unique ones verbose (int): verbosity level Return: indices (list of integers): indices of the isomorphism classes mm (list of arrays): the arrays in normal form Example: >>> import oapackage.graphtools; import numpy as np >>> sols=[oapackage.exampleArray(idx) for idx in [26,26,27,26]] >>> idx, mm = oapackage.graphtools.selectIsomorphismClasses(sols, verbose=0) >>> _, unique_indices = np.unique(idx, return_index=True) >>> print('found %d isomorphism classes from %d arrays' % (len(unique_indices), len(sols)) ) found 2 isomorphism classes from 4 arrays >>> representatives = [sols[idx] for idx in unique_indices] """ mm = [] for ii, al in enumerate(sols): if verbose: oapackage.tprint( 'selectIsomorphismClasses: process aray %d/%d' % (ii, len(sols))) al = oapackage.makearraylink(al) tt = oapackage.reduceOAnauty(al, verbose >= 2) alx = tt.apply(al) mm.append(np.array(alx)) # perform uniqueness check nn = len(mm) qq = np.array([None] * nn, dtype=object) for ii in range(nn): qq[ii] = mm[ii].flatten() # Trick to make unique work... _, indices = np.unique(np.vstack(qq), axis=0, return_inverse=True) if verbose >= 1: print('selectIsomorphismClasses: reduce %d to %d' % (len(sols), np.unique(indices).size)) return indices, mm
def selectIsomorphismClasses(sols, verbose=1): """ Select isomorphism classes from a list of designs Args: sols (list of arrays): list of arrays from which to determine the unique ones verbose (int): verbosity level Return: indices (list of integers): indices of the isomorphism classes mm (list of arrays): the arrays in normal form Example: >>> import oapackage.graphtools; import numpy as np >>> sols=[oapackage.exampleArray(idx) for idx in [26,26,27,26]] >>> idx, mm = oapackage.graphtools.selectIsomorphismClasses(sols, verbose=0) >>> _, unique_indices = np.unique(idx, return_index=True) >>> print('found %d isomorphism classes from %d arrays' % (len(unique_indices), len(sols)) ) found 2 isomorphism classes from 4 arrays >>> representatives = [sols[idx] for idx in unique_indices] """ mm = [] for ii, al in enumerate(sols): if verbose: oapackage.tprint('selectIsomorphismClasses: process aray %d/%d' % (ii, len(sols))) al = oapackage.makearraylink(al) tt = oapackage.reduceOAnauty(al, verbose >= 2) alx = tt.apply(al) mm.append(np.array(alx)) # perform uniqueness check nn = len(mm) qq = np.array([None] * nn, dtype=object) for ii in range(nn): qq[ii] = mm[ii].flatten() # Trick to make unique work... _, indices = np.unique(np.vstack(qq), axis=0, return_inverse=True) if verbose >= 1: print('selectIsomorphismClasses: reduce %d to %d' % (len(sols), np.unique(indices).size)) return indices, mm
def optimize_arrays(sols, alpha=None, verbose=1, niter=400): """ Optimize set of designs """ vv2 = [] ll = oapackage.arraylist_t() for ii, A in enumerate(sols): Dinit = A.Defficiency() Df, Ax = oapackage.optimDeffPython(A, niter=niter, verbose=0, alpha=alpha) D, Ds, D1 = Ax.Defficiencies() ll.push_back(Ax) oapackage.tprint('optimize array %d: %f -> D %f Ds %f D1 %f' % (ii, Dinit, D, Ds, D1)) vv2.append(np.array([D, Ds, D1])) return np.array(vv2).T, ll
def read_hadamard(f, narrays=1e9, N=32, k=32): """ Read all designs in a file with hex encoding """ if isinstance(f, str): fid = open(f, 'rt') else: fid = f ll = [] for ii in range(int(narrays)): oapackage.tprint('reading array %d' % ii, dt=.5) ii = _get_integer(fid) if ii == -1: break al = _get_array(fid, N, k) if al is not None: ll += [al] if isinstance(f, str): fid.close() print('read_hadamard: %d designs' % len(ll)) return ll
infile = oapackage.arrayfile_t(inputfile) narrays=infile.narrays #outfile = oapackage.arrayfile_t(outputfile, infile.nrows, infile.ncols, -1, fmode ) if not infile.isopen(): raise Exception('could not open file %s' % inputfile) print('oaselect: reading %d arrays from %s' % (infile.narrays, inputfile) ) #%% Loop over the arrays outlist=[] if narrays==-1: narrays=oapackage.arrayfile_t.NARRAYS_MAX for ii in range(narrays): oapackage.tprint('oaselect: parse array %d/%d' % (ii, narrays) ) al=infile.readnext() if al.index==-1: break if verbose>=3: print(' index %d' % al.index) if fname=='subset': if ii%args.nsubset==0: outlist.append(al) else: if filterArray(al, fname, verbose=0): outlist.append(al) if narrays>0: oapackage.tprint('oaselect: parse array %d/%d' % (ii, narrays), dt=-1 ) oapackage.writearrayfile(outputfile, outlist, fmode, infile.nrows, infile.ncols)