Randomization problem: No error message, yet still repeating rows no matter what i do

OS: Windows 11
PsychoPy version v2023.2.3
Standard Standalone? (y/n) Y

What are you trying to achieve?:
My experiment ran smoothly, and I was happy until I noticed repeats. It’s a little complicated, but it involves making many decisions, and then it randomly breaks into an ‘execution trial’, meaning it picks one of the rows (and thereby a text) to display. The reason it is complicated is that it involves assigning an Excel sheet based on ExpInfo and then selecting rows based on their choices.

if R_Level < 8:
groupFile = ‘below.xlsx’
elif R_Level > 9:
groupFile = ‘above.xlsx’
else:
groupFile = ‘same.xlsx’

if Effort.contains(mouse) or Reward1_2.contains(mouse):
Comb_list = [1, 1]
Reject += 1
elif image_7.contains(mouse) or image_8.contains(mouse):
Comb_list = [RewardLevel, EffortLevel]
Accept += 1

if Comb_list:
all_selections = [‘0:9’, ‘10:19’, ‘20:29’, ‘30:39’, ‘40:49’, ‘50:58’]
rl = ‘t’ + str(Comb_list[0]) + ‘.jpeg’
el = ‘b’ + str(Comb_list[1]) + ‘.jpeg’
selected_rows_range = all_selections[Comb_list[1] - 1]

I have figured out how to do this with the help of this community. However, no matter what i do, repeats are still happening.

it involves 3 loops, 1 outer loop and 2 inner loops.

What did you try to make it work?:

  • Searched and tried multiple ways of fixing through this forum, even took suggestions from AI :face_with_peeking_eye:
  • made the outer loop sequential (it was original random too).
  • Tried creating a new column in all the excel sheets, which assigns a unique number to each row/text (called TextNum). And then adding in code, which stores used_TextNums and then if repeats happen (by comparing current TextNum to used_textNum), it replaces it with a new row/TextNum (selected_textNum)
  • Tried purely doing it via selected rows, and saying if this row repeats, then find a new row.

What specifically went wrong when you tried that?:
There were no errors messages. It did it’s job, but repeats happened basically and then excel sheet indicated it wasn’t working. :frowning:

I have now gone back to basic and my old code. I was wondering if anyone has unique solutions or some ways of fixing this or any suggestions (no matter how simple)? Perhaps something else is causing this issue? Please let me know if you want any more information. I didn’t want to make this too long. :slight_smile:

Hello @Hans

Did you copy and paste the code? This has “smart” quotes. Make them dumb ones, like ’ or ".

Do you plan to run the experiment online?

This does not translate to PsychoJS. What do try to achieve here?

You could add some print(variable) commands to check the value of your variables and see if they take the values you expect. And yes, it is probably best to turn off all randomisation for debugging purposes.

Best wishes Jens

1 Like

Hiya. I don’t plan to run it online and I am not using PsychoJS. In terms of the all_selections quotes, I realised that, and changed them.

all_selections = [‘0:9’, ‘10:19’, ‘20:29’, ‘30:39’, ‘40:49’, ‘50:58’]

In terms of the debugging, the funny thing is that is it working in all ways apart from repeats. How far should I strip back to see where the problem is happening? For example, have all loops be sequential?

This list is missing several of your rows. Try

all_selections = ['0:10', '10:20', '20:30', '30:40', '40:50', '50:59']

assuming you have five sets of 10 rows and one set of 9.

1 Like

Okay. Thanks. I had this original, but thought this might be the reason for the repeats (obviously wasn’t). I attempted your complicated randomization thing on the blog page, but it really didn’t like me (i think it probably doesn’t work in my case due to all the constrictions). Any hep would be appreciated. Been working on this for weeks with no progress, just 1 step forward and then 2 back.

Please could you explain what you mean by repeats in terms of your loops and routines?

1 Like

Okay, so in my groupFile, I have rows of ‘texts’ (corresponding to a path to a png file containing a short story). I don’t want them to repeat. So, for example, I just ran my experiment. And I have a column that tracks TextNum (allowing me to check for any repeats), resulting in 9,4, 24, 5, 23, 1, 9, 1,1, 10, 6. As you can see, 1 repeated 3 and 9 twice. Meaning that text 1 repeats 3 times and text 9 repeats twice. I don’t want this to happen. It has no reason to happen (since I have roughly 10 texts), yet it keeps happening. Hopefully, this makes sense.

@wakecarter Here is an example of my excel sheet

If you have “roughly” 10 texts then how were texts 23 and 24 presented?

I suspect that the issue is that you keep restarting the loop using selected_rows_range. If you restart a loop it has no knowledge of what was previously presented.

1 Like

Okay, so, all_selections = [‘0:10’, ‘10:20’, ‘20:30’, ‘30:40’, ‘40:50’, ‘50:59’]. Each of these row ranges includes 10 texts. So, in total, there are 59 texts.

One Loop includes the code in Game_1 End Routine, while the texts are executed in Two Loop (via the loop, selected_rows = selected_row_range). Then Loop 3, makes it go back to One Loop. Do you think loop two is re-starting? If so, why?

Perhaps this idea has some merit to it. Since my Two Loop index is all messed up unlike Three Loop and One Loop. For example, Two.thisIndex is 8, 3, 3, 4, 2, 0, 8, 0, 0, 9, 5 (note that this does not correlate with TextNum, meaning that the thisIndex repeating does not mean the text themselves repeat). Hopefully, this makes sense.

Okay, hopefully, I understand you correctly; I have stripped it back and defined all_selections to begin the experiment tab in the Game_1 Routine. Like so,

all_selections = [‘0:10’, ‘10:20’, ‘20:30’, ‘30:40’, ‘40:50’, ‘50:59’]

My other code (seen in the original post) is in the Game_1 Start and End routine.

if Effort.contains(mouse) or Reward1_2.contains(mouse):
Comb_list = [1, 1]
Reject += 1
elif image_7.contains(mouse) or image_8.contains(mouse):
Comb_list = [RewardLevel, EffortLevel]
Accept += 1

Then, since the problem still occurred, I created a new routine between/outside of One Loop and Two Loop (called Break 2, in the End routine) and put my code for picking the selected_row in there. Two Loop then reads in the selected_row.

# Function to select a row based on Comb_list
if Comb_list:
    rl = 't' + str(Comb_list[0]) + '.jpeg'
    el = 'b' + str(Comb_list[1]) + '.jpeg'
    selected_row = all_selections[Comb_list[1] - 1]

Sadly, no dice. At first, I thought it was working. It was much better but then repeats started to happen. See trail output; repeats are highlighted in bold.

(2,1, 17, 8, 6, 23, 12, 11, 10, 17, 5, 7, 19, 5, 15, 9, 26, 5, 13, 6, 29, 20)

As you can also see, the repeats are not due to all the rows being used up, as they are still not used(for example, 0:10, 0, 3 and 4 were never used. For 10:20, 14 and 16 were used). If it were using all rows, no repeats would happen.


Btw, One and Two are ticked for is. Trials, but Three Lope is not.

Please help me understand what the heck is going on, @wakecarter. Did I understand you correctly? OR is the the Loop restarting in another way? Is it due to using One.finished = TRUE (Ending the Loop early?). Thanks for any help this forum can give. :slight_smile:

Please could you upload your psyexp file so I can work out what you are doing?

1 Like

Hi, yes, sorry. Here it is. It is a bit of a mess I am aware. Thanks so much for trying to help. :slight_smile:

Temporal-Conditioning-main-effort.psyexp (495.0 KB)

Three is a loop with no spreadsheet and 25 repeats

One points to decisionsTrials.xlsx which has 76 rows (selected rows is redundant)

However, it ends after a few trials (between 2 and 8 – and possibly on trial 5. I use .thisN not .thisTrialN but I think both work).

if One.thisTrialN == random_trial_num:
    One.finished = True
    break # break is redundant
elif attempts >= 2 and One.thisTrialN == 4:
    One.finished = True 
    break # break is redundant
else: 
    One.finished = False # This is redundant

Try adding the following in Break 2

print('Comb_list',Comb_list)
print('selected_rows',selected_rows)

I think that the issue might be that if Comb_list is false Two will run with the same rows as the previous loop. It might work to add Comb_list as the number of repeats for Two.

1 Like

Hi. Yes, currently:

Three Lope has no spreadsheet; I just want it to keep everything going until it reaches 75 choice trials

For One Loop, there are 76 trials to choose from, but it finishes on random trials.

The selected rows are for Two Loop essentials it picks the row based on. It is only supposed to run once and then go back to Loop One.

I will fix and add the code you mentioned. Sadly, I don’t know what you mean when you say.

Why would it work if Comb_list is the number of repeats? I only want it once and Comb_list is for selecting the correct row group. Thanks for your help

This is the current output, I stop it after a while (since the repeats were happening), but as you can see. I honestly have no clue what to do :frowning:

I tried adding the print statements, but I am unsure how they work.

Thanks for any help btw.

MEP-DAY-000_main-effort-exp-v1_1.csv (100.0 KB)

You have code if Comb_list:. Why?

1 Like

Honestly, because I was trying to piece everything together, and that seemed reasonable. I am guessing it is redundant since the Comb_list is never empty. Sorry, I am self-taught with python.

Also, thank you for your link to the blog. Very useful!!

@wakecarter I have added in the print statements and removed the if Comb_list:

It printed both correctly in the stdout tab, and tbh; the repeats were reduced (only one this time, but still an issue). So unsure what is causing it!

It is printed like this.

selected_rows 0:10 #typo fixed

Secondly, I want my trail to finish on 75 trails (across the whole exp, hence why count_trails is used to count how many choice trials have been conducted - see loop one). I wonder if there is a better way to code this. The code is below, and my Three Loop includes nreps = 25 (sequential)

#resetting comb_list and random_trial_num
Comb_list = []
random_trial_num = random.randint(2, 9)

if Two.thisTrialN == 0 or attempts == 2:
    Two.finished = True
    break  # Exit loop if Two.thisTrialN is 0
else:
    Two.finished = False

if count_trials >= 75:
    Three.finished = True
    break  # Exit loop if count_trials is 75 or more this is not working! 
else:
    Three.finished = False

Any help is appreciated.

MEP-DAY-000_main-effort-exp-v1_4.csv (114.9 KB)