Esempio n. 1
0
def betweenness_sample_size(graph, sample_size, set_attributes=True, time_out=0):
    """Compute approximate betweenness using VC-Dimension and a specified sample size."""
    logging.info("Computing approximate betweenness using VC-Dimension, fixed sample size")
    if not time_out:
        (stats, betw) = do_betweenness_sample_size(graph, sample_size)
    else:
        logging.info("Adding timeout")
        timeout_betweenness = timeout.add_timeout(do_betweenness_sample_size, time_out)
        timeout_betweenness(graph, sample_size)
        while (not timeout_betweenness.ready) and (not timeout_betweenness.expired):
            pass
        if timeout_betweenness.ready:
            (stats, betw) = timeout_betweenness.value
            logging.info("Betweenness computed in %s seconds", stats['time'])
            stats["timed_out"] = 0
        else:
            logging.info("Betweenness computation timer expired after %d seconds.", time_out)
            #We need the partial results een if we have timeout
            #betw = [0] * graph.vcount()
            #try:        
            (stats, betw) = timeout_betweenness.value
            #except NotReadyError:

            # We still the stat even in the case that the algorithm does not terminate
            #stats = {"time": time_out, "timed_out": 1, "forward_touched_edges": -1,
                   # "backward_touched_edges": -1, "sample_size": sample_size}
            #stats["timed_out"] = 0
    # Write attributes to graph, if specified
    if set_attributes:
        for key in stats:
            graph["vc_" + key] = stats[key]
        graph.vs["vc_betw"] = betw

    return (stats, betw)
Esempio n. 2
0
def betweenness_sample_size(graph, sample_size, set_attributes=True, time_out=0):
    """Compute approximate betweenness using Brandes and Pich algo and a specified sample size."""
    logging.info("Computing approximate betweenness using Brandes and Pich algorithm, fixed sample size")
    if not time_out:
        (stats, betw) = do_betweenness_sample_size(graph, sample_size)
    else:
        timeout_betweenness = timeout.add_timeout(do_betweenness_sample_size, time_out)
        timeout_betweenness(graph, sample_size)
        while (not timeout_betweenness.ready) and (not timeout_betweenness.expired):
            pass
        if timeout_betweenness.ready:
            (stats, betw) = timeout_betweenness.value
            logging.info("Betweenness computed in %s seconds", stats['time'])
            stats["timed_out"] = 0
        else:
            logging.info("Betweenness computation timer expired after %d seconds.", time_out)
            betw = [0] * graph.vcount()
            stats = {"time": time_out, "timed_out": 1, "forward_touched_edges": -1,
                    "backward_touched_edges": -1, "sample_size": sample_size}
    # Write attributes to graph, if specified
    if set_attributes:
        for key in stats:
            graph["bp_" + key] = stats[key]
        graph.vs["bp_betw"] = betw

    return (stats, betw)
Esempio n. 3
0
def betweenness(graph, epsilon, delta, k, use_approx_diameter=True,
        set_attributes=True, time_out=0):
    """Compute approximate betweenness using VC-Dimension.
    
    Compute approximations of the betweenness centrality of all the vertices in
    the graph using sampling and the VC-Dimension, and the time needed to
    compute them, and some other statistics.

    Return a tuple with the statistics (a dictionary) and the list
    of betweenness values (one for each vertex in the graph).

    The meaning of the use_approx_diameter parameter is peculiar. If True or
    1 (default), compute an approximation of the diameter (only valid for
    undirected, unweighted graphs). If False or 0, compute
    the exact diameter (which kind of defeat the purpose of sampling, by the
    way). If any integer > 1, use this value for the diameter, i.e. do not
    perform any computation for the diameter.
    If set_attributes is True (default), then set the values of the betweenness
    as vertex attributes, and the time as a graph attribute.
    
    """
    logging.info("Computing approximate betweenness using VC-Dimension")
    if not time_out:
        (stats, betw) = do_betweenness(graph, epsilon, delta, k,
                use_approx_diameter)
    else:
        timeout_betweenness = timeout.add_timeout(do_betweenness, time_out)
        timeout_betweenness(graph, epsilon, delta, k, use_approx_diameter)
        while (not timeout_betweenness.ready) and (not timeout_betweenness.expired):
            pass
        if timeout_betweenness.ready:
            (stats, betw) = timeout_betweenness.value
            logging.info("Betweenness computed in %s seconds", stats['time'])
            stats["timed_out"] = 0
        else:
            logging.info("Betweenness computation timer expired after %d seconds.", time_out)
            betw = [0] * graph.vcount()
            stats = {"time": time_out, "timed_out": 1, "forward_touched_edges": -1,
                    "backward_touched_edges": -1, "sample_size_1": -1,
                    "sample_size_2": -1,
                    "diameter": -1, "diameter_touched_edges": -1 }
    stats["delta"] = delta
    if int(use_approx_diameter) == 1:
        stats["diam_type"] = "approx" 
    elif int(use_approx_diameter) == 0:
        stats["diam_type"] = "exact"
    else:
        stats["diam_type"] = "specif"
    stats["epsilon"] = epsilon
    stats["k"] = k

    # Write attributes to graph, if specified
    if set_attributes:
        for key in stats:
            graph["vc_" + key] = stats[key]
        graph.vs["vc_betw"] = betw

    return (stats, betw)
Esempio n. 4
0
def betweenness(graph, epsilon, delta, set_attributes=True, time_out=0):
    """Compute approx. betweenness using BrandesGSS algorithm.
    
    Compute approximations of the betweenness centrality of all the vertices in
    the graph using the algorithm by Robert Geisberger, Peter Sanders, Dominik
    Schultes, and the time needed to compute them. For the algorithm, see
    http://www.siam.org/proceedings/alenex/2008/alx08_09geisbergerr.pdf .

    Return a tuple with various stats about the computation and the list
    of betweenness values (one for each vertex in the graph).
    If set_attributes is True (default), then set the values of the betweenness
    as vertex attributes, and the time as a graph attribute.
    
    """
    # We do not use logging from here to the end of the computation to avoid
    # wasting time
    logging.info(
        "Computing approximate betweenness using GeisbergerSS, linear scaling algorithm"
    )
    if not time_out:
        (stats, betw) = do_betweenness(graph, epsilon, delta)
    else:
        timeout_betweenness = timeout.add_timeout(do_betweenness, time_out)
        timeout_betweenness(graph, epsilon, delta)
        while (not timeout_betweenness.ready) and (
                not timeout_betweenness.expired):
            pass
        if timeout_betweenness.ready:
            (stats, betw) = timeout_betweenness.value
            logging.info("Betweenness computed in %s seconds", stats['time'])
            stats["timed_out"] = 0
        else:
            logging.info(
                "Betweenness computation timer expired after %d seconds.",
                time_out)
            betw = [0] * graph.vcount()
            stats = {
                "time": time_out,
                "timed_out": 1,
                "forward_touched_edges": -1,
                "backward_touched_edges": -1,
                "sample_size": -1
            }

    stats["delta"] = delta
    stats["epsilon"] = epsilon
    # Write attributes to graph, if specified
    if set_attributes:
        for key in stats:
            graph["bp_" + key] = stats[key]
        graph.vs["bp_betw"] = betw

    return (stats, betw)
Esempio n. 5
0
def betweenness(graph, epsilon, delta, set_attributes=True, time_out=0):
    """Compute approx. betweenness using Brandes and Pick algorithm.
    
    Compute approximations of the betweenness centrality of all the vertices in
    the graph using the algorithm by Brandes and Pich, and the time needed to
    compute them. For the algorihm, see
    http://www.worldscientific.com/doi/abs/10.1142/S0218127407018403 .

    Return a tuple with the time needed to compute the betweenness and the list
    of betweenness values (one for each vertex in the graph).
    If set_attributes is True (default), then set the values of the betweenness
    as vertex attributes, and the time as a graph attribute.
    
    """
    # We do not use logging from here to the end of the computation to avoid
    # wasting time
    logging.info(
        "Computing approximate betweenness using Brandes and Pich algorithm")
    if not time_out:
        (stats, betw) = do_betweenness(graph, epsilon, delta)
    else:
        timeout_betweenness = timeout.add_timeout(do_betweenness, time_out)
        timeout_betweenness(graph, epsilon, delta)
        while (not timeout_betweenness.ready) and (
                not timeout_betweenness.expired):
            pass
        if timeout_betweenness.ready:
            (stats, betw) = timeout_betweenness.value
            logging.info("Betweenness computed in %s seconds", stats['time'])
            stats["timed_out"] = 0
        else:
            logging.info(
                "Betweenness computation timer expired after %d seconds.",
                time_out)
            betw = [0] * graph.vcount()
            stats = {
                "time": time_out,
                "timed_out": 1,
                "forward_touched_edges": -1,
                "backward_touched_edges": -1,
                "sample_size": -1
            }

    stats["delta"] = delta
    stats["epsilon"] = epsilon
    # Write attributes to graph, if specified
    if set_attributes:
        for key in stats:
            graph["bp_" + key] = stats[key]
        graph.vs["bp_betw"] = betw

    return (stats, betw)
Esempio n. 6
0
    def check(self, entry, limit=0, ident=''):
        """Check the entry against the verse's official text.

        Calls with a non-positive limit are blocking in nature. Those with
        a limit greater than zero are started asynchronously and run in a
        separate process. If a timeout manager is running, a cancellation
        method is registered using an IP address and the verse reference."""
        if limit <= 0:
            return compare.search(self.__text, entry)
        # We are working with a timeout call.
        self.__search = timeout.add_timeout(compare.search, limit)
        self.__search(self.__text, entry)
        if Verse.__manager:
            # The verse manager timeout system should be used.
            with Verse.__timeout:
                session = manager.Session(limit + 1, self.__search.cancel)
                Verse.__timeout[ident + ' -> ' + self.__addr] = session
Esempio n. 7
0
    def check(self, entry, limit=0, ident=''):
        """Check the entry against the verse's official text.

        Calls with a non-positive limit are blocking in nature. Those with
        a limit greater than zero are started asynchronously and run in a
        separate process. If a timeout manager is running, a cancellation
        method is registered using an IP address and the verse reference."""
        if limit <= 0:
            return compare.search(self.__text, entry)
        # We are working with a timeout call.
        self.__search = timeout.add_timeout(compare.search, limit)
        self.__search(self.__text, entry)
        if Verse.__manager:
            # The verse manager timeout system should be used.
            with Verse.__timeout:
                session = manager.Session(limit + 1, self.__search.cancel)
                Verse.__timeout[ident + ' -> ' + self.__addr] = session
Esempio n. 8
0
def betweenness(graph, epsilon, delta, set_attributes=True, time_out=0):
    """Compute approx. betweenness using Brandes and Pick algorithm.
    
    Compute approximations of the betweenness centrality of all the vertices in
    the graph using the algorithm by Brandes and Pich, and the time needed to
    compute them. For the algorihm, see
    http://www.worldscientific.com/doi/abs/10.1142/S0218127407018403 .

    Return a tuple with the time needed to compute the betweenness and the list
    of betweenness values (one for each vertex in the graph).
    If set_attributes is True (default), then set the values of the betweenness
    as vertex attributes, and the time as a graph attribute.
    
    """
    # We do not use logging from here to the end of the computation to avoid
    # wasting time
    logging.info("Computing approximate betweenness using Brandes and Pich algorithm")
    if not time_out:
        (stats, betw) = do_betweenness(graph, epsilon, delta)
    else:
        timeout_betweenness = timeout.add_timeout(do_betweenness, time_out)
        timeout_betweenness(graph, epsilon, delta)
        while (not timeout_betweenness.ready) and (not timeout_betweenness.expired):
            pass
        if timeout_betweenness.ready:
            (stats, betw) = timeout_betweenness.value
            logging.info("Betweenness computed in %s seconds", stats['time'])
            stats["timed_out"] = 0
        else:
            logging.info("Betweenness computation timer expired after %d seconds.", time_out)
            betw = [0] * graph.vcount()
            stats = {"time": time_out, "timed_out": 1, "forward_touched_edges": -1,
                    "backward_touched_edges": -1, "sample_size": -1}

    stats["delta"] = delta
    stats["epsilon"] = epsilon
    # Write attributes to graph, if specified
    if set_attributes:
        for key in stats:
            graph["bp_" + key] = stats[key]
        graph.vs["bp_betw"] = betw

    return (stats, betw)
Esempio n. 9
0
def betweenness(graph, epsilon, delta, set_attributes=True, time_out=0):
    """Compute approx. betweenness using BrandesGSS algorithm.
    
    Compute approximations of the betweenness centrality of all the vertices in
    the graph using the algorithm by Robert Geisberger, Peter Sanders, Dominik
    Schultes, and the time needed to compute them. For the algorithm, see
    http://www.siam.org/proceedings/alenex/2008/alx08_09geisbergerr.pdf .

    Return a tuple with various stats about the computation and the list
    of betweenness values (one for each vertex in the graph).
    If set_attributes is True (default), then set the values of the betweenness
    as vertex attributes, and the time as a graph attribute.
    
    """
    # We do not use logging from here to the end of the computation to avoid
    # wasting time
    logging.info("Computing approximate betweenness using GeisbergerSS, linear scaling algorithm")
    if not time_out:
        (stats, betw) = do_betweenness(graph, epsilon, delta)
    else:
        timeout_betweenness = timeout.add_timeout(do_betweenness, time_out)
        timeout_betweenness(graph, epsilon, delta)
        while (not timeout_betweenness.ready) and (not timeout_betweenness.expired):
            pass
        if timeout_betweenness.ready:
            (stats, betw) = timeout_betweenness.value
            logging.info("Betweenness computed in %s seconds", stats['time'])
            stats["timed_out"] = 0
        else:
            logging.info("Betweenness computation timer expired after %d seconds.", time_out)
            betw = [0] * graph.vcount()
            stats = {"time": time_out, "timed_out": 1, "forward_touched_edges": -1,
                    "backward_touched_edges": -1, "sample_size": -1}

    stats["delta"] = delta
    stats["epsilon"] = epsilon
    # Write attributes to graph, if specified
    if set_attributes:
        for key in stats:
            graph["bp_" + key] = stats[key]
        graph.vs["bp_betw"] = betw

    return (stats, betw)
Esempio n. 10
0
def betweenness_sample_size(graph,
                            sample_size,
                            set_attributes=True,
                            time_out=0):
    """Compute approximate betweenness using VC-Dimension and a specified sample size."""
    logging.info(
        "Computing approximate betweenness using VC-Dimension, fixed sample size"
    )
    if not time_out:
        (stats, betw) = do_betweenness_sample_size(graph, sample_size)
    else:
        logging.info("Adding timeout")
        timeout_betweenness = timeout.add_timeout(do_betweenness_sample_size,
                                                  time_out)
        timeout_betweenness(graph, sample_size)
        while (not timeout_betweenness.ready) and (
                not timeout_betweenness.expired):
            pass
        if timeout_betweenness.ready:
            (stats, betw) = timeout_betweenness.value
            logging.info("Betweenness computed in %s seconds", stats['time'])
            stats["timed_out"] = 0
        else:
            logging.info(
                "Betweenness computation timer expired after %d seconds.",
                time_out)
            #We need the partial results een if we have timeout
            #betw = [0] * graph.vcount()
            #try:
            (stats, betw) = timeout_betweenness.value
            #except NotReadyError:

            # We still the stat even in the case that the algorithm does not terminate
            #stats = {"time": time_out, "timed_out": 1, "forward_touched_edges": -1,
            # "backward_touched_edges": -1, "sample_size": sample_size}
            #stats["timed_out"] = 0
    # Write attributes to graph, if specified
    if set_attributes:
        for key in stats:
            graph["vc_" + key] = stats[key]
        graph.vs["vc_betw"] = betw

    return (stats, betw)
Esempio n. 11
0
def betweenness(graph, set_attributes=True, time_out=0):
    """Compute exact betweenness of vertices in graph and statistics

    Return a tuple with the statistics (a dictionary) and the list of
    betweenness values (one for each vertex in the graph).
    If set_attributes is True (default), then set the values of the betweenness
    as vertex attributes, and the time as a graph attribute.

    """
    # We minimize the use of logging from here to the end of the computation to avoid
    # wasting time
    logging.info("Computing exact betweenness")
    if not time_out:
        (stats, betw) = do_betweenness(graph)
    else:
        timeout_betweenness = timeout.add_timeout(do_betweenness, time_out)
        timeout_betweenness(graph)
        while (not timeout_betweenness.ready) and (
                not timeout_betweenness.expired):
            pass
        if timeout_betweenness.ready:
            (stats, betw) = timeout_betweenness.value
            logging.info("Betweenness computed in %s seconds", stats['time'])
            stats["timed_out"] = 0
        else:
            logging.info(
                "Betweenness computation timer expired after %d seconds.",
                time_out)
            betw = [0] * graph.vcount()
            stats = {
                "time": time_out,
                "timed_out": 1,
                "forward_touched_edges": -1,
                "backward_touched_edges": -1
            }

    # Write attributes to graph, if specified
    if set_attributes:
        for key in stats:
            graph["exact_" + key] = stats[key]
        graph.vs["exact_betw"] = betw

    return (stats, betw)
Esempio n. 12
0
def betweenness_sample_size(graph,
                            sample_size,
                            set_attributes=True,
                            time_out=0):
    """Compute approximate betweenness using Geisberger et al. algo, linear
    scaling version and a specified sample size."""
    logging.info(
        "Computing approximate betweenness using GeisbergerSS algorithm, linear scaling fixed sample size"
    )
    if not time_out:
        (stats, betw) = do_betweenness_sample_size(graph, sample_size)
    else:
        timeout_betweenness = timeout.add_timeout(do_betweenness_sample_size,
                                                  time_out)
        timeout_betweenness(graph, sample_size)
        while (not timeout_betweenness.ready) and (
                not timeout_betweenness.expired):
            pass
        if timeout_betweenness.ready:
            (stats, betw) = timeout_betweenness.value
            logging.info("Betweenness computed in %s seconds", stats['time'])
            stats["timed_out"] = 0
        else:
            logging.info(
                "Betweenness computation timer expired after %d seconds.",
                time_out)
            betw = [0] * graph.vcount()
            stats = {
                "time": time_out,
                "timed_out": 1,
                "forward_touched_edges": -1,
                "backward_touched_edges": -1,
                "sample_size": sample_size
            }
    # Write attributes to graph, if specified
    if set_attributes:
        for key in stats:
            graph["bp_" + key] = stats[key]
        graph.vs["bp_betw"] = betw

    return (stats, betw)
Esempio n. 13
0
def betweenness(graph, set_attributes=True, time_out=0):
    """Compute exact betweenness of vertices in graph and statistics

    Return a tuple with the statistics (a dictionary) and the list of
    betweenness values (one for each vertex in the graph).
    If set_attributes is True (default), then set the values of the betweenness
    as vertex attributes, and the time as a graph attribute.

    """
    # We minimize the use of logging from here to the end of the computation to avoid
    # wasting time 
    logging.info("Computing exact betweenness")
    if not time_out:
        (stats, betw) = do_betweenness(graph)
    else:
        timeout_betweenness = timeout.add_timeout(do_betweenness, time_out)
        timeout_betweenness(graph)
        while (not timeout_betweenness.ready) and (not timeout_betweenness.expired):
            pass
        if timeout_betweenness.ready:
            (stats, betw) = timeout_betweenness.value
            logging.info("Betweenness computed in %s seconds", stats['time'])
            stats["timed_out"] = 0
        else:
            logging.info("Betweenness computation timer expired after %d seconds.", time_out)
            betw = [0] * graph.vcount()
            stats = {"time": time_out, "timed_out": 1, "forward_touched_edges": -1,
                    "backward_touched_edges": -1}

    # Write attributes to graph, if specified
    if set_attributes:
        for key in stats:
            graph["exact_" + key] = stats[key]
        graph.vs["exact_betw"] = betw

    return (stats, betw)
Esempio n. 14
0
 def __init__(self, addr, text):
     """Initialize the reference and text of a Verse instance."""
     self.__addr = addr
     self.__text = text
     self.__search = timeout.add_timeout(compare.search)
Esempio n. 15
0
 def __init__(self, addr, text):
     """Initialize the reference and text of a Verse instance."""
     self.__addr = addr
     self.__text = text
     self.__search = timeout.add_timeout(compare.search)
Esempio n. 16
0
def betweenness(graph,
                epsilon,
                delta,
                k,
                use_approx_diameter=True,
                set_attributes=True,
                time_out=0):
    """Compute approximate betweenness using VC-Dimension.
    
    Compute approximations of the betweenness centrality of all the vertices in
    the graph using sampling and the VC-Dimension, and the time needed to
    compute them, and some other statistics.

    Return a tuple with the statistics (a dictionary) and the list
    of betweenness values (one for each vertex in the graph).

    The meaning of the use_approx_diameter parameter is peculiar. If True or
    1 (default), compute an approximation of the diameter (only valid for
    undirected, unweighted graphs). If False or 0, compute
    the exact diameter (which kind of defeat the purpose of sampling, by the
    way). If any integer > 1, use this value for the diameter, i.e. do not
    perform any computation for the diameter.
    If set_attributes is True (default), then set the values of the betweenness
    as vertex attributes, and the time as a graph attribute.
    
    """
    logging.info("Computing approximate betweenness using VC-Dimension")
    if not time_out:
        (stats, betw) = do_betweenness(graph, epsilon, delta, k,
                                       use_approx_diameter)
    else:
        timeout_betweenness = timeout.add_timeout(do_betweenness, time_out)
        timeout_betweenness(graph, epsilon, delta, k, use_approx_diameter)
        while (not timeout_betweenness.ready) and (
                not timeout_betweenness.expired):
            pass
        if timeout_betweenness.ready:
            (stats, betw) = timeout_betweenness.value
            logging.info("Betweenness computed in %s seconds", stats['time'])
            stats["timed_out"] = 0
        else:
            logging.info(
                "Betweenness computation timer expired after %d seconds.",
                time_out)
            betw = [0] * graph.vcount()
            stats = {
                "time": time_out,
                "timed_out": 1,
                "forward_touched_edges": -1,
                "backward_touched_edges": -1,
                "sample_size_1": -1,
                "sample_size_2": -1,
                "diameter": -1,
                "diameter_touched_edges": -1
            }
    stats["delta"] = delta
    if int(use_approx_diameter) == 1:
        stats["diam_type"] = "approx"
    elif int(use_approx_diameter) == 0:
        stats["diam_type"] = "exact"
    else:
        stats["diam_type"] = "specif"
    stats["epsilon"] = epsilon
    stats["k"] = k

    # Write attributes to graph, if specified
    if set_attributes:
        for key in stats:
            graph["vc_" + key] = stats[key]
        graph.vs["vc_betw"] = betw

    return (stats, betw)