Beispiel #1
0
def plot_commSizeDist(nc, ncName):
    """Plot community size distribution."""
    bins = binner.Bins(int, 1, nc.getGiantSize(), 'linlog', 1.5)
    binned_data = bins.bin_count_divide(nc.getCommunitySizes())

    # Set plotting properties
    l, b, r, t = 0.1, 0.1, 0.98, (0.93 if print_titles else 0.98)
    axes_rect = [l, b, r - l, t - b]  #[left, bottom, width, height]
    pylab.rcParams.update(fig_utils.get_rcParams(float(fig_width_cm)),
                          fig_ratio=0.8,
                          font_sizes=font_sizes)

    fig = pylab.figure()
    ax = fig.add_axes(axes_rect)
    ax.loglog(bins.centers, binned_data, 'b.-')

    ax.set_xlabel(r"Community size")
    ax.set_ylabel(r"Number of communities")
    if print_titles:
        ax.set_title(r"Community size distribution (binned)")

    # Adjust axis
    V = ax.axis()
    ax.axis((1, bins.bin_limits[-1], V[2], V[3]))

    # Save figure.
    fig_utils.savefig(fig, ncName + "_sizeDist", save_formats)
Beispiel #2
0
def plot_commSizeDist(nc, ncName):
    """Plot community size distribution."""
    bins = binner.Bins(int, 1, nc.getGiantSize(), 'linlog', 1.5)
    binned_data = bins.bin_count_divide(nc.getCommunitySizes())

    # Set plotting properties
    l, b, r, t = 0.1, 0.1, 0.98, (0.93 if print_titles else 0.98)
    axes_rect = [l,b,r-l,t-b] #[left, bottom, width, height]
    pylab.rcParams.update(fig_utils.get_rcParams(float(fig_width_cm)),
                          fig_ratio=0.8,
                          font_sizes=font_sizes)
    
    fig = pylab.figure()
    ax = fig.add_axes(axes_rect)
    ax.loglog(bins.centers, binned_data, 'b.-')

    ax.set_xlabel(r"Community size")
    ax.set_ylabel(r"Number of communities")
    if print_titles:
        ax.set_title(r"Community size distribution (binned)")

    # Adjust axis
    V = ax.axis()
    ax.axis((1, bins.bin_limits[-1], V[2], V[3]))

    # Save figure.
    fig_utils.savefig(fig, ncName + "_sizeDist", save_formats)
Beispiel #3
0
def create_motif_figure(N_h, N_v):
    """Create a figure for plotting motifs.

    `event_types` is needed to construct colors used in the labels of
    events.
    """
    # Physical size of the output.
    #t_cm_scatter, l_cm_scatter = 1.0, 1.2 # scatter left margin in cm
    #h_cm_scatter, w_cm_scatter = 8.0, 8.0 # scatter plot width in cm
    w_cm, h_cm = 2.05, 1.85 # width and height of motif in cm
    t_cm, b_cm = 0.3, 0.6   # top and bottom margin in cm
    l_cm, r_cm = 0.30, 0.30 # left and right margin in cm
    ws_cm, hs_cm = 0.16, 0.36 # wspace and hspace in cm

    # Derived physical sizes of the figure.
    w_cm = l_cm+r_cm+N_h*w_cm+(N_h-1)*ws_cm
    h_cm = t_cm+b_cm+N_v*h_cm+(N_v-1)*hs_cm 

    # Set plotting properties
    font_sizes = {'text': 14, 'title':14}
    params = fig_utils.get_rcParams(w_cm, h_cm/w_cm, font_sizes)
    pylab.rcParams.update(params)
    subplot_specs = {'left':l_cm/w_cm, 'right':1-r_cm/w_cm,
                     'top':1-t_cm/h_cm, 'bottom':b_cm/h_cm,
                     'wspace':ws_cm/w_cm, 'hspace':hs_cm/h_cm}
    #motif_colors = {2: "#990033", 3: "#0000ff", 4:"#C850BE", 5:"#64C55F"}
    
    fig = pylab.figure()
    fig.subplots_adjust(**subplot_specs)
    i_ax = fig_utils.SubplotHelper(N_v,N_h)
    return fig, i_ax
Beispiel #4
0
def plot_avgCommWeight(nc, ncName, net):
    """Plot average weight of communities."""

    def w_gen(comm, net):
        w_avg_total = np.mean(list(net.weights))
        for c in comm:
            subnet = transforms.getSubnet(net,c)
            if len(subnet):
                w = np.sum(list(subnet.weights))/(len(subnet)*w_avg_total)
                yield len(subnet), w

    bins = binner.Bins(int, 1, nc.getGiantSize(), 'linlog', 1.5)
    perc = (0.1, 0.25, 0.5, 0.75, 0.9)
    binned_data = bins.bin_percentile(w_gen(nc, net), perc)

    # Set plotting properties
    l, b, r, t = 0.1, 0.1, 0.98, (0.93 if print_titles else 0.98)
    axes_rect = [l,b,r-l,t-b] #[left, bottom, width, height]
    pylab.rcParams.update(fig_utils.get_rcParams(float(fig_width_cm)),
                          fig_ratio=0.8,
                          font_sizes=font_sizes)
    
    fig = pylab.figure()
    ax = fig.add_axes(axes_rect)

    plot_styles = ('b:', 'b--', 'r-', 'b--', 'b:')
    labels = (r"%d$^{\mathrm{th}}$ percentile" % int(100*perc[0]),
              r"%d$^{\mathrm{th}}$ percentile" % int(100*perc[1]),
              r"Median",
              r"%d$^{\mathrm{th}}$ percentile" % int(100*perc[3]),
              r"%d$^{\mathrm{th}}$ percentile" % int(100*perc[4]))
    for p, data, sty, lbl in zip(perc, binned_data, plot_styles, labels):
        ax.loglog(bins.centers, data, sty, label=lbl)

    ax.set_xlabel(r"Community size")
    ax.set_ylabel(r"$\langle w_{\mathrm{comm}} \rangle /"
                  r"\langle w_{\mathrm{total}} \rangle$")
    if print_titles:
        ax.set_title(r"Ratio of avg community weigth to avg total weight (binned)")

    ax.legend(loc='best')

    # Adjust axis
    V = ax.axis()
    ax.axis((1, bins.bin_limits[-1], V[2], V[3]))

    # Save figure.
    fig_utils.savefig(fig, ncName + "_weightDist", save_formats)
Beispiel #5
0
def plot_avgCommWeight(nc, ncName, net):
    """Plot average weight of communities."""
    def w_gen(comm, net):
        w_avg_total = np.mean(list(net.weights))
        for c in comm:
            subnet = transforms.getSubnet(net, c)
            if len(subnet):
                w = np.sum(list(subnet.weights)) / (len(subnet) * w_avg_total)
                yield len(subnet), w

    bins = binner.Bins(int, 1, nc.getGiantSize(), 'linlog', 1.5)
    perc = (0.1, 0.25, 0.5, 0.75, 0.9)
    binned_data = bins.bin_percentile(w_gen(nc, net), perc)

    # Set plotting properties
    l, b, r, t = 0.1, 0.1, 0.98, (0.93 if print_titles else 0.98)
    axes_rect = [l, b, r - l, t - b]  #[left, bottom, width, height]
    pylab.rcParams.update(fig_utils.get_rcParams(float(fig_width_cm)),
                          fig_ratio=0.8,
                          font_sizes=font_sizes)

    fig = pylab.figure()
    ax = fig.add_axes(axes_rect)

    plot_styles = ('b:', 'b--', 'r-', 'b--', 'b:')
    labels = (r"%d$^{\mathrm{th}}$ percentile" % int(100 * perc[0]),
              r"%d$^{\mathrm{th}}$ percentile" % int(100 * perc[1]), r"Median",
              r"%d$^{\mathrm{th}}$ percentile" % int(100 * perc[3]),
              r"%d$^{\mathrm{th}}$ percentile" % int(100 * perc[4]))
    for p, data, sty, lbl in zip(perc, binned_data, plot_styles, labels):
        ax.loglog(bins.centers, data, sty, label=lbl)

    ax.set_xlabel(r"Community size")
    ax.set_ylabel(r"$\langle w_{\mathrm{comm}} \rangle /"
                  r"\langle w_{\mathrm{total}} \rangle$")
    if print_titles:
        ax.set_title(
            r"Ratio of avg community weigth to avg total weight (binned)")

    ax.legend(loc='best')

    # Adjust axis
    V = ax.axis()
    ax.axis((1, bins.bin_limits[-1], V[2], V[3]))

    # Save figure.
    fig_utils.savefig(fig, ncName + "_weightDist", save_formats)
Beispiel #6
0
def plot_topology_chart(motifs, chartFileName, plot_single_motifs=False):
    """Create topology chart of motifs.

    Parameters
    ----------
    motifs : iterable
       The typed motifs for which the chart is produced. The order is
       not important (the names of the motifs are independent of the
       order).
    chartFileName : str
       File name where the chart is saved. The suffix should be some
       image file format. If empty, no plot is created.
    plot_single_motifs : bool
       If True, every untyped motif is plotted in a separate figure.

    Returns
    -------
    labels : {int, str}
       A dictionary where the key is the hash of the untyped motif and
       the value gives a textual name for the motif that is shown in
       the motif chart.
    
    """

    def get_label(h, v_label, labels, lvl):
        v_label[lvl] += 1 
        v_label[lvl+1:] = [0]*(3-lvl)
        label = "-".join(map(str, v_label[:lvl+1]))
        labels[h] = label
        return label

    graph_data = {}
    hash_types = ('graph','digraph','multigraph','motif')
    # First find out the topology tree. We go through hashes in
    # increasing detail: first undirected graph, then digraph, then
    # multigraph, and finally motif that includes also the timings.
    for m in motifs:
        data = graph_data
        for hash_type in hash_types:
            h = m.get_hash(hash_type, type_map=m.basic_type_map)#, canonical_labels=True)
            clabels = m.get_node_roles(hash_type, latex=True)
            if h not in data:
                m_cp = m.copy()
                m_cp.remap_types(m.basic_type_map)
                node_labels = dict((i,r"$%s$" % str(r)) for i,r in clabels.iteritems())
                data[h] = (m_cp,node_labels,{})
            data = data[h][-1]

    # Get max size of a row.
    N_v, N_h = 0, 0
    for m, tmp, digraph_data in graph_data.itervalues():
        #N_v += 1
        for m, tmp, multigraph_data in digraph_data.itervalues():
            #N_v += 1
            for m, tmp, motif_data in multigraph_data.itervalues():
                N_v += 1
                N_h = max(N_h, 4 + len(motif_data))

    # Physical size of the output.
    w_cm_motif, h_cm_motif = 2.05, 1.85 # width and height of motif in cm
    t_cm, b_cm = 1.0, 0.5 # top and bottom margin in cm
    l_cm, r_cm = 0.30, 0.30 # left and right margin in cm
    ws_cm, hs_cm = 0.13, 0.36 # wspace and hspace in cm

    # Derived physical sizes of the figure.
    w_cm = l_cm+r_cm+N_h*w_cm_motif+(N_h-1)*ws_cm
    h_cm = t_cm+b_cm+N_v*h_cm_motif+(N_v-1)*hs_cm 

    # Set plotting properties
    font_sizes = {'text': 14, 'title':13}
    params = fig_utils.get_rcParams(w_cm, h_cm/w_cm, font_sizes)
    pylab.rcParams.update(params)
    subplot_specs = {'left':l_cm/w_cm, 'right':1-r_cm/w_cm,
                     'top':1-t_cm/h_cm, 'bottom':b_cm/h_cm,
                     'wspace':ws_cm/w_cm_motif, 'hspace':hs_cm/h_cm_motif}
    labelFontSize=9
    orderFontSize=7
    node_size=10
    node_label_size=0.8*node_size

    # Create figure and subplot helper.
    fig = pylab.figure()
    fig.subplots_adjust(**subplot_specs)
    prev_coords = mf.init_prev_coords()
    i_ax = fig_utils.SubplotHelper(N_v,N_h)

    # Go through the data depth first, plotting the motifs in each branch.
    v_label = [0,0,0,0]
    labels = {} # To map untyped hashes to their labels.

    # Sort graphs by the number of nodes and edges.
    graph_hashes = [(m.nof_nodes, m.nof_edges, h) for h,(m,tmp1,tmp2) in graph_data.iteritems()]
    for h in map(operator.itemgetter(-1), sorted(graph_hashes)):
        m, node_labels, digraph_data = graph_data[h]
        # Update label
        label = get_label(h, v_label, labels, lvl=0)
        # Plot motif m as graph.
        ax = i_ax.add_subplot(fig)
        mf.plot_motif(ax, m, prev_coords, node_color="white", node_edge_color="black",
                      label=label, labelFontSize=labelFontSize, orderFontSize=orderFontSize,
                      topology="graph", node_size=node_size,
                      node_labels=node_labels, node_label_size=node_label_size)

        if ax.is_first_row():
            ax.set_title("Graph")

        # Sort digraphs by the number of directed edges (they all have
        # the same number of nodes because the undirected underlying
        # graph is the same).
        digraph_hashes = [(m.nof_dir_edges, h) for h,(m,tmp1,tmp2) in digraph_data.iteritems()]
        for h in map(operator.itemgetter(-1), sorted(digraph_hashes)):
            m, node_labels, multigraph_data = digraph_data[h]
            i_ax.set_index_at(col=2)
            label = get_label(h, v_label, labels, lvl=1)
            # Plot motif m as digraph.
            ax = i_ax.add_subplot(fig)
            mf.plot_motif(ax, m, prev_coords, node_color="white", node_edge_color="black",
                          label=label, labelFontSize=labelFontSize, orderFontSize=orderFontSize,
                          topology="digraph", node_size=node_size,
                          node_labels=node_labels, node_label_size=node_label_size)

            if ax.is_first_row():
                ax.set_title("Digraph")

            # Sort digraphs by the total number of events.
            multigraph_hashes = [(m.nof_events, h) for h,(m,tmp1,tmp2) in multigraph_data.iteritems()]
            for h in map(operator.itemgetter(-1), sorted(multigraph_hashes)):
                m, node_labels, motif_data = multigraph_data[h]
                i_ax.set_index_at(col=3)
                label = get_label(h, v_label, labels, lvl=2)
                # Plot motif m as multigraph.
                ax = i_ax.add_subplot(fig)
                mf.plot_motif(ax, m, prev_coords, node_color="white", node_edge_color="black",
                              label=label, labelFontSize=labelFontSize, orderFontSize=orderFontSize,
                              topology="multigraph", node_size=node_size,
                              node_labels=node_labels, node_label_size=node_label_size)

                if ax.is_first_row():
                    ax.set_title("Multigraph")

                i_ax.right()

                # Sort motifs by their hash.
                for h in sorted(motif_data):
                    m, node_labels, tmp = motif_data[h]
                    i_ax.right()
                    label = get_label(h, v_label, labels, lvl=3)
                    # Plot motif m as motif.
                    ax = i_ax.add_subplot(fig)
                    mf.plot_motif(ax, m, prev_coords, node_color="white", node_edge_color="black",
                                  label=label, labelFontSize=labelFontSize, orderFontSize=orderFontSize,
                                  topology="motif", node_size=node_size,
                                  node_labels=node_labels, node_label_size=node_label_size)
                    
                    if ax.is_first_row() and i_ax.col==5:
                        ax.set_title("Motifs ...")

                    if plot_single_motifs and chartFileName:
                        # Plot the motif alone in a separate figure.
                        params = fig_utils.get_rcParams(w_cm_motif, h_cm_motif/w_cm_motif, font_sizes)
                        pylab.rcParams.update(params)
                        fig_motif = pylab.figure()
                        mf.plot_motif(fig_motif.add_axes([0.,0.,1.,1.]), m, prev_coords, node_color="white", node_edge_color="black",
                                      label="",orderFontSize=orderFontSize,
                                      topology="motif", node_size=node_size,
                                      node_labels=node_labels, node_label_size=node_label_size)
                        motifFileName = "%s/motif_%s.pdf" % ("/".join(chartFileName.split("/")[:-1]), label)
                        fig_utils.savefig(fig_motif, motifFileName, verbose=True)

                i_ax.next_row()

    if chartFileName:
        fig_utils.savefig(fig, chartFileName, verbose=True)
    return labels
Beispiel #7
0
def plot_topology_chart(motifs, chartFileName, plot_single_motifs=False):
    """Create topology chart of motifs.

    Parameters
    ----------
    motifs : iterable
       The typed motifs for which the chart is produced. The order is
       not important (the names of the motifs are independent of the
       order).
    chartFileName : str
       File name where the chart is saved. The suffix should be some
       image file format. If empty, no plot is created.
    plot_single_motifs : bool
       If True, every untyped motif is plotted in a separate figure.

    Returns
    -------
    labels : {int, str}
       A dictionary where the key is the hash of the untyped motif and
       the value gives a textual name for the motif that is shown in
       the motif chart.
    
    """
    def get_label(h, v_label, labels, lvl):
        v_label[lvl] += 1
        v_label[lvl + 1:] = [0] * (3 - lvl)
        label = "-".join(map(str, v_label[:lvl + 1]))
        labels[h] = label
        return label

    graph_data = {}
    hash_types = ('graph', 'digraph', 'multigraph', 'motif')
    # First find out the topology tree. We go through hashes in
    # increasing detail: first undirected graph, then digraph, then
    # multigraph, and finally motif that includes also the timings.
    for m in motifs:
        data = graph_data
        for hash_type in hash_types:
            h = m.get_hash(
                hash_type,
                type_map=m.basic_type_map)  #, canonical_labels=True)
            clabels = m.get_node_roles(hash_type, latex=True)
            if h not in data:
                m_cp = m.copy()
                m_cp.remap_types(m.basic_type_map)
                node_labels = dict(
                    (i, r"$%s$" % str(r)) for i, r in clabels.iteritems())
                data[h] = (m_cp, node_labels, {})
            data = data[h][-1]

    # Get max size of a row.
    N_v, N_h = 0, 0
    for m, tmp, digraph_data in graph_data.itervalues():
        #N_v += 1
        for m, tmp, multigraph_data in digraph_data.itervalues():
            #N_v += 1
            for m, tmp, motif_data in multigraph_data.itervalues():
                N_v += 1
                N_h = max(N_h, 4 + len(motif_data))

    # Physical size of the output.
    w_cm_motif, h_cm_motif = 2.05, 1.85  # width and height of motif in cm
    t_cm, b_cm = 1.0, 0.5  # top and bottom margin in cm
    l_cm, r_cm = 0.30, 0.30  # left and right margin in cm
    ws_cm, hs_cm = 0.13, 0.36  # wspace and hspace in cm

    # Derived physical sizes of the figure.
    w_cm = l_cm + r_cm + N_h * w_cm_motif + (N_h - 1) * ws_cm
    h_cm = t_cm + b_cm + N_v * h_cm_motif + (N_v - 1) * hs_cm

    # Set plotting properties
    font_sizes = {'text': 14, 'title': 13}
    params = fig_utils.get_rcParams(w_cm, h_cm / w_cm, font_sizes)
    pylab.rcParams.update(params)
    subplot_specs = {
        'left': l_cm / w_cm,
        'right': 1 - r_cm / w_cm,
        'top': 1 - t_cm / h_cm,
        'bottom': b_cm / h_cm,
        'wspace': ws_cm / w_cm_motif,
        'hspace': hs_cm / h_cm_motif
    }
    labelFontSize = 9
    orderFontSize = 7
    node_size = 10
    node_label_size = 0.8 * node_size

    # Create figure and subplot helper.
    fig = pylab.figure()
    fig.subplots_adjust(**subplot_specs)
    prev_coords = mf.init_prev_coords()
    i_ax = fig_utils.SubplotHelper(N_v, N_h)

    # Go through the data depth first, plotting the motifs in each branch.
    v_label = [0, 0, 0, 0]
    labels = {}  # To map untyped hashes to their labels.

    # Sort graphs by the number of nodes and edges.
    graph_hashes = [(m.nof_nodes, m.nof_edges, h)
                    for h, (m, tmp1, tmp2) in graph_data.iteritems()]
    for h in map(operator.itemgetter(-1), sorted(graph_hashes)):
        m, node_labels, digraph_data = graph_data[h]
        # Update label
        label = get_label(h, v_label, labels, lvl=0)
        # Plot motif m as graph.
        ax = i_ax.add_subplot(fig)
        mf.plot_motif(ax,
                      m,
                      prev_coords,
                      node_color="white",
                      node_edge_color="black",
                      label=label,
                      labelFontSize=labelFontSize,
                      orderFontSize=orderFontSize,
                      topology="graph",
                      node_size=node_size,
                      node_labels=node_labels,
                      node_label_size=node_label_size)

        if ax.is_first_row():
            ax.set_title("Graph")

        # Sort digraphs by the number of directed edges (they all have
        # the same number of nodes because the undirected underlying
        # graph is the same).
        digraph_hashes = [(m.nof_dir_edges, h)
                          for h, (m, tmp1, tmp2) in digraph_data.iteritems()]
        for h in map(operator.itemgetter(-1), sorted(digraph_hashes)):
            m, node_labels, multigraph_data = digraph_data[h]
            i_ax.set_index_at(col=2)
            label = get_label(h, v_label, labels, lvl=1)
            # Plot motif m as digraph.
            ax = i_ax.add_subplot(fig)
            mf.plot_motif(ax,
                          m,
                          prev_coords,
                          node_color="white",
                          node_edge_color="black",
                          label=label,
                          labelFontSize=labelFontSize,
                          orderFontSize=orderFontSize,
                          topology="digraph",
                          node_size=node_size,
                          node_labels=node_labels,
                          node_label_size=node_label_size)

            if ax.is_first_row():
                ax.set_title("Digraph")

            # Sort digraphs by the total number of events.
            multigraph_hashes = [(m.nof_events, h)
                                 for h, (m, tmp1,
                                         tmp2) in multigraph_data.iteritems()]
            for h in map(operator.itemgetter(-1), sorted(multigraph_hashes)):
                m, node_labels, motif_data = multigraph_data[h]
                i_ax.set_index_at(col=3)
                label = get_label(h, v_label, labels, lvl=2)
                # Plot motif m as multigraph.
                ax = i_ax.add_subplot(fig)
                mf.plot_motif(ax,
                              m,
                              prev_coords,
                              node_color="white",
                              node_edge_color="black",
                              label=label,
                              labelFontSize=labelFontSize,
                              orderFontSize=orderFontSize,
                              topology="multigraph",
                              node_size=node_size,
                              node_labels=node_labels,
                              node_label_size=node_label_size)

                if ax.is_first_row():
                    ax.set_title("Multigraph")

                i_ax.right()

                # Sort motifs by their hash.
                for h in sorted(motif_data):
                    m, node_labels, tmp = motif_data[h]
                    i_ax.right()
                    label = get_label(h, v_label, labels, lvl=3)
                    # Plot motif m as motif.
                    ax = i_ax.add_subplot(fig)
                    mf.plot_motif(ax,
                                  m,
                                  prev_coords,
                                  node_color="white",
                                  node_edge_color="black",
                                  label=label,
                                  labelFontSize=labelFontSize,
                                  orderFontSize=orderFontSize,
                                  topology="motif",
                                  node_size=node_size,
                                  node_labels=node_labels,
                                  node_label_size=node_label_size)

                    if ax.is_first_row() and i_ax.col == 5:
                        ax.set_title("Motifs ...")

                    if plot_single_motifs and chartFileName:
                        # Plot the motif alone in a separate figure.
                        params = fig_utils.get_rcParams(
                            w_cm_motif, h_cm_motif / w_cm_motif, font_sizes)
                        pylab.rcParams.update(params)
                        fig_motif = pylab.figure()
                        mf.plot_motif(fig_motif.add_axes([0., 0., 1., 1.]),
                                      m,
                                      prev_coords,
                                      node_color="white",
                                      node_edge_color="black",
                                      label="",
                                      orderFontSize=orderFontSize,
                                      topology="motif",
                                      node_size=node_size,
                                      node_labels=node_labels,
                                      node_label_size=node_label_size)
                        motifFileName = "%s/motif_%s.pdf" % ("/".join(
                            chartFileName.split("/")[:-1]), label)
                        fig_utils.savefig(fig_motif,
                                          motifFileName,
                                          verbose=True)

                i_ax.next_row()

    if chartFileName:
        fig_utils.savefig(fig, chartFileName, verbose=True)
    return labels