Search code examples
psychopy

Take random data from CSV file which satisfy some parameter condition


I have a CSV file which looks like this:

cond, arg
T, arg1 
T, arg2
T, arg3
T, arg4
T, arg5
P, arg6
P, arg7
P, arg8
P, arg9
P, arg10

Right now I am able to get random value from “arg” column just putting the value “$arg” into “Text element”

My question is, just by using the “Builder view”, for example the “Text element” and the “Code element”, is possible to get randomly the values belonging to the rows which satisfy the condition $cond=‘P’?


Solution

  • I would suggest that the easiest way to do this in Builder would be to split your CSV file into two separate files (in this case, one containing "arg1"-"arg5" and the other containing "arg6"-"arg10"), named, say, T.csv and P.csv.

    Then in your loop, instead of having a hard-coded conditions filename, like "conditions.csv", put a variable name, like "$conditionFilename" (without the quotes). You then just need a way to specify the value of that variable before the loop gets created. e.g. Let's say you add a field called "conditionType" to the Experiment Settings dialog which the experimenter will set before each run, which will contain the values "P" or "T". Then in a code component, in the "Begin Experiment" tab, put something like this:

    if expInfo['conditionType'] == 'P':
        conditionFilename = 'P.csv'
    else:
        conditionFilename = 'T.csv'
    

    Set the loop to be random, and you will get random selection within those particular trials. I think the conditionType variable will be automatically stored in your data along with the other experiment settings.