Skip to content

Code to select random conditions is incorrect #3256

@carolfs

Description

@carolfs

From PsychoPy's documentation:

The parameter Select rows allows this. You can specify which rows you want to use by inserting values here:

  • 0,2,5 gives the 1st, 3rd and 5th entry of a list - Python starts with index zero)
  • random(4)*10 gives 4 indices from 0 to 10 (so selects 4 out of 11 conditions)

The first item is incorrect: 0,2,5 gives the 1st, 3rd, and 6th entries.

The second item is just plain wrong as far as I understand. numpy's random function generates random numbers in the interval [0, 1), thus random(4)*10 (which by the way should be $random(4)*10) generates 4 floating-point numbers in that interval. Multiplying by 10 thus generates 4 floating-point numbers in the [0, 10) interval. So these numbers are floats, not integers. But as far as I understand PsychoPy converts them to integers like this:

    # the selection might now be a slice or a series of indices
    if isinstance(selection, slice):
        trialList = trialList[selection]
    elif len(selection) > 0:
        allConds = trialList
        trialList = []
        for ii in selection:
            trialList.append(allConds[int(round(ii))])

Thus the numbers are rounded before being converted to integers. This means that numbers from 0 to 0.5 are converted to 0, numbers from 0.5 to 1.5 are converted to 1 and so on. Thus, there's a lower probability that 0 or 10 will be selected.
Here is a plot that illustrates the problem.

The probability for every number should be 1/11, but it's not.

In my opinion, int(round(ii)) should be changed to int(np.floor(ii)) and the documentation to "random(4)*10 gives 4 indices from 0 to 9 (so selects 4 out of 10 conditions)." This will make sure every number has the same probability of being selected.

The correct distribution for numbers from 0 to 9.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions