Can you use a (pandas) DataFrame object as a loop source?
I have a stimuli list file loaded in the pool, which contains pairs of sentences. I only ever want to show 1 of the 2 sentences in a pair to every participant, and which of the two is shown should be determined randomly for every participant.
I know that this doesn't work with selecting the file itself as a source for the loop, as the 'maxrep' constraint only works for repetition of a specific row, not of two rows that have the same value in one of their columns.
Therefore, I've tried creating an inline script near the start of the experiment, where I use the pandas function .sample() to select one of the two sentences from each pair, and write these to a new DataFrame object, but now I cannot figure out how to use this DataFrame object as a source for the experimental loop of my experiment (using [DataFrameName] as loop source doesn't work).
Now I could have the inline script write a new CSV file for every participant, load that CSV file into the pool, and then use that as the source, but that would clutter the experimental PC with a lot of CSV files, and just feels like an inelegant solution in general.
Is there a way to make this work, or otherwise an alternative solution (with just OpenSesame functions) to the sampling method required that I've overlooked?
Comments
Hi @KarsLigtenberg ,
The data structure used by
loop
items is a DataMatrix, which is an alternative to pandas DataFrame and a bit more light-weight and intuitive:If you want to use a pandas DataFrame to populate the
loop
table, you first need to convert it to a DataMatrix, and then assign it to thedm
property of theloop
item. Here's the general idea:Hope this helps!
— Sebastiaan
Check out SigmundAI.eu for our OpenSesame AI assistant!
Hi @sebastiaan ,
Thank you very much!
I had only glossed that section of the looping manual and hadn't thought to just convert the pandas DataFrame to a DataMatrix, your suggestion did the trick wonderfully.