-
Notifications
You must be signed in to change notification settings - Fork 2
FEAT: Add Support for Geoembeddings #16
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
Open
martham93
wants to merge
6
commits into
main
Choose a base branch
from
martha/validate-embeddings
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,211 @@ | ||
| """ | ||
| Models for the Geoembeddings Zarr Convention. | ||
|
|
||
| This convention defines metadata for geospatial embedding groups stored in | ||
| Zarr format, including encoder model provenance, source data references, | ||
| and processing parameters. | ||
|
|
||
| Specification: https://github.com/geo-embeddings/embeddings-zarr-convention | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import Annotated, Final, Literal | ||
|
|
||
| from pydantic import BaseModel, Field, model_validator | ||
|
|
||
| from geozarr_toolkit.conventions.common import ZarrConventionMetadata, is_none | ||
|
|
||
| GEOEMB_UUID: Final[Literal["61c12cc5-0e28-4056-999a-480cf3fb7e4c"]] = ( | ||
| "61c12cc5-0e28-4056-999a-480cf3fb7e4c" | ||
| ) | ||
| GEOEMB_SCHEMA_URL: Final[str] = ( | ||
| "https://github.com/geo-embeddings/embeddings-zarr-convention/blob/main/schema.json" | ||
| ) | ||
| GEOEMB_SPEC_URL: Final[str] = ( | ||
| "https://github.com/geo-embeddings/embeddings-zarr-convention/blob/main/README.md" | ||
| ) | ||
|
|
||
|
|
||
| class GeoembConventionMetadata(ZarrConventionMetadata): | ||
| """Metadata for the geoemb: convention in zarr_conventions array.""" | ||
|
|
||
| uuid: Literal["61c12cc5-0e28-4056-999a-480cf3fb7e4c"] = GEOEMB_UUID | ||
| name: Literal["geoemb:"] = "geoemb:" | ||
| schema_url: str = GEOEMB_SCHEMA_URL | ||
| spec_url: str = GEOEMB_SPEC_URL | ||
| description: str = ( | ||
| "Geoembeddings convention for geospatial embedding arrays with model provenance" | ||
| ) | ||
|
|
||
|
|
||
| class ChipLayout(BaseModel): | ||
| """ | ||
| Chip layout configuration for chip-type embeddings. | ||
|
|
||
| Describes how the source imagery was divided into chips (patches). | ||
|
|
||
| Attributes | ||
| ---------- | ||
| layout_type : str | ||
| Type of chip layout. Either "regular_grid" or "irregular". | ||
| chip_size : list[int] | ||
| Chip dimensions [height, width] in pixels. | ||
| stride : list[int] | None | ||
| Stride between chips [y, x]. Defaults to chip_size if not specified. | ||
| grid_id : str | None | ||
| Identifier for a predefined grid system. | ||
| grid_definition : str | None | ||
| URL to grid definition document. | ||
| """ | ||
|
|
||
| layout_type: Literal["regular_grid", "irregular"] | ||
| chip_size: list[int] = Field(min_length=2, max_length=2) | ||
| stride: list[int] | None = Field(None, exclude_if=is_none) | ||
| grid_id: str | None = Field(None, exclude_if=is_none) | ||
| grid_definition: str | None = Field(None, exclude_if=is_none) | ||
|
|
||
| model_config = {"extra": "forbid"} | ||
|
|
||
| @model_validator(mode="after") | ||
| def validate_chip_size_positive(self) -> ChipLayout: | ||
| """Validate that chip_size values are positive.""" | ||
| if any(s < 1 for s in self.chip_size): | ||
| raise ValueError("chip_size values must be positive integers") | ||
| return self | ||
|
|
||
| @model_validator(mode="after") | ||
| def validate_stride_length(self) -> ChipLayout: | ||
| """Validate that stride has exactly 2 elements when provided.""" | ||
| if self.stride is not None and len(self.stride) != 2: | ||
| raise ValueError("stride must have exactly 2 elements [y, x]") | ||
| return self | ||
|
|
||
|
|
||
| class ScaleScalar(BaseModel): | ||
| """ | ||
| Scalar scale for linear dequantization. | ||
|
|
||
| Dequantize with: value = quantized * scale + offset. | ||
| """ | ||
|
|
||
| type: Literal["scalar"] | ||
| scale: float | ||
| offset: float = 0.0 | ||
|
|
||
| model_config = {"extra": "forbid"} | ||
|
|
||
|
|
||
| class ScaleArray(BaseModel): | ||
| """ | ||
| Per-pixel scale factors stored in a separate Zarr array. | ||
|
|
||
| Dequantize with: value[..., y, x] = quantized[..., y, x] * array[..., y, x]. | ||
| Non-finite values (NaN, +inf) in the scale array indicate no-data pixels. | ||
| """ | ||
|
|
||
| type: Literal["array"] | ||
| array_name: str | ||
| nodata: float | str | None = Field(None, exclude_if=is_none) | ||
|
|
||
| model_config = {"extra": "forbid"} | ||
|
|
||
|
|
||
| Scale = Annotated[ScaleScalar | ScaleArray, Field(discriminator="type")] | ||
|
|
||
|
|
||
| class Quantization(BaseModel): | ||
| """ | ||
| Quantization details for compressed embeddings. | ||
|
|
||
| Attributes | ||
| ---------- | ||
| method : str | ||
| Quantization method (e.g., "linear", "per_pixel_scale", | ||
| "product_quantization", "binary"). | ||
| original_dtype : str | ||
| Original data type before quantization (e.g., "float32"). | ||
| quantized_dtype : str | None | ||
| Data type after quantization (e.g., "int8"). | ||
| scale : ScaleScalar | ScaleArray | None | ||
| Scale parameters for dequantization. | ||
| link : str | None | ||
| URL to quantization codebook or lookup table. | ||
| """ | ||
|
|
||
| method: str | ||
| original_dtype: str | ||
| quantized_dtype: str | None = Field(None, exclude_if=is_none) | ||
| scale: Scale | None = Field(None, exclude_if=is_none) | ||
| link: str | None = Field(None, exclude_if=is_none) | ||
|
|
||
| model_config = {"extra": "forbid"} | ||
|
|
||
|
|
||
| class Geoemb(BaseModel): | ||
| """ | ||
| Geoembeddings convention attributes for a Zarr group. | ||
|
|
||
| Attributes | ||
| ---------- | ||
| type : str | ||
| Type of embedding: "pixel" for per-pixel embeddings, | ||
| "chip" for image patch embeddings. Required. | ||
| dimensions : int | ||
| Dimensionality of the embedding vector. Required. | ||
| model : str | ||
| URL reference to the encoder model used to generate embeddings. Required. | ||
| source_data : list[str] | ||
| URL references to the source datasets. Required, at least one item. | ||
| data_type : str | ||
| Data type of stored embeddings (e.g., "float32", "int8"). Required. | ||
| gsd : float | None | ||
| Ground sample distance in meters. | ||
| chip_layout : ChipLayout | None | ||
| Chip layout configuration. Required when type is "chip". | ||
| quantization : Quantization | None | ||
| Compression/quantization details. | ||
| spatial_layout : str | None | ||
| Spatial organization scheme: "utm_zones" or "global". | ||
| build_version : str | None | ||
| Version of the software that built this store. | ||
| benchmark : list[str] | None | ||
| URLs to benchmark evaluation results. | ||
| """ | ||
|
|
||
| type: Literal["pixel", "chip"] = Field(alias="geoemb:type") | ||
| dimensions: int = Field(alias="geoemb:dimensions", ge=1) | ||
| model: str = Field(alias="geoemb:model") | ||
| source_data: list[str] = Field(alias="geoemb:source_data", min_length=1) | ||
| data_type: str = Field(alias="geoemb:data_type") | ||
| gsd: float | None = Field(None, alias="geoemb:gsd", exclude_if=is_none) | ||
| chip_layout: ChipLayout | None = Field( | ||
| None, alias="geoemb:chip_layout", exclude_if=is_none | ||
| ) | ||
| quantization: Quantization | None = Field( | ||
| None, alias="geoemb:quantization", exclude_if=is_none | ||
| ) | ||
| spatial_layout: Literal["utm_zones", "global"] | None = Field( | ||
| None, alias="geoemb:spatial_layout", exclude_if=is_none | ||
| ) | ||
| build_version: str | None = Field( | ||
| None, alias="geoemb:build_version", exclude_if=is_none | ||
| ) | ||
| benchmark: list[str] | None = Field( | ||
| None, alias="geoemb:benchmark", exclude_if=is_none | ||
| ) | ||
|
|
||
| model_config = { | ||
| "extra": "allow", | ||
| "populate_by_name": True, | ||
| "serialize_by_alias": True, | ||
| } | ||
|
|
||
| @model_validator(mode="after") | ||
| def validate_chip_layout_required(self) -> Geoemb: | ||
| """Validate that chip_layout is provided when type is 'chip'.""" | ||
| if self.type == "chip" and self.chip_layout is None: | ||
| raise ValueError( | ||
| "geoemb:chip_layout is required when geoemb:type is 'chip'" | ||
| ) | ||
| return self | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not actually sure what to do about these links (these don't feel like the right thing to use). The readme of convention references v1 but those don't actually exist yet, and those links 404
https://github.com/geo-embeddings/embeddings-zarr-convention