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 meg_gain(self, meg_file=None): """ Call OpenMEEG's GainMEG method to calculate the final projection matrix. Optionaly saving the matrix for later use. The OpenMEEG matrix is converted to a Numpy array before return. """ LOG.info("Computing GainMEG...") meg_gain = om.GainMEG(self.om_inverse_head, self.om_source_matrix, self.om_head2sensor, self.om_source2sensor) LOG.info("meg_gain: %d x %d" % (meg_gain.nlin(), meg_gain.ncol())) if meg_file is not None: LOG.info("Saving meg_gain as %s..." % meg_file) meg_gain.save( os.path.join(OM_STORAGE_DIR, meg_file + OM_SAVE_SUFFIX)) return om.asarray(meg_gain)
def meg_gain(self, meg_file=None): """ Call OpenMEEG's GainMEG method to calculate the final projection matrix. Optionaly saving the matrix for later use. The OpenMEEG matrix is converted to a Numpy array before return. """ LOG.info("Computing GainMEG...") meg_gain = om.GainMEG(self.om_inverse_head, self.om_source_matrix, self.om_head2sensor, self.om_source2sensor) LOG.info("meg_gain: %d x %d" % (meg_gain.nlin(), meg_gain.ncol())) if meg_file is not None: LOG.info("Saving meg_gain as %s..." % meg_file) meg_gain.save(os.path.join(OM_STORAGE_DIR, meg_file + OM_SAVE_SUFFIX)) return om.asarray(meg_gain)
#print m1.ncol() m2 = om.Matrix() m2.load(ssm_file) #m2.setvalue(2,3,-0.2) # m2(2,3)=-0.2 #print m2(2,3) #print m2(0, 0) #print m2.nlin() #print m2.ncol() ############################################################################### # Numpy interface # For a Vector v = hm(1, 10, 1, 1).getcol(0) vec = om.asarray(v) m = om.fromarray(vec) assert ((v - m.getcol(0)).norm() < 1e-15) # For a Matrix mat = om.asarray(m2) assert ((m2 - om.fromarray(mat)).frobenius_norm() < 1e-15) #print mat.shape #print mat.sum() #mat[0:2, 1:3] = 0 #print mat[0:5, 0:5] #remove useless files os.remove(hm_file) os.remove(ssm_file)
import numpy as np import openmeeg as om from scipy import linalg from mayavi import mlab from mesh import Mesh cortex = Mesh("data/model/cortex.tri") electrodes = np.loadtxt('data/model/eeg_channels_locations.txt') squids = np.loadtxt('data/model/meg_channels_locations.squids') M = om.Matrix('leadfields/eeg_leadfield.mat') G_eeg = om.asarray(M) chan_idx = 28 n_dipoles = G_eeg.shape[1] ############################################################################### # Generate activation map with only one dipole active x_simu = np.zeros(n_dipoles) dipole_idx = 1000 x_simu[dipole_idx] = 1 # compute forward model m = np.dot(G_eeg, x_simu) # add measurement noise m += 1e-8 * np.random.randn(*m.shape) # show topography electrodes_mesh = Mesh("data/model/eeg_channels_mesh.tri")
m1 = om.SymMatrix() m1.load(hm_file) print m1(0, 0) print m1.nlin() print m1.ncol() m2 = om.Matrix() m2.load(ssm_file) print m2(0, 0) print m2.nlin() print m2.ncol() ############################################################################### # Numpy interface # For a Vector vec = om.asarray(ai_vector) print vec.size print vec[0:5] print vec # For a Matrix mat = om.asarray(m2) print mat.shape print mat.sum() mat[0:2, 1:3] = 0 print mat[0:5, 0:5]
#print m1.ncol() m2 = om.Matrix() m2.load(ssm_file) #m2.setvalue(2,3,-0.2) # m2(2,3)=-0.2 #print m2(2,3) #print m2(0, 0) #print m2.nlin() #print m2.ncol() ############################################################################### # Numpy interface # For a Vector v=hm(1,10,1,1).getcol(0) vec = om.asarray(v) m=om.fromarray(vec) assert((v-m.getcol(0)).norm() < 1e-15) # For a Matrix mat = om.asarray(m2) assert((m2-om.fromarray(mat)).frobenius_norm() < 1e-15) #print mat.shape #print mat.sum() #mat[0:2, 1:3] = 0 #print mat[0:5, 0:5] #remove useless files os.remove(hm_file) os.remove(ssm_file)