Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
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)