def test_basic(self): assert_allclose(windows.nuttall(6, sym=False), [0.0003628, 0.0613345, 0.5292298, 1.0, 0.5292298, 0.0613345]) assert_allclose(windows.nuttall(7, sym=False), [0.0003628, 0.03777576895352025, 0.3427276199688195, 0.8918518610776603, 0.8918518610776603, 0.3427276199688196, 0.0377757689535203]) assert_allclose(windows.nuttall(6), [0.0003628, 0.1105152530498718, 0.7982580969501282, 0.7982580969501283, 0.1105152530498719, 0.0003628]) assert_allclose(windows.nuttall(7, True), [0.0003628, 0.0613345, 0.5292298, 1.0, 0.5292298, 0.0613345, 0.0003628])
def test_basic(self): assert_allclose(windows.nuttall(6, sym=False), [0.0003628, 0.0613345, 0.5292298, 1.0, 0.5292298, 0.0613345]) assert_allclose(windows.nuttall(7, sym=False), [0.0003628, 0.03777576895352025, 0.3427276199688195, 0.8918518610776603, 0.8918518610776603, 0.3427276199688196, 0.0377757689535203]) assert_allclose(windows.nuttall(6), [0.0003628, 0.1105152530498718, 0.7982580969501282, 0.7982580969501283, 0.1105152530498719, 0.0003628]) assert_allclose(windows.nuttall(7, True), [0.0003628, 0.0613345, 0.5292298, 1.0, 0.5292298, 0.0613345, 0.0003628])
def apply_filter(data_array, filter_array, fft_multiplier=None, ifft_multiplier=None, output_multiplier=None, apply_window_func=False, window_shift=None, invert_filter=False): """FFT image cube, apply mask and iFFT the output back to image domain. Parameters ---------- data_array : ndarray Image cube. filter_array : ndarray Filter to multiply to FFT(data_array). Same shape as data_array. Assume (min, max) = (0, 1). fft_multiplier : float, optional Scalar to multiply to FFT output before masking. ifft_multiplier : float, optional Scalar to multiply to the masked array before iFFT back. output_multiplier : float, optional Scalar to multiply to output apply_window_func : bool, optional Apply blackman-nuttall window function to the first dimension of the data_array before FFT. window_shift : int Shift window function by these many pixels. Negative will shift to the left. invert_filter: bool, optional Invert the filter (1 - filter_array) before applying. Default is False. Returns ------- out: ndarray Masked image cube. """ if apply_window_func: k = nuttall(data_array.shape[0]) if window_shift: k = np.roll(k, window_shift) w = (np.ones_like(data_array).T * k).T else: w = np.ones_like(data_array) data_fft = fftshift(fftn(ifftshift(data_array * w))) if fft_multiplier is not None: data_fft *= fft_multiplier if invert_filter: filter_array = 1 - filter_array data_fft *= filter_array out = fftshift(ifftn(ifftshift(data_fft))) / w if ifft_multiplier is not None: out *= ifft_multiplier if output_multiplier is not None: out *= output_multiplier return out.real
def apply_filter(data_array, filter_array, fft_multiplier=None, ifft_multiplier=None, output_multiplier=None, apply_window_func=False, window_shift=None, invert_filter=False): """FFT image cube, apply mask and iFFT the output back to image domain. Parameters ---------- data_array : ndarray Image cube. filter_array : ndarray Filter to multiply to FFT(data_array). Same shape as data_array. Assume (min, max) = (0, 1). fft_multiplier : float, optional Scalar to multiply to FFT output before masking. ifft_multiplier : float, optional Scalar to multiply to the masked array before iFFT back. output_multiplier : float, optional Scalar to multiply to output apply_window_func : bool, optional Apply blackman-nuttall window function to the first dimension of the data_array before FFT. window_shift : int Shift window function by these many pixels. Negative will shift to the left. invert_filter: bool, optional Invert the filter (1 - filter_array) before applying. Default is False. Returns ------- out: ndarray Masked image cube. """ if apply_window_func: k = nuttall(data_array.shape[0]) if window_shift: k = np.roll(k, window_shift) w = (np.ones_like(data_array).T * k).T else: w = np.ones_like(data_array) data_fft = fftshift(fftn(ifftshift(data_array * w))) if fft_multiplier is not None: data_fft *= fft_multiplier if invert_filter: filter_array = 1 - filter_array data_fft *= filter_array out = fftshift(ifftn(ifftshift(data_fft))) / w if ifft_multiplier is not None: out *= ifft_multiplier if output_multiplier is not None: out *= output_multiplier return out.real
def pre_processing(self): """ Complete various pre-processing steps for encoded protein sequences before doing any of the DSP-related functions or transformations. Zero-pad the sequences, remove any +/- infinity or NAN values, get the approximate protein spectra and window function parameter names. Parameters ---------- :self (PyDSP object): instance of PyDSP class. Returns ------- None """ #zero-pad encoded sequences so they are all the same length self.protein_seqs = zero_padding(self.protein_seqs) #get shape parameters of proteins seqs self.num_seqs = self.protein_seqs.shape[0] self.signal_len = self.protein_seqs.shape[1] #replace any positive or negative infinity or NAN values with 0 self.protein_seqs[self.protein_seqs == -np.inf] = 0 self.protein_seqs[self.protein_seqs == np.inf] = 0 self.protein_seqs[self.protein_seqs == np.nan] = 0 #replace any NAN's with 0's #self.protein_seqs.fillna(0, inplace=True) self.protein_seqs = np.nan_to_num(self.protein_seqs) #initialise zeros array to store all protein spectra self.fft_power = np.zeros((self.num_seqs, self.signal_len)) self.fft_real = np.zeros((self.num_seqs, self.signal_len)) self.fft_imag = np.zeros((self.num_seqs, self.signal_len)) self.fft_abs = np.zeros((self.num_seqs, self.signal_len)) #list of accepted spectra, window functions and filters all_spectra = ['power', 'absolute', 'real', 'imaginary'] all_windows = [ 'hamming', 'blackman', 'blackmanharris', 'gaussian', 'bartlett', 'kaiser', 'barthann', 'bohman', 'chebwin', 'cosine', 'exponential' 'flattop', 'hann', 'boxcar', 'hanning', 'nuttall', 'parzen', 'triang', 'tukey' ] all_filters = [ 'savgol', 'medfilt', 'symiirorder1', 'lfilter', 'hilbert' ] #set required input parameters, raise error if spectrum is none if self.spectrum == None: raise ValueError( 'Invalid input Spectrum type ({}) not available in valid spectra: {}' .format(self.spectrum, all_spectra)) else: #get closest correct spectra from user input, if no close match then raise error spectra_matches = (get_close_matches(self.spectrum, all_spectra, cutoff=0.4)) if spectra_matches == []: raise ValueError( 'Invalid input Spectrum type ({}) not available in valid spectra: {}' .format(self.spectrum, all_spectra)) else: self.spectra = spectra_matches[0] #closest match in array if self.window_type == None: self.window = 1 #window = 1 is the same as applying no window else: #get closest correct window function from user input window_matches = (get_close_matches(self.window, all_windows, cutoff=0.4)) #check if sym=True or sym=False #get window function specified by window input parameter, if no match then window = 1 if window_matches != []: if window_matches[0] == 'hamming': self.window = hamming(self.signal_len, sym=True) self.window_type = "hamming" elif window_matches[0] == "blackman": self.window = blackman(self.signal_len, sym=True) self.window = "blackman" elif window_matches[0] == "blackmanharris": self.window = blackmanharris(self.signal_len, sym=True) #** self.window_type = "blackmanharris" elif window_matches[0] == "bartlett": self.window = bartlett(self.signal_len, sym=True) self.window_type = "bartlett" elif window_matches[0] == "gaussian": self.window = gaussian(self.signal_len, std=7, sym=True) self.window_type = "gaussian" elif window_matches[0] == "kaiser": self.window = kaiser(self.signal_len, beta=14, sym=True) self.window_type = "kaiser" elif window_matches[0] == "hanning": self.window = hanning(self.signal_len, sym=True) self.window_type = "hanning" elif window_matches[0] == "barthann": self.window = barthann(self.signal_len, sym=True) self.window_type = "barthann" elif window_matches[0] == "bohman": self.window = bohman(self.signal_len, sym=True) self.window_type = "bohman" elif window_matches[0] == "chebwin": self.window = chebwin(self.signal_len, sym=True) self.window_type = "chebwin" elif window_matches[0] == "cosine": self.window = cosine(self.signal_len, sym=True) self.window_type = "cosine" elif window_matches[0] == "exponential": self.window = exponential(self.signal_len, sym=True) self.window_type = "exponential" elif window_matches[0] == "flattop": self.window = flattop(self.signal_len, sym=True) self.window_type = "flattop" elif window_matches[0] == "boxcar": self.window = boxcar(self.signal_len, sym=True) self.window_type = "boxcar" elif window_matches[0] == "nuttall": self.window = nuttall(self.signal_len, sym=True) self.window_type = "nuttall" elif window_matches[0] == "parzen": self.window = parzen(self.signal_len, sym=True) self.window_type = "parzen" elif window_matches[0] == "triang": self.window = triang(self.signal_len, sym=True) self.window_type = "triang" elif window_matches[0] == "tukey": self.window = tukey(self.signal_len, sym=True) self.window_type = "tukey" else: self.window = 1 #window = 1 is the same as applying no window #calculate convolution from protein sequences if self.convolution is not None: if self.window is not None: self.convoled_seqs = signal.convolve( self.protein_seqs, self.window, mode='same') / sum( self.window) if self.filter != None: #get closest correct filter from user input filter_matches = (get_close_matches(self.filter, all_filters, cutoff=0.4)) #set filter attribute according to approximate user input if filter_matches != []: if filter_matches[0] == 'savgol': self.filter = savgol_filter(self.signal_len, self.signal_len) elif filter_matches[0] == 'medfilt': self.filter = medfilt(self.signal_len) elif filter_matches[0] == 'symiirorder1': self.filter = symiirorder1(self.signal_len, c0=1, z1=1) elif filter_matches[0] == 'lfilter': self.filter = lfilter(self.signal_len) elif filter_matches[0] == 'hilbert': self.filter = hilbert(self.signal_len) else: self.filter = "" #no filter