Hex2vec embedder
In [1]:
Copied!
from srai.embedders import Hex2VecEmbedder
from srai.joiners import IntersectionJoiner
from srai.loaders import OSMOnlineLoader
from srai.neighbourhoods import H3Neighbourhood
from srai.regionalizers import H3Regionalizer
from srai.utils import geocode_to_region_gdf
from srai.plotting import plot_regions, plot_numeric_data
from pytorch_lightning import seed_everything
from srai.embedders import Hex2VecEmbedder
from srai.joiners import IntersectionJoiner
from srai.loaders import OSMOnlineLoader
from srai.neighbourhoods import H3Neighbourhood
from srai.regionalizers import H3Regionalizer
from srai.utils import geocode_to_region_gdf
from srai.plotting import plot_regions, plot_numeric_data
from pytorch_lightning import seed_everything
In [2]:
Copied!
SEED = 71
seed_everything(SEED)
SEED = 71
seed_everything(SEED)
Global seed set to 71
Out[2]:
71
Load data from OSM¶
First use geocoding to get the area
In [3]:
Copied!
area_gdf = geocode_to_region_gdf("Wrocław, Poland")
plot_regions(area_gdf, tiles_style="CartoDB positron")
area_gdf = geocode_to_region_gdf("Wrocław, Poland")
plot_regions(area_gdf, tiles_style="CartoDB positron")
Out[3]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Next, download the data for the selected region and the specified tags. We're using OSMOnlineLoader
here, as it's faster for low numbers of tags. In a real life scenario with more tags, you would likely want to use the OSMPbfLoader
.
In [4]:
Copied!
tags = {
"leisure": "park",
"landuse": "forest",
"amenity": ["bar", "restaurant", "cafe"],
"water": "river",
"sport": "soccer",
}
loader = OSMOnlineLoader()
features_gdf = loader.load(area_gdf, tags)
folium_map = plot_regions(area_gdf, colormap=["rgba(0,0,0,0)"], tiles_style="CartoDB positron")
features_gdf.explore(m=folium_map)
tags = {
"leisure": "park",
"landuse": "forest",
"amenity": ["bar", "restaurant", "cafe"],
"water": "river",
"sport": "soccer",
}
loader = OSMOnlineLoader()
features_gdf = loader.load(area_gdf, tags)
folium_map = plot_regions(area_gdf, colormap=["rgba(0,0,0,0)"], tiles_style="CartoDB positron")
features_gdf.explore(m=folium_map)
Downloading sport: soccer : 100%|██████████| 7/7 [00:02<00:00, 2.84it/s]
Out[4]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Prepare the data for embedding¶
After downloading the data, we need to prepare it for embedding. Namely - we need to regionalize the selected area, and join the features with regions.
In [5]:
Copied!
regionalizer = H3Regionalizer(resolution=9)
regions_gdf = regionalizer.transform(area_gdf)
plot_regions(regions_gdf, tiles_style="CartoDB positron")
regionalizer = H3Regionalizer(resolution=9)
regions_gdf = regionalizer.transform(area_gdf)
plot_regions(regions_gdf, tiles_style="CartoDB positron")
Out[5]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [6]:
Copied!
joiner = IntersectionJoiner()
joint_gdf = joiner.transform(regions_gdf, features_gdf)
joint_gdf
joiner = IntersectionJoiner()
joint_gdf = joiner.transform(regions_gdf, features_gdf)
joint_gdf
Out[6]:
region_id | feature_id |
---|---|
891e204304bffff | relation/8833861 |
891e204304fffff | relation/8833861 |
891e2043047ffff | relation/8833861 |
891e204304bffff | way/346555876 |
891e2043043ffff | way/346555876 |
... | ... |
891e2047547ffff | way/772238372 |
way/772238371 | |
way/772238373 | |
891e2045537ffff | node/6780053133 |
way/236429476 |
3778 rows × 0 columns
Embedding¶
After preparing the data we can proceed with generating embeddings for the regions.
In [7]:
Copied!
import warnings
neighbourhood = H3Neighbourhood(regions_gdf)
embedder = Hex2VecEmbedder([15, 10])
with warnings.catch_warnings():
warnings.simplefilter("ignore")
embeddings = embedder.fit_transform(
regions_gdf,
features_gdf,
joint_gdf,
neighbourhood,
trainer_kwargs={"max_epochs": 5, "accelerator": "cpu"},
batch_size=100,
)
embeddings
import warnings
neighbourhood = H3Neighbourhood(regions_gdf)
embedder = Hex2VecEmbedder([15, 10])
with warnings.catch_warnings():
warnings.simplefilter("ignore")
embeddings = embedder.fit_transform(
regions_gdf,
features_gdf,
joint_gdf,
neighbourhood,
trainer_kwargs={"max_epochs": 5, "accelerator": "cpu"},
batch_size=100,
)
embeddings
100%|██████████| 3168/3168 [00:00<00:00, 22537.17it/s] GPU available: False, used: False TPU available: False, using: 0 TPU cores IPU available: False, using: 0 IPUs HPU available: False, using: 0 HPUs Missing logger folder: /home/runner/work/srai/srai/examples/embedders/lightning_logs | Name | Type | Params --------------------------------------- 0 | encoder | Sequential | 280 --------------------------------------- 280 Trainable params 0 Non-trainable params 280 Total params 0.001 Total estimated model params size (MB)
`Trainer.fit` stopped: `max_epochs=5` reached.
Out[7]:
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | |
---|---|---|---|---|---|---|---|---|---|---|
region_id | ||||||||||
891e2040ec3ffff | 0.334089 | -0.291687 | -0.000071 | 0.276342 | 0.182729 | -0.121621 | -0.276749 | -0.103540 | -0.204520 | 0.302638 |
891e204769bffff | 0.334089 | -0.291687 | -0.000071 | 0.276342 | 0.182729 | -0.121621 | -0.276749 | -0.103540 | -0.204520 | 0.302638 |
891e2045017ffff | 0.334089 | -0.291687 | -0.000071 | 0.276342 | 0.182729 | -0.121621 | -0.276749 | -0.103540 | -0.204520 | 0.302638 |
891e204304bffff | -0.665747 | 0.135241 | -0.414056 | -1.090132 | -0.105327 | 0.393818 | 0.001832 | 0.582357 | -0.185385 | -0.446995 |
891e204082fffff | 0.334089 | -0.291687 | -0.000071 | 0.276342 | 0.182729 | -0.121621 | -0.276749 | -0.103540 | -0.204520 | 0.302638 |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
891e20452cbffff | 0.334089 | -0.291687 | -0.000071 | 0.276342 | 0.182729 | -0.121621 | -0.276749 | -0.103540 | -0.204520 | 0.302638 |
891e20402c7ffff | 0.334089 | -0.291687 | -0.000071 | 0.276342 | 0.182729 | -0.121621 | -0.276749 | -0.103540 | -0.204520 | 0.302638 |
891e2042ebbffff | -0.316712 | 0.105574 | -0.301756 | -0.492271 | 0.036685 | 0.233540 | -0.301246 | 0.326174 | -0.085026 | 0.006382 |
891e204262fffff | 0.334089 | -0.291687 | -0.000071 | 0.276342 | 0.182729 | -0.121621 | -0.276749 | -0.103540 | -0.204520 | 0.302638 |
891e2042357ffff | -0.618911 | 0.607905 | 0.091841 | -0.111167 | -0.364905 | 0.245106 | 0.198321 | 0.026477 | 0.398948 | -0.180863 |
3168 rows × 10 columns
Visualizing the embeddings' similarity¶
In [8]:
Copied!
from sklearn.cluster import KMeans
clusterizer = KMeans(n_clusters=5, random_state=SEED)
clusterizer.fit(embeddings)
embeddings["cluster"] = clusterizer.labels_
embeddings
from sklearn.cluster import KMeans
clusterizer = KMeans(n_clusters=5, random_state=SEED)
clusterizer.fit(embeddings)
embeddings["cluster"] = clusterizer.labels_
embeddings
/opt/hostedtoolcache/Python/3.10.12/x64/lib/python3.10/site-packages/sklearn/cluster/_kmeans.py:870: FutureWarning: The default value of `n_init` will change from 10 to 'auto' in 1.4. Set the value of `n_init` explicitly to suppress the warning warnings.warn(
Out[8]:
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | cluster | |
---|---|---|---|---|---|---|---|---|---|---|---|
region_id | |||||||||||
891e2040ec3ffff | 0.334089 | -0.291687 | -0.000071 | 0.276342 | 0.182729 | -0.121621 | -0.276749 | -0.103540 | -0.204520 | 0.302638 | 1 |
891e204769bffff | 0.334089 | -0.291687 | -0.000071 | 0.276342 | 0.182729 | -0.121621 | -0.276749 | -0.103540 | -0.204520 | 0.302638 | 1 |
891e2045017ffff | 0.334089 | -0.291687 | -0.000071 | 0.276342 | 0.182729 | -0.121621 | -0.276749 | -0.103540 | -0.204520 | 0.302638 | 1 |
891e204304bffff | -0.665747 | 0.135241 | -0.414056 | -1.090132 | -0.105327 | 0.393818 | 0.001832 | 0.582357 | -0.185385 | -0.446995 | 2 |
891e204082fffff | 0.334089 | -0.291687 | -0.000071 | 0.276342 | 0.182729 | -0.121621 | -0.276749 | -0.103540 | -0.204520 | 0.302638 | 1 |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
891e20452cbffff | 0.334089 | -0.291687 | -0.000071 | 0.276342 | 0.182729 | -0.121621 | -0.276749 | -0.103540 | -0.204520 | 0.302638 | 1 |
891e20402c7ffff | 0.334089 | -0.291687 | -0.000071 | 0.276342 | 0.182729 | -0.121621 | -0.276749 | -0.103540 | -0.204520 | 0.302638 | 1 |
891e2042ebbffff | -0.316712 | 0.105574 | -0.301756 | -0.492271 | 0.036685 | 0.233540 | -0.301246 | 0.326174 | -0.085026 | 0.006382 | 2 |
891e204262fffff | 0.334089 | -0.291687 | -0.000071 | 0.276342 | 0.182729 | -0.121621 | -0.276749 | -0.103540 | -0.204520 | 0.302638 | 1 |
891e2042357ffff | -0.618911 | 0.607905 | 0.091841 | -0.111167 | -0.364905 | 0.245106 | 0.198321 | 0.026477 | 0.398948 | -0.180863 | 2 |
3168 rows × 11 columns
In [9]:
Copied!
plot_numeric_data(regions_gdf, embeddings, "cluster", tiles_style="CartoDB positron")
plot_numeric_data(regions_gdf, embeddings, "cluster", tiles_style="CartoDB positron")
Out[9]:
Make this Notebook Trusted to load map: File -> Trust Notebook