> For the complete documentation index, see [llms.txt](https://docs.unitlab.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.unitlab.ai/api-sdk-cli/zh/sdks/data-group-automation.md).

# Data Group 自动化

使用 Unitlab Python SDK 根据文件名，将资产库中的相关文件（如同一时刻的多个相机视角）自动分组为 Data Groups。

Data Group 将多个文件放在同一个任务中，标注员可以并排对照标注。典型的分组包括机器人工位在同一时间戳的四个相机画面，或同一医学检查的多个视图。分组根据资产库文件夹中的文件名构建。

## 让 Unitlab 自动识别模式 <a href="#let-unitlab-find-the-pattern" id="let-unitlab-find-the-pattern"></a>

```python
folder = client.assets.folder("FOLDER_ID")
suggestion = folder.suggest_grouping()
print(suggestion.get("suggested"), suggestion.get("group_name_template"))

result = folder.auto_group()          # uses the suggestion
print(result.created_count, "groups in", result.grouped_folder().name)
```

如果不传配置调用 `auto_group()` 且未找到可信的模式，会抛出 `ValidationError`。此时请自行描述模式。

## 自定义模式 <a href="#describe-the-pattern" id="describe-the-pattern"></a>

对于名为 `cell04_0001_front.jpg`、`cell04_0001_left.jpg` 等的文件：

```python
from unitlab import tiles_from_template

config = tiles_from_template(
    "{cell}_{frame}_{view}",
    tile_values={"view": ["front", "left", "right", "wrist"]},
)
estimate = folder.estimate_grouping(config)
print(estimate)

result = folder.auto_group(config)
```

`tile_values` 中列出的字段用于命名分块（tile），其他字段（`cell`、`frame`）用于标识分组。返回结果是普通 dict，可在传入前调整：

| 键                           | 默认值                   | 含义                                                 |
| --------------------------- | --------------------- | -------------------------------------------------- |
| `grouping_keys`             | 不在 `tile_values` 中的字段 | 决定哪些文件属于同一分组                                       |
| `group_name_template`       | `"{cell} {frame}"`    | 每个分组的名称                                            |
| `tiles`                     | 每种组合一个                | `tile_id`、`title`、`match_rule`（正则表达式）、`exclusions` |
| `minimum_matched_tiles`     | `1`                   | 创建分组所需的最少分块数                                       |
| `required_tiles`            | `[]`                  | 每个分组必须包含的分块 ID                                     |
| `incomplete_group_handling` | `allow_incomplete`    | 如何处理缺少分块的分组                                        |
| `layout_type`               | `grid`                | 分块在 Workbench 中的排列方式                               |
| `recursive`                 | `True`                | 包含子文件夹                                             |

## 限制 <a href="#limits" id="limits"></a>

一个文件夹最多可包含 5,000 个文件，每次运行最多生成 1,000 个分组。超出时 `auto_group()` 会抛出 `ValidationError`。请将文件拆分到更小的文件夹中并逐个运行。

## 分组之后 <a href="#after-grouping" id="after-grouping"></a>

将分组后的文件夹（或由其构建的数据集）关联到项目。每个分组会成为一个 `kind="group"` 的 Data Unit。

## 相关内容 <a href="#related" id="related"></a>

[文件夹](https://docs.unitlab.ai/api-sdk-cli/zh/sdks/folders.md) · [关联与取消关联资产库数据](https://docs.unitlab.ai/api-sdk-cli/zh/sdks/attach-and-detach-project-sources.md) · [Data Unit](https://docs.unitlab.ai/api-sdk-cli/zh/sdks/data-units.md)
