按已有标注筛选条目
只保留标注者以某种方式标注过的条目,例如被标为有毒的帖子,并通过一条命令或 data_files 设置把它们载入第二个 Potato 任务。
python -m potato.filter_by_annotation 读取某个 Potato 任务保存的答案,并写出一个新的数据文件,其中只包含标注者选择了指定标签的条目。新文件保留原条目的每个字段。第二个任务可以直接把该文件作为 data_files 条目载入,无需任何转换。典型的两阶段研究会先在一个快速的第一任务中筛查每条帖子是否有毒,再只把被标为有毒的帖子送入第二个任务,询问每条帖子包含哪种毒性。
当 data_files 条目带有 filter_by_prior_annotation 设置时,Potato 会在第二个任务的配置内部应用同样的筛选。命令会写出一个固定的文件,便于你检查和保存,而配置设置则在第二个服务器每次启动时重新筛选。
筛选所比较的答案
筛选会从第一个任务的 output_annotation_dir 中读取每位标注者保存的状态,路径为 <output_annotation_dir>/<user>/user_state.json。筛选从不读取导出文件,因此筛选前无需先导出第一个任务。
只有标签答案参与比较。Potato 把片段标注存放在状态文件的另一部分,筛选不会读取这一部分,所以片段方案无法用来选择条目。对于选择类方案,用于筛选的值是已保存的标签:
radio和multiselect。 即labels下书写的标签名。triage。 即accept、reject或skip,与按钮上显示的文字无关。
对于 multiselect,只要标注者勾选了某个标签,无论还勾选了其他什么,该标注者都会计入这个值。请先使用 --summary 运行命令,查看某个方案实际保存的值。
命令行选项
命令接受下表中的选项。--annotations 和 --schema 始终是必需的:
| 选项 | 默认值 | 选项的作用 |
|---|---|---|
--annotations, -a | 必需 | 第一个任务的 output_annotation_dir。 |
--schema, -s | 必需 | 要筛选的方案的 name,按完全匹配且区分大小写。 |
--data, -d | 无 | 原始数据文件,为 JSON 数组或 JSON Lines。筛选时必需。 |
--value, -v | 无 | 要保留的一个或多个值。给出多个值时,标注者选择其中任意一个即匹配。筛选时必需。 |
--output, -o | 无 | 筛选后条目的写入位置。缺少的上级文件夹会被创建。筛选时必需。 |
--rule | majority | 如何由多位标注者的答案决定是否匹配:majority、any 或 all。 |
--invert | 关闭 | 保留不匹配的条目。 |
--id-key | id | 每个数据条目中存放条目 ID 的字段。 |
--format | json | 输出格式,json 或 jsonl。 |
--summary | 关闭 | 打印每个值得到的答案数,而不执行筛选。 |
--verbose, -V | 关闭 | 打印调试日志。 |
缺少 --data、--value 或 --output 时,命令会报错并指出缺少的选项。--summary 不需要 --data 或 --value,且统计的是答案数而不是条目数。一个被三位标注者标为 toxic 的条目会使 toxic 的计数加 3。
多位标注者条目的规则
当 num_annotators_per_item 大于 1 时,每个条目带有每位标注者的一份答案,--rule 把这些答案转化为一个决定:
majority。 在回答了该方案的标注者中,超过半数选择了筛选值时,条目匹配。四位标注者中有两位属于平局,不匹配。any。 至少有一位标注者选择了筛选值时,条目匹配。all。 每位回答了该方案的标注者都选择了筛选值时,条目匹配。
只有回答了该方案的标注者才计入规则。没有任何人回答的条目永远不匹配,因此反向筛选会包含每个未回答的条目,以及未达到规则要求的条目。Potato 把裁定决定保存到 <output_annotation_dir>/adjudication/decisions.json,筛选不会读取该文件。在裁定页面上已解决的条目,仍然按标注者的投票来保留或丢弃。
输入与输出文件
命令读取写成 JSON 数组、单个 JSON 对象或 JSON Lines 的数据文件。在 JSON Lines 文件中,无法解析的行会被静默跳过。命令不读取 CSV、TSV 或 Parquet 文件,也不会给出提示。收到这类文件时,命令会把它当作 JSON Lines 读取,跳过每一行,并写出一个空的输出文件。对于 CSV、TSV 和 Parquet 数据,请使用在第二个配置中筛选里的配置设置。配置设置接受 data_files 条目所接受的每种格式。
输出按原顺序包含匹配的条目,并保留条目的每个字段。--format json 写出带缩进的 JSON 数组,--format jsonl 每行写出一个条目。两种文件都可以在另一个任务中用作 data_files 条目。
--id-key 必须指向与第一个任务中 item_properties.id_key 相同的字段,因为 Potato 把每个答案保存在条目 ID 之下。ID 按字符串比较。ID 字段缺失或为空的条目会被排除在输出之外,并给出警告。ID 为 null 时会变成字符串 None,不会给出警告,并且会保留在 --invert 的输出中。
毒性筛查的两阶段示例
示例把两个任务放在同一个研究文件夹内的相邻文件夹 screen/ 和 detail/ 中:
toxicity-study/
screen/
config.yaml
data/posts.jsonl
detail/
config.yaml
data/第一个任务把每条帖子展示给三位标注者,并提出一个是非问题。screen/data/posts.jsonl 每行包含一条帖子:
{"id": "p001", "text": "Thanks for the link, that fixed my install."}
{"id": "p002", "text": "Nobody wants you here. Leave."}
{"id": "p003", "text": "This patch is garbage and so are you."}
{"id": "p004", "text": "Has anyone tried the new release on Windows?"}screen/config.yaml 通过 num_annotators_per_item 把每条帖子发给三位标注者。每条帖子有三个答案,筛选才有票数可计:
annotation_task_name: "Toxicity screen"
task_dir: .
data_files:
- data/posts.jsonl
item_properties:
id_key: id
text_key: text
output_annotation_dir: annotation_output/
num_annotators_per_item: 3
user_config:
allow_all_users: true
annotation_schemes:
- annotation_type: radio
name: toxicity
description: Is this post toxic?
labels:
- toxic
- not_toxic在第一个任务的文件夹中启动它,让标注者完成这些帖子:
cd screen
potato start config.yaml -p 8000假设三位标注者都把 p002 标为有毒,三人中有两人把 p003 标为有毒,有一人把 p004 标为有毒。在研究文件夹中运行,摘要会显示第一个任务保存的内容:
python -m potato.filter_by_annotation \
--annotations screen/annotation_output/ \
--schema toxicity \
--summaryAnnotation summary for schema 'toxicity':
----------------------------------------
not_toxic: 6 (50.0%)
toxic: 6 (50.0%)
----------------------------------------
Total answers: 12使用默认 majority 规则的筛选会保留三位标注者中至少两位标为有毒的帖子,并把保留的帖子写入第二个任务的数据文件夹:
python -m potato.filter_by_annotation \
--annotations screen/annotation_output/ \
--data screen/data/posts.jsonl \
--schema toxicity \
--value toxic \
--output detail/data/toxic_posts.json命令会打印 Wrote 2 items to detail/data/toxic_posts.json,文件中包含 p002 和 p003 及其原始文本。在相同答案下,--rule any 保留 p002、p003 和 p004,--rule all 只保留 p002。
detail/config.yaml 载入筛选后的文件,并询问每条帖子包含哪些种类的毒性:
annotation_task_name: "Toxicity categories"
task_dir: .
data_files:
- data/toxic_posts.json
item_properties:
id_key: id
text_key: text
output_annotation_dir: annotation_output/
user_config:
allow_all_users: true
annotation_schemes:
- annotation_type: multiselect
name: toxicity_type
description: Which kinds of toxicity does this post contain?
labels:
- insult
- threat
- identity_attack
- profanity先执行 cd detail,再用 potato start config.yaml -p 8001 在自己的端口上启动第二个任务。第二个任务的标注者只会看到 p002 和 p003。
在第二个配置中筛选
写成映射的 data_files 条目可以带有 filter_by_prior_annotation 块,Potato 会在服务器启动加载数据时筛选该文件。这样第二个任务就不需要单独的筛选后文件。下面的条目替换上面 detail/config.yaml 中的 data_files 列表:
data_files:
- path: data/posts.jsonl
filter_by_prior_annotation:
annotation_dir: ../screen/annotation_output/
schema: toxicity
value: toxic
rule: majority该块接受下表中的选项:
| 选项 | 默认值 | 选项的作用 |
|---|---|---|
annotation_dir | 必需 | 第一个任务的 output_annotation_dir。 |
schema | 必需 | 要筛选的方案。 |
value | 必需 | 一个值,或一个值的列表。 |
rule | majority | majority、any 或 all,详见多位标注者条目的规则。 |
invert | false | 保留不匹配的条目。 |
database | 无 | 第一个任务的 database 块,适用于第一个任务把标注者状态存放在 MySQL 中的情况。此时筛选会从该数据库而不是 annotation_dir 读取标注者。 |
当数据文件解析到启动 Potato 的文件夹之外时,Potato 会在启动时以配置错误终止。因此从 detail/ 启动时,path 不能指回 screen/,所以请先把 posts.jsonl 复制到 detail/data/。从包含两个任务的文件夹中使用 potato start detail/config.yaml 启动,同样可以通过检查。Potato 不会对 annotation_dir 做同样的检查,而是从第二个任务的文件夹解析该路径,因此 ../screen/annotation_output/ 能够找到第一个任务的答案。
配置筛选与命令有三点不同:
- 筛选何时运行。 Potato 只在启动时筛选一次。第一个任务之后保存的答案,要重启后才会到达第二个任务。
- 缺少选项。 缺少
annotation_dir、schema或value时,Potato 会记录警告并加载未经筛选的整个文件。annotation_dir不存在时会产生警告且完全不加载任何条目,因为没有任何条目有答案。 - 规则与格式。
rule不是这三个名称之一时,Potato 会在启动时终止。配置筛选适用于 CSV、TSV、JSON、JSON Lines 和 Parquet 文件,并且只适用于data_files条目,不适用于data_sources。
对于使用 MySQL 的第一个任务,仍然必须设置 annotation_dir,因为缺少 annotation_dir 时 Potato 会跳过该块。命令行工具没有数据库选项,只读取目录。
Python API
同样的筛选也可以作为 Python 函数使用。filter_items_by_annotation 以字典列表的形式返回匹配的条目:
from potato.filter_by_annotation import filter_items_by_annotation, get_annotation_summary
toxic = filter_items_by_annotation(
annotation_dir="screen/annotation_output/",
data_file="screen/data/posts.jsonl",
schema_name="toxicity",
filter_value="toxic",
id_key="id",
invert=False,
rule="majority",
)
counts = get_annotation_summary("screen/annotation_output/", "toxicity")filter_value 接受一个字符串或字符串列表。get_annotation_summary 返回一个字典,把每个值映射到其答案数。
延伸阅读
- 分流,用于快速初筛的接受、拒绝和跳过按钮
- 单选与多选,筛选所比较的选择类方案
- 任务分配,介绍
num_annotators_per_item和每位标注者的上限 - 裁定,用于解决标注者存在分歧的条目
- 数据格式,介绍
data_files条目可以载入的内容 - 导出格式,用于导出第二个任务的答案
- 用户模拟器,在正式研究之前用合成标注者测试两阶段设置
实现细节请参阅源文档。