Skip to content

Filter Items by Earlier Annotations

Keep only the items annotators labeled a given way, such as posts marked toxic, and load them into a second Potato task with a command or a data_files setting.

python -m potato.filter_by_annotation reads the answers saved by one Potato task and writes a new data file holding only the items whose annotators chose a given label. The new file keeps every field of the original items. A second task loads the file as its data_files entry without any conversion. A typical two-stage study screens every post for toxicity in a fast first task, then sends only the posts marked toxic to a second task that asks what kind of toxicity each post contains.

Potato applies the same filter inside the second task's config when a data_files entry carries a filter_by_prior_annotation setting. The command writes a fixed file you can inspect and keep, while the config setting filters again every time the second server starts.

Answers the filter compares

The filter reads each annotator's saved state from the first task's output_annotation_dir, at <output_annotation_dir>/<user>/user_state.json. The filter never reads export files. You do not need to export the first task before filtering.

Only label answers take part in the comparison. Potato stores span annotations in a separate part of the state file, and the filter does not read that part. A span scheme therefore cannot select items. For the choice schemes, the value you filter on is the stored label:

  • radio and multiselect. The label name as written under labels.
  • triage. accept, reject, or skip, whatever captions the buttons show.

A multiselect annotator counts toward a value when the annotator ticked that label, whatever else the annotator ticked. Run the command with --summary first to see the exact values stored for a scheme.

Command-line options

The command takes the options below. --annotations and --schema are always required:

OptionDefaultWhat the option controls
--annotations, -arequiredThe first task's output_annotation_dir.
--schema, -srequiredThe name of the scheme to filter on, matched exactly and case-sensitively.
--data, -dnoneThe original data file, as a JSON array or JSON Lines. Required for filtering.
--value, -vnoneOne or more values to keep. Several values match an annotator who chose any one of the listed values. Required for filtering.
--output, -ononeWhere to write the filtered items. Missing parent folders are created. Required for filtering.
--rulemajorityHow several annotators' answers decide a match: majority, any, or all.
--invertoffKeeps the items that do not match.
--id-keyidThe field in each data item that holds the item ID.
--formatjsonThe output format, json or jsonl.
--summaryoffPrints how many answers each value received, in place of filtering.
--verbose, -VoffPrints debug logging.

Without --data, --value, or --output, the command stops with an error naming the missing option. --summary needs neither --data nor --value, and counts answers rather than items. An item that three annotators labeled toxic adds 3 to the toxic count.

Rules for items with several annotators

With num_annotators_per_item above 1, each item carries one answer per annotator, and --rule turns those answers into one decision:

  • majority. The item matches when more than half of the annotators who answered the scheme chose a filter value. Two of four annotators is a tie and does not match.
  • any. The item matches when at least one annotator chose a filter value.
  • all. The item matches when every annotator who answered the scheme chose a filter value.

Only annotators who answered the scheme count toward the rule. Items no one has answered never match. An inverted filter therefore includes every unanswered item, along with the items that fell short of the rule. Potato saves adjudication decisions to <output_annotation_dir>/adjudication/decisions.json, and the filter does not read that file. An item resolved on the adjudication page is still kept or dropped by the annotators' votes.

Input and output files

The command reads a data file written as a JSON array, as one JSON object, or as JSON Lines. In a JSON Lines file, a line that does not parse is skipped without a warning. The command does not read CSV, TSV, or Parquet files, and does not say so. Given one, the command reads the file as JSON Lines, skips every line, and writes an empty output file. For CSV, TSV, and Parquet data, use the config setting in Filtering inside the second config. The config setting accepts every format a data_files entry accepts.

The output holds the matching items in their original order, with every field the items had. --format json writes an indented JSON array and --format jsonl writes one item per line. Either file works as a data_files entry in another task.

--id-key must name the same field as item_properties.id_key in the first task, because Potato saves each answer under the item's ID. The IDs are compared as strings. An item whose ID field is missing or empty is left out of the output with a warning. An ID of null becomes the string None, draws no warning, and stays in an --invert output.

Two-stage example with a toxicity screen

The example keeps the two tasks in sibling folders, screen/ and detail/, inside one study folder:

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

The first task shows each post to three annotators and asks one yes-or-no question. screen/data/posts.jsonl holds one post per line:

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 sends each post to three annotators with num_annotators_per_item. Three answers per post give the filter votes to count:

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

Start the first task from its folder and let the annotators work through the posts:

bash
cd screen
potato start config.yaml -p 8000

Suppose all three annotators marked p002 toxic, two of the three marked p003 toxic, and one marked p004 toxic. From the study folder, the summary shows what the first task stored:

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

The filter with the default majority rule keeps the posts that at least two of the three annotators marked toxic, and writes the kept posts into the second task's data folder:

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

The command prints Wrote 2 items to detail/data/toxic_posts.json, and the file holds p002 and p003 with the original text. With the same answers, --rule any keeps p002, p003, and p004, and --rule all keeps p002 alone.

detail/config.yaml loads the filtered file and asks which kinds of toxicity each post contains:

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

Start the second task on its own port with cd detail and potato start config.yaml -p 8001. The second task's annotators see only p002 and p003.

Filtering inside the second config

A data_files entry written as a mapping takes a filter_by_prior_annotation block, and Potato filters the file when the server loads its data at startup. The second task then needs no separate filtered file. The entry below replaces the data_files list in detail/config.yaml above:

yaml
data_files:
  - path: data/posts.jsonl
    filter_by_prior_annotation:
      annotation_dir: ../screen/annotation_output/
      schema: toxicity
      value: toxic
      rule: majority

The block takes the options below:

OptionDefaultWhat the option controls
annotation_dirrequiredThe first task's output_annotation_dir.
schemarequiredThe scheme to filter on.
valuerequiredOne value, or a list of values.
rulemajoritymajority, any, or all, as described in Rules for items with several annotators.
invertfalseKeeps the items that do not match.
databasenoneThe first task's database block, when the first task stored annotator state in MySQL. The filter then reads the annotators from that database in place of annotation_dir.

Potato stops at startup with a configuration error when a data file resolves outside the folder you start Potato from. Started from detail/, the path therefore cannot point back into screen/, so copy posts.jsonl into detail/data/ first. Starting Potato from the folder that holds both tasks, with potato start detail/config.yaml, also passes the check. Potato does not check annotation_dir the same way and resolves the path from the second task's folder. ../screen/annotation_output/ therefore reaches the first task's answers.

The config filter behaves differently from the command in three ways:

  • When the filter runs. Potato filters once, at startup. Answers the first task saves afterwards reach the second task only after a restart.
  • Missing options. Without annotation_dir, schema, or value, Potato logs a warning and loads the whole file unfiltered. An annotation_dir that does not exist produces a warning and loads no items at all, because no item has an answer.
  • Rules and formats. Potato stops at startup on a rule other than the three names. The config filter works on CSV, TSV, JSON, JSON Lines, and Parquet files, and only on data_files entries, never on data_sources.

For a MySQL-backed first task, annotation_dir must still be set, because Potato skips the block when annotation_dir is missing. The command-line tool has no database option and reads only the directory.

Python API

The same filter is available as a Python function. filter_items_by_annotation returns the matching items as a list of dictionaries:

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 takes one string or a list of strings. get_annotation_summary returns a dictionary from each value to its number of answers.

Further reading

For implementation details, see the source documentation.