コード例 #1
0
ファイル: imaging_gui.py プロジェクト: justin0bk/CaImpipe
    def setup_imaging(self) :
        

        # load spectrum
        if os.path.isfile(os.path.join(self.ipath, self.name, 'sp_' + self.name + '.mat')):
            P = so.loadmat(os.path.join(self.ipath, self.name, 'sp_' + self.name + '.mat'))
            self.SP = P['SP']
            self.freq = P['freq']
            self.stime = P['t'][0]
            self.sdt = self.stime[1]-self.stime[0]
            self.sp_exists = 0 #1 (originally 1 if sp_ file is found)
        
        # read image stack
        if os.path.isfile(os.path.join(self.ipath, self.name, 'recording_' + self.name + '_aligned.hdf5')):
            fid = h5py.File(os.path.join(self.ipath, self.name, 'recording_' + self.name + '_aligned.hdf5'))
            print "loaded motion corrected file (*_aligned.hdf5)"
        else:
            fid = h5py.File(os.path.join(self.ipath, self.name, 'recording_' + self.name + '_downsamp.hdf5'))

        
        self.stack = fid['images']
        self.nframes = self.stack.shape[0]


        # read mean of disk filtered stack
        if os.path.isfile(os.path.join(self.ipath, self.name, 'recording_' + self.name + '_diskmean.mat')):
            self.disk_img = so.loadmat(os.path.join(self.ipath, self.name, 'recording_' + self.name + '_diskmean.mat'))['mean']
        else:
            self.disk_img = self.stack[0,:,:]

        # read brain states
        # test if sleep state file exists
        if os.path.isfile(os.path.join(self.ipath, self.name, 'remidx_' + self.name + '.txt')): #added remidx'z' on the string to remove this dependence
            self.sr = 1000 #get_snr(self.ipath, self.name)

            tmp = load_stateidx(self.ipath, self.name)
        else:
            tmp = np.zeros((0,))
        self.M = np.zeros((1, tmp.shape[0]))
        self.M[0,:] = tmp
        print "HERE", tmp.shape[0]
        

        # get colorrange minimum and maximum values
        tmp = self.stack[1000:1500,20:-20,20:-20]
        self.cmin = tmp.min()
        self.cmax = tmp.max()

        self.panel = wx.Panel(self)
        self.Bind(wx.EVT_CHAR_HOOK, self.on_key_down)

        # call functions
        self.create_image_axes()
        # called each time something is supposed to change on the figure
        self.draw_figure() 
コード例 #2
0
    def load_session(self):
        self.SR_eeg = sleepy.get_snr(self.ppath, self.name)
        self.dt = 1/self.SR_eeg
        # number of sampled point for one fourier bin
        #self.fbin = np.round((1/self.dt) * 2.5)

        # graphs going into dock_session
        self.graph_activity   = pg.PlotWidget()
        self.graph_brainstate = pg.PlotWidget()
        self.graph_eegspec    = pg.PlotWidget()
        self.graph_emgampl    = pg.PlotWidget()

        # graphs for dock EEG/EMG
        self.graph_eeg = pg.PlotWidget()
        self.graph_emg = pg.PlotWidget()

        # load data ###########################################################
        # load EEG/EMG
        self.eeg_pointer = 0
        self.emg_pointer = 0
        self.EEG_list = []
        self.EMG_list = []
        self.eeg_spec_list = []
        self.emg_amp_list = []
        EEG = np.squeeze(so.loadmat(os.path.join(self.ppath, self.name, 'EEG.mat'))['EEG'])
        self.EEG_list.append(EEG)
        if os.path.isfile(os.path.join(self.ppath, self.name, 'EEG2.mat')):
            EEG2 = np.squeeze(so.loadmat(os.path.join(self.ppath, self.name, 'EEG2.mat'))['EEG2'])
            self.EEG_list.append(EEG2)
        self.EEG = self.EEG_list[0]

        EMG = np.squeeze(so.loadmat(os.path.join(self.ppath, self.name, 'EMG.mat'))['EMG'])
        self.EMG_list.append(EMG)
        if os.path.isfile(os.path.join(self.ppath, self.name, 'EMG2.mat')):
            EMG2 = np.squeeze(so.loadmat(os.path.join(self.ppath, self.name, 'EMG2.mat'))['EMG2'])
            self.EMG_list.append(EMG2)
        self.EMG = self.EMG_list[0]

        # load spectrogram / EMG amplitude
        if not(os.path.isfile(os.path.join(self.ppath, self.name, 'sp_' + self.name + '.mat'))):
            # spectrogram does not exist, generate it
            sleepy.calculate_spectrum(self.ppath, self.name, fres=0.5)
            print("Calculating spectrogram for recording %s\n" % self.name)
        
        spec = so.loadmat(os.path.join(self.ppath, self.name, 'sp_' + self.name + '.mat'))
        self.ftime = spec['t'][0]
        self.fdt = spec['dt'][0][0]
        # the first time bin of the spectrogram goes from 0 to 5s; therefore
        # I add the self.fdt (= 2.5 s)
        #self.ftime += self.fdt

        # New lines
        self.fbin = np.round((1 / self.dt) * self.fdt)
        if self.fbin % 2 == 1:
            self.fbin += 1
        # END new lines
        self.eeg_spec = spec['SP']
        self.eeg_spec_list.append(spec['SP'])
        if 'SP2' in spec:
            self.eeg_spec_list.append(spec['SP2'])
        # max color for spectrogram color-range
        self.color_max = np.max(self.eeg_spec)
        freq = np.squeeze(spec['freq'])
        self.ifreq = np.where(freq <= 25)[0][-1]
        self.fdx = freq[1]-freq[0]
        self.mfreq = np.where((freq>=10) & (freq <= 200))[0]
        
        emg_spec = so.loadmat(os.path.join(self.ppath, self.name, 'msp_' + self.name + '.mat'))
        EMGAmpl = np.sqrt(emg_spec['mSP'][self.mfreq,:].sum(axis=0))
        self.EMGAmpl = EMGAmpl
        self.emg_amp_list.append(EMGAmpl)
        if 'mSP2' in emg_spec:
            EMGAmpl2 = np.sqrt(emg_spec['mSP2'][self.mfreq,:].sum(axis=0))
            self.emg_amp_list.append(EMGAmpl2)

        self.nbin = len(self.ftime) #number of bins in fourier time
        self.len_eeg = len(self.EEG)
                        
        # load brain state
        if not(os.path.isfile(os.path.join(self.ppath, self.name, 'remidx_' + self.name + '.txt'))):
            # predict brain state
            M,S = sleepy.sleep_state(self.ppath, self.name, pwrite=1, pplot=0)
        (A,self.K) = sleepy.load_stateidx(self.ppath, self.name)
        # needs to be packed into 1 x nbin matrix for display
        self.M = np.zeros((1,self.nbin))
        self.M[0,:] = A
                
        # load laser
        self.laser = np.zeros((self.nbin,))
        laser_file = os.path.join(self.ppath, self.name, 'laser_' + self.name + '.mat')
        if os.path.isfile(laser_file):
            try:
                self.laser = np.squeeze(so.loadmat(laser_file)['laser'])            
            except:
                self.laser = np.squeeze(np.array(h5py.File(laser_file,'r').get('laser')))
        
        # downsample laser to brainstate time
        (idxs, idxe) = laser_start_end(self.laser, SR=self.SR_eeg)
        # downsample EEG time to spectrogram time    
        idxs = [int(i/self.fbin) for i in idxs]
        idxe = [int(i/self.fbin) for i in idxe]
        self.laser_dn = np.zeros((self.nbin,))
        for (i,j) in zip(idxs, idxe):
            self.laser_dn[i:j+1] = 1
                
        # max color for spectrogram
        self.color_max = np.max(self.eeg_spec)

        ### END load data #############################################################
        # Plot whole session
        self.dock_session.addWidget(self.graph_treck, row=0, col=0, rowspan=1)
        self.dock_session.addWidget(self.graph_activity, row=1, col=0, rowspan=1)
        self.dock_session.addWidget(self.graph_brainstate, row=2, col=0)
        self.dock_session.addWidget(self.graph_eegspec, row=3, col=0, rowspan=1)
        self.dock_session.addWidget(self.graph_emgampl, row=4, col=0)

        # display data
        # mix colormaps
        pos = np.array([0, 0.2, 0.4, 0.6, 0.8])
        #color = np.array([[0,255,255,255], [255,0,255, 255], [192,192,192,255], (0, 0, 0, 255), (255,255,0, 255)], dtype=np.ubyte)
        color = np.array([[0,255,255,255], [150,0,255, 255], [192,192,192,255], (0, 0, 0, 255), (255,255,0, 255)], dtype=np.ubyte)
        cmap = pg.ColorMap(pos, color)
        self.lut_brainstate = cmap.getLookupTable(0.0, 1.0, 5)
        
        pos = np.array([0., 0.05, .2, .4, .6, .9])
        color = np.array([[0, 0, 0, 255], [0,0,128,255], [0,255,0,255], [255,255,0, 255], (255,165,0,255), (255,0,0, 255)], dtype=np.ubyte)
        cmap = pg.ColorMap(pos, color)
        self.lut_spectrum = cmap.getLookupTable(0.0, 1.0, 256)

        # plot brainstate
        self.image_brainstate = pg.ImageItem() 
        self.graph_brainstate.addItem(self.image_brainstate)                
        self.image_brainstate.setImage(self.M.T)

        self.image_brainstate.scale(self.fdt,1)
        self.graph_brainstate.setMouseEnabled(x=True, y=False)
        #self.graph_brainstate.setXLink(self.graph_eegspec)
        limits = {'xMin': 0*self.fdt, 'xMax': self.ftime[-1], 'yMin': 0, 'yMax': 1}
        self.graph_brainstate.setLimits(**limits)

        ax = self.graph_brainstate.getAxis(name='left')
        labelStyle = {'color': '#FFF', 'font-size': '10pt'}
        ax.setLabel('Brainstate', units='', **labelStyle)
        ax.setTicks([[(0, ''), (1, '')]])

        ax = self.graph_brainstate.getAxis(name='bottom')
        ax.setTicks([[]])

        self.image_brainstate.setLookupTable(self.lut_brainstate)
        self.image_brainstate.setLevels([1, 5])
                
        # plot EEG spectrum
        # clear plot and then reload ImageItem        
        self.graph_eegspec.clear()
        self.image_eegspec = pg.ImageItem() 
        self.graph_eegspec.addItem(self.image_eegspec)
        ax = self.graph_eegspec.getAxis(name='bottom')
        ax.setTicks([[]])
        
        # scale image to seconds, minutes or hours        
        self.image_eegspec.setImage(self.eeg_spec[0:self.ifreq,:].T)
        self.image_eegspec.scale(self.fdt, 1.0*self.fdx)
        
        # mousescroll only allowed along x axis
        self.graph_eegspec.setMouseEnabled(x=True, y=False)
        limits = {'xMin': 0*self.fdt, 'xMax': self.ftime[-1], 'yMin': 0, 'yMax': 20}
        self.graph_eegspec.setLimits(**limits)
        # label for y-axis
        ax = self.graph_eegspec.getAxis(name='left')
        labelStyle = {'color': '#FFF', 'font-size': '10pt'}
        ax.setLabel('Freq', units='Hz', **labelStyle)
        # xTicks
        ax.setTicks([[(0, '0'), (10, '10')]])

        # colormap
        self.image_eegspec.setLookupTable(self.lut_spectrum)
        # link graph with self.graph_brainstate
        # and link all other graphs together
        self.graph_eegspec.setXLink(self.graph_brainstate)
        self.graph_brainstate.setXLink(self.graph_eegspec)
        self.graph_eegspec.setXLink(self.graph_treck)

        # plot EMG ampl
        self.graph_emgampl.clear()
        self.graph_emgampl.plot(self.ftime, self.EMGAmpl)
        self.graph_emgampl.setMouseEnabled(x=False, y=True)
        self.graph_emgampl.setXLink(self.graph_eegspec)
        limits = {'xMin': 0, 'xMax': self.ftime[-1]}
        self.graph_emgampl.setLimits(**limits)
        # y-axis
        ax = self.graph_emgampl.getAxis(name='left')
        labelStyle = {'color': '#FFF', 'font-size': '10pt'}
        ax.setLabel('EMG', units='uV', **labelStyle)
        ax.enableAutoSIPrefix(enable=False)
        # x-axis        
        ax = self.graph_emgampl.getAxis(name='bottom')
        labelStyle = {'color': '#FFF', 'font-size': '10pt'}
        ax.setLabel('Time', units='s', **labelStyle)

        self.graph_eegspec.scene().sigMouseClicked.connect(self.mouse_pressed)
        self.graph_brainstate.scene().sigMouseClicked.connect(self.mouse_pressed)
        self.graph_treck.scene().sigMouseClicked.connect(self.mouse_pressed)
        self.graph_activity.scene().sigMouseClicked.connect(self.mouse_pressed)
        
        # setup graph_treck ###########################################################
        self.graph_treck.setMouseEnabled(x=True, y=False)
        limits = {'xMin': 0, 'xMax': self.ftime[-1]}
        self.graph_treck.setLimits(**limits)
        self.graph_treck.setXLink(self.graph_eegspec)
                                
        ax = self.graph_treck.getAxis(name='left')
        labelStyle = {'color': '#FFF', 'font-size': '10pt'}
        ax.setLabel('Behavior', units='', **labelStyle)
        ax = self.graph_treck.getAxis(name='bottom')
        ax.setTicks([[]])

        ax = self.graph_treck.getAxis(name='left')
        label_style = {'color': '#FFF', 'font-size': '10pt'}
        ax.setLabel('Behavior', units='', **label_style)
        ax.setTicks([self.ticks])
        # END setup graph_treck #######################################################

        # setup graph_activity ########################################################        
        ax = self.graph_activity.getAxis(name='left')
        limits = {'xMin': 0, 'xMax': self.ftime[-1]}
        self.graph_activity.setLimits(**limits)
        labelStyle = {'color': '#FFF', 'font-size': '10pt'}
        ax.setLabel('Activity', units='', **labelStyle)
        ax = self.graph_activity.getAxis(name='bottom')
        ax.setTicks([[]])
        #self.graph_activity.setMouseEnabled(x=False, y=True)
        self.graph_activity.setXLink(self.graph_treck)
        # END setup graph_activity #####################################################

        # Done With dock "Session" ####################################################
        
        # plot raw EEG, EMG in dock EEG/EMG (dock_eeg)
        self.graph_eeg.setXLink(self.graph_emg)
        self.graph_eeg.setMouseEnabled(x=True, y=True)
        self.graph_emg.setMouseEnabled(x=True, y=True)
        self.graph_eeg.setXRange(self.curr_time-self.twin_view/2, self.curr_time+self.twin_view/2, padding=0)

        # setup graph_eeg
        ax = self.graph_eeg.getAxis(name='left')
        labelStyle = {'color': '#FFF', 'font-size': '12pt'}
        ax.setLabel('EEG', units='uV', **labelStyle)
        ax = self.graph_eeg.getAxis(name='bottom')
        ax.setTicks([[]])                           
        ax = self.graph_eeg.getAxis(name='bottom')

        # setup graph_emg
        ax = self.graph_emg.getAxis(name='left')
        labelStyle = {'color': '#FFF', 'font-size': '12pt'}
        ax.setLabel('EMG', units='uV', **labelStyle)
        ax = self.graph_eeg.getAxis(name='bottom')
        ax.setTicks([[]])                   

        ax = self.graph_emg.getAxis(name='bottom')
        labelStyle = {'color': '#FFF', 'font-size': '10pt'}
        ax.setLabel('Time', units='s', **labelStyle)


        self.dock_eeg.addWidget(self.graph_eeg, row=0, col=0)
        self.dock_eeg.addWidget(self.graph_emg, row=1, col=0)
コード例 #3
0
ファイル: vypro.py プロジェクト: Lozanoda/Lab
def fibpho_video(ppath,
                 name,
                 ts,
                 te,
                 fmax=20,
                 emg_legend=1000,
                 vm=2.0,
                 time_legend=10,
                 dff_legend=10,
                 ffmpeg_path='ffmpeg'):
    """
    Generate video for fiber photometry recording.
    The function requires that ffmpeg is installed on your system (http://ffmpeg.org).
    Windows Users: Specify the full path to the ffmpeg program

    The resulting video has 1 Hz resolution and will be saved in folder $ppath/$name
    :param ppath: base folder
    :param name:
    :param ts: start time in seconds
    :param te: end time in second
    :param fmax: maximum frequency on EEG spectrogram
    :param emg_legend: EMG legend in microVolts!
    :param vm: controls saturation of EEG spectrogram; a value in the range from 1 to 2 should work best
    :param time_legend: time legend in seconds
    :param dff_legend: DF/F in %
    :param ffmpeg_path: full, absolute path to ffmpeg program; important for to set in Windows
    :return: n/a
    """

    # helper function ######################
    def closest_neighbor(vec, x):
        d = np.abs(vec - x)
        el = np.min(d)
        idx = np.argmin(d)
        return el, idx

    ########################################

    # setup figure arrangement
    sleepy.set_fontsize(12)
    plt.ion()
    plt.figure()
    plt.figure(figsize=(8, 6))

    ax_video = plt.axes([0.1, 0.55, 0.8, 0.43])
    ax_eeg = plt.axes([0.1, 0.38, 0.8, 0.15])
    ax_emg = plt.axes([0.1, 0.25, 0.8, 0.1])
    ax_bs = plt.axes([0.1, 0.22, 0.8, 0.02])
    ax_bs_legend = plt.axes([0.1, 0.24, 0.2, 0.02])
    ax_dff = plt.axes([0.1, 0.05, 0.8, 0.15])
    ax_dff_legend = plt.axes([0.05, 0.05, 0.05, 0.15])
    ax_time = plt.axes([0.1, 0.001, 0.8, 0.03])

    movie_stack = os.path.join(ppath, name, 'MStack')
    if not (os.path.isdir(movie_stack)):
        os.mkdir(movie_stack)

    sr = sleepy.get_snr(ppath, name)
    M = sleepy.load_stateidx(ppath, name)[0]
    dt = 1.0 / sr

    nbins = int(np.round(sr) * 5.0 / 2)
    Mup = spyke.upsample_mx(M, nbins)

    EEG = so.loadmat(os.path.join(ppath, name, 'EEG.mat'),
                     squeeze_me=True)['EEG']
    EMG = so.loadmat(os.path.join(ppath, name, 'EMG.mat'),
                     squeeze_me=True)['EMG']
    vid_time = so.loadmat(os.path.join(ppath, name, 'video_timing.mat'),
                          squeeze_me=True)['onset']
    len_eeg = EEG.shape[0]

    t = np.arange(0, len_eeg) * dt
    its = closest_neighbor(t, ts)[1]
    ite = closest_neighbor(t, te)[1]
    data_eeg = EEG[its:ite]
    states = sleepy.downsample_states(Mup[its:ite], int(np.round(sr)))
    state_map = [[0, 1, 1], [0.5, 0, 1], [0.6, 0.6, 0.6]]

    # load and resample DF/F
    dff = so.loadmat(os.path.join(ppath, name, 'DFF.mat'),
                     squeeze_me=True)['dff'] * 100
    dff = spyke.downsample_vec(dff[its:ite], int(np.round(sr)))
    dff_max = np.max(dff)
    dff_max = dff_max + 0.2 * dff_max
    dff_min = np.min(dff)
    dff_min = dff_min - 0.1 * dff_min

    # setup axis for video
    ax_video.get_xaxis().set_visible(False)
    ax_video.get_yaxis().set_visible(False)
    ax_video.spines["top"].set_visible(False)
    ax_video.spines["right"].set_visible(False)
    ax_video.spines["bottom"].set_visible(False)
    ax_video.spines["left"].set_visible(False)

    # setup axes for EEG spectrogram
    sleepy.box_off(ax_eeg)
    ax_eeg.set_xticks([])
    plt.gcf().text(0.11, 0.49, 'EEG', color='white')
    plt.gcf().text(0.11, 0.18, '$\mathrm{\Delta F/F}$', color='blue')

    # setup axes for EMG
    ax_emg.get_xaxis().set_visible(False)
    ax_emg.get_yaxis().set_visible(False)
    ax_emg.spines["top"].set_visible(False)
    ax_emg.spines["right"].set_visible(False)
    ax_emg.spines["bottom"].set_visible(False)
    ax_emg.spines["left"].set_visible(False)
    emg_max = np.max(np.abs(EMG[its:ite]))
    emg_max = emg_max + emg_max * 0.1
    plt.gcf().text(0.905, 0.31, "%.1f mV" % (emg_legend / 1000.0), rotation=90)
    plt.gcf().text(0.11, 0.35, 'EMG')

    ax_dff.get_xaxis().set_visible(False)
    ax_dff.get_yaxis().set_visible(False)
    ax_dff.spines["top"].set_visible(False)
    ax_dff.spines["right"].set_visible(False)
    ax_dff.spines["bottom"].set_visible(False)
    ax_dff.spines["left"].set_visible(False)

    ax_bs.get_xaxis().set_visible(False)
    ax_bs.get_yaxis().set_visible(False)
    ax_bs.spines["top"].set_visible(False)
    ax_bs.spines["right"].set_visible(False)
    ax_bs.spines["bottom"].set_visible(False)
    ax_bs.spines["left"].set_visible(False)

    # calculate spectrogram
    fspec, tspec, Sxx = scipy.signal.spectrogram(data_eeg,
                                                 fs=sr,
                                                 nperseg=int(2 * np.round(sr)),
                                                 noverlap=int(np.round(sr)))
    ifreq = np.where(fspec <= fmax)[0]
    med = np.median(Sxx.max(axis=0))

    # setup time legend (beolow DF/F panel)
    ax_time.plot((tspec[0], tspec[0] + time_legend), [1, 1],
                 color='black',
                 linewidth=2)
    ax_time.set_xlim((tspec[0], tspec[-1]))
    ax_time.set_ylim((-1, 1.1))
    ax_time.get_xaxis().set_visible(False)
    ax_time.get_yaxis().set_visible(False)
    ax_time.spines["top"].set_visible(False)
    ax_time.spines["right"].set_visible(False)
    ax_time.spines["bottom"].set_visible(False)
    ax_time.spines["left"].set_visible(False)
    ax_time.text(tspec[0], -1, '%s s' % str(time_legend))

    # setup legend for DF/F
    ax_dff_legend.set_xlim((tspec[0], tspec[-1]))
    ax_dff_legend.set_ylim((-1, 1))
    ax_dff_legend.get_xaxis().set_visible(False)
    ax_dff_legend.get_yaxis().set_visible(False)
    ax_dff_legend.spines["top"].set_visible(False)
    ax_dff_legend.spines["right"].set_visible(False)
    ax_dff_legend.spines["bottom"].set_visible(False)
    ax_dff_legend.spines["left"].set_visible(False)
    ax_dff_legend.set_ylim((dff_min, dff_max))
    ax_dff_legend.set_xlim((0, 1))
    ax_dff_legend.plot((0.5, 0.5), (dff_min, dff_min + dff_legend),
                       color='black',
                       linewidth=2)
    ax_dff_legend.text(0,
                       dff_min + dff_legend / 2.0,
                       str(dff_legend) + '%',
                       rotation=90)

    # legend for brain states
    ax_bs_legend.set_ylim((0, 2))
    ax_bs_legend.set_xlim((0, 10))
    ax_bs_legend.text(0.5, 0.5, 'REM', color=state_map[0])
    ax_bs_legend.text(3.3, 0.5, 'NREM', color=state_map[2])
    ax_bs_legend.text(7, 0.5, 'Wake', color=state_map[1])
    ax_bs_legend.get_xaxis().set_visible(False)
    ax_bs_legend.get_yaxis().set_visible(False)
    ax_bs_legend.spines["top"].set_visible(False)
    ax_bs_legend.spines["right"].set_visible(False)
    ax_bs_legend.spines["bottom"].set_visible(False)
    ax_bs_legend.spines["left"].set_visible(False)

    tstart = t[its]
    for i in range(2, len(tspec)):
        curr_t = tstart + tspec[i]
        ax_eeg.cla()
        ax_eeg.pcolor(tspec[:i],
                      fspec[ifreq],
                      Sxx[ifreq, :i],
                      vmin=0,
                      vmax=med * vm)
        ax_eeg.set_xlim((tspec[0], tspec[-1]))
        ax_eeg.set_xticks([])
        ax_eeg.set_ylabel('Freq. (Hz)')

        # displary current movie frame
        ax_video.cla()
        iframe = closest_neighbor(vid_time, curr_t)[1]
        img = cv2.imread(
            os.path.join(ppath, name, 'Stack', 'fig%d.jpg' % (iframe + 1)))
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        ax_video.imshow(cv2.transpose(img))

        # show EMG
        emg = EMG[its:closest_neighbor(t, curr_t)[1]]
        ax_emg.cla()
        temg = np.arange(0, len(emg)) * dt
        ax_emg.plot(temg, emg, color='black', linewidth=0.5)
        ax_emg.set_xlim((tspec[0], tspec[-1]))
        ax_emg.set_ylim((-emg_max, emg_max))
        # plot EMG legend
        ax_emg.plot(([tspec[-1] - 1, tspec[-1] - 1]),
                    (-emg_legend / 2.0, emg_legend / 2.0),
                    color='black',
                    linewidth=2)

        # plot brain state patches
        j = i - 1
        ax_bs.add_patch(
            patches.Rectangle((tspec[j], 0),
                              tspec[j + 1] - tspec[j],
                              1,
                              facecolor=state_map[int(states[j]) - 1],
                              edgecolor=state_map[int(states[j]) - 1]))
        ax_bs.set_xlim((tspec[0], tspec[-1]))
        ax_bs.set_ylim((0, 1))

        ax_dff.cla()
        ax_dff.plot(tspec[:i], dff[:i], color='blue')
        ax_dff.set_xlim((tspec[0], tspec[-1]))
        ax_dff.set_ylim((dff_min, dff_max))

        plt.savefig(os.path.join(movie_stack, 'fig%d.png' % i))

    encode_video(ppath,
                 name,
                 stack='MStack',
                 ending='.png',
                 fr=10,
                 outpath=os.path.join(ppath, name),
                 ffmpeg_path=ffmpeg_path,
                 vidname='movie_fibpho_')
コード例 #4
0
ファイル: vypro.py プロジェクト: Lozanoda/Lab
def opto_video(ppath,
               name,
               ts,
               te,
               fmax=20,
               emg_legend=1000,
               vm=2.0,
               time_legend=10,
               ffmpeg_path='ffmpeg'):
    """
    Generate video for optogenetic sleep recording.
    The function requires that ffmpeg is installed on your system (http://ffmpeg.org).
    Windows Users: Specify the full path to the ffmpeg program

    The resulting video has 1 Hz resolution and will be saved in folder $ppath/$name
    :param ppath: base folder
    :param name: name of recording
    :param ts: start time in seconds
    :param te: end time in second
    :param fmax: maximum frequency on EEG spectrogram
    :param emg_legend: EMG legend in micro Volts
    :param vm: controls saturation of EEG spectrogram; a value in the range from 1 to 2 should work best
    :param time_legend: time legend in seconds
    :param ffmpeg_path: full, absolute path to ffmpeg program; important for to set in Windows
    :return: n/a
    """

    # helper function ######################
    def closest_neighbor(vec, x):
        d = np.abs(vec - x)
        el = np.min(d)
        idx = np.argmin(d)
        return el, idx

    ########################################

    # setup figure arrangement
    sleepy.set_fontsize(12)
    plt.ion()
    plt.figure()
    plt.figure(figsize=(8, 6))

    ax_video = plt.axes([0.1, 0.52, 0.8, 0.45])
    ax_laser = plt.axes([0.1, 0.45, 0.8, 0.03])
    ax_eeg = plt.axes([0.1, 0.28, 0.8, 0.15])
    ax_emg = plt.axes([0.1, 0.11, 0.8, 0.15])
    ax_bs = plt.axes([0.1, 0.05, 0.8, 0.05])
    ax_time = plt.axes([0.1, 0.01, 0.8, 0.031])

    movie_stack = os.path.join(ppath, name, 'MStack')
    if not (os.path.isdir(movie_stack)):
        os.mkdir(movie_stack)

    sr = sleepy.get_snr(ppath, name)
    M = sleepy.load_stateidx(ppath, name)[0]
    dt = 1.0 / sr

    nbins = int(np.round(sr) * 5.0 / 2)
    Mup = spyke.upsample_mx(M, nbins)

    EEG = so.loadmat(os.path.join(ppath, name, 'EEG.mat'),
                     squeeze_me=True)['EEG']
    EMG = so.loadmat(os.path.join(ppath, name, 'EMG.mat'),
                     squeeze_me=True)['EMG']
    vid_time = so.loadmat(os.path.join(ppath, name, 'video_timing.mat'),
                          squeeze_me=True)['onset']
    len_eeg = EEG.shape[0]

    t = np.arange(0, len_eeg) * dt
    its = closest_neighbor(t, ts)[1]
    ite = closest_neighbor(t, te)[1]
    data_eeg = EEG[its:ite]
    states = sleepy.downsample_states(Mup[its:ite], int(np.round(sr)))
    state_map = [[0, 1, 1], [0.5, 0, 1], [0.6, 0.6, 0.6]]

    # load laser
    laser_map = [[1, 1, 1], [0, 0.3, 1]]
    laser = sleepy.load_laser(ppath, name)
    laser = laser[its:ite]
    idxs, idxe = sleepy.laser_start_end(laser, SR=sr)
    npers = int(np.round(sr))
    idxs = [int(i / npers) for i in idxs]
    idxe = [int(i / npers) for i in idxe]

    # setup axis for video
    ax_video.get_xaxis().set_visible(False)
    ax_video.get_yaxis().set_visible(False)
    ax_video.spines["top"].set_visible(False)
    ax_video.spines["right"].set_visible(False)
    ax_video.spines["bottom"].set_visible(False)
    ax_video.spines["left"].set_visible(False)

    # setup axes for EEG spectrogram
    sleepy.box_off(ax_eeg)
    ax_eeg.set_xticks([])
    plt.gcf().text(0.11, 0.38, 'EEG', color='white')

    # setup axes for EMG
    ax_emg.get_xaxis().set_visible(False)
    ax_emg.get_yaxis().set_visible(False)
    ax_emg.spines["top"].set_visible(False)
    ax_emg.spines["right"].set_visible(False)
    ax_emg.spines["bottom"].set_visible(False)
    ax_emg.spines["left"].set_visible(False)
    emg_max = np.max(np.abs(EMG[its:ite]))
    emg_max = emg_max + emg_max * 0.1
    # write "EMG" and label EMG legend
    plt.gcf().text(0.905, 0.21, "%.1f mV" % (emg_legend / 1000.0), rotation=90)
    plt.gcf().text(0.11, 0.25, 'EMG')

    ax_bs.get_xaxis().set_visible(False)
    ax_bs.get_yaxis().set_visible(False)
    ax_bs.spines["top"].set_visible(False)
    ax_bs.spines["right"].set_visible(False)
    ax_bs.spines["bottom"].set_visible(False)
    ax_bs.spines["left"].set_visible(False)

    # calculate spectrogram
    fspec, tspec, Sxx = scipy.signal.spectrogram(data_eeg,
                                                 fs=sr,
                                                 nperseg=2 * npers,
                                                 noverlap=npers)
    ifreq = np.where(fspec <= fmax)[0]
    med = np.median(Sxx.max(axis=0))
    nspec = len(tspec)
    laser = np.zeros((nspec, ))
    for (i, j) in zip(idxs, idxe):
        laser[i:j + 1] = 1

    # setup axis for laser
    ax_laser.get_xaxis().set_visible(False)
    ax_laser.get_yaxis().set_visible(False)
    ax_laser.spines["top"].set_visible(False)
    ax_laser.spines["right"].set_visible(False)
    ax_laser.spines["bottom"].set_visible(False)
    ax_laser.spines["left"].set_visible(False)
    # write "Laser"
    plt.gcf().text(0.11, 0.46, 'Laser', color=laser_map[1])

    # legend for brain states
    plt.gcf().text(0.7, 0.01, 'REM', color=state_map[0])
    plt.gcf().text(0.77, 0.01, 'Wake', color=state_map[1])
    plt.gcf().text(0.84, 0.01, 'NREM', color=state_map[2])

    # setup time legend (beolow DF/F panel)
    ax_time.plot((tspec[0], tspec[0] + time_legend), [1, 1],
                 color='black',
                 linewidth=2)
    ax_time.set_xlim((tspec[0], tspec[-1]))
    ax_time.set_ylim((-1, 1.1))
    ax_time.get_xaxis().set_visible(False)
    ax_time.get_yaxis().set_visible(False)
    ax_time.spines["top"].set_visible(False)
    ax_time.spines["right"].set_visible(False)
    ax_time.spines["bottom"].set_visible(False)
    ax_time.spines["left"].set_visible(False)
    ax_time.text(tspec[0], -1, '%s s' % str(time_legend))

    tstart = t[its]
    for i in range(2, len(tspec)):
        curr_t = tstart + tspec[i]
        ax_eeg.cla()
        ax_eeg.pcolor(tspec[:i],
                      fspec[ifreq],
                      Sxx[ifreq, :i],
                      vmin=0,
                      vmax=med * vm)
        ax_eeg.set_xlim((tspec[0], tspec[-1]))
        ax_eeg.set_xticks([])
        ax_eeg.set_ylabel('Freq. (Hz)')

        # displary current movie frame
        ax_video.cla()
        iframe = closest_neighbor(vid_time, curr_t)[1]
        img = cv2.imread(
            os.path.join(ppath, name, 'Stack', 'fig%d.jpg' % (iframe + 1)))
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        ax_video.imshow(img)

        # show EMG
        emg = EMG[its:closest_neighbor(t, curr_t)[1]]
        ax_emg.cla()
        temg = np.arange(0, len(emg)) * dt
        ax_emg.plot(temg, emg, color='black', linewidth=0.5)
        ax_emg.set_xlim((tspec[0], tspec[-1]))
        ax_emg.set_ylim((-emg_max, emg_max))
        # plot EMG legend
        ax_emg.plot(([tspec[-1] - 1, tspec[-1] - 1]),
                    (-emg_legend / 2.0, emg_legend / 2.0),
                    color='black',
                    linewidth=2)

        # plot brain state patches
        j = i - 1
        ax_bs.add_patch(
            patches.Rectangle((tspec[j - 1], 0),
                              tspec[j] - tspec[j - 1],
                              1,
                              facecolor=state_map[int(states[j]) - 1],
                              edgecolor=state_map[int(states[j]) - 1]))
        ax_bs.set_xlim((tspec[0], tspec[-1]))
        ax_bs.set_ylim((0, 1))

        # plot laser
        #pdb.set_trace()
        ax_laser.add_patch(
            patches.Rectangle((tspec[j - 1], 0),
                              tspec[j] - tspec[j - 1],
                              1,
                              facecolor=laser_map[int(laser[j])],
                              edgecolor=laser_map[int(laser[j])]))
        ax_laser.set_ylim((0, 1))
        ax_laser.set_xlim((tspec[0], tspec[-1]))

        if i % 10 == 0:
            print("done with frame %d out of %d frames" % (i, len(tspec)))
        plt.savefig(os.path.join(movie_stack, 'fig%d.png' % i))

    encode_video(ppath,
                 name,
                 stack='MStack',
                 ending='.png',
                 fr=5,
                 outpath=os.path.join(ppath, name),
                 ffmpeg_path=ffmpeg_path,
                 vidname='movie_opto_')
コード例 #5
0
ファイル: spont_rem.py プロジェクト: tortugar/Lab
    ampl = np.sqrt(SM[imu,:].sum(axis=0)*df)
      
    # load normalized spectrogram
    #SP, t, freq = sleepy.normalize_spectrogram(ppath, name, fmax, pplot=False)
    P = so.loadmat(os.path.join(ppath, name,  'sp_' + name + '.mat'), squeeze_me=True)
    SP = P['SP']
    freq = P['freq']
    t = P['t']

    ifreq = np.where(freq<=fmax)[0]
    SP = SP[ifreq,:]

    sp_mean = SP.mean(axis=1)
    SP = np.divide(SP, np.tile(sp_mean, (SP.shape[1], 1)).T)

    M = sleepy.load_stateidx(ppath, name)[0]
    rem_idx = np.where(M==1)[0]
    seq = sleepy.get_sequences(np.where(M==1)[0])
    
    SR = sleepy.get_snr(ppath, name)
    NBIN = np.round(2.5*SR)
    dt = NBIN * 1.0/SR
    
    ipre = int(np.round(pre/dt))
    ipost = int(np.round(post/dt))
    
    for s in seq:
        if len(s)*dt >= rem_thr and s[0]-pre >=0 and s[0]+ipost < len(M):
            tmp = SP[:, s[0]-ipre:s[0]+ipost]
            spec_mice[idf].append(tmp)