Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Extract the stimulus and response onsets from data file info
- def findOnsets(target, allLines):
- #find stimulus and response onsets
- stimulus_onsets = {} #for stimulus onsets
- stimulus_count = 0 #track stimulus number
- zero_count = 0 #track number of time stamps back to zero
- responses = [1] #for response onsets #SW_19-08-18: Put 1 instead of 0 to prevent breaking in line 113
- response_count = 0 #track response number
- #loop through data file lines
- for i in range(0,len(allLines)):
- #stop if you have reached the end of the data file lines
- if i == len(allLines)-1:
- break
- #if not at the end
- else:
- print(target.sub_id, target.go_stim, allLines[i], stimulus_count, response_count)
- #get info from current line
- cur_time = int(allLines[i][0]) #current time stamp
- cur_fsr_value = int(allLines[i][1]) #current response value
- next_fsr_value = int(allLines[i+1][1]) #next response value
- cur_light_value = int(allLines[i][2]) #current light sensor value
- next_light_value = int(allLines[i+1][2]) #next light sensor value
- ##If it is the beginning or the time stamp starting again, start new list
- if i == 0 or (cur_time < int(allLines[i-1][0]) and i > 300):
- zero_count = zero_count+1 #increase zero count
- stimulus_onsets[zero_count] = [] #new stimulus list
- if zero_count > 1: #if it is the 2nd or more zero count
- #get the time stamp before it went back to zero
- pre_zero_time = int(allLines[i-1][0])
- ### Determine stimulus onsets ###
- ##If the next light value is an increase of 10 or more,
- #and we are at the very beginning within the first zero count
- if (next_light_value - cur_light_value > 10 and zero_count == 1):
- if len(stimulus_onsets[zero_count]) == 0:
- stimulus_onsets[zero_count].append((i,cur_time))
- stimulus_count = stimulus_count+1
- responses.append((0,0))
- #or have recorded some stimuli onsets
- elif (len(stimulus_onsets[zero_count]) > 0 and \
- cur_time > 400 + \
- stimulus_onsets[zero_count][len(stimulus_onsets[zero_count])-1][1]):
- stimulus_onsets[zero_count].append((i,cur_time))
- stimulus_count = stimulus_count+1
- responses.append((0,0))
- ##If the next light value is an increase of 10 or more,
- #and we are within the 2nd or more zero count,
- elif (next_light_value - cur_light_value > 10 and zero_count > 1):
- #but we have not counted a new stimulus yet since the time went back to zero
- if (len(stimulus_onsets[zero_count])==0 and \
- cur_time+pre_zero_time > 400 + \
- stimulus_onsets[zero_count-1][len(stimulus_onsets[zero_count-1])-1][1]):
- stimulus_onsets[zero_count].append((i,cur_time))
- stimulus_count = stimulus_count+1
- responses.append((0,0))
- #and we have already counted a new stimulus since the time went back to zero
- elif (len(stimulus_onsets[zero_count]) > 0 and \
- cur_time > 400 + \
- stimulus_onsets[zero_count][len(stimulus_onsets[zero_count])-1][1]):
- stimulus_onsets[zero_count].append((i,cur_time))
- stimulus_count = stimulus_count+1
- responses.append((0,0))
- ### Determine response onsets ###
- ##See if the current force sensor value is zero,
- #and the next one is not zero
- if (cur_fsr_value == 0) and (next_fsr_value > 10):
- 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
- #responses2[stimulus_count-1] = (stimulus_count,i,cur_time)
- response_count=response_count+1
- return stimulus_onsets, responses
- #Write the stimulus and response info to a csv file
- def writeInfo(target, stimulus_onsets, responses):
- #create csv file and headers
- if not(os.path.exists('StimResponseOnsets_Thesis.csv')):
- datafile = open('StimResponseOnsets_Thesis.csv','wb') #'wb' to remove blank lines
- writer=csv.writer(datafile)
- writer.writerow(('SubType','SubID','BlockCond','TimeCond','GoStim',\
- 'StimLine','StimOnset','ResponseLine','ResponseOnset'))
- else:
- datafile = open('StimResponseOnsets_Thesis.csv','ab')
- writer=csv.writer(datafile)
- #loop through stimuli and responses
- count=0
- for zero_count in stimulus_onsets:
- for stimulus_onset in stimulus_onsets[zero_count]:
- count=count+1
- writer.writerow((target.sub_type,\
- target.sub_id,\
- target.block_cond,\
- target.time_cond,\
- target.go_stim,\
- str(stimulus_onset[0]),\
- str(stimulus_onset[1]),\
- str(responses[count-1][0]),\
- str(responses[count-1][1]),\
- ))
- datafile.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement