<jupyter_start><jupyter_text>---------------------------------------  If any part of this notebook is used in your research, please cite with the reference found in [`README.md`](https://github.com/pysal/spaghettibibtex-citation). --------------------------------------  $SPA$tial $G$rap$H$s: n$ET$works, $T$opology, & $I$nference Tutorial for `pysal.spaghetti`: Working with point patterns: empirical observations James D. Gaboardi [] 1. Instantiating a `pysal.spaghetti.Network`2. Allocating observations to a network    * snapping3. Visualizing original and snapped locations    * visualization with `geopandas` and `matplotlib`<jupyter_code>import os
last_modified = None
if os.name == "posix":
    last_modified = !stat -f\
                    "# This notebook was last updated: %Sm"\
                     Spaghetti_Pointpatterns_Empirical.ipynb
elif os.name == "nt":
    last_modified = !for %a in (Spaghetti_Pointpatterns_Empirical.ipynb)\
                    do echo # This notebook was last updated: %~ta
    
if last_modified:
    get_ipython().set_next_input(last_modified[-1])
# This notebook was last updated: Nov 12 12:32:17 2019<jupyter_output><empty_output><jupyter_text>-----------------<jupyter_code>import spaghetti as spgh
from libpysal import examples
import geopandas as gpd
import matplotlib.pyplot as plt
import matplotlib.lines as mlines
from shapely.geometry import Point, LineString

%matplotlib inline

__author__ = "<NAME> <<EMAIL>>"<jupyter_output><empty_output><jupyter_text>1. Instantiating a `pysal.spaghetti.Network` Instantiate the network from `.shp` file<jupyter_code>ntw = spgh.Network(in_data=examples.get_path('streets.shp'))<jupyter_output><empty_output><jupyter_text>2. Allocating observations to a network Snap point patterns to the network<jupyter_code># Crimes with attributes
ntw.snapobservations(
    examples.get_path('crimes.shp'), 'crimes', attribute=True
)

# Schools without attributes
ntw.snapobservations(
    examples.get_path('schools.shp'), 'schools', attribute=False
)<jupyter_output><empty_output><jupyter_text>3. Visualizing original and snapped locations  True and snapped school locations<jupyter_code>schools_df = spgh.element_as_gdf(ntw, pp_name='schools', snapped=False)

snapped_schools_df = spgh.element_as_gdf(ntw, pp_name='schools', snapped=True)<jupyter_output><empty_output><jupyter_text>True and snapped crime locations<jupyter_code>crimes_df = spgh.element_as_gdf(ntw, pp_name='crimes', snapped=False)

snapped_crimes_df = spgh.element_as_gdf(ntw, pp_name='crimes', snapped=True)<jupyter_output><empty_output><jupyter_text>Create `geopandas.GeoDataFrame` objects of the vertices and arcs<jupyter_code># network nodes and edges
vertices_df, arcs_df = spgh.element_as_gdf(ntw, vertices=True, arcs=True)<jupyter_output><empty_output><jupyter_text>Plotting `geopandas.GeoDataFrame` objects<jupyter_code># legend patches
arcs = mlines.Line2D([], [], c='k', label='Network Arcs', alpha=.5)
vtxs = mlines.Line2D(
    [], [], c='k', lw=0, ms=2.5, marker='o', label='Network Vertices'
)
schl = mlines.Line2D(
    [], [], c='k', lw=0, ms=25, marker='X', label='School Locations'
)
snp_schl = mlines.Line2D(
    [], [], c='k', lw=0, ms=12, marker='o', label='Snapped Schools'
)
crme = mlines.Line2D(
    [], [], c='r', lw=0, ms=7, marker='x', label='Crime Locations', alpha=.75
)
snp_crme = mlines.Line2D(
    [], [], c='r', lw=0, ms=3, marker='o', label='Snapped Crimes', alpha=.75
)
patches = [arcs, vtxs, schl, snp_schl, crme, snp_crme]
# plot figure
base = arcs_df.plot(color='k', alpha=.25, figsize=(12,12), zorder=0)
vertices_df.plot(ax=base, color='k', markersize=5, alpha=1)
crimes_df.plot(
    ax=base, color='r', marker='x', markersize=50, alpha=.5, zorder=1
)
snapped_crimes_df.plot(
    ax=base, color='r', markersize=20, alpha=.5, zorder=1
)
schools_df.plot(
    ax=base, cmap='tab20', column='id', marker='X', markersize=500, alpha=.5, zorder=2
)
snapped_schools_df.plot(
    ax=base,cmap='tab20', column='id', markersize=200, alpha=.5, zorder=2
)

# add legend
plt.legend(
    handles=patches,
    fancybox=True,
    framealpha=0.8,
    scatterpoints=1,
    fontsize="xx-large",
    bbox_to_anchor=(1.04, .6)
);<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15979137031768612, 'fraction_numerical': 0.019203413940256046, 'mean_word_length': 5.018544935805991, 'pattern_counts': {'":': 2, '<': 36, '<?xml version=': 0, '>': 36, 'https://': 1, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '566285', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1595, 'n_tokens_neox': 1535, 'n_words': 318, 'path': 'notebooks/Spaghetti_Pointpatterns_Empirical.ipynb', 'repo_name': 'rahul799/spaghetti', 'size': '4238'}