Exemplo n.º 1
0
def local_k(network, events, refs, scale_set, cache=None):
    """
    Computes local K function

    network: an undirected network data to which reference points are injected
    refs: a set of reference points on the given network
          points unprojected into the network
    events: a set of event points on the given network
            points projected into the network
    scale_set: a tuple defining spatial scales to be examined
               (min, max, interval)
    """

    node2localK = {}
    net_distances = {}
    if cache: net_distances = cache
    for node in refs:
        node = node[1][0]
        a_dest = network[node].keys()[0]
        node_proj = (node, a_dest, 0, network[node][a_dest])
        if node not in net_distances:
            net_distances[node] = pynet.dijkstras(network, node, scale_set[1])
        if a_dest not in net_distances:
            net_distances[a_dest] = pynet.dijkstras(network, node,
                                                    scale_set[1])
        distances = pynet.proj_distances_undirected(
            network, node_proj, events, scale_set[1],
            cache=net_distances).values()
        node2localK[node] = pykfuncs.kt_values(scale_set, distances, 1)
    return node2localK, net_distances
Exemplo n.º 2
0
def local_k(network, events, refs, scale_set, cache=None):
    """
    Computes local K function

    network: an undirected network data to which reference points are injected
    refs: a set of reference points on the given network
          points unprojected into the network
    events: a set of event points on the given network
            points projected into the network
    scale_set: a tuple defining spatial scales to be examined
               (min, max, interval)
    """

    node2localK = {}
    net_distances = {}
    if cache: net_distances = cache
    for node in refs:
        node = node[1][0]
        a_dest = network[node].keys()[0]
        node_proj = (node, a_dest, 0, network[node][a_dest])
        if node not in net_distances:
            net_distances[node] = pynet.dijkstras(network, node, scale_set[1])
        if a_dest not in net_distances:
            net_distances[a_dest] = pynet.dijkstras(network, node, scale_set[1])
        distances = pynet.proj_distances_undirected(network, node_proj, events, scale_set[1], cache=net_distances).values()
        node2localK[node] = pykfuncs.kt_values(scale_set, distances, 1)
    return node2localK, net_distances
Exemplo n.º 3
0
def dist_weights(network, id2linkpoints, link2id, bandwidth):
    """
    Obtains a distance-based spatial weights matrix using network distance

    Parameters:
        network: an undirected network without additional attributes 
        id2linkpoints: a dictionary that includes a list of network-projected, midpoints of edges in the network
        link2id: a dictionary that associates each edge to a unique id
        bandwidth: a threshold distance for creating a spatial weights matrix

    Returns:
        w : a distance-based, binary spatial weights matrix
        id2link: a dictionary that associates a unique id to each edge of the network
    """
    linkpoints = id2linkpoints.values()
    neighbors, id2link = {}, {}
    net_distances = {}
    for linkpoint in id2linkpoints:
        if linkpoints[linkpoint] not in net_distances:
            net_distances[linkpoints[linkpoint][0]] = pynet.dijkstras(
                network, linkpoints[linkpoint][0], r=bandwidth)
            net_distances[linkpoints[linkpoint][1]] = pynet.dijkstras(
                network, linkpoints[linkpoint][1], r=bandwidth)
        ngh = pynet.proj_distances_undirected(network,
                                              linkpoints[linkpoint],
                                              linkpoints,
                                              r=bandwidth,
                                              cache=net_distances)
        #ngh = pynet.proj_distances_undirected(network, linkpoints[linkpoint], linkpoints, r=bandwidth)
        if linkpoints[linkpoint] in ngh:
            del ngh[linkpoints[linkpoint]]
        if linkpoint not in neighbors:
            neighbors[linkpoint] = []
        for k in ngh.keys():
            neighbor = link2id[k[:2]]
            if neighbor not in neighbors[linkpoint]:
                neighbors[linkpoint].append(neighbor)
            if neighbor not in neighbors:
                neighbors[neighbor] = []
            if linkpoint not in neighbors[neighbor]:
                neighbors[neighbor].append(linkpoint)
        id2link[linkpoint] = id2linkpoints[linkpoint][:2]
    weights = copy.copy(neighbors)
    for ngh in weights:
        weights[ngh] = [1.0] * len(weights[ngh])
    return pysal.weights.W(neighbors, weights), id2link
Exemplo n.º 4
0
def dist_weights(network, id2linkpoints, link2id, bandwidth):
    """
    Obtains a distance-based spatial weights matrix using network distance

    Parameters:
        network: an undirected network without additional attributes 
        id2linkpoints: a dictionary that includes a list of network-projected, midpoints of edges in the network
        link2id: a dictionary that associates each edge to a unique id
        bandwidth: a threshold distance for creating a spatial weights matrix

    Returns:
        w : a distance-based, binary spatial weights matrix
        id2link: a dictionary that associates a unique id to each edge of the network
    """
    linkpoints = id2linkpoints.values()
    neighbors, id2link = {}, {}
    net_distances = {}
    for linkpoint in id2linkpoints:
        if linkpoints[linkpoint] not in net_distances:
            net_distances[linkpoints[linkpoint][0]] = pynet.dijkstras(network, linkpoints[linkpoint][0], r=bandwidth)
            net_distances[linkpoints[linkpoint][1]] = pynet.dijkstras(network, linkpoints[linkpoint][1], r=bandwidth)
        ngh = pynet.proj_distances_undirected(network, linkpoints[linkpoint], linkpoints, r=bandwidth, cache=net_distances)
        #ngh = pynet.proj_distances_undirected(network, linkpoints[linkpoint], linkpoints, r=bandwidth)
        if linkpoints[linkpoint] in ngh:
            del ngh[linkpoints[linkpoint]]
        if linkpoint not in neighbors:
            neighbors[linkpoint] = []
        for k in ngh.keys():
            neighbor = link2id[k[:2]]
            if neighbor not in neighbors[linkpoint]:
                neighbors[linkpoint].append(neighbor)
            if neighbor not in neighbors:
                neighbors[neighbor] = []
            if linkpoint not in neighbors[neighbor]:
                neighbors[neighbor].append(linkpoint)
        id2link[linkpoint] = id2linkpoints[linkpoint][:2]
    weights = copy.copy(neighbors)
    for ngh in weights:
        weights[ngh] = [1.0]*len(weights[ngh])
    return pysal.weights.W(neighbors, weights), id2link