Gtfs2vec embedder
In [1]:
Copied!
import geopandas as gpd
import pandas as pd
from pytorch_lightning import seed_everything
from shapely.geometry import Polygon
from srai.constants import REGIONS_INDEX
from srai.embedders import GTFS2VecEmbedder
import geopandas as gpd
import pandas as pd
from pytorch_lightning import seed_everything
from shapely.geometry import Polygon
from srai.constants import REGIONS_INDEX
from srai.embedders import GTFS2VecEmbedder
Example on artificial data¶
Define features and regions¶
In [2]:
Copied!
features_gdf = gpd.GeoDataFrame(
{
"trip_count_at_6": [1, 0, 0],
"trip_count_at_7": [1, 1, 0],
"trip_count_at_8": [0, 0, 1],
"directions_at_6": [
{"A", "A1"},
{"B", "B1"},
{"C"},
],
},
geometry=gpd.points_from_xy([1, 2, 5], [1, 2, 2]),
index=pd.Index(name="stop_id", data=[1, 2, 3]),
)
features_gdf
features_gdf = gpd.GeoDataFrame(
{
"trip_count_at_6": [1, 0, 0],
"trip_count_at_7": [1, 1, 0],
"trip_count_at_8": [0, 0, 1],
"directions_at_6": [
{"A", "A1"},
{"B", "B1"},
{"C"},
],
},
geometry=gpd.points_from_xy([1, 2, 5], [1, 2, 2]),
index=pd.Index(name="stop_id", data=[1, 2, 3]),
)
features_gdf
Out[2]:
trip_count_at_6 | trip_count_at_7 | trip_count_at_8 | directions_at_6 | geometry | |
---|---|---|---|---|---|
stop_id | |||||
1 | 1 | 1 | 0 | {A1, A} | POINT (1 1) |
2 | 0 | 1 | 0 | {B1, B} | POINT (2 2) |
3 | 0 | 0 | 1 | {C} | POINT (5 2) |
In [3]:
Copied!
regions_gdf = gpd.GeoDataFrame(
geometry=[
Polygon([(0, 0), (0, 3), (3, 3), (3, 0)]),
Polygon([(4, 0), (4, 3), (7, 3), (7, 0)]),
Polygon([(8, 0), (8, 3), (11, 3), (11, 0)]),
],
index=pd.Index(name=REGIONS_INDEX, data=["ff1", "ff2", "ff3"]),
)
regions_gdf
regions_gdf = gpd.GeoDataFrame(
geometry=[
Polygon([(0, 0), (0, 3), (3, 3), (3, 0)]),
Polygon([(4, 0), (4, 3), (7, 3), (7, 0)]),
Polygon([(8, 0), (8, 3), (11, 3), (11, 0)]),
],
index=pd.Index(name=REGIONS_INDEX, data=["ff1", "ff2", "ff3"]),
)
regions_gdf
Out[3]:
geometry | |
---|---|
region_id | |
ff1 | POLYGON ((0 0, 0 3, 3 3, 3 0, 0 0)) |
ff2 | POLYGON ((4 0, 4 3, 7 3, 7 0, 4 0)) |
ff3 | POLYGON ((8 0, 8 3, 11 3, 11 0, 8 0)) |
In [4]:
Copied!
ax = regions_gdf.plot()
features_gdf.plot(ax=ax, color="red")
ax = regions_gdf.plot()
features_gdf.plot(ax=ax, color="red")
Out[4]:
<Axes: >
In [5]:
Copied!
joint_gdf = gpd.GeoDataFrame()
joint_gdf.index = pd.MultiIndex.from_tuples(
[("ff1", 1), ("ff1", 2), ("ff2", 3)],
names=[REGIONS_INDEX, "stop_id"],
)
joint_gdf
joint_gdf = gpd.GeoDataFrame()
joint_gdf.index = pd.MultiIndex.from_tuples(
[("ff1", 1), ("ff1", 2), ("ff2", 3)],
names=[REGIONS_INDEX, "stop_id"],
)
joint_gdf
Out[5]:
region_id | stop_id |
---|---|
ff1 | 1 |
2 | |
ff2 | 3 |
Get features without embedding them¶
In [6]:
Copied!
embedder = GTFS2VecEmbedder(skip_autoencoder=True)
res = embedder.transform(regions_gdf, features_gdf, joint_gdf)
res
embedder = GTFS2VecEmbedder(skip_autoencoder=True)
res = embedder.transform(regions_gdf, features_gdf, joint_gdf)
res
Out[6]:
directions_at_6 | |
---|---|
region_id | |
ff1 | 1.00 |
ff2 | 0.25 |
ff3 | 0.00 |