Example #1
0
def plot_mat(syst, ax):
    syst, mat, sol = analyze_system(
        syst, use_ode_sde_diff=True,
        repetition_num=5, tmax=10)
    plot_corr_mat(mat, ax)

    ax.set_xticks([], [])
    ax.set_yticks([], [])
Example #2
0
def plot_result(motifs, data, fname_app='', sub_num=10):
    """ Create result plot
    """
    mpl.style.use('default')
    sub_num = min(sub_num, len(motifs))
    print(' > Plotting results ({})'.format(fname_app[1:]))

    # overview plots
    plt.figure(figsize=(30, 4 * sub_num))
    gs = mpl.gridspec.GridSpec(sub_num, 3, width_ratios=[1, 2, 1])

    idx = map(int,
        np.linspace(0, len(motifs), num=sub_num, endpoint=False))

    corrs = []
    for ai, i in tqdm(enumerate(idx), total=sub_num):
        c1, c2, c3 = motifs[i]

        # plot all possible correlations and select optimal one
        sel, all_corrs = plot_all_correlations(
            (c1, c2, c3), data,
            plt.subplot(gs[ai, 2]) if ai < sub_num else None)

        # get intensities
        sols = []
        for foo in [c1, c2, c3]:
            sols.append(data[foo]['intensities'][sel[foo]])

        # compute correlation matrix
        corr_mat = get_correlation_matrix(sols)
        corrs.extend(all_corrs)

        # plot rest
        plotter.plot_corr_mat(corr_mat, plt.subplot(gs[ai, 0]))

        series_ax = plt.subplot(gs[ai, 1])
        plotter.plot_system_evolution(
            sols, series_ax,
            xlabel='sample')
        series_ax.set_title('{}\n{}\n{}'.format(c1, c2, c3))

    plt.tight_layout()
    plotter.save_figure('images/rl_motifs{}.pdf'.format(fname_app), bbox_inches='tight')

    # correlation histogram
    plt.figure()

    plotter.plot_histogram(corrs, plt.gca())

    plt.tight_layout()
    plotter.save_figure('images/rl_corr_hist{}.pdf'.format(fname_app), bbox_inches='tight')
Example #3
0
def plot_system_overview(data, sample_size=20):
    """ Plot systems vs correlations
    """
    # extract sample
    dsample = [data[i]
        for i in sorted(random.sample(range(len(data)), min(len(data), sample_size)))]

    # plot sample
    fig = plt.figure(figsize=(13, 4*len(dsample)))
    gs = gridspec.GridSpec(len(dsample), 3, width_ratios=[1, 1, 2])

    for i, (system, corr_mat, solution) in enumerate(dsample):
        plot_system(system, plt.subplot(gs[i, 0]))
        plot_corr_mat(corr_mat, plt.subplot(gs[i, 1]))
        plot_system_evolution(solution, plt.subplot(gs[i, 2]))

    plt.tight_layout()
    save_figure('images/overview.pdf', bbox_inches='tight', dpi=300)
    plt.close()
Example #4
0
def main(fname, skip_filtered=True):
    """ Main interface
    """
    if os.path.isfile(fname):
        systems = load_systems(fname)
        if systems.ndim == 0:
            systems = [np.asscalar(systems)]
        print('Integrating %d systems' % len(systems))

        core_num = int(multiprocessing.cpu_count() * 4 / 5)
        print('Using %d cores' % core_num)

        data = []
        with tqdm(total=len(systems)) as pbar:
            with multiprocessing.Pool(core_num) as p:
                for res in p.imap(analyze_system, systems, chunksize=10):
                    if not skip_filtered or not res[1] is None:
                        data.append(res)
                    pbar.update()
        print('Found result for %d systems' % len(data))

        if not skip_filtered:
            data = cluster_data(data)
        cache_data(data)
    else:
        syst = system_from_string(fname)
        syst, mat, sol = analyze_system(syst, use_ode_sde_diff=False)
        if mat is None:
            print('No sensible steady-state found')
        else:
            print(mat)

        fig = plt.figure(figsize=(20, 6))
        gs = gridspec.GridSpec(1, 3, width_ratios=[1, 1, 2])
        plt.style.use('seaborn-poster')

        plot_system(syst, plt.subplot(gs[0, 0]))
        plot_corr_mat(mat, plt.subplot(gs[0, 1]))
        plot_system_evolution(sol, plt.subplot(gs[0, 2]))

        plt.tight_layout()
        save_figure('images/single_case.pdf', bbox_inches='tight', dpi=300)
        plt.close()
Example #5
0
def simulate_graph(graph):
    """ Generate dynamics on graph
    """
    # create system
    J = np.copy(nx.to_numpy_matrix(graph))
    np.fill_diagonal(J, -1)
    D = np.zeros((J.shape[0],))
    E = np.zeros((J.shape[0],))
    I = np.ones((J.shape[0],))

    # add input to nodes of zero in-degree
    zero_indgr = []
    for i, row in enumerate(J.T):
        inp = np.sum(row)
        inp += 1 # compensate for self-inhibition
        if inp == 0: zero_indgr.append(i)

    D[zero_indgr] = 1
    E[zero_indgr] = 1
    print('>', '{}/{} nodes with zero indegree'.format(len(zero_indgr), len(graph.nodes())))

    # simulate system
    syst = SDESystem(J, D, E, I)
    syst, mat, sol = analyze_system(syst, filter_trivial_ss=False)

    # plot results
    fig = plt.figure(figsize=(30, 15))
    gs = mpl.gridspec.GridSpec(1, 2, width_ratios=[1, 2])

    if not mat is None:
        # only keep non-zero indegree node correlations
        mat = extract_sub_matrix(mat, zero_indgr)

        node_inds = list_diff(range(J.shape[0]), zero_indgr)
        used_nodes = np.array(graph.nodes())[node_inds]

        plot_corr_mat(
            mat, plt.subplot(gs[0]),
            show_values=False, labels=used_nodes)
    plot_system_evolution(sol, plt.subplot(gs[1]), show_legend=False)

    save_figure('images/peak_network_simulation.pdf', bbox_inches='tight', dpi=300)
Example #6
0
def plot_system_overview(data, sample_size=20):
    """ Plot systems vs correlations
    """
    # extract sample
    dsample = [
        data[i] for i in sorted(
            random.sample(range(len(data)), min(len(data), sample_size)))
    ]

    # plot sample
    fig = plt.figure(figsize=(13, 4 * len(dsample)))
    gs = gridspec.GridSpec(len(dsample), 3, width_ratios=[1, 1, 2])

    for i, (system, corr_mat, solution) in enumerate(dsample):
        plot_system(system, plt.subplot(gs[i, 0]))
        plot_corr_mat(corr_mat, plt.subplot(gs[i, 1]))
        plot_system_evolution(solution, plt.subplot(gs[i, 2]))

    plt.tight_layout()
    save_figure('images/overview.pdf', bbox_inches='tight', dpi=300)
    plt.close()
Example #7
0
def plot_individuals(examples, fname, val_func=None):
    """ Plot a selection of individual results
    """
    if val_func is None:
        mod = -1
    else:
        mod = 0

    # plot selected networks
    if len(examples[0]) == 2:  # is pair of networks
        fig = plt.figure(figsize=(50, 4 * len(examples)))
        gs = mpl.gridspec.GridSpec(
            len(examples),
            6 + mod,
            width_ratios=[1, 2, 1, 2, 1 + (-3 * mod), 4])
    else:  # each entry is single network
        fig = plt.figure(figsize=(25, 4 * len(examples)))
        gs = mpl.gridspec.GridSpec(len(examples), 3, width_ratios=[1, 1, 2])

    counter = 0
    for i, net in enumerate(examples):
        if len(net) == 2:  # pair of networks
            raw_p, enh_p = net

            # -.- ...
            if len(raw_p) == 2:
                _, raw = raw_p
                _, enh = enh_p
            else:
                raw = raw_p
                enh = enh_p

            plot_system(raw[0], plt.subplot(gs[i, 0]))
            plot_corr_mat(raw[1], plt.subplot(gs[i, 1]))
            plot_system(enh[0], plt.subplot(gs[i, 2]))
            plot_corr_mat(enh[1], plt.subplot(gs[i, 3]))
            plot_system_evolution(enh[2], plt.subplot(gs[i, 5 + mod]))

            # plot marker
            mark_ax = plt.subplot(gs[i, 4])
            if not val_func is None:
                mark_ax.imshow([[handle_enh_entry(raw_p, enh_p, val_func)]],
                               cmap=get_matrix_cmap(),
                               vmin=0,
                               vmax=3)
                mark_ax.axis('off')
            else:
                print('Tried to use `val_func`, but it\'s None')
        else:  # single network
            if net[1] is None:
                counter += 1
                plot_system(net[0], plt.subplot(gs[i, 0]))
                plot_system_evolution(net[2], plt.subplot(gs[i, 2]))
                continue

            plot_system(net[0], plt.subplot(gs[i, 0]))
            plot_corr_mat(net[1], plt.subplot(gs[i, 1]))
            plot_system_evolution(net[2], plt.subplot(gs[i, 2]))

    if counter > 0:
        #print('{} broken results'.format(counter))
        pass

    plt.tight_layout()
    save_figure('%s_zoom.pdf' % fname.replace('.pdf', ''),
                bbox_inches='tight',
                dpi=300)
    plt.close()
Example #8
0
def plot_individuals(examples, fname, val_func=None):
    """ Plot a selection of individual results
    """
    if val_func is None:
        mod = -1
    else:
        mod = 0

    # plot selected networks
    if len(examples[0]) == 2: # is pair of networks
        fig = plt.figure(figsize=(50, 4*len(examples)))
        gs = mpl.gridspec.GridSpec(
            len(examples), 6+mod,
            width_ratios=[1, 2, 1, 2, 1+(-3*mod), 4])
    else: # each entry is single network
        fig = plt.figure(figsize=(25, 4*len(examples)))
        gs = mpl.gridspec.GridSpec(len(examples), 3, width_ratios=[1, 1, 2])

    counter = 0
    for i, net in enumerate(examples):
        if len(net) == 2: # pair of networks
            raw_p, enh_p = net

            # -.- ...
            if len(raw_p) == 2:
                _, raw = raw_p
                _, enh = enh_p
            else:
                raw = raw_p
                enh = enh_p

            plot_system(raw[0], plt.subplot(gs[i, 0]))
            plot_corr_mat(raw[1], plt.subplot(gs[i, 1]))
            plot_system(enh[0], plt.subplot(gs[i, 2]))
            plot_corr_mat(enh[1], plt.subplot(gs[i, 3]))
            plot_system_evolution(enh[2], plt.subplot(gs[i, 5+mod]))

            # plot marker
            mark_ax = plt.subplot(gs[i, 4])
            if not val_func is None:
                mark_ax.imshow(
                    [[handle_enh_entry(raw_p, enh_p, val_func)]],
                    cmap=get_matrix_cmap(), vmin=0, vmax=3)
                mark_ax.axis('off')
            else:
                print('Tried to use `val_func`, but it\'s None')
        else: # single network
            if net[1] is None:
                counter += 1
                plot_system(net[0], plt.subplot(gs[i, 0]))
                plot_system_evolution(net[2], plt.subplot(gs[i, 2]))
                continue

            plot_system(net[0], plt.subplot(gs[i, 0]))
            plot_corr_mat(net[1], plt.subplot(gs[i, 1]))
            plot_system_evolution(net[2], plt.subplot(gs[i, 2]))

    if counter > 0:
        #print('{} broken results'.format(counter))
        pass

    plt.tight_layout()
    save_figure('%s_zoom.pdf' % fname.replace('.pdf', ''), bbox_inches='tight', dpi=300)
    plt.close()