Simulate with Python API

The API is still under development and can change at any time without warning.

For more control over the workings of the ASReview software, the ASReview Python API can be used. For example, it is possible to use custom models or implement different sampling strategies. This example shows how to simulate a review with the ASReview API and store the results in an ASReview project file.

Please keep in mind that the ASReview API is experimental at the moment. Improvements and simplifications are planned.

from pathlib import Path

from asreview import ASReviewData, ASReviewProject
from import ReviewSimulate

Create a temporary folder for the results and examples in this document.

project_path = Path("tmp_data")

Create an ASReviewProject to store the results

# Create a project object and folder
project = ASReviewProject.create(
    project_path=project_path / "api_simulation",

Add a dataset to the project folder in the folder data (can also be stored somewhere else, but it is advised to use the data folder). In the following example, a dataset is downloaded from the benchmark platform with CURL (macOS, Unix systems).

curl > tmp_data/api_simulation/data/van_de_Schoot_2017.csv
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  9.9M  100  9.9M    0     0  13.2M      0 --:--:-- --:--:-- --:--:-- 13.2M

Add the reference to the dataset to the project.


Setup the models.

from asreview.models.classifiers import NaiveBayesClassifier
from asreview.models.query import MaxQuery
from asreview.models.balance import DoubleBalance
from asreview.models.feature_extraction import Tfidf

# Select models to use
train_model = NaiveBayesClassifier()
query_model = MaxQuery()
balance_model = DoubleBalance()
feature_model = Tfidf()

Run the simulation with the ReviewSimulate class.

data_obj = ASReviewData.from_file(Path("tmp_data", "api_simulation", "data", "van_de_Schoot_2017.csv"))
# Initialize the simulation reviewer
reviewer = ReviewSimulate(
    as_data=            data_obj,
    model=              train_model,
    query_model=        query_model,
    balance_model=      balance_model,
    feature_model=      feature_model,
    n_instances=        10,
    project=            project,
    n_prior_included=   1,
    n_prior_excluded=   1,
# Start the review process
except Exception as err:
    raise err

Export the project to a location of choice, in this case tmp_data/api_example.asreview.

# Finish and export the project
project.export(Path("tmp_data", "api_example.asreview"))

The following code removes the temporary folder that was created:

import shutil