Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add isochrone and silhouette tests #392

Merged
merged 3 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 32 additions & 19 deletions geosnap/tests/test_diagnostics.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
from geosnap.io import get_census
from geosnap import DataStore
from geosnap.analyze import find_k, find_region_k
from geosnap.analyze import find_k, find_region_k, cluster, regionalize
from numpy.testing import assert_array_equal, assert_array_almost_equal

reno = get_census(msa_fips="39900", datastore=DataStore(), years=[2010])
columns = [
"median_household_income",
"p_poverty_rate",
"p_unemployment_rate",
]

def test_find_k():
reno = get_census(msa_fips="39900", datastore=DataStore(), years=[2010])
columns = [
"median_household_income",
"p_poverty_rate",
"p_unemployment_rate",
]

def test_find_k():
ks = find_k(
reno,
columns=columns,
Expand All @@ -20,17 +20,10 @@ def test_find_k():
)
# Aspatial Clusters

assert_array_almost_equal( ks.T.values[0], [2,2,2])

def test_find_region_k():

reno = get_census(msa_fips="39900", datastore=DataStore(), years=[2010])
columns = [
"median_household_income",
"p_poverty_rate",
"p_unemployment_rate",
]
assert_array_almost_equal(ks.T.values[0], [2, 2, 2])


def test_find_region_k():
ks = find_region_k(
reno,
columns=columns,
Expand All @@ -39,4 +32,24 @@ def test_find_region_k():
)
# Aspatial Clusters

assert_array_almost_equal( ks.values[0],[2.,2.,2.,2.,2.])
assert_array_almost_equal(ks.values[0], [2.0, 2.0, 2.0, 2.0, 2.0])


def test_cluster_diagnostics():
ward, ward_mod = cluster(
reno, columns=columns, method="ward", n_clusters=5, return_model=True
)
assert ward_mod.silhouette_score.round(4) == 0.2991
assert ward_mod.davies_bouldin_score.round(4) == 1.0336
assert ward_mod.calinski_harabasz_score.round(4) == 88.6627


def test_region_diagnostics():
ward, ward_mod = regionalize(
reno, columns=columns, method="ward_spatial", n_clusters=5, return_model=True
)
assert ward_mod[2010].boundary_silhouette.boundary_silhouette.mean().round(4) == 0.2076
assert ward_mod[2010].path_silhouette.path_silhouette.mean().round(4) == -0.0801
assert ward_mod[2010].silhouette_scores.silhouette_score.mean().round(4) ==0.063
assert ward_mod[2010].nearest_label.nearest_label.head().tolist() == [1, 2, 0, 2, 4]

42 changes: 42 additions & 0 deletions geosnap/tests/test_isochrones.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from geosnap.analyze import (
isochrones_from_id,
isochrones_from_gdf,
)
from geosnap.io import get_acs
from geosnap import DataStore

import pandana as pdna
import geopandas as gpd
import os

if not os.path.exists("./41740.h5"):
import quilt3 as q3

b = q3.Bucket("s3://spatial-ucr")
b.fetch("osm/metro_networks_8k/41740.h5", "./41740.h5")

datasets = DataStore()
sd_tracts = get_acs(datasets, county_fips="06073", years=[2018])
sd_network = pdna.Network.from_hdf5("41740.h5")
example_origin = 1985327805


def test_isos_from_ids():
iso = isochrones_from_id(example_origin, sd_network, threshold=1600)
assert iso.area.round(6).tolist()[0] == 0.000128


def test_isos_from_gdf():
sd_network.nodes_df["geometry"] = gpd.points_from_xy(
sd_network.nodes_df.x, sd_network.nodes_df.y
)
example_point = gpd.GeoDataFrame(
sd_network.nodes_df.loc[example_origin]
).T.set_geometry("geometry")
example_point = example_point.set_crs(4326)
t = isochrones_from_gdf(
origins=example_point,
network=sd_network,
threshold=1600,
)
assert t.area.round(8).tolist()[0] == 0.00012821
Loading