Aggregate responses in pool

Aggregate the responses from Tolokers for all completed tasks in a pool.

Tip

We recommend that you try our crowd-kit library. It has various aggregation methods and executes on your computer.

Steps to follow

1. Import Toloka-Kit

Connect the Toloka-Kit library to your script.

import toloka.client as toloka

2. Instantiate TolokaClient

Replace the sample OAuth token with your own one.

toloka_client = toloka.
('AQC2AGAJgyNSA8CtpdO9MWy_QEB6s6kDjHUoElE', 'PRODUCTION');

3. Configure aggregation operation

Set up the parameters which you want to use for the aggregation: in this example we choose tasks from the pool with the pool_id value, set the aggregation type to DAWID_SKENE, and specify output fields to aggregate. The aggregate_solutions_by_pool() method starts the aggregation.

aggregation_operation = toloka_client.
(

type=toloka.aggregation.AggregatedSolutionType.DAWID_SKENE,

pool_id='36502086',

fields=[toloka.aggregation.PoolAggregatedSolutionRequest.Field(name='result')]

)

4. Start aggregation and wait for result

Aggregation takes some time depending on the number of tasks and responses from Tolokers. The wait_operation() method displays the progress of the aggregation operation and shows when it finishes.

aggregation_operation = toloka_client.
(aggregation_operation)

The output from the code portion above will look like this.

100%|████████████████████████████████████████████| 100/100 [00:52<00:00, 1.92it/s]

5. Display aggregation results

After the aggregation operation 100% finished, get the results with the help of the get_aggregated_solutions() method and iterate through them.

aggregation_results = toloka_client.
(aggregation_operation.id)

for result in aggregation_results:

print(result.task_id, result.output_values['result'])

You should get an output with the task IDs and the result output data values which looks like this.

00022cfa46--637cf3e76e13181a0164e4b3 cat
00022cfa46--637cf3e76e13181a0164e4b7 cat
00022cfa46--637cf3e76e13181a0164e4bc dog
00022cfa46--637cf3e86e13181a0164e4c0 cat
00022cfa46--637cf3e86e13181a0164e4c4 cat
00022cfa46--637cf3e86e13181a0164e4c8 dog
00022cfa46--637cf3e86e13181a0164e4cd dog
00022cfa46--637cf3e86e13181a0164e4d0 dog

Complete code: Aggregate responses in pool

import toloka.client as toloka
toloka_client = toloka.TolokaClient('AQC2AGAJgyNSA8CtpdO9MWy_QEB6s6kDjHUoElE', 'PRODUCTION')
aggregation_operation = toloka_client.aggregate_solutions_by_pool(
type=toloka.aggregation.AggregatedSolutionType.DAWID_SKENE,
pool_id='36502086',
fields=[toloka.aggregation.PoolAggregatedSolutionRequest.Field(name='result')]
)
aggregation_operation = toloka_client.wait_operation(aggregation_operation)
aggregation_results = toloka_client.get_aggregated_solutions(aggregation_operation.id)
for result in aggregation_results:
print(result.task_id, result.output_values['result'])
List of classes and methods used in this recipe

See also

Toloka-Kit
OverviewGetting OAuth tokenQuick start
Recipes
Reference
toloka.client
toloka.async_client
toloka.autoquality [autoquality]
toloka.metrics
toloka.streaming