Skip to content

Data Types API

Description

A data type defines the type of data that is emitted and stored by the Sensor. A data type can be associated with multiple Data Formats.

The following data types are pre-defined, along with their data_type_id:

Data Type data_type_id
Default 0
RGB 1
NIR 2
Thermal 3
Multispectral 4
Weather 5
GPS 6
Calibration 7
Depth 8
IMU 9
Disparity 10
Confidence 11

Module

This module defines the DataType class, which represents a data type for categorizing and describing data.

It includes methods for creating, retrieving, updating, and deleting data types, as well as methods for checking existence, searching, and managing associations with data formats.

This module includes the following methods:

  • exists: Check if a data type with the given name exists.
  • create: Create a new data type.
  • get: Retrieve a data type by its name.
  • get_by_id: Retrieve a data type by its ID.
  • get_all: Retrieve all data types.
  • search: Search for data types based on various criteria.
  • update: Update the details of a data type.
  • delete: Delete a data type.
  • refresh: Refresh the data type's data from the database.
  • get_info: Get the additional information of the data type.
  • set_info: Set the additional information of the data type.
  • get_associated_data_formats: Get all data formats associated with the data type.
  • associate_data_format: Associate the data type with a data format.
  • unassociate_data_format: Unassociate the data type from a data format.
  • belongs_to_data_format: Check if the data type is associated with a specific data format.
  • add_new_data_format: Create and associate a new data format with the data type.

DataType

Bases: APIBase

Represents a data type for categorizing and describing data.

Attributes:

Name Type Description
id Optional[ID]

The unique identifier of the data type.

data_type_name str

The name of the data type (e.g., "Temperature", "Yield").

data_type_info Optional[dict]

Additional information about the data type.

Source code in gemini/api/data_type.py
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
class DataType(APIBase):
    """
    Represents a data type for categorizing and describing data.

    Attributes:
        id (Optional[ID]): The unique identifier of the data type.
        data_type_name (str): The name of the data type (e.g., "Temperature", "Yield").
        data_type_info (Optional[dict]): Additional information about the data type.
    """

    id: Optional[ID] = Field(None, validation_alias=AliasChoices("id", "data_type_id"))

    data_type_name: str
    data_type_info: Optional[dict] = None

    def __str__(self):
        """Return a string representation of the DataType object."""
        return f"DataType(data_type_name={self.data_type_name}, id={self.id})"

    def __repr__(self):
        """Return a detailed string representation of the DataType object."""
        return f"DataType(data_type_name={self.data_type_name}, id={self.id})"

    @classmethod
    def exists(
        cls,
        data_type_name: str
    ) -> bool:
        """
        Check if a data type with the given name exists.

        Examples:
            >>> DataType.exists("Temperature")
            True

        Args:
            data_type_name (str): The name of the data type.
        Returns:
            bool: True if the data type exists, False otherwise.
        """
        try:
            exists = DataTypeModel.exists(data_type_name=data_type_name)
            return exists
        except Exception as e:
            logger.error(f"Error checking existence of data type: {e}")
            return False

    @classmethod
    def create(
        cls,
        data_type_name: str,
        data_type_info: dict = None,
    ) -> Optional["DataType"]:
        """
        Create a new data type. If a data type with the same name already exists, it will return that instance.

        Examples:
            >>> DataType.create("Temperature", {"unit": "Celsius"})
            DataType(data_type_name="Temperature", id=...)

        Args:
            data_type_name (str): The name of the data type.
            data_type_info (dict, optional): Additional information about the data type. Defaults to {{}}.
        Returns:
            Optional["DataType"]: The created data type, or None if an error occurred.
        """
        try:
            db_instance = DataTypeModel.get_or_create(
                data_type_name=data_type_name,
                data_type_info=data_type_info,
            )
            instance = cls.model_validate(db_instance)
            return instance
        except Exception as e:
            logger.error(f"Error creating data type: {e}")
            return None

    @classmethod
    def get(cls, data_type_name: str) -> Optional["DataType"]:
        """
        Retrieve a data type by its name.

        Examples:
            >>> DataType.get("Temperature")
            DataType(data_type_name="Temperature", id=...)

        Args:
            data_type_name (str): The name of the data type.
        Returns:
            Optional["DataType"]: The data type, or None if not found.
        """
        try:
            db_instance = DataTypeModel.get_by_parameters(data_type_name=data_type_name)
            if not db_instance:
                logger.debug(f"Data type with name {data_type_name} not found.")
                return None
            instance = cls.model_validate(db_instance)
            return instance
        except Exception as e:
            logger.error(f"Error getting data type: {e}")
            return None

    @classmethod
    def get_by_id(cls, id: UUID | int | str) -> Optional["DataType"]:
        """
        Retrieve a data type by its ID.

        Examples:
            >>> DataType.get_by_id(...)  
            DataType(data_type_name="Temperature", id=...)

        Args:
            id (UUID | int | str): The ID of the data type.
        Returns:
            Optional["DataType"]: The data type, or None if not found.
        """
        try:
            db_instance = DataTypeModel.get(id)
            if not db_instance:
                logger.warning(f"Data type with ID {id} does not exist.")
                return None
            instance = cls.model_validate(db_instance)
            return instance
        except Exception as e:
            logger.error(f"Error getting data type by ID: {e}")
            return None

    @classmethod
    def get_all(cls, limit: int = None, offset: int = None) -> Optional[List["DataType"]]:
        """
        Retrieve all data types.

        Examples:
            >>> DataType.get_all()
            [DataType(data_type_name="Temperature", id=...), DataType(data_type_name="Yield", id=...)]


        Returns:
            Optional[List["DataType"]]: A list of all data types, or None if an error occurred.
        """
        try:
            instances = DataTypeModel.all(limit=limit, offset=offset)
            if not instances or len(instances) == 0:
                logger.info("No data types found.")
                return None
            instances = [cls.model_validate(instance) for instance in instances]
            return instances
        except Exception as e:
            logger.error(f"Error getting all data types: {e}")
            return None

    @classmethod
    def search(
        cls,
        data_type_name: str = None,
        data_type_info: dict = None
    ) -> Optional[List["DataType"]]:
        """
        Search for data types based on various criteria.

        Examples:
            >>> DataType.search(data_type_name="Temperature")
            [DataType(data_type_name="Temperature", id=...)]

        Args:
            data_type_name (str, optional): The name of the data type. Defaults to None.
            data_type_info (dict, optional): Additional information about the data type. Defaults to None.
        Returns:
            Optional[List["DataType"]]: A list of matching data types, or None if an error occurred.
        """
        try:
            if not any([data_type_name, data_type_info]):
                logger.warning("At least one search parameter must be provided.")
                return None

            instances = DataTypeModel.search(
                data_type_name=data_type_name,
                data_type_info=data_type_info
            )
            if not instances or len(instances) == 0:
                logger.info("No data types found with the provided search parameters.")
                return None
            instances = [cls.model_validate(instance) for instance in instances]
            return instances
        except Exception as e:
            logger.error(f"Error searching data types: {e}")
            return None

    def update(
        self,
        data_type_name: str = None,
        data_type_info: dict = None,
    ) -> Optional["DataType"]:
        """
        Update the details of the data type.

        Examples:
            >>> data_type = DataType.get("Temperature")
            >>> updated_data_type = data_type.update(data_type_name="New Temperature", data_type_info={"unit": "Fahrenheit"})
            >>> print(updated_data_type)
            DataType(data_type_name="New Temperature", id=...)

        Args:
            data_type_name (str, optional): The new name of the data type. Defaults to None.
            data_type_info (dict, optional): The new information. Defaults to None.
        Returns:
            Optional["DataType"]: The updated data type, or None if an error occurred.
        """
        try:
            if not any([data_type_name, data_type_info]):
                logger.warning("At least one parameter must be provided for update.")
                return None

            current_id = self.id
            data_type = DataTypeModel.get(current_id)
            if not data_type:
                logger.warning(f"Data type with ID {current_id} does not exist.")
                return None

            data_type = DataTypeModel.update(
                data_type,
                data_type_name=data_type_name,
                data_type_info=data_type_info
            )
            instance = self.model_validate(data_type)
            self.refresh()
            return instance
        except Exception as e:
            logger.error(f"Error updating data type: {e}")
            return None

    def delete(self) -> bool:
        """
        Delete the data type.

        Examples:
            >>> data_type = DataType.get("Temperature")
            >>> success = data_type.delete()
            >>> print(success)
            True

        Returns:
            bool: True if the data type was deleted, False otherwise.
        """
        try:
            current_id = self.id
            data_type = DataTypeModel.get(current_id)
            if not data_type:
                logger.warning(f"Data type with ID {current_id} does not exist.")
                return False
            DataTypeModel.delete(data_type)
            return True
        except Exception as e:
            logger.error(f"Error deleting data type: {e}")
            return False

    def refresh(self) -> Optional["DataType"]:
        """
        Refresh the data type's data from the database. It is rarely called by the user
        as it is automatically called on access.

        Examples:
            >>> data_type = DataType.get("Temperature")
            >>> refreshed_data_type = data_type.refresh()
            >>> print(refreshed_data_type)
            DataType(data_type_name="Temperature", id=...)

        Returns:
            Optional["DataType"]: The refreshed data type, or None if an error occurred.
        """
        try:
            db_instance = DataTypeModel.get(self.id)
            if not db_instance:
                logger.warning(f"Data type with ID {self.id} does not exist.")
                return self
            instance = self.model_validate(db_instance)
            for key, value in instance.model_dump().items():
                if hasattr(self, key) and key != "id":
                    setattr(self, key, value)
            return self
        except Exception as e:
            logger.error(f"Error refreshing data type: {e}")
            return None

    def get_info(self) -> Optional[dict]:
        """
        Get the additional information of the data type.

        Examples:
            >>> data_type = DataType.get("Temperature")
            >>> info = data_type.get_info()
            >>> print(info)
            {"unit": "Celsius"}

        Returns:
            Optional[dict]: The data type's info, or None if not found.
        """
        try:
            current_id = self.id
            data_type = DataTypeModel.get(current_id)
            if not data_type:
                logger.warning(f"Data type with ID {current_id} does not exist.")
                return None
            data_type_info = data_type.data_type_info
            if not data_type_info:
                logger.info("DataType info is empty.")
                return None
            return data_type_info
        except Exception as e:
            logger.error(f"Error getting data type info: {e}")
            return None

    def set_info(self, data_type_info: dict) -> Optional["DataType"]:
        """
        Set the additional information of the data type.

        Examples:
            >>> data_type = DataType.get("Temperature")
            >>> updated_data_type = data_type.set_info({"unit": "Fahrenheit"})
            >>> print(updated_data_type.data_type_info)
            {"unit": "Fahrenheit"}

        Args:
            data_type_info (dict): The new information to set.
        Returns:
            Optional["DataType"]: The updated data type, or None if an error occurred.
        """
        try:
            current_id = self.id
            data_type = DataTypeModel.get(current_id)
            if not data_type:
                logger.warning(f"Data type with ID {current_id} does not exist.")
                return None
            data_type = DataTypeModel.update(
                data_type,
                data_type_info=data_type_info,
            )
            instance = self.model_validate(data_type)
            self.refresh()
            return instance
        except Exception as e:
            logger.error(f"Error setting data type info: {e}")
            return None

    def get_associated_data_formats(self) -> Optional[List["DataFormat"]]:
        """
        Get all data formats associated with the data type.

        Examples:
            >>> data_type = DataType.get("Temperature")
            >>> data_formats = data_type.get_associated_data_formats()
            >>> for df in data_formats:
            ...     print(df)
            DataFormat(data_format_name="CSV", data_format_mime_type="text/csv", id=...)
            DataFormat(data_format_name="JSON", data_format_mime_type="application/json", id=...)


        Returns:
            Optional[List["DataFormat"]]: A list of associated data formats, or None if not found.
        """
        try:
            from gemini.api.data_format import DataFormat
            current_id = self.id
            data_type_formats = DataTypeFormatsViewModel.search(data_type_id=current_id)
            if not data_type_formats or len(data_type_formats) == 0:
                logger.info(f"No associated data formats found for data type ID {current_id}.")
                return None
            data_formats = [DataFormat.model_validate(df) for df in data_type_formats]
            return data_formats
        except Exception as e:
            logger.error(f"Error getting associated data formats: {e}")
            return None

    def associate_data_format(self, data_format_name: str) -> Optional["DataType"]:
        """
        Associate the data type with a data format.

        Examples:
            >>> data_type = DataType.get("Temperature")
            >>> associated_data_format = data_type.associate_data_format("CSV")
            >>> print(associated_data_format)
            DataFormat(data_format_name="CSV", data_format_mime_type="text/csv", id=...)

        Args:
            data_format_name (str): The name of the data format to associate.
        Returns:
            Optional["DataType"]: The associated data format, or None if an error occurred.
        """
        try:
            from gemini.api.data_format import DataFormat
            data_format = DataFormat.get(data_format_name=data_format_name)
            if not data_format:
                logger.warning(f"Data format {data_format_name} does not exist.")
                return None
            existing_association = DataTypeFormatModel.get_by_parameters(
                data_type_id=self.id,
                data_format_id=data_format.id
            )
            if existing_association:
                logger.info(f"Data format {data_format_name} is already associated with data type ID {self.id}.")
                return data_format
            new_association = DataTypeFormatModel.get_or_create(
                data_type_id=self.id,
                data_format_id=data_format.id
            )
            if not new_association:
                logger.info(f"Failed to associate data format {data_format_name} with data type ID {self.id}.")
                return None
            self.refresh()
            return data_format
        except Exception as e:
            logger.error(f"Error associating data format: {e}")
            return None

    def unassociate_data_format(self, data_format_name: str) -> Optional["DataFormat"]:
        """
        Unassociate the data type from a data format.

        Examples:
            >>> data_type = DataType.get("Temperature")
            >>> unassociated_data_format = data_type.unassociate_data_format("CSV")
            >>> print(unassociated_data_format)
            DataFormat(data_format_name="CSV", data_format_mime_type="text/csv", id=...)

        Args:
            data_format_name (str): The name of the data format to unassociate.
        Returns:
            Optional["DataFormat"]: The unassociated data format, or None if an error occurred.
        """
        try:
            from gemini.api.data_format import DataFormat
            data_format = DataFormat.get(data_format_name=data_format_name)
            if not data_format:
                logger.warning(f"Data format {data_format_name} does not exist.")
                return None
            existing_association = DataTypeFormatModel.get_by_parameters(
                data_type_id=self.id,
                data_format_id=data_format.id
            )
            if not existing_association:
                logger.info(f"Data format {data_format_name} is not associated with data type ID {self.id}.")
                return None
            is_deleted = DataTypeFormatModel.delete(existing_association)
            if not is_deleted:
                logger.info(f"Failed to unassociate data format {data_format_name} from data type ID {self.id}.")
                return None
            self.refresh()
            return data_format
        except Exception as e:
            logger.error(f"Error unassociating data format: {e}")
            return None

    def belongs_to_data_format(self, data_format_name: str) -> bool:
        """
        Check if the data type is associated with a specific data format.

        Examples:
            >>> data_type = DataType.get("Temperature")
            >>> is_associated = data_type.belongs_to_data_format("CSV")
            >>> print(is_associated)
            True

        Args:
            data_format_name (str): The name of the data format to check.
        Returns:
            bool: True if associated, False otherwise.
        """
        try:
            from gemini.api.data_format import DataFormat
            data_format = DataFormat.get(data_format_name=data_format_name)
            if not data_format:
                logger.warning(f"Data format {data_format_name} does not exist.")
                return False
            association_exists = DataTypeFormatModel.exists(
                data_type_id=self.id,
                data_format_id=data_format.id
            )
            return association_exists
        except Exception as e:
            logger.error(f"Error checking if data type belongs to data format: {e}")
            return False

    def add_new_data_format(
        self,
        data_format_name: str,
        data_format_mime_type: str = None,
        data_format_info: dict = None
    ) -> Optional["DataFormat"]:
        """
        Create and associate a new data format with the data type. If the data format already exists,
        it will associate the existing data format with the data type.

        Examples:
            >>> data_type = DataType.get("Temperature")
            >>> new_data_format = data_type.add_new_data_format("NewFormat", "application/newformat", {"description": "A new format"})
            >>> print(new_data_format)
            DataFormat(data_format_name="NewFormat", id=5, data_format_mime_type="application/newformat", id=...)

        Args:
            data_format_name (str): The name of the new data format.
            data_format_mime_type (str, optional): The MIME type of the data format. Defaults to None.
            data_format_info (dict, optional): Additional information about the data format. Defaults to {{}}.
        Returns:
            Optional["DataFormat"]: The created and associated data format, or None if an error occurred.
        """
        try:
            from gemini.api.data_format import DataFormat
            new_data_format = DataFormat.create(
                data_format_name=data_format_name,
                data_format_mime_type=data_format_mime_type,
                data_format_info=data_format_info
            )
            if not new_data_format:
                logger.info(f"Failed to create new data format {data_format_name}.")
                return None
            new_association = DataTypeFormatModel.get_or_create(
                data_type_id=self.id,
                data_format_id=new_data_format.id
            )
            if not new_association:
                logger.info(f"Failed to associate new data format {data_format_name} with data type ID {self.id}.")
                return None
            self.refresh()
            return new_data_format
        except Exception as e:
            logger.error(f"Error adding new data format: {e}")
            return None

__repr__()

Return a detailed string representation of the DataType object.

Source code in gemini/api/data_type.py
def __repr__(self):
    """Return a detailed string representation of the DataType object."""
    return f"DataType(data_type_name={self.data_type_name}, id={self.id})"

__str__()

Return a string representation of the DataType object.

Source code in gemini/api/data_type.py
def __str__(self):
    """Return a string representation of the DataType object."""
    return f"DataType(data_type_name={self.data_type_name}, id={self.id})"

add_new_data_format(data_format_name, data_format_mime_type=None, data_format_info=None)

Create and associate a new data format with the data type. If the data format already exists, it will associate the existing data format with the data type.

Examples:

>>> data_type = DataType.get("Temperature")
>>> new_data_format = data_type.add_new_data_format("NewFormat", "application/newformat", {"description": "A new format"})
>>> print(new_data_format)
DataFormat(data_format_name="NewFormat", id=5, data_format_mime_type="application/newformat", id=...)

Parameters:

Name Type Description Default
data_format_name str

The name of the new data format.

required
data_format_mime_type str

The MIME type of the data format. Defaults to None.

None
data_format_info dict

Additional information about the data format. Defaults to {{}}.

None

Returns: Optional["DataFormat"]: The created and associated data format, or None if an error occurred.

Source code in gemini/api/data_type.py
def add_new_data_format(
    self,
    data_format_name: str,
    data_format_mime_type: str = None,
    data_format_info: dict = None
) -> Optional["DataFormat"]:
    """
    Create and associate a new data format with the data type. If the data format already exists,
    it will associate the existing data format with the data type.

    Examples:
        >>> data_type = DataType.get("Temperature")
        >>> new_data_format = data_type.add_new_data_format("NewFormat", "application/newformat", {"description": "A new format"})
        >>> print(new_data_format)
        DataFormat(data_format_name="NewFormat", id=5, data_format_mime_type="application/newformat", id=...)

    Args:
        data_format_name (str): The name of the new data format.
        data_format_mime_type (str, optional): The MIME type of the data format. Defaults to None.
        data_format_info (dict, optional): Additional information about the data format. Defaults to {{}}.
    Returns:
        Optional["DataFormat"]: The created and associated data format, or None if an error occurred.
    """
    try:
        from gemini.api.data_format import DataFormat
        new_data_format = DataFormat.create(
            data_format_name=data_format_name,
            data_format_mime_type=data_format_mime_type,
            data_format_info=data_format_info
        )
        if not new_data_format:
            logger.info(f"Failed to create new data format {data_format_name}.")
            return None
        new_association = DataTypeFormatModel.get_or_create(
            data_type_id=self.id,
            data_format_id=new_data_format.id
        )
        if not new_association:
            logger.info(f"Failed to associate new data format {data_format_name} with data type ID {self.id}.")
            return None
        self.refresh()
        return new_data_format
    except Exception as e:
        logger.error(f"Error adding new data format: {e}")
        return None

associate_data_format(data_format_name)

Associate the data type with a data format.

Examples:

>>> data_type = DataType.get("Temperature")
>>> associated_data_format = data_type.associate_data_format("CSV")
>>> print(associated_data_format)
DataFormat(data_format_name="CSV", data_format_mime_type="text/csv", id=...)

Parameters:

Name Type Description Default
data_format_name str

The name of the data format to associate.

required

Returns: Optional["DataType"]: The associated data format, or None if an error occurred.

Source code in gemini/api/data_type.py
def associate_data_format(self, data_format_name: str) -> Optional["DataType"]:
    """
    Associate the data type with a data format.

    Examples:
        >>> data_type = DataType.get("Temperature")
        >>> associated_data_format = data_type.associate_data_format("CSV")
        >>> print(associated_data_format)
        DataFormat(data_format_name="CSV", data_format_mime_type="text/csv", id=...)

    Args:
        data_format_name (str): The name of the data format to associate.
    Returns:
        Optional["DataType"]: The associated data format, or None if an error occurred.
    """
    try:
        from gemini.api.data_format import DataFormat
        data_format = DataFormat.get(data_format_name=data_format_name)
        if not data_format:
            logger.warning(f"Data format {data_format_name} does not exist.")
            return None
        existing_association = DataTypeFormatModel.get_by_parameters(
            data_type_id=self.id,
            data_format_id=data_format.id
        )
        if existing_association:
            logger.info(f"Data format {data_format_name} is already associated with data type ID {self.id}.")
            return data_format
        new_association = DataTypeFormatModel.get_or_create(
            data_type_id=self.id,
            data_format_id=data_format.id
        )
        if not new_association:
            logger.info(f"Failed to associate data format {data_format_name} with data type ID {self.id}.")
            return None
        self.refresh()
        return data_format
    except Exception as e:
        logger.error(f"Error associating data format: {e}")
        return None

belongs_to_data_format(data_format_name)

Check if the data type is associated with a specific data format.

Examples:

>>> data_type = DataType.get("Temperature")
>>> is_associated = data_type.belongs_to_data_format("CSV")
>>> print(is_associated)
True

Parameters:

Name Type Description Default
data_format_name str

The name of the data format to check.

required

Returns: bool: True if associated, False otherwise.

Source code in gemini/api/data_type.py
def belongs_to_data_format(self, data_format_name: str) -> bool:
    """
    Check if the data type is associated with a specific data format.

    Examples:
        >>> data_type = DataType.get("Temperature")
        >>> is_associated = data_type.belongs_to_data_format("CSV")
        >>> print(is_associated)
        True

    Args:
        data_format_name (str): The name of the data format to check.
    Returns:
        bool: True if associated, False otherwise.
    """
    try:
        from gemini.api.data_format import DataFormat
        data_format = DataFormat.get(data_format_name=data_format_name)
        if not data_format:
            logger.warning(f"Data format {data_format_name} does not exist.")
            return False
        association_exists = DataTypeFormatModel.exists(
            data_type_id=self.id,
            data_format_id=data_format.id
        )
        return association_exists
    except Exception as e:
        logger.error(f"Error checking if data type belongs to data format: {e}")
        return False

create(data_type_name, data_type_info=None) classmethod

Create a new data type. If a data type with the same name already exists, it will return that instance.

Examples:

>>> DataType.create("Temperature", {"unit": "Celsius"})
DataType(data_type_name="Temperature", id=...)

Parameters:

Name Type Description Default
data_type_name str

The name of the data type.

required
data_type_info dict

Additional information about the data type. Defaults to {{}}.

None

Returns: Optional["DataType"]: The created data type, or None if an error occurred.

Source code in gemini/api/data_type.py
@classmethod
def create(
    cls,
    data_type_name: str,
    data_type_info: dict = None,
) -> Optional["DataType"]:
    """
    Create a new data type. If a data type with the same name already exists, it will return that instance.

    Examples:
        >>> DataType.create("Temperature", {"unit": "Celsius"})
        DataType(data_type_name="Temperature", id=...)

    Args:
        data_type_name (str): The name of the data type.
        data_type_info (dict, optional): Additional information about the data type. Defaults to {{}}.
    Returns:
        Optional["DataType"]: The created data type, or None if an error occurred.
    """
    try:
        db_instance = DataTypeModel.get_or_create(
            data_type_name=data_type_name,
            data_type_info=data_type_info,
        )
        instance = cls.model_validate(db_instance)
        return instance
    except Exception as e:
        logger.error(f"Error creating data type: {e}")
        return None

delete()

Delete the data type.

Examples:

>>> data_type = DataType.get("Temperature")
>>> success = data_type.delete()
>>> print(success)
True

Returns:

Name Type Description
bool bool

True if the data type was deleted, False otherwise.

Source code in gemini/api/data_type.py
def delete(self) -> bool:
    """
    Delete the data type.

    Examples:
        >>> data_type = DataType.get("Temperature")
        >>> success = data_type.delete()
        >>> print(success)
        True

    Returns:
        bool: True if the data type was deleted, False otherwise.
    """
    try:
        current_id = self.id
        data_type = DataTypeModel.get(current_id)
        if not data_type:
            logger.warning(f"Data type with ID {current_id} does not exist.")
            return False
        DataTypeModel.delete(data_type)
        return True
    except Exception as e:
        logger.error(f"Error deleting data type: {e}")
        return False

exists(data_type_name) classmethod

Check if a data type with the given name exists.

Examples:

>>> DataType.exists("Temperature")
True

Parameters:

Name Type Description Default
data_type_name str

The name of the data type.

required

Returns: bool: True if the data type exists, False otherwise.

Source code in gemini/api/data_type.py
@classmethod
def exists(
    cls,
    data_type_name: str
) -> bool:
    """
    Check if a data type with the given name exists.

    Examples:
        >>> DataType.exists("Temperature")
        True

    Args:
        data_type_name (str): The name of the data type.
    Returns:
        bool: True if the data type exists, False otherwise.
    """
    try:
        exists = DataTypeModel.exists(data_type_name=data_type_name)
        return exists
    except Exception as e:
        logger.error(f"Error checking existence of data type: {e}")
        return False

get(data_type_name) classmethod

Retrieve a data type by its name.

Examples:

>>> DataType.get("Temperature")
DataType(data_type_name="Temperature", id=...)

Parameters:

Name Type Description Default
data_type_name str

The name of the data type.

required

Returns: Optional["DataType"]: The data type, or None if not found.

Source code in gemini/api/data_type.py
@classmethod
def get(cls, data_type_name: str) -> Optional["DataType"]:
    """
    Retrieve a data type by its name.

    Examples:
        >>> DataType.get("Temperature")
        DataType(data_type_name="Temperature", id=...)

    Args:
        data_type_name (str): The name of the data type.
    Returns:
        Optional["DataType"]: The data type, or None if not found.
    """
    try:
        db_instance = DataTypeModel.get_by_parameters(data_type_name=data_type_name)
        if not db_instance:
            logger.debug(f"Data type with name {data_type_name} not found.")
            return None
        instance = cls.model_validate(db_instance)
        return instance
    except Exception as e:
        logger.error(f"Error getting data type: {e}")
        return None

get_all(limit=None, offset=None) classmethod

Retrieve all data types.

Examples:

>>> DataType.get_all()
[DataType(data_type_name="Temperature", id=...), DataType(data_type_name="Yield", id=...)]

Returns:

Type Description
Optional[List[DataType]]

Optional[List["DataType"]]: A list of all data types, or None if an error occurred.

Source code in gemini/api/data_type.py
@classmethod
def get_all(cls, limit: int = None, offset: int = None) -> Optional[List["DataType"]]:
    """
    Retrieve all data types.

    Examples:
        >>> DataType.get_all()
        [DataType(data_type_name="Temperature", id=...), DataType(data_type_name="Yield", id=...)]


    Returns:
        Optional[List["DataType"]]: A list of all data types, or None if an error occurred.
    """
    try:
        instances = DataTypeModel.all(limit=limit, offset=offset)
        if not instances or len(instances) == 0:
            logger.info("No data types found.")
            return None
        instances = [cls.model_validate(instance) for instance in instances]
        return instances
    except Exception as e:
        logger.error(f"Error getting all data types: {e}")
        return None

get_associated_data_formats()

Get all data formats associated with the data type.

Examples:

>>> data_type = DataType.get("Temperature")
>>> data_formats = data_type.get_associated_data_formats()
>>> for df in data_formats:
...     print(df)
DataFormat(data_format_name="CSV", data_format_mime_type="text/csv", id=...)
DataFormat(data_format_name="JSON", data_format_mime_type="application/json", id=...)

Returns:

Type Description
Optional[List[DataFormat]]

Optional[List["DataFormat"]]: A list of associated data formats, or None if not found.

Source code in gemini/api/data_type.py
def get_associated_data_formats(self) -> Optional[List["DataFormat"]]:
    """
    Get all data formats associated with the data type.

    Examples:
        >>> data_type = DataType.get("Temperature")
        >>> data_formats = data_type.get_associated_data_formats()
        >>> for df in data_formats:
        ...     print(df)
        DataFormat(data_format_name="CSV", data_format_mime_type="text/csv", id=...)
        DataFormat(data_format_name="JSON", data_format_mime_type="application/json", id=...)


    Returns:
        Optional[List["DataFormat"]]: A list of associated data formats, or None if not found.
    """
    try:
        from gemini.api.data_format import DataFormat
        current_id = self.id
        data_type_formats = DataTypeFormatsViewModel.search(data_type_id=current_id)
        if not data_type_formats or len(data_type_formats) == 0:
            logger.info(f"No associated data formats found for data type ID {current_id}.")
            return None
        data_formats = [DataFormat.model_validate(df) for df in data_type_formats]
        return data_formats
    except Exception as e:
        logger.error(f"Error getting associated data formats: {e}")
        return None

get_by_id(id) classmethod

Retrieve a data type by its ID.

Examples:

>>> DataType.get_by_id(...)  
DataType(data_type_name="Temperature", id=...)

Parameters:

Name Type Description Default
id UUID | int | str

The ID of the data type.

required

Returns: Optional["DataType"]: The data type, or None if not found.

Source code in gemini/api/data_type.py
@classmethod
def get_by_id(cls, id: UUID | int | str) -> Optional["DataType"]:
    """
    Retrieve a data type by its ID.

    Examples:
        >>> DataType.get_by_id(...)  
        DataType(data_type_name="Temperature", id=...)

    Args:
        id (UUID | int | str): The ID of the data type.
    Returns:
        Optional["DataType"]: The data type, or None if not found.
    """
    try:
        db_instance = DataTypeModel.get(id)
        if not db_instance:
            logger.warning(f"Data type with ID {id} does not exist.")
            return None
        instance = cls.model_validate(db_instance)
        return instance
    except Exception as e:
        logger.error(f"Error getting data type by ID: {e}")
        return None

get_info()

Get the additional information of the data type.

Examples:

>>> data_type = DataType.get("Temperature")
>>> info = data_type.get_info()
>>> print(info)
{"unit": "Celsius"}

Returns:

Type Description
Optional[dict]

Optional[dict]: The data type's info, or None if not found.

Source code in gemini/api/data_type.py
def get_info(self) -> Optional[dict]:
    """
    Get the additional information of the data type.

    Examples:
        >>> data_type = DataType.get("Temperature")
        >>> info = data_type.get_info()
        >>> print(info)
        {"unit": "Celsius"}

    Returns:
        Optional[dict]: The data type's info, or None if not found.
    """
    try:
        current_id = self.id
        data_type = DataTypeModel.get(current_id)
        if not data_type:
            logger.warning(f"Data type with ID {current_id} does not exist.")
            return None
        data_type_info = data_type.data_type_info
        if not data_type_info:
            logger.info("DataType info is empty.")
            return None
        return data_type_info
    except Exception as e:
        logger.error(f"Error getting data type info: {e}")
        return None

refresh()

Refresh the data type's data from the database. It is rarely called by the user as it is automatically called on access.

Examples:

>>> data_type = DataType.get("Temperature")
>>> refreshed_data_type = data_type.refresh()
>>> print(refreshed_data_type)
DataType(data_type_name="Temperature", id=...)

Returns:

Type Description
Optional[DataType]

Optional["DataType"]: The refreshed data type, or None if an error occurred.

Source code in gemini/api/data_type.py
def refresh(self) -> Optional["DataType"]:
    """
    Refresh the data type's data from the database. It is rarely called by the user
    as it is automatically called on access.

    Examples:
        >>> data_type = DataType.get("Temperature")
        >>> refreshed_data_type = data_type.refresh()
        >>> print(refreshed_data_type)
        DataType(data_type_name="Temperature", id=...)

    Returns:
        Optional["DataType"]: The refreshed data type, or None if an error occurred.
    """
    try:
        db_instance = DataTypeModel.get(self.id)
        if not db_instance:
            logger.warning(f"Data type with ID {self.id} does not exist.")
            return self
        instance = self.model_validate(db_instance)
        for key, value in instance.model_dump().items():
            if hasattr(self, key) and key != "id":
                setattr(self, key, value)
        return self
    except Exception as e:
        logger.error(f"Error refreshing data type: {e}")
        return None

search(data_type_name=None, data_type_info=None) classmethod

Search for data types based on various criteria.

Examples:

>>> DataType.search(data_type_name="Temperature")
[DataType(data_type_name="Temperature", id=...)]

Parameters:

Name Type Description Default
data_type_name str

The name of the data type. Defaults to None.

None
data_type_info dict

Additional information about the data type. Defaults to None.

None

Returns: Optional[List["DataType"]]: A list of matching data types, or None if an error occurred.

Source code in gemini/api/data_type.py
@classmethod
def search(
    cls,
    data_type_name: str = None,
    data_type_info: dict = None
) -> Optional[List["DataType"]]:
    """
    Search for data types based on various criteria.

    Examples:
        >>> DataType.search(data_type_name="Temperature")
        [DataType(data_type_name="Temperature", id=...)]

    Args:
        data_type_name (str, optional): The name of the data type. Defaults to None.
        data_type_info (dict, optional): Additional information about the data type. Defaults to None.
    Returns:
        Optional[List["DataType"]]: A list of matching data types, or None if an error occurred.
    """
    try:
        if not any([data_type_name, data_type_info]):
            logger.warning("At least one search parameter must be provided.")
            return None

        instances = DataTypeModel.search(
            data_type_name=data_type_name,
            data_type_info=data_type_info
        )
        if not instances or len(instances) == 0:
            logger.info("No data types found with the provided search parameters.")
            return None
        instances = [cls.model_validate(instance) for instance in instances]
        return instances
    except Exception as e:
        logger.error(f"Error searching data types: {e}")
        return None

set_info(data_type_info)

Set the additional information of the data type.

Examples:

>>> data_type = DataType.get("Temperature")
>>> updated_data_type = data_type.set_info({"unit": "Fahrenheit"})
>>> print(updated_data_type.data_type_info)
{"unit": "Fahrenheit"}

Parameters:

Name Type Description Default
data_type_info dict

The new information to set.

required

Returns: Optional["DataType"]: The updated data type, or None if an error occurred.

Source code in gemini/api/data_type.py
def set_info(self, data_type_info: dict) -> Optional["DataType"]:
    """
    Set the additional information of the data type.

    Examples:
        >>> data_type = DataType.get("Temperature")
        >>> updated_data_type = data_type.set_info({"unit": "Fahrenheit"})
        >>> print(updated_data_type.data_type_info)
        {"unit": "Fahrenheit"}

    Args:
        data_type_info (dict): The new information to set.
    Returns:
        Optional["DataType"]: The updated data type, or None if an error occurred.
    """
    try:
        current_id = self.id
        data_type = DataTypeModel.get(current_id)
        if not data_type:
            logger.warning(f"Data type with ID {current_id} does not exist.")
            return None
        data_type = DataTypeModel.update(
            data_type,
            data_type_info=data_type_info,
        )
        instance = self.model_validate(data_type)
        self.refresh()
        return instance
    except Exception as e:
        logger.error(f"Error setting data type info: {e}")
        return None

unassociate_data_format(data_format_name)

Unassociate the data type from a data format.

Examples:

>>> data_type = DataType.get("Temperature")
>>> unassociated_data_format = data_type.unassociate_data_format("CSV")
>>> print(unassociated_data_format)
DataFormat(data_format_name="CSV", data_format_mime_type="text/csv", id=...)

Parameters:

Name Type Description Default
data_format_name str

The name of the data format to unassociate.

required

Returns: Optional["DataFormat"]: The unassociated data format, or None if an error occurred.

Source code in gemini/api/data_type.py
def unassociate_data_format(self, data_format_name: str) -> Optional["DataFormat"]:
    """
    Unassociate the data type from a data format.

    Examples:
        >>> data_type = DataType.get("Temperature")
        >>> unassociated_data_format = data_type.unassociate_data_format("CSV")
        >>> print(unassociated_data_format)
        DataFormat(data_format_name="CSV", data_format_mime_type="text/csv", id=...)

    Args:
        data_format_name (str): The name of the data format to unassociate.
    Returns:
        Optional["DataFormat"]: The unassociated data format, or None if an error occurred.
    """
    try:
        from gemini.api.data_format import DataFormat
        data_format = DataFormat.get(data_format_name=data_format_name)
        if not data_format:
            logger.warning(f"Data format {data_format_name} does not exist.")
            return None
        existing_association = DataTypeFormatModel.get_by_parameters(
            data_type_id=self.id,
            data_format_id=data_format.id
        )
        if not existing_association:
            logger.info(f"Data format {data_format_name} is not associated with data type ID {self.id}.")
            return None
        is_deleted = DataTypeFormatModel.delete(existing_association)
        if not is_deleted:
            logger.info(f"Failed to unassociate data format {data_format_name} from data type ID {self.id}.")
            return None
        self.refresh()
        return data_format
    except Exception as e:
        logger.error(f"Error unassociating data format: {e}")
        return None

update(data_type_name=None, data_type_info=None)

Update the details of the data type.

Examples:

>>> data_type = DataType.get("Temperature")
>>> updated_data_type = data_type.update(data_type_name="New Temperature", data_type_info={"unit": "Fahrenheit"})
>>> print(updated_data_type)
DataType(data_type_name="New Temperature", id=...)

Parameters:

Name Type Description Default
data_type_name str

The new name of the data type. Defaults to None.

None
data_type_info dict

The new information. Defaults to None.

None

Returns: Optional["DataType"]: The updated data type, or None if an error occurred.

Source code in gemini/api/data_type.py
def update(
    self,
    data_type_name: str = None,
    data_type_info: dict = None,
) -> Optional["DataType"]:
    """
    Update the details of the data type.

    Examples:
        >>> data_type = DataType.get("Temperature")
        >>> updated_data_type = data_type.update(data_type_name="New Temperature", data_type_info={"unit": "Fahrenheit"})
        >>> print(updated_data_type)
        DataType(data_type_name="New Temperature", id=...)

    Args:
        data_type_name (str, optional): The new name of the data type. Defaults to None.
        data_type_info (dict, optional): The new information. Defaults to None.
    Returns:
        Optional["DataType"]: The updated data type, or None if an error occurred.
    """
    try:
        if not any([data_type_name, data_type_info]):
            logger.warning("At least one parameter must be provided for update.")
            return None

        current_id = self.id
        data_type = DataTypeModel.get(current_id)
        if not data_type:
            logger.warning(f"Data type with ID {current_id} does not exist.")
            return None

        data_type = DataTypeModel.update(
            data_type,
            data_type_name=data_type_name,
            data_type_info=data_type_info
        )
        instance = self.model_validate(data_type)
        self.refresh()
        return instance
    except Exception as e:
        logger.error(f"Error updating data type: {e}")
        return None