def sinc_detrend(self, signal): trend = core.sinc_smooth(signal, self.T_c, self.dt, self.M) detrended = signal - trend # for easier interface return directly return detrended
def sinc_detrend(self, signal, T_c): ''' Convenience function which right away subtracts the trend obtained by sinc filtering. See 'sinc_smooth' for details. ''' trend = core.sinc_smooth(signal, T_c, self.dt, self.M) detrended = signal - trend return detrended
def sinc_smooth(self, signal, T_c): ''' Convolve the signal with a sinc filter of cut-off period *T_c*. Returns the smoothed signal representing the non-linear trend. Parameters ---------- signal : a sequence T_c : float, Cut off period for the sinc-filter detrending, all periods larger than that one are removed from the signal Returns ------- trend : numpy 1d-array ''' trend = core.sinc_smooth(signal, T_c, self.dt, M=self.M) return trend
def get_trend(self, signal): trend = core.sinc_smooth(signal, self.T_c, self.dt, M=self.M) return trend
def transform_stack(movie, dt, Tmin, Tmax, nT, T_c = None, win_size = None): ''' Analyzes a 3-dimensional array with shape (NFrames, ydim, xdim) along its 1st axis 'pixel-by-pixel'. Returns four arrays with the same shape as the input array holding the results of the transform for each pixel. For high spatial resolution input this might take a very long time as ydim \times xdim transformations have to be calculated! Parallel execution is recommended (see `run_parallel` below). Parameters ---------- movie : ndarray with ndim = 3, transform is done along 1st axis dt : float, sampling interval Tmin : float, smallest period Tmax : float, largest period nT : int, number of periods/transforms T_c : float, sinc cut off period, defaults to None to disable sinc-detrending (not recommended!) win_size : float, amplitude normalization sliding window size. Default is None which disables normalization. Returns ------- results : dictionary, with keys holding the output movies 'phase' : 32bit ndarray, holding the instantaneous phases 'period' : 32bit ndarray, holding the instantaneous periods 'power' : 32bit ndarray, holding the wavelet powers 'amplitude' : 32bit ndarray, holding the instantaneous amplitudes ''' if Tmin < 2 * dt: logger.warning('Warning, Nyquist limit is 2 times the sampling interval!') logger.info('..setting Tmin to {:.2f}'.format( 2 * dt )) Tmin = 2 * dt if Tmax > dt * movie.shape[0]: logger.warning('Warning: Very large periods chosen!') logger.info('..setting Tmax to {:.2f}'.format( dt * Nt )) Tmax = dt * Nt # the periods to scan for periods = np.linspace(Tmin, Tmax, nT) # create output arrays, needs 32bit for Fiji FloatProcessor :/ period_movie = np.zeros(movie.shape, dtype=np.float32) phase_movie = np.zeros(movie.shape, dtype=np.float32) power_movie = np.zeros(movie.shape, dtype=np.float32) amplitude_movie = np.zeros(movie.shape, dtype=np.float32) ydim, xdim = movie.shape[1:] # F, Y, X ordering Npixels = ydim * xdim logger.info(f'Computing the transforms for {Npixels} pixels') sys.stdout.flush() # loop over pixel coordinates for x in range(xdim): for y in range(ydim): # show progress if Npixels < 10: logger.info(f"Processed {(ydim*x + y)/Npixels * 100 :.1f}%..") sys.stdout.flush() elif (ydim*x + y)%(int(Npixels/5)) == 0 and x != 0: logger.info(f"Processed {(ydim*x + y)/Npixels * 100 :.1f}%..") input_vec = movie[:, y, x] # the time_series at pixel (x,y) signal = input_vec # detrending if T_c is not None: trend = pbcore.sinc_smooth(signal, T_c, dt) signal = signal - trend # amplitude normalization? if win_size is not None: signal = pbcore.normalize_with_envelope(signal, win_size, dt) sigma = np.std(signal) Nt = len(signal) modulus, wlet = pbcore.compute_spectrum(signal, dt, periods) ridge_ys = pbcore.get_maxRidge_ys(modulus) ridge_periods = periods[ridge_ys] powers = modulus[ridge_ys, np.arange(Nt)] phases = np.angle(wlet[ridge_ys, np.arange(Nt)]) # map to [0, 2pi] phases = phases % (2 * np.pi) amplitudes = pbcore.power_to_amplitude(ridge_periods, powers, sigma, dt) phase_movie[:, y, x] = phases period_movie[:, y, x] = ridge_periods power_movie[:, y, x] = powers amplitude_movie[:, y, x] = amplitudes results = {'phase' : phase_movie, 'period' : period_movie, 'power' : power_movie, 'amplitude' : amplitude_movie} return results