# Comprehensive deterministic counterbalancing using pavlovias "shelf"

**URL:** <https://discourse.psychopy.org/t/comprehensive-deterministic-counterbalancing-using-pavlovias-shelf/30351>\
**Category:** Online experiments\
**Tags:** counterbalancing, pavlovia, shelf\
**Created:** [August 10, 2022, 8:42am UTC](https://discourse.psychopy.org/t/comprehensive-deterministic-counterbalancing-using-pavlovias-shelf/30351 "2022-08-10T08:42:00Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![ajus](https://avatars.discourse-cdn.com/v4/letter/a/dc4da7/32.png) [@ajus](https://discourse.psychopy.org/u/ajus)\
**Post date:** [August 10, 2022, 8:42am UTC](https://discourse.psychopy.org/t/comprehensive-deterministic-counterbalancing-using-pavlovias-shelf/30351/1 "2022-08-10T08:42:00Z")

</div>

Hi all,

I coded a little procedure to have the ability to counterbalance deterministically using the [shelf](https://www.psychopy.org/online/shelf.html) feature of pavlovia. While it’s obviously lacking all the other features of the [VESPR study portal](https://moryscarter.com/vespr/portal/admin/), it does a similar thing when it comes to counterbalancing. The pros are that it does not require you to set up anything outside of pavlovia and you have full control.

**What it does**

Whenever a participant starts your experiment on pavlovia, the code looks up how many participants already completed each condition or are currently active in each condition (i.e., not timed-out or completed) and compares it to the number of participants you want to have in each condition. If all conditions are full, it terminates the experiment. If not, it randomly allocates the participant to one of the groups with the lowest number of participants. When the participant finishes the experiment, their participation is marked as completed.

**What you need to do to use it**

Add a record called “counterbalancing” to your shelf and set its scope to the experiment that should use this record. The record needs to be a dictionary with the following structure

```auto
{
  "info": {
    "counter": 0,
    "time_out": 2,
    "group": [1, 2, 3, 4],
    "n_wanted": [1, 1, 1, 1],
    "n_complete": [0,0,0,0],
    "n_start": [0,0,0,0],
    "active_starting_times": {
      "1": [],
      "2": [],
      "3": [],
      "4": []
    }
  }
}

```

The following values need to be adjusted to your needs:

- `time_out` is the number of minutes after which a participant is assumed to have abandoned the experiment.
- `group` contains the levels of condition factor. In the example, there are four conditions with values 1 through 4.
- `n_wanted` are the numbers of participants you want to have in each of the groups. The first entry refers to the first entry in `group` and so on.
- The keys of `active_starting_times` (here 1, 2, 3, 4) need to be the names of your groups. For example, if your groups are called “A”, “B”, and “C”, `active_starting_times` would need to be `{"A": [], "B": [], "C": []}`

Now, download the [demo](https://gitlab.pavlovia.org/ajus/counterbalancing_demo) and copy-paste the “CounterbalancingRoutine” somewhere in your experiment (DisplayGroup is only there for demonstration and can be ignored). You now can use the variable `my_group` to change the experiment for the respective participant (e.g., `if(my_group == 1){ConditionsFile = "ABC.csv"}`).

The code also gives each participant a unique id (`my_id`) that is used in the name of the data file for this participant.

Let me know if you need any help with the implementation,  
Adrian
