Example #1
0
def plot_visualize_mft_sources(fwdmag, stcdata, tmin, tstep, subject,
                               subjects_dir):
    '''
    Plot the MFT sources at time point of peak.
    '''
    print "##### Attempting to plot:"
    # cf. decoding/plot_decoding_spatio_temporal_source.py
    vertices = [s['vertno'] for s in fwdmag['src']]
    if len(vertices) == 1:
        vertices = [
            fwdmag['src'][0]['vertno']
            [fwdmag['src'][0]['rr'][fwdmag['src'][0]['vertno']][:, 0] <= -0.],
            fwdmag['src'][0]['vertno'][
                fwdmag['src'][0]['rr'][fwdmag['src'][0]['vertno']][:, 0] > -0.]
        ]

    stc_feat = SourceEstimate(stcdata,
                              vertices=vertices,
                              tmin=-0.2,
                              tstep=tstep,
                              subject=subject)
    for hemi in ['lh', 'rh']:
        brain = stc_feat.plot(surface='white',
                              hemi=hemi,
                              subjects_dir=subjects_dir,
                              transparent=True,
                              clim='auto')
        brain.show_view('lateral')
        # use peak getter to move visualization to the time point of the peak
        tmin = 0.095
        tmax = 0.10
        print "Restricting peak search to [%fs, %fs]" % (tmin, tmax)
        if hemi == 'both':
            vertno_max, time_idx = stc_feat.get_peak(hemi='rh',
                                                     time_as_index=True,
                                                     tmin=tmin,
                                                     tmax=tmax)
        else:
            vertno_max, time_idx = stc_feat.get_peak(hemi=hemi,
                                                     time_as_index=True,
                                                     tmin=tmin,
                                                     tmax=tmax)
        if hemi == 'lh':
            comax = fwdmag['src'][0]['rr'][vertno_max]
            print "hemi=%s: vertno_max=%d, time_idx=%d fwdmag['src'][0]['rr'][vertno_max] = " %\
                  (hemi, vertno_max, time_idx), comax
        elif len(fwdmag['src']) > 1:
            comax = fwdmag['src'][1]['rr'][vertno_max]
            print "hemi=%s: vertno_max=%d, time_idx=%d fwdmag['src'][1]['rr'][vertno_max] = " %\
                  (hemi, vertno_max, time_idx), comax

        print "hemi=%s: setting time_idx=%d" % (hemi, time_idx)
        brain.set_data_time_index(time_idx)
        # draw marker at maximum peaking vertex
        brain.add_foci(vertno_max,
                       coords_as_verts=True,
                       hemi=hemi,
                       color='blue',
                       scale_factor=0.6)
        offsets = np.append([0], [s['nuse'] for s in fwdmag['src']])
        if hemi == 'lh':
            ifoci = [
                np.nonzero([
                    stcdata[0:offsets[1], time_idx] >=
                    0.25 * np.max(stcdata[:, time_idx])
                ][0])
            ]
            vfoci = fwdmag['src'][0]['vertno'][ifoci[0][0]]
            cfoci = fwdmag['src'][0]['rr'][vfoci]
            print "Coords  of %d sel. vfoci: " % cfoci.shape[0]
            print cfoci
            print "vfoci: "
            print vfoci
            print "brain.geo['lh'].coords[vfoci] : "
            print brain.geo['lh'].coords[vfoci]
        elif len(fwdmag['src']) > 1:
            ifoci = [
                np.nonzero([
                    stcdata[offsets[1]:, time_idx] >=
                    0.25 * np.max(stcdata[:, time_idx])
                ][0])
            ]
            vfoci = fwdmag['src'][1]['vertno'][ifoci[0][0]]
            cfoci = fwdmag['src'][1]['rr'][vfoci]
            print "Coords  of %d sel. vfoci: " % cfoci.shape[0]
            print cfoci
            print "vfoci: "
            print vfoci
            print "brain.geo['rh'].coords[vfoci] : "
            print brain.geo['rh'].coords[vfoci]

        mrfoci = np.zeros(cfoci.shape)
        invmri_head_t = invert_transform(fwdmag['info']['mri_head_t'])
        mrfoci = apply_trans(invmri_head_t['trans'], cfoci, move=True)
        print "mrfoci: "
        print mrfoci

        # Just some blops:
        bloblist = np.zeros((300, 3))
        for i in xrange(100):
            bloblist[i, 0] = float(i)
            bloblist[i + 100, 1] = float(i)
            bloblist[i + 200, 2] = float(i)
        mrblobs = apply_trans(invmri_head_t['trans'], bloblist, move=True)
        brain.save_image('testfig_map_%s.png' % hemi)
        brain.close()
Example #2
0
def plot_visualize_mft_sources(fwdmag, stcdata, tmin, tstep,
                               subject, subjects_dir):
    '''
    Plot the MFT sources at time point of peak.
    '''
    print "##### Attempting to plot:"
    # cf. decoding/plot_decoding_spatio_temporal_source.py
    vertices = [s['vertno'] for s in fwdmag['src']]
    if len(vertices) == 1:
        vertices = [fwdmag['src'][0]['vertno'][fwdmag['src'][0]['rr'][fwdmag['src'][0]['vertno']][:, 0] <= -0.],
                    fwdmag['src'][0]['vertno'][fwdmag['src'][0]['rr'][fwdmag['src'][0]['vertno']][:, 0] > -0.]]

    stc_feat = SourceEstimate(stcdata, vertices=vertices,
                              tmin=-0.2, tstep=tstep, subject=subject)
    for hemi in ['lh', 'rh']:
        brain = stc_feat.plot(surface='white', hemi=hemi, subjects_dir=subjects_dir,
                              transparent=True, clim='auto')
        brain.show_view('lateral')
        # use peak getter to move visualization to the time point of the peak
        tmin = 0.095
        tmax = 0.10
        print "Restricting peak search to [%fs, %fs]" % (tmin, tmax)
        if hemi == 'both':
            vertno_max, time_idx = stc_feat.get_peak(hemi='rh', time_as_index=True,
                                                     tmin=tmin, tmax=tmax)
        else:
            vertno_max, time_idx = stc_feat.get_peak(hemi=hemi, time_as_index=True,
                                                     tmin=tmin, tmax=tmax)
        if hemi == 'lh':
            comax = fwdmag['src'][0]['rr'][vertno_max]
            print "hemi=%s: vertno_max=%d, time_idx=%d fwdmag['src'][0]['rr'][vertno_max] = " %\
                  (hemi, vertno_max, time_idx), comax
        elif len(fwdmag['src']) > 1:
            comax = fwdmag['src'][1]['rr'][vertno_max]
            print "hemi=%s: vertno_max=%d, time_idx=%d fwdmag['src'][1]['rr'][vertno_max] = " %\
                  (hemi, vertno_max, time_idx), comax

        print "hemi=%s: setting time_idx=%d" % (hemi, time_idx)
        brain.set_data_time_index(time_idx)
        # draw marker at maximum peaking vertex
        brain.add_foci(vertno_max, coords_as_verts=True, hemi=hemi, color='blue',
                       scale_factor=0.6)
        offsets = np.append([0], [s['nuse'] for s in fwdmag['src']])
        if hemi == 'lh':
            ifoci = [np.nonzero([stcdata[0:offsets[1],time_idx]>=0.25*np.max(stcdata[:,time_idx])][0])]
            vfoci = fwdmag['src'][0]['vertno'][ifoci[0][0]]
            cfoci = fwdmag['src'][0]['rr'][vfoci]
            print "Coords  of %d sel. vfoci: " % cfoci.shape[0]
            print cfoci
            print "vfoci: "
            print vfoci
            print "brain.geo['lh'].coords[vfoci] : "
            print brain.geo['lh'].coords[vfoci]
        elif len(fwdmag['src']) > 1:
            ifoci = [np.nonzero([stcdata[offsets[1]:,time_idx]>=0.25*np.max(stcdata[:,time_idx])][0])]
            vfoci = fwdmag['src'][1]['vertno'][ifoci[0][0]]
            cfoci = fwdmag['src'][1]['rr'][vfoci]
            print "Coords  of %d sel. vfoci: " % cfoci.shape[0]
            print cfoci
            print "vfoci: "
            print vfoci
            print "brain.geo['rh'].coords[vfoci] : "
            print brain.geo['rh'].coords[vfoci]

        mrfoci = np.zeros(cfoci.shape)
        invmri_head_t = invert_transform(fwdmag['info']['mri_head_t'])
        mrfoci = apply_trans(invmri_head_t['trans'],cfoci, move=True)
        print "mrfoci: "
        print mrfoci

        # Just some blops:
        bloblist = np.zeros((300,3))
        for i in xrange(100):
            bloblist[i,0] = float(i)
            bloblist[i+100,1] = float(i)
            bloblist[i+200,2] = float(i)
        mrblobs = apply_trans(invmri_head_t['trans'], bloblist, move=True)
        brain.save_image('testfig_map_%s.png' % hemi)
        brain.close()
Example #3
0
def plot_visualize_mft_sources(fwdmag, stcdata, tmin, tstep, subject,
                               subjects_dir):
    """
    Plot the MFT sources at time point of peak.
    Parameters
    ----------
    fwdmag:  forward solution
    stcdata: stc with ||cdv|| (point sequence as in fwdmag['source_rr'])
    tmin, tstep, subject: passed to mne.SourceEstimate()
    """
    print("##### Attempting to plot:")
    # cf. decoding/plot_decoding_spatio_temporal_source.py
    vertices = [s['vertno'] for s in fwdmag['src']]
    if len(vertices) == 1:
        vertices = [
            fwdmag['src'][0]['vertno']
            [fwdmag['src'][0]['rr'][fwdmag['src'][0]['vertno']][:, 0] <= -0.],
            fwdmag['src'][0]['vertno'][
                fwdmag['src'][0]['rr'][fwdmag['src'][0]['vertno']][:, 0] > -0.]
        ]
    elif len(vertices) > 2:
        warnings.warn(
            'plot_visualize_mft_sources(): Cannot handle more than two sources spaces'
        )
        return

    stc_feat = SourceEstimate(stcdata,
                              vertices=vertices,
                              tmin=tmin,
                              tstep=tstep,
                              subject=subject)
    itmaxsum = np.argmax(np.sum(stcdata, axis=0))
    twmin = tmin + tstep * float(itmaxsum - stcdata.shape[1] / 20)
    twmax = tmin + tstep * float(itmaxsum + stcdata.shape[1] / 20)
    for ihemi, hemi in enumerate(['lh', 'rh', 'both']):
        brain = stc_feat.plot(surface='white',
                              hemi=hemi,
                              subjects_dir=subjects_dir,
                              transparent=True,
                              clim='auto')
        # use peak getter to move visualization to the time point of the peak
        print("Restricting peak search to [%fs, %fs]" % (twmin, twmax))
        if hemi == 'both':
            brain.show_view('parietal')
            vertno_max, time_idx = stc_feat.get_peak(hemi=None,
                                                     time_as_index=True,
                                                     tmin=twmin,
                                                     tmax=twmax)
        else:
            brain.show_view('lateral')
            vertno_max, time_idx = stc_feat.get_peak(hemi=hemi,
                                                     time_as_index=True,
                                                     tmin=twmin,
                                                     tmax=twmax)
        print("hemi=%s: setting time_idx=%d" % (hemi, time_idx))
        brain.set_data_time_index(time_idx)
        if hemi == 'lh' or hemi == 'rh':
            # draw marker at maximum peaking vertex
            brain.add_foci(vertno_max,
                           coords_as_verts=True,
                           hemi=hemi,
                           color='blue',
                           scale_factor=0.6)

        if len(fwdmag['src']) > ihemi:
            fwds = fwdmag['src'][ihemi]
            comax = fwds['rr'][vertno_max]
            print("hemi=%s: vertno_max=%d, time_idx=%d fwdmag['src'][%d]['rr'][vertno_max] = " % \
                  (hemi, vertno_max, time_idx, ihemi), comax)

            offsets = np.append([0], [s['nuse'] for s in fwdmag['src']])
            if hemi == 'lh':
                ifoci = [
                    np.nonzero([
                        stcdata[0:offsets[1], time_idx] >=
                        0.25 * np.max(stcdata[:, time_idx])
                    ][0])
                ]
            elif len(fwdmag['src']) > 1:
                ifoci = [
                    np.nonzero([
                        stcdata[offsets[1]:, time_idx] >=
                        0.25 * np.max(stcdata[:, time_idx])
                    ][0])
                ]
            vfoci = fwds['vertno'][ifoci[0][0]]
            cfoci = fwds['rr'][vfoci]
            print("Coords  of %d sel. vfoci: " % cfoci.shape[0])
            print(cfoci)
            print("vfoci: ")
            print(vfoci)
            print("brain.geo[%s].coords[vfoci] : " % hemi)
            print(brain.geo[hemi].coords[vfoci])

            mrfoci = np.zeros(cfoci.shape)
            invmri_head_t = invert_transform(fwdmag['info']['mri_head_t'])
            mrfoci = apply_trans(invmri_head_t['trans'], cfoci, move=True)
            print("mrfoci: ")
            print(mrfoci)

            # Just some blops along the coordinate axis:
            # This will not yield reasonable results w an inflated brain.
            # bloblist = np.zeros((300,3))
            # for i in xrange(100):
            #    bloblist[i,0] = float(i)
            #    bloblist[i+100,1] = float(i)
            #    bloblist[i+200,2] = float(i)
            # mrblobs = apply_trans(invmri_head_t['trans'], bloblist, move=True)
            # brain.add_foci(mrblobs, coords_as_verts=False, hemi=hemi, color='yellow', scale_factor=0.3)
        brain.save_image('testfig_map_%s.png' % hemi)
        brain.close()