Commit c885a9a1 authored by Kelvin Rodriguez's avatar Kelvin Rodriguez Committed by GitHub
Browse files

Merge pull request #171 from acpaquette/cluster

Cluster Plot (closes #157)
parents 2acbb126 af58e0b6
Loading
Loading
Loading
Loading
+19 −1
Original line number Diff line number Diff line
@@ -14,7 +14,7 @@ from plio.io.io_gdal import GeoDataset
from autocnet.graph import markov_cluster
from autocnet.graph.edge import Edge
from autocnet.graph.node import Node
from autocnet.vis.graph_view import plot_graph
from autocnet.vis.graph_view import plot_graph, cluster_plot


class CandidateGraph(nx.Graph):
@@ -570,6 +570,24 @@ class CandidateGraph(nx.Graph):
        """
        return plot_graph(self, ax=ax, **kwargs)

    def plot_cluster(self, ax=None, **kwargs):
        """
        Plot the graph based on the clusters generated by
        the markov clustering algorithm

        Parameters
        ----------
        ax : object
             A MatPlotLib axes object.

        Returns
        -------
        ax : object
             A MatPlotLib axes object.

        """
        return cluster_plot(self, ax, **kwargs)

    def create_edge_subgraph(self, edges):
        """
        Create a subgraph using a list of edges.
+42 −1
Original line number Diff line number Diff line
@@ -106,10 +106,10 @@ def plot_node(node, ax=None, clean_keys=[], index_mask=None, **kwargs):

    return ax


def plot_edge_decomposition(edge, ax=None, clean_keys=[], image_space=100,
                            scatter_kwargs={}, line_kwargs={}, image_kwargs={}):


    if ax is None:
        ax = plt.gca()

@@ -175,6 +175,8 @@ def plot_edge_decomposition(edge, ax=None, clean_keys=[], image_space=100,
        ax.plot((l[0][0], l[1][0]), (l[0][1], l[1][1]), color=color, **line_kwargs)

    return ax


def plot_edge(edge, ax=None, clean_keys=[], image_space=100,
              scatter_kwargs={}, line_kwargs={}, image_kwargs={}):
    """
@@ -271,3 +273,42 @@ def plot_edge(edge, ax=None, clean_keys=[], image_space=100,
        ax.plot((l[0][0], l[1][0]), (l[0][1], l[1][1]), color=color, **line_kwargs)

    return ax


def cluster_plot(graph, ax=None, cmap='Spectral'):  # pragma: no cover
    """
    Parameters
    ----------
    graph : object
            A networkX or derived graph object

    ax : object
         A MatPlotLib axes object

    cmap : str
           A MatPlotLib color map string. Default 'Spectral'

    Returns
    -------
    ax : object
         A MatPlotLib axes object that was either passed in
         or a new axes object
    """
    if ax is None:
        ax = plt.gca()

    if not hasattr(graph, 'clusters'):
        raise AttributeError('Clusters have not been computed.')

    cmap = matplotlib.cm.get_cmap(cmap)

    colors = []

    for i, n in graph.nodes_iter(data=True):
        for j in enumerate(graph.clusters):
            if i in graph.clusters.get(j[1]):
                colors.append(cmap(j[1])[0])
                continue

    nx.draw(graph, ax=ax, node_color=colors)
    return ax