def toCIspinorbReverse(norb, ne, civec2): print '\n[toCIspinorbReverse]: spin-orbit rep C(Ia,Ib)<-C(I)' nsA = misc.binomial(norb, ne[0]) nsB = misc.binomial(norb, ne[1]) nelec = ne[0] + ne[1] nsorb = 2 * norb print 'nsA,nsB=', nsA, nsB civec = numpy.zeros((nsA, nsB)) for isA in range(nsA): bsA = addr2str_o1(norb, ne[0], isA) strA = bit2string(bsA, norb) strA = [2 * i for i in strA] for isB in range(nsB): bsB = addr2str_o1(norb, ne[1], isB) strB = bit2string(bsB, norb) strB = [2 * i + 1 for i in strB] # conversion string = strA + strB sgn = string_sgn(string) string.sort() addr = str2addr_o1(nsorb, nelec, string2bit(string)) civec[isA, isB] = civec2[addr] * sgn #print civec2[numpy.argwhere(civec2>1.e-10)] norm = numpy.linalg.norm(civec) print "NORM1=", norm #if(abs(norm-1.0)>1.e-6): exit(1) return civec
def toCItensor2(nsorb, nelec, civec): print '\n[toCItensor2]' ndim = misc.binomial(nsorb, nelec) print 'nsorb=', nsorb print 'nelec=', nelec print 'C_K^N=', ndim, civec.size if ndim != civec.size: print 'ndim=' print 'civec.size=' exit(1) # phys=K-N+1 nphys = nsorb - nelec + 1 citensor = numpy.zeros(nphys**nelec) print 'd_K^N=', citensor.shape stride = [nphys**(nelec - i - 1) for i in range(nelec)] offset = numpy.array(range(nelec)) for i in range(civec.size): bstring = bin(addr2str_o1(nsorb, nelec, i)) orblst0 = numpy.array(bit2string(int(bstring, 2), nsorb)) orblst1 = orblst0 - offset addr = numpy.dot(orblst1, stride) #print orblst0,orblst1,addr citensor[addr] = civec[i] print 'NORM of Tensor=', numpy.linalg.norm(citensor) return citensor
def toCItensor(nsorb, nelec, civec, iop=0): print '\n[toCItensor]: iop=', iop ndim = misc.binomial(nsorb, nelec) print 'nsorb=', nsorb print 'nelec=', nelec print 'C_K^N=', ndim, civec.size if ndim != civec.size: print 'ndim=' print 'civec.size=' exit(1) citensor = numpy.zeros(nsorb**nelec) print 'd_K^N=', citensor.shape stride = [nsorb**(nelec - i - 1) for i in range(nelec)] if iop == 0: for i in range(civec.size): bstring = bin(addr2str_o1(nsorb, nelec, i)) orblst = tuple(bit2string(int(bstring, 2), nsorb)) addr = numpy.dot(orblst, stride) citensor[addr] = civec[i] else: for i in range(civec.size): bstring = bin(addr2str_o1(nsorb, nelec, i)) orblst = tuple(bit2string(int(bstring, 2), nsorb)) for j in itools.permutations(orblst, nelec): addr = numpy.dot(j, stride) citensor[addr] = string_sgn(j) * civec[i] fac = 1.0 / math.sqrt(math.factorial(nelec)) citensor = citensor * fac print 'NORM of Tensor=', numpy.linalg.norm(citensor) return citensor
def toCIspinorb(norb, ne, civec): print '\n[toCIspinorb]: spin-orbit rep C(Ia,Ib)->C(I)' nsA, nsB = civec.shape nelec = ne[0] + ne[1] nsorb = 2 * norb ndim = misc.binomial(nsorb, nelec) print 'nsA,nsB=', nsA, nsB print 'nstring=', ndim print 'nfactor=', ndim * 1.0 / (nsA * nsB) civec2 = numpy.zeros(ndim) for isA in range(nsA): bsA = addr2str_o1(norb, ne[0], isA) strA = bit2string(bsA, norb) strA = [2 * i for i in strA] for isB in range(nsB): bsB = addr2str_o1(norb, ne[1], isB) strB = bit2string(bsB, norb) strB = [2 * i + 1 for i in strB] # conversion string = strA + strB sgn = string_sgn(string) string.sort() #print string2bit(string) #print string #print civec[isA,isB] #print addr #addr=fci.cistring.str2addr_o1(nsorb,nelec,string2bit(string)) addr = str2addr_o1(nsorb, nelec, string2bit(string)) #print nsorb,string,sgn,addr,bin(string2bit(string)) civec2[addr] = civec[isA, isB] * sgn #print civec2[numpy.argwhere(civec2>1.e-10)] norm = numpy.linalg.norm(civec2) print "NORM1=", norm if (abs(norm - 1.0) > 1.e-6): exit(1) return civec2
def toONtensorReverse(nsorb, nelec, ontensor): print '\n[toONtensorReverse]:' ndim = misc.binomial(nsorb, nelec) print 'nsorb=', nsorb print 'nelec=', nelec print 'C_K^N=', ndim civec = numpy.zeros(ndim) for i in range(civec.size): bstring = bin(addr2str_o1(nsorb, nelec, i)) addr = int(bstring, 2) civec[i] = ontensor[addr] print 'NORM of Tensor=', numpy.linalg.norm(civec) return civec
def toCItensorReverse(nsorb, nelec, citensor, iop=0): #print '\n[toCItensorReverse]: iop=',iop ndim = misc.binomial(nsorb, nelec) #print 'nsorb=',nsorb #print 'nelec=',nelec #print 'C_K^N=',ndim civec = numpy.zeros(ndim) stride = [nsorb**(nelec - i - 1) for i in range(nelec)] for i in range(civec.size): bstring = bin(addr2str_o1(nsorb, nelec, i)) orblst = tuple(bit2string(int(bstring, 2), nsorb)) addr = numpy.dot(orblst, stride) civec[i] = citensor[addr] if iop == 1: fac = math.sqrt(math.factorial(nelec)) civec = civec * fac #print 'NORM of Tensor=',numpy.linalg.norm(civec) return civec
def toONtensor(nsorb, nelec, civec): print '\n[toONtensor]:' ndim = misc.binomial(nsorb, nelec) print 'nsorb=', nsorb print 'nelec=', nelec print 'C_K^N=', ndim, civec.size if ndim != civec.size: exit(1) ontensor = numpy.zeros(2**nsorb) print 'd_2^K=', ontensor.shape for i in range(civec.size): bstring = bin(addr2str_o1(nsorb, nelec, i)) addr = int(bstring, 2) print 'idx=', i, bstring, addr # idx= 0 0b1 1 # idx= 1 0b10 2 # idx= 2 0b100 4 ontensor[addr] = civec[i] print 'NORM of Tensor=', numpy.linalg.norm(ontensor) return ontensor
def remove_redundant_reads(self): ''' remove additional redundant reads that are not warranted by a binomial test.''' def rm_max(list, max): # remove reads that are more than the maximum specified. list_new = [] pre = 0 count = 0 for x in list: if x == pre: count += 1 else: pre = x count = 1 if count > max: continue list_new.append(x) return list_new for filename in self.filename_list: max = misc.binomial( self.read_total_per_file[filename], 1.0/self.genome_size) # calculate maximum duplicates for each genomic location. info ("The maximum at one position for %s is %d", filename, max) total_before = self.read_total_per_file[filename] total_after = 0 for chr in self.chr_list: for strand in ["f", "r"]: reads = self.data_dict_by_strands[chr][filename][strand] reads.sort() reads = rm_max(reads, max) total_after += len(reads) self.data_dict_by_strands[chr][filename][strand] = reads self.read_total_per_file[filename] = total_after debug("Total # of reads before removing redundancies is %d", total_before) debug("Total # of reads after removing redundancies is %d", total_after) info("The percentage of redundant reads for %s is %f ", filename, 1-float(total_after)/total_before)
def remove_redundant_reads(self): ''' remove additional redundant reads that are not warranted by a binomial test.''' def rm_max(list, max): # remove reads that are more than the maximum specified. list_new = [] pre = 0 count = 0 for x in list: if x == pre: count += 1 else: pre = x count = 1 if count > max: continue list_new.append(x) return list_new for filename in self.filename_list: max = misc.binomial(self.read_total_per_file[filename], 1.0 / self.genome_size) # calculate maximum duplicates for each genomic location. info("The maximum at one position for %s is %d", filename, max) total_before = self.read_total_per_file[filename] total_after = 0 for chr in self.chr_list: for strand in ["f", "r"]: reads = self.data_dict_by_strands[chr][filename][strand] reads.sort() reads = rm_max(reads, max) total_after += len(reads) self.data_dict_by_strands[chr][filename][strand] = reads self.read_total_per_file[filename] = total_after debug("Total # of reads before removing redundancies is %d", total_before) debug("Total # of reads after removing redundancies is %d", total_after) info("The percentage of redundant reads for %s is %f ", filename, 1 - float(total_after) / total_before)