def getIBIandPuilDilation(self, pulse_ind, samples_pulse, pupil_ind,samples_pupil): ''' This method computes statistics on the IBI and pupil diameter per trial, as well as aggregating data from all the trials indicated by the row_ind input to compute histograms of the data. Additionally it computes the heart rate variability (HRV, standard deviation of the IBIs) per trial. Inputs: - pulse_ind: N x 1 array containing indices of the pulse signal that correspond to particular events, e.g. center hold, where there are N events of interest - samples_pulse: N x 1 array containing the number of samples to compute the data from the pulse signal for the event of interest - pupil_ind: N x 1 array containing indices of the pupil signal that correspond to particular events, e.g. center hold, where there are N events of interest - samples_pupil: N x 1 array containing the number of samples to compute the data from the pupil signal for the event of interest. Note: the pulse data is often computed over the duration of the trial but pupil data in some cases may be restricted to short epochs within the trial. Outputs: - ibi_mean: list of length N containing the average IBI for each event - hrv: list of length N containing the standard deviation of the IBIs (HRV) for each event - all_ibi: list of variable length containing all IBI values measured across all events - pupil_mean: list of length N containing the average pupil diameter for each event - all_pupil: list of variable length containing all pupil diameter values (measured while eyes are detected to be open) across all events ''' ibi_mean = [] hrv = [] pupil_mean = [] all_ibi = [] all_pupil = [] for i in range(0,len(pulse_ind)): pulse_snippet = self.pulse_data[pulse_ind[i]:pulse_ind[i]+samples_pulse[i]] ibi_snippet = findIBIs(pulse_snippet,self.pulse_samprate) all_ibi += ibi_snippet.tolist() if np.isnan(np.nanmean(ibi_snippet)): ibi_mean.append(ibi_mean[-1]) # repeat last measurement hrv.append(hrv[-1]) else: ibi_mean.append(np.nanmean(ibi_snippet)) hrv.append(np.nanstd(ibi_snippet)) ibi_std.append(np.nanstd(ibi_snippet)) pupil_snippet = self.pupil_data[pupil_ind[i]:pupil_ind[i]+samples_pupil[i]] pupil_snippet_range = range(0,len(pupil_snippet)) eyes_closed = np.nonzero(np.less(pupil_snippet,-3.3)) eyes_closed = np.ravel(eyes_closed) if len(eyes_closed) > 1: find_blinks = eyes_closed[1:] - eyes_closed[:-1] blink_inds = np.ravel(np.nonzero(np.not_equal(find_blinks,1))) eyes_closed_ind = [eyes_closed[0]] eyes_closed_ind += eyes_closed[blink_inds].tolist() eyes_closed_ind += eyes_closed[blink_inds+1].tolist() eyes_closed_ind += [eyes_closed[-1]] eyes_closed_ind.sort() for i in np.arange(1,len(eyes_closed_ind),2): rm_range = range(np.nanmax(eyes_closed_ind[i-1]-20,0),np.minimum(eyes_closed_ind[i] + 20,len(pupil_snippet)-1)) rm_indices = [pupil_snippet_range.index(rm_range[ind]) for ind in range(0,len(rm_range)) if (rm_range[ind] in pupil_snippet_range)] pupil_snippet_range = np.delete(pupil_snippet_range,rm_indices) pupil_snippet_range = pupil_snippet_range.tolist() pupil_snippet = pupil_snippet[pupil_snippet_range] pupil_snippet_mean = np.nanmean(pupil_snippet) pupil_snippet_std = np.nanstd(pupil_snippet) window = np.floor(pupil_samprate/10) # sample window equal to ~100 ms #pupil_snippet = (pupil_snippet[0:window]- pupil_snippet_mean)/float(pupil_snippet_std) pupil_snippet = pupil_snippet[0:window] all_pupil += pupil_snippet.tolist() if np.isnan(np.nanmean(pupil_snippet)): pupil_mean.append(pupil_mean[-1]) else: pupil_mean.append(np.nanmean(pupil_snippet)) pupil_std.append(np.nanstd(pupil_snippet)) return ibi_mean, hrv, all_ibi, pupil_mean, all_pupil
# Find IBIs and pupil data for all succesful stress trials. samples_pulse_successful_stress = np.floor(response_time_successful_stress*pulse_samprate) #number of samples in trial interval for pulse signal samples_pupil_successful_stress = np.floor(response_time_successful_stress*pupil_samprate) samples_hdeeg_successful_stress = np.floor(response_time_successful_stress*hdeeg_samprate) #ibi_stress = dict() #pupil_stress = dict() ibi_stress_mean = [] ibi_stress_std = [] pupil_stress_mean = [] pupil_stress_std = [] all_ibi_stress = [] all_pupil_stress = [] for i in range(0,len(row_ind_successful_stress)): pulse_snippet = pulse_data[pulse_ind_successful_stress[i]:pulse_ind_successful_stress[i]+samples_pulse_successful_stress[i]] ibi_snippet = findIBIs(pulse_snippet,pulse_samprate) all_ibi_stress += ibi_snippet.tolist() ibi_stress_mean.append(np.mean(ibi_snippet)) ibi_stress_std.append(np.std(ibi_snippet)) #ibi_stress['i'] = ibi_snippet pupil_snippet = pupil_data[pupil_ind_successful_stress[i]:pupil_ind_successful_stress[i]+samples_pupil_successful_stress[i]] #eyes_open = np.nonzero(np.greater(pupil_snippet,-0.5)) #eyes_open = np.ravel(eyes_open) pupil_snippet_range = range(0,len(pupil_snippet)) eyes_closed = np.nonzero(np.less(pupil_snippet,-3.3)) eyes_closed = np.ravel(eyes_closed) if len(eyes_closed) > 1: find_blinks = eyes_closed[1:] - eyes_closed[:-1] blink_inds = np.ravel(np.nonzero(np.not_equal(find_blinks,1))) eyes_closed_ind = [eyes_closed[0]] eyes_closed_ind += eyes_closed[blink_inds].tolist()