utils.sidebar_groups

 1import fiftyone as fo
 2
 3
 4def arrange_fields_in_groups(dataset):
 5    """Arranges dataset fields into groups based on predefined task categories and updates the dataset's sidebar configuration."""
 6    task_field_mapping = {  # Prefixes end with a "_"
 7        "Object Detection": ["pred_od_"],
 8        "Zero-Shot Object Detection": ["pred_zsod_"],
 9        "Ensemble Selection": ["n_unique_ensemble_selection"],
10        "Semantic Segmentation": ["pred_ss_"],
11        "Depth Estimation": ["pred_de_"],
12        "Anomaly Detection": ["pred_anomaly_"],
13        "Embedding Selection": [
14            "embedding_selection",
15            "embedding_selection_model",
16            "embedding_selection_count",
17        ],
18        "Embedding Computation": ["representativeness_", "uniqueness_", "distance"],
19        "Evaluation": ["eval_"],
20        "Data Recording": ["location", "timestamp", "name", "time_of_day"],
21    }
22
23    dataset_fields = dataset.get_field_schema()
24
25    # Create a new dict for each task which has a list of all fields that match the prefix or fieldname in dataset_fields
26    task_groups = {}
27    for task, prefixes in task_field_mapping.items():
28        task_groups[task] = []
29        for field_name in dataset_fields:
30            # Check if field matches any prefix or exact field name
31            if (
32                any(field_name.startswith(prefix) for prefix in prefixes)
33                or field_name in prefixes
34            ):
35                task_groups[task].append(field_name)
36
37    sidebar_groups = fo.DatasetAppConfig.default_sidebar_groups(dataset)
38
39    # Create a mapping of fields to their original groups for later removal
40    field_to_original_group = {}
41    for group in sidebar_groups:
42        for path in group.paths:
43            field_to_original_group[path] = group.name
44
45    # Create a new group for every task that has at least one field
46    n_added_groups = 0
47    moved_fields = set()
48    for task, fields in task_groups.items():
49        if len(fields) > 0:
50            group = fo.SidebarGroupDocument(name=task)
51            group.paths = []
52            for field in fields:
53                group.paths.append(field)
54                moved_fields.add(field)
55            sidebar_groups.append(group)
56            n_added_groups += 1
57
58    if n_added_groups > 0:
59        for group in sidebar_groups:
60            # Collapse default groups
61            if group.name in [
62                "metadata",
63                "other",
64                "primitives",
65                "Embedding Computation",
66            ]:
67                group.expanded = False
68
69            # Remove moved fields from their original groups
70            group.paths = [
71                path
72                for path in group.paths
73                if not (
74                    path in moved_fields and field_to_original_group[path] == group.name
75                )
76            ]
77
78        dataset.app_config.sidebar_groups = sidebar_groups
79        dataset.save()
def arrange_fields_in_groups(dataset):
 5def arrange_fields_in_groups(dataset):
 6    """Arranges dataset fields into groups based on predefined task categories and updates the dataset's sidebar configuration."""
 7    task_field_mapping = {  # Prefixes end with a "_"
 8        "Object Detection": ["pred_od_"],
 9        "Zero-Shot Object Detection": ["pred_zsod_"],
10        "Ensemble Selection": ["n_unique_ensemble_selection"],
11        "Semantic Segmentation": ["pred_ss_"],
12        "Depth Estimation": ["pred_de_"],
13        "Anomaly Detection": ["pred_anomaly_"],
14        "Embedding Selection": [
15            "embedding_selection",
16            "embedding_selection_model",
17            "embedding_selection_count",
18        ],
19        "Embedding Computation": ["representativeness_", "uniqueness_", "distance"],
20        "Evaluation": ["eval_"],
21        "Data Recording": ["location", "timestamp", "name", "time_of_day"],
22    }
23
24    dataset_fields = dataset.get_field_schema()
25
26    # Create a new dict for each task which has a list of all fields that match the prefix or fieldname in dataset_fields
27    task_groups = {}
28    for task, prefixes in task_field_mapping.items():
29        task_groups[task] = []
30        for field_name in dataset_fields:
31            # Check if field matches any prefix or exact field name
32            if (
33                any(field_name.startswith(prefix) for prefix in prefixes)
34                or field_name in prefixes
35            ):
36                task_groups[task].append(field_name)
37
38    sidebar_groups = fo.DatasetAppConfig.default_sidebar_groups(dataset)
39
40    # Create a mapping of fields to their original groups for later removal
41    field_to_original_group = {}
42    for group in sidebar_groups:
43        for path in group.paths:
44            field_to_original_group[path] = group.name
45
46    # Create a new group for every task that has at least one field
47    n_added_groups = 0
48    moved_fields = set()
49    for task, fields in task_groups.items():
50        if len(fields) > 0:
51            group = fo.SidebarGroupDocument(name=task)
52            group.paths = []
53            for field in fields:
54                group.paths.append(field)
55                moved_fields.add(field)
56            sidebar_groups.append(group)
57            n_added_groups += 1
58
59    if n_added_groups > 0:
60        for group in sidebar_groups:
61            # Collapse default groups
62            if group.name in [
63                "metadata",
64                "other",
65                "primitives",
66                "Embedding Computation",
67            ]:
68                group.expanded = False
69
70            # Remove moved fields from their original groups
71            group.paths = [
72                path
73                for path in group.paths
74                if not (
75                    path in moved_fields and field_to_original_group[path] == group.name
76                )
77            ]
78
79        dataset.app_config.sidebar_groups = sidebar_groups
80        dataset.save()

Arranges dataset fields into groups based on predefined task categories and updates the dataset's sidebar configuration.