dataprops
dataprops
¶
Classes:
| Name | Description |
|---|---|
Data4Proc |
|
DataProps |
A class for handling the properties and transformations of data, including labels. |
TensorLabels |
A class to manage labels associated with data and perform operations on them. |
Classes¶
Data4Proc
¶
Initializes a Data4Proc object, which is a container for one or two DataProps instances. It creates a
DataProps object for a private stream and, optionally, a public stream.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
Variable length argument list to be passed to the |
()
|
|
private_only
|
bool
|
If True, only a private stream's |
False
|
public_only
|
bool
|
If True, only a public stream's |
False
|
**kwargs
|
Arbitrary keyword arguments passed to the |
{}
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If both |
Methods:
| Name | Description |
|---|---|
to_list_of_dicts |
Converts the contained |
to_dict |
Raises a |
from_dict |
Raises a |
clone |
Creates and returns a deep copy of the |
is_public |
Raises a |
Source code in unaiverse/dataprops.py
Methods:¶
to_list_of_dicts
¶
Converts the contained DataProps objects into a list of dictionaries. Each dictionary represents the
properties of a single data stream.
Returns:
| Type | Description |
|---|---|
|
A list of dictionaries, where each dictionary holds the properties of a |
Source code in unaiverse/dataprops.py
to_dict
¶
Raises a RuntimeError because this method is intended for a single DataProps object, not for the
container class Data4Proc which can hold multiple properties.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
Always, as this method is not supported. |
Source code in unaiverse/dataprops.py
from_dict
¶
Raises a RuntimeError because this method is intended for a single DataProps object, not for the
container class Data4Proc which can hold multiple properties.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
Always, as this method is not supported. |
Source code in unaiverse/dataprops.py
clone
¶
Creates and returns a deep copy of the Data4Proc object.
Returns:
| Type | Description |
|---|---|
|
A new |
Source code in unaiverse/dataprops.py
is_public
¶
Raises a RuntimeError because this method is intended for a single DataProps object, not for the
container class Data4Proc which can hold multiple properties.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
Always, as this method is not supported. |
Source code in unaiverse/dataprops.py
DataProps
¶
DataProps(name: str = 'unk', group: str = 'none', data_type: str = 'text', data_desc: str = 'unk', tensor_shape: tuple[int | None, ...] | None = None, tensor_labels: list[str] | str | None = None, tensor_dtype: dtype | str | None = None, tensor_labeling_rule: str = 'max', stream_to_proc_transforms: Callable[..., Any] | PreTrainedTokenizerBase | str | dict | tuple[dict | Callable[..., Any] | PreTrainedTokenizerBase | str | None, dict | Callable[..., Any] | PreTrainedTokenizerBase | str | None] | None = None, proc_to_stream_transforms: Callable[..., Any] | PreTrainedTokenizerBase | str | list | None = None, delta: float = -1, pubsub: bool = False, public: bool = False)
A class for handling the properties and transformations of data, including labels. It supports different data types: 'tensor', 'tensor_token_id', 'img', and 'text'.
Attributes:
| Name | Type | Description |
|---|---|---|
VALID_DATA_TYPES |
tuple
|
Tuple of valid data types ('tensor', 'tensor_token_id', 'img', 'text'). |
Initializes a DataProps instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the data (default is "unk"). |
'unk'
|
group
|
str
|
Name of the group to which this DataProps belong (default: "none"). |
'none'
|
data_type
|
str
|
The type of data ('tensor', 'img', or 'text'). |
'text'
|
data_desc
|
str
|
Description of the data (default is "unk"). |
'unk'
|
tensor_shape
|
tuple[int | None] or None
|
Shape of the tensor data (e.g., (3, 224, 224) or (3, None, None) for tensors that are variable size images). It is None for non-tensor data. |
None
|
tensor_labels (list[str] or "AutoTokenizer
|
|
required | |
tensor_dtype
|
dtype or str or None
|
The string representing the Pytorch dtype of the tensor data. |
None
|
tensor_labeling_rule
|
str
|
The labeling rule for tensor data ('max' or 'geqX' where X is a number). |
'max'
|
stream_to_proc_transforms
|
callable or PreTrainedTokenizerBase or str or dict or a list* or None
|
A callable stream format to tensor format conversion fcn (any callable thing, torchvision transforms, a pretrained tokenizer, or the model ID from which it can be downloaded (it must have prefix "AutoTokenizer:"), or a vocabulary str->int). It is None for already-tensorial data. *If you need to distinguish the transform applied to the inputs and to targets, you can pass a list of two elements like the just described ones - one for input, one for targets, respectively. |
None
|
proc_to_stream_transforms
|
callable or PreTrainedTokenizerBase or str or list or None
|
A callable tensor to stream format function (any callable thing, torchvision transforms, a Pretrained tokenizer (HF), or the model ID from which it can be downloaded, (it must have prefix "AutoTokenizer:"), or a vocabulary int->str). It is None for non text data. |
None
|
delta
|
float
|
Time interval between consecutive data samples (<= 0 for real-time data). |
-1
|
pubsub
|
bool
|
If the stream is supposed to be sent to/received from a Pub/Sub topic. |
False
|
public
|
bool
|
If the stream is supposed to be accessed through the public net or through the private one. |
False
|
Returns:
| Type | Description |
|---|---|
|
None |
Methods:
| Name | Description |
|---|---|
to_dict |
Serializes the |
from_dict |
A static method that deserializes a dictionary into a |
clone |
Creates and returns a deep copy of the |
get_name |
Retrieves the name of the data stream. |
get_group |
Retrieves the name of the group to which the data stream belongs. |
get_description |
Retrieves the description of the data. |
get_tensor_labels |
Retrieves the list of tensor labels, if they exist. |
set_name |
Sets a new name for the stream, with a check for invalid characters. |
set_group |
Sets a new group name for the stream, with a check for invalid characters. |
set_description |
Sets a new description for the data. |
set_public |
Sets whether the stream is public or not. |
set_pubsub |
Sets whether the stream uses Pub/Sub or direct messaging. |
is_tensor |
Checks if the data type is 'tensor'. |
is_img |
Checks if the data type is 'img'. |
is_text |
Checks if the data type is 'text'. |
is_tensor_long |
Checks if the tensor's data type is |
is_tensor_float |
Checks if the tensor's data type is a float type (e.g., |
is_tensor_img |
Checks if the tensor's shape corresponds to a typical image format (4D, with 1 or 3 channels). |
is_tensor_token_ids |
Checks if the tensor represents token IDs, which is indicated by |
is_tensor_target_id |
Checks if the tensor represents a single target ID, indicated by |
is_all |
Checks if the data type is 'all', which is a wildcard type. |
net_hash |
Generates a unique network hash for the stream using a provided prefix, Pub/Sub status, and name/group. |
peer_id_from_net_hash |
A static method to extract the peer ID from a network hash. |
name_or_group_from_net_hash |
A static method to extract the name or group from a network hash. |
is_pubsub_from_net_hash |
A static method to check if a network hash belongs to a Pub/Sub stream. |
name_or_group |
Retrieves the group name if it's set, otherwise defaults to the stream name. |
build_net_hash |
A static method to construct a complete network hash from a prefix, Pub/Sub status, and name/group. |
normalize_net_hash |
A static method that cleans up or normalizes a network hash string to a canonical format, particularly |
is_pubsub |
Checks if the stream is set to use Pub/Sub. |
is_public |
Checks if the stream is set to be public. |
set_tensor_labels_from_auto_tokenizer |
Initializes and sets the tensor labels by fetching the vocabulary from a Hugging Face |
set_tensor_labels |
Sets the labels for the data. |
adapt_tensor_to_tensor_labels |
Interleaves data in function of its corresponding labels and the current super-set labels. |
clear_label_adaptation |
Removes the padding and returns the original data from an adapted tensor. |
is_flat_tensor_with_labels |
Checks if the tensor is a 2D array and has labels, which is a common structure for general feature data. |
has_tensor_labels |
Checks if any tensor labels are associated with the stream. |
to_text |
Converts the tensor data into a text-based representation exploiting the given labels and the labeling rule. |
check_and_preprocess |
Prepares incoming data for a processor by validating its type and applying necessary transformations. |
check_and_postprocess |
Takes a processor's output and validates it before converting it back into a stream-compatible format. |
is_compatible |
Checks if the current DataProps instance is compatible with another DataProps instance. |
Source code in unaiverse/dataprops.py
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 | |
Methods:¶
to_dict
¶
Serializes the DataProps object into a dictionary, making it suitable for transmission or storage.
It converts complex types like torch.dtype and TensorLabels into simple, serializable formats.
Returns:
| Type | Description |
|---|---|
|
A dictionary representation of the object's properties. |
Source code in unaiverse/dataprops.py
from_dict
staticmethod
¶
A static method that deserializes a dictionary into a DataProps object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
d_props
|
The dictionary containing the object's properties. |
required |
Returns:
| Type | Description |
|---|---|
|
A new |
Source code in unaiverse/dataprops.py
clone
¶
Creates and returns a deep copy of the DataProps instance.
It preserves the original transformation objects rather than re-evaluating them.
Returns:
| Type | Description |
|---|---|
|
A new |
Source code in unaiverse/dataprops.py
get_name
¶
Retrieves the name of the data stream.
Returns:
| Type | Description |
|---|---|
|
A string representing the stream's name. |
get_group
¶
Retrieves the name of the group to which the data stream belongs.
Returns:
| Type | Description |
|---|---|
|
A string representing the group name. |
get_description
¶
Retrieves the description of the data.
Returns:
| Type | Description |
|---|---|
|
A string with the data description. |
get_tensor_labels
¶
Retrieves the list of tensor labels, if they exist.
Returns:
| Type | Description |
|---|---|
|
A list of strings or None. |
set_name
¶
Sets a new name for the stream, with a check for invalid characters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The new name as a string. |
required |
set_group
¶
Sets a new group name for the stream, with a check for invalid characters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
group
|
str
|
The new group name as a string. |
required |
Source code in unaiverse/dataprops.py
set_description
¶
Sets a new description for the data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
desc
|
str
|
The new description as a string. |
required |
set_public
¶
Sets whether the stream is public or not.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
public
|
bool
|
A boolean value. |
required |
set_pubsub
¶
Sets whether the stream uses Pub/Sub or direct messaging.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pubsub
|
bool
|
A boolean value. |
required |
is_tensor
¶
Checks if the data type is 'tensor'.
Returns:
| Type | Description |
|---|---|
|
True if the type is 'tensor', False otherwise. |
is_img
¶
Checks if the data type is 'img'.
Returns:
| Type | Description |
|---|---|
|
True if the type is 'img', False otherwise. |
is_text
¶
Checks if the data type is 'text'.
Returns:
| Type | Description |
|---|---|
|
True if the type is 'text', False otherwise. |
is_tensor_long
¶
Checks if the tensor's data type is torch.long.
Returns:
| Type | Description |
|---|---|
|
True if the dtype is |
is_tensor_float
¶
Checks if the tensor's data type is a float type (e.g., torch.float32).
Returns:
| Type | Description |
|---|---|
|
True if the dtype is a float type, False otherwise. |
Source code in unaiverse/dataprops.py
is_tensor_img
¶
Checks if the tensor's shape corresponds to a typical image format (4D, with 1 or 3 channels).
Returns:
| Type | Description |
|---|---|
|
True if the shape matches, False otherwise. |
Source code in unaiverse/dataprops.py
is_tensor_token_ids
¶
Checks if the tensor represents token IDs, which is indicated by torch.long data type and a 2D shape
suitable for sequences.
Returns:
| Type | Description |
|---|---|
|
True if it matches, False otherwise. |
Source code in unaiverse/dataprops.py
is_tensor_target_id
¶
Checks if the tensor represents a single target ID, indicated by torch.long data type and a 1D shape.
Returns:
| Type | Description |
|---|---|
|
True if it matches, False otherwise. |
Source code in unaiverse/dataprops.py
is_all
¶
Checks if the data type is 'all', which is a wildcard type.
Returns:
| Type | Description |
|---|---|
|
True if the type is 'all', False otherwise. |
net_hash
¶
Generates a unique network hash for the stream using a provided prefix, Pub/Sub status, and name/group.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prefix
|
str
|
The prefix, typically the peer ID. |
required |
Returns:
| Type | Description |
|---|---|
|
A string representing the network hash. |
Source code in unaiverse/dataprops.py
peer_id_from_net_hash
staticmethod
¶
A static method to extract the peer ID from a network hash.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
net_hash
|
The network hash string. |
required |
Returns:
| Type | Description |
|---|---|
|
A string representing the peer ID. |
Source code in unaiverse/dataprops.py
name_or_group_from_net_hash
staticmethod
¶
A static method to extract the name or group from a network hash.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
net_hash
|
The network hash string. |
required |
Returns:
| Type | Description |
|---|---|
|
A string representing the name or group. |
Source code in unaiverse/dataprops.py
is_pubsub_from_net_hash
staticmethod
¶
A static method to check if a network hash belongs to a Pub/Sub stream.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
net_hash
|
The network hash string. |
required |
Returns:
| Type | Description |
|---|---|
|
True if the hash is for a Pub/Sub stream, False otherwise. |
Source code in unaiverse/dataprops.py
name_or_group
¶
Retrieves the group name if it's set, otherwise defaults to the stream name.
Returns:
| Type | Description |
|---|---|
|
A string representing the name or group. |
Source code in unaiverse/dataprops.py
build_net_hash
staticmethod
¶
A static method to construct a complete network hash from a prefix, Pub/Sub status, and name/group.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prefix
|
str
|
The peer ID prefix. |
required |
pubsub
|
bool
|
The Pub/Sub status. |
required |
name_or_group
|
str
|
The name or group of the stream. |
required |
Returns:
| Type | Description |
|---|---|
|
The constructed network hash string. |
Source code in unaiverse/dataprops.py
normalize_net_hash
staticmethod
¶
A static method that cleans up or normalizes a network hash string to a canonical format, particularly for direct messages.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
not_normalized_net_hash
|
str
|
The network hash to normalize. |
required |
Returns:
| Type | Description |
|---|---|
|
The normalized network hash string. |
Source code in unaiverse/dataprops.py
is_pubsub
¶
Checks if the stream is set to use Pub/Sub.
Returns:
| Type | Description |
|---|---|
|
True if it's a Pub/Sub stream, False otherwise. |
is_public
¶
Checks if the stream is set to be public.
Returns:
| Type | Description |
|---|---|
|
True if it's a public stream, False otherwise. |
set_tensor_labels_from_auto_tokenizer
¶
Initializes and sets the tensor labels by fetching the vocabulary from a Hugging Face AutoTokenizer
model ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_id
|
The ID of the tokenizer model. |
required |
Source code in unaiverse/dataprops.py
set_tensor_labels
¶
Sets the labels for the data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels
|
list[str] or None
|
List of labels to associate with the data. |
required |
labeling_rule
|
str
|
The labeling rule for the labels. |
'max'
|
Returns:
| Type | Description |
|---|---|
|
None |
Source code in unaiverse/dataprops.py
adapt_tensor_to_tensor_labels
¶
Interleaves data in function of its corresponding labels and the current super-set labels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Tensor
|
The data tensor to interleave. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
torch.Tensor: The interleaved data tensor. |
Source code in unaiverse/dataprops.py
clear_label_adaptation
¶
Removes the padding and returns the original data from an adapted tensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Tensor
|
The adapted tensor. |
required |
Returns:
| Type | Description |
|---|---|
|
The original, un-padded tensor. |
Source code in unaiverse/dataprops.py
is_flat_tensor_with_labels
¶
Checks if the tensor is a 2D array and has labels, which is a common structure for general feature data.
Returns:
| Type | Description |
|---|---|
|
True if it is, False otherwise. |
Source code in unaiverse/dataprops.py
has_tensor_labels
¶
Checks if any tensor labels are associated with the stream.
Returns:
| Type | Description |
|---|---|
|
True if labels exist, False otherwise. |
to_text
¶
Converts the tensor data into a text-based representation exploiting the given labels and the labeling rule.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Tensor or str
|
The data tensor to convert into text (if a string, then pass-through only). |
required |
Returns:
| Type | Description |
|---|---|
|
str or None: The corresponding text representation of the data. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the data type is not supported for conversion. |
Source code in unaiverse/dataprops.py
check_and_preprocess
¶
check_and_preprocess(data: str | Image | Tensor, allow_class_ids: bool = False, targets: bool = False, device: device = device('cpu'))
Prepares incoming data for a processor by validating its type and applying necessary transformations.
It handles different data types, including tensors, text (strings), and images, raising ValueError if
the data type is unexpected or incompatible with the stream's properties. For text and images, it can apply a
pre-configured transformation (like a tokenizer or a standard image transform) to convert the data into a
tensor format suitable for processing. For tensors, it performs validation on shape and data type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
str | Image | Tensor
|
The data sample to check and preprocess. |
required |
allow_class_ids
|
bool
|
A boolean to allow single-element long tensors, typically for class IDs. |
False
|
targets
|
bool
|
A boolean to indicate if the data is a target (used to select the correct transformation in a dual-transform setup). |
False
|
device
|
device
|
The PyTorch device (e.g., 'cpu' or 'cuda') to which the tensor should be moved. |
device('cpu')
|
Returns:
| Type | Description |
|---|---|
|
The preprocessed data, typically a tensor on the specified device. |
Source code in unaiverse/dataprops.py
804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 | |
check_and_postprocess
¶
Takes a processor's output and validates it before converting it back into a stream-compatible format.
It handles torch.Tensor data, applying a proc_to_stream_transform (if one exists) to convert the tensor
into an appropriate format for the stream, such as a string for text or a PIL Image for images. It performs
a final check on the data's format (shape, dtype, etc.) to ensure consistency with the stream's properties.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
str | Image | Tensor
|
The output from the processor, typically a tensor. |
required |
Returns:
| Type | Description |
|---|---|
|
The post-processed data, in a stream-compatible format (e.g., a string, image, or CPU tensor). |
Source code in unaiverse/dataprops.py
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 | |
is_compatible
¶
is_compatible(props_to_compare: DataProps) -> bool
Checks if the current DataProps instance is compatible with another DataProps instance. Checks include data type, shape, and labels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
props_to_compare
|
DataProps
|
The DataProps instance to check compatibility with. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if compatible, False otherwise. |
Source code in unaiverse/dataprops.py
TensorLabels
¶
TensorLabels(data_props: DataProps, labels: list[str] | None, labeling_rule: str = 'max')
A class to manage labels associated with data and perform operations on them.
Attributes:
| Name | Type | Description |
|---|---|---|
VALID_LABELING_RULES |
tuple
|
Tuple of valid labeling rules ('max', 'geq'). |
Initializes the TensorLabels instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data_props
|
DataProps
|
The DataProps instance that owns these labels. |
required |
labels
|
list[str] or None
|
List of labels. |
required |
labeling_rule
|
str
|
The rule for labeling (either 'max' or 'geqX', where X is a number). |
'max'
|
Returns:
| Type | Description |
|---|---|
|
None |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If the labels or labeling_rule are invalid. |
Methods:
| Name | Description |
|---|---|
to_dict |
Serializes the |
clear_indices |
Resets the internal |
interleave_with |
Interleaves the current labels with a super-set of labels, determining how to index them. |
Source code in unaiverse/dataprops.py
Methods:¶
to_dict
¶
Serializes the TensorLabels instance into a dictionary, which includes the list of labels and the original
labeling rule.
Returns:
| Type | Description |
|---|---|
|
A dictionary containing the labels and the original labeling rule. |
Source code in unaiverse/dataprops.py
clear_indices
¶
Resets the internal indices attribute to None. This effectively clears any previous label adaptation
that was performed and allows the object to revert to its original, non-interleaved state.
Source code in unaiverse/dataprops.py
interleave_with
¶
Interleaves the current labels with a super-set of labels, determining how to index them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
superset_labels
|
list[str]
|
The super-set of labels to interleave with. |
required |
Returns:
| Type | Description |
|---|---|
|
None |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If the super-set of labels is not compatible. |