Models utils#
- spatialdata.models.get_model(e, validate=True)[source]#
Get the model for the given element. Validate using the model if
validateisTrue.- Parameters:
- e
SpatialElement|AnnData The element.
- validate
bool(default:True) Whether to validate the element using the model.
- e
- Return type:
type[Image2DModel] |type[Image3DModel] |type[Labels2DModel] |type[Labels3DModel] |type[PointsModel] |type[ShapesModel] |type[TableModel]- Returns:
The SpatialData model.
- spatialdata.models.validate_element(e)[source]#
Validate a spatial element against its model schema.
- Parameters:
- e
SpatialElement The spatial element to validate.
- e
- Raises:
ValueError – If the element is invalid (e.g. missing or empty transformations, wrong dtypes).
- Return type:
- spatialdata.models.SpatialElement = SpatialElement#
Type alias.
Type aliases are created through the type statement:
type Alias = int
In this example, Alias and int will be treated equivalently by static type checkers.
At runtime, Alias is an instance of TypeAliasType. The __name__ attribute holds the name of the type alias. The value of the type alias is stored in the __value__ attribute. It is evaluated lazily, so the value is computed only if the attribute is accessed.
Type aliases can also be generic:
type ListOrSet[T] = list[T] | set[T]
In this case, the type parameters of the alias are stored in the __type_params__ attribute.
See PEP 695 for more information.
- spatialdata.models.get_axes_names(e)[source]#
- spatialdata.models.get_axes_names(e: DataArray) tuple[str, ...]
- spatialdata.models.get_axes_names(e: DataTree) tuple[str, ...]
- spatialdata.models.get_axes_names(e: GeoDataFrame) tuple[str, ...]
- spatialdata.models.get_axes_names(e: DataFrame) tuple[str, ...]
- spatialdata.models.get_axes_names(e: DataFrame) tuple[str, ...]
Get the dimensions of a SpatialElement.
- Parameters:
- e
SpatialElement|DataFrame SpatialElement
- e
- Return type:
- Returns:
Dimensions of the SpatialElement (e.g. (“z”, “y”, “x”))
- spatialdata.models.points_geopandas_to_dask_dataframe(gdf, suppress_z_warning=False)[source]#
Convert a GeoDataFrame which represents 2D or 3D points to a Dask DataFrame that passes the schema validation.
- Parameters:
- gdf
GeoDataFrame GeoDataFrame with a geometry column that contains 2D or 3D points.
- gdf
- Return type:
- Returns:
The Dask DataFrame converted from the GeoDataFrame. The Dask DataFrame passes the schema validation.
Notes
The returned Dask DataFrame gets the ‘x’ and ‘y’ columns from the geometry column, and eventually the ‘z’ column (and the rest of the columns), from the remaining columns of the GeoDataFrame.
- spatialdata.models.points_dask_dataframe_to_geopandas(points, suppress_z_warning=False)[source]#
Convert a Dask DataFrame to a GeoDataFrame.
- Parameters:
- points
DataFrame Dask DataFrame with columns “x” and “y”. Eventually, it can contain a column “z” that will be not included in the geometry column.
- points
- Return type:
- Returns:
The GeoDataFrame with the geometry column constructed from the “x” and “y” columns and, if present, the rest of the columns.
Notes
The “z” column is not included in the geometry column because it is not supported by GeoPandas. The resulting GeoDataFrame does not currenlty passes the validation of the SpatialData models. In fact currently points need to be saved as a Dask DataFrame. We will be restructuring the models to allow for GeoDataFrames soon.
- spatialdata.models.get_channel_names(data)[source]#
- spatialdata.models.get_channel_names(data: DataArray) list[Any]
- spatialdata.models.get_channel_names(data: DataTree) list[Any]
Get channels from data for an image element (both single and multiscale).
Notes
For multiscale images, the channels are validated to be consistent across scales.
- spatialdata.models.set_channel_names(element, channel_names)[source]#
Set the channel names for a image
SpatialElementin theSpatialDataobject.- Parameters:
- Return type:
- Returns:
The image
SpatialElementor parsedImageModelwith the channel names set to thecdimension.
- spatialdata.models.force_2d(gdf)[source]#
Force the geometries of a shapes object GeoDataFrame to be 2D by modifying the geometries in place.
Geopandas introduced a method called
force_2d()to drop the z dimension. Unfortunately, this feature, as of geopandas == 0.14.3, is still not released. Similarly, the recently released shapely >= 2.0.3 implementedforce_2d(), but currently there are installation errors.A similar function has been developed in When
.force_2d()- Parameters:
- gdf
GeoDataFrame GeoDataFrame with 2D or 3D geometries
- gdf
- Return type: