def run(): def c2(c): return np.array([[1., c], [c, 1.]]) def cov(n, w, dr=0): distance = np.array([[((i - j + (n / 2.)) % n) - n / 2. for i in range(n)] for j in range(n)]) gauss = np.exp(-(distance**2) / (2. * w * w)) - dr out = np.dot(gauss, gauss) return out / out[0, 0] IN = np.array([[1., .1, .1]]) out = [] for s1 in np.linspace(.1, 5, 10): for s2 in np.linspace(.1, 5, 10): CC = s1 * cov(3, .1) NC = s2 * cov(3, .1) out.append([ GSM.gexp(0, IN, CC, NC, precom=False), np.linalg.det(CC), np.linalg.det(NC), np.linalg.norm(CC), np.linalg.norm(NC) ]) np.savetxt("./NCORtest.csv", out)
def make_GSM_resp(filt, quenched, K, mgs, tag, COR, ncor, n_trial=1): #make sure everything is the right type assert quenched in (True, False) assert type(ncor) == np.ndarray GV = [] GVnn = [] #generate samples for each filter set if quenched: nftemp = np.tile(filt, [n_trial, 1]) print(nftemp.shape) noise = np.reshape( np.random.multivariate_normal(np.zeros(np.prod(nftemp.shape[1:])), ncor, n_trial * filt.shape[0]), tuple([n_trial * filt.shape[0]]) + filt.shape[1:]) nftemp += noise else: nftemp = filt #run it for each value of k for k in K: GV.append( np.array([ MGSM.gexp(i, nftemp, COR, k * k * ncor, precom=True) for i in range(filt.shape[1]) ]).transpose()) GVnn.append(MGSM.gnn(filt, COR)) print(GV[-1].shape) GV = np.concatenate(GV) GVnn = np.concatenate(GVnn) print(tag) print(GV.shape) print(GVnn.shape) np.savetxt(tag + "_noisy.csv", GV) np.savetxt(tag + "_clean.csv", GVnn)
def get_seg_probs(X, CC, NC, P): print("segprob") SEG1 = MGSM.NPShared(X, CC, NC) * P[0] SEG21 = MGSM.NPShared(np.reshape(X[:, 0], [-1, 1]), np.array([[CC[0, 0]]]), np.array([[NC[0, 0]]])) SEG22 = MGSM.NPShared(np.reshape(X[:, 1], [-1, 1]), np.array([[CC[1, 1]]]), np.array([[NC[1, 1]]])) SEG2 = SEG21 * SEG22 * P[1] NORM = SEG1 + SEG2 nnSEG1 = MGSM.LPShared(X, CC) * P[0] nnSEG21 = MGSM.LPShared(np.reshape(X[:, 0], [-1, 1]), np.array([[CC[0, 0]]])) nnSEG22 = MGSM.LPShared(np.reshape(X[:, 1], [-1, 1]), np.array([[CC[1, 1]]])) nnSEG2 = nnSEG21 * nnSEG22 * P[1] nnNORM = nnSEG1 + nnSEG2 O1 = np.reshape(np.array([SEG1 / NORM, SEG2 / NORM]), [2, -1]) O2 = np.reshape(np.array([nnSEG1 / nnNORM, nnSEG2 / nnNORM]), [2, -1]) print(np.max(O1)) print(np.max(nnSEG2)) print(np.min(O1)) print(np.min(nnSEG2)) return O1, O2
def run_KW(k, w): resp1.append([]) resp2.append([]) resp3.append([]) nresp1.append([]) nresp2.append([]) nresp3.append([]) W = w CC = cov(N, .6) NC = cov(N, .6 * w) NC = NC * np.linalg.norm(CC) / np.linalg.norm(NC) NN = 0 * np.random.multivariate_normal(np.zeros(len(NC)), NC, [3, NUM_CON]) stim1 = np.array([cc * I1 for cc in con]) stim2 = np.array([cc * I2 for cc in con]) stim3 = np.array([cc * I3 for cc in con]) resp1[-1].append(MGSM.gexp(0, stim1 + NN[0] / (k), CC, NC / (k * k))) resp2[-1].append(MGSM.gexp(0, stim2 + NN[1] / (k), CC, NC / (k * k))) resp3[-1].append(MGSM.gexp(0, stim3 + NN[2] / (k), CC, NC / (k * k))) nresp1[-1].append(MGSM.gnn(stim1 + 0 * NN[0] / (k), CC)[:, 0]) nresp2[-1].append(MGSM.gnn(stim2 + 0 * NN[0] / (k), CC)[:, 0]) nresp3[-1].append(MGSM.gnn(stim3 + 0 * NN[0] / (k), CC)[:, 0])
def make_resp(filt, model, quenched, K, mgs, tag, COR, ncor, n_trial=1, NOISE=True): P = COR[0] CNS = COR[1] C1 = COR[2] C2 = COR[3] print(P.shape) print(CNS.shape) print(C1.shape) print(C2.shape) GVnn = MGSM.MGSM_gnn(filt, CNS, C1, C2, P, model) return GVnn
def run(): def c2(c): return np.array([[1., c], [c, 1.]]) def cov(n, w, dr=0): distance = np.array([[((i - j + (n / 2.)) % n) - n / 2. for i in range(n)] for j in range(n)]) gauss = np.exp(-(distance**2) / (2. * w * w)) - dr out = np.dot(gauss, gauss) return out / out[0, 0] def inp(n, w, c1, c2, i): stim1 = np.array([((j + (n / 2.)) % n) - (n / 2.) for j in range(n)]) stim2 = np.array([(((j - (i % n)) + (n / 2.)) % n) - (n / 2.) for j in range(n)]) gauss1 = np.exp(-(stim1**2) / (2. * w * w)) gauss2 = np.exp(-(stim2**2) / (2. * w * w)) return c1 * gauss1 + c2 * gauss2 LO = [] HI = [] nnLO = [] nnHI = [] ip = np.array([[1, 0], [0, 1], [1, 1]]) for c in np.linspace(-.9, .9, 20): cov = c2(c) nc = np.identity(2) * (1 + c) sHI = GSM.gexp(0, 20 * ip, cov, nc * (2 * 2), precom=True) sLO = GSM.gexp(0, ip, cov, nc * (2 * 2), precom=False) snnLO = GSM.gnn(ip, cov)[:, 0] snnHI = GSM.gnn(20 * ip, cov)[:, 0] HI.append([c, sHI[2] / (sHI[0])]) LO.append([c, sLO[2] / (sLO[0])]) nnHI.append([c, snnHI[2] / (snnHI[0])]) nnLO.append([c, snnLO[2] / (snnLO[0])]) HI = np.array(HI) LO = np.array(LO) nnHI = np.array(nnHI) nnLO = np.array(nnLO) plt.plot(HI[:, 0], [1 for x in HI], 'k--', linewidth=1) plt.plot(HI[:, 0], HI[:, 1], 'k') plt.plot(nnHI[:, 0], nnHI[:, 1], 'k--') plt.plot(LO[:, 0], LO[:, 1], 'r') plt.plot(nnLO[:, 0], nnLO[:, 1], 'r--') plt.xlabel("Correlation") plt.ylabel("Modulation Ratio") plt.xlim(-1, 1) plt.ylim(0, 2) plt.xticks([-.5, 0, .5]) plt.tight_layout() plt.savefig("./2DAIparam.pdf") print("done with AI") #what it is that we want to do here? We want to look at COS con = 20 * np.logspace(-2, 0, 50) NN = 2 WW = np.array([.15]) out1 = [] out2 = [] nnout1 = [] nnout2 = [] out12 = [] out22 = [] nnout12 = [] nnout22 = [] NCOV = np.identity(2) k = .75 I1 = np.array([[c1, c1 / 10] for c1 in con]) I2 = np.array([[c1 + con[-1] / 10, con[-1] + c1 / 10] for c1 in con]) I12 = np.array([[c1, 0] for c1 in con]) I22 = np.array([[c1, con[-1]] for c1 in con]) print(I1.shape) print(I2.shape) CC = c2(.6) NCOV = np.identity(2) out1 = GSM.gexp(0, I1, CC, NCOV / (k * k), precom=False) out2 = GSM.gexp(0, I2, CC, NCOV / (k * k), precom=False) nnout1 = GSM.gnn(I1, CC).T nnout2 = GSM.gnn(I2, CC).T out12 = GSM.gexp(0, I12, CC, NCOV / (k * k), precom=False) out22 = GSM.gexp(0, I22, CC, NCOV / (k * k), precom=False) nnout12 = GSM.gnn(I12, CC).T nnout22 = GSM.gnn(I22, CC).T print(nnout2) plt.figure() plt.plot(con / con[-1], out1, 'r') plt.plot(con / con[-1], out2, 'k') plt.plot(con / con[-1], nnout1[0], 'r--') plt.plot(con / con[-1], nnout2[0], 'k--') plt.xlabel("contrast") plt.ylabel("Respose") # plt.yscale("log") plt.xscale("log") plt.tight_layout() plt.savefig("./2Dssfig_0.pdf") plt.figure() plt.plot(con / con[-1], out12, 'r') plt.plot(con / con[-1], out22, 'k') plt.plot(con / con[-1], nnout12[0], 'r--') plt.plot(con / con[-1], nnout22[0], 'k--') plt.xlabel("contrast") plt.ylabel("Respose") # plt.yscale("log") plt.xscale("log") plt.tight_layout() plt.savefig("./2Dssfig_1.pdf")
return out elif t < 1.75 and TARGET: out += np.array([1.,.5]) elif t > .125 and MASK: out += np.array([.5,1.]) return out def stimfunc(STIM,t,nt,MASK = True,TARGET = True): if STIM == 1: return np.array([S1(T,MASK,TARGET) for T in np.linspace(0,t,nt)]) elif STIM == 2: return np.array([S2(T,MASK,TARGET) for T in np.linspace(0,t,nt)]) elif STIM == 3: return np.array([S3(T,MASK,TARGET) for T in np.linspace(0,t,nt)]) nta = 80 tot = .4 ta = tot/(nta) Fa = np.exp(-ta)*np.identity(2) stima = np.array([10*stimfunc(s,.4,nta + 1,MASK = M[1],TARGET = M[0])[:k] for k in range(1,81,5) for s in [1,2,3] for M in [[True,False],[False,True],[True,True]]]) stimp = np.array([10*stimfunc(s,.4,nta + 1,MASK = M[1],TARGET = M[0]) for s in [1,2,3] for M in [[True,False],[False,True],[True,True]]]) cor = [.1] resp = np.array([inference.att_gexp(0,np.array([s]),cov(c),cov(0),inference.Q_self_con(cov(c),Fa),Fa) for c in cor for s in stima]) np.savetxt("./att_2d_resp.csv",resp) np.savetxt("./att_2d_stim.csv",np.reshape(stimp,[-1,2]))
def run_w(): #what it is that we want to do here? We want to look at COS resp1 = [] resp2 = [] resp3 = [] nresp1 = [] nresp2 = [] nresp3 = [] NUM_CURVE = 20 NUM_CON = 50 CMAX = 1 CMIN = -2 N = 8 I1 = inp(N, .6, 1, 1, -1 + N / 2) I2 = inp(N, .6, 1, 0, -1 + N / 2) I3 = inp(N, .6, 0, 1, -1 + N / 2) KK = np.linspace(.27, .75, NUM_CURVE) con = np.logspace(CMIN, CMAX, NUM_CON) if 0: for k in KK: print(k) W = k CC = cov(N, W) NC = cov(N, W) np.savetxt("./covariance_test.csv", CC) stim1 = np.array( [cc * I1 for cc in np.logspace(CMIN, CMAX, NUM_CON)]) stim2 = np.array( [cc * I2 for cc in np.logspace(CMIN, CMAX, NUM_CON)]) stim3 = np.array( [cc * I3 for cc in np.logspace(CMIN, CMAX, NUM_CON)]) resp1.append(MGSM.gexp(0, stim1, CC, NC / (2 * 2), precom=False)) resp2.append(MGSM.gexp(0, stim2, CC, NC / (2 * 2), precom=False)) resp3.append(MGSM.gexp(0, stim3, CC, NC / (2 * 2), precom=False)) nresp1.append(MGSM.gnn(stim1, CC)[:, 0]) nresp2.append(MGSM.gnn(stim2, CC)[:, 0]) nresp3.append(MGSM.gnn(stim3, CC)[:, 0]) np.savetxt("./param_files/wresp1.csv", resp1) np.savetxt("./param_files/wresp2.csv", resp2) np.savetxt("./param_files/wresp3.csv", resp3) np.savetxt("./param_files/wnnresp1.csv", nresp1) np.savetxt("./param_files/wnnresp2.csv", nresp2) np.savetxt("./param_files/wnnresp3.csv", nresp3) else: resp1 = np.loadtxt("./param_files/wresp1.csv") resp2 = np.loadtxt("./param_files/wresp2.csv") resp3 = np.loadtxt("./param_files/wresp3.csv") nresp1 = np.loadtxt("./param_files/wnnresp1.csv") nresp2 = np.loadtxt("./param_files/wnnresp2.csv") nresp3 = np.loadtxt("./param_files/wnnresp3.csv") resp1 = np.array(resp1) resp2 = np.array(resp2) resp3 = np.array(resp3) nresp1 = np.array(nresp1) nresp2 = np.array(nresp2) nresp3 = np.array(nresp3) AI = resp1 / (resp2 + resp3) nAI = nresp1 / (nresp2 + nresp3) fig = plt.figure() ax = fig.add_subplot(111) cc = con / np.max(con) for k in range(0, len(AI), 2): ax.plot(cc, AI[k], label=str(KK[k])) ax.plot(cc, nAI[k], '--') ax.plot(con / np.max(con), [1 for k in range(len(cc))], 'k--') plt.xlabel("contrast") plt.ylabel("A.I.") plt.ylim(.5, 1.25) plt.tight_layout() fig.savefig("./paramfig_W.pdf") MAX = np.array([[KK[k] * 180 / np.pi, np.max(AI[k])] for k in range(len(AI))]) plt.figure() plt.plot(MAX[:, 0], MAX[:, 1], "r") plt.xscale('log') plt.tight_layout() plt.xlabel("w") plt.ylabel("Max AI") plt.savefig("./max_AI_by_W.pdf") pts = [] for k in range(len(AI)): pts.append([KK[k], get_trans(AI[k], con)]) pts = np.array(pts) plt.figure() plt.plot(pts[:, 0], pts[:, 1]) plt.xscale('log') plt.yscale('log') plt.tight_layout() plt.xlabel("w") plt.ylabel("Transition (con)") plt.savefig("./trans_by_W.pdf") plt.figure() plt.plot(KK, AI[:, -6]) plt.tight_layout() plt.title("con. = {}".format(KK[-6])) plt.xlabel("w") plt.ylabel("High Contrast AI") plt.savefig("./HighAI_by_W.pdf")
def on_off_response(data,SNR,dt,nframes): par = data["params"] #I want to make a bunch of gratings in 6 orientations #I want to simulate them many times, with noise, and record the full temporal response for a while(until stability?) #lets go for 100 presentations of each of the 6 gratings. #I need enough to do a numerical estimation of the mutual information if par["segmentation"] != "gsm": print("On off is only for GSM models!") exit() f_pos = model_tools.get_f_pos(par["filter_position"],par["filter_distance"]*np.max(par["wavelengths"]),par["n_surrounds"]) indices = np.concatenate([[[a,b,c] for a in range(par["n_angles"]) for b in range(len(par["wavelengths"])) for c in range(2)] for p in f_pos]) positions = np.concatenate([[p for a in range(par["n_angles"]) for b in range(len(par["wavelengths"])) for c in range(2)] for p in f_pos]) fullsize = int(5*max(par["wavelengths"]) + 2*np.max(f_pos)) minwav = np.min(par["wavelengths"]) out = [] grats = stim.make_grating(.5,0,par["wavelengths"][0],fullsize/2,fullsize) print("Getting Coefficients") #get filters coeffs = make_data.get_filter_maps(grats,data["kernels"]) print("DT: {}".format(dt)) path = [coeffs.shape[-1]/2 for k in range(nframes)] print(path) rundat = make_data.sample_path(coeffs,path,indices,positions)/np.array([data["fac"]]) print(rundat.shape) #add a few zeroes for before and after stimulus onset Z = np.zeros([2] + list(rundat.shape[1:])) #2 zeros before, 6 after rundat = np.concatenate([Z,rundat,Z,Z,Z,Z,Z],axis = 0) #get all filters ind = [k for k in range(len(indices))] feps = [[linalg.logm(m)/data["params"]["walk_dt"] for m in f] for f in data["F"]] FF = [[np.float32(linalg.expm(dt * data["params"]["walk_dt"] * m)) for m in f] for f in feps] #print(FF[0][0]) QQ = [[inference.Q_self_con(data["C"][k][m],FF[k][m]) for m in range(len(data["F"][k]))] for k in range(len(data["F"]))] NC = [[m/(SNR**2) for m in f] for f in data["C"]] runf = rundat #run the response analysis out = [] for k in range(1,runf.shape[0]): print("{}\t{}".format(k,runf.shape)) responses = inference.general_MGSM_g_att(np.array([runf[:k]]),data["segs"],data["C"],NC,QQ,FF,data["P"],ind,stable = True,op = False) out.append(responses) return np.array(out)
def run(): #what it is that we want to do here? We want to look at COS resp1 = [] resp2 = [] resp3 = [] nresp1 = [] nresp2 = [] nresp3 = [] NUM_CURVE = 20 NUM_CON = 50 CMAX = 2.5 CMIN = -2 N = 8 I1 = inp(N, float(N) / 5, 1, 1, -1 + N / 2) I2 = inp(N, float(N) / 5, 1, 0, -1 + N / 2) I3 = inp(N, float(N) / 5, 0, 1, -1 + N / 2) KK = np.logspace(-1, 1, NUM_CURVE) for k in KK: print(k) W = float(N) / (5) CC = cov(N, W) NC = CC np.savetxt("./covariance_test.csv", CC) stim1 = np.array([cc * I1 for cc in np.logspace(CMIN, CMAX, NUM_CON)]) stim2 = np.array([cc * I2 for cc in np.logspace(CMIN, CMAX, NUM_CON)]) stim3 = np.array([cc * I3 for cc in np.logspace(CMIN, CMAX, NUM_CON)]) resp1.append(MGSM.gexp(0, stim1, CC, NC / (k * k), precom=False)) resp2.append(MGSM.gexp(0, stim2, CC, NC / (k * k), precom=False)) resp3.append(MGSM.gexp(0, stim3, CC, NC / (k * k), precom=False)) nresp1.append(MGSM.gnn(stim1, CC)[:, 0]) nresp2.append(MGSM.gnn(stim2, CC)[:, 0]) nresp3.append(MGSM.gnn(stim3, CC)[:, 0]) resp1 = np.array(resp1) resp2 = np.array(resp2) resp3 = np.array(resp3) nresp1 = np.array(nresp1) nresp2 = np.array(nresp2) nresp3 = np.array(nresp3) AI = resp1 / (resp2 + resp3) nAI = nresp1 / (nresp2 + nresp3) fig = plt.figure() ax = fig.add_subplot(111) ax.set_prop_cycle("color", [cm(1. * i / NUM_CURVE) for i in range(NUM_CURVE)]) K = np.linspace(.5, 5, NUM_CURVE) cc = np.logspace(CMIN, CMAX, NUM_CON) / (10**CMAX) for k in range(0, len(AI), 1): ax.plot(cc, AI[k], label=str(K[k])) ax.plot(cc, nAI[k], '--') handles, labels = ax.get_legend_handles_labels() # ax.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3, # ncol=2, mode="expand", borderaxespad=0.) ax.plot(cc, [1 for k in range(len(cc))], 'k--') plt.xscale('log') fig.savefig("./paramfig.pdf") MAX = np.array([[KK[k], np.max(AI[k])] for k in range(len(AI))]) plt.figure() plt.plot(MAX[:, 0], MAX[:, 1], "r") plt.xscale('log') plt.savefig("./max_AI_by_k.pdf") pts = [] for k in range(len(AI)): print(np.argmax(AI[k])) for j in range(np.argmax(AI[k]), len(AI[k]) - 1): if AI[k][j] >= 1 and AI[k][j + 1] <= 1: pts.append([KK[k], cc[j]]) break pts = np.array(pts) plt.figure() plt.plot(pts[:, 0], pts[:, 1]) plt.xscale('log') plt.yscale('log') plt.savefig("./trans_by_K.pdf")
if args["fexp"]: feps = [[linalg.logm(m) / data["params"]["walk_dt"] for m in f] for f in data["F"]] FF = [[ np.float32(linalg.expm(args["dt"] * data["params"]["walk_dt"] * m)) for m in f ] for f in feps] else: FF = [[np.eye(len(m)) + args["dt"] * (m - np.eye(len(m))) for m in f] for f in data["F"]] #print(FF[0][0]) QQ = [[args["dt"] * m for m in f] for f in data["Q"]] NC = [[m / (args["snr"]**2) for m in f] for f in data["C"]] print(rundat.shape) test_max = [ inference.find_GSM_pia_max(np.array(k), data["C"][0][0], NC[0][0], QQ[0][0], FF[0][0], 0, np.inf, 10., 1.) for k in rundat[0] ] test_gapp = [ inference.att_egia(0, np.array(rundat[0][k]), test_max[k][0], data["C"][0][0], NC[0][0], QQ[0][0], FF[0][0]) for k in range(len(test_max)) ] print(test_gapp) print([k[0] for k in test_max])
def run_k(): #what it is that we want to do here? We want to look at COS resp1 = [] resp2 = [] resp3 = [] nresp1 = [] nresp2 = [] nresp3 = [] NUM_CURVE = 10 NUM_CON = 50 NUM_W = 20 CMAX = 1. CMIN = -2. N = 8 I1 = inp(N, .6, 1, 1, -1 + N / 2) I2 = inp(N, .6, 1, 0, -1 + N / 2) I3 = inp(N, .6, 0, 1, -1 + N / 2) KK = np.logspace(0, 1, NUM_CURVE) con = np.logspace(CMIN, CMAX, NUM_CON) WW = np.linspace(.1, 2, NUM_W) for w in range(len(WW)): resp1.append([]) resp2.append([]) resp3.append([]) nresp1.append([]) nresp2.append([]) nresp3.append([]) for k in KK: print(k) W = WW[w] CC = cov(N, W) NC = CC np.savetxt("./covariance_test.csv", CC) stim1 = np.array( [cc * I1 for cc in np.logspace(CMIN, CMAX, NUM_CON)]) stim2 = np.array( [cc * I2 for cc in np.logspace(CMIN, CMAX, NUM_CON)]) stim3 = np.array( [cc * I3 for cc in np.logspace(CMIN, CMAX, NUM_CON)]) resp1[w].append(MGSM.gexp(0, stim1, CC, NC / (k * k))) #,precom = False)) resp2[w].append(MGSM.gexp(0, stim2, CC, NC / (k * k))) #,precom = False)) resp3[w].append(MGSM.gexp(0, stim3, CC, NC / (k * k))) #,precom = False)) nresp1[w].append(MGSM.gnn(stim1, CC)[:, 0]) nresp2[w].append(MGSM.gnn(stim2, CC)[:, 0]) nresp3[w].append(MGSM.gnn(stim3, CC)[:, 0]) resp1 = np.array(resp1) resp2 = np.array(resp2) resp3 = np.array(resp3) nresp1 = np.array(nresp1) nresp2 = np.array(nresp2) nresp3 = np.array(nresp3) AI = resp1 / (resp2 + resp3) nAI = nresp1 / (nresp2 + nresp3) fig = plt.figure() ax = fig.add_subplot(111) ax.set_prop_cycle("color", [cm(1. * i / NUM_CURVE) for i in range(NUM_CURVE)]) K = np.linspace(.5, 5, NUM_CURVE) cc = 100 * np.logspace(CMIN, CMAX, NUM_CON) / (10**CMAX) for w in [1]: for k in range(0, len(AI[w]), len(AI[w]) / 5): ax.plot(100 * con / np.max(con), AI[w, k], label=str(K[k])) ax.plot(100 * con / np.max(con), nAI[w, k], '--') handles, labels = ax.get_legend_handles_labels() # ax.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3, # ncol=2, mode="expand", borderaxespad=0.) ax.plot(cc, [1 for k in range(len(cc))], 'k--') plt.xlabel("Contrast") plt.ylabel("A.I.") plt.tight_layout() fig.savefig("./paramfig_K.pdf") fig = plt.figure() ax = fig.add_subplot(111) ax.set_prop_cycle("color", [cm(1. * i / NUM_CURVE) for i in range(NUM_CURVE)]) WW = np.linspace(.1, 2, NUM_W) cc = 100 * np.logspace(CMIN, CMAX, NUM_CON) / (10**CMAX) for k in [5]: for w in range(0, len(AI), len(AI) / 5): ax.plot(100 * con / np.max(con), AI[w, k], label=str(WW[w])) ax.plot(100 * con / np.max(con), nAI[w, k], '--') handles, labels = ax.get_legend_handles_labels() # ax.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3, # ncol=2, mode="expand", borderaxespad=0.) ax.plot(cc, [1 for k in range(len(cc))], 'k--') plt.xlim([0, 20]) plt.xlabel("Contrast") plt.ylabel("A.I.") plt.tight_layout() fig.savefig("./paramfig_W.pdf") plt.figure() for w in [5]: MAX = np.array([[1. / KK[k], np.max(AI[w, k])] for k in range(len(AI[w]))]) plt.plot(MAX[:, 0], MAX[:, 1], colortab[0]) # plt.xscale('log') plt.xlabel("1/SNR") plt.ylabel("A.I.") plt.tight_layout() plt.savefig("./max_AI_by_k.pdf") plt.figure() for k in [5]: MAX = np.array([[WW[w] * 180. / np.pi, np.max(AI[w, k])] for w in range(len(AI))]) plt.plot(MAX[:, 0], MAX[:, 1], colortab[0]) plt.xlabel("Width (deg.)") plt.ylabel("A.I.") plt.tight_layout() plt.savefig("./max_AI_by_W.pdf") plt.figure() for w in [5]: pts = [] for k in range(len(AI[w])): pts.append( [1. / KK[k], get_trans(AI[w, k], 100 * con / np.max(con))]) pts = np.array(pts) plt.plot(pts[:, 0], pts[:, 1], colortab[0]) plt.xlabel("1/SNR") plt.ylabel("Contrast") plt.xscale('log') plt.yscale('linear') plt.tight_layout() plt.savefig("./trans_by_K.pdf") plt.figure() for k in [5]: pts = [] for w in range(len(AI)): pts.append([ WW[w] * 180. / np.pi, get_trans(AI[w, k], 100 * con / np.max(con)) ]) pts = np.array(pts) plt.plot(pts[:, 0], pts[:, 1], colortab[0]) plt.xlabel("Width (deg.)") plt.ylabel("Contrast") plt.yscale('linear') plt.tight_layout() plt.savefig("./trans_by_W.pdf")
def run_cov(): #what it is that we want to do here? We want to look at COS resp1 = [] resp2 = [] resp3 = [] nresp1 = [] nresp2 = [] nresp3 = [] NUM_CURVE = 10 NUM_CON = 50 NUM_W = 10 CMAX = 1. CMIN = -1. N = 8 I1 = inp(N, .6, 1, 1, -1 + N / 2) I2 = inp(N, .6, 1, 0, -1 + N / 2) I3 = inp(N, .6, 0, 1, -1 + N / 2) KK = np.logspace(0, 1, NUM_CURVE) con = np.logspace(CMIN, CMAX, NUM_CON) WW = [.1, .5, 1.] for k in WW: print(k) CC = cov(N, .5) if k == WW[0]: NC = np.identity(N) else: NC = cov(N, k) NC = NC * np.linalg.norm(CC) / (np.linalg.norm(NC) * 4) sn = 1 stim1 = np.array([cc * I1 for cc in np.logspace(CMIN, CMAX, NUM_CON)]) stim2 = np.array([cc * I2 for cc in np.logspace(CMIN, CMAX, NUM_CON)]) stim3 = np.array([cc * I3 for cc in np.logspace(CMIN, CMAX, NUM_CON)]) resp1.append(sn * MGSM.gexp(0, stim1 / sn, CC, NC, precom=False)) resp2.append(sn * MGSM.gexp(0, stim2 / sn, CC, NC, precom=False)) resp3.append(sn * MGSM.gexp(0, stim3 / sn, CC, NC, precom=False)) nresp1.append(MGSM.gnn(stim1, CC)[:, 0]) nresp2.append(MGSM.gnn(stim2, CC)[:, 0]) nresp3.append(MGSM.gnn(stim3, CC)[:, 0]) resp1 = np.array(resp1) resp2 = np.array(resp2) resp3 = np.array(resp3) nresp1 = np.array(nresp1) nresp2 = np.array(nresp2) nresp3 = np.array(nresp3) AI = resp1 / (resp2 + resp3) nAI = nresp1 / (nresp2 + nresp3) print(AI[-1]) fig = plt.figure() ax = fig.add_subplot(111) ax.set_prop_cycle("color", [cm(1. * i / NUM_CURVE) for i in range(NUM_CURVE)]) K = np.linspace(.5, 5, NUM_CURVE) cc = 100 * np.logspace(CMIN, CMAX, NUM_CON) / (10**CMAX) for k in range(0, len(AI)): ax.plot(100 * con / np.max(con), AI[k], colortab[k], label=str(K[k])) ax.plot(100 * con / np.max(con), nAI[k], colortab[k] + '--') handles, labels = ax.get_legend_handles_labels() # ax.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3, # ncol=2, mode="expand", borderaxespad=0.) ax.plot(cc, [1 for k in range(len(cc))], 'k--') plt.xlabel("Contrast") plt.ylabel("A.I.") plt.tight_layout() fig.savefig("./paramfig_NC.pdf")
grat = stim.make_SS_filters(.5, 0, 0, 0, 16, 0, 5 * 16, int(np.max(np.linalg.norm(sites, axis=1))), get_grat=True) inp = np.array([get_grating_data(g, sites, RF) / fac for g in grat]) nc = CNS f = .5 * np.identity(len(CNS)) q = inf.Q_self_con(CNS, f) inp += np.random.multivariate_normal(np.zeros_like(inp[0]), nc, len(inp)) aI = np.tile(np.expand_dims(inp, 1), [1, 3, 1]) resp = inf.gnn(inp, CNS) iresp = inf.att_gexp(0, aI, CNS, nc, q, f) f = .75 * np.identity(len(CNS)) q = inf.Q_self_con(CNS, f) aresp = inf.att_gexp(0, aI, CNS, nc, q, f) print(oris)
def run_k(): #what it is that we want to do here? We want to look at COS resp1 = [] resp2 = [] resp3 = [] nresp1 = [] nresp2 = [] nresp3 = [] NUM_CURVE = 10 NUM_CON = 50 CMAX = 1. CMIN = -2. N = 8 I1 = inp(N, .6, 1, 1, -1 + N / 2) I2 = inp(N, .6, 1, 0, -1 + N / 2) I3 = inp(N, .6, 0, 1, -1 + N / 2) KK = np.logspace(0, 1, NUM_CURVE) con = np.logspace(CMIN, CMAX, NUM_CON) if 0: for k in KK: print(k) W = .6 CC = cov(N, W) NC = CC np.savetxt("./covariance_test.csv", CC) stim1 = np.array( [cc * I1 for cc in np.logspace(CMIN, CMAX, NUM_CON)]) stim2 = np.array( [cc * I2 for cc in np.logspace(CMIN, CMAX, NUM_CON)]) stim3 = np.array( [cc * I3 for cc in np.logspace(CMIN, CMAX, NUM_CON)]) resp1.append(MGSM.gexp(0, stim1, CC, NC / (k * k), precom=False)) resp2.append(MGSM.gexp(0, stim2, CC, NC / (k * k), precom=False)) resp3.append(MGSM.gexp(0, stim3, CC, NC / (k * k), precom=False)) nresp1.append(MGSM.gnn(stim1, CC)[:, 0]) nresp2.append(MGSM.gnn(stim2, CC)[:, 0]) nresp3.append(MGSM.gnn(stim3, CC)[:, 0]) np.savetxt("./param_files/kresp1.csv", resp1) np.savetxt("./param_files/kresp2.csv", resp2) np.savetxt("./param_files/kresp3.csv", resp3) np.savetxt("./param_files/knnresp1.csv", nresp1) np.savetxt("./param_files/knnresp2.csv", nresp2) np.savetxt("./param_files/knnresp3.csv", nresp3) else: resp1 = np.loadtxt("./param_files/kresp1.csv") resp2 = np.loadtxt("./param_files/kresp2.csv") resp3 = np.loadtxt("./param_files/kresp3.csv") nresp1 = np.loadtxt("./param_files/knnresp1.csv") nresp2 = np.loadtxt("./param_files/knnresp2.csv") nresp3 = np.loadtxt("./param_files/knnresp3.csv") resp1 = np.array(resp1) resp2 = np.array(resp2) resp3 = np.array(resp3) nresp1 = np.array(nresp1) nresp2 = np.array(nresp2) nresp3 = np.array(nresp3) AI = resp1 / (resp2 + resp3) nAI = nresp1 / (nresp2 + nresp3) fig = plt.figure() ax = fig.add_subplot(111) K = np.linspace(.5, 5, NUM_CURVE) cc = np.logspace(CMIN, CMAX, NUM_CON) / (10**CMAX) for k in range(0, len(AI), 2): ax.plot(con / np.max(con), AI[k], label=str(K[k])) ax.plot(con / np.max(con), nAI[k], '--') ax.plot(cc, [1 for k in range(len(cc))], 'k--') plt.tight_layout() plt.xlabel("contrast") plt.ylabel("AI") plt.ylim(0, 1.5) fig.savefig("./paramfig_K.pdf") MAX = np.array([[1. / KK[k], np.max(AI[k])] for k in range(len(AI))]) plt.figure() plt.plot(MAX[:, 0], MAX[:, 1], "r") plt.xscale('log') plt.tight_layout() plt.xlabel("1/SNR") plt.ylabel("Max AI") plt.savefig("./max_AI_by_k.pdf") plt.figure() plt.plot(con / np.max(con), resp2[NUM_CURVE / 2, :], "r") plt.plot(con / np.max(con), nresp2[NUM_CURVE / 2, :], "r--") plt.xscale('log') plt.tight_layout() plt.xlabel("Contrast") plt.ylabel("Response") plt.savefig("./param_CRF.pdf") pts = [] for k in range(len(AI)): pts.append([KK[k], get_trans(AI[k], con)]) pts = np.array(pts) plt.figure() plt.plot(1. / pts[:, 0], pts[:, 1]) plt.xscale('log') # plt.yscale('log') plt.tight_layout() plt.xlabel("1/SNR") plt.ylabel("Transition (con.)") plt.savefig("./trans_by_K.pdf")
import numpy as np import GSM.MGSM_inference as MGSM cov = np.array([[1, .1], [.1, 1]]) ncov = np.array([[1, 0], [0, 1]]) qcov = np.array([[.01, 0], [0, .01]]) print("cov", cov) print("ncov", ncov) print("qcov", qcov) ff = np.array([[1, 0], [1, 1]]) res = MGSM.att_gexp(0, ff, ff, cov, ncov, qcov) print(res)
def self_con(Q): return [ MGSM.F_self_con(CNS, Q[0]), [MGSM.F_self_con(C1[k], Q[1][k]) for k in range(4)], [MGSM.F_self_con(C2[k], Q[2][k]) for k in range(4)] ]
#S = np.random.rand(m,m) #S = np.dot(S,S.transpose()) S = np.array([[ np.exp(1 + np.cos(x - y)) for y in np.linspace(0, 2 * np.pi - (2. * np.pi / m), m) ] for x in np.linspace(0, 2 * np.pi - (2. * np.pi / m), m)]) print(S.shape) b = np.random.multivariate_normal(np.zeros(m), S, n) dd = a * b chtest = MGSM.CHtov( np.fliplr( np.flipud( np.transpose(np.linalg.cholesky(np.cov(np.transpose(dd)) / 2))))) print(MGSM.log_likelihood_center(dd, S)) out, test = fit.fit_center(dd, GRAD=True, init=chtest) print("TRUE", MGSM.log_likelihood_center(dd, S)) print(np.mean(np.abs(S))) print(np.mean(np.abs(S - out))) #for k in range(len(test)): # print(np.mean(np.abs(S-test[k]))) np.savetxt("./truecov.csv", S) np.savetxt("./fitcov.csv", out)
def make_resp(filt, model, quenched, K, mgs, tag, COR, ncor, n_trial=1): P = COR[0] CNS = COR[1] C1 = COR[2] C2 = COR[3] NCNS = ncor[0] NC1 = ncor[1] NC2 = ncor[2] fullcor = ncor[3] print(CNS.shape) print(C1.shape) #make sure everything is the right type assert model in ("ours", "coen_cagli") assert quenched in (True, False) assert type(ncor) == list GV = [] GVnn = [] #generate samples for each filter set if quenched: nftemp = np.tile(filt, [n_trial, 1, 1]) noise = np.reshape( np.random.multivariate_normal(np.zeros(np.prod(nftemp.shape[1:])), fullcor, n_trial * filt.shape[0]), tuple([n_trial * filt.shape[0]]) + filt.shape[1:]) nftemp += noise else: nftemp = filt #run it for each value of k SP = [] SPnn = [] for k in K: print(k) print(nftemp.shape) GV.append( MGSM.MGSM_g(nftemp, [CNS, C1, C2], [NCNS * (k * k), NC1 * (k * k), NC2 * (k * k)], P)) GVnn.append(MGSM.MGSM_gnn(filt, [CNS, C1, C2], P)) if model == "ours": SP.append( MGSM.get_noisy_seg_weight(filt, CNS, C1, C2, NCNS * (k * k), NC1 * (k * k), NC2 * (k * k), P)) SPnn.append(MGSM.get_seg_weight(filt, CNS, C1, C2, P)) elif model == "coen_cagli": SP.append( MGSM.get_noisy_CC_seg_weight(filt, CNS, C1, C2, NCNS * (k * k), NC1 * (k * k), NC2 * (k * k), P)) SPnn.append(MGSM.get_CC_seg_weight(filt, CNS, C1, C2, P)) GV = np.concatenate(GV) GVnn = np.concatenate(GVnn) SP = np.concatenate(SP) SPnn = np.concatenate(SPnn) np.savetxt(tag + "_noisy.csv", GV) np.savetxt(tag + "_clean.csv", GVnn) np.savetxt(tag + "_SP_noisy.csv", SP) np.savetxt(tag + "_SP_clean.csv", SPnn) return GV, GVnn
def make_att_resp(filt1, filt2, model, quenched, K, mgs, tag, COR, ncor, qcor, fcor, n_trial=1): P = COR[0] CNS = COR[1] C1 = COR[2] C2 = COR[3] NCNS = ncor[0] NC1 = ncor[1] NC2 = ncor[2] QCNS = qcor[0] QC1 = qcor[1] QC2 = qcor[2] FCNS = fcor[0] FC1 = fcor[1] FC2 = fcor[2] fullcor = ncor[3] fullqcor = qcor[3] print(CNS.shape) print(C1.shape) #make sure everything is the right type assert model in ("ours", "coen_cagli") assert quenched in (True, False) assert type(ncor) == list GV = [] GVnn = [] #run it for each value of k SP = [] SPnn = [] for k in K: print(k) GV.append( MGSM.MGSM_att_g(filt1, filt2, [CNS, C1, C2], [NCNS * (k * k), NC1 * (k * k), NC2 * (k * k)], [QCNS, QC1, QC2], [FCNS, FC1, FC2], P)) GVnn.append(MGSM.MGSM_gnn(filt2, [CNS, C1, C2], P)) SP.append( MGSM.get_att_CC_seg_weight(filt1, filt2, CNS, C1, C2, NCNS * (k * k), NC1 * (k * k), NC2 * (k * k), QCNS, QC1, QC2, FCNS, FC1, FC2, P)) SPnn.append(MGSM.get_CC_seg_weight(filt2, CNS, C1, C2, P)) GV = np.concatenate(GV) GVnn = np.concatenate(GVnn) SP = np.concatenate(SP) SPnn = np.concatenate(SPnn) np.savetxt(tag + "_noisy.csv", GV) np.savetxt(tag + "_clean.csv", GVnn) np.savetxt(tag + "_SP_noisy.csv", SP) np.savetxt(tag + "_SP_clean.csv", SPnn) return GV, GVnn
def mutual_information_data(data,dt,snr = [.5,1.],n_samples=1000,use_grat = True): #get the BSDS file location F = open("./CONFIG","r") for l in F: BSDSloc = l.split("=")[1] break F.close() img_names = glob.glob(BSDSloc + "*.jpg") ########################### maxframe = 20 par = data["params"] #I want to make a bunch of gratings in 3 orientations #I want to simulate them many times, with noise, and record the full temporal response for a while(until stability?) #lets go for 100 presentations of each of the 3 gratings. #I need enough to do a numerical estimation of the mutual information if par["segmentation"] != "gsm": print("Mutual Information is only for GSM models!") exit() f_pos = model_tools.get_f_pos(par["filter_position"],par["filter_distance"]*np.max(par["wavelengths"]),par["n_surrounds"]) indices = np.concatenate([[[a,b,c] for a in range(par["n_angles"]) for b in range(len(par["wavelengths"])) for c in range(2)] for p in f_pos]) positions = np.concatenate([[p for a in range(par["n_angles"]) for b in range(len(par["wavelengths"])) for c in range(2)] for p in f_pos]) fullsize = int(5*max(par["wavelengths"]) + 2*np.max(f_pos)) minwav = np.min(par["wavelengths"]) out = [] outv = [] imnum = 0 imind = np.random.choice(np.arange(len(img_names)),3,replace = False) for o in np.linspace(0,np.pi,4)[:-1]: print("ORI: {}".format(o)) #make the gratings if use_grat: grats = stim.make_grating(1.,o,par["wavelengths"][0],fullsize/2,fullsize) print("Getting Coefficients") #get filters else: grats = proc.load_grayscale_image(img_names[imind[imnum]])#.make_grating(1.,o,par["wavelengths"][0],fullsize/2,fullsize) imnum += 1 print("Getting Coefficients") #get filters coeffs = make_data.get_filter_maps(grats,data["kernels"]) print(np.array(coeffs).shape) #extract the right ones out.append([]) outv.append([]) for n in range(len(snr)): out[-1].append([]) outv[-1].append([]) print("SNR: {}".format(snr[n])) path = [coeffs.shape[-1]/2 for k in range(maxframe)] print(path) rundat = make_data.sample_path(coeffs,path,indices,positions)/np.array([data["fac"]]) print(rundat.shape) #add a few zeroes for before stimulus onset Z = np.zeros([int(2)] + list(rundat.shape[1:])) rundat = np.concatenate([Z,rundat],axis = 0) #get all filters ind = [k for k in range(len(indices))] feps = [[linalg.logm(m)/data["params"]["walk_dt"] for m in f] for f in data["F"]] FF = [[np.float32(linalg.expm(dt * data["params"]["walk_dt"] * m)) for m in f] for f in feps] #print(FF[0][0]) QQ = [[inference.Q_self_con(data["C"][k][m],FF[k][m]) for m in range(len(data["F"][k]))] for k in range(len(data["F"]))] NC = [[m/((snr[n])**2) for m in f] for f in data["C"]] #add noise noise = np.random.multivariate_normal(np.zeros(rundat.shape[1]),NC[0][0],[n_samples,rundat.shape[0]]) runf = noise + np.expand_dims(rundat,0) #run the response analysis for k in [1,len(Z)] + range(len(Z)+1,runf.shape[1],2) + [runf.shape[1]]: print("{}\t{}".format(k,runf[:,:k].shape)) responses = inference.general_MGSM_g_att(runf[:,:k],data["segs"],data["C"],NC,QQ,FF,data["P"],ind,stable = True,op = False) vresponses = inference.general_MGSM_p_att(runf[:,:k],data["segs"],data["C"],NC,QQ,FF,data["P"],ind,stable = True,op = False) out[-1][-1].append(responses) outv[-1][-1].append(vresponses) out[-1][-1] = np.array(out[-1][-1]) outv[-1][-1] = np.array(outv[-1][-1]) return out,outv
def run_WDIFF_plots(): #what it is that we want to do here? We want to look at COS resp1 = [] resp2 = [] resp3 = [] nresp1 = [] nresp2 = [] nresp3 = [] NUM_CURVE = 10 NUM_CON = 50 NUM_W = 20 CMAX = 1. CMIN = -2. N = 8 I1 = inp(N,.6,1,1,-1 + N/2) I2 = inp(N,.6,1,0,-1 + N/2) I3 = inp(N,.6,0,1,-1 + N/2) WW = np.logspace(.1,10,NUM_CURVE) con = np.logspace(CMIN,CMAX,NUM_CON) for w in [0,1,-1]: resp1.append([]) resp2.append([]) resp3.append([]) nresp1.append([]) nresp2.append([]) nresp3.append([]) for k in WW: W = w CC = cov(N,.6) if W == 0 or W == -1: NC = cov_special(N,W) else: NC = CC.copy() NC = NC*np.linalg.norm(CC)/np.linalg.norm(NC) np.savetxt("./covariance_test.csv",CC) stim1 = np.array([cc * I1 for cc in np.logspace(CMIN,CMAX,NUM_CON)]) stim2 = np.array([cc * I2 for cc in np.logspace(CMIN,CMAX,NUM_CON)]) stim3 = np.array([cc * I3 for cc in np.logspace(CMIN,CMAX,NUM_CON)]) resp1[-1].append(MGSM.gexp(0,stim1,CC,NC/(k*k))) resp2[-1].append(MGSM.gexp(0,stim2,CC,NC/(k*k))) resp3[-1].append(MGSM.gexp(0,stim3,CC,NC/(k*k))) nresp1[-1].append(MGSM.gnn(stim1,CC)[:,0]) nresp2[-1].append(MGSM.gnn(stim2,CC)[:,0]) nresp3[-1].append(MGSM.gnn(stim3,CC)[:,0]) resp1 = np.array(resp1) resp2 = np.array(resp2) resp3 = np.array(resp3) nresp1 = np.array(nresp1) nresp2 = np.array(nresp2) nresp3 = np.array(nresp3) AI = resp1/(resp2+resp3) nAI = nresp1/(nresp2+nresp3) for w in range(len(AI)): fig = plt.figure() ax = fig.add_subplot(111) ax.set_prop_cycle("color",[cm(1.*i/NUM_CURVE) for i in range(NUM_CURVE)]) K = np.linspace(.5,5,NUM_CURVE) cc = 100*np.logspace(CMIN,CMAX,NUM_CON)/(10**CMAX) for k in range(0,len(AI[w]),len(AI[w])/5): ax.plot(100*con/np.max(con),AI[w,k],label=str(K[k])) ax.plot(100*con/np.max(con),nAI[w,k],'--') handles, labels = ax.get_legend_handles_labels() # ax.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3, # ncol=2, mode="expand", borderaxespad=0.) ax.plot(cc,[1 for k in range(len(cc))],'k--') plt.xlim([0,20]) plt.xlabel("Contrast") plt.ylabel("A.I.") plt.tight_layout() fig.savefig("./paramfig_K_{}_by_W.pdf".format(w))