Utils

The vibe_core.data.utils modules provides utility functions and classes for working with STAC (SpatioTemporal Asset Catalog) Items and converting them to and from BaseVibe objects. This module simplifies the process of managing and organizing the data used throughout FarmVibes.AI, offering utility functions for serializing and deserializing STAC Items, field conversion, and type resolution.

Hierarchy

classDiagram class FieldConverter { } class StacConverter { }

Documentation

class vibe_core.data.utils.FieldConverter(serializer: Callable[[Any], Any], deserializer: Callable[[Any], Any])

Bases: tuple

A named tuple representing a field converter.

deserializer: Callable[[Any], Any]

A function that deserializes a value.

serializer: Callable[[Any], Any]

Serializes a value.

class vibe_core.data.utils.StacConverter

Bases: object

A class that converts BaseVibe objects to STAC Items.

convert_field(field_value, field_type, converter)

Converts a field value to a given type, using a converter function.

Parameters:
  • field_value (Any) – The value of the field to convert.

  • field_type (Any) – The type to convert the field value to.

  • converter (Callable[[Any, Any], Any]) – The converter function to use.

Returns:

Any – The converted field value.

deserialize_fields(field_values, field_types)

Deserializes a dictionary of fields.

Parameters:
  • field_values (Dict[str, Any]) – The dictionary of field values to deserialize.

  • field_types (Dict[str, Any]) – The dictionary of field types to deserialize to.

Returns:

Dict[str, Any] – The deserialized dictionary of field values.

from_stac_item(input)

Converts a STAC item or a list of STAC items to a :class`BaseVibe` or a list of :class`BaseVibe`.

Parameters:

input (Union[Item, List[Item]]) – The STAC item or list of STAC items to convert.

Returns:

Union[BaseVibe, List[BaseVibe]] – A :class`BaseVibe` or a list of :class`BaseVibe`.

resolve_type(input)

Resolves the type of a :class`BaseVibe` object from a STAC item.

Parameters:

input (Item) – The STAC item to resolve the type from.

Returns:

Type[BaseVibe] – The type of :class`BaseVibe`.

sanitize_properties(properties)

Sanitizes a dictionary of properties to ensure they are JSON serializable.

Parameters:

properties (Dict[Any, Any]) – The dictionary of properties to sanitize.

Returns:

Dict[Any, Any] – The sanitized dictionary of properties.

serialize_fields(field_values, field_types)

Serializes a dictionary of fields.

Parameters:
  • field_values (Dict[str, Any]) – The dictionary of field values to serialize.

  • field_types (Dict[str, Any]) – The dictionary of field types to serialize to.

Returns:

Dict[str, Any] – The serialized dictionary of field values.

to_stac_item(input)

Converts a :class`BaseVibe` or a list of :class`BaseVibe` to a STAC item or a list of STAC items.

Parameters:

input (Union[List[BaseVibe], BaseVibe]) – The :class`BaseVibe` or list of :class`BaseVibe` to convert.

Returns:

A STAC item or a list of STAC items.

BASEVIBE_FALLBACK_DATETIME = datetime.datetime(1970, 1, 1, 0, 0)

The fallback datetime to use for BaseVibe objects.

VIBE_DATA_TYPE_FIELD = 'terravibes_data_type'

The name of the field that contains the data type of the BaseVibe object.

field_converters = {<class 'shapely.geometry.base.BaseGeometry'>: FieldConverter(serializer=<function mapping>, deserializer=<function shape>), <class 'datetime.datetime'>: FieldConverter(serializer=<function to_isoformat>, deserializer=<built-in method fromisoformat of type object>)}

A dictionary mapping field types to field converters.

vibe_core.data.utils.convert_time_range(item)

Converts the time range of a STAC item to a tuple of datetimes.

Parameters:

item (Item) – The STAC item to convert the time range for.

Returns:

Tuple[datetime, datetime] – A tuple of datetimes representing the start and end of the time range.

vibe_core.data.utils.deserialize_stac(arg)

Deserializes a dictionary or a list of dictionaries to a STAC item or a list of STAC items.

Parameters:

arg (Union[List[Dict[str, Any]], Dict[str, Any]]) – The dictionary or list of dictionaries to deserialize.

Returns:

A STAC item or a list of STAC items.

vibe_core.data.utils.get_base_type(vibetype)

Determines the base type of a typing specification.

Parameters:

vibetype (Union[Type[BaseVibe], Type[List[BaseVibe]]]) – The type to determine the base type of.

Returns:

Type[BaseVibe] – The base type of vibetype.

Raises:

ValueError – If the type hierarchy contains nested container types (e.g., List[List[DataVibe]]).

Doctests: >>> get_base_type(DataVibe) vibe_core.data.DataVibe >>> get_base_type([List[DataVibe]) vibe_core.data.DataVibe

vibe_core.data.utils.get_most_specific_type(types)

Determines the most specific type of a list of types.

Parameters:

types (List[Union[Type[BaseVibe], Type[List[BaseVibe]]]]) – The list of types to determine the most specific type of.

Returns:

Union[Type[BaseVibe], Type[List[BaseVibe]]] – The most specific type of types.

Raises:

ValueError – If the types are not compatible.

vibe_core.data.utils.is_container_type(typeclass)

Checks if a type is a container type.

Parameters:

typeclass (Union[Type[TypeVar(V)], List[Type[TypeVar(V)]]]) – The type to check.

Returns:

bool – True if the type is a container type, False otherwise.

vibe_core.data.utils.is_json_serializable(x)

Checks if a field is JSON serializable by Python’s default serializer.

Parameters:

x (Any) – The value to check.

Returns:

bool – True if the value is JSON serializable, False otherwise.

vibe_core.data.utils.is_vibe_list(typeclass)

Checks if a type is a list of :class`BaseVibe` objects.

Parameters:

typeclass (Union[Type[BaseVibe], Type[List[BaseVibe]]]) – The type to check.

Returns:

bool – True if the type is a list of :class`BaseVibe` objects, False otherwise.

vibe_core.data.utils.serialize_input(input_data)

Serializes a single :class`BaseVibe` object, or a list or dictionary of them, to a STAC item or a list or dictionary of STAC items.

Parameters:

input_data (Any) – The :class`BaseVibe` object or a list or dictionary of :class`BaseVibe` objects to serialize.

Returns:

Any – A list, a dictionary or a single STAC Item representing the :class`BaseVibe` object.

Raises:

NotImplementedError – If the input data is not a :class`BaseVibe` object, or a list or dictionary of :class`BaseVibe`.

vibe_core.data.utils.serialize_stac(arg)

Serializes a STAC item or a list of STAC items to a dictionary or a list of dictionaries.

Parameters:

arg (Union[Item, List[Item]]) – The STAC item or list of STAC items to serialize.

Returns:

A dictionary or a list of dictionaries representing the STAC item or list of STAC items.

vibe_core.data.utils.to_isoformat(x)

Converts a datetime object to an ISO format string.

Parameters:

x (datetime) – The datetime object to convert.

Returns:

str – The ISO format string.