Advertisement
Guest User

randomization with constrains in python

a guest
Nov 24th, 2015
660
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.64 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Tue Nov 17 14:38:26 2015
  4.  
  5. @author: erik
  6. """
  7. from random import shuffle
  8. from collections import Counter
  9.  
  10. liststim = []
  11. colors, firstfigures = ['Black', 'Blue'], ['Rect', 'Triangle', 'X', 'Circle']
  12. secondfigures = firstfigures
  13. for col in colors:
  14.     for figure in firstfigures:
  15.         for figure2 in secondfigures:
  16.             liststim.append(col + '-' + figure + '_' + figure2)
  17.  
  18. def randShift(listostim, ntrials):
  19.     '''
  20.    Will randomize 50 % of shifting trials
  21.    based on that each filename (of the images) starts with Black or Blue-.
  22.    
  23.    listostim is a list of trials
  24.    ntrials is an integer indicating number of trials
  25.    '''
  26.     nEachstim = ntrials/len(liststim) #Number of each stim is going to be even
  27.     stimList = listostim
  28.     trialTypes = []
  29.     stims = []
  30.     countOfStim = dict((el,0) for el in stimList)
  31.     count = {'ns':0,'s':0}
  32.     for i in range(ntrials):
  33.         shuffle(stimList)
  34.         for idx in range(len(stimList)):
  35.             if not stims:
  36.                 countOfStim[stimList[idx]] +=1
  37.                 stims.append(stimList[idx])
  38.                 count['ns'] +=1
  39.                 trialTypes.append("No-Shifting")
  40.             elif stims:
  41.                 if count['s'] <= ntrials/2:
  42.                     if countOfStim[stimList[idx]] <= nEachstim-1:
  43.                         if stimList[idx][:5] != stims[i-1][:5]:
  44.                             count['s'] +=1
  45.                             countOfStim[stimList[idx]] +=1
  46.                             stims.append(stimList[idx])
  47.                             trialTypes.append("Shifting")
  48.        
  49.                         else:
  50.                             count['ns'] +=1
  51.                             countOfStim[stimList[idx]] +=1
  52.                             stims.append(stimList[idx])
  53.                             trialTypes.append("No-Shifting")
  54.                 elif count['s'] > ntrials/2:
  55.                     if countOfStim[stimList[idx]] <= nEachstim-1:
  56.                         if stimList[idx][:5] == stims[i-1][:5]:
  57.                             count['ns'] +=1
  58.                             countOfStim[stimList[idx]] +=1
  59.                             stims.append(stimList[idx])
  60.                             trialTypes.append("No-Shifting")
  61.     #Frequency of the trialtypes
  62.     freq = Counter(trialTypes).values()
  63.     if freq[0] == freq[1] and sum(freq) == ntrials:
  64.         return stims
  65.     #We need to run again if the constrains we need is not satisfied
  66.     elif freq[0] != ntrials/2 and freq[1] !=ntrials/2 or sum(freq) !=ntrials:
  67.         return randShift(stimList, ntrials)
  68.  
  69. randomized = randShift(liststim, 96)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement