def baseline_subtraction(x,y, fstgo): print('The channel with the minimum counts in each segment will be a point on a segmented linearly interpolated background.') min_indices, min_x_list, min_y_list, x_split_list, y_split_list = min_spectrum_split(x, y, fstgo) interp_function = sp.interpolate.interp1d(min_x_list, min_y_list, kind = 'linear', fill_value = 'extrapolate') plt.plot(x,y) plt.plot(x, interp_function(x)) plt.plot(x, y - interp_function(x)) plt.show() redo = ad.y_or_n('Would you like to reselect the boundaries?') if redo == 'y': return baseline_subtraction(x,y) return( y - interp_function(x))
directory = os.getcwd() + '/spectra' fileno = ad.listfiles(directory) fileselect = ad.inputfileselector(fileno) f = ad.openfilefromlist(fileselect, directory) x, yinit = ad.file_reader(directory + '/' + f) spe = ad.spectrum_plotter(x, yinit) spe.show() ''' ###########################################Baseline Subtraction################################################## ''' base_flag = ad.y_or_n( "Does this spectrum need baseline subtraction to fid the peaks.\nThe subtraction will not be applied to the fit. Instead, there will be a linear background applied to each fitting region." ) if base_flag == 'y': ybase = sig.baseline_subtraction(x, yinit) else: ybase = yinit ''' ###########################################Contaminant Deletion################################################## ''' cont_del = ad.y_or_n("Would you like to remove any contaminants?") if cont_del == 'y': yclipfit, yclipfind = sig.contaminant_clipper(x, yinit, ybase) else:
fileno = ad.listfiles(directory) fileselect = ad.inputfileselector(fileno) f = ad.openfilefromlist(fileselect, directory) x, yinit = ad.file_reader(directory + '/' + f) spe = ad.spectrum_plotter(x, yinit) spe.show() #yinit = sig.baseline_subtraction(x,yinit, fstgo) ''' ###########################################Contaminant Deletion################################################## ''' cont_del = ad.y_or_n("Would you like to remove any contaminants?") if cont_del == 'y': xlist, ylist, yclip = sig.contaminant_clipper(x, yinit, yinit, split=True) else: xlist, ylist = [[x], [yinit]] yclip = yinit for x2, y2 in zip(xlist, ylist): plt.plot(x2, y2) plt.show() ''' ##########################################Threshold set and Peak Region Detection########################################################### ''' rlist, betalist = [], []
def contaminant_clipper(x, yinit, ybase, split = False): ''' clips bits out of the spectrum after prompting the user to do so ''' yclipfit = yinit #clip things out of this yclip variable so yinit doesn't change yclipfind = ybase if split: xlist, ylist = [[],[]] borders = [0, len(x)] while True: #set a buffer such that it can be reinstated to the last one if the user changes their minddef contaminant_c_split(x, y) yclipfitbuffer = yclipfit yclipfindbuffer = yclipfind #plot what the current spectrum is clip_spectrum = ad.spectrum_plotter(x, yclipfind) clip_spectrum.show() #get the user to input a valid set of bounds in which to delete the contaminant while True: lb = input('input a lower bound on the contaminant in channels') ub = input('input an upper bound on the contaminant in channels') if np.chararray.isdigit(ub + lb): #has to all be digits lb = int(lb) ub = int(ub) if (lb > min(x) and ub > lb and ub < max(x)): #lower and upper bounds have to be in the spectrum, upper bound has to be bigger than lower break print('Invalid bounds, try again') #get which indices x is less than the lower bound and greater than the upper bound set x21 = np.where(x < lb)[0] x22 = np.where(x > ub)[0] #get y arrays above and below the contaminant y21fit = yclipfit[x21] y22fit = yclipfit[x22] y21find = yclipfind[x21] y22find = yclipfind[x22] #how many channels does the contaminant have length_difference = len(x) - len(x21) - len(x22) #get an array of 0s to replace the contaminant ys with y20 = np.zeros(length_difference) #now to stick together the arrays #need the ys below the contaminant, then the zeroes where the contaminant was, and then the ys above the contaminant ynewfit = np.append(y21fit, y20) ynewfit = np.append(ynewfit, y22fit) ynewfind = np.append(y21find, y20) ynewfind = np.append(ynewfind, y22find) #now replace the old ys with the new one yclipfit = ynewfit yclipfind = ynewfind #now plot that and ask if they're happy with it clip_spectrum = ad.spectrum_plotter(x, yclipfind) clip_spectrum.show() confirmclip = ad.y_or_n('Are you happy with this removal?') #what to do now? if they aren't happy, ask if they want another go. if they think there aren't any contaminants, return to yinit #if there still are, then they can have another go if confirmclip == 'n': cancel = ad.y_or_n('Are you sure there are any contaminants?') if cancel == 'n': yclipfit = yinit yclipfind = ybase if split: xlist, ylist = [[x],[yinit]] break if cancel == 'y': yclipfit = yclipfitbuffer yclipfind = yclipfindbuffer continue else: #all good, so can continue if split: borders.append(int(max(np.intersect1d(x, x[x21], return_indices = True)[1]))) borders.append(int(min(np.intersect1d(x, x[x22], return_indices = True)[1]))) else: pass #do they want to get rid of more contaminants? continue loop if they do, don't if they don't moreclip = ad.y_or_n('Would you like to remove any more contaminants?') if moreclip == 'y': continue else: break if split: borders = np.sort(borders) for i, border in enumerate(borders): if ((i % 2 == 0) and (i is not len(borders))): ylist.append(yinit[border:borders[i+1]]) xlist.append(x[border:borders[i+1]]) return(xlist, ylist, yclipfind) return yclipfit, yclipfind