def apodise(image, border, order=8):
    """
    Parameters
    ----------

    image: np.ndarray
    border: int, the size of the boreder in pixels

    Note
    ----
    The image is assumed to be of float datatype, no datatype management
    is performed.

    This is different from the original apodistation method,
    which multiplied the image borders by a quater of a sine.
    """
    # stackoverflow.com/questions/46211487/apodization-mask-for-fast-fourier-transforms-in-python
    nx, ny = image.shape
    # Define a general Gaussian in 2D as outer product of the function with itself
    window = np.outer(
        general_gaussian(nx, order, nx // 2 - border),
        general_gaussian(ny, order, ny // 2 - border),
    )
    ap_image = window * image

    return ap_image
Example #2
0
    def __init__(self, config, tool, **kwargs):
        """
        Use a method for filtering the signal

        Parameters
        ----------
        config : traitlets.loader.Config
            Configuration specified by config file or cmdline arguments.
            Used to set traitlet values.
            Set to None if no configuration to pass.
        tool : ctapipe.core.Tool
            Tool executable that is calling this component.
            Passes the correct logger to the component.
            Set to None if no Tool to pass.
        kwargs
        """
        super().__init__(config=config, parent=tool, **kwargs)

        # Cleaning steps for plotting
        self.stage_names = [
            '0: raw',
            '1: convolved (fast)',
            '2: no pulse',
            '3: smooth baseline',
            '4: cleaned',
        ]
        self.stages = {}

        self.kernel_fast = general_gaussian(3, p=1.0, sig=1)
        self.kernel_slow = general_gaussian(10, p=1.0, sig=32)
Example #3
0
    def __init__(self, config, tool, **kwargs):
        """
        Use a method to smooth waveform

        Parameters
        ----------
        config : traitlets.loader.Config
            Configuration specified by config file or cmdline arguments.
            Used to set traitlet values.
            Set to None if no configuration to pass.
        tool : ctapipe.core.Tool
            Tool executable that is calling this component.
            Passes the correct logger to the component.
            Set to None if no Tool to pass.
        kwargs
        """
        super().__init__(config=config, parent=tool, **kwargs)

        if self.focus == 'pulse':
            n_points = 20
            pulsew = 1
            self.kernel = general_gaussian(n_points, p=1.0, sig=pulsew)
        elif self.focus == 'baseline':
            n_points = 10
            w = 32
            self.kernel = general_gaussian(n_points, p=1.0, sig=w)
def filter_and_subtract(filtered, wavelength, window_size, sigma, pad=True):
    mask = filtered.mask.copy()
    orig = filtered.copy()
    pad_size = 0
    orig_begin = 0
    orig_end = mask.size

    if pad:
        trimmed_filtered, orig_begin, orig_end = trim_array_from_mask(filtered, mask, buffer=0)
        low_extent = np.ma.concatenate( (trimmed_filtered[:window_size/2+1], trimmed_filtered[:window_size/2+1][::-1]) )
        high_extent = np.ma.concatenate( (trimmed_filtered[-window_size/2:][::-1], trimmed_filtered[-window_size/2:]) )
        pad_size = low_extent.size
        filtered = np.ma.concatenate( (low_extent, trimmed_filtered, high_extent) )

    window = general_gaussian(window_size, p=2.5, sig=sigma, sym=True) + general_gaussian(window_size, p=0.5, sig=sigma*4, sym=True)

    if np.any(filtered.mask) and np.any(~filtered.mask):
        filtered[mask] = np.interp(wavelength[mask], wavelength[~mask], filtered[~mask])
    filtered = fftconvolve(window, filtered)
    filtered = (np.ma.average(orig) / np.ma.average(filtered)) * filtered
    filtered = np.roll(filtered, -(window_size-1)/2)[:-(window_size-1)]

    if pad:
        new_filtered = np.ma.empty((mask.size, ), dtype=float)
        new_filtered[:] = 0
        new_filtered[orig_begin:orig_end+1] = filtered[pad_size:-pad_size]
        new_filtered.mask = mask
        filtered = new_filtered

    return filtered
Example #5
0
def getData():    #function called to get data
    
    startTime = time.time()
    raw650 = np.array(session[s1Vals])
    raw950 = np.array(session[s2Vals])
  
 #   while True:

   #     if time.time() - startTime >= 5:
    startTime = time.time()
    print('got data')
    working950 = reject_outliers(raw950)
    working650 = reject_outliers(raw650)
    sig950 = np.std(working950)
    sig650 = np.std(working650)
    print(sig650)
    window950 = signal.general_gaussian(51, p=1.5, sig= sig950)
    filtered950 = signal.fftconvolve(window950, working950)
    filtered950 = (np.average(working950) / np.average(filtered950)) * filtered950
    window650 = signal.general_gaussian(51, p=1.5, sig= sig650)
    filtered650 = signal.fftconvolve(window650, working650)
    filtered650 = (np.average(working650) / np.average(filtered650)) * filtered650

  #  filtered = np.roll(filtered, -25)
 #    plt.plot(working950)
 # #   plt.plot(window950)
 #    plt.plot(filtered950)
 #    plt.plot(raw650)
 #    plt.show()
    
    print(filtered950)
    
    print(working950)
    concentration(filtered650,filtered950)
Example #6
0
def get_reconstruction(tiff_path, discs, row, params):
    # Read the tiff file
    imgs = read_tiff(tiff_path)

    if imgs[0].shape[0] != imgs[0].shape[1]:
        print('The input image is not square. The image will be cropped to be a*a where `a` is the smallest dimension.')
        a = min(imgs[0].shape[0], imgs[0].shape[1])
        slice_x, slice_y = slice(0, a), slice(0, a)
        imgs = [img[slice_x, slice_y] for img in imgs]
    
    window = params['window']
    a, p, sig = params['a'], params['p'], params['sig']
    do_psd = params['do_psd']
    
    # Apodization
    width, height = imgs[0].shape  # Images aren't supposed to have 3rd dimension
    if window is not None and window.lower()=='gaussian':
        w = np.outer(signal.general_gaussian(width, p=p, sig=sig), signal.general_gaussian(width, p=p, sig=sig))
    elif window is not None and window.lower()=='tukey':
        w = np.outer(signal.tukey(width, alpha=a), signal.tukey(height, alpha=a))
    elif window is None or window.lower() is 'none':
        w=1

    imgs = [w*img for img in tqdm(imgs, desc='Processing Apodization', leave=False)]
    
    # Periodic Smooth Decomposition
    if do_psd:
        imgs = [psd(img)[0] for img in tqdm(imgs, desc='Processing PSD', leave=False)]
    
    imgs = [cp.array(img) for img in imgs]  # Transfer to GPU

    IMAGESIZE = imgs[0].shape[0]
    scale = params['scale']            
    hres_size = (IMAGESIZE * scale, IMAGESIZE * scale)

    # Remove keys not used by the reconstruction algo
    prms = {k: params[k] for k in params.keys() - ['scale', 'do_psd', 'window', 'a', 'p', 'sig']}
    
    # Reconstruction
    print('Performing Reconstruction...', end='')
    obj, pupil = reconstruct_v2(
        imgs,
        discs,
        row,
        hres_size,
        **prms
    )
    print('Done!')
    
    return obj, pupil, imgs
    def __init__(self, config, tool, **kwargs):
        """
        Use a method for filtering the signal

        Parameters
        ----------
        config : traitlets.loader.Config
            Configuration specified by config file or cmdline arguments.
            Used to set traitlet values.
            Set to None if no configuration to pass.
        tool : ctapipe.core.Tool
            Tool executable that is calling this component.
            Passes the correct logger to the component.
            Set to None if no Tool to pass.
        kwargs
        """
        super().__init__(config=config, tool=tool, **kwargs)

        # Cleaning steps for plotting
        self.stages = {}

        if self.t0:
            self.log.info("User has set t0, extracted t0 will be overridden")

        self.kernel = general_gaussian(10, p=1.0, sig=32)
	def peakFinder(self, WLSTraces, windowParametersX, dataParametersX, medianFactorPF, stdFactor,
		convWindow, convPower, convSig, minimaWindowPF):

		countedPhotons = np.array([])
		photonInd = []
		print('Looking at '+str(len(WLSTraces.columns))+'traces.')
		for i, thisTrace in enumerate(WLSTraces.columns):
			data = np.array(-1*WLSTraces[thisTrace][windowParametersX['SIL']:windowParametersX['SIU']])
			medCutoff = medianFactorPF*np.median(abs(data))
			stdCutoff = stdFactor*np.std(data[np.where(abs(data) < medCutoff)])
			window = signal.general_gaussian(convWindow, p=convPower, sig=convSig)
			filtered = signal.fftconvolve(window, data)
			filtered = (np.average(data) / np.average(filtered)) * filtered
			filtered = np.roll(filtered, -1*int((convWindow-1)/2))
			peakind = signal.argrelmax(filtered, order=minimaWindowPF)[0]
			newind = peakind[np.where(filtered[peakind] > stdCutoff)]
			photonInd.append(newind)
			countedPhotons = np.append(countedPhotons, len(newind))
			if ((i % 100) == 0):
				print('Working on traces '+self.c.blue(str(i)+' - '+str(i+99))+' for peak detection...')
		print('Total photons counted: '+self.c.lightgreen(str(sum(countedPhotons))))
		print('Average number of photons per trace: '+self.c.lightgreen(str(sum(countedPhotons)/len(WLSTraces.columns))))
		print('Median number of photons: '+self.c.lightgreen(str(np.median(countedPhotons))))
		
		return photonInd, countedPhotons
    def detect(self, sampFreq, snd):
        if type(snd[0]) != np.int16:
            sndBackup = [snd[i][0] for i in range(len(snd))]
            snd = np.array(sndBackup)

        width = np.array(
            range(int(0.15 * sampFreq), int(3 * sampFreq), int(
                0.05 * sampFreq))) / 100

        snd = snd / 1000.0

        power = abs(snd)
        power = self.myfilter(power, sampFreq)

        powerSample = self.downSampling(power, 100)

        window = signal.general_gaussian(500, p=0.5, sig=500)
        filtered = signal.fftconvolve(window, powerSample)
        filtered = (np.average(powerSample) / np.average(filtered)) * filtered
        filtered = np.roll(filtered, -25)

        peaks = signal.find_peaks_cwt(filtered, width)

        peakTimeDiff = [peaks[i] - peaks[i - 1] for i in range(1, len(peaks))]

        return (np.std(peakTimeDiff) / np.mean(peakTimeDiff)) < 0.4
Example #10
0
 def Slownoise_Smooth(self,data):
     """
     generates smoothed signal from peak-less signal and raw_data
     takes: peak-less signal , raw_data
     returns: smoothed signal 1
     """
     slownoise = self.RemovePeaksBasedOnRMS(data,3)
     nsamples = 500
     window = sig.general_gaussian(nsamples, p=1.0, sig=nsamples/5)
     slownoise_smooth = sig.fftconvolve(slownoise, window, "same")
     slownoise_smooth = (np.average(slownoise) / np.average(slownoise_smooth)) * slownoise_smooth
     self.filtered_signal_1 = data - slownoise_smooth
     self.peakless = slownoise 
     '''
     plt.figure()
     plt.title('slownoise')
     plt.xlabel('Time [s]')
     plt.ylabel('Voltage [V]')
     plt.plot(self.time,data,'grey')
     plt.plot(self.time,slownoise,'red')
     plt.title('slownoise_smooth')
     plt.plot(self.time,slownoise_smooth,'blue')
     #plt.figure()
     plt.xlabel('Time [s]')
     plt.ylabel('Voltage [V]')
     #plt.title('raw data ,filtered signal 1')
     #plt.plot(self.time,data,'grey')
     #plt.plot(self.time,self.filtered_signal_1)
     plt.show()
     '''
     return self.filtered_signal_1
Example #11
0
	def plot_cm(self, minVal, maxVal, y_axmin=0, y_axmax=255, array=None, smooth=False, peaks=False, mind=1, ind=1, canny=False):
		#Create Plot of Contour Mean Intensities.
		if self.all_cm == None or self.filtered_contours == None:
			self.big_cm(canny=canny, minVal=minVal, maxVal=maxVal, array=array)

		c_m = self.all_cm

		if peaks and not smooth:

			data = c_m[:,ind,1]
			window = signal.general_gaussian(4, p=0.5, sig=100)
			filtered = signal.fftconvolve(window, data)
			filtered = (np.average(data)/np.average(filtered))*filtered
		 		
			detect_peaks(filtered, show=True, mpd = mind, y_axmin=y_axmin, y_axmax=y_axmax)
			#Source: http://nbviewer.ipython.org/github/demotu/BMC/blob/master/notebooks/DetectPeaks.ipynb

		if smooth:
			xnew=np.linspace(0,len(c_m[:,:,1]),len(c_m[:,:,1]))
			y1=c_m[:,ind,1]#for now must be specified
			f=interp1d(xnew,y1,kind='cubic')(xnew)
			plt.plot(f)
			plt.ylabel('Mean Intensity')
			plt.show()

		if not smooth and not peaks:
			plt.plot(c_m[:,:,1]) 
			plt.ylabel('Mean Intensity')
			plt.show()
    def Slownoise_Smooth(self,data,ite):
        """
        generates smoothed signal from peak-less signal and raw_data
        takes: peak-less signal , raw_data
        returns: smoothed signal 1
        """


        slownoise,level = self.RemovePeaksBasedOnRMS3(data)
        meanzero = np.mean(slownoise)
        self.peakless = slownoise - meanzero #peakless signal - pedestal mean
        data_at_0 = data - meanzero #subtract mean of pedestal from data 
        # not given that trace is always with same offset // gaincalc only takes into account meanzero of last segment
        nsamples = 700 #determines width of smoothing gaussian (/5) = sigma of gauss 
        window = sig.general_gaussian(nsamples, p=1.0, sig=nsamples/5)
        slownoise_smooth = sig.fftconvolve(self.peakless, window, "same")
        slownoise_smooth = (np.average(self.peakless) / np.average(slownoise_smooth)) * slownoise_smooth
        self.filtered_signal_1 = data_at_0 - slownoise_smooth

        self.ReportProgress(ite,int(float(self.calcsegmentspro)/100*self.ranger))
       
        #print 'datamean ',np.mean(data)
        #print 'meanzero ',meanzero
        #print 'data0 ',np.mean(data_at_0)

        return self.filtered_signal_1
Example #13
0
def audio_resample_tf(y,
                      orig_sr,
                      target_sr,
                      res_type='kaiser_best',
                      fix=True,
                      scale=False,
                      use_smoothing=True,
                      use_bicubic=False):
    ratio = float(target_sr) / orig_sr
    y_length = y.get_shape().as_list()[1]
    n_samples = int(np.ceil(y_length * ratio))

    if use_smoothing:
        window = general_gaussian(5, 0.5, True).astype(np.float32)
        window /= window.sum()

        window_tf = tf.constant(window[:, np.newaxis, np.newaxis, np.newaxis],
                                dtype=tf.float32)
        y = tf.nn.conv2d(y, window_tf, strides=[1, 1, 1, 1], padding="SAME")

    if (not use_bicubic) and (np.mod(np.log2(float(orig_sr) / target_sr), 1)
                              == 0.0):
        for i in range(int(np.log2(float(orig_sr) / target_sr)) - 1):
            y = y[:, ::2, :, :]
            y = tf.nn.conv2d(y,
                             window_tf,
                             strides=[1, 1, 1, 1],
                             padding="SAME")
        y_hat = y[:, ::2, :, :]
    else:
        y_hat = bicubic_interp_2d(y, [n_samples, 1], endpoint=False)

    if scale:
        y_hat /= np.sqrt(ratio)
    return y_hat
Example #14
0
def Apod(im, size, power, sigma):
    av = im.mean()
    im = im - av
    gen_gaussian = general_gaussian(size, power, sigma)
    window = np.outer(gen_gaussian, gen_gaussian)
    apod = im * window
    apod = apod + av
    return apod
    def Slownoise_Smooth(self, data, ite):
        """
        generates smoothed signal from peak-less signal and raw_data
        takes: peak-less signal , raw_data
        returns: smoothed signal 1
        """

        slownoise, level = self.RemovePeaksBasedOnRMS3(data)
        meanzero = np.mean(slownoise)

        self.peakless = slownoise - meanzero  #peakless signal - pedestal mean
        meanpeakless = np.mean(self.peakless)
        #print meanpeakless
        #print meanzero, ' ',meanpeakless
        data_at_0 = data - meanzero  #subtract mean of pedestal from data
        # not given that trace is always with same offset // gain-calc only takes into account meanzero of last segment

        nsamples = 700  #determines width of smoothing gaussian (/5) = sigma of gauss
        window = sig.general_gaussian(nsamples, p=1.0, sig=nsamples / 5)
        slownoise_smooth = sig.fftconvolve(self.peakless, window, "same")
        slownoise_smooth = (np.average(self.peakless) /
                            np.average(slownoise_smooth)) * slownoise_smooth

        self.filtered_signal_1 = data_at_0 - slownoise_smooth

        if ite == 0:

            fig3, (ax1, ax2) = plt.subplots(1, 2, sharey=False)
            rawdat = self.raw_data * 1e3  #mV
            ax1.set_title('Slow Noise')
            ax1.set_xlabel('Time [$\mu$s]')
            ax1.set_xlim(0, 100)
            ax1.set_ylabel('Voltage [mV]')
            ax1.plot(self.time, data, 'b', label='Raw Data')
            ax1.plot(self.time,
                     slownoise_smooth,
                     'w',
                     label='Slownoise Smooth')

            ax2.set_title('Zoom')
            ax2.set_xlabel('Time [$\mu$s]')
            ax2.set_xlim(45, 46)
            ax2.set_ylim(-0.01, 0.04)
            ax2.set_ylabel('Voltage [mV]')
            ax2.plot(self.time, data, 'b', label='Raw Data')
            ax2.plot(self.time,
                     slownoise_smooth,
                     'k',
                     label='Slownoise Smooth')
            ax2.legend()
            fig3.savefig(self.data_file_path + 'SlowNoiseSmoothZoom.pdf',
                         format='pdf',
                         bbox_inches='tight')
            #plt.show()

        self.ReportProgress(ite, self.ranger)
        return self.filtered_signal_1
Example #16
0
    def __init__(self, config, tool, **kwargs):
        super().__init__(config=config, tool=tool, **kwargs)

        # Cleaning steps for plotting
        self.stages = {}

        self.kernel = general_gaussian(10, p=1.0, sig=32)

        self.extractor = self.get_extractor()
Example #17
0
    def __init__(self, config, tool, **kwargs):
        super().__init__(config=config, tool=tool, **kwargs)

        # Cleaning steps for plotting
        self.stages = {}

        self.kernel = general_gaussian(10, p=1.0, sig=32)

        self.extractor = self.get_extractor()
Example #18
0
def findbiggestGauss(x, y, fitwindow=20):
    window = signal.general_gaussian(11, p=0.5, sig=10)
    filtered = signal.fftconvolve(window, y)
    filtered = (np.average(y) / np.average(filtered)) * filtered
    filtered = np.roll(filtered, -5)[:len(y)]
    window = signal.general_gaussian(11, p=0.5, sig=200)
    deriv = np.gradient(filtered)
    dfiltered = signal.fftconvolve(window, deriv)
    dfiltered = (np.average(deriv) / np.average(dfiltered)) * dfiltered
    dfiltered = np.roll(dfiltered, -15)[:len(deriv)]
    zeros = np.sign(dfiltered)
    zeros[zeros == 0] = -1  # replace zeros with -1
    zeros = np.where(np.diff(zeros))[0]
    max = np.argmax(filtered[zeros])
    max = zeros[max]
    # print(x[max])
    initial_guess = (filtered[max], x[max], 10)
    minind = max - fitwindow
    if minind < 0:
        minind = 0
    maxind = max + fitwindow
    if maxind >= len(x):
        maxind = len(x) - 1
    indices = np.linspace(minind, maxind, fitwindow * 2 + 1, dtype=int)

    # popt, pcov = curve_fit(gauss, x[indices], y[indices], p0=initial_guess)
    # perr = np.sqrt(np.diag(pcov))
    def err_fun(p):
        fit = gauss(x[indices], *p)
        diff = np.abs(y[indices] - fit)**2
        return np.sum(diff)

    minimizer_kwargs = {"method": "SLSQP", "tol": 1e-12}
    res = basinhopping(err_fun,
                       initial_guess,
                       minimizer_kwargs=minimizer_kwargs,
                       niter=50)

    # res = minimize(err_fun, start, method='SLSQP',tol=1e-12,bounds = bnds, options={ 'disp': True, 'maxiter': 500})
    # res = minimize(err_fun, initial_guess, method='L-BFGS-B', jac=False, options={'disp': False, 'maxiter': 500})
    # res = minimize(err_fun, start, method='nelder-mead', options={'xtol': 1e-2, 'disp': True})
    popt = res.x
    fitted = gauss(x, popt[0], popt[1], popt[2])
    return popt, fitted
Example #19
0
	def ppeaks(self, minVal, maxVal, ind=1, mph=1, mpd=1, mph2=-95, mpd2=1, thresh=0, 
		array=None, fps=40, calc_widths=False, minWidth=4, maxWidth=50, avg_fluor=True, 
		show=False, show_vl=False, deltax=25, y_axmin=0, y_axmax=2.2, stddev=None,lam=100, p=.001, niter=6, normed=False):
		''' Peak Detection of Contour Mean Intensities.
		http://nbviewer.ipython.org/github/demotu/BMC/blob/master/notebooks/DetectPeaks.ipynb
		mph : detect peaks that are greater than minimum peak height.
    	mph2: same, just for valleys (and negative!)
    	mpd : detect peaks that are at least separated by minimum peak distance.
    	mpd2 : same, but valleys
    	threshold : detect peaks (valleys) that are greater (smaller) than `threshold`
        			in relation to their immediate neighbors.

		Seems to work: (see iPython for other population params.)
		for i in range(length):
    		q.ppeaks(ind=i, mph=85, mpd=15,thresh=0.15, minVal=70, maxVal=175)
    	Normed divides each bit of data by its baseline.
		'''
		if self.all_cm == None or self.filtered_contours == None:
			self.big_cm(canny=True, array=array, minVal=minVal, maxVal=maxVal)
		if stddev==None:
			c_m = self.all_cm
			data = c_m[:,ind,1]
		if stddev!=None: #this means look at nonblinky cells (stddev will be low, coded earlier.)
			if self.stdd==None:
				self.create_std(stddev=stddev, avg_fluor=avg_fluor)
			ar = self.stdd
			data = ar[ind,:]

		window = signal.general_gaussian(4, p=0.5, sig=100)
		filtered = signal.fftconvolve(window, data)
		filtered = (np.average(data)/np.average(filtered))*filtered
		filtered = filtered[4:-5] #truncates bad boundaries from fftconvolve
		original_filtered = np.copy(filtered)

		bline=self.baseline_als(filtered, lam=lam, p=p, niter=niter)
		if normed:
			filtered=filtered/bline
		peaks = detect_peaks(x=filtered, mpd=mpd, mph=mph, threshold=thresh, edge='rising', 
									show=True, y_axmin=0, y_axmax=y_axmax)

		#collect the normed traces of all cells. 
		self.normed_trace.append(filtered)

		y_peaks = []
		for ind in peaks:
			y_peaks.append(filtered[ind])
				
		if calc_widths==True:
			self.p_width(peaks=peaks, bline=bline, filtered=filtered, minWidth=minWidth, maxWidth=maxWidth, cell_ind=ind, show=show, data=data, deltax=deltax, normed=normed)
		
		frames = np.arange(0,len(filtered))
		if show_vl==True:
			plt.plot(frames, original_filtered, 'm', frames, bline,'b--') #filtered-->original_filtered
			plt.show()
Example #20
0
def peak_finder(times, residuals, errors, params, n_peaks=4, plots=False, verbose=False):
    # http://stackoverflow.com/a/25666951
    # Convolve residuals with a gaussian, find relative maxima
    n_points_kernel = 100
    window = signal.general_gaussian(n_points_kernel+1, p=1, sig=12)
    filtered = signal.fftconvolve(window, residuals)
    filtered = (np.average(residuals) / np.average(filtered)) * filtered
    filtered = np.roll(filtered, int(-n_points_kernel/2))
    maxes = signal.argrelmax(filtered)[0]
    maxes = maxes[maxes < len(residuals)]
    maxes = maxes[residuals[maxes] > 0]

    lower_t_bound, upper_t_bound = get_in_transit_bounds(times, params)
    maxes_in_transit = maxes[(times[maxes] < upper_t_bound) & 
                             (times[maxes] > lower_t_bound)]

    if len(maxes_in_transit) == 0:
        if verbose: 
            print('no maxes found')
        return None

    peak_times = times[maxes_in_transit]
    peak_amplitudes = residuals[maxes_in_transit]#len(peak_times)*[3*np.std(residuals)]
    peak_sigmas = len(peak_times)*[4./60/24] # 5 min
    input_parameters = np.vstack([peak_amplitudes, peak_times, peak_sigmas]).T.ravel()
    # result = optimize.fmin(chi2, input_parameters, args=(times, residuals, errors))
    result = optimize.fmin_bfgs(chi2, input_parameters, disp=False,
                                args=(times, residuals, errors))        
    #print(result, result == input_parameters)
    
    if plots:
        fig, ax = plt.subplots(3, 1, figsize=(8, 10), sharex=True)
    
        ax[0].errorbar(times, residuals, fmt='.', color='k')
        [ax[0].axvline(t) for t in times[maxes_in_transit]]
        ax[0].plot(times, summed_gaussians(times, input_parameters), 'r')
        #ax[1].errorbar(times, gaussian_model, fmt='.', color='r')
        ax[0].axhline(0, color='k', ls='--')
        ax[0].set_ylabel('Transit Residuals')
    
        ax[1].errorbar(times, residuals, fmt='.', color='k')
        ax[1].plot(times, summed_gaussians(times, result), 'r')
        #ax[1].errorbar(times, gaussian_model, fmt='.', color='r')
        ax[1].axhline(0, color='k', ls='--')
        ax[1].set_ylabel('Residuals')
    
        ax[2].errorbar(times, residuals - summed_gaussians(times, result), fmt='.', color='k')
        #ax[1].errorbar(times, gaussian_model, fmt='.', color='r')
        ax[2].axhline(0, color='k', ls='--')
        ax[2].set_ylabel('Residuals')

        fig.tight_layout()
        plt.show()
    return result
Example #21
0
def fftfilter(y):
    window = signal.general_gaussian(11, p=0.5, sig=10)
    filtered = signal.fftconvolve(window, y)
    filtered = (np.average(y) / np.average(filtered)) * filtered
    filtered = np.roll(filtered, -5)[:len(y)]
    # filtered = filtered[:1024]
    #peakidx = signal.find_peaks_cwt(filtered, np.arange(1, 10), noise_perc=0.01)
    #plt.plot(x, y)
    #plt.plot(x, filtered, "g")
    #plt.plot(x[peakidx], y[peakidx], "rx")
    #plt.show()
    return filtered
def noise_pnorm(R, p, n, train_data):
    train_n = np.zeros((200, 204))
    for i in enumerate(train_data):
        epsilon = signal.general_gaussian(n, p, sig=p)
        epsilon[abs(epsilon) < 1e-5] = 0
        sign = [(-1)**np.random.randint(2) for i in range(n)]
        x = np.multiply(epsilon, sign)
        z = (np.random.uniform(0, 1))**(1 / n)
        x_pnorm = np.linalg.norm(x, ord=p)
        y = R * z * x / x_pnorm
        train_n[i[0]] = train_data[i[0]] + y
    return train_n
    def __init__(self, config=None, tool=None, **kwargs):
        super().__init__(config=config, tool=tool, **kwargs)

        # Cleaning steps for plotting
        self.stages = {}
        self.stage_names = [
            '0: raw', '1: baseline_sub', '2: no_pulse', '3: smooth_baseline',
            '4: smooth_wf', '5: cleaned'
        ]

        self.kernel = general_gaussian(10, p=1.0, sig=32)

        self.extractor = self.get_extractor()
Example #24
0
    def work(self, input_items, output_items):
        in0 = input_items[0]
        out0 = output_items[0]

        window = signal.general_gaussian(self.m, p=self.p, sig=self.sigma)

        for i in range(0, len(in0)):
            filtered = signal.fftconvolve(window, in0[i])
            filtered = (numpy.average(in0[i]) /
                        numpy.average(filtered)) * filtered
            out0[i][:] = numpy.roll(filtered, -self.m / 2)[:len(in0[i])]

        return len(in0)
Example #25
0
    def Fastnoise_Smooth(self,filtered_signal_1):
        """
        smoothes noise-less signal again
        takes: noise-less signal
        returns: smoothed noise-less signal
       
        peak detection more reliable
        """
        nsamples = 10#20
        window = sig.general_gaussian(nsamples, p=1.0, sig=nsamples/5)
        self.filtered_signal_2 = sig.fftconvolve(filtered_signal_1, window, "same")
        self.filtered_signal_2 = (np.average(filtered_signal_1) / np.average(self.filtered_signal_2)) * self.filtered_signal_2

        return self.filtered_signal_2
Example #26
0
def raw_ctd_filter(input_array=None,
                   filter_type='triangle',
                   win_size=24,
                   parameters=None):
    """raw_ctd_filter function 

    Function takes NUMPY array 
    of raw ctd data and returns filtered data. This function also needs 
    one of three filter types (boxcar, gaussian, triangle) as well as 
    window size. 

    Args:
        param1 (ndarray): Numpy ndarray with predefined header with at 
        param2 (str): One of three tested filter types
          boxcar, gaussian_std, triangle.
          default is triangle
        param3 (int): A window size for the filter. Default is 24, which 
          is the number of frames per second from a SBE9+/11 CTD/Dech unit.
        param4 (ndarray): parameters the dtype names used in filtering the 
          analytical inputs.

    Returns:
        Narray: The return value is a matrix of filtered ctd data with 
          the above listed header values.

    """

    if input_array is None:
        print("In raw_ctd_filter: No data array.")
        return
    else:
        return_array = input_array
        if parameters is None:
            print("In raw_ctd_filter: Empty parameter list.")
        else:
            for p in parameters:
                if filter_type is 'boxcar':
                    win = sig.boxcar(win_size)
                    return_array[str(p)] = sig.convolve(
                        input_array[str(p)], win, mode='same') / len(win)
                elif filter_type is 'gaussian':
                    sigma = np.std(arr)
                    win = sig.general_gaussian(win_size, 1.0, sigma)
                    return_array[str(p)] = sig.convolve(
                        input_array[str(p)], win, mode='same') / (len(win))
                elif filter_type is 'triangle':
                    win = sig.triang(win_size)
                    return_array[p] = 2 * sig.convolve(
                        input_array[p], win, mode='same') / len(win)
    return return_array
def general_gaussian_wf_vs_nf(μ, σ, p):
    """
    Plot a general Gaussian distribution of weight factor vs number factor.

    :param μ: mean
    :param σ: standard deviation
    :param p: shape
    :return: fig, ax
    """
    number_of_points = 100
    numbers = general_gaussian(number_of_points, p,
                               number_of_points * σ / (2 * μ))
    weights = np.linspace(0, 2 * μ, number_of_points)
    return graph_wf_vs_nf(numbers, weights)
Example #28
0
    def __init__(self, config, tool, **kwargs):
        super().__init__(config=config, tool=tool, **kwargs)

        # Cleaning steps for plotting
        self.stages = {}
        self.stage_names = ['0: raw',
                            '1: baseline_sub',
                            '2: no_pulse',
                            '3: smooth_baseline',
                            '4: smooth_wf',
                            '5: cleaned']

        self.kernel = general_gaussian(10, p=1.0, sig=32)

        self.extractor = self.get_extractor()
    def Slownoise_Smooth(self, data, ite):
        """
        generates smoothed signal from peak-less signal and raw_data
        takes: peak-less signal , raw_data
        returns: smoothed signal 1
        """

        rmsn = 2
        slownoise, level = self.RemovePeaksBasedOnRMS3(data, rmsn)

        nsamples = 700  #determines width of smoothing gaussian (/5) = sigma of gauss
        window = sig.general_gaussian(nsamples, p=1.0, sig=nsamples / 5)
        slownoise_smooth = sig.fftconvolve(slownoise, window, "same")
        #slownoise_smooth = (np.average(slownoise) / np.average(slownoise_smooth)) * slownoise_smooth
        slownoise_smooth = (np.average(slownoise) /
                            np.average(slownoise_smooth)) * slownoise_smooth

        self.filtered_signal_1 = data - slownoise_smooth
        self.peakless = slownoise

        if ite == 1:
            fig8 = plt.figure(8)
            ax8 = fig8.add_axes([0.1, 0.1, 0.8, 0.8])
            ax8.set_title('Filter Levels')
            ax8.set_xlabel('Time [us]')
            ax8.set_ylabel('Voltage [V]')
            #ax8.set_xlim(0,100)
            rawdat = ax8.plot(self.time, data, 'grey', label='Raw Data')
            slown = ax8.plot(self.time, slownoise, 'red', label='slow Noise')
            plt.plot((np.amin(self.time), np.amax(self.time)),
                     (level * rmsn, level * rmsn),
                     'b--',
                     label='Cut level')
            ax8.set_title('Smoothed slow Noise')
            slowns = ax8.plot(self.time,
                              slownoise_smooth,
                              'blue',
                              label='Smoothed slow Noise')
            ax8.legend()
            #plt.show()
            fig8.savefig(self.data_file_path + 'FilterLevels.ps',
                         format='ps',
                         bbox_inches='tight')
            #ax8.legend_.remove()
            #fig8.clf()

        self.ReportProgress(ite)
        return self.filtered_signal_1
Example #30
0
def smooth_with_gaussian(time_series, sfreq=1000, gaussian_width=50, N=1000):
    # gaussian_width in samples
    # ---------------------
    import math
    from scipy import signal

    norm_factor = np.sqrt(
        2 * math.pi * gaussian_width**
        2) / sfreq  # sanity check: norm_factor = gaussian_window.sum()
    gaussian_window = signal.general_gaussian(
        M=N, p=1, sig=gaussian_width)  # generate gaussian filter
    norm_factor = (gaussian_window / sfreq).sum()
    smoothed_time_series = np.convolve(time_series,
                                       gaussian_window / norm_factor,
                                       mode="full")  # smooth
    smoothed_time_series = smoothed_time_series[int(round(
        N / 2)):-(int(round(N / 2)) - 1)]  # trim ends
    return smoothed_time_series
Example #31
0
    def _find_grid_pins(resampled_row, fiber, exposure):
        data = resampled_row['flux']+resampled_row['sky']
        wavelength = resampled_row['wavelength']

        window = general_gaussian(21, p=0.5, sig=3)
        filtered = data[:peak_seek_end].copy()
        if np.any(filtered.mask) and np.any(~filtered.mask):
            filtered[filtered.mask] = np.interp(wavelength[filtered.mask], wavelength[~filtered.mask], filtered[~filtered.mask])
        filtered = fftconvolve(window, filtered)
        filtered = (np.ma.average(data[:peak_seek_end]) / np.ma.average(filtered)) * filtered
        filtered = np.roll(filtered, -10)[:peak_seek_end]
        filtered_peak_inds = np.array(argrelextrema(filtered, np.ma.greater))
        filtered_peak_wlens = (filtered_peak_inds*0.975)+3500.26

        peak_set = [fiber, exposure]
        for target in pin_peaks:
            peak_set.append(_find_closest_peak(target, wavelength, data))

        return tuple(peak_set)
Example #32
0
def smoothing_to_get_peaks(sgnl, x_sgnl, param):
    if param['do']:
        window = signal.general_gaussian(param['M'],
                                         p=param['p'],
                                         sig=param['sig'])
        ftrd_sgnl = signal.fftconvolve(window, sgnl)
        ftrd_sgnl = (np.average(sgnl) / np.average(ftrd_sgnl)) * ftrd_sgnl
        ftrd_sgnl = ftrd_sgnl[:((-1) * param['M']) + 1]
    else:
        ftrd_sgnl = sgnl

    if param['plot']:
        plt.plot(x_sgnl, ftrd_sgnl)
        plt.xlabel('Frequency [Hz]', fontsize=10)
        plt.ylabel('Amplitude', fontsize=10)
        plt.title("Gausian filtering of Freq. Domain FFT", fontsize=12)
        plt.show()

    return ftrd_sgnl
    def Fastnoise_Smooth(self,filtered_signal_1):
        """
        smoothes noise-less signal again
        takes: noise-less signal
        returns: smoothed noise-less signal
        peak detection more reliable
        """
        
        sig_smooth_ns = 4 # Depends on pulse shape... 
        win_smooth_ns = sig_smooth_ns * 5 #... possibly FWHM * 1.25
        samples_per_ns = 2.5
        window = sig.general_gaussian(win_smooth_ns*samples_per_ns
                                      , p=1.0, sig=sig_smooth_ns*samples_per_ns)
        self.filtered_signal_2 = sig.fftconvolve(filtered_signal_1, window, "same")
        self.filtered_signal_2 = (np.average(filtered_signal_1) / np.average(self.filtered_signal_2)) * self.filtered_signal_2



        return self.filtered_signal_2
def get_onset_envelope(Sxx, ts, timestep=None, draw=False):
    """
    Gets the strength of note onsets throughout a recording given a
    spectrogram. Calculates spectral flux and then uses a Gaussian
    filter to find the onset strength envelope.
    """
    # calculate spectral flux
    dt = ts[1] - ts[0]
    if timestep is None:
        timestep = 0
    shift_len = int(timestep / dt) + 1
    n, m = Sxx.shape
    blank = np.zeros((n, shift_len))
    old = np.hstack((blank, Sxx))
    new = np.hstack((Sxx, blank))
    diff = (new - old)[:, :m]
    diff = onset_filter(diff)
    diff = np.sum(diff, axis=0)
    diff[:shift_len] = 0

    if draw:
        plt.plot(diff)

    # gaussian filter
    diff += 1
    size = 2
    window = signal.general_gaussian(size * 2 + 1, p=1, sig=20)
    filtered = signal.fftconvolve(window, diff)
    filtered = (np.average(diff) / np.average(filtered)) * filtered
    filtered = np.roll(filtered, -size)
    diff = filtered
    diff = diff[:m]

    l = np.average(diff) - np.std(diff)
    smallest = (np.max(diff) - np.min(diff)) * 0.1 + np.min(diff)
    l = smallest
    diff[diff < l] = l
    if draw:
        plt.plot(diff)
        plt.show()

    return diff
Example #35
0
    def _find_grid_pins(resampled_row, fiber, exposure):
        data = resampled_row['flux'] + resampled_row['sky']
        wavelength = resampled_row['wavelength']

        window = general_gaussian(21, p=0.5, sig=3)
        filtered = data[:peak_seek_end].copy()
        if np.any(filtered.mask) and np.any(~filtered.mask):
            filtered[filtered.mask] = np.interp(wavelength[filtered.mask],
                                                wavelength[~filtered.mask],
                                                filtered[~filtered.mask])
        filtered = fftconvolve(window, filtered)
        filtered = (np.ma.average(data[:peak_seek_end]) /
                    np.ma.average(filtered)) * filtered
        filtered = np.roll(filtered, -10)[:peak_seek_end]
        filtered_peak_inds = np.array(argrelextrema(filtered, np.ma.greater))
        filtered_peak_wlens = (filtered_peak_inds * 0.975) + 3500.26

        peak_set = [fiber, exposure]
        for target in pin_peaks:
            peak_set.append(_find_closest_peak(target, wavelength, data))

        return tuple(peak_set)
Example #36
0
    def Slownoise_Smooth(self, data, ite):
        """
        generates smoothed signal from peak-less signal and raw_data
        takes: peak-less signal , raw_data
        returns: smoothed signal 1
        """

        slownoise, level = self.RemovePeaksBasedOnRMS3(data)
        meanzero = np.mean(slownoise)

        self.peakless = slownoise - meanzero  #peakless signal - pedestal mean
        meanpeakless = np.mean(self.peakless)
        #print meanpeakless
        #print meanzero, ' ',meanpeakless
        data_at_0 = data - meanzero  #subtract mean of pedestal from data
        # not given that trace is always with same offset // gain-calc only takes into account meanzero of last segment

        nsamples = 700  #determines width of smoothing gaussian (/5) = sigma of gauss
        window = sig.general_gaussian(nsamples, p=1.0, sig=nsamples / 5)
        slownoise_smooth = sig.fftconvolve(self.peakless, window, "same")
        slownoise_smooth = (np.average(self.peakless) /
                            np.average(slownoise_smooth)) * slownoise_smooth

        self.filtered_signal_1 = data_at_0 - slownoise_smooth

        if ite == 0:
            fig8 = plt.figure(8)
            #ax8 = fig8.add_axes([0.1, 0.1, 0.8, 0.8])
            #ax8.set_title('Filter Levels')
            #ax8.set_xlabel('Time [$\mu$]')
            #ax8.set_ylabel('Voltage [V]')
            ax81 = fig8.add_subplot(311)
            ax82 = fig8.add_subplot(312)
            ax83 = fig8.add_subplot(313)
            #ax84 = fig8.add_subplot(224)
            #ax8.set_xlim(0,100)
            ax81.plot(self.time, data, 'lightgrey', label='Raw Data')
            ax81.plot((np.amin(self.time), np.amax(self.time)),
                      (meanzero, meanzero),
                      'b-',
                      label='MeanZero')  #rmsn from RemovePeaksBasedOnRMS3 = 2
            ax82.plot(self.time, data_at_0, 'grey', label='Raw Data Corrected')
            ax82.plot(
                (np.amin(self.time), np.amax(self.time)),
                (meanpeakless, meanpeakless),
                'g-',
                label='MeanPeakLess')  #rmsn from RemovePeaksBasedOnRMS3 = 2

            ax83.plot(self.time, data, 'lightgrey', label='Raw Data')
            ax83.plot(self.time, slownoise, 'grey', label='Slownoise')
            ax83.plot((np.amin(self.time), np.amax(self.time)),
                      (meanzero, meanzero),
                      'b-',
                      label='MeanZero')  #rmsn from RemovePeaksBasedOnRMS3 = 2
            ax81.legend()
            ax82.legend()
            ax83.legend()
            fig8.savefig(self.data_file_path + 'PeaklessGen.pdf',
                         format='pdf',
                         bbox_inches='tight')

            #rawdatcorr = ax8.plot(self.time,data_at_0,'grey',label='Raw Data Corrected')
            #slown = ax8.plot(self.time,slownoise,'red',label='slow Noise')
            #plt.plot((np.amin(self.time),np.amax(self.time)), (level*2,level*2), 'k--',label='Cut level') #rmsn from RemovePeaksBasedOnRMS3 = 2
            #plt.plot((np.amin(self.time),np.amax(self.time)), (meanzero,meanzero), 'b-',label='MeanZero') #rmsn from RemovePeaksBasedOnRMS3 = 2
            #plt.plot((np.amin(self.time),np.amax(self.time)), (meanpeakless,meanpeakless), 'b--',label='MeanPeakLess') #rmsn from RemovePeaksBasedOnRMS3 = 2
            #ax8.set_title('Smoothed slow Noise')
            #slowns = ax8.plot(self.time,slownoise_smooth,'blue',label='Smoothed slow Noise')
            #ax8.legend()
            #plt.show()
            #fig8.savefig(self.data_file_path+'FilterLevels.pdf',format='pdf',bbox_inches='tight')
            #ax8.legend_.remove()
            #fig8.clf()

        self.ReportProgress(ite, self.ranger)
        return self.filtered_signal_1
	def gauss_filter(self, data):
		window = signal.general_gaussian(51, p=0.5, sig=20)
	 	filtered = signal.fftconvolve(window, data)
	 	filtered = (np.average(data) / np.average(filtered)) * filtered
	 	filtered = np.roll(filtered, -25)
		return filtered
 def gaussian_smooth(data):
     window = general_gaussian(51, p=0.5, sig=20)
     filtered = fftconvolve(window, data)
     filtered = (np.average(data) / np.average(filtered)) * filtered
     filtered = np.roll(filtered, -25)
     return filtered[0:len(data)]
Example #39
0
# Plot the window and its frequency response:

from scipy import signal
from scipy.fftpack import fft, fftshift
import matplotlib.pyplot as plt

window = signal.general_gaussian(51, p=1.5, sig=7)
plt.plot(window)
plt.title(r"Generalized Gaussian window (p=1.5, $\sigma$=7)")
plt.ylabel("Amplitude")
plt.xlabel("Sample")

plt.figure()
A = fft(window, 2048) / (len(window) / 2.0)
freq = np.linspace(-0.5, 0.5, len(A))
response = 20 * np.log10(np.abs(fftshift(A / abs(A).max())))
plt.plot(freq, response)
plt.axis([-0.5, 0.5, -120, 0])
plt.title(r"Freq. resp. of the gen. Gaussian " r"window (p=1.5, $\sigma$=7)")
plt.ylabel("Normalized magnitude [dB]")
plt.xlabel("Normalized frequency [cycles per sample]")
Example #40
0
    def run(self):

        self.info("")
        self.info("SAM-FP Tools: Wavelength Calibration")
        self.info("by Bruno Quint ([email protected])")
        self.info("version {:s}".format(version.__str__))
        self.info("Starting program.")
        self.info("")

        self.info("Input filename: {0.input_file:s}")
        self.info("Output filename: {0.output_file:s}")

        # Seistemic wavelength
        try:
            header = pyfits.getheader(self.input_file)
        except IOError:
            self.error("File not found:\n {0.input_file:s}")
            self.error("Leaving program now.")
            sys.exit(1)

        if self.wavelength is None:
            try:
                self.wavelength = header['PHMWCAL']
                self.info("Seistemic wavelength found in the header: "
                          "CRVAL = {0.wavelength:.2f} Angstrom".format(self))
            except KeyError:
                msg = ("No wavelength could be found. "
                       "Leaving program now.")
                self.error(msg)
                sys.exit(1)
        else:
            self.info("Seistemic wavelength providen by the user: "******"CRVAL = {0.wavelength:.2f} Angstrom")


        # Finding the reference pixel
        data = pyfits.getdata(self.input_file)
        s = np.mean(data, axis=2)
        s = np.mean(s, axis=1)

        midpt = np.median(s)
        std = np.std(s)

        self.debug("Collapsed data statistics: ")
        self.debug("median = {:.2f}".format(midpt))
        self.debug("std = {:.2f}".format(std))

        p = np.percentile(data, 50)
        s_ = s.copy()
        s_[s < p] = 0.

        k = signal.general_gaussian(10, 1, 5)
        cc = signal.correlate(s_[5:-5], k, mode="same")
        self.arg_max = np.argmax(cc) + 5

        self.info("Reference channel is: ")
        self.info("CRPIX3 = {0.arg_max:d}")

        # Find the step in angstroms
        try:
            z_fsr = header[self.key_zfsr]
        except KeyError:
            self.warn('%s card was not found in the header.' % self.key_zfsr)
            z_fsr = input('    Please, enter the Free-Spectral-Range in bcv:'
                          '\n    > ')
        except TypeError:
            z_fsr = input('    Please, enter the Free-Spectral-Range in bcv:'
                          '\n    >  ')

        try:
            z_step = header[self.key_z_step]
        except KeyError:
            self.warn('%s card was not found in the header.' % self.key_z_step)
            z_step = input('    Please, enter the step between channels in bcv'
                           '\n     >  ')
        except TypeError:
            self.warn('Header was not passed to "WCal.get_wavelength_step"'
                      ' method')
            z_step = input('    Please, enter the step between channels in bcv'
                           '\n    >  ')

        w_order = 2 * (self.gap_size * 1e-6) / (self.wavelength * 1e-10)

        w_fsr = self.wavelength / (w_order * (1 + 1 / w_order ** 2))

        queensgate_constant = self.wavelength / z_fsr

        w_step = z_step / queensgate_constant

        self.queesgate_constant = queensgate_constant
        self.w_order = w_order
        self.w_fsr = w_fsr
        self.w_step = w_step
        self.z_fsr = z_fsr
        self.z_step = z_step

        self.info('Gap size e = {0.gap_size:.1f} um')
        self.info('Interference order p({0.wavelength:.02f}) = {0.w_order:.2f}')
        self.info(' Z Free-Spectral-Range = {0.z_fsr:.02f} bcv')
        self.info(' W Free-Spectral-Range = {0.w_fsr:.02f} A')
        self.info(' Queesgate constant = {0.queesgate_constant:.02f} bcv / A')
        self.info(' Z Step = {0.z_step:.03f} bcv / channel')
        self.info(' W Step = {0.w_step:.04f} A / channel')

        header.set('CRVAL3', value=self.wavelength,
                   comment='Seistemic wavelength.')
        header.set('CRPIX3', value=self.arg_max)

        header.set('CDELT3', value=self.w_step)
        header.set('C3_3', value=self.w_step)

        header.set('WCAL_W0', value=self.wavelength,
                   comment='Seistemic Wavelength [A]', after='PHMFIT_C')
        header.set('WCAL_DW', value=self.wavelength,
                   comment='Wavelength increment / channel [A]',
                   after='PHMFIT_C')
        header.add_blank('-- sam-fp wavelength calibration --',
                         after='PHMFIT_C')

        pyfits.writeto(self.input_file.replace('.fits', '_wcal.fits'), data,
                       header, overwrite=True)
Example #41
0
    def get_fwhm(z, s, show=False):
        """
        Returns the full-width-at-half-maximum using different models. These
        models can be displayed.

        Parameters
        ----------
            z (array like) : the abscissa of the reference spectrum containing
            the bcv values for each channel or the channel itself.

            s (array like) : the ordinate of the reference spectrum containing
            the intensity at each channel or at each bcv value.

            show (bool, optional) : display plots?

        Returns
        -------
            fwhm (float) : the full-width-at-half-maximum in units equal to z
            (either channel or bcv).
        """

        # Clear data
        p = np.percentile(s, 50.)
        s_ = s.copy()
        s_[s < p] = 0.

        # Find maxima avoiding the borders
        k = signal.general_gaussian(10, 1, 5)
        cc = signal.correlate(s_[5:-5], k, mode="same")
        arg_maxima = np.array([np.argmax(cc)]) + 5

        # arg_maxima = signal.argrelmax(s_[2:-2], order=5)[0]
        _log.debug('Peaks found at: ' + np.array2string(arg_maxima,
                                                        separator=', '))

        fitter = fitting.LevMarLSQFitter()
        g_fwhm, l_fwhm, gauss = [], [], 0

        for (i, argm) in enumerate(arg_maxima):

            g = models.Gaussian1D(amplitude=s_[argm], mean=z[argm], stddev=3.)
            g_fit = fitter(g, z[1:-1], s_[1:-1])
            g_fwhm.append(g_fit.stddev * 2.355)

            l_model = models.Lorentz1D(amplitude=s_[argm], x_0=z[argm], fwhm=3.)
            l_fit = fitter(l_model, z[1:-1], s_[1:-1])
            l_fwhm.append(l_fit.fwhm)
        
            g_rms = np.sqrt(np.mean((s - g_fit(z)) ** 2))
            l_rms = np.sqrt(np.mean((s - l_fit(z)) ** 2))

            _log.info("Peak at {:2d}".format(argm))
            _log.info("gaussian fit rms: {:.2f}".format(g_rms))
            _log.info("lorentzian fit rms: {:.2f}".format(l_rms))

            if g_rms > l_rms:
                gauss += 1
            else:
                gauss -= 1

        g_fwhm = np.mean(g_fwhm)
        l_fwhm = np.mean(l_fwhm)
        _log.info("Gaussian fwhm: {:.2f}".format(g_fwhm))
        _log.info("Lorentzian fwhm: {:.2f}".format(l_fwhm))

        if gauss > 0:
            fwhm_measured = g_fwhm
        elif gauss < 0:
            fwhm_measured = l_fwhm
        else:
            fwhm_measured = (g_fwhm + l_fwhm) * 0.5

        if show:

            z_ = np.linspace(z[0], z[-1], 1000)
            fig, axs = plt.subplots(2, 1, sharex='all')
            axs[0].plot(z, s, 'ko')
            axs[0].plot(z[5:-5], cc / cc.max() * s_.max(), 'y-', label='Cross-correlation')
            axs[0].grid()

            if len(arg_maxima) > 0:

                axs[0].plot(z_, g_fit(z_), 'b-')
                axs[0].plot(z_, l_fit(z_), 'r--')
                axs[0].legend(loc='best')
                axs[0].set_ylabel('Normalized spectrum')

                for amax in arg_maxima:
                    axs[0].axvline(z[amax], c='k', ls='--', alpha=0.5)

                # Display the errors
                axs[1].plot(z, s - g_fit(z), 'bx', alpha=0.5, label='Error - Gaussian Fit')
                axs[1].plot(z, s - l_fit(z), 'ro', alpha=0.5, label='Error - Lorentzian Fit')
                axs[1].set_ylabel('Fir Errors [adu]')
                axs[1].set_xlabel('z [bcv]')
                axs[1].legend(loc='best')
                axs[1].grid()

            plt.tight_layout()
            plt.show()

        return fwhm_measured
Example #42
0
 def __init__(self, freq, spectrum):
     
     self._freq = np.array(freq)
     self._spectrum = np.array(spectrum)
     self._conv_window = spsignal.general_gaussian(10, 1, 3)
Example #43
0
def peak_finder(times, residuals, errors, transit_params, n_peaks=4,
                plots=False, verbose=False, skip_priors=False):
    """
    Find peaks in the residuals from a fit to a transit light curve, which
    correspond to starspot occultations.

    Parameters
    ----------
    times : `numpy.ndarray`
        Times [JD]
    residuals : `numpy.ndarray`
        Fluxes
    errors : `numpy.ndarray`
        Uncertainties on residuals
    transit_params : `~batman.TransitParams`
        Transit light curve parameters
    n_peaks : bool (optional)
        Number of peaks to search for. If more than `n_peaks` are found, return
        only the `n_peaks` largest amplitude peaks.
    plots : bool (optional)
        Show diagnostic plots
    verbose : bool (optional)
        Warn if no peaks are found

    Returns
    -------
    result_in_transit : list or `None`
        List of all spot parameters in [amp, t0, sig, amp, t0, sig, ...] order
        for spots detected.

    Notes
    -----
    Review of minimizers tried for `peak_finder`:

    `~scipy.optimize.fmin` gets amplitudes right, but doesn't vary sigmas much.
    For this reason, it tends to do a better job of finding nearby, semi-
    overlapping spots.

    `~scipy.optimize.fmin_powell` varies amplitudes and sigmas lots, but
    as a result, sometimes two nearby spots are fit with one wide gaussian.
    """
    # http://stackoverflow.com/a/25666951
    # Convolve residuals with a gaussian, find relative maxima
    n_points_kernel = 100
    window = signal.general_gaussian(n_points_kernel+1, p=1, sig=10)
    filtered = signal.fftconvolve(window, residuals)
    filtered = (np.max(residuals) / np.max(filtered)) * filtered
    filtered = np.roll(filtered, int(-n_points_kernel/2))[:len(residuals)]

    maxes = signal.argrelmax(filtered)[0]

    # Only take maxima, not minima
    maxes = maxes[filtered[maxes] > 0]

    lower_t_bound, upper_t_bound = get_in_transit_bounds(times, transit_params)
    maxes_in_transit = maxes[(times[maxes] < upper_t_bound) &
                             (times[maxes] > lower_t_bound)]

    # Only take the `n_peaks` highest peaks
    if len(maxes_in_transit) > n_peaks:
        highest_maxes_in_transit = maxes_in_transit[np.argsort(filtered[maxes_in_transit])][-n_peaks:]
    else:
        highest_maxes_in_transit = maxes_in_transit

    # plt.plot(times, filtered)
    # plt.plot(times, residuals, '.')
    # plt.plot(times[maxes_in_transit], filtered[maxes_in_transit], 'ro')
    # [plt.axvline(times[m], color='k') for m in maxes]
    # [plt.axvline(times[m], color='m') for m in maxes_in_transit]
    # if len(maxes_in_transit) > n_peaks:
    #     [plt.axvline(times[m], color='b') for m in highest_maxes_in_transit]
    # plt.axvline(upper_t_bound, color='r')
    # plt.axvline(lower_t_bound, color='r')
    # plt.show()

    if len(maxes_in_transit) == 0:
        if verbose:
            print('no maxes found')
        return None

    peak_times = times[highest_maxes_in_transit]
    peak_amplitudes = residuals[highest_maxes_in_transit]
    peak_sigmas = np.zeros(len(peak_times)) + 2./60/24  # 3 min
    input_parameters = np.vstack([peak_amplitudes, peak_times,
                                  peak_sigmas]).T.ravel()

    result = optimize.fmin_powell(peak_finder_chi2, input_parameters,
                                  disp=False, args=(times, residuals, errors),
                                  xtol=0.00001, ftol=0.00001)

    # if np.all(result == input_parameters):
    #     print('oh no!, fmin didnt produce a fit')

    # Only use gaussians that occur in transit (fmin fit is unbounded in time)
    # and amplitude is positive:
    split_result = np.split(result, len(input_parameters)/3)
    result_in_transit = []
    for amplitude, t0, sigma in split_result:
        depth = transit_params.rp**2

        trial_params = np.array([amplitude, t0, sigma])
        if not np.isinf(lnprior(trial_params, residuals, lower_t_bound,
                                upper_t_bound, transit_params, skip_priors)):
            result_in_transit.extend([amplitude, t0, np.abs(sigma)])
    result_in_transit = np.array(result_in_transit)

    if len(result_in_transit) == 0:
        return None

    if plots:
        fig, ax = plt.subplots(3, 1, figsize=(8, 10), sharex=True)

        ax[0].errorbar(times, residuals, fmt='.', color='k')
        [ax[0].axvline(t) for t in result_in_transit[1::3]]
        ax[0].plot(times, summed_gaussians(times, input_parameters), 'r')
        ax[0].axhline(0, color='k', ls='--')
        ax[0].set_ylabel('Transit Residuals')

        ax[1].errorbar(times, residuals, fmt='.', color='k')
        ax[1].plot(times, summed_gaussians(times, result_in_transit), 'r')
        ax[1].axhline(0, color='k', ls='--')
        ax[1].set_ylabel('Residuals')

        ax[2].errorbar(times,
                       residuals - summed_gaussians(times, result_in_transit),
                       fmt='.', color='k')
        #ax[1].errorbar(times, gaussian_model, fmt='.', color='r')
        ax[2].axhline(0, color='k', ls='--')
        ax[2].set_ylabel('Residuals')

        for axis in ax:
            axis.axvline(upper_t_bound, color='r')
            axis.axvline(lower_t_bound, color='r')

        fig.tight_layout()
        plt.show()

    return result_in_transit
# Plot the window and its frequency response:

from scipy import signal
from scipy.fftpack import fft, fftshift
import matplotlib.pyplot as plt

window = signal.general_gaussian(51, p=1.5, sig=7)
plt.plot(window)
plt.title(r"Generalized Gaussian window (p=1.5, $\sigma$=7)")
plt.ylabel("Amplitude")
plt.xlabel("Sample")

plt.figure()
A = fft(window, 2048) / (len(window)/2.0)
freq = np.linspace(-0.5, 0.5, len(A))
response = 20 * np.log10(np.abs(fftshift(A / abs(A).max())))
plt.plot(freq, response)
plt.axis([-0.5, 0.5, -120, 0])
plt.title(r"Freq. resp. of the gen. Gaussian window (p=1.5, $\sigma$=7)")
plt.ylabel("Normalized magnitude [dB]")
plt.xlabel("Normalized frequency [cycles per sample]")
def resconvolve(sourceimage, lowres, highres, resultimage = 'same'):
	"""
    Convolve sourceimage to a lower resolution image. 

    Parameters
    ----------
    sourceimage : string 
    	Rath to image to be convolved.
    lowres : float
    	Resolution of the current image (in arcsec).
    highres : float
    	Resolution of the requied convolved image (in arcsec).
    resultimage : string
    	If 'same', then use the same directoy and file name is sourceimage.resamp.fits.
    	If specified then use the given path and file name.	
    	Default is 'same'.

    Returns
    -------
    saves a file : fits
        Stores an 2-D image of sourceimage convolved to the required resolution at resultimage. 
        The header of sourceimage is used.

   	"""
	# Load in source data and header information
	data,header = fits.getdata(sourceimage,header = True)
	# nan to zero
	data[np.isnan(data)] = 0
	# number of x and y pixels
	x = header['NAXIS2']
	y = header['NAXIS1']
	# get the pixelsize in arcsec, where the value is in degree*3600(arcsec/degree)
	pixelsize = header['CDELT2']*3600
	# gaussian consant to convert sigma to FWHM 2*sqrt(2ln(2))
	constant = 2*np.sqrt(2*np.log(2))
	# FWHM of current image 
	FWHM_high = highres/pixelsize
	# sigma of current image
	sigma_high = FWHM_high/constant
	# FWHM of required resolution
	FWHM_low = lowres/pixelsize
	# sigma of required resolution
	sigma_low = FWHM_low/constant
	# FWHM calulated for the gaussian of convolution kernel
	FWHM = np.sqrt(FWHM_low**2 - FWHM_high**2)
	# sigma for the gaussian of convolution kernel
	sigma = FWHM/constant
	# making the 2-D image of the convolution kernel by making 2, 1-D gaussian
	# and normalized by the gaussian normalization factor
	gauss1 = signal.general_gaussian(x,1, sigma)/((sigma)*np.sqrt(2*np.pi))
	gauss2 = signal.general_gaussian(y,1, sigma)/((sigma)*np.sqrt(2*np.pi))
	# combine these two to create 2D image
	kernel = np.outer(gauss1, gauss2)
	# convolve the image using signal.fftconvolve premade function and kernel
	convolved = signal.fftconvolve(data, kernel, mode='same')

	# change the resolution if it's there
	header['RESO'] = (lowres, '[arcsec] Resolution of the map')

	# name of the path based on the resultimage
	if resultimage == 'same':
		path = sourceimage[:-5]+'.conv.fits'
	else:
		path = resultimage
		      
	# saves as the fits file	
	fits.writeto(path, convolved, header)

	print "Convolving done. File is saved. \n"

	return