コード例 #1
0
ファイル: map_to_brain.py プロジェクト: vala1958/network
def plot_brain_substates_start_end(trial_num_states, lst_start_and_end_indices,
                                   obs, vmin, vmax, state_number, time_bins,
                                   name, trial_type, save_figures, file, title,
                                   kind_prepro, run):

    if len(lst_start_and_end_indices) > 8:
        print(
            'number of substates is to large (>8), please use other function or select manually, maximal 8'
        )
    else:

        allenMap, allenIndices = load_matrix('')
        sub_state_number_list = [
            x for x in range(0, len(lst_start_and_end_indices))
        ]

        figsize = get_figsize(lst_start_and_end_indices)

        fig, ax = plt.subplots(1,
                               len(lst_start_and_end_indices),
                               figsize=figsize,
                               tight_layout=False)

        for i in range(len(lst_start_and_end_indices)):
            average_obs_per_sub_state = (np.mean(
                obs[lst_start_and_end_indices[i][0]:
                    lst_start_and_end_indices[i][1], :],
                axis=0))
            if i == len(lst_start_and_end_indices) - 1:
                plot_area_values(fig,
                                 ax[i],
                                 allenMap,
                                 allenIndices,
                                 average_obs_per_sub_state,
                                 vmin=vmin,
                                 vmax=vmax,
                                 haveColorBar=True)
            else:
                plot_area_values(fig,
                                 ax[i],
                                 allenMap,
                                 allenIndices,
                                 average_obs_per_sub_state,
                                 vmin=vmin,
                                 vmax=vmax,
                                 haveColorBar=False)

            ax[i].set_title('state {}, substate {} '.format(
                state_number, sub_state_number_list[i]))

        if save_figures:
            new = name + ' plots results {} {} {}'.format(
                trial_type, time_bins, trial_num_states)
            subdirectory(file, new)
            plt.savefig(new +
                        '/brainmap_sub_{}_{}_{}_{}_{}_{}_{}_run{}.png'.format(
                            name, trial_type, time_bins, title, get_date(),
                            state_number, kind_prepro, run))

        plt.show()
コード例 #2
0
ファイル: fit_and_viterbi.py プロジェクト: vala1958/network
def plot_transistion_matrix(hmm, time_bins, save_figures, name, trial_type,
                            trial_num_states, file, type_fit, run):
    """
    function to plot the Transition Matrix and the  masked transition matrix (diagonal elements)
    :param hmm:         model
    :param time_bins:   number of frames
    :return:            fig
    """

    trans_matrix = hmm.transitions.transition_matrix

    fig = plt.figure(figsize=(6, 5))

    sns.heatmap(trans_matrix, cmap='viridis', alpha=0.8)
    plt.title('learned Transition Matrix: {}, {}, {}, {}, {}'.format(
        name, trial_type, time_bins, trial_num_states, type_fit))
    plt.ylabel('state number')

    if save_figures:
        # create new subdirectory name, function subdirectory creates that subdirectory if it does not already
        # exist
        new = name + ' plots results {} {} {}'.format(trial_type, time_bins,
                                                      trial_num_states)
        subdirectory(file, new)
        plt.savefig(new + '/Trans.Mat_{}_{}_{}_run{}.png'.format(
            name, trial_type, get_date(), run, bbox_inches="tight"))

    return fig
コード例 #3
0
ファイル: fit_and_viterbi.py プロジェクト: vala1958/network
def plot_fitting_curve(hmm_lls, optimizer, save_figures, file, name,
                       trial_type, time_bins, trial_num_states, title,
                       kind_prepro, run):

    fig = plt.figure(figsize=(8, 6))

    plt.plot(hmm_lls, label="EM")

    #if true_ll is known:
    # plt.plot([0, N_iters], true_ll * np.ones(2), ':k', label="True")

    plt.xlabel('{} Iteration'.format(optimizer))
    plt.ylabel("Log Probability")
    plt.legend(loc="lower right")

    if save_figures:
        # create new subdirectory name, function subdirectory creates that subdirectory if it does not already
        # exist
        new = name + ' plots results {} {} {}'.format(trial_type, time_bins,
                                                      trial_num_states)
        subdirectory(file, new)
        plt.savefig(new + '/fit_{}_{}_{}_{}_{}_run{}.png'.format(
            name,
            trial_type,
            title,
            kind_prepro,
            get_date(),
            run,
            bbox_inches='tight',
        ))
    return fig
コード例 #4
0
ファイル: map_to_brain.py プロジェクト: vala1958/network
def plot_brain_states_nan(trial_num_states, lst_average_obs_per_state, vmin,
                          vmax, time_bins, name, trial_type, delay,
                          save_figures, file, title, kind_prepro, run):
    """
    function to plot the brain states. each brainstate is one image and each is saved individually, including the
    possibility of empty (non existing) states
    """

    allenMap, allenIndices = load_matrix('')
    state_number_list = [x for x in range(0, trial_num_states)]

    for i in range(len(lst_average_obs_per_state)):

        sum_mean_all_roi = sum(lst_average_obs_per_state[i])

        if sum_mean_all_roi == 0:
            print('state {} does not exist!'.format(i))

        else:
            fig, ax = plt.subplots(1, 1, figsize=(6, 6), tight_layout=False)

            plot_area_values(fig,
                             ax,
                             allenMap,
                             allenIndices,
                             lst_average_obs_per_state[i],
                             vmin=vmin,
                             vmax=vmax,
                             haveColorBar=True)
            ax.set_title(
                'activity of the 27 Rois: {}, {}, {}\n{}s, {}, state {}  '.
                format(name, trial_type, time_bins, delay, title,
                       state_number_list[i]))

            if save_figures:
                new = name + ' plots results {} {} {}'.format(
                    trial_type, time_bins, trial_num_states)
                subdirectory(file, new)
                plt.savefig(new +
                            '/brainmap_{}_{}_{}_{}_{}_{}_{}_run{}.png'.format(
                                name, trial_type, time_bins, title, get_date(),
                                state_number_list[i], kind_prepro, run))

    # plot the as png exported legend
    plt.rcParams["axes.grid"] = False
    plt.figure(figsize=(4, 2))

    img = plt.imread('legend.png')
    plt.imshow(img)
    plt.axis('off')

    if save_figures:
        new = name + ' plots results {} {} {}'.format(trial_type, time_bins,
                                                      trial_num_states)
        subdirectory(file, new)
        plt.savefig(new + '/legend.png')

    plt.show()
コード例 #5
0
ファイル: hyperparameter.py プロジェクト: vala1958/network
def bar_plot_single(mini, maxi, arr, trial_type, name, kind, e_model, obs,
                    save_figure, file, run):

    maxi = maxi + 1  # python counting
    ind = np.arange(maxi - mini)  # the x locations for the groups
    width = 0.5  # the width of the bars: can also be len(x) sequence

    # Höhe der Bars
    lst_arr_bar = []
    for i in range(mini, maxi):
        lst_arr_bar.append(arr[arr == i].size)

    def autolabel(rects, ax):
        """
        Attach a text label above each bar displaying its height
        """
        for rect in rects:
            height = rect.get_height()
            ax.text(rect.get_x() + rect.get_width() / 2.,
                    1. * height,
                    '%d' % int(height),
                    ha='center',
                    va='bottom')

        return autolabel

    lst_bar_hights = lst_arr_bar.copy()

    fig, ax = plt.subplots(1, figsize=(4, 4), sharey=True)

    rect = ax.bar(ind, lst_bar_hights, width, color='g', alpha=0.5)
    ax.axhline(0, color='grey', linewidth=0.8)
    ax.set_ylabel('occurrence')
    ax.set_title(
        'Occurrence:\nNumbers of states\n{}, {}, {}\ntime steps {}\n{}'.format(
            trial_type, name, kind, obs.shape[0], e_model))
    ax.set_xticks(ind)
    ax.set_xticklabels(([str(x) for x in range(mini, maxi)]))

    autolabel(rect, ax)

    if save_figure:
        # create new subdirectory name, function subdirectory creates that subdirectory if it does not already
        # exist
        new = name + ' plots hyperparameter'
        subdirectory(file, new)
        plt.savefig(new +
                    '/{}_{}_{}_{}_{}_{}_run{}.png'.format(name,
                                                          trial_type,
                                                          kind,
                                                          obs.shape[0],
                                                          get_date(),
                                                          e_model,
                                                          run,
                                                          bbox_inches="tight"))

    return fig
コード例 #6
0
ファイル: map_to_brain.py プロジェクト: vala1958/network
def plot_brain_states_1(trial_num_states, lst_average_obs_per_state, vmin,
                        vmax, time_bins, name, trial_type, save_figures, file,
                        title, run):
    """
    function to plot the brainstates in one row and stored as one image, color bar only at the last map
    """

    allenMap, allenIndices = load_matrix('')
    state_number_list = [x for x in range(0, trial_num_states)]

    if trial_num_states > 8:
        print(
            'number of states is to large (squeezed plot), please use other plot function'
        )
    else:

        figsize = get_figsize(lst_average_obs_per_state)

        fig, ax = plt.subplots(1,
                               trial_num_states,
                               figsize=figsize,
                               tight_layout=False)

        for i in range(len(lst_average_obs_per_state)):
            if i == len(lst_average_obs_per_state) - 1:
                plot_area_values(fig,
                                 ax[i],
                                 allenMap,
                                 allenIndices,
                                 lst_average_obs_per_state[i],
                                 vmin=vmin,
                                 vmax=vmax,
                                 haveColorBar=True)
            else:
                plot_area_values(fig,
                                 ax[i],
                                 allenMap,
                                 allenIndices,
                                 lst_average_obs_per_state[i],
                                 vmin=vmin,
                                 vmax=vmax,
                                 haveColorBar=False)

            ax[i].set_title('state {}  '.format(state_number_list[i]))

        if save_figures:
            new = name + ' plots results {} {} {}'.format(
                trial_type, time_bins, trial_num_states)
            subdirectory(file, new)
            plt.savefig(new + '/brainmap_1_{}_{}_{}_{}_{}_{}_run{}.png'.format(
                name, trial_type, time_bins, title, get_date(),
                state_number_list[i], run))

        plt.show()
コード例 #7
0
ファイル: fit_and_viterbi.py プロジェクト: vala1958/network
def plot_transition_matrices(hmm, time_bins, save_figures, name, trial_type,
                             trial_num_states, delay, file, type_fit,
                             kind_prepro, run):
    """
    function to plot the Transition Matrix and the  masked transition matrix (diagonal elements)
    :param hmm:                    fitted model
    :param time_bins:              number of frames for the viterbi algo
    :param save_figures:           true or false, depending if the figures have to be safed
    :param name:                   mouse name (eg. M5)
    :param trial_type:             trial typ (eg. Hit)
    :param trial_num_states:       number of hidden states (the model is fitted to)
    :param file:                   the current working directory
    :param run:                    integer: to prevent overwriting of runs with identical params
    :return:                       fig
    """

    trans_matrix = hmm.transitions.transition_matrix

    trans_matrix_zero_diag = np.copy(trans_matrix)
    np.fill_diagonal(trans_matrix_zero_diag, 0)

    fig, ax = plt.subplots(1, 2, figsize=(10, 4))

    ax[0] = sns.heatmap(trans_matrix, ax=ax[0], cmap='viridis', alpha=0.8)
    ax[0].set_title(
        'learned Transition Matrix:\n {}, {}, {}, {}, {}s, {}'.format(
            name, trial_type, time_bins, trial_num_states, delay, type_fit))
    ax[0].set_ylabel('state number')

    ax[1] = sns.heatmap(trans_matrix_zero_diag,
                        mask=trans_matrix_zero_diag == 0,
                        ax=ax[1],
                        cmap='viridis',
                        alpha=0.8)
    ax[1].set_title('masked diagonal:\n same details as unmasked')
    ax[1].set_ylabel('state number')

    if save_figures:
        # create new subdirectory name, function subdirectory creates that subdirectory if it does not already
        # exist
        new = name + ' plots results {} {} {}'.format(trial_type, time_bins,
                                                      trial_num_states)
        subdirectory(file, new)
        plt.savefig(
            new +
            '/Trans.Mats_{}_{}_{}_{}_run{}.png'.format(name,
                                                       trial_type,
                                                       get_date(),
                                                       kind_prepro,
                                                       run,
                                                       bbox_inches="tight"))

    return fig
コード例 #8
0
ファイル: fit_and_viterbi.py プロジェクト: vala1958/network
def plot_state_sequence(viterbi_states, trial_num_states, time_bins, name,
                        trial_type, delay, title, kind_prepro, type_fit,
                        save_figures, file, run):
    """

    :param viterbi_states:    most probable state sequence (list)
    :param trial_num_states   integer, number of hidden states (to produce a list of integers up to that number)
                              (trial_num_states)
    :return:                  figure of the state sequence
    """
    trial_lst_states = [i + 1 for i in range(0, trial_num_states)]

    plt.rcParams["axes.grid"] = False
    cmap_trial = gradient_cmap(color_function(trial_lst_states))

    fig = plt.figure(figsize=(8, 2))
    plt.imshow(viterbi_states[None, :],
               aspect="auto",
               cmap=cmap_trial,
               vmin=0,
               vmax=len(color_function(trial_lst_states)) - 1,
               extent=(0, time_bins, 0.8, 0.82),
               alpha=1)
    plt.xlim(0, time_bins)
    plt.ylabel("Viterbi ")
    plt.yticks([])
    plt.xlabel("frames")
    plt.title('state sequence: {}, {}, {}, {}, {}s, {}, {}, {}  '.format(
        name, trial_type, time_bins, trial_num_states, delay, title,
        kind_prepro, type_fit))
    plt.tight_layout()

    if save_figures:
        # create new subdirectory name, function subdirectory creates that subdirectory if it does not already
        # exist
        new = name + ' plots results {} {} {}'.format(trial_type, time_bins,
                                                      trial_num_states)
        subdirectory(file, new)
        plt.savefig(
            new +
            '/sequence {}_{}_{}_{}_{}_run{}.png'.format(name,
                                                        trial_type,
                                                        title,
                                                        kind_prepro,
                                                        get_date(),
                                                        run,
                                                        bbox_inches="tight"))

    return fig
コード例 #9
0
ファイル: plots.py プロジェクト: vala1958/network
def plot_data_1(obs, name, trial_type, frames, delay, save_figure, file, title,
                run):
    """
    function to plot the prepared data for individual selection for averaged and concatenated data. This is
    necessary because the plot_data function has as argument the trial index which could be missing if only averaged
    or concatenated data is used. For single trial this is optional, also plot_data can be used
    :param obs:
    :param name:
    :param trial_type:
    :param frames:
    :param save_figure:
    :param file:
    :param title:
    :param run:
    :return:
    """

    lim = 1.05 * abs(obs).max()

    fig = plt.figure(figsize=(10, 10))
    for d in range(obs.shape[1]):
        plt.plot(obs[:, d] + lim * d, '-k')

    plt.xlim(0, obs.shape[0])
    plt.xlabel("time steps")

    plt.yticks(lim * np.arange(obs.shape[1]),
               ['x{}'.format(d + 1) for d in range(obs.shape[1])])

    plt.ylabel('observations')
    plt.title('{}, {} trials, {} time steps, {}s delay, {}'.format(
        name, trial_type, frames, delay, title))

    if save_figure:
        # create new subdirectory name, function subdirectory creates that subdirectory if it does not already
        # exist
        new = name + ' plots data'
        subdirectory(file, new)
        plt.savefig(new + '/{}_{}_{}_{}_run{}.png'.format(
            name, trial_type, title, get_date(), run, bbox_inches="tight"))

    return fig
コード例 #10
0
ファイル: map_to_brain.py プロジェクト: vala1958/network
def plot_brain_substates_1(trial_num_states, lst_sub_states, obs, vmin, vmax,
                           state_number, time_bins, name, trial_type,
                           save_figures, file, title, run):
    """
    function with a double loop for more then 8 states (does not work probably)
    """
    allenMap, allenIndices = load_matrix('')
    sub_state_number_list = [x for x in range(0, len(lst_sub_states))]

    figsize = get_figsize(lst_sub_states)
    number_of_rows = int(np.ceil(len(lst_sub_states) / 8))
    print(number_of_rows)

    fig, ax = plt.subplots(number_of_rows,
                           8,
                           figsize=figsize,
                           tight_layout=False)
    for j in range(number_of_rows):
        for i in range(8):
            average_obs_per_sub_state = (np.mean(obs[lst_sub_states[(j * 8) +
                                                                    i], :],
                                                 axis=0))
            plot_area_values(fig,
                             ax[j, i],
                             allenMap,
                             allenIndices,
                             average_obs_per_sub_state,
                             vmin=vmin,
                             vmax=vmax,
                             haveColorBar=False)
            ax[j, i].set_title('state {}, substate {} '.format(
                state_number, sub_state_number_list[i]))

    if save_figures:
        new = name + ' plots results {} {} {}'.format(trial_type, time_bins,
                                                      trial_num_states)
        subdirectory(file, new)
        plt.savefig(new + '/brainmap_sub_{}_{}_{}_{}_{}_{}_run{}.png'.format(
            name, trial_type, time_bins, title, get_date(), state_number, run))

    plt.show()
コード例 #11
0
ファイル: hyperparameter.py プロジェクト: vala1958/network
def bar_plot(mini, maxi, Hit, CR, FA, Early, Miss, All, name, kind, e_model,
             obs, save_figure, file, run):

    maxi = maxi + 1  # python counting
    ind = np.arange(maxi - mini)  # the x locations for the groups
    width = 0.5  # the width of the bars: can also be len(x) sequence

    # Höhe der Bars
    lst_Hit_bar = []
    for i in range(mini, maxi):
        lst_Hit_bar.append(Hit[Hit == i].size)

    lst_CR_bar = []
    for i in range(mini, maxi):
        lst_CR_bar.append(CR[CR == i].size)

    lst_FA_bar = []
    for i in range(mini, maxi):
        lst_FA_bar.append(FA[FA == i].size)

    lst_Early_bar = []
    for i in range(mini, maxi):
        lst_Early_bar.append(Early[Early == i].size)

    lst_Miss_bar = []
    for i in range(mini, maxi):
        lst_Miss_bar.append(Miss[Miss == i].size)

    lst_All_bar = []
    for i in range(mini, maxi):
        lst_All_bar.append(All[All == i].size)

    def autolabel(rects, ind):
        """
        Attach a text label above each bar displaying its height
        """
        for rect in rects:
            height = rect.get_height()
            ax[ind].text(rect.get_x() + rect.get_width() / 2.,
                         1. * height,
                         '%d' % int(height),
                         ha='center',
                         va='bottom')

        return autolabel

    lst_bar_hights = [
        lst_Hit_bar, lst_CR_bar, lst_FA_bar, lst_Early_bar, lst_Miss_bar,
        lst_All_bar
    ]
    lst_trial_typs = ['Hit', 'CR', 'FA', 'Early', 'Miss', 'All']

    fig, ax = plt.subplots(1, 6, figsize=(16, 4), sharey=True)

    for i in range(6):
        rect = ax[i].bar(ind, lst_bar_hights[i], width, color='g', alpha=0.5)
        ax[i].axhline(0, color='grey', linewidth=0.8)
        ax[0].set_ylabel('occurrence')
        ax[i].set_title(
            'Occurrence:\nNumbers of states\n{}, {}, {}\ntime steps {}\n{}'.
            format(lst_trial_typs[i], name, kind, obs.shape[0], e_model))
        ax[i].set_xticks(ind)
        ax[i].set_xticklabels(([str(x) for x in range(mini, maxi)]))

        autolabel(rect, ind=i)

    if save_figure:
        # create new subdirectory name, function subdirectory creates that subdirectory if it does not already
        # exist
        new = name + ' plots hyperparameter'
        subdirectory(file, new)
        plt.savefig(new +
                    '/{}_{}_{}_{}_{}_run{}.png'.format(name,
                                                       kind,
                                                       obs.shape[0],
                                                       e_model,
                                                       get_date(),
                                                       run,
                                                       bbox_inches="tight"))

    return fig
コード例 #12
0
ファイル: hyperparameter.py プロジェクト: vala1958/network
def plots_of_eollh_llh_AIC_vs_statenumbers_2(lst_loops, lst_table,
                                             max_num_states, e_model, kind,
                                             obs, save_figure, name,
                                             trial_type, file, run):

    if e_model == 'autoregressive':
        e_model = 'autoreg.'

    lst_table_short = [lst_table[i] for i in lst_loops]
    i = 0
    for item in lst_table_short:

        loop = lst_loops[i]
        lst_y = [
            np.asarray(item.iloc[0].values),
            np.asarray(item.iloc[1].values)
        ]
        label_y = ['e/o cross sum', 'normal']

        lst_y2 = [np.asarray(item.iloc[2].values)]
        label_y2 = 'AIC'

        txt = 'E-model: {}\nprepro.: {}'.format(e_model, kind)
        txt1 = '{}\n{} trials\ntime steps: {}\ndim: {}  '.format(
            name, trial_type, obs.shape[0], obs.shape[1])

        x = number_states(max_num_states)
        colors = ['r', 'g']

        fig, ax1 = plt.subplots(1, figsize=(8, 4))

        for n, colors in enumerate(colors):
            ax1.plot(x, lst_y[n], color=colors, label=label_y[n])
            ax1.set_xlabel('number of states')
            ax1.set_ylabel('log likelihood')
            ax1.set_title(
                'log likelihoods/AIC \nfor different state numbers, loop {}, run {}'
                .format(loop, run))
            ax1.legend(bbox_to_anchor=(1.09, 1), loc=2, borderaxespad=0.0)
            ax1.text(x=1.11,
                     y=0.11,
                     transform=ax1.transAxes,
                     s=txt,
                     fontsize=10,
                     bbox=dict(facecolor='w', alpha=0.15),
                     horizontalalignment='left',
                     verticalalignment='top')

            ax1.text(x=1.11,
                     y=0.40,
                     transform=ax1.transAxes,
                     s=txt1,
                     fontsize=10,
                     bbox=dict(facecolor='w', alpha=0.15),
                     horizontalalignment='left',
                     verticalalignment='top')

            ax1.set_facecolor('whitesmoke')
            ax1.grid(color='lavender',
                     which='both',
                     linestyle='-',
                     linewidth=1.8)
            fig.subplots_adjust(right=0.75)

            ax2 = ax1.twinx()
            ax2.plot(x,
                     lst_y2[0],
                     color='darkgrey',
                     linestyle='--',
                     label=label_y2)
            ax2.set_ylabel(label_y2)
            ax2.legend(bbox_to_anchor=(1.09, 0.85), loc=2, borderaxespad=0.0)

            if save_figure:
                # create new subdirectory name, function subdirectory creates that subdirectory if it does not already
                # exist
                new = name + ' plots hyperparameter'
                subdirectory(file, new)
                plt.savefig(
                    new +
                    '/{}_{}_{}_{}_{}_run{}.png'.format(name,
                                                       trial_type,
                                                       get_date(),
                                                       loop,
                                                       kind,
                                                       run,
                                                       bbox_inches="tight"))

        i += 1

    return fig
コード例 #13
0
ファイル: map_to_brain.py プロジェクト: vala1958/network
def plot_brain_substates_2(trial_num_states, lst_sub_states, obs, vmin, vmax,
                           state_number, time_bins, name, trial_type,
                           save_figures, file, title, kind_prepro, run):
    """
    function with a double loop for more then 8 states
    """
    allenMap, allenIndices = load_matrix('')
    sub_state_number_list = [x for x in range(0, len(lst_sub_states))]

    figsize = get_figsize_2(lst_sub_states)
    number_of_rows = int(np.ceil(len(lst_sub_states) / 8))

    fig, ax = plt.subplots(number_of_rows,
                           8,
                           figsize=figsize,
                           tight_layout=False)
    ax = ax.ravel()
    for i in range(len(lst_sub_states)):
        if len(lst_sub_states) < 8:
            if i == len(lst_sub_states) - 1:
                average_obs_per_sub_state = (np.mean(obs[lst_sub_states[i], :],
                                                     axis=0))
                plot_area_values(fig,
                                 ax[i],
                                 allenMap,
                                 allenIndices,
                                 average_obs_per_sub_state,
                                 vmin=vmin,
                                 vmax=vmax,
                                 haveColorBar=True)
            else:
                average_obs_per_sub_state = (np.mean(obs[lst_sub_states[i], :],
                                                     axis=0))
                plot_area_values(fig,
                                 ax[i],
                                 allenMap,
                                 allenIndices,
                                 average_obs_per_sub_state,
                                 vmin=vmin,
                                 vmax=vmax,
                                 haveColorBar=False)
        else:
            if i == 7 or i == 15 or i == 23:
                average_obs_per_sub_state = (np.mean(obs[lst_sub_states[i], :],
                                                     axis=0))
                plot_area_values(fig,
                                 ax[i],
                                 allenMap,
                                 allenIndices,
                                 average_obs_per_sub_state,
                                 vmin=vmin,
                                 vmax=vmax,
                                 haveColorBar=True)
            else:
                average_obs_per_sub_state = (np.mean(obs[lst_sub_states[i], :],
                                                     axis=0))
                plot_area_values(fig,
                                 ax[i],
                                 allenMap,
                                 allenIndices,
                                 average_obs_per_sub_state,
                                 vmin=vmin,
                                 vmax=vmax,
                                 haveColorBar=False)

        ax[i].set_title('state {}, substate {} '.format(
            state_number, sub_state_number_list[i]))

    if save_figures:
        new = name + ' plots results {} {} {}'.format(trial_type, time_bins,
                                                      trial_num_states)
        subdirectory(file, new)
        plt.savefig(new +
                    '/brainmap_sub_{}_{}_{}_{}_{}_{}_{}_run{}.png'.format(
                        name, trial_type, time_bins, title, kind_prepro,
                        get_date(), state_number, run))

    plt.show()
コード例 #14
0
ファイル: map_to_brain.py プロジェクト: vala1958/network
def plot_brain_states_2_nan(trial_num_states, lst_average_obs_per_state, vmin,
                            vmax, time_bins, name, trial_type, save_figures,
                            file, title, kind_prepro, run):
    """
    function with a double loop for more then 8 states , saved as one image, including  the possibility of empty states
     """

    allenMap, allenIndices = load_matrix('')
    state_number_list = [x for x in range(0, trial_num_states)]

    figsize_2 = get_figsize_2(lst_average_obs_per_state)
    number_of_rows = int(np.ceil(len(lst_average_obs_per_state) / 8))

    fig, ax = plt.subplots(number_of_rows,
                           8,
                           figsize=figsize_2,
                           tight_layout=False)
    ax = ax.ravel()

    for i in range(len(lst_average_obs_per_state)):

        sum_mean_all_roi = sum(lst_average_obs_per_state[i])

        if sum_mean_all_roi == 0:
            print('state {} does not exist!'.format(i))
        else:

            if len(lst_average_obs_per_state) < 8:
                if i == len(lst_average_obs_per_state) - 1:
                    plot_area_values(fig,
                                     ax[i],
                                     allenMap,
                                     allenIndices,
                                     lst_average_obs_per_state[i],
                                     vmin=vmin,
                                     vmax=vmax,
                                     haveColorBar=True)

                else:
                    plot_area_values(fig,
                                     ax[i],
                                     allenMap,
                                     allenIndices,
                                     lst_average_obs_per_state[i],
                                     vmin=vmin,
                                     vmax=vmax,
                                     haveColorBar=False)

            else:
                if i == 7 or i == 15 or i == 23:
                    plot_area_values(fig,
                                     ax[i],
                                     allenMap,
                                     allenIndices,
                                     lst_average_obs_per_state[i],
                                     vmin=vmin,
                                     vmax=vmax,
                                     haveColorBar=True)
                else:
                    plot_area_values(fig,
                                     ax[i],
                                     allenMap,
                                     allenIndices,
                                     lst_average_obs_per_state[i],
                                     vmin=vmin,
                                     vmax=vmax,
                                     haveColorBar=False)

        ax[i].set_title('state {} '.format(state_number_list[i]))
        ax[i].set_yticks([])
        ax[i].set_xticks([])

    if save_figures:
        new = name + ' plots results {} {} {}'.format(trial_type, time_bins,
                                                      trial_num_states)
        subdirectory(file, new)
        plt.savefig(new + '/brainmap_2_{}_{}_{}_{}_{}_{}_run{}.png'.format(
            name, trial_type, time_bins, title, get_date(), kind_prepro, run))

    plt.show()
コード例 #15
0
ファイル: map_to_brain.py プロジェクト: vala1958/network
def plot_brain_states_1_nan(trial_num_states, lst_average_obs_per_state, vmin,
                            vmax, time_bins, name, trial_type, save_figures,
                            file, title, kind_prepro, run):
    """
    function to plot the brain states in one row and stored as one image, including the option of empty states
    """

    allenMap, allenIndices = load_matrix('')
    state_number_list = [x for x in range(0, trial_num_states)]

    if trial_num_states > 8:
        print(
            'number of states is to large (squeezed plot), please use other plot function'
        )
    else:
        figsize = get_figsize(lst_average_obs_per_state)

        fig, ax = plt.subplots(1,
                               trial_num_states,
                               figsize=figsize,
                               tight_layout=False)

        for i in range(len(lst_average_obs_per_state)):

            sum_mean_all_roi = sum(lst_average_obs_per_state[i])

            if sum_mean_all_roi == 0:
                print('state {} does not exist!'.format(i))

            else:

                if i == trial_num_states - 1:
                    plot_area_values(fig,
                                     ax[i],
                                     allenMap,
                                     allenIndices,
                                     lst_average_obs_per_state[i],
                                     vmin=vmin,
                                     vmax=vmax,
                                     haveColorBar=True)
                else:
                    plot_area_values(fig,
                                     ax[i],
                                     allenMap,
                                     allenIndices,
                                     lst_average_obs_per_state[i],
                                     vmin=vmin,
                                     vmax=vmax,
                                     haveColorBar=False)

            ax[i].set_title('state {}  '.format(state_number_list[i]))
            ax[i].set_yticks([])
            ax[i].set_xticks([])

        if save_figures:
            new = name + ' plots results {} {} {}'.format(
                trial_type, time_bins, trial_num_states)
            subdirectory(file, new)
            plt.savefig(new + '/brainmap_1_{}_{}_{}_{}_{}_{}_run{}.png'.format(
                name, trial_type, time_bins, title, get_date(), kind_prepro,
                run))

        plt.show()
コード例 #16
0
ファイル: plots.py プロジェクト: vala1958/network
def plot_data(obs, name, trial_type, frames, delay, trial_index, save_figure,
              file, title, run, kind_data, kind_prepro):
    """
    plots the data (2d array, dF/F) before proprocessing
    :param obs:           2d array
    :param name:          string, mouse (M5, M6 etc)
    :param trial_type     string, trial type (Hit, FA etc)
    :param frames         integer number of time steps
    :param title          string, which data (averaged, single trial, conc)
    :param run            integer, run number to avoid overwriting
    :param save_figure    boolsche variable (True if input = 'yes', False otherwise)
    :param trial_index    integer, index of single trial
    :param file           current working directory (function: get_file_and_label )
    :param title          string, which type of data: average, single or concatenated
    :param kind_data      integer, 0,1,2 depending on type of data (0=average, 1=single, 2=conc)
    :param kind_prepro    string, 'nil' for no preprocessing, 'FA' for FA, 'PCA' for PCA
    :return:              figure
    """

    lim = 1.05 * abs(obs).max()

    fig = plt.figure(figsize=(10, 10))
    for d in range(obs.shape[1]):
        plt.plot(obs[:, d] + lim * d, '-k')

    plt.xlim(0, obs.shape[0])
    plt.xlabel("time steps")

    plt.yticks(lim * np.arange(obs.shape[1]),
               ['x{}'.format(d + 1) for d in range(obs.shape[1])])

    if kind_data == 0:
        if kind_prepro == 'nil':
            plt.ylabel('observations')
            plt.title('{}, {} trials, {} time steps, {}s delay, {}, '.format(
                name, trial_type, frames, delay, title))

            if save_figure:
                # create new subdirectory name, function subdirectory creates that subdirectory if it does not already
                # exist
                new = name + ' plots data'
                subdirectory(file, new)
                plt.savefig(
                    new + '/{}_{}_{}_{}_run{}.png'.format(name,
                                                          trial_type,
                                                          title,
                                                          get_date(),
                                                          run,
                                                          bbox_inches="tight"))
        elif kind_prepro == 'FA':
            plt.ylabel('factors')
            plt.title('{}, {} trials, {} time steps, {}s delay, {}, FA'.format(
                name, trial_type, frames, delay, title))

            if save_figure:
                # create new subdirectory name, function subdirectory creates that subdirectory if it does not already
                # exist
                new = name + ' plots preprocessed'
                subdirectory(file, new)
                plt.savefig(
                    new +
                    '/{}_{}_{}_{}_FA_run{}.png'.format(name,
                                                       trial_type,
                                                       title,
                                                       get_date(),
                                                       run,
                                                       bbox_inches="tight"))
        elif kind_prepro == 'PCA':
            plt.ylabel('components')
            plt.title(
                '{}, {} trials, {} time steps, {}s delay, {}, PCA'.format(
                    name, trial_type, frames, delay, title))

            if save_figure:
                # create new subdirectory name, function subdirectory creates that subdirectory if it does not already
                # exist
                new = name + ' plots preprocessed'
                subdirectory(file, new)
                plt.savefig(
                    new +
                    '/{}_{}_{}_{}_PCA_run{}.png'.format(name,
                                                        trial_type,
                                                        title,
                                                        get_date(),
                                                        run,
                                                        bbox_inches="tight"))

    elif kind_data == 1:
        title_plot = 'single trial, number: {}'.format(trial_index)
        if kind_prepro == 'nil':
            plt.ylabel('observations')
            plt.title('{}, {} trials, {} time steps, {}s delay, {}'.format(
                name, trial_type, frames, delay, title_plot))

            if save_figure:
                # create new subdirectory name, function subdirectory creates that subdirectory if it does not already
                # exist
                new = name + ' plots data'
                subdirectory(file, new)
                plt.savefig(
                    new + '/{}_{}_{}_{}_run{}.png'.format(name,
                                                          trial_type,
                                                          title,
                                                          get_date(),
                                                          run,
                                                          bbox_inches="tight"))
        elif kind_prepro == 'FA':
            plt.ylabel('factors')
            plt.title('{}, {} trials, {} time steps, {}s delay, {}, FA'.format(
                name, trial_type, frames, delay, title_plot))

            if save_figure:
                # create new subdirectory name, function subdirectory creates that subdirectory if it does not already
                # exist
                new = name + ' plots preprocessed'
                subdirectory(file, new)
                plt.savefig(
                    new +
                    '/{}_{}_{}_{}_FA_run{}.png'.format(name,
                                                       trial_type,
                                                       title,
                                                       get_date(),
                                                       run,
                                                       bbox_inches="tight"))
        elif kind_prepro == 'PCA':
            plt.ylabel('components')
            plt.title(
                '{}, {} trials, {} time steps, {}s delay, {}, PCA'.format(
                    name, trial_type, frames, delay, title_plot))

            if save_figure:
                # create new subdirectory name, function subdirectory creates that subdirectory if it does not already
                # exist
                new = name + ' plots preprocessed'
                subdirectory(file, new)
                plt.savefig(
                    new +
                    '/{}_{}_{}_{}_PCA_run{}.png'.format(name,
                                                        trial_type,
                                                        title,
                                                        get_date(),
                                                        run,
                                                        bbox_inches="tight"))

    elif kind_data == 2:
        if kind_prepro == 'nil':
            plt.ylabel('observations')
            plt.title('{}, {} trials, {} time steps, {}s delay, {}'.format(
                name, trial_type, frames, delay, title))

            if save_figure:
                # create new subdirectory name, function subdirectory creates that subdirectory if it does not already
                # exist
                new = name + ' plots data'
                subdirectory(file, new)
                plt.savefig(
                    new + '/{}_{}_{}_{}_run{}.png'.format(name,
                                                          trial_type,
                                                          title,
                                                          get_date(),
                                                          run,
                                                          bbox_inches="tight"))
        elif kind_prepro == 'FA':
            plt.ylabel('factors')
            plt.title('{}, {} trials, {} time steps, {}s delay, {}, FA'.format(
                name, trial_type, frames, delay, title))

            if save_figure:
                # create new subdirectory name, function subdirectory creates that subdirectory if it does not already
                # exist
                new = name + ' plots preprocessed'
                subdirectory(file, new)
                plt.savefig(
                    new +
                    '/{}_{}_{}_{}_FA_run{}.png'.format(name,
                                                       trial_type,
                                                       title,
                                                       get_date(),
                                                       run,
                                                       bbox_inches="tight"))
        elif kind_prepro == 'PCA':
            plt.ylabel('components')
            plt.title(
                '{}, {} trials, {} time steps, {}s delay, {}, PCA'.format(
                    name, trial_type, frames, delay, title))

            if save_figure:
                # create new subdirectory name, function subdirectory creates that subdirectory if it does not already
                # exist
                new = name + ' plots preprocessed'
                subdirectory(file, new)
                plt.savefig(
                    new +
                    '/{}_{}_{}_{}_PCA_run{}.png'.format(name,
                                                        trial_type,
                                                        title,
                                                        get_date(),
                                                        run,
                                                        bbox_inches="tight"))
    return fig
コード例 #17
0
ファイル: fit_and_viterbi.py プロジェクト: vala1958/network
def plot_posterior_prob(hmm, obs_vit, time_bins, trial_num_states, name,
                        trial_type, delay, title, kind_prepro, type_fit,
                        save_figures, file, run):
    """
    function that calculates the posterior probability (alpha matrix), informing about the probability of a specific
    state at a given time step having seen the observations up to that that time step. This probabilities for each
    hidden state is plotted.
    :param hmm:                    fitted model
    :param obs_vit                 the observation data used for the viterbi algorithm
    :param time_bins:              number of frames for the viterbi algo
    :param save_figures:           true or false, depending if the figures have to be safed
    :param name:                   mouse name (eg. M5)
    :param trial_type:             trial typ (eg. Hit)
    :param trial_num_states:       number of hidden states (the model is fitted to)
    :param file:                   the current working directory
    :param title:                  typ of data (averaged or single, for viterbi)
    :param run:                    integer: to prevent overwriting of runs with identical params
    :return:                       fig
    """

    # posterior state probability, alpha matrix in forward pass
    posterior_probs = hmm.expected_states(obs_vit)[0]
    print(
        'shape of the array holding the posterior probabilities of each hidden state\nand time step: {}'
        .format(np.shape(posterior_probs)))

    trial_lst_states = [i + 1 for i in range(0, trial_num_states)]
    # color function zur defintion der colors für  trial und true number of states
    colors_trial = color_function(trial_lst_states)

    xticks_lst = np.arange(0, time_bins + 1, step=10)

    fig = plt.figure(figsize=(12, 3.5), dpi=80, facecolor='w', edgecolor='k')
    plt.style.use('seaborn')
    for k in range(trial_num_states):
        plt.plot(posterior_probs[:, k],
                 label="State " + str(k + 1),
                 lw=2,
                 color=colors_trial[k])

    plt.ylim((-0.01, 1.01))
    plt.xticks(xticks_lst)
    plt.yticks([0, 0.5, 1], fontsize=10)
    plt.xlabel("frames")
    plt.ylabel("p(state)")

    plt.title('posterior Probability: {}, {}, {}, {}, {}s ,{}, {}, {}'.format(
        name, trial_type, time_bins, trial_num_states, delay, title,
        kind_prepro, type_fit))

    if save_figures:
        # create new subdirectory name, function subdirectory creates that subdirectory if it does not already
        # exist
        new = name + ' plots results {} {} {}'.format(trial_type, time_bins,
                                                      trial_num_states)
        subdirectory(file, new)
        plt.savefig(
            new +
            '/post_prob_{}_{}_{}_{}_{}_run{}.png'.format(name,
                                                         trial_type,
                                                         title,
                                                         kind_prepro,
                                                         get_date(),
                                                         run,
                                                         bbox_inches="tight"))

    return posterior_probs, fig
コード例 #18
0
ファイル: fit_and_viterbi.py プロジェクト: vala1958/network
def plot_occupation_duration(trial_num_states, vit_durs_stacked, name,
                             trial_type, time_bins, delay, title, kind_prepro,
                             type_fit, lst_viterbi_total_occupancy_time,
                             save_figures, file, run):
    """

    :return:
    """
    trial_lst_states = [i + 1 for i in range(0, trial_num_states)]
    # color function zur defintion der colors für  trial und true number of states
    colors_trial = color_function(trial_lst_states)

    # x Achsen defintion
    x_trial = []
    for i in range(len(trial_lst_states)):
        x_trial.append('state {}'.format(i))

    # trial states viterbi (not permuted)
    fig, ax = plt.subplots(1, 2, figsize=(14, 4))
    # plt.style.use('seaborn')
    ax[0].hist(vit_durs_stacked,
               label=['state ' + str(s) for s in range(trial_num_states)],
               color=colors_trial,
               alpha=1)
    ax[0].set_xlabel('Duration')
    ax[0].set_ylabel('Frequency')
    ax[0].legend()
    ax[0].set_title(
        'Histogram State Durations: {}, {}, {}, {}, {}s, {}, {}\n {}  '.format(
            name, trial_type, time_bins, trial_num_states, delay, title,
            kind_prepro, type_fit))

    rect1 = ax[1].bar(x_trial,
                      lst_viterbi_total_occupancy_time,
                      width=0.4,
                      label=x_trial,
                      color=colors_trial,
                      alpha=1)
    ax[1].set_ylabel('total occupancy time')
    ax[1].set_title(
        'occupancy time: {}, {}, {}, {}, {}s, {}, {}\n {}  '.format(
            name, trial_type, time_bins, trial_num_states, delay, title,
            kind_prepro, type_fit))
    autolabel(rect1, ax)

    if save_figures:
        # create new subdirectory name, function subdirectory creates that subdirectory if it does not already
        # exist
        new = name + ' plots results {} {} {}'.format(trial_type, time_bins,
                                                      trial_num_states)
        subdirectory(file, new)
        plt.savefig(
            new +
            '/occupancy {}_{}_{}_{}_{}_run{}.png'.format(name,
                                                         trial_type,
                                                         title,
                                                         kind_prepro,
                                                         get_date(),
                                                         run,
                                                         bbox_inches="tight"))

    # the lengend for later use, new: there is an extra function "generate_legend" in map_to_brain
    #export_legend(legend)

    return fig
コード例 #19
0
ファイル: fit_and_viterbi.py プロジェクト: vala1958/network
def plot_obs_with_state_sequence(trial_num_states, obs, viterbi_states,
                                 time_bins, name, trial_type, delay, title,
                                 save_figures, file, kind, run):
    """
    plot the obs before FA, underlayed with viterbi sequence color plot
    :return:   fig
    """

    trial_lst_states = [i + 1 for i in range(0, trial_num_states)]
    cmap_trial = gradient_cmap(color_function(trial_lst_states))

    lim = 1.05 * abs(obs).max()

    if kind == 0:
        fig = plt.figure(figsize=(10, 10))
    else:
        fig = plt.figure(figsize=(10, 5))

    for d in range(obs.shape[1]):
        plt.plot(obs[:, d] + lim * d, '-k')

    plt.imshow(viterbi_states[None, :],
               aspect="auto",
               cmap=cmap_trial,
               vmin=0,
               vmax=len(color_function(trial_lst_states)) - 1,
               extent=(0, time_bins, -lim, obs.shape[1] * lim),
               alpha=0.2)

    plt.xlim(0, obs.shape[0])
    plt.xlabel("frames")
    plt.yticks(lim * np.arange(obs.shape[1]),
               ['x{}'.format(d + 1) for d in range(obs.shape[1])])

    if kind == 0:
        plt.ylabel('observations before preprocessing')
        plt.title('observations {}, {}, {}, {}, {}s, {}'.format(
            name, trial_type, time_bins, trial_num_states, delay, title))
        if save_figures:
            # create new subdirectory name, function subdirectory creates that subdirectory if it does not already
            # exist
            new = name + ' plots results {} {} {}'.format(
                trial_type, time_bins, trial_num_states)
            subdirectory(file, new)
            plt.savefig(new + '/observations {}_{}_{}_{}_run{}.png'.format(
                name, trial_type, title, get_date(), run, bbox_inches="tight"))

    elif kind == 1:
        plt.ylabel('factors')
        plt.title('factors {}, {}, {}, {}, {}s, {}'.format(
            name, trial_type, time_bins, trial_num_states, delay, title))
        if save_figures:
            # create new subdirectory name, function subdirectory creates that subdirectory if it does not already
            # exist
            new = name + ' plots results {} {} {}'.format(
                trial_type, time_bins, trial_num_states)
            subdirectory(file, new)
            plt.savefig(new + '/factors {}_{}_{}_{}_run{}.png'.format(
                name, trial_type, title, get_date(), run, bbox_inches="tight"))
    elif kind == 2:
        plt.ylabel('components')
        plt.title('components {}, {}, {}, {}, {}s, {}'.format(
            name, trial_type, time_bins, trial_num_states, delay, title))
        if save_figures:
            # create new subdirectory name, function subdirectory creates that subdirectory if it does not already
            # exist
            new = name + ' plots results {} {} {}'.format(
                trial_type, time_bins, trial_num_states)
            subdirectory(file, new)
            plt.savefig(new + '/components {}_{}_{}_{}_run{}.png'.format(
                name, trial_type, title, get_date(), run, bbox_inches="tight"))

    return fig