跳轉到內容

按已有標註篩選條目

只保留標註者以某種方式標註過的條目,例如被標為有毒的帖子,並通過一條命令或 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無篩選後條目的寫入位置。缺少的上級資料夾會被建立。篩選時必需。
--rulemajority如何由多位標註者的答案決定是否匹配:majority、any 或 all。
--invert關閉保留不匹配的條目。
--id-keyid每個資料條目中存放條目 ID 的欄位。
--formatjson輸出格式,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/ 中:

text
toxicity-study/
  screen/
    config.yaml
    data/posts.jsonl
  detail/
    config.yaml
    data/

第一個任務把每條帖子展示給三位標註者,並提出一個是非問題。screen/data/posts.jsonl 每行包含一條帖子:

json
{"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 把每條帖子發給三位標註者。每條帖子有三個答案,篩選才有票數可計:

yaml
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

在第一個任務的資料夾中啟動它,讓標註者完成這些帖子:

bash
cd screen
potato start config.yaml -p 8000

假設三位標註者都把 p002 標為有毒,三人中有兩人把 p003 標為有毒,有一人把 p004 標為有毒。在研究資料夾中執行,摘要會顯示第一個任務儲存的內容:

bash
python -m potato.filter_by_annotation \
    --annotations screen/annotation_output/ \
    --schema toxicity \
    --summary
text
Annotation summary for schema 'toxicity':
----------------------------------------
  not_toxic: 6 (50.0%)
  toxic: 6 (50.0%)
----------------------------------------
  Total answers: 12

使用預設 majority 規則的篩選會保留三位標註者中至少兩位標為有毒的帖子,並把保留的帖子寫入第二個任務的資料資料夾:

bash
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 載入篩選後的檔案,並詢問每條帖子包含哪些種類的毒性:

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 列表:

yaml
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必需一個值,或一個值的列表。
rulemajoritymajority、any 或 all,詳見多位標註者條目的規則。
invertfalse保留不匹配的條目。
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 以字典列表的形式返回匹配的條目:

python
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 條目可以載入的內容
  • 匯出格式,用於匯出第二個任務的答案
  • 使用者模擬器,在正式研究之前用合成標註者測試兩階段設定

實現細節請參閱源文件。