Beispiel #1
0
    def cnn_xjustdims(self):
        if self.selection is 'all':
            self.total_batch_size = len(self.stc)#number of events. we'll consider each event an example.
        else:                
            self.total_batch_size = len(self.selection)#number of events. we'll consider each event an example.

        n_eeg = self.epochs_eeg.get_data().shape[1]

        eeg_xyz=np.squeeze(np.array([self.epochs_eeg.info['chs'][i]['loc'][:3].reshape([1,3]) for i in range(0,n_eeg)]))

        n_meg = self.epochs_meg.get_data().shape[1]

        meg_xyz=np.squeeze(np.array([self.epochs_meg.info['chs'][i]['loc'][:3].reshape([1,3]) for i in range(0,n_meg)]))

        if self.selection is 'all':
            eeg_data = np.array(self.epochs_eeg.get_data())#batch_sizexmxn_steps
            meg_data = np.array(self.epochs_meg.get_data())#batch_sizexmxn_steps
        else:
            eeg_data = np.array(self.epochs_eeg.get_data())[self.selection,:,:]#batch_sizexmxn_steps
            meg_data = np.array(self.epochs_meg.get_data())[self.selection,:,:]#batch_sizexmxn_steps

        self.n_steps=meg_data.shape[2]

        self.meas_dims=[11,11]
        print "Image grid dimensions: ", self.meas_dims
        tf_meas = meas_class.meas(meg_data,meg_xyz, eeg_data,eeg_xyz, self.meas_dims, self.n_steps, self.total_batch_size)
        if self.pca is True:
            self.Wt=tf_meas.pca(Wt=self.Wt)
        elif self.pca is False:
            tf_meas.scale()
        else:
            pass

        tf_meas.interp()
        tf_meas.reshape()
        #for b in range(0,n_steps*total_batch_size):
        #    tf_meas.plot(b)
        self.meas_img_all = tf_meas.meas_img
        self.m =tf_meas.m

        if self.selection is 'all':
            dipole=np.array([self.stc[i]._data for i in range(0,len(self.stc))]).transpose((1,2,0))
        else:
            dipole=np.array([self.stc[i]._data for i in self.selection]).transpose((1,2,0)) 

        if self.locate is not False:
            if self.rnn is True or self.cnn is 'fft':
                self.locationXYZT()
            else:
                self.location()
        else:
            if self.rnn is True or self.cnn is 'fft':
                #pxn_stepsxbatchsize
                self.qtrue_all,self.p=meas_class.scale_dipoleXYZT_OH(dipole,subsample=self.subsample)    
                #bxnxp
            else:
                #pxn_stepsxbatchsize
                self.qtrue_all,self.p=meas_class.scale_dipole(dipole,subsample=self.subsample)    
Beispiel #2
0
 def rat_prepro(self):
     tf_meas = meas_class.meas(self.meg_data,self.meg_xyz, self.eeg_data,self.eeg_xyz, self.meas_dims, self.n_steps, self.batch_size)
     if self.pca is True:
         self.Wt=tf_meas.pca(Wt = self.Wt)
     elif self.pca is False:
         tf_meas.scale()
     else:
         pass
     
     if self.n_chan_in is 2:
         if self.cnn is True:    
             print "Image grid dimensions: ", self.meas_dims
             tf_meas.interp()
             tf_meas.reshape()
             self.meas_img_all = tf_meas.meas_img
             self.m =tf_meas.m
         else:
             self.meas_img_all = np.vstack((tf_meas.meas_in[0],tf_meas.meas_in[1]))
             self.m =tf_meas.m0+tf_meas.m1
     elif self.n_chan_in is 1:
         if self.cnn is True:    
             print "Image grid dimensions: ", self.meas_dims
             tf_meas.interp()
             tf_meas.reshape()
             self.meas_img_all = np.expand_dims(tf_meas.meas_img[:,:,:,:,0],axis=-1)
             self.m =tf_meas.m
         else:
             self.meas_img_all = np.vstack((tf_meas.meas_in[0]))
             self.m =tf_meas.m0
         
     if self.locate is not False:
         if self.rnn is True or self.cnn is 'fft':
             self.location_rat_XYZT()
         else:
             self.location_rat()
     else:
         if self.rnn is True or self.cnn is 'fft':
             #pxn_stepsxbatchsize
             self.qtrue_all,self.p=meas_class.scale_dipoleXYZT_OH(self.dipole,subsample=self.subsample)    
             #bxnxp
         else:
             #pxn_stepsxbatchsize
             self.qtrue_all,self.p=meas_class.scale_dipole(self.dipole,subsample=self.subsample)    
Beispiel #3
0
    def fftcnn_xjustdims(self):
        if self.selection is 'all':
            self.total_batch_size = len(self.stc)#number of evens. we'll consider each event an example.
        else:
            self.total_batch_size = len(self.selection)#number of events. we'll consider each event an example.

        n_eeg = self.epochs_eeg.get_data().shape[1]

        eeg_xyz=np.squeeze(np.array([self.epochs_eeg.info['chs'][i]['loc'][:3].reshape([1,3]) for i in range(0,n_eeg)]))

        n_meg = self.epochs_meg.get_data().shape[1]

        meg_xyz=np.squeeze(np.array([self.epochs_meg.info['chs'][i]['loc'][:3].reshape([1,3]) for i in range(0,n_meg)]))

        if self.selection is 'all':
            eeg_data = np.array(self.epochs_eeg.get_data())#batch_sizexmxself.n_steps
            meg_data = np.array(self.epochs_meg.get_data())#batch_sizexmxn_steps
        else:
            eeg_data = np.array(self.epochs_eeg.get_data())[self.selection,:,:]#batch_sizexmxn_steps
            meg_data = np.array(self.epochs_meg.get_data())[self.selection,:,:]#batch_sizexmxn_steps

        self.n_steps=meg_data.shape[2]

        self.meas_dims=[self.n_steps, n_eeg+n_meg]
        print "Meas dims in: ", self.meas_dims
        tf_meas = meas_class.meas(meg_data,meg_xyz, eeg_data,eeg_xyz, self.meas_dims, self.n_steps, self.total_batch_size)
        if self.pca is True:
            self.Wt=tf_meas.pca(Wt=self.Wt)
        elif self.pca is False:
            tf_meas.scale()
        else:
            pass

        tf_meas.stack_reshape()
        #ff=np.fft.fft(tf_meas.meas_stack,axis=1)
        ff=tf_meas.meas_stack

        #print ff.shape
        #meas_img_all = np.expand_dims(np.abs(ff)*np.abs(ff),-1)
        self.meas_img_all = np.expand_dims(ff,-1)


        #print meas_img_all.shape
        self.m =tf_meas.m

        if self.selection is 'all':
            dipole=np.array([self.stc[i]._data for i in range(0,len(self.stc))]).transpose((1,2,0))
        else:
            dipole=np.array([self.stc[i]._data for i in self.selection]).transpose((1,2,0))

        if self.locate is not False:
            if self.rnn is True or self.cnn is 'fft':
                self.locationXYZT()
            else:
                self.location()
        else:
            if self.rnn is True or self.cnn is 'fft':
                #pxn_stepsxbatchsize
                self.qtrue_all,self.p=meas_class.scale_dipoleXYZT_OH(dipole,subsample=self.subsample)    
                #bxnxp
            else:
                #pxn_stepsxbatchsize
                self.qtrue_all,self.p=meas_class.scale_dipole(dipole,subsample=self.subsample)    
Beispiel #4
0
def xcnn_xjustdims(stc,
                   epochs,
                   epochs_eeg,
                   epochs_meg,
                   subject,
                   selection='all',
                   pca=False,
                   subsample=1,
                   justdims=True,
                   cnn=True,
                   locate=True,
                   rnn=True,
                   Wt=None):
    if selection is 'all':
        total_batch_size = len(
            stc)  #number of events. we'll consider each event an example.
    else:
        total_batch_size = len(
            selection
        )  #number of events. we'll consider each event an example.

    n_eeg = epochs_eeg.get_data().shape[1]

    eeg_xyz = np.squeeze(
        np.array([
            epochs_eeg.info['chs'][i]['loc'][:3].reshape([1, 3])
            for i in range(0, n_eeg)
        ]))

    n_meg = epochs_meg.get_data().shape[1]

    meg_xyz = np.squeeze(
        np.array([
            epochs_meg.info['chs'][i]['loc'][:3].reshape([1, 3])
            for i in range(0, n_meg)
        ]))

    if selection is 'all':
        eeg_data = np.array(epochs_eeg.get_data())  #batch_sizexmxn_steps
        meg_data = np.array(epochs_meg.get_data())  #batch_sizexmxn_steps
    else:
        eeg_data = np.array(
            epochs_eeg.get_data())[selection, :, :]  #batch_sizexmxn_steps
        meg_data = np.array(
            epochs_meg.get_data())[selection, :, :]  #batch_sizexmxn_steps

    n_steps = meg_data.shape[2]

    meas_dims = n_eeg + n_meg
    print "Meas dims in: ", meas_dims
    tf_meas = meas_class.meas(meg_data, meg_xyz, eeg_data, eeg_xyz, meas_dims,
                              n_steps, total_batch_size)
    if pca is True:
        Wt = tf_meas.pca(Wt=Wt)
    elif pca is False:
        tf_meas.scale()
    else:
        pass

    tf_meas.stack_reshape()
    meas_img_all = tf_meas.meas_stack
    m = tf_meas.m

    if selection is 'all':
        dipole = np.array([stc[i]._data
                           for i in range(0, len(stc))]).transpose((1, 2, 0))
    else:
        dipole = np.array([stc[i]._data for i in selection]).transpose(
            (1, 2, 0))

    if locate is not False:
        if rnn is True or cnn is 'fft':
            qtrue_all, p = locationXYZT(stc,
                                        subject,
                                        selection=selection,
                                        locate=locate)
        else:
            qtrue_all, p = location(stc,
                                    subject,
                                    selection=selection,
                                    locate=locate)
    else:
        if rnn is True or cnn is 'fft':
            #pxn_stepsxbatchsize
            qtrue_all, p = meas_class.scale_dipoleXYZT_OH(dipole,
                                                          subsample=subsample)
            #bxnxp
        else:
            #pxn_stepsxbatchsize
            qtrue_all, p = meas_class.scale_dipole(dipole, subsample=subsample)
            #bxnxp
    del stc, epochs, epochs_eeg, epochs_meg
    return meas_img_all, qtrue_all, meas_dims, m, p, n_steps, total_batch_size, Wt
Beispiel #5
0
def rat_real(stim='Tones',
             selection='all',
             pca=True,
             subsample=1,
             justdims=True,
             cnn=False,
             locate=True,
             treat=None,
             rnn=False,
             Wt=None):
    #print 'selection ',selection
    ecog_thresh = 1e-5
    if stim == 'Tones':
        name = '/home/jcasa/meld/code/python/rattest/processed/ECOG_MEG_Tones.grouped.pickle'
    elif stim == 'P1':
        name = '/home/jcasa/meld/code/python/rattest/processed/ECOG_MEG_P1.grouped.pickle'
    elif stim == 'P0':
        name = '/home/jcasa/meld/code/python/rattest/processed/MEG_ECOG.grouped.pickle'
    with open(name, 'r') as f:
        b = pickle.load(f)
    ecog_data = np.transpose(np.array(b["ECoG_average"]),
                             (1, 2, 0))  #pxnxb - for dipole scaling
    eeg_data = np.transpose(np.array(b["ECoG_average"]), (0, 1, 2))
    ecog_data[abs(ecog_data) > ecog_thresh] = 100e-9  #A
    #eeg_data[abs(eeg_data)>ecog_thresh]=100e-6#V
    meg_data = np.transpose(np.array(b["MEG_average"]),
                            (0, 1, 2))  #bxmxn - for meas_class pca formatting
    fs_MEG = b["fs_MEG"]
    fs_ECoG = b["fs_ECoG"]
    flag = b["flag"]
    n_treat = b["n_treat"]
    treatments = b["treatments"]
    meg_xyz = b["meg_xyz"] / 1000.  #mx3
    ecog_xyz = b["ecog_xyz"] / 1000.  #in meters
    n_chan_in = 1
    n_steps = meg_data.shape[2]
    total_batch_size = ecog_data.shape[2]
    m = meg_data.shape[1]
    p = ecog_data.shape[0]
    meas_dims = m
    #print 'n_steps', n_steps, 'total_batch_size', total_batch_size, 'm', m, 'p', p
    #print 'MEG array: ',meg_data.shape
    #print 'ECOG array: ',ecog_data.shape
    #print 'fake EEG array: ',eeg_data.shape
    if pca:
        tf_meas = meas_class.meas(meg_data, meg_xyz, np.array([]),
                                  np.array([]), meas_dims, n_steps,
                                  total_batch_size)
        Wt = tf_meas.pca()
        tf_meas.stack_reshape(
            n_chan_in=n_chan_in)  #ignore ecog - just a placeholder
        meas_img_all = tf_meas.meas_stack
    else:
        meas_img_all = np.transpose(meg_data, (0, 2, 1))

    #scale dipoles ~ ecog
    #print 'Locate: ',locate
    if rnn:
        if locate is False:
            qtrue_all, p = meas_class.scale_dipoleXYZT_OH(ecog_data,
                                                          subsample=subsample)
        else:
            p = 3
            qtrue_all = location_rat_XYZT(locate, total_batch_size, n_steps, p,
                                          ecog_data, ecog_xyz)
    else:
        if locate is False:
            qtrue_all, p = meas_class.scale_dipole(ecog_data,
                                                   subsample=subsample)
        else:
            p = 3
            qtrue_all = location_rat(locate, total_batch_size, n_steps, p,
                                     ecog_data, ecog_xyz)

    if justdims is True:
        return meas_dims, m, p, n_steps, total_batch_size, Wt
    else:
        #print 'meas_img_all ',meas_img_all.shape
        #print 'qtrue_all ',qtrue_all.shape

        if selection is 'all':
            return meas_img_all, qtrue_all, meas_dims, m, p, n_steps, total_batch_size, Wt
        else:
            return meas_img_all[selection, :, :], qtrue_all[
                selection, :, :], meas_dims, m, p, n_steps, np.size(
                    selection), Wt
Beispiel #6
0
def rat_prepro(n_chan_in,
               dipole,
               dipole_xyz,
               meg_data,
               meg_xyz,
               eeg_data,
               eeg_xyz,
               meas_dims,
               n_steps,
               batch_size,
               subject,
               selection='all',
               pca=True,
               subsample=1,
               justdims=False,
               cnn=True,
               locate=True,
               rnn=False,
               Wt=None):
    print n_chan_in
    if locate is True:
        p = 3
    elif locate > 0:
        p = 3 * locate
    else:
        p = dipole.shape[0]

    tf_meas = meas_class.meas(meg_data, meg_xyz, eeg_data, eeg_xyz, meas_dims,
                              n_steps, batch_size)
    if pca is True:
        Wt = tf_meas.pca(Wt=Wt)
    elif pca is False:
        tf_meas.scale()
    else:
        pass

    if n_chan_in is 2:
        if cnn is True:
            print "Image grid dimensions: ", meas_dims
            tf_meas.interp()
            tf_meas.reshape()
            meas_img_all = tf_meas.meas_img
            m = tf_meas.m

        else:
            tf_meas.stack_reshape(n_chan_in=n_chan_in)
            meas_img_all = tf_meas.meas_stack
            m = tf_meas.m0 + tf_meas.m1
            meas_dims = m
    elif n_chan_in is 1:
        if cnn is True:
            print "Image grid dimensions: ", meas_dims
            tf_meas.interp()
            tf_meas.reshape()
            meas_img_all = np.expand_dims(tf_meas.meas_img[:, :, :, :, 0],
                                          axis=-1)
            m = tf_meas.m
        else:
            tf_meas.stack_reshape(n_chan_in=n_chan_in)
            meas_img_all = tf_meas.meas_stack
            m = tf_meas.m0
            meas_dims = m

    if locate is not False:
        if rnn is True or cnn is 'fft':
            qtrue_all = location_rat_XYZT(locate, batch_size, n_steps, p,
                                          dipole, dipole_xyz)
        else:
            qtrue_all = location_rat(locate, batch_size, n_steps, p, dipole,
                                     dipole_xyz)
    else:

        #print p, dipole.shape, "Dipoles (rat_prepro, before scaling)"

        if rnn is True or cnn is 'fft':
            #pxn_stepsxbatchsize
            qtrue_all, p = meas_class.scale_dipoleXYZT_OH(dipole,
                                                          subsample=subsample)
            #bxnxp
        else:
            #pxn_stepsxbatchsize
            qtrue_all, p = meas_class.scale_dipole(dipole, subsample=subsample)
            #bxnxp

    #print p, qtrue_all.shape, "Dipoles (rat_prepro, after scaling)"
    assert qtrue_all.shape == (batch_size, n_steps,
                               p), str(qtrue_all.shape) + ' ' + str(
                                   (batch_size, n_steps, p))
    if cnn is True:
        assert meas_img_all.shape == (
            batch_size, n_steps, meas_dims[0], meas_dims[1],
            1), str(meas_img_all.shape) + ' ' + str(
                (batch_size, n_steps, meas_dims[0], meas_dims[1], 1))
    else:
        assert meas_img_all.shape == (batch_size, n_steps,
                                      m), str(meas_img_all.shape) + ' ' + str(
                                          (batch_size, n_steps, m))

    return meas_img_all, qtrue_all, meas_dims, m, p, n_steps, batch_size, Wt