srai

repository·main·Indexed 18 days ago

https://github.com/kraina-ai/srai

A Python library for geospatial machine learning and data mining focused on vector geometries. srai provides a complete pipeline for acquiring spatial data from OSM and OvertureMaps, regionalization using H3, S2, or Voronoi, and generating vector embeddings. It includes specialized loaders, embedders, and the Open Benchmark for Spatial Representations (OBSR) for evaluating tasks such as price prediction, crime activity estimation, human mobility prediction, and travel time estimation.

Tokens
47.4K
Snippets
160
Records
185
Agent score
63%

What's inside srai

  1. Overview of srai functionalities

    main

    SRAI (Spatial Representations for Artificial Intelligence) is a Python library for geospatial machine learning focused on vector geometries. It enables the following workflows:

    • Data Acquisition: Downloading OpenStreetMap (OSM) or OvertureMaps data for specific areas.
    • Vector Processing: Extracting features like road networks, buildings, and POIs from vector data.
    • GTFS Processing: Extracting features from General Transit Feed Specification (GTFS) data.
    • Regionalization: Splitting areas into micro-regions using algorithms like Uber's H3 or Voronoi.
    • Embedding: Converting regions into vector spaces using spatial features and PyTorch models (e.g., hex2vec).
    • Datasets: Accessing prepared datasets and benchmarks for downstream geospatial tasks.
  2. Available prediction tasks in OBSR

    main

    The Open Benchmark for Spatial Representations (OBSR) categorizes tasks into two main types: Point-based and Trajectory-based. These tasks are designed to evaluate how well spatial representations (like region embeddings) capture economic, social, or mobility patterns.

    Point-based tasks

    • Price prediction: Predict average prices (e.g., short-term rentals or house sales) for a hexagonal region at a specific H3 resolution. Uses region embeddings combined with optional features.
    • Activity prediction: Estimate crime intensity (a continuous value between 0 and 1) within a geographic region. Formulated as a regression problem using region embeddings.

    Trajectory-based tasks

    • Human Mobility Prediction (HMC): An autoregressive sequence classification problem. Predicts the next hexagon ID in a sequence based on a user's recent movement trajectory.
    • Travel Time Estimation (TTE): A sequence-to-value regression problem. Predicts the total travel time for a trip based on a sequence of hexagon IDs representing the route.
  3. Explore srai library API components

    main

    The srai library API is organized into several functional modules. You can find detailed documentation and examples for each of the following components:

    • Embedders: For generating spatial embeddings.
    • Joiners: For joining spatial datasets.
    • Loaders: For loading spatial data.
    • Neighbourhoods: For analyzing or defining spatial neighborhoods.
    • Regionalizers: For partitioning spatial data into regions.
  4. Available Loaders in srai

    main

    The srai library provides several specialized loaders for importing spatial and transit data into spatial representations. Depending on your data source, you can use one of the following loaders:

    • GeoparquetLoader: For loading data from GeoParquet files.
    • GTFSLoader: For loading General Transit Feed Specification (GTFS) data (public transit schedules).
    • OSMOnlineLoader: For fetching OpenStreetMap (OSM) data directly from online services.
    • OSMPbfLoader: For loading OpenStreetMap data from .pbf files.
    • OSMWayLoader: For loading OpenStreetMap way data.
    • OvertureMapsLoader: For loading data from Overture Maps datasets.
  5. Apply transfer learning with spatial embeddings

    main
    When pre-calculated embeddings for your specific city are unavailable, you can use srai to perform transfer learning. This involves using models pre-trained on other cities to obtain spatial representations for your target area, which can then be used as features to train your own predictive models.
  6. How embedding works in srai

    main

    Embedding maps spatial regions into a vector space. The process follows a standard pipeline involving four main components:

    1. Loader: Loads spatial features (e.g., OSMOnlineLoader).
    2. Regionalizer: Splits the area into regions (e.g., H3Regionalizer).
    3. Joiner: Joins the loaded features to the regions (e.g., IntersectionJoiner).
    4. Embedder: Transforms the joined data into vectors (e.g., CountEmbedder, Hex2VecEmbedder).

    Simple Embedders (like CountEmbedder) do not require a fitting step.

    Complex Embedders (like Hex2VecEmbedder or GTFS2VecEmbedder) follow a scikit-learn style API and require a fit or fit_transform step, often using a neighbourhood object (e.g., H3Neighbourhood) to capture spatial context.

    # Standard Embedding Pipeline Example
    from srai.embedders import CountEmbedder
    from srai.joiners import IntersectionJoiner
    from srai.loaders import OSMOnlineLoader
    from srai.plotting import plot_regions, plot_numeric_data
    from srai.regionalizers import H3Regionalizer, geocode_to_region_gdf
    
    loader = OSMOnlineLoader()
    regionalizer = H3Regionalizer(resolution=9)
    joiner = IntersectionJoiner()
    
    query = {"amenity": "bicycle_parking"}
    area = geocode_to_region_gdf("Malmö, Sweden")
    
    # 1. Load
    features = loader.load(area, query)
    # 2. Regionalize
    regions = regionalizer.transform(area)
    # 3. Join
    joint = joiner.transform(regions, features)
    # 4. Embed
    embedder = CountEmbedder()
    embeddings = embedder.transform(regions, features, joint)
    
    # Visualization
    folium_map = plot_regions(area, colormap=["rgba(0,0,0,0.1)"], tiles_style="CartoDB positron")
    plot_numeric_data(regions, "amenity_bicycle_parking", embeddings, map=folium_map)
  7. Train custom hex2vec embeddings from OSM data

    main
    If you need spatial embeddings tailored specifically to your area of operations (e.g., for logistics demand prediction), srai provides a solution to train your own hex2vec embeddings using OpenStreetMap (OSM) data. This allows you to generate representations that are optimized for your specific geographic context rather than relying on general pre-trained models.
  8. Extract features from GTFS data using GTFSLoader

    main

    Use GTFSLoader to extract transit features from a GTFS (General Transit Feed Specification) file. It extracts trip counts and available directions for each stop within 1-hour time windows. You can use the utility download_file to fetch the GTFS zip file first.

    from pathlib import Path
    from srai.loaders import GTFSLoader, download_file
    from srai.plotting import plot_regions
    from srai.regionalizers import geocode_to_region_gdf
    
    area = geocode_to_region_gdf("Vienna, Austria")
    gtfs_file = Path("vienna_gtfs.zip")
    # Download the GTFS data
    download_file("https://transitfeeds.com/p/stadt-wien/888/latest/download", gtfs_file.as_posix())
    
    loader = GTFSLoader()
    features = loader.load(gtfs_file)
    
    folium_map = plot_regions(area, colormap=["rgba(0,0,0,0.1)"], tiles_style="CartoDB positron")
    # Explore features like 'trips_at_8'
    features[["trips_at_8", "geometry"]].explore("trips_at_8", m=folium_map)
  9. Explore srai usage examples and use cases

    main

    The srai library provides several real-world usage examples and Jupyter notebooks to demonstrate its functionality. You can explore these in the examples/use_cases directory or via the main repository.

    Key use cases include:

    • Simple Machine Learning with Overture Maps data: Demonstrates how to use Overture Maps data for machine learning tasks.
    • Spatial dataset splitting: Shows how to perform spatial splitting on datasets.
  10. Perform regionalization of an area

    main

    Regionalization divides a given area into smaller, manageable regions. Supported regionalizers include:

    • H3Regionalizer: Uses Uber's H3 hexagonal grid.
    • S2Regionalizer: Uses Google's S2 geometry.
    • VoronoiRegionalizer: Uses Voronoi diagrams.
    • AdministativeBoundaryRegionalizer: Uses existing administrative boundaries.

    Use the .transform(area) method to generate the regions as a GeoDataFrame.

    from srai.regionalizers import H3Regionalizer, geocode_to_region_gdf
    from srai.plotting import plot_regions
    
    area = geocode_to_region_gdf("Berlin, Germany")
    regionalizer = H3Regionalizer(resolution=7)
    
    # Returns a GeoDataFrame of regions
    regions = regionalizer.transform(area)
    
    folium_map = plot_regions(area, colormap=["rgba(0,0,0,0.1)"], tiles_style="CartoDB positron")
    plot_regions(regions_gdf=regions, map=folium_map)
  11. Install srai via pip

    main

    Install the core srai package and its primary dependencies using pip. Note that this library is under heavy development and may have breaking changes between minor versions.

    pip install srai
  12. Install srai optional dependencies

    main

    To enable specific functionalities like OSM data downloading, GTFS processing, or PyTorch-based embeddings, install the corresponding optional dependency extras:

    • srai[all]: Installs all optional dependencies.
    • srai[osm]: Required to download OpenStreetMap data.
    • srai[overturemaps]: Required to download Overture Maps data.
    • srai[datasets]: Required for downloading datasets.
    • srai[voronoi]: Required for Voronoi-based regionalization.
    • srai[gtfs]: Required to process GTFS data.
    • srai[plotting]: Required to plot graphs and maps.
    • srai[torch]: Required to use torch-based embedders.
    pip install "srai[osm,gtfs,torch]"