Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code to select random conditions is incorrect #3256

Closed
carolfs opened this issue Oct 28, 2020 · 2 comments · Fixed by #3258
Closed

Code to select random conditions is incorrect #3256

carolfs opened this issue Oct 28, 2020 · 2 comments · Fixed by #3258

Comments

@carolfs
Copy link
Contributor

carolfs commented Oct 28, 2020

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.

@peircej
Copy link
Member

peircej commented Oct 28, 2020

I agree on all the above points, which were clearly expressed 👏 I think those docs have been written in a slight rush (on a relatively rarely-used feature so haven't been noticed) and aren't quite right.

Are you comfortable submitting a pull request with the changes? (The docs source is in the repo as well)

@carolfs
Copy link
Contributor Author

carolfs commented Oct 30, 2020

I will try to submit one later today.

carolfs added a commit to carolfs/psychopy that referenced this issue Oct 30, 2020
carolfs added a commit to carolfs/psychopy that referenced this issue Oct 30, 2020
@mdcutone mdcutone linked a pull request Nov 5, 2020 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants