Index
This module contains embedders, used to convert spatial data to their vector representations.
Embedders are designed to unify different types of spatial data embedding methods, such as hex2vec or gtfs2vec into a single interface. This allows to easily switch between different embedding methods without changing the rest of the code.
¶
Bases: ABC
Abstract class for embedders.
¶
abstractmethod
Embed regions using features.
PARAMETER | DESCRIPTION |
---|---|
regions_gdf |
Region indexes and geometries.
TYPE:
|
features_gdf |
Feature indexes, geometries and feature values.
TYPE:
|
joint_gdf |
Joiner result with region-feature multi-index.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
DataFrame
|
pd.DataFrame: Embedding and geometry index for each region in regions_gdf. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If any of the gdfs index names is None. |
ValueError
|
If joint_gdf.index is not of type pd.MultiIndex or doesn't have 2 levels. |
ValueError
|
If index levels in gdfs don't overlap correctly. |
Source code in srai/embedders/_base.py
¶
Bases: LightningModule
Class for model based on LightningModule.
¶
Get model config.
Source code in srai/embedders/_base.py
¶
Save the model to a directory.
PARAMETER | DESCRIPTION |
---|---|
path |
Path to the directory.
TYPE:
|
¶
classmethod
Load model from a file.
PARAMETER | DESCRIPTION |
---|---|
path |
Path to the file.
TYPE:
|
**kwargs |
Additional kwargs to pass to the model constructor.
TYPE:
|
Source code in srai/embedders/_base.py
ContextualCountEmbedder(
neighbourhood,
neighbourhood_distance,
concatenate_vectors=False,
expected_output_features=None,
count_subcategories=False,
num_of_multiprocessing_workers=-1,
multiprocessing_activation_threshold=None,
)
¶
ContextualCountEmbedder(
neighbourhood,
neighbourhood_distance,
concatenate_vectors=False,
expected_output_features=None,
count_subcategories=False,
num_of_multiprocessing_workers=-1,
multiprocessing_activation_threshold=None,
)
Bases: CountEmbedder
ContextualCountEmbedder.
PARAMETER | DESCRIPTION |
---|---|
neighbourhood |
Neighbourhood object used to get neighbours for the contextualization.
TYPE:
|
neighbourhood_distance |
How many neighbours levels should be included in the embedding.
TYPE:
|
concatenate_vectors |
Whether to sum all neighbours into a single vector
with the same width as
TYPE:
|
count_subcategories |
Whether to count all subcategories individually or count features only on the highest level based on features column name. Defaults to False.
TYPE:
|
num_of_multiprocessing_workers |
Number of workers used for
multiprocessing. Defaults to -1 which results in a total number of available
cpu threads.
TYPE:
|
multiprocessing_activation_threshold |
Number of seeds required to start processing on multiple processes. Activating multiprocessing for a small amount of points might not be feasible. Defaults to 100.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
ValueError
|
If |
Source code in srai/embedders/contextual_count_embedder.py
¶
Embed a given GeoDataFrame.
Creates region embeddings by counting the frequencies of each feature value and applying a contextualization based on neighbours of regions. For each region, features will be altered based on the neighbours either by adding averaged values dimished based on distance, or by adding new separate columns with neighbour distance postfix. Expects features_gdf to be in wide format with each column being a separate type of feature (e.g. amenity, leisure) and rows to hold values of these features for each object. The rows will hold numbers of this type of feature in each region. Numbers can be fractional because neighbourhoods are averaged to represent a single value from all neighbours on a given level.
PARAMETER | DESCRIPTION |
---|---|
regions_gdf |
Region indexes and geometries.
TYPE:
|
features_gdf |
Feature indexes, geometries and feature values.
TYPE:
|
joint_gdf |
Joiner result with region-feature multi-index.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
DataFrame
|
pd.DataFrame: Embedding for each region in regions_gdf. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If features_gdf is empty and self.expected_output_features is not set. |
ValueError
|
If any of the gdfs index names is None. |
ValueError
|
If joint_gdf.index is not of type pd.MultiIndex or doesn't have 2 levels. |
ValueError
|
If index levels in gdfs don't overlap correctly. |
Source code in srai/embedders/contextual_count_embedder.py
¶
Bases: Embedder
Simple Embedder that counts occurences of feature values.
PARAMETER | DESCRIPTION |
---|---|
count_subcategories |
Whether to count all subcategories individually or count features only on the highest level based on features column name. Defaults to True.
TYPE:
|
Source code in srai/embedders/count_embedder.py
¶
Embed a given GeoDataFrame.
Creates region embeddings by counting the frequencies of each feature value. Expects features_gdf to be in wide format with each column being a separate type of feature (e.g. amenity, leisure) and rows to hold values of these features for each object. The resulting DataFrame will have columns made by combining the feature name (column) and value (row) e.g. amenity_fuel or type_0. The rows will hold numbers of this type of feature in each region.
PARAMETER | DESCRIPTION |
---|---|
regions_gdf |
Region indexes and geometries.
TYPE:
|
features_gdf |
Feature indexes, geometries and feature values.
TYPE:
|
joint_gdf |
Joiner result with region-feature multi-index.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
DataFrame
|
pd.DataFrame: Embedding for each region in regions_gdf. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If features_gdf is empty and self.expected_output_features is not set. |
ValueError
|
If any of the gdfs index names is None. |
ValueError
|
If joint_gdf.index is not of type pd.MultiIndex or doesn't have 2 levels. |
ValueError
|
If index levels in gdfs don't overlap correctly. |
Source code in srai/embedders/count_embedder.py
GeoVexEmbedder(
target_features,
batch_size=32,
neighbourhood_radius=4,
convolutional_layers=2,
embedding_size=32,
convolutional_layer_size=256,
)
¶
GeoVexEmbedder(
target_features,
batch_size=32,
neighbourhood_radius=4,
convolutional_layers=2,
embedding_size=32,
convolutional_layer_size=256,
)
Bases: CountEmbedder
GeoVex Embedder.
PARAMETER | DESCRIPTION |
---|---|
target_features |
The features
that are to be used in the embedding. Should be in "flat" format,
i.e. "
TYPE:
|
batch_size |
Batch size. Defaults to 32.
TYPE:
|
convolutional_layers |
Number of convolutional layers. Defaults to 2.
TYPE:
|
neighbourhood_radius |
Radius of the neighbourhood. Defaults to 4.
TYPE:
|
embedding_size |
Size of the embedding. Defaults to 32.
TYPE:
|
convolutional_layer_size |
Size of the first convolutional layer.
TYPE:
|
Source code in srai/embedders/geovex/embedder.py
invalid_cells: list[str]
property
¶
List of invalid h3s.
¶
Create region embeddings.
PARAMETER | DESCRIPTION |
---|---|
regions_gdf |
Region indexes and geometries.
TYPE:
|
features_gdf |
Feature indexes, geometries and feature values.
TYPE:
|
joint_gdf |
Joiner result with region-feature multi-index.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
DataFrame
|
pd.DataFrame: Region embeddings. |
Source code in srai/embedders/geovex/embedder.py
fit(
regions_gdf,
features_gdf,
joint_gdf,
neighbourhood,
learning_rate=0.001,
trainer_kwargs=None,
)
¶
fit(
regions_gdf,
features_gdf,
joint_gdf,
neighbourhood,
learning_rate=0.001,
trainer_kwargs=None,
)
Fit the model to the data.
PARAMETER | DESCRIPTION |
---|---|
regions_gdf |
Region indexes and geometries.
TYPE:
|
features_gdf |
Feature indexes, geometries and feature values.
TYPE:
|
joint_gdf |
Joiner result with region-feature multi-index.
TYPE:
|
neighbourhood |
The neighbourhood to use. Should be intialized with the same regions.
TYPE:
|
learning_rate |
Learning rate. Defaults to 0.001.
TYPE:
|
trainer_kwargs |
Trainer kwargs. This is where the number of epochs can be set. Defaults to None.
TYPE:
|
Source code in srai/embedders/geovex/embedder.py
fit_transform(
regions_gdf,
features_gdf,
joint_gdf,
neighbourhood,
learning_rate=0.001,
trainer_kwargs=None,
)
¶
fit_transform(
regions_gdf,
features_gdf,
joint_gdf,
neighbourhood,
learning_rate=0.001,
trainer_kwargs=None,
)
Fit the model to the data and create region embeddings.
PARAMETER | DESCRIPTION |
---|---|
regions_gdf |
Region indexes and geometries.
TYPE:
|
features_gdf |
Feature indexes, geometries and feature values.
TYPE:
|
joint_gdf |
Joiner result with region-feature multi-index.
TYPE:
|
neighbourhood |
The neighbourhood to use. Should be intialized with the same regions.
TYPE:
|
negative_sample_k_distance |
Distance of negative samples. Defaults to 2.
TYPE:
|
learning_rate |
Learning rate. Defaults to 0.001.
TYPE:
|
trainer_kwargs |
Trainer kwargs. This is where the number of epochs can be set. Defaults to None.
TYPE:
|
Source code in srai/embedders/geovex/embedder.py
¶
Save the model to a directory.
PARAMETER | DESCRIPTION |
---|---|
path |
Path to the directory.
TYPE:
|
Source code in srai/embedders/geovex/embedder.py
¶
classmethod
Load the model from a directory.
PARAMETER | DESCRIPTION |
---|---|
path |
Path to the directory.
TYPE:
|
model_module |
Model class.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
GeoVexEmbedder
|
GeoVexEmbedder object.
TYPE:
|
Source code in srai/embedders/geovex/embedder.py
¶
Bases: Embedder
GTFS2Vec Embedder.
PARAMETER | DESCRIPTION |
---|---|
hidden_size |
Hidden size in encoder and decoder. Defaults to 48.
TYPE:
|
embedding_size |
Embedding size. Defaults to 64.
TYPE:
|
skip_autoencoder |
Skip using autoencoder as part of embedding.
TYPE:
|
Source code in srai/embedders/gtfs2vec/embedder.py
¶
Embed a given data.
PARAMETER | DESCRIPTION |
---|---|
regions_gdf |
Region indexes and geometries.
TYPE:
|
features_gdf |
Feature indexes, geometries and feature values.
TYPE:
|
joint_gdf |
Joiner result with region-feature multi-index.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
DataFrame
|
pd.DataFrame: Embedding and geometry index for each region in regions_gdf. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If any of the gdfs index names is None. |
ValueError
|
If joint_gdf.index is not of type pd.MultiIndex or doesn't have 2 levels. |
ValueError
|
If index levels in gdfs don't overlap correctly. |
ValueError
|
If number of features is incosistent with the model. |
ModelNotFitException
|
If model is not fit. |
Source code in srai/embedders/gtfs2vec/embedder.py
¶
Fit model to a given data.
PARAMETER | DESCRIPTION |
---|---|
regions_gdf |
Region indexes and geometries.
TYPE:
|
features_gdf |
Feature indexes, geometries and feature values.
TYPE:
|
joint_gdf |
Joiner result with region-feature multi-index.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
ValueError
|
If any of the gdfs index names is None. |
ValueError
|
If joint_gdf.index is not of type pd.MultiIndex or doesn't have 2 levels. |
ValueError
|
If index levels in gdfs don't overlap correctly. |
Source code in srai/embedders/gtfs2vec/embedder.py
¶
Fit model and transform a given data.
PARAMETER | DESCRIPTION |
---|---|
regions_gdf |
Region indexes and geometries.
TYPE:
|
features_gdf |
Feature indexes, geometries and feature values.
TYPE:
|
joint_gdf |
Joiner result with region-feature multi-index.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
DataFrame
|
pd.DataFrame: Embedding and geometry index for each region in regions_gdf. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If any of the gdfs index names is None. |
ValueError
|
If joint_gdf.index is not of type pd.MultiIndex or doesn't have 2 levels. |
ValueError
|
If index levels in gdfs don't overlap correctly. |
Source code in srai/embedders/gtfs2vec/embedder.py
¶
Save the model to a directory.
PARAMETER | DESCRIPTION |
---|---|
path |
Path to the directory.
TYPE:
|
Source code in srai/embedders/gtfs2vec/embedder.py
¶
classmethod
Load the model from a directory.
PARAMETER | DESCRIPTION |
---|---|
path |
Path to the directory.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Hex2VecEmbedder
|
The loaded embedder.
TYPE:
|
Source code in srai/embedders/gtfs2vec/embedder.py
¶
Bases: CountEmbedder
Hex2Vec Embedder.
PARAMETER | DESCRIPTION |
---|---|
encoder_sizes |
Sizes of the encoder layers. The input layer size shouldn't be included - it's inferred from the data. The last element is the embedding size. Defaults to [150, 75, 50].
TYPE:
|
Source code in srai/embedders/hex2vec/embedder.py
¶
Create region embeddings.
PARAMETER | DESCRIPTION |
---|---|
regions_gdf |
Region indexes and geometries.
TYPE:
|
features_gdf |
Feature indexes, geometries and feature values.
TYPE:
|
joint_gdf |
Joiner result with region-feature multi-index.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
DataFrame
|
pd.DataFrame: Embedding and geometry index for each region in regions_gdf. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If features_gdf is empty and self.expected_output_features is not set. |
ValueError
|
If any of the gdfs index names is None. |
ValueError
|
If joint_gdf.index is not of type pd.MultiIndex or doesn't have 2 levels. |
ValueError
|
If index levels in gdfs don't overlap correctly. |
Source code in srai/embedders/hex2vec/embedder.py
fit(
regions_gdf,
features_gdf,
joint_gdf,
neighbourhood,
negative_sample_k_distance=2,
batch_size=32,
learning_rate=0.001,
trainer_kwargs=None,
)
¶
fit(
regions_gdf,
features_gdf,
joint_gdf,
neighbourhood,
negative_sample_k_distance=2,
batch_size=32,
learning_rate=0.001,
trainer_kwargs=None,
)
Fit the model to the data.
PARAMETER | DESCRIPTION |
---|---|
regions_gdf |
Region indexes and geometries.
TYPE:
|
features_gdf |
Feature indexes, geometries and feature values.
TYPE:
|
joint_gdf |
Joiner result with region-feature multi-index.
TYPE:
|
neighbourhood |
The neighbourhood to use. Should be intialized with the same regions.
TYPE:
|
negative_sample_k_distance |
When sampling negative samples, sample from a distance > k. Defaults to 2.
TYPE:
|
batch_size |
Batch size. Defaults to 32.
TYPE:
|
learning_rate |
Learning rate. Defaults to 0.001.
TYPE:
|
trainer_kwargs |
Trainer kwargs. Defaults to None.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
ValueError
|
If features_gdf is empty and self.expected_output_features is not set. |
ValueError
|
If any of the gdfs index names is None. |
ValueError
|
If joint_gdf.index is not of type pd.MultiIndex or doesn't have 2 levels. |
ValueError
|
If index levels in gdfs don't overlap correctly. |
ValueError
|
If negative_sample_k_distance < 2. |
Source code in srai/embedders/hex2vec/embedder.py
fit_transform(
regions_gdf,
features_gdf,
joint_gdf,
neighbourhood,
negative_sample_k_distance=2,
batch_size=32,
learning_rate=0.001,
trainer_kwargs=None,
)
¶
fit_transform(
regions_gdf,
features_gdf,
joint_gdf,
neighbourhood,
negative_sample_k_distance=2,
batch_size=32,
learning_rate=0.001,
trainer_kwargs=None,
)
Fit the model to the data and return the embeddings.
PARAMETER | DESCRIPTION |
---|---|
regions_gdf |
Region indexes and geometries.
TYPE:
|
features_gdf |
Feature indexes, geometries and feature values.
TYPE:
|
joint_gdf |
Joiner result with region-feature multi-index.
TYPE:
|
neighbourhood |
The neighbourhood to use. Should be intialized with the same regions.
TYPE:
|
negative_sample_k_distance |
When sampling negative samples, sample from a distance > k. Defaults to 2.
TYPE:
|
batch_size |
Batch size. Defaults to 32.
TYPE:
|
learning_rate |
Learning rate. Defaults to 0.001.
TYPE:
|
trainer_kwargs |
Trainer kwargs. Defaults to None.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
DataFrame
|
pd.DataFrame: Region embeddings. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If features_gdf is empty and self.expected_output_features is not set. |
ValueError
|
If any of the gdfs index names is None. |
ValueError
|
If joint_gdf.index is not of type pd.MultiIndex or doesn't have 2 levels. |
ValueError
|
If index levels in gdfs don't overlap correctly. |
ValueError
|
If negative_sample_k_distance < 2. |
Source code in srai/embedders/hex2vec/embedder.py
¶
Save the model to a directory.
PARAMETER | DESCRIPTION |
---|---|
path |
Path to the directory.
TYPE:
|
Source code in srai/embedders/hex2vec/embedder.py
¶
classmethod
Load the model from a directory.
PARAMETER | DESCRIPTION |
---|---|
path |
Path to the directory.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Hex2VecEmbedder
|
The loaded embedder.
TYPE:
|
Source code in srai/embedders/hex2vec/embedder.py
¶
Bases: Embedder
Highway2Vec Embedder.
PARAMETER | DESCRIPTION |
---|---|
hidden_size |
Hidden size in encoder and decoder. Defaults to 64.
TYPE:
|
embedding_size |
Embedding size. Defaults to 30.
TYPE:
|
Source code in srai/embedders/highway2vec/embedder.py
¶
Embed regions using features.
PARAMETER | DESCRIPTION |
---|---|
regions_gdf |
Region indexes and geometries.
TYPE:
|
features_gdf |
Feature indexes, geometries and feature values.
TYPE:
|
joint_gdf |
Joiner result with region-feature multi-index.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
DataFrame
|
pd.DataFrame: Embedding and geometry index for each region in regions_gdf. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If any of the gdfs index names is None. |
ValueError
|
If joint_gdf.index is not of type pd.MultiIndex or doesn't have 2 levels. |
ValueError
|
If index levels in gdfs don't overlap correctly. |
Source code in srai/embedders/highway2vec/embedder.py
¶
Fit the model to the data.
PARAMETER | DESCRIPTION |
---|---|
regions_gdf |
Region indexes and geometries.
TYPE:
|
features_gdf |
Feature indexes, geometries and feature values.
TYPE:
|
joint_gdf |
Joiner result with region-feature multi-index.
TYPE:
|
trainer_kwargs |
Trainer kwargs. Defaults to None.
TYPE:
|
dataloader_kwargs |
Dataloader kwargs. Defaults to None.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
ValueError
|
If any of the gdfs index names is None. |
ValueError
|
If joint_gdf.index is not of type pd.MultiIndex or doesn't have 2 levels. |
ValueError
|
If index levels in gdfs don't overlap correctly. |
Source code in srai/embedders/highway2vec/embedder.py
¶
Fit the model to the data and return the embeddings.
PARAMETER | DESCRIPTION |
---|---|
regions_gdf |
Region indexes and geometries.
TYPE:
|
features_gdf |
Feature indexes, geometries and feature values.
TYPE:
|
joint_gdf |
Joiner result with region-feature multi-index.
TYPE:
|
trainer_kwargs |
Trainer kwargs. Defaults to None.
TYPE:
|
dataloader_kwargs |
Dataloader kwargs. Defaults to None.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
DataFrame
|
pd.DataFrame: Region embeddings. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If any of the gdfs index names is None. |
ValueError
|
If joint_gdf.index is not of type pd.MultiIndex or doesn't have 2 levels. |
ValueError
|
If index levels in gdfs don't overlap correctly. |
Source code in srai/embedders/highway2vec/embedder.py
¶
Save the model to a directory.
PARAMETER | DESCRIPTION |
---|---|
path |
Path to the directory.
TYPE:
|
Source code in srai/embedders/highway2vec/embedder.py
¶
classmethod
Load the model from a directory.
PARAMETER | DESCRIPTION |
---|---|
path |
Path to the directory.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Hex2VecEmbedder
|
The loaded embedder.
TYPE:
|