utils.sample_field_operations

 1import logging
 2
 3import fiftyone as fo
 4
 5
 6def add_sample_field(
 7    v51_dataset_or_view, v51_field, v51_type, v51_embedded_doc_type=None
 8):
 9    """Wrapper for V51 function 'add_sample_field()' to also work for dataset views"""
10    type_dataset = fo.core.dataset.Dataset
11    type_dataset_view = fo.core.view.DatasetView
12    try:
13        if type(v51_dataset_or_view) == type_dataset:
14            dataset_operation = v51_dataset_or_view
15        elif type(v51_dataset_or_view) == type_dataset_view:
16            dataset_operation = fo.load_dataset(v51_dataset_or_view.dataset_name)
17        else:
18            logging.error(
19                f"Type {type(v51_dataset_or_view)} not supported for variable 'v51_dataset_or_view'"
20            )
21
22        dataset_operation.add_sample_field(v51_field, v51_type, v51_embedded_doc_type)
23    except Exception as e:
24        logging.error(f"Adding field {v51_field} of type {v51_type} failed: {e}")
25
26
27def rename_sample_field(v51_dataset_or_view, v51_field_old, v51_field_new):
28    """Wrapper for V51 function 'rename_sample_field()' to also work for dataset views"""
29    type_dataset = fo.core.dataset.Dataset
30    type_dataset_view = fo.core.view.DatasetView
31    try:
32        if type(v51_dataset_or_view) == type_dataset:
33            dataset_operation = v51_dataset_or_view
34        elif type(v51_dataset_or_view) == type_dataset_view:
35            dataset_operation = fo.load_dataset(v51_dataset_or_view.dataset_name)
36        else:
37            logging.error(
38                f"Type {type(v51_dataset_or_view)} not supported for variable 'v51_dataset_or_view'"
39            )
40
41        dataset_operation.rename_sample_field(v51_field_old, v51_field_new)
42    except Exception as e:
43        logging.error(f"Renaming field {v51_field_old} to {v51_field_new} failed: {e}")
def add_sample_field(v51_dataset_or_view, v51_field, v51_type, v51_embedded_doc_type=None):
 7def add_sample_field(
 8    v51_dataset_or_view, v51_field, v51_type, v51_embedded_doc_type=None
 9):
10    """Wrapper for V51 function 'add_sample_field()' to also work for dataset views"""
11    type_dataset = fo.core.dataset.Dataset
12    type_dataset_view = fo.core.view.DatasetView
13    try:
14        if type(v51_dataset_or_view) == type_dataset:
15            dataset_operation = v51_dataset_or_view
16        elif type(v51_dataset_or_view) == type_dataset_view:
17            dataset_operation = fo.load_dataset(v51_dataset_or_view.dataset_name)
18        else:
19            logging.error(
20                f"Type {type(v51_dataset_or_view)} not supported for variable 'v51_dataset_or_view'"
21            )
22
23        dataset_operation.add_sample_field(v51_field, v51_type, v51_embedded_doc_type)
24    except Exception as e:
25        logging.error(f"Adding field {v51_field} of type {v51_type} failed: {e}")

Wrapper for V51 function 'add_sample_field()' to also work for dataset views

def rename_sample_field(v51_dataset_or_view, v51_field_old, v51_field_new):
28def rename_sample_field(v51_dataset_or_view, v51_field_old, v51_field_new):
29    """Wrapper for V51 function 'rename_sample_field()' to also work for dataset views"""
30    type_dataset = fo.core.dataset.Dataset
31    type_dataset_view = fo.core.view.DatasetView
32    try:
33        if type(v51_dataset_or_view) == type_dataset:
34            dataset_operation = v51_dataset_or_view
35        elif type(v51_dataset_or_view) == type_dataset_view:
36            dataset_operation = fo.load_dataset(v51_dataset_or_view.dataset_name)
37        else:
38            logging.error(
39                f"Type {type(v51_dataset_or_view)} not supported for variable 'v51_dataset_or_view'"
40            )
41
42        dataset_operation.rename_sample_field(v51_field_old, v51_field_new)
43    except Exception as e:
44        logging.error(f"Renaming field {v51_field_old} to {v51_field_new} failed: {e}")

Wrapper for V51 function 'rename_sample_field()' to also work for dataset views