def test_attack(n0, n1, numCov, err=.001): n = n0 + n1 x = [rand.randint(0, 2) for i in range(0, n)] y = [1 for i in range(0, n)] for i in range(0, n0): y[i] = 0 covs = [[rand.randint(0, 1) for i in range(0, n)] for j in range(0, numCov)] ORs = [] for i in range(0, numCov): print i ret = [x] ret.append(covs[i]) X = np.asarray(ret).T X = AC(X, False) lr = LR(y, X) res_lr = lr.fit(disp=0) OR = math.exp(float(res_lr.params[0])) ret.append(attack(y, covs, OR, err)) ORs.append(OR) print "The number of matches is: " for r in ret: str(len(r)) print ORs for r in ret: print r print len(ret)
def runAnalysis(self,y): log_res=[0 for i in range(0,self.m)]; for i in range(0,self.m): I=[i] I.extend([-1]) x=self.X[:,I]; lr=LR(y,x); res_lr=lr.fit(disp=0) if self.params=="Coef": log_res[i]=float(res_lr.params[0]); if self.params=="Odds": coef=float(res_lr.params[0]); log_res[i]=math.exp(coef); """ if self.params=="pval": log_res[i]=; if self.params=="logpval": pval=; if pval>0: log_res[i]=-np.log10(pval); else: log_res[i]=-1.0; """ return np.asarray(log_res);
def Log_Calc(y, x): x1 = AC(x, False) lr = LR(y, x1) try: res_lr = lr.fit(disp=0) except: return -1 return math.exp(res_lr.params[0])
def test_attack_nocovar(n0, n1, err=.001): n = n0 + n1 x = [rand.randint(0, 2) for i in range(0, n)] y = [1 for i in range(0, n)] for i in range(0, n0): y[i] = 0 x = AC(x, False) lr = LR(y, x) res_lr = lr.fit(disp=0) OR = math.exp(float(res_lr.params[0])) ret = attack_no_covar(n0, n1, OR, err) print "The number of matches is: " + str(len(ret)) print OR for r in ret: print r
def attack_no_covar(n0, n1, OR, err=.001): n = n0 + n1 y = [1 for i in range(0, n)] for i in range(0, n0): y[i] = 0 num_match = 0 ret = [] ##iterate through all possibilities and test for i0 in range(0, n0 + 1): ##number in controls with 2 minor alleles for i1 in range( 0, (n0 - i0 + 1)): ##number in controls with 1 minor alleles for j0 in range(0, n1 + 1): ##number in cases with 2 minor alleles for j1 in range( 0, (n1 - j0 + 1)): ##number in cases with 1 minor alleles i2 = n0 - i0 - i1 j2 = n1 - j0 - j1 x = [0 for i in range(0, n)] x[:i0] = [2 for i in range(0, i0)] cur = i0 x[cur:cur + i1] = [1 for i in range(0, i1)] cur = cur + i1 cur = cur + i2 x[cur:cur + j0] = [2 for i in range(0, j0)] cur = cur + j0 x[cur:cur + j1] = [1 for i in range(0, j1)] res_lr = "" x = AC(x, False) lr = LR(y, x) try: res_lr = lr.fit(disp=0) except: #print x; continue try: OR_cur = math.exp(float(res_lr.params[0])) except: continue if round_sig(OR_cur, err) == round_sig(OR, err): num_match = num_match + 1 ret.append([i0, i1, i2, j0, j1, j2]) #print "match!\n"; return ret
def attack(y, cov, OR, err=.001, bnd=.5, numStep=10): n = n0 + n1 num_match = 0 ret = [] COV = [y] COV.extend(cov) COV = np.asarray(COV).T iter = IterTable(COV) cur = 0 iter.next() [yCur, covCur] = iter.get() while not iter.isDone(): #print cur; cur = cur + 1 [yCur, covCur] = iter.get() lr = LR(yCur, AC(covCur, False)) try: res_lr = lr.fit(disp=0) except: iter.next() continue OR_cur = 1.0 try: OR_cur = math.exp(float(res_lr.params[0])) except: iter.next() continue if abs(OR_cur - OR) < err: print "match" num_match = num_match + 1 ret.append(iter.getTable()) #if abs(OR-OR_cur)/OR_cur>bnd: #for i in range(0,numStep): #iter.next(); iter.next() return ret
def test_attack_pheno(n, numCov, err=.001): #n=n0+n1; x = [rand.randint(0, 2) for i in range(0, n)] #y=[1 for i in range(0,n)] #for i in range(0,n0): # y[i]=0; covs = [[rand.randint(0, 1) for i in range(0, n)] for j in range(0, numCov)] ORs = [] ret = [] for i in range(0, numCov): print i y = covs[i] #ret=[x]; #ret.append(covs[i]); #X=np.asarray(ret).T X = AC(x, False) lr = LR(y, X) res_lr = lr.fit(disp=0) OR = math.exp(float(res_lr.params[0])) n1 = sum(y) #n=len(y); n0 = n - n1 ret.append(AL.attack_no_covar(n0, n1, OR, err=.001)) ORs.append(OR) print "The number of matches is: " for r in ret: str(len(r)) print ORs marg = [] pos = 0 for r in ret: marg.append([]) for i in r: marg[pos].append((i[0] + i[3], i[1] + i[4], i[2] + i[5])) pos = pos + 1 print "\n" for mar in marg: print mar #marg=[list(set(mar)) for mar in marg]; inAll = [] notIn = False for m in marg[0]: for mar in marg: if not m in mar: notIn = True if not notIn: inAll.append(m) notIn = False poss = [[ret[j][i] for i in range(0, len(ret[j])) if marg[j][i] in inAll] for j in range(0, len(ret))] poss = [[tuple(i) for i in p] for p in poss] posCov = [(0), (1)] for i in range(1, m): dctnew = [] for d in PosCov: dctnew.append((d + (0))) dctnew.append((d + (1))) posCov = dctnew dctCov = {} for i in range(0, len(posCov)): dctCov[posCov[i]] = i cnt = {} for c in posCov: cnt[c] = 0 for i in range(0, len(covs[0])): cnt[tuple([c[i] for c in covs])] = cnt[tuple([c[i] for c in covs])] + 1 chosen = [] numchose = 0 testAll(chosen, numchose, poss, dctCov, cnt)
def test_attack_pheno(n,numCov,err=.001): print "Generate Data!" x=[rand.randint(0,2) for i in range(0,n)] covs=[[rand.randint(0,1) for i in range(0,n)] for j in range(0,numCov)] ORs=[]; ret=[]; print "Get ORs!" for i in range(0,numCov): print i; y=covs[i] #ret=[x]; #ret.append(covs[i]); #X=np.asarray(ret).T X=AC(x,False); lr=LR(y,X); res_lr=lr.fit(disp=0) OR=math.exp(float(res_lr.params[0])); n1=sum(y); #n=len(y); n0=n-n1; ret.append(AL.attack_no_covar(n0,n1,OR,err=.001)); ORs.append(OR) marg=[] pos=0; for r in ret: marg.append([]); for i in r: marg[pos].append((i[0]+i[3],i[1]+i[4],i[2]+i[5])) pos=pos+1; print "\n" for mar in marg: print mar; inAll=[]; notIn=False; for m in marg[0]: for mar in marg: if not m in mar: notIn=True; if not notIn: inAll.append(m); notIn=False; poss=[[ret[j][i] for i in range(0,len(ret[j])) if marg[j][i] in inAll] for j in range(0,len(ret))]; poss=[[tuple(i) for i in p] for p in poss] print "Creates list of all possible covariate combinations" posCov=[(0,),(1,)] for i in range(1,numCov): dctnew=[]; for d in posCov: dctnew.append((d+(0,))) dctnew.append((d+(1,))) posCov=dctnew print "Create map from covariate combinations into possible indices" dctCov={}; for i in range(0,len(posCov)): dctCov[posCov[i]]=i; print "Create map from possible covariate combinations to number of times they occur" cnt={} for c in posCov: cnt[c]=0; for i in range(0,len(covs[0])): cnt[tuple([c[i] for c in covs])]=cnt[tuple([c[i] for c in covs])]+1; print "Iterate over all possible combinations and find possible solutions!" chosen=[]; numchose=0; (A,numeq)=makeMat(covs) ret=testAll(chosen,numchose,poss,dctCov,cnt,A,numeq) print "Possible solutions:" print len(ret); #for r in ret: #print r; print ret[0];