def main(argv): #create a dir for leadfields and tmp if not op.exists("tmp"): import os os.mkdir('tmp') if not op.exists("leadfields"): import os os.mkdir('leadfields') filename_O = 'leadfields/Original_' + argv + '.vtp' filename_R = 'leadfields/Reconstructed_' + argv + '.vtp' if recompute: # set matrices filenames filename_Xo = op.join('tmp', argv + '_Xo.mat') filename_CM = op.join('tmp', argv + '_CM.mat') model = load_headmodel(argv) # Compute the projector onto the sensors M = om.Head2EEGMat(model['geometry'], model['sensors']) # 'Brain' is the name of the domain containing the sources (a-priori) if recompute_CM or not op.exists(filename_CM): # CM, a matrix N_unknown X N_sensors #CM = om.CorticalMat(model['geometry'], M, 'Brain',3, alphas[argv], betas[argv], op.join('tmp',argv + '_P.mat')) CM = om.CorticalMat2(model['geometry'], M, 'Brain',3, gammas[argv], op.join('tmp',argv + '_H.mat')) CM.save(str(filename_CM)) else: CM = om.Matrix(str(filename_CM)) if model.has_key('dipsources'): # for testing: lets compute a forward solution with a few dipoles # and then display both the reconstruction through the CorticalMapping # and the original if recompute_Xo or not op.exists(filename_Xo): X_original = forward_problem(model) X_original.save(str(filename_Xo)) else: X_original = om.Matrix(str(filename_Xo)) V_s = M * X_original # get the potentials at sensors elif model.has_key('potentials'): V_s = model['potentials'] else: print("Error: either specify input potentials or dipsources to\ simulate them.") X_reconstructed = CM * V_s print "Error norm = ", (V_s-M * X_reconstructed).frobenius_norm() # write the geometry and the solution as a VTK file (viewable in pavaview) if model.has_key('dipsources'): model['geometry'].write_vtp(str(filename_O), X_original) model['geometry'].write_vtp(str(filename_R), X_reconstructed) if model.has_key('dipsources'): display_vtp(filename_O) compare_vtp(filename_O,filename_R) display_vtp(filename_R)
def main(argv): """Compute the tdcs.""" # create a dir for leadfields and tmp if not op.exists("tmp"): import os os.mkdir('tmp') if not op.exists("leadfields"): import os os.mkdir('leadfields') filename = 'leadfields/HDTDCS_' + argv + '.vtp' filename_HMi = op.join('tmp', argv + '_HMi.mat') if recompute: model = load_headmodel(argv) if recompute_HMi or not op.exists(filename_HMi): hm = om.HeadMat(model['geometry']) hm.invert() hm.save(filename_HMi) else: print("Loading %s" % filename_HMi) hm = om.SymMatrix(filename_HMi) sm = om.EITSourceMat(model['geometry'], model['tdcssources']) # set here the input currents (actually a current density [I/L]) activation = om.fromarray( np.array([[-4., 1.], [1., -4.], [1., 1.], [1., 1.], [1., 1.]])) # each column must have a zero mean # now apply the currents and get the result X = hm * (sm * activation) # concatenate X with input currents (to see the what was injected) Xt = np.append(om.asarray(X), np.zeros((model['geometry'].size() - X.nlin(), X.ncol())), 0) currents = om.asarray(activation) for s in range(model['tdcssources'].getNumberOfSensors()): # get the triangles supporting this sensor tris = model['tdcssources'].getInjectionTriangles(s) for it in tris: Xt[it.getindex(), :] = (currents[s, :] * model['tdcssources'].getWeights()(s)) X = om.fromarray(Xt) model['geometry'].write_vtp(filename, X) display_vtp(filename)
def main(argv): filename_O = "leadfields/Original_" + argv + ".vtp" filename_R = "leadfields/Reconstructed_" + argv + ".vtp" fig = plt.figure() ax = fig.add_subplot(111, projection="3d") # ax.xaxis.set_scale('log'); ax.yaxis.set_scale('log'); ax.zaxis.set_scale('log') N1 = 5 # choose sampling here N2 = 1 # choose sampling here xs = np.random.rand(N1, N2) ys = np.random.rand(N1, N2) zs = np.random.rand(N1, N2) alphas = np.logspace(0.3, 1.5, N1) betas = np.logspace(0.3, -0.3, N2) for alph in range(0, N1): for bet in range(0, N2): if recompute: # set matrices filenames filename_Xo = op.join("tmp", argv + "_Xo.mat") filename_CM = op.join("tmp", argv + "_CM.mat") model = load_headmodel(argv) # Compute the projector onto the sensors M = om.Head2EEGMat(model["geometry"], model["sensors"]) # 'Brain' is the name of the domain containing the sources (a-priori) if recompute_CM or not op.exists(filename_CM): alpha = alphas[alph] beta = betas[bet] # CM, a matrix N_unknown X N_sensors # CM = om.CorticalMat(model['geometry'], M, 'Brain',3,alpha,beta,op.join('tmp',argv + '_P.mat')) CM = om.CorticalMat2(model["geometry"], M, "Brain", 3, alpha, op.join("tmp", argv + "_H.mat")) CM.save(str(filename_CM)) else: CM = om.Matrix(str(filename_CM)) # for testing: lets compute a forward solution with a few dipoles # and then display both the reconstruction through the CorticalMapping # and the original if recompute_Xo or not op.exists(filename_Xo): X_original = forward_problem(model) X_original.save(str(filename_Xo)) else: X_original = om.Matrix(str(filename_Xo)) V_s = M * X_original # get the potentials at sensors X_reconstructed = CM * (V_s) # write the geometry and the solution as a VTK file (viewable in pavaview) model["geometry"].write_vtp(str(filename_R), X_reconstructed) norm = (V_s - M * X_reconstructed).getcol(0).norm() rdm, mag = compare_vtp(filename_O, filename_R) sys.stderr.write( "||=" + str(norm) + "\talpha=" + str(alpha) + "\tbeta=" + str(beta) + "\t\tRDM=" + str(rdm) + "\trMAG=" + str(mag) + "\t" + str(mag + rdm) + "\n" ) sys.stdout.write( "||=" + str(norm) + "\talpha=" + str(alpha) + "\tbeta=" + str(beta) + "\t\tRDM=" + str(rdm) + "\trMAG=" + str(mag) + "\t" + str(mag + rdm) + "\n" ) xs[alph, bet] = alpha ys[alph, bet] = beta zs[alph, bet] = rdm + mag ax.plot_wireframe(np.log(xs), np.log(ys), np.log(zs)) ax.set_xlabel("alpha") ax.set_ylabel("beta") ax.set_zlabel("RDM + MAG") i = np.nonzero(zs == np.min(zs)) sys.stderr.write("xs = " + str(xs[i]) + " ys = " + str(ys[i]) + " rdm+mag= " + str(np.min(zs)) + "\n") sys.stdout.write("xs = " + str(xs[i]) + " ys = " + str(ys[i]) + " rdm+mag= " + str(np.min(zs)) + "\n") plt.show()
def main(argv): """Search parameters for the cortical mapping.""" filename_O = 'leadfields/Original_' + argv + '.vtp' filename_R = 'leadfields/Reconstructed_' + argv + '.vtp' fig = plt.figure() ax = fig.add_subplot(111, projection='3d') # ax.xaxis.set_scale('log') # ax.yaxis.set_scale('log') # ax.zaxis.set_scale('log') N1 = 5 # choose sampling here N2 = 1 # choose sampling here xs = np.random.rand(N1, N2) ys = np.random.rand(N1, N2) zs = np.random.rand(N1, N2) alphas = np.logspace(0.3, 1.5, N1) betas = np.logspace(0.3, -0.3, N2) for alph in range(0, N1): for bet in range(0, N2): if recompute: # set matrices filenames filename_Xo = op.join('tmp', argv + '_Xo.mat') filename_CM = op.join('tmp', argv + '_CM.mat') model = load_headmodel(argv) # Compute the projector onto the sensors M = om.Head2EEGMat(model['geometry'], model['sensors']) # 'Brain' is the name of the domain containing the sources # (a-priori) if recompute_CM or not op.exists(filename_CM): alpha = alphas[alph] beta = betas[bet] # CM, a matrix N_unknown X N_sensors # CM = om.CorticalMat(model['geometry'], M, 'Brain', 3, # alpha, beta, op.join('tmp', argv + '_P.mat')) CM = om.CorticalMat2(model['geometry'], M, 'Brain', 3, alpha, op.join('tmp', argv + '_H.mat')) CM.save(str(filename_CM)) else: CM = om.Matrix(str(filename_CM)) # for testing: lets compute a forward solution with a few # dipoles and then display both the reconstruction through the # CorticalMapping and the original if recompute_Xo or not op.exists(filename_Xo): X_original = forward_problem(model) X_original.save(str(filename_Xo)) else: X_original = om.Matrix(str(filename_Xo)) V_s = M * X_original # get the potentials at sensors X_reconstructed = CM * (V_s) # write the geometry and the solution as a VTK file # (viewable in pavaview) model['geometry'].write_vtp(str(filename_R), X_reconstructed) norm = (V_s - M * X_reconstructed).getcol(0).norm() rdm, mag = compare_vtp(filename_O, filename_R) print("||=%f" % norm, "\talpha=%f" % alpha, "\tbeta=%f" % beta, "\t\tRDM=%f" % rdm, "\trMAG=%f" % mag, "\t", str(mag + rdm), "\n", file=sys.stderr) print("||=%f" % norm, "\talpha=%f" % alpha, "\tbeta=%f" % beta, "\t\tRDM=%f" % rdm, "\trMAG=%f" % mag, "\t", str(mag + rdm), "\n") xs[alph, bet] = alpha ys[alph, bet] = beta zs[alph, bet] = rdm + mag ax.plot_wireframe(np.log(xs), np.log(ys), np.log(zs)) ax.set_xlabel('alpha') ax.set_ylabel('beta') ax.set_zlabel('RDM + MAG') i = np.nonzero(zs == np.min(zs)) print('xs = %f' % xs[i], ' ys = %f' % ys[i], ' rdm+mag=%f' % np.min(zs), "\n", file=sys.stderr) print('xs = %f' % xs[i], ' ys = %f' % ys[i], ' rdm+mag=%f' % np.min(zs), "\n") plt.show()