Advertisement
Guest User

Untitled

a guest
Aug 19th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.47 KB | None | 0 0
  1. #Extract the stimulus and response onsets from data file info
  2. def findOnsets(target, allLines):
  3.     #find stimulus and response onsets
  4.     stimulus_onsets = {} #for stimulus onsets
  5.     stimulus_count = 0 #track stimulus number
  6.     zero_count = 0 #track number of time stamps back to zero
  7.     responses = [1] #for response onsets #SW_19-08-18: Put 1 instead of 0 to prevent breaking in line 113
  8.     response_count = 0 #track response number
  9.     #loop through data file lines
  10.     for i in range(0,len(allLines)):
  11.         #stop if you have reached the end of the data file lines
  12.         if i == len(allLines)-1:
  13.             break
  14.         #if not at the end
  15.         else:
  16.             print(target.sub_id, target.go_stim, allLines[i], stimulus_count, response_count)
  17.             #get info from current line
  18.             cur_time = int(allLines[i][0]) #current time stamp
  19.             cur_fsr_value = int(allLines[i][1]) #current response value
  20.             next_fsr_value = int(allLines[i+1][1]) #next response value
  21.             cur_light_value = int(allLines[i][2]) #current light sensor value
  22.             next_light_value = int(allLines[i+1][2]) #next light sensor value
  23.             ##If it is the beginning or the time stamp starting again, start new list
  24.             if i == 0 or (cur_time < int(allLines[i-1][0]) and i > 300):
  25.                 zero_count = zero_count+1 #increase zero count
  26.                 stimulus_onsets[zero_count] = [] #new stimulus list
  27.                 if zero_count > 1: #if it is the 2nd or more zero count
  28.                     #get the time stamp before it went back to zero
  29.                     pre_zero_time = int(allLines[i-1][0])
  30.             ### Determine stimulus onsets ###
  31.             ##If the next light value is an increase of 10 or more,
  32.               #and we are at the very beginning within the first zero count
  33.             if (next_light_value - cur_light_value > 10 and zero_count == 1):
  34.                 if len(stimulus_onsets[zero_count]) == 0:
  35.                     stimulus_onsets[zero_count].append((i,cur_time))
  36.                     stimulus_count = stimulus_count+1
  37.                     responses.append((0,0))
  38.                 #or have recorded some stimuli onsets
  39.                 elif (len(stimulus_onsets[zero_count]) > 0 and \
  40.                          cur_time > 400 + \
  41.                          stimulus_onsets[zero_count][len(stimulus_onsets[zero_count])-1][1]):
  42.                     stimulus_onsets[zero_count].append((i,cur_time))
  43.                     stimulus_count = stimulus_count+1
  44.                     responses.append((0,0))
  45.             ##If the next light value is an increase of 10 or more,
  46.               #and we are within the 2nd or more zero count,
  47.             elif (next_light_value - cur_light_value > 10 and zero_count > 1):
  48.                 #but we have not counted a new stimulus yet since the time went back to zero
  49.                 if (len(stimulus_onsets[zero_count])==0 and \
  50.                     cur_time+pre_zero_time > 400 + \
  51.                     stimulus_onsets[zero_count-1][len(stimulus_onsets[zero_count-1])-1][1]):
  52.                     stimulus_onsets[zero_count].append((i,cur_time))
  53.                     stimulus_count = stimulus_count+1
  54.                     responses.append((0,0))
  55.                 #and we have already counted a new stimulus since the time went back to zero
  56.                 elif (len(stimulus_onsets[zero_count]) > 0 and \
  57.                       cur_time > 400 + \
  58.                       stimulus_onsets[zero_count][len(stimulus_onsets[zero_count])-1][1]):
  59.                     stimulus_onsets[zero_count].append((i,cur_time))
  60.                     stimulus_count = stimulus_count+1
  61.                     responses.append((0,0))
  62.             ### Determine response onsets ###
  63.             ##See if the current force sensor value is zero,
  64.              #and the next one is not zero
  65.             if (cur_fsr_value == 0) and (next_fsr_value > 10):
  66.                 responses[len(responses)-1] = (i,cur_time) ## SW_19-08-18: broke here, list is empty and len-1 gives 0-1 as list position I think
  67.                 #responses2[stimulus_count-1] = (stimulus_count,i,cur_time)
  68.                 response_count=response_count+1
  69.     return stimulus_onsets, responses
  70.  
  71.  
  72. #Write the stimulus and response info to a csv file
  73. def writeInfo(target, stimulus_onsets, responses):
  74.     #create csv file and headers
  75.     if not(os.path.exists('StimResponseOnsets_Thesis.csv')):
  76.         datafile = open('StimResponseOnsets_Thesis.csv','wb') #'wb' to remove blank lines
  77.         writer=csv.writer(datafile)
  78.         writer.writerow(('SubType','SubID','BlockCond','TimeCond','GoStim',\
  79.                          'StimLine','StimOnset','ResponseLine','ResponseOnset'))
  80.     else:
  81.         datafile = open('StimResponseOnsets_Thesis.csv','ab')
  82.         writer=csv.writer(datafile)
  83.     #loop through stimuli and responses
  84.     count=0
  85.     for zero_count in stimulus_onsets:
  86.         for stimulus_onset in stimulus_onsets[zero_count]:
  87.             count=count+1
  88.             writer.writerow((target.sub_type,\
  89.                              target.sub_id,\
  90.                              target.block_cond,\
  91.                              target.time_cond,\
  92.                              target.go_stim,\
  93.                              str(stimulus_onset[0]),\
  94.                              str(stimulus_onset[1]),\
  95.                              str(responses[count-1][0]),\
  96.                              str(responses[count-1][1]),\
  97.                              ))
  98.     datafile.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement