<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).**---------------- Quickstart Creating and visualizing a `spaghetti.Network` object**Author: <NAME>** ******This notebook is a basic walk-through for:**1. Instantiating a network2. Allocating observations to a network (snapping points)3. Visualizing the original and network-snapped locations with `geopandas` and `matplotlib`<jupyter_code>%load_ext watermark
%watermark
import geopandas
import libpysal
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.lines as mlines
import matplotlib_scalebar
from matplotlib_scalebar.scalebar import ScaleBar
import spaghetti
%matplotlib inline
%watermark -w
%watermark -iv
try:
    from IPython.display import set_matplotlib_formats
    set_matplotlib_formats("retina")
except ImportError:
    pass<jupyter_output><empty_output><jupyter_text>------------------------------ 1. Instantiating a `spaghetti.Network` object Instantiate the network from a `.shp` file<jupyter_code>ntw = spaghetti.Network(in_data=libpysal.examples.get_path("streets.shp"))<jupyter_output><empty_output><jupyter_text>------------------------------ 2. Allocating observations (snapping points) to a network: Schools without attributes<jupyter_code>ntw.snapobservations(
    libpysal.examples.get_path("schools.shp"), "schools", attribute=False
)<jupyter_output><empty_output><jupyter_text>True vs. snapped school coordinates comparison:  `spaghetti.Network` attributes<jupyter_code>print("observation 1\ntrue coords:\t%s\nsnapped coords:\t%s" % (
    ntw.pointpatterns["schools"].points[0]["coordinates"],
    ntw.pointpatterns["schools"].snapped_coordinates[0]
))<jupyter_output>observation 1
true coords:	(727082.0462136, 879863.260705768)
snapped coords:	(727287.6644417326, 879867.3863186113)<jupyter_text>Crimes with attributes<jupyter_code>ntw.snapobservations(
    libpysal.examples.get_path("crimes.shp"), "crimes", attribute=True
)<jupyter_output><empty_output><jupyter_text>True vs. snapped crime coordinates comparison: `spaghetti.Network` attributes<jupyter_code>print("observation 1\ntrue coords:\t%s\nsnapped coords:\t%s" % (
    ntw.pointpatterns["crimes"].points[0]["coordinates"],
    ntw.pointpatterns["crimes"].snapped_coordinates[0]
))<jupyter_output>observation 1
true coords:	(727913.0000000029, 875720.9999999977)
snapped coords:	(727919.2473619275, 875942.4986759046)<jupyter_text>------------------------------ 3. Visualizing original and snapped locations True and snapped school locations<jupyter_code>true_schools_df = spaghetti.element_as_gdf(
    ntw, pp_name="schools", snapped=False
)
snapped_schools_df = spaghetti.element_as_gdf(
    ntw, pp_name="schools", snapped=True
)<jupyter_output><empty_output><jupyter_text>True vs. snapped school coordinates comparison: `geopandas.GeoDataFrame`<jupyter_code># Compare true point coordinates & snapped point coordinates
print("observation 1\ntrue coords:\t%s\nsnapped coords:\t%s" % (
    true_schools_df.geometry[0].coords[:][0],
    snapped_schools_df.geometry[0].coords[:][0]
))<jupyter_output>observation 1
true coords:	(727082.0462136, 879863.260705768)
snapped coords:	(727287.6644417326, 879867.3863186113)<jupyter_text>True and snapped crime locations<jupyter_code>true_crimes_df = spaghetti.element_as_gdf(
    ntw, pp_name="crimes", snapped=False
)
snapped_crimes_df = spaghetti.element_as_gdf(
    ntw, pp_name="crimes", snapped=True
)<jupyter_output><empty_output><jupyter_text>True vs. snapped crime coordinates comparison: `geopandas.GeoDataFrame`<jupyter_code>print("observation 1\ntrue coords:\t%s\nsnapped coords:\t%s" % (
    true_crimes_df.geometry[0].coords[:][0],
    snapped_crimes_df.geometry[0].coords[:][0]
))<jupyter_output>observation 1
true coords:	(727913.0000000029, 875720.9999999977)
snapped coords:	(727919.2473619275, 875942.4986759046)<jupyter_text>Create `geopandas.GeoDataFrame` objects of the vertices and arcs<jupyter_code># network nodes and edges
vertices_df, arcs_df = spaghetti.element_as_gdf(ntw, vertices=True, arcs=True)<jupyter_output><empty_output><jupyter_text>Create legend patches for the `matplotlib` plot<jupyter_code># create legend arguments and keyword arguments for matplotlib
args = [], []
kwargs = {"c":"k"}

# set arcs legend entry
arcs = mlines.Line2D(*args, **kwargs, label="Network Arcs", alpha=0.5)

# update keyword arguments for matplotlib
kwargs.update({"lw":0})

# set vertices legend entry
vertices = mlines.Line2D(
    *args, **kwargs, ms=2.5, marker="o", label="Network Vertices"
)
# set true school locations legend entry
tschools = mlines.Line2D(
    *args, **kwargs, ms=25, marker="X", label="School Locations"
)
# set network-snapped school locations legend entry
sschools = mlines.Line2D(
    *args, **kwargs, ms=12, marker="o", label="Snapped Schools"
)
# update keyword arguments for matplotlib
kwargs.update({"c":"r", "alpha":0.75})

# set true crimes locations legend entry
tcrimes = mlines.Line2D(
    *args, **kwargs, ms=7, marker="x", label="Crime Locations"
)
# set network-snapped crimes locations legend entry
scrimes = mlines.Line2D(
    *args, **kwargs, ms=3, marker="o", label="Snapped Crimes"
)
# combine all legend patches
patches = [arcs, vertices, tschools, sschools, tcrimes, scrimes]<jupyter_output><empty_output><jupyter_text>Plotting `geopandas.GeoDataFrame` objects<jupyter_code># set the streets as the plot base
base = arcs_df.plot(color="k", alpha=0.25, figsize=(12, 12), zorder=0)

# create vertices keyword arguments for matplotlib
kwargs = {"ax":base}
vertices_df.plot(color="k", markersize=5, alpha=1, **kwargs)

# update crime keyword arguments for matplotlib
kwargs.update({"alpha":0.5, "zorder":1})
true_crimes_df.plot(color="r", marker="x", markersize=50, **kwargs)
snapped_crimes_df.plot(color="r", markersize=20, **kwargs)

# update schools keyword arguments for matplotlib
kwargs.update({"cmap":"tab20", "column":"id", "zorder":2})
true_schools_df.plot(marker="X", markersize=500, **kwargs)
snapped_schools_df.plot(markersize=200, **kwargs)

# add scale bar
kw = {"units":"ft", "dimension":"imperial-length", "fixed_value":1000}
base.add_artist(ScaleBar(1, location="lower left", box_alpha=.75, **kw))

# add legend
plt.legend(
    handles=patches,
    fancybox=True,
    framealpha=0.8,
    scatterpoints=1,
    fontsize="xx-large",
    bbox_to_anchor=(1.04, 0.75),
    borderpad=2.,
    labelspacing=2.
);<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14559853792263175, 'fraction_numerical': 0.05147730734084679, 'mean_word_length': 6.248344370860927, 'pattern_counts': {'":': 13, '<': 50, '<?xml version=': 0, '>': 50, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '21487', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 2524, 'n_tokens_neox': 2293, 'n_words': 472, 'path': 'notebooks/quickstart.ipynb', 'repo_name': 'omar-3/spaghetti', 'size': '6577'}