hyped.core.features.dtypes module¶
Data Type (DType) System Module.
This module defines a comprehensive type system for representing data types used in a structured framework. It includes abstractions for primitive types as well as nested structures including sequence and mapping types.
- hyped.core.features.dtypes.BoolType = PrimitiveType(_arrow_type=DataType(bool))¶
Boolean type.
- class hyped.core.features.dtypes.ClassLabelType(_arrow_type: Final[pa.DataType] = DataType(int64), names: None | tuple[str] = None)[source]¶
Bases:
PrimitiveTypeRepresents a class label type, typically used for categorical labels in datasets.
- classmethod from_dict(data: dict) PrimitiveType[source]¶
Constructs a PrimitiveType instance from a dictionary representation.
- Parameters:
data (dict) – A dictionary with the following keys: -
type(str): Must be “PrimitiveType”. -arrow_type(str): The string representation of the PyArrow type.- Returns:
An instance of the PrimitiveType class.
- Return type:
- Raises:
ValueError – If the
typefield is not “PrimitiveType”.
- property hf_feature: Features¶
Returns the corresponding HuggingFace Feature for this class label type.
- Returns:
The Hugging Face Feature representation of the type.
- Return type:
datasets.Features
- names: None | tuple[str] = None¶
A tuple of class label names, which must be provided during initialization.
- to_dict() dict[source]¶
Converts the
PrimitiveTypeinstance to a dictionary representation.- Returns:
- A dictionary with the following keys:
type(str): The type identifier (“PrimitiveType”).arrow_type(str): The string representation of the PyArrow type.
- Return type:
- class hyped.core.features.dtypes.DType[source]¶
Bases:
ABCAbstract base class for types in the system.
This class provides the foundation for defining types within the system. Subclasses must implement the
arrow_typeproperty to map their type to a corresponding PyArrow data type- abstract property arrow_type: DataType¶
Abstract property that returns the corresponding PyArrow data type.
This property must be implemented by subclasses to provide the PyArrow data type representation for the specific type.
- Returns:
The PyArrow data type representation of the type.
- Return type:
pa.DataType
- abstractmethod classmethod from_dict(data: dict) DType[source]¶
Constructs an object from its dictionary representation.
Subclasses must implement this method to enable deserialization of the object from a dictionary.
- abstract property hf_feature: Features¶
Abstract property that returns the corresponding HuggingFace Feature.
This property must be implemented by subclasses to provide the HuggingFace Feature representation for the specific type.
- Returns:
The Hugging Face Feature representation of the type.
- Return type:
datasets.Features
- hyped.core.features.dtypes.Float16Type = PrimitiveType(_arrow_type=DataType(halffloat))¶
16-bit floating point type.
- hyped.core.features.dtypes.Float32Type = PrimitiveType(_arrow_type=DataType(float))¶
32-bit floating point type.
- hyped.core.features.dtypes.Float64Type = PrimitiveType(_arrow_type=DataType(double))¶
64-bit floating point type.
- hyped.core.features.dtypes.Int16Type = PrimitiveType(_arrow_type=DataType(int16))¶
16-bit signed integer type.
- hyped.core.features.dtypes.Int32Type = PrimitiveType(_arrow_type=DataType(int32))¶
32-bit signed integer type.
- hyped.core.features.dtypes.Int64Type = PrimitiveType(_arrow_type=DataType(int64))¶
64-bit signed integer type.
- hyped.core.features.dtypes.Int8Type = PrimitiveType(_arrow_type=DataType(int8))¶
8-bit signed integer type.
- class hyped.core.features.dtypes.MappingType(fields: tuple[tuple[str, DType]])[source]¶
Bases:
DType,Mapping[str,DType]Represents a mapping type in the typing system.
This type associates field names with their corresponding types.
- property arrow_schema: Schema¶
Returns the
PyArrowschema for this mapping.- Returns:
The
PyArrowschema representation.- Return type:
pa.Schema
- property arrow_type: StructType¶
Returns the corresponding
PyArrowdata type for this mapping.- Returns:
The
PyArrowstruct type representation.- Return type:
pa.StructType
- classmethod construct(fields: dict[str, DType]) MappingType[source]¶
Creates a
MappingTypeinstance from a dictionary of fields.- Parameters:
fields (dict[str, DType]) – A dictionary mapping keys to their types.
- Returns:
A new
MappingTypeinstance.- Return type:
- fields: tuple[tuple[str, DType]]¶
The fields in the mapping, represented as a tuple of key-type pairs.
- classmethod from_dict(data: dict) MappingType[source]¶
Deserializes a
MappingTypeinstance from a dictionary.- Parameters:
data (dict) – A dictionary containing the serialized form of a MappingType.
- Returns:
A new instance of
MappingTypeconstructed from the data.- Return type:
- Raises:
ValueError – If the “type” field in the dictionary is not “MappingType”.
- property hf_feature: Features¶
Returns the corresponding HuggingFace dataset features for this mapping.
This property converts the PyArrow schema associated with the mapping type into a HuggingFace
Featuresobject.- Returns:
The Hugging Face
Featuresobject representing the mapping type.- Return type:
datasets.Features
- to_dict() dict[source]¶
Serializes the
MappingTypeinstance to a dictionary.Converts the mapping’s fields into a dictionary format, suitable for JSON serialization or storage.
- Returns:
A dictionary representing the mapping type.
- Return type:
- class hyped.core.features.dtypes.PrimitiveType(_arrow_type: DataType)[source]¶
Bases:
DTypeRepresents a primitive type in the typing system.
A primitive type corresponds directly to a PyArrow primitive data type such as
int32,float64, orbool. This class provides methods for serialization and deserialization of primitive types.- property arrow_type: DataType¶
Returns the corresponding
PyArrowdata type for this primitive type.- Returns:
The
PyArrowdata type representation.- Return type:
pa.DataType
- classmethod from_dict(data: dict) PrimitiveType[source]¶
Constructs a PrimitiveType instance from a dictionary representation.
- Parameters:
data (dict) – A dictionary with the following keys: -
type(str): Must be “PrimitiveType”. -arrow_type(str): The string representation of the PyArrow type.- Returns:
An instance of the PrimitiveType class.
- Return type:
- Raises:
ValueError – If the
typefield is not “PrimitiveType”.
- property hf_feature: Features¶
Returns the corresponding HuggingFace Feature for this primitive type.
- Returns:
The Hugging Face Feature representation of the type.
- Return type:
datasets.Features
- to_dict() dict[source]¶
Converts the
PrimitiveTypeinstance to a dictionary representation.- Returns:
- A dictionary with the following keys:
type(str): The type identifier (“PrimitiveType”).arrow_type(str): The string representation of the PyArrow type.
- Return type:
- class hyped.core.features.dtypes.SequenceType(value_type: DType, length: int = 4294967295)[source]¶
-
Represents a sequence type in the typing system.
- property arrow_type: ListType¶
Returns the corresponding
PyArrowdata type for this sequence.- Returns:
The
PyArrowlist type representation.- Return type:
pa.ListType
- classmethod from_dict(data: dict) SequenceType[source]¶
Deserializes a dictionary representation into a
SequenceTypeinstance.- Parameters:
data (dict) – A dictionary containing serialized information of a sequence type.
- Returns:
The deserialized sequence type.
- Return type:
- Raises:
ValueError – If the dictionary does not represent a SequenceType.
- property hf_feature: Features¶
Returns the corresponding HuggingFace Feature for this sequence type.
- Returns:
The Hugging Face Feature representation of the type.
- Return type:
datasets.Features
- length: int = 4294967295¶
The length of the sequence
Set to
UNDEFINED_SEQUENCE_LENGTHin case of dynamic sequence lengths.
- hyped.core.features.dtypes.StringType = PrimitiveType(_arrow_type=DataType(string))¶
String type.
- hyped.core.features.dtypes.UInt16Type = PrimitiveType(_arrow_type=DataType(uint16))¶
16-bit unsigned integer type.
- hyped.core.features.dtypes.UInt32Type = PrimitiveType(_arrow_type=DataType(uint32))¶
32-bit unsigned integer type.
- hyped.core.features.dtypes.UInt64Type = PrimitiveType(_arrow_type=DataType(uint64))¶
64-bit unsigned integer type.
- hyped.core.features.dtypes.UInt8Type = PrimitiveType(_arrow_type=DataType(uint8))¶
An 8-bit unsigned integer type.
- hyped.core.features.dtypes.UNDEFINED_SEQUENCE_LENGTH = 4294967295¶
A constant representing an undefined sequence length.
This value is used when the length of a sequence is unknown or unspecified.
- hyped.core.features.dtypes.build_dtype_from_arrow_type(arrow_type: DataType) DType[source]¶
Build a data type from a given Arrow type.
- hyped.core.features.dtypes.build_dtype_from_dict(data: dict) DType[source]¶
Constructs a DType instance from a dictionary.
This function determines the type of the serialized data and calls the appropriate from_dict method to reconstruct the corresponding DType instance.
- Parameters:
data (dict) – A dictionary representing the serialized form of a DType, containing a “type” field.
- Returns:
An instance of the appropriate DType subclass.
- Return type:
- Raises:
ValueError – If the “type” field does not correspond to a recognized DType subclass.
- hyped.core.features.dtypes.build_dtype_from_hf_feature(feature: dict | list | tuple | Value | ClassLabel | Translation | TranslationVariableLanguages | LargeList | List | Array2D | Array3D | Array4D | Array5D | Audio | Image | Video | Pdf) DType[source]¶
Build a data type from a given Hugging Face feature.
- hyped.core.features.dtypes.build_dtype_from_python_object(obj: Any) DType[source]¶
Build a data type from a Python object.
- Parameters:
obj (Any) – The object to derive the data type from.
- Returns:
The corresponding data type.
- Return type:
- Raises:
TypeError – If the object type is unsupported.
RuntimeError – If there are inconsistencies in list item types.
- hyped.core.features.dtypes.cast_dtype(src_dtype: DType, tgt_dtype: DType) DType[source]¶
Perform type casting of a source to a target data type.
This function attempts to cast a source data type (
src_dtype) to a target data type (tgt_dtype). It supports casting between various data types such as sequences, mappings, and primitive types. If the casting is not feasible due to type mismatches or constraints (e.g., incompatible lengths for sequences or mismatched keys for mappings), an exception is raised.Special Behavior for Mappings: - When both
src_dtypeandtgt_dtypeareMappingType, the outputretains all fields from src_dtype, but fields specified in tgt_dtype are cast to their corresponding target types.
Fields in
src_dtypethat are not specified intgt_dtyperemain unchanged in the output.Keys in
tgt_dtypemust be a subset of the keys insrc_dtype. If not, aRuntimeErroris raised.
Example:
src_dtype = MappingType.construct({"field": Int32Type, "other": BoolType}) tgt_dtype = MappingType.construct({"field": Int64Type}) result = cast_dtype(src_dtype, tgt_dtype) # result: # MappingType.construct({"field": Int64Type, "other": BoolType})
- Parameters:
- Returns:
The resulting data type after a successful cast.
- Return type:
- Raises:
RuntimeError – If the casting operation is not feasible due to type mismatches or constraints.
- hyped.core.features.dtypes.common_dtype(*dtypes: DType) DType[source]¶
Determine the common dtype for a set of input dtypes.
This function identifies a dtype to which all input dtypes can be cast without loss of information, based on a predefined priority hierarchy. It supports primitive types, sequences, and mappings.
- Parameters:
dtypes (DType) – The input dtypes to compare. These can be
PrimitiveType,SequenceType, orMappingTypeobjects.- Returns:
The common dtype that can represent all input dtypes.
- Return type:
- Raises:
AssertionError – If no dtypes are provided or the dtypes cannot be combined.
RuntimeError – If dtypes are incompatible or cannot be resolved to a common dtype.