예제 #1
0
파일: plotter.py 프로젝트: salnat/moran_lab
def plot_psth(spike_train,
              event_dic,
              bin_width=0.05,
              start_time=-1,
              end_time=4,
              overlap=0,
              normalize='Hz'):
    """
    spike_train is an array
    event dic is a dictionary of event times gotten from the get_all_events_from_directory function.
    bin width, start time and end time are integers in seconds.

    plots a smooth curve PSTH for every taste based on event times and correlation with spikes that happened within the start to end times.
    """

    assert end_time > start_time, 'start time cannot be bigger or equal to end time'
    assert normalize == 'Hz' or normalize == 'Average', 'normalize parameter has to be Average or Hz'
    # assert isinstance(spike_train,(list,np.array,np.ndarray)), 'spike train has to be a list of spike times.'
    assert isinstance(event_dic, dict), 'event dic needs to be a dictionary'

    bin_amount = (end_time - start_time) // bin_width
    plt.figure(1)
    for taste in event_dic:
        print('collecting spike times for event of taste: %s') % taste
        spikes = [
            spike_train[i] - event for i in range(len(spike_train))
            for event in event_dic[taste]
            if start_time < spike_train[i] - event < end_time
        ]
        hist1, bin_edges = np.histogram(spikes, bin_amount,
                                        (start_time, end_time))
        average_spikes_in_bin = hist1 / float(len(event_dic[taste]))
        if normalize == 'Hz':
            spikes_in_bin = average_spikes_in_bin / bin_width
        norm_curve = savitzky_golay(spikes_in_bin, 9, 3)
        plt.plot(bin_edges[:-1],
                 norm_curve,
                 label=taste,
                 color=get_color_for_taste(taste))
    plt.xlabel('time')
    if normalize == 'Hz':
        plt.ylabel('Fire rate - spikes / s (Hz)')
    else:
        plt.ylabel('spikes')
    plt.legend()
    plt.axvline(0, linestyle='--', color='k')  # vertical lines
    plt.show()
    return
예제 #2
0
def plot_psth_with_rasters_with_without_laser(spike_train,
                                              event_dic_with_laser_data,
                                              laser_start,
                                              laser_stop,
                                              taste_list,
                                              bin_width=0.05,
                                              start_time=-2,
                                              end_time=5,
                                              overlap=0,
                                              normalize='Hz'):
    """
    plots a figure with 4 subplots, the top will be a raster with all tastes given in the list. the bottom will be a PSTH with the tastes given in the list.
    spike train is a list of spike times.
    event_dic is a dictionary with keys as tastes and values that are lists of taste times.
    start and end time are the boundaries in secs of the PSTH and raster.
    """
    assert isinstance(
        taste_list, list
    ), 'tastes parameter need to be a list of the taste event file names'
    assert end_time > start_time, 'start time cannot be bigger or equal to end time'
    assert normalize == 'Hz' or normalize == 'Average', 'normalize parameter has to be Average or Hz'
    # assert isinstance(spike_train,(list,np.array,np.ndarray)), 'spike train has to be a list of spike times.'
    assert isinstance(event_dic_with_laser_data,
                      dict), 'event dic needs to be a dictionary'

    ############ Rasters ##############

    fig = plt.figure(figsize=(20, 15), dpi=1000)
    ax1 = fig.add_subplot(221)
    j = 0
    for taste in taste_list:
        # print 'collecting spike times for event of taste: %s' % taste
        event_times_list = []
        for event in event_dic_with_laser_data['without laser'][taste]:
            # get the spike times that are in the range of start-stop from each event.
            event_times_list.append([
                spike_train[i] - event for i in range(len(spike_train))
                if start_time < spike_train[i] - event < end_time
            ])
        for ith, trial in enumerate(event_times_list):
            ax1.vlines(trial,
                       j + ith + .5,
                       j + ith + 1.5,
                       color=get_color_for_taste(taste))
        j += len(event_times_list)
    ax1.set_ylim(.5, len(event_times_list) * len(taste_list) + .5)
    ax1.set_xlabel('time')
    ax1.set_ylabel('trial')
    ax1.set_title('Normal')
    ax1.axvline(0, linestyle='--', color='k')  # vertical lines
    ax1.axvline(laser_start, linestyle='--', color='r')  # vertical lines
    ax1.axvline(laser_stop, linestyle='--', color='r')  # vertical lines

    ax2 = fig.add_subplot(222)
    j = 0
    for taste in taste_list:
        # print 'collecting spike times for event of taste: %s' % taste
        event_times_list = []
        for event in event_dic_with_laser_data['with laser'][taste]:
            # get the spike times that are in the range of start-stop from each event.
            event_times_list.append([
                spike_train[i] - event for i in range(len(spike_train))
                if start_time < spike_train[i] - event < end_time
            ])
        for ith, trial in enumerate(event_times_list):
            ax2.vlines(trial,
                       j + ith + .5,
                       j + ith + 1.5,
                       color=get_color_for_taste(taste))
        j += len(event_times_list)
    ax2.set_ylim(.5, len(event_times_list) * len(taste_list) + .5)
    ax2.set_xlabel('time')
    ax2.set_ylabel('trial')
    ax2.set_title('BLAx')
    ax2.axvline(0, linestyle='--', color='k')  # vertical lines
    ax2.axvline(laser_start, linestyle='--', color='r')  # vertical lines
    ax2.axvline(laser_stop, linestyle='--', color='r')  # vertical lines

    ############ PSTHs ##############

    ax3 = fig.add_subplot(221)
    bin_amount = (end_time - start_time) // bin_width
    for taste in taste_list:
        # get the spike times that are in the range of start-stop from each event.
        spikes = [
            spike_train[i] - event for i in range(len(spike_train))
            for event in event_dic_with_laser_data['without laser'][taste]
            if start_time < spike_train[i] - event < end_time
        ]
        hist1, bin_edges = np.histogram(spikes, bin_amount,
                                        (start_time, end_time))
        average_spikes_in_bin = hist1 / float(
            len(event_dic_with_laser_data['without laser'][taste]))
        if normalize == 'Hz':
            spikes_in_bin = average_spikes_in_bin / bin_width
        norm_curve = savitzky_golay(spikes_in_bin, 9, 3)
        ax3.plot(bin_edges[:-1],
                 norm_curve,
                 label=taste,
                 color=get_color_for_taste(taste))
    ax3.set_xlabel('time')
    if normalize == 'Hz':
        ax3.set_ylabel('Fire rate - spikes / s (Hz)')
    else:
        ax3.set_ylabel('spikes')
    ax3.set_label('Normal')
    ax3.axvline(0, linestyle='--', color='k')  # vertical lines
    ax3.axvline(laser_start, linestyle='--', color='r')  # vertical lines
    ax3.axvline(laser_stop, linestyle='--', color='r')  # vertical lines

    ax4 = fig.add_subplot(222)
    bin_amount = (end_time - start_time) // bin_width
    for taste in taste_list:
        # get the spike times that are in the range of start-stop from each event.
        spikes = [
            spike_train[i] - event for i in range(len(spike_train))
            for event in event_dic_with_laser_data['with laser'][taste]
            if start_time < spike_train[i] - event < end_time
        ]
        hist1, bin_edges = np.histogram(spikes, bin_amount,
                                        (start_time, end_time))
        average_spikes_in_bin = hist1 / float(
            len(event_dic_with_laser_data['with laser'][taste]))
        if normalize == 'Hz':
            spikes_in_bin = average_spikes_in_bin / bin_width
        norm_curve = savitzky_golay(spikes_in_bin, 9, 3)
        ax4.plot(bin_edges[:-1],
                 norm_curve,
                 label=taste,
                 color=get_color_for_taste(taste))
    ax4.set_xlabel('time')
    if normalize == 'Hz':
        ax4.set_ylabel('Fire rate - spikes / s (Hz)')
    else:
        ax4.set_ylabel('spikes')
    ax4.set_label('Blax')
    ax4.axvline(0, linestyle='--', color='k')  # vertical lines
    ax4.axvline(laser_start, linestyle='--', color='r')  # vertical lines
    ax4.axvline(laser_stop, linestyle='--', color='r')  # vertical lines
    ax4.legend()
    adjust_ylim(ax3, ax4)
    # plt.show()
    return fig
예제 #3
0
파일: plotter.py 프로젝트: salnat/moran_lab
def plot_psth_with_rasters_for_axes(raster_ax,
                                    psth_ax,
                                    spike_train,
                                    event_dic,
                                    taste_list,
                                    bin_width=0.05,
                                    start_time=-1,
                                    end_time=4,
                                    overlap=0,
                                    normalize='Hz'):
    """
    plots a figure with 2 subplots, the top will be a raster with all tastes given in the list. the bottom will be a PSTH with the tastes given in the list.
    spike train is a list of spike times.
    event_dic is a dictionary with keys as tastes and values that are lists of taste times.
    start and end time are the boundaries in secs of the PSTH and raster.
    """

    assert isinstance(
        taste_list, list
    ), 'tastes parameter need to be a list of the taste event file names'
    assert end_time > start_time, 'start time cannot be bigger or equal to end time'
    assert normalize == 'Hz' or normalize == 'Average', 'normalize parameter has to be Average or Hz'
    # assert isinstance(spike_train,(list,np.array,np.ndarray)), 'spike train has to be a list of spike times.'
    assert isinstance(event_dic, dict), 'event dic needs to be a dictionary'

    ############ Raster ##############

    j = 0
    for taste in taste_list:
        print('collecting spike times for events of taste: {}'.format(taste))
        event_times_list = []
        for event in event_dic[taste]:
            # get the spike times that are in the range of start-stop from each event.
            left_border = np.searchsorted(spike_train - event, -1)
            right_border = np.searchsorted(spike_train - event, 4)
            spikes = spike_train[left_border:right_border] - event
            event_times_list.append(spikes)
        for ith, trial in enumerate(event_times_list):
            raster_ax.vlines(trial,
                             j + ith + .5,
                             j + ith + 1.5,
                             color=get_color_for_taste(taste))
        j += len(event_times_list)
    raster_ax.set_ylim(.5, len(event_times_list) * len(taste_list) + .5)
    raster_ax.set_xlabel('time')
    raster_ax.set_ylabel('trial')
    raster_ax.axvline(0, linestyle='--', color='k')  # vertical lines

    ############ PSTH ##############

    bin_amount = (end_time - start_time) // bin_width
    all_spikes = np.array([])
    for taste in taste_list:
        for event in event_dic[taste]:
            # get the spike times that are in the range of start-stop from each event.
            left_border = np.searchsorted(spike_train - event, -1)
            right_border = np.searchsorted(spike_train - event, 4)
            spikes = spike_train[left_border:right_border] - event
            all_spikes = np.concatenate((all_spikes, spikes))
        hist1, bin_edges = np.histogram(all_spikes, int(bin_amount),
                                        (start_time, end_time))
        average_spikes_in_bin = hist1 / float(len(event_dic[taste]))
        if normalize == 'Hz':
            spikes_in_bin = average_spikes_in_bin / bin_width
        norm_curve = savitzky_golay(spikes_in_bin, 9, 3)
        psth_ax.plot(bin_edges[:-1],
                     norm_curve,
                     label=taste,
                     color=get_color_for_taste(taste))
    psth_ax.set_xlabel('time')
    if normalize == 'Hz':
        psth_ax.set_ylabel('Fire rate - spikes / s (Hz)')
    else:
        psth_ax.set_ylabel('spikes')
    psth_ax.axvline(0, linestyle='--', color='k')  # vertical lines
    psth_ax.legend()
    # plt.show()
    return raster_ax, psth_ax
예제 #4
0
파일: plotter.py 프로젝트: salnat/moran_lab
def plot_psth_with_raster_divide_by_trial(electrode,
                                          cluster,
                                          spike_train,
                                          trial_change,
                                          event_dic,
                                          taste_list,
                                          bin_width=0.05,
                                          start_time=-1,
                                          end_time=4,
                                          overlap=0,
                                          normalize='Hz'):
    """
    plots a figure with 2 subplots, the top will be a raster with all tastes given in the list. the bottom will be a PSTH with the tastes given in the list.
    spike train is a list of spike times.
    event_dic is a dictionary with keys as tastes and values that are lists of taste times.
    start and end time are the boundaries in secs of the PSTH and raster.
    """

    assert isinstance(
        taste_list, list
    ), 'tastes parameter need to be a list of the taste event file names'
    assert end_time > start_time, 'start time cannot be bigger or equal to end time'
    assert normalize == 'Hz' or normalize == 'Average', 'normalize parameter has to be Average or Hz'
    # assert isinstance(spike_train,(list,np.array,np.ndarray)), 'spike train has to be a list of spike times.'
    assert isinstance(event_dic, dict), 'event dic needs to be a dictionary'

    ############ Raster ##############

    fig = plt.figure(1, (20, 20))
    fig.suptitle('Electrode: {}, Cluster: {}'.format(electrode, cluster),
                 fontsize=20)
    ax1 = fig.add_subplot(211)
    j = 0
    for taste in taste_list:
        print('collecting spike times for events of taste: {}'.format(taste))
        event_times_list = []
        for event in event_dic[taste]:
            # get the spike times that are in the range of start-stop from each event.
            event_times_list.append([
                spike_train[i] - event for i in range(len(spike_train))
                if start_time < spike_train[i] - event < end_time
            ])
        for ith, trial in enumerate(event_times_list):
            ax1.vlines(trial,
                       j + ith + .5,
                       j + ith + 1.5,
                       color=get_color_for_taste(taste))
        j += len(event_times_list)
    ax1.set_ylim(.5, len(event_times_list) * len(taste_list) + .5)
    ax1.set_xlabel('time')
    ax1.set_ylabel('trial')
    ax1.axvline(0, linestyle='--', color='k')  # vertical lines

    ############ PSTH ##############

    ax2 = fig.add_subplot(212)
    bin_amount = (end_time - start_time) // bin_width
    for taste in taste_list:
        # get the spike times that are in the range of start-stop from each event.
        spikes = [
            spike_train[i] - event for i in range(len(spike_train))
            for event in event_dic[taste][:trial_change]
            if start_time < spike_train[i] - event < end_time
        ]
        hist1, bin_edges = np.histogram(spikes, bin_amount,
                                        (start_time, end_time))
        average_spikes_in_bin = hist1 / float(trial_change)
        if normalize == 'Hz':
            spikes_in_bin = average_spikes_in_bin / bin_width
        norm_curve = savitzky_golay(spikes_in_bin, 9, 3)
        ax2.plot(bin_edges[:-1],
                 norm_curve,
                 label=taste + ' - first trials',
                 color=get_color_for_taste(taste),
                 linestyle='solid')

        spikes = [
            spike_train[i] - event for i in range(len(spike_train))
            for event in event_dic[taste][trial_change:]
            if start_time < spike_train[i] - event < end_time
        ]
        hist1, bin_edges = np.histogram(spikes, bin_amount,
                                        (start_time, end_time))
        average_spikes_in_bin = hist1 / float(
            len(event_dic[taste]) - trial_change)
        if normalize == 'Hz':
            spikes_in_bin = average_spikes_in_bin / bin_width
        norm_curve = savitzky_golay(spikes_in_bin, 9, 3)
        ax2.plot(bin_edges[:-1],
                 norm_curve,
                 label=taste + ' - last trials',
                 color=get_color_for_taste(taste),
                 linestyle='dashed')
    ax2.set_xlabel('time')
    if normalize == 'Hz':
        ax2.set_ylabel('Fire rate - spikes / s (Hz)')
    else:
        ax2.set_ylabel('spikes')
    ax2.axvline(0, linestyle='--', color='k')  # vertical lines
    ax2.legend()
    # plt.show()
    return fig