Esempio n. 1
0
def r_igraph_clust_coeff(g, save_fn=None):
  """
  Compute clustering coefficient/transitivity of graph g 
  and save as necessary

  @param g: The igraph loaded via Rpy2 so an R object

  @param save_fn: the filename you want to use to save it. If not provided
  the graph adds a clustcoeff attribute to all nodes and returns.
  @return: the graph with the clustcoeff attribute appended
  """

  clustcoeff = robjects.r("""
  suppressMessages(require(igraph))
  fn <- function(g){
  igraph::transitivity(g, "local")
  }
  """)
  ccvector = clustcoeff(g)

  if save_fn:
    save_fn = os.path.abspath(save_fn)
    createSave(save_fn, ccvector) # TODO: Clean input for programmatic access
    print "Clustering Coefficient saved as %s ..." % save_fn
  else:
    g = r_igraph_set_vertex_attr(g, "clustcoeff", ccvector)  # Attribute name may need to change

  return g # return so we can use for other attributes
Esempio n. 2
0
def r_igraph_clust_coeff(g, save_fn=None):
    """
  Compute clustering coefficient/transitivity of graph g 
  and save as necessary

  @param g: The igraph loaded via Rpy2 so an R object

  @param save_fn: the filename you want to use to save it. If not provided
  the graph adds a clustcoeff attribute to all nodes and returns.
  @return: the graph with the clustcoeff attribute appended
  """

    clustcoeff = robjects.r("""
  suppressMessages(require(igraph))
  fn <- function(g){
  igraph::transitivity(g, "local")
  }
  """)
    ccvector = clustcoeff(g)

    if save_fn:
        save_fn = os.path.abspath(save_fn)
        createSave(save_fn,
                   ccvector)  # TODO: Clean input for programmatic access
        print "Clustering Coefficient saved as %s ..." % save_fn
    else:
        g = r_igraph_set_vertex_attr(
            g, "clustcoeff", ccvector)  # Attribute name may need to change

    return g  # return so we can use for other attributes
Esempio n. 3
0
def r_igraph_scan1(g, save_fn=None):
    """
  Compute the scan statistic 1 of graph g and save as necessary

  @param g: The igraph loaded via Rpy2 so an R object

  @param save_fn:  the filename you want to use to save it. If not provided
  the graph adds a scan1 attribute to all nodes and returns.

  @return: The graph with the scan1 attribute appended
  """
    scanstat1 = robjects.r("""
  suppressMessages(require(igraph))
  fn <- function(g){
  igraph::local_scan(g)
  }
  """)
    ss1vector = scanstat1(g)

    if save_fn:
        save_fn = os.path.abspath(save_fn)
        createSave(os.path.abspath(save_fn),
                   ss1vector)  # TODO: Clean input for programmatic access
        print "Scan Statistic saved as %s ..." % save_fn
    else:
        g = r_igraph_set_vertex_attr(
            g, "scan1", ss1vector)  # Attribute name may need to change

    return g  # return so we can use for other attributes
Esempio n. 4
0
def r_igraph_triangles(g, save_fn=None):
    """
  Compute local triangle count of graph g and save as necessary

  @param g: The igraph loaded via Rpy2 so an R object
  
  @param save_fn: the filename you want to use to save it. If not provided
  the graph adds a tri count attribute to all nodes and returns.

  @return: The graph with the tri vertex attribute appended
  """

    triangles = robjects.r("""
  suppressMessages(require(igraph))
  fn <- function(g){
  igraph::adjacent.triangles(g)
  }
  """)
    trivector = triangles(g)

    if save_fn:
        save_fn = os.path.abspath(save_fn)
        createSave(save_fn,
                   trivector)  # TODO: Clean input for programmatic access
        print "Triangle Count saved as %s ..." % save_fn
    else:
        g = r_igraph_set_vertex_attr(
            g, "tri", trivector)  # Attribute name may need to change

    return g  # return so we can use for other attributes
Esempio n. 5
0
def r_igraph_degree(g, mode="total", save_fn=None):
    """
  Compute degree of graph g and save as necessary

  @param g: The igraph loaded via Rpy2 so an R object

  @param mode: the type of degree. Default is an undirected i.e. in-degree + out degree
  @param save_fn: the filename you want to use to save it. If not provided
      the graph adds a degree attribute to all nodes and returns.

  @return: the graph with the degree attribute set.
  """

    deg = robjects.r("""
  suppressMessages(require(igraph))
  fn <- function(g){
  igraph::degree(g, mode="%s")
  }
  """ % mode)
    degvector = deg(g)

    if save_fn:
        save_fn = os.path.abspath(save_fn)
        createSave(save_fn,
                   degvector)  # TODO: Clean input for programmatic access
        print "Degree saved as %s ..." % save_fn
    else:
        g = r_igraph_set_vertex_attr(
            g, "degree", degvector)  # Attribute name may need to change

    return g  # return so we can use for other attributes
Esempio n. 6
0
def r_igraph_degree(g, mode="total", save_fn=None):
    """
  Compute degree of graph g and save as necessary

  @param g: The igraph loaded via Rpy2 so an R object

  @param mode: the type of degree. Default is an undirected i.e. in-degree + out degree
  @param save_fn: the filename you want to use to save it. If not provided
      the graph adds a degree attribute to all nodes and returns.

  @return: the graph with the degree attribute set.
  """

    deg = robjects.r(
        """
  suppressMessages(require(igraph))
  fn <- function(g){
  igraph::degree(g, mode="%s")
  }
  """
        % mode
    )
    degvector = deg(g)

    if save_fn:
        save_fn = os.path.abspath(save_fn)
        createSave(save_fn, degvector)  # TODO: Clean input for programmatic access
        print "Degree saved as %s ..." % save_fn
    else:
        g = r_igraph_set_vertex_attr(g, "degree", degvector)  # Attribute name may need to change

    return g  # return so we can use for other attributes
Esempio n. 7
0
def r_igraph_triangles(g, save_fn=None):
  """
  Compute local triangle count of graph g and save as necessary

  @param g: The igraph loaded via Rpy2 so an R object
  
  @param save_fn: the filename you want to use to save it. If not provided
  the graph adds a tri count attribute to all nodes and returns.

  @return: The graph with the tri vertex attribute appended
  """

  triangles = robjects.r("""
  suppressMessages(require(igraph))
  fn <- function(g){
  igraph::adjacent.triangles(g)
  }
  """)
  trivector = triangles(g)

  if save_fn:
    save_fn = os.path.abspath(save_fn)
    createSave(save_fn, trivector) # TODO: Clean input for programmatic access
    print "Triangle Count saved as %s ..." % save_fn
  else:
    g = r_igraph_set_vertex_attr(g, "tri", trivector)  # Attribute name may need to change

  return g # return so we can use for other attributes
Esempio n. 8
0
def r_igraph_scan1(g, save_fn=None):
  """
  Compute the scan statistic 1 of graph g and save as necessary

  @param g: The igraph loaded via Rpy2 so an R object

  @param save_fn:  the filename you want to use to save it. If not provided
  the graph adds a scan1 attribute to all nodes and returns.

  @return: The graph with the scan1 attribute appended
  """
  scanstat1 = robjects.r("""
  suppressMessages(require(igraph))
  fn <- function(g){
  igraph::local_scan(g)
  }
  """)
  ss1vector = scanstat1(g)

  if save_fn:
    save_fn = os.path.abspath(save_fn)
    createSave(os.path.abspath(save_fn), ss1vector) # TODO: Clean input for programmatic access
    print  "Scan Statistic saved as %s ..." % save_fn
  else:
    g = r_igraph_set_vertex_attr(g, "scan1", ss1vector)  # Attribute name may need to change

  return g # return so we can use for other attributes
Esempio n. 9
0
    print "eigenvectors ssaved as %s ..." % save_fn[1]
  else:

    global gl_eigvects
    gl_eigvects = eigs[0][1]

    print "Setting eigenvalues as graph attr ..."
    g = r_igraph_set_graph_attribute(g, "eigvals", "["+", ".join(map(cut, (eigs[0][0])))+"]") # Return a comma separated string

    eig_idx = eigs[1]
    print "Mapping eigenvectors ..."
    eigvects = map(get_str_eigvects, [(idx, idx+nev) for idx in xrange(0, ((len(eigs[1]))*nev), nev)])
    del eigs

    print "Setting eigenvectors as vertex attr ..."
    g = r_igraph_set_vertex_attr(g, "latent_pos", value=eigvects, index=eig_idx, is_str=True) # Could not create char sequences only lists :-/
    print "Eigenvalue computation not saved to disk. Eigen-pairs added as graph attributes ...."

  return g

def get_str_eigvects(idx):
  """
  Used for mapping to get eigenvectors that correspond to each vertex of the graph

  @param idx: a 2-tuple that gives the indexes of the eigenvector 1-d flattened matrix that correspond
  to the particular vertex

  @return: A vector i.e the eigenvector (latent position) for that vertex cast to a string
  """
  global gl_eigvects
  return "["+", ".join(map(cut, (gl_eigvects[idx[0]:idx[1]])))+"]"
Esempio n. 10
0
        print "Setting eigenvalues as graph attr ..."
        g = r_igraph_set_graph_attribute(
            g, "eigvals", "[" + ", ".join(map(cut, (eigs[0][0]))) +
            "]")  # Return a comma separated string

        eig_idx = eigs[1]
        print "Mapping eigenvectors ..."
        eigvects = map(get_str_eigvects,
                       [(idx, idx + nev)
                        for idx in xrange(0, ((len(eigs[1])) * nev), nev)])
        del eigs

        print "Setting eigenvectors as vertex attr ..."
        g = r_igraph_set_vertex_attr(
            g, "latent_pos", value=eigvects, index=eig_idx,
            is_str=True)  # Could not create char sequences only lists :-/
        print "Eigenvalue computation not saved to disk. Eigen-pairs added as graph attributes ...."

    return g


def get_str_eigvects(idx):
    """
  Used for mapping to get eigenvectors that correspond to each vertex of the graph

  @param idx: a 2-tuple that gives the indexes of the eigenvector 1-d flattened matrix that correspond
  to the particular vertex

  @return: A vector i.e the eigenvector (latent position) for that vertex cast to a string
  """