<jupyter_start><jupyter_text>A network is composed of a single topological representation of a road and $n$ point patterns which are snapped to the network.<jupyter_code>ntw.pointpatterns
dir(ntw.pointpatterns['crimes'])<jupyter_output><empty_output><jupyter_text>Attributes for every point pattern1. dist_to_node dict keyed by pointid with the value being a dict in the form {node: distance to node, node: distance to node}2. obs_to_edge dict keyed by edge with the value being a dict in the form {pointID:(x-coord, y-coord), pointID:(x-coord, y-coord), ... }3. obs_to_node4. points geojson like representation of the point pattern.  Includes properties if read with attributes=True5. snapped_coordinates dict keyed by pointid with the value being (x-coord, y-coord) Counts per edge are important, but should not be precomputed since we have different representations of the network (digitized and graph currently).  (Relatively) Uniform segmentation still needs to be done.<jupyter_code>counts = ntw.count_per_edge(ntw.pointpatterns['crimes'].obs_to_edge,
                            graph=False)
sum(counts.values()) / float(len(counts.keys()))<jupyter_output><empty_output><jupyter_text>Segmentation<jupyter_code>n200 = ntw.segment_edges(200.0)
counts = n200.count_per_edge(n200.pointpatterns['crimes'].obs_to_edge, graph=False)
sum(counts.values()) / float(len(counts.keys()))<jupyter_output><empty_output><jupyter_text>Visualization of the shapefile derived, unsegmented network with nodes in a larger, semi-opaque form and the distance segmented network with small, fully opaque nodes.<jupyter_code>import networkx as nx
figsize(10,10)

g = nx.Graph()
for e in ntw.edges:
    g.add_edge(*e)
for n, p in ntw.node_coords.iteritems():
    g.node[n] = p
nx.draw(g, ntw.node_coords, node_size=300, alpha=0.5)

g = nx.Graph()
for e in n200.edges:
    g.add_edge(*e)
for n, p in n200.node_coords.iteritems():
    g.node[n] = p
nx.draw(g, n200.node_coords, node_size=25, alpha=1.0)<jupyter_output><empty_output><jupyter_text>Moran's I using the digitized network<jupyter_code>#Binary Adjacency
#ntw.contiguityweights(graph=False)

w = ntw.contiguityweights(graph=False)


#Build the y vector
#edges = ntw.w.neighbors.keys()
edges = w.neighbors.keys()
y = np.zeros(len(edges))
for i, e in enumerate(edges):
    if e in counts.keys():
        y[i] = counts[e]

#Moran's I
#res = ps.esda.moran.Moran(y, ntw.w, permutations=99)
res = ps.esda.moran.Moran(y, w, permutations=99)
print dir(res)<jupyter_output>['EI', 'EI_sim', 'I', 'VI_norm', 'VI_rand', 'VI_sim', '_Moran__calc', '_Moran__moments', '__doc__', '__init__', '__module__', 'n', 'p_norm', 'p_rand', 'p_sim', 'p_z_sim', 'permutations', 'seI_norm', 'seI_rand', 'seI_sim', 'sim', 'w', 'y', 'z', 'z2ss', 'z_norm', 'z_rand', 'z_sim']<jupyter_text>Moran's I using the graph representation to generate the WNote that we have to regenerate the counts per edge, since the graph will have less edges.<jupyter_code>counts = ntw.count_per_edge(ntw.pointpatterns['crimes'].obs_to_edge, graph=True)

#Binary Adjacency
#ntw.contiguityweights(graph=True)
w = ntw.contiguityweights(graph=True)
#Build the y vector
#edges = ntw.w.neighbors.keys()
edges = w.neighbors.keys()
y = np.zeros(len(edges))
for i, e in enumerate(edges):
    if e in counts.keys():
        y[i] = counts[e]

#Moran's I
#res = ps.esda.moran.Moran(y, ntw.w, permutations=99)
res = ps.esda.moran.Moran(y, w, permutations=99)


print dir(res)<jupyter_output>['EI', 'EI_sim', 'I', 'VI_norm', 'VI_rand', 'VI_sim', '_Moran__calc', '_Moran__moments', '__doc__', '__init__', '__module__', 'n', 'p_norm', 'p_rand', 'p_sim', 'p_z_sim', 'permutations', 'seI_norm', 'seI_rand', 'seI_sim', 'sim', 'w', 'y', 'z', 'z2ss', 'z_norm', 'z_rand', 'z_sim']<jupyter_text>Moran's I using the segmented network and intensities instead of counts<jupyter_code>#Binary Adjacency
#n200.contiguityweights(graph=False)
w = n200.contiguityweights(graph=False)


#Compute the counts
counts = n200.count_per_edge(n200.pointpatterns['crimes'].obs_to_edge, graph=False)

#Build the y vector and convert from raw counts to intensities
#edges = n200.w.neighbors.keys()
edges = w.neighbors.keys()
y = np.zeros(len(edges))
for i, e in enumerate(edges):
    if e in counts.keys():
        length = n200.edge_lengths[e]
        y[i] = counts[e] / length
      
#Moran's I
#res = ps.esda.moran.Moran(y, n200.w, permutations=99)
res = ps.esda.moran.Moran(y, w, permutations=99)


print dir(res)<jupyter_output>['EI', 'EI_sim', 'I', 'VI_norm', 'VI_rand', 'VI_sim', '_Moran__calc', '_Moran__moments', '__doc__', '__init__', '__module__', 'n', 'p_norm', 'p_rand', 'p_sim', 'p_z_sim', 'permutations', 'seI_norm', 'seI_rand', 'seI_sim', 'sim', 'w', 'y', 'z', 'z2ss', 'z_norm', 'z_rand', 'z_sim']<jupyter_text>Timings for distance based methods, e.g. G-function<jupyter_code>import time
t1 = time.time()
n0 = ntw.allneighbordistances(ntw.pointpatterns['crimes'])
print time.time()-t1
import time
t1 = time.time()
n1 = n200.allneighbordistances(n200.pointpatterns['crimes'])
print time.time()-t1<jupyter_output>11.2402291298<jupyter_text>Note that the first time these methods are called, the underlying node-to-node shortest path distance matrix has to be calculated. Subsequent calls will not require this, and will be much faster:<jupyter_code>import time
t1 = time.time()
n0 = ntw.allneighbordistances(ntw.pointpatterns['crimes'])
print time.time()-t1
import time
t1 = time.time()
n1 = n200.allneighbordistances(n200.pointpatterns['crimes'])
print time.time()-t1<jupyter_output>0.160092830658<jupyter_text>Simulate a point pattern on the networkNeed to supply a count of the number of points and a distirbution (default is uniform).  Generally this will not be called by the user, since the simulation will be used for Monte Carlo permutation.<jupyter_code>npts = ntw.pointpatterns['crimes'].npoints
sim = ntw.simulate_observations(npts)
sim<jupyter_output><empty_output><jupyter_text>Create a nearest neighbor matrix using the crimes point patternRight now, both the G and K functions generate a full distance matrix.  This is because, I know that the full generation is correct and I believe that the truncated generated, e.g. nearest neighbor, has a big.  G-function<jupyter_code>gres = ps.NetworkG(ntw,
                         ntw.pointpatterns['crimes'],
                         permutations = 99)
figsize(5,5)
plot(gres.xaxis, gres.observed, 'b-', linewidth=1.5, label='Observed')
plot(gres.xaxis, gres.upperenvelope, 'r--', label='Upper')
plot(gres.xaxis, gres.lowerenvelope, 'k--', label='Lower')
legend(loc='best')<jupyter_output><empty_output><jupyter_text>K-function<jupyter_code>kres = ps.NetworkK(ntw,
                         ntw.pointpatterns['crimes'],
                         permutations=99)
figsize(5,5)
plot(kres.xaxis, kres.observed, 'b-', linewidth=1.5, label='Observed')
plot(kres.xaxis, kres.upperenvelope, 'r--', label='Upper')
plot(kres.xaxis, kres.lowerenvelope, 'k--', label='Lower')
legend(loc='best')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14090520922288644, 'fraction_numerical': 0.01949900370054085, 'mean_word_length': 5.49445471349353, 'pattern_counts': {'":': 0, '<': 44, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '98835', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 2726, 'n_tokens_neox': 2567, 'n_words': 641, 'path': 'pysal/network/Network Usage.ipynb', 'repo_name': 'Aniq55/pysal', 'size': '7026'}