# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 2
#     language: python
#     name: python2
# ---

import itertools
import pandas as pd
import random
from copy import deepcopy

# +
## Three Steps: 
## 1) Generate all your counterbalancing and trials and put them into a dataframe 
## 2) Define experimental structure
## 3) Fit the trials into experimental structure (e.g. assigning targets to cue part, assigning stimuli 
#to single-stimulus parts etc. This is the most complicated part.

# +
names = ['James','Orion','Cowell','Merrit']


names = ['Cowell', 'Elias', 'Weston', 'Ellis', 'Finn', 'Oliver', 'Finley',
         'James','March', 'Meyer', 'Niko', 'Orion', 'Nikolai', 'Dominic']
# -

all_trials = [item for item in itertools.combinations(names,2)]
print all_trials

all_trial_shuffled = []
for item in all_trials: #shuffle name1, name2 order
    item2 = list(item)
    random.shuffle(item2)
    all_trial_shuffled.append(item2)
print all_trial_shuffled


random.shuffle(all_trial_shuffled) #shuffle trials

df = pd.DataFrame(all_trial_shuffled)
print df.shape

df

df.columns = ['name1','name2']

df.head()



# +
## Experiment Constants ##
stimulus_positions = [(-5,0),(5,0)] #this may change per stimulus, it can be excluded here and drawn from the trial list
stimulus_path = False
stimulus_ext = False
choices = ['1','2']
key_mappings = {'1':0,'2':1}

## Trial Structure: 1) part order 2) part type, 3) timing of part, 4) fixation, 5) when to start responsetimer
trial_parts = [(1,  'blank',              0.3,  'black',  False),
               (2,  'single-stim',        False,  False,  True),
               (3,  'response',           False,  False,  False)
                ]
# -

trial_part_keys = ['trial_part_order','trial_part_type','timing','fixation','start_response_timer']
trial_structure_data = []
for item in trial_parts:
    trial_structure_data.append(dict(zip(trial_part_keys,item)))

df['correct_response'] = False
df['stimulus_path'] = stimulus_path
df['stimulus_ext'] = stimulus_ext
df['stimulus_positions'] = [stimulus_positions] * len(df)
df['choices'] = [choices] * len(df)
df['key_mappings'] = [key_mappings] * len(df)



df_dict = df.T.to_dict().values()    

# +
# List of variables you want to keep from original stimulus generation file (will probably be most of them)
keep_vars = ['name1','name2', 'correct_response', 'choices','key_mappings']

mapping_dict = {1:'blank',2:'names',3:'response'}

trial_structure_data_list = []
all_trials = []
for i,trial in enumerate(df_dict):
    
    keep = {}
    for x in keep_vars:
        keep[x] = trial[x]   
    all_trials.append(keep)
    
    
    for j, part in enumerate(trial_structure_data):
        
        #Assign General Variables
        part['stimulus_positions'] = trial['stimulus_positions']
        part['stimulus_path'] = trial['stimulus_path']
        part['stimulus_ext'] = trial['stimulus_ext']
        
                
        #Assign stimuli
        if mapping_dict[j+1] == 'names':
            part['stimulus'] = list([trial['name1'],trial['name2']])
        
        #Assign blanks
        if mapping_dict[j+1] in ['blank', 'response_separate']:
            part['stimulus'] = False
        
                    
    trial_structure_data_list.append(deepcopy(trial_structure_data))
    
    
    

# -

for item in trial_structure_data_list:
    print item[1]['stimulus']

for i,item in enumerate(zip(all_trials, trial_structure_data_list)):
    all_trials[i]['trial_structure_data'] = item[1]
    all_trials[i]['trial_num'] = i+1
    

all_trials[:5]

# +
import pickle

with open('all_trials.pkl', 'wb') as handle:
    pickle.dump(all_trials, handle, protocol=pickle.HIGHEST_PROTOCOL)



# -





StarcoderdataJupyterScriptsDedupFiltered