def get_psth_peaks_simple(t, psth, nid, widthmaxpoints, minthresh=3, medianx=2, fwfraction=0.5): """Extract peaks from PSTH, simpler, faster, more robust method. Find contiguous ranges of baseline-exceeding points. Within each range, find the biggest value. If that value exceeds thresh, designate that as a peak. Slice out that baseline-exceeding range of data, and run argfwhm on it, with outer kwarg - ie search from the outer edges in when looking for FWHM. If baseline is so high that it exceeds FWHM for that peak, discard the peak. t is passed only so that it can be conveniently returned for plotting""" baseline = medianx * np.median(psth) thresh = baseline + minthresh # peak detection threshold # indices of all baseline-exceeding points in psth: baselineis, = np.where( psth >= (baseline + EPS)) # add EPS because baseline is often 0 # find only those local peaks above baseline that are separated from each # other by at least one point below baseline: # indices into baselineis of breaks in baselineis, marking borders of contiguous ranges: splitis = np.where(np.diff(baselineis) > 1)[0] + 1 if len(splitis) == 0: splitbaselineis = [ ] # this will cause nothing to happen in the for loop below else: # list of arrays of indices, representing contiguous ranges of baseline-exceeding psth: splitbaselineis = np.array_split(baselineis, splitis) peakis, lis, ris = [], [], [] print("n%d" % nid, end='') for baselineis in splitbaselineis: # for each contiguous baseline-exceeding range of points localpsth = psth[ baselineis] # slice that range of points out of the psth peakii = localpsth.argmax() if localpsth[ peakii] < thresh: # peak in this range of points doesn't exceed thresh continue # skip to next range try: # get left and right FWHM indices: lii, rii = argfwhm(localpsth, peakii, fraction=fwfraction, method='outer') except ValueError: # peaki has no FWHM continue # skip to next range if (rii - lii) > widthmaxpoints: # peak is too wide continue # skip to next range offset = baselineis[0] peakis.append(offset + peakii) lis.append(offset + lii) ris.append(offset + rii) print('.', end='') # printed dot indicates a found peak peakis = np.asarray(peakis) lis = np.asarray(lis) ris = np.asarray(ris) return t, psth, thresh, baseline, peakis, lis, ris
def get_psth_peaks_simple(t, psth, nid, widthmaxpoints, minthresh=3, medianx=2, fwfraction=0.5): """Extract peaks from PSTH, simpler, faster, more robust method. Find contiguous ranges of baseline-exceeding points. Within each range, find the biggest value. If that value exceeds thresh, designate that as a peak. Slice out that baseline-exceeding range of data, and run argfwhm on it, with outer kwarg - ie search from the outer edges in when looking for FWHM. If baseline is so high that it exceeds FWHM for that peak, discard the peak. t is passed only so that it can be conveniently returned for plotting""" baseline = medianx * np.median(psth) thresh = baseline + minthresh # peak detection threshold # indices of all baseline-exceeding points in psth: baselineis, = np.where(psth >= (baseline+EPS)) # add EPS because baseline is often 0 # find only those local peaks above baseline that are separated from each # other by at least one point below baseline: # indices into baselineis of breaks in baselineis, marking borders of contiguous ranges: splitis = np.where(np.diff(baselineis) > 1)[0] + 1 if len(splitis) == 0: splitbaselineis = [] # this will cause nothing to happen in the for loop below else: # list of arrays of indices, representing contiguous ranges of baseline-exceeding psth: splitbaselineis = np.array_split(baselineis, splitis) peakis, lis, ris = [], [], [] print("n%d" % nid, end='') for baselineis in splitbaselineis: # for each contiguous baseline-exceeding range of points localpsth = psth[baselineis] # slice that range of points out of the psth peakii = localpsth.argmax() if localpsth[peakii] < thresh: # peak in this range of points doesn't exceed thresh continue # skip to next range try: # get left and right FWHM indices: lii, rii = argfwhm(localpsth, peakii, fraction=fwfraction, method='outer') except ValueError: # peaki has no FWHM continue # skip to next range if (rii - lii) > widthmaxpoints: # peak is too wide continue # skip to next range offset = baselineis[0] peakis.append(offset + peakii) lis.append(offset + lii) ris.append(offset + rii) print('.', end='') # printed dot indicates a found peak peakis = np.asarray(peakis) lis = np.asarray(lis) ris = np.asarray(ris) return t, psth, thresh, baseline, peakis, lis, ris
extii = abs(extis - aligni).argmin() # extremum closest to aligni exti1 = extis[extii] # index of extremum closest to aligni, assume main extremum # find index of extremum to the right of the one closest to aligni. If the one # closest to aligni is already the rightmost, make that one the secondary, and the one # to its left the primary: try: exti2 = extis[extii+1] except IndexError: exti2 = exti1 # make old primary the new secondary # set new primary to be the one to the left, hopefully without another IndexError: exti1 = extis[extii-1] nswaps += 1 # 0.75 seems to give max fwhm2 bimodality, but 0.5 gives best overall clusterability # in fwhm2 vs aai space li1, ri1 = argfwhm(wave, exti1, fraction=0.5) li2, ri2 = argfwhm(wave, exti2, fraction=0.5) fwhm1 = (ri1 - li1) * newtres fwhm2 = (ri2 - li2) * newtres #ti1, ti2 = wave.argmax(), wave.argmin() # previously used biggest peaks for ipi ipi = (exti2 - exti1) * newtres # interval between primary and secondary peaks duration2 = (ri2 - li1) * newtres # start of primary to end of secondary peak ext1medt, ext2medt = (li1 + ri1)/2, (li2 + ri2)/2 ext1width, ext2width = ri1 - li1, ri2 - li2 # new measure of peak temporal asymmetry: time between mode and median, normalized # by peak width: ai1 = (exti1 - ext1medt) / ext1width ai2 = (exti2 - ext2medt) / ext2width #ext1wave, ext2wave = wave[li1:ri1], wave[li2:ri2] #ext1mean, ext2mean = ext1wave.mean(), ext2wave.mean() #ext1med, ext2med = np.median(ext1wave), np.median(ext2wave)