def denoise(self, vert_win=1, hor_win=10, noise=None, ftype='wiener'): """ Denoising filter For now this just uses the scipy wiener filter, We could experiment with other options though Parameters --------- vert_win: int; optional vertical window size hor_win: int; optional horizontal window size noise: float; optional power of noise reduction, default is the average of the local variance of the image ftype: string; optional filter type """ if ftype == 'wiener': if noise is None: self.data = wiener(self.data, mysize=(vert_win, hor_win)) else: self.data = wiener(self.data, mysize=(vert_win, hor_win), noise=noise) else: raise TypeError( 'Only the wiener filter has been implemented for denoising.')
def denoise(self, vert_win=1, hor_win=10, noise=None, ftype='wiener'): """ Denoising filter For now this just uses the scipy wiener filter, We could experiment with other options though Parameters --------- vert_win: int; optional vertical window size hor_win: int; optional horizontal window size noise: float; optional power of noise reduction, default is the average of the local variance of the image ftype: string; optional filter type Raises ------ """ if ftype == 'wiener': if noise is None: # We want an error if there is no variance with np.errstate(divide='raise'): try: self.data = wiener(self.data, mysize=(vert_win, hor_win)) except FloatingPointError: raise ValueError('Could not compute variance, specify noise for denoise') else: self.data = wiener(self.data, mysize=(vert_win, hor_win), noise=noise) else: raise ValueError('Only the wiener filter has been implemented for denoising.')
def smooth_data(X, smoother="savgol", window_length=35): """Apply smoothing to data.""" # Axis along which to smooth axis = np.ndim(X) - 1 # Check that window_length was appropriately set if window_length > X.shape[axis]: print("Window length {} larger than size of array {}.".format( window_length, X.shape[axis])) window_length = X.shape[axis] if np.mod(window_length, 2) == 0: window_length -= 1 print("Shrinking window to size {}.".format(window_length)) if smoother == "savgol": if window_length <= 3: raise ValueError( "window_length must be larger than 3, currently set to {}". format(window_length)) return signal.savgol_filter(X, window_length, polyorder=3, axis=axis) if smoother == "median": if axis == 0: return signal.medfilt(X, kernel_size=window_length) else: return np.vstack( [signal.medfilt(x, kernel_size=window_length) for x in X]) if smoother == "wiener": if axis == 0: return signal.wiener(X, mysize=window_length) else: return np.vstack( [signal.wiener(x, mysize=window_length) for x in X])
def get_mother_cell_growth_props(self, mother_data_frame, lower_threshold=0.35, upper_threshold=0.75): """Get growth properties for a mother cell. Args: mother_data_frame (pandas.Dataframe): Segmentation data of a mother cell lower_threshold (float): Trench loading lower threshold upper_threshold (float): Trench loading upper threshold Returns: doubling_time (numpy.ndarray): Doubling times data array for the trench, containing times, doubling times, and interval to previous doubling time doubling_time_smoothed (numpy.ndarray): Smoothed doubling times data array for the trench, containing times, doubling times, and interval to previous doubling time growth_rate_data (numpy.ndarray): Growth ratedata array for the trench, containing growth rates for length and area (smoothed and raw) """ # Get loading fractions loading_fractions = np.array(mother_data_frame["trench_loadings"]) # Check whether trench unloads cutoff_index = len(loading_fractions) times_within_thresholds = (loading_fractions > lower_threshold)*(loading_fractions < upper_threshold) times_outside_thresholds = ~times_within_thresholds # Throw out trenches once you see a run of more than 5 timepoints with loading outside the loading thresholds if cutoff_index < 5: return None, None, None for i in range(min(len(loading_fractions)-4, self.timepoint_full_trenches), len(loading_fractions)-4): if np.all(times_outside_thresholds[i:i+5]): cutoff_index = i break if cutoff_index < 5: return None, None, None # extract data from dataframes times = np.array(mother_data_frame["time_s"])[:cutoff_index] major_axis_length = np.array(mother_data_frame["major_axis_length"])[:cutoff_index] minor_axis_length = np.array(mother_data_frame["minor_axis_length"])[:cutoff_index] solidity = np.array(mother_data_frame["solidity"])[:cutoff_index] area = np.array(mother_data_frame["area"])[:cutoff_index] # Interpolate individual points where trench loading is messed up, # indicative of drift or bad segmentation repaired_data = self.repair_trench_loadings(np.array([major_axis_length, area, minor_axis_length, solidity]).T, times_outside_thresholds[:cutoff_index]) major_axis_length = repaired_data[:,0].flatten() area = repaired_data[:,1].flatten() minor_axis_length = repaired_data[:,2].flatten() solidity = repaired_data[:,3].flatten() # Smooth with Wiener filter (minimum mean squared error) mal_smoothed = signal.wiener(major_axis_length) area_smoothed = signal.wiener(area) doubling_time = self.get_doubling_times(times, major_axis_length, relative_threshold=1.5) doubling_time_smoothed = self.get_doubling_times(times, mal_smoothed, relative_threshold=1.5) # Calculate growth rate as a gradient instantaneous_growth_rate_length = np.gradient(major_axis_length, times) instantaneous_growth_rate_area = np.gradient(area, times) instantaneous_growth_rate_length_smoothed = np.gradient(mal_smoothed, times) instantaneous_growth_rate_area_smoothed = np.gradient(area_smoothed, times) growth_rate_data = np.array([times, major_axis_length, mal_smoothed, instantaneous_growth_rate_length, instantaneous_growth_rate_length_smoothed, instantaneous_growth_rate_area, instantaneous_growth_rate_area_smoothed, minor_axis_length, solidity]).T growth_rate_data = np.concatenate([growth_rate_data, growth_rate_data[:,3:5]/major_axis_length[:, None], growth_rate_data[:,5:7]/area[:, None]], axis=1) return doubling_time, doubling_time_smoothed, growth_rate_data
def wiener_new(data, window): ''' Apply the Wiener's filter to a positional data set. Args: data (numpy array): containing all of the positional data in the format of (time, x, y, z) window (int): window size of the Wiener filter Returns: numpy array: filtered data in the same format ''' x = data[:, 1] y = data[:, 2] z = data[:, 3] x_new = wiener(x, window) y_new = wiener(y, window) z_new = wiener(z, window) new_positions = np.zeros((len(data), 4)) new_positions[:, 1] = x_new new_positions[:, 2] = y_new new_positions[:, 3] = z_new new_positions[:, 0] = data[:, 0] return new_positions
def process(self, generated: np.ndarray, recorded: np.ndarray): w1 = 10 w2 = self.sample_rate / 2.0 sweep_smplen = int(generated.size) k_end = 10**((-6 * np.log2(w2 / w1)) / 20) k = np.log(k_end) / sweep_smplen attenuation = np.exp(np.arange(sweep_smplen) * k) divisor = generated[-1::-1] * attenuation if len(np.shape(recorded)) == 1: impulse_out = sig.wiener(sig.fftconvolve(recorded, divisor))[sweep_smplen:] sample_max = np.max(np.abs(impulse_out)) return np.float32(impulse_out) / sample_max elif len(np.shape(recorded)) == 2: reshaped_record = np.array(list(zip(*recorded))) impulse_out = list() for x in reshaped_record: impulse_out.append( sig.wiener(sig.fftconvolve(x, divisor))[sweep_smplen:]) sample_max = np.max(np.abs(impulse_out)) return np.float32(np.array(list(zip(*impulse_out))) / sample_max) else: raise ValueError( f"Recorded signal has a weird shape {np.shape(recorded)}")
def generateChc(L, H): L1 = wiener(L, [5, 5]) H1 = wiener(H, [5, 5]) x_ax = (L1 + H1) / 2 y_ax = H1 - L1 y_s = 0.17213 * (x_ax**3) - 1.399 * (x_ax**2) + 1.2392 * x_ax - 0.0027535 y_p = 0.228 * (x_ax**3) - 0.51622 * (x_ax**2) + 0.30413 * x_ax + 0.0053274 y_al = -0.409873 * (x_ax**4) + 0.90975 * (x_ax**3) - 1.298 * ( x_ax**2) + 0.81073 * x_ax + 0.0018109 y_a = (y_p + y_al) / 2 y_b = (y_al + y_s) / 2 choice_v1 = np.zeros(y_ax.shape) choice_v1[y_ax > y_b] = 1 choice_v1[choice_v1 == 0] = 2 choice_v1[y_ax < y_a] = 3 a = L1 b = H1 c = np.log(a) d = np.log(b) q = c / d choice_v1[(q < 1.17) & (H < 0.19) & (L < 0.16)] = 4 choice_v1[(q < 1.24) & (H < 0.42) & (L < 0.3)] = 4 choice_v1[(x_ax < 0.06) & (y_ax < 0.06)] = 1 return (choice_v1 - 1).astype(np.uint8)
def generateChc(self): L1 = wiener(self.L, [5, 5]) H1 = wiener(self.H, [5, 5]) x_ax = (L1 + H1) / 2 y_ax = H1 - L1 y_s = 0.17213 * (x_ax**3) - 1.399 * (x_ax** 2) + 1.2392 * x_ax - 0.0027535 y_p = 0.228 * (x_ax**3) - 0.51622 * (x_ax** 2) + 0.30413 * x_ax + 0.0053274 y_al = -0.409873 * (x_ax**4) + 0.90975 * (x_ax**3) - 1.298 * ( x_ax**2) + 0.81073 * x_ax + 0.0018109 y_a = (y_p + y_al) / 2 y_b = (y_al + y_s) / 2 self.choice = np.zeros(y_ax.shape) self.choice[y_ax > y_b] = 1 self.choice[self.choice == 0] = 2 self.choice[y_ax < y_a] = 3 a = L1 b = H1 c = np.log(a) d = np.log(b) q = c / d self.choice[(q < 1.17) & (self.H < 0.19) & (self.L < 0.16)] = 4 self.choice[(q < 1.24) & (self.H < 0.42) & (self.L < 0.3)] = 4 self.choice[(x_ax < 0.06) & (y_ax < 0.06)] = 1 self.choice = (self.choice - 1).astype(np.uint8)
def get_processed_image_clip(start_date="2017-10-01", num_days=31, end_date=None, **kwargs): ''' Intake a date range of photo records and then generate the enhanced resulted single image param: start date type:string param: num_days type:int param: end_date type:string output: np.array ''' l = getimage.get_image_date_range(start_date, num_days, end_date, **kwargs) w, h = l[0].shape arr = zeros((h, w), float) N = len(l) for im in l: imarr = array(im, dtype=float) arr = arr + imarr / N out = matrix.round(arr) out *= 255.0 / out.max() out = signal.wiener(out, 5) avg = 60 out[(out >= 0.9 * avg) & (out <= 1.7 * avg)] *= 0.5 filtered = signal.wiener(out, 5) return filtered
def get_processed_image_band_reject(start_date="2017-10-01", num_days=31, end_date=None, **kwargs): ''' Intake a date range of photo records and then generate the enhanced resulted single image param: start date type:string param: num_days type:int param: end_date type:string output: np.array ''' l = getimage.get_image_date_range(start_date, num_days, end_date, **kwargs) w, h = l[0].shape arr = zeros((h, w), float) N = len(l) for im in l: imarr = array(im, dtype=float) arr = arr + imarr / N out = matrix.round(arr) out *= 255.0 / out.max() out = signal.wiener(out, 5) avg = 60.0 bandwidth = 40 reject_ratio = 0.4 myFunc = np.vectorize(lambda x: x * (((avg**2 - x**2)**2 / ( (2 * bandwidth)**2 * x**2 + (avg**2 - x**2)**2))**0.5 * reject_ratio + (1 - reject_ratio))) out = myFunc(out) filtered = signal.wiener(out, 5) return filtered
def test_basic(self): g = array([[5,6,4,3],[3,5,6,2],[2,3,5,6],[1,6,9,7]],'d') h = array([[2.16374269,3.2222222222, 2.8888888889, 1.6666666667], [2.666666667, 4.33333333333, 4.44444444444, 2.8888888888], [2.222222222, 4.4444444444, 5.4444444444, 4.801066874837], [1.33333333333, 3.92735042735, 6.0712560386, 5.0404040404]]) assert_array_almost_equal(signal.wiener(g), h, decimal=6) assert_array_almost_equal(signal.wiener(g, mysize=3), h, decimal=6)
def cplxwiener_filter(real_input, imag_input, mysize_=5, noise_=None): """cplxwiener_filter Implementation of Wiener filter on complex image scipy.signal.wiener(im, mysize=None, noise=None)[source] Perform a Wiener filter on an N-dimensional array. The Wiener filter is a simple deblurring filter for denoising images. This is not the Wiener filter commonly described in image reconstruction problems but instead it is a simple, local-mean filter. Apply a Wiener filter to the N-dimensional array im. Parameters: im : ndarray An N-dimensional array. mysize : int or arraylike, optional A scalar or an N-length list giving the size of the Wiener filter window in each dimension. Elements of mysize should be odd. If mysize is a scalar, then this scalar is used as the size in each dimension. noise : float, optional The noise-power to use. If None, then noise is estimated as the average of the local variance of the input. Returns: out : ndarray Wiener filtered result with the same shape as im. """ # scipy.signal.wiener(im, mysize=None, noise=None) # ,(size_, size_, size_) filtersiz = (3, 3, 3) print "Complex Wiener filter window size ", mysize_, " noise ", noise_ if not noise_: noise_ = ndimage.standard_deviation(real_input) if not hasattr(mysize_, "__len__"): filtersize = np.array(mysize_) else: filtersize = mysize_ if real_input.ndim == 3: real_img = wiener(real_input, mysize=filtersize, noise=noise_) imag_img = wiener(imag_input, mysize=filtersize, noise=noise_) else: real_img = np.empty_like(real_input) imag_img = np.empty_like(real_input) for echo in xrange(0, real_input.shape[4]): for acq in xrange(0, real_input.shape[3]): real_img[:, :, :, acq, echo] = wiener(real_input[:, :, :, acq, echo], mysize=filtersize, noise=noise_) imag_img[:, :, :, acq, echo] = wiener(imag_input[:, :, :, acq, echo], mysize=filtersize, noise=noise_) filtered_image = np.empty_like(real_input, dtype=np.complex64) filtered_image.real = real_img filtered_image.imag = imag_img return filtered_image
def perform_abel_discontinuous(x, data): if data.ndim == 1: data = np.atleast_2d(data) ny, nx = data.shape dx = x[1] - x[0] r = x[nx / 2:nx - 1] nr = len(r) smoothed = np.zeros((ny, nx)) deriv = np.zeros((ny, nx)) f_abel = np.zeros((ny, nr)) sigma_deriv = 2 for i in range(0, ny): w_discont = int(np.where( data[i, nx / 2:] != 0)[0][0]) # what happens if measured phase is zero? # unlikely to have exact value... if w_discont == 0: smoothed[i, :] = signal.wiener(data[i, :], mysize=10) deriv[i, :] = ifft( fft(smoothed[i, :]) * fft(periodic_gaussian_deriv(x, sigma_deriv * dx))).real tmp = abel(x, deriv[i, :]) f_abel[i, :] = tmp[0] else: # avoid performing operations over discontinuity left = range(0, int(nx / 2 - w_discont + 1)) right = range(int(nx / 2 + w_discont), nx) smoothed[i, left] = signal.wiener(data[i, left], mysize=10) smoothed[i, right] = signal.wiener(data[i, right], mysize=10) deriv[i, :] = ifft( fft(smoothed[i, :]) * fft(periodic_gaussian_deriv(x, sigma_deriv * dx))).real #deriv[i, left] = ifft(fft(smoothed[i,left]) * fft(periodic_gaussian_deriv(x[left], sigma_deriv*dx))).real #deriv[i, right] = ifft(fft(smoothed[i,right]) * fft(periodic_gaussian_deriv(x[right], sigma_deriv*dx))).real tmp = abel(x, deriv[i, :]) f_abel[i, :] = tmp[0] f_abel[i, 0:w_discont + 1] = 0 return (r, f_abel, deriv, smoothed)
def __init__(self, data): data = ast.literal_eval(data) # Apply wiener filter to the data self.x = signal.wiener([ float(x) for x in data["dataX"] ]) self.y = signal.wiener([ float(y) for y in data["dataY"]]) self.z = signal.wiener([ float(z) for z in data["dataZ"]]) t = data["timestamps"] # set a message if one is included try: self.msg = data['msg'] except KeyError: self.msg = False #convert timestamps into deltas self.t = [(int(x)-int(t[0]))*10**-9 for x in t]
def reduce_noise(audio, method="wiener"): """ Apply a certain method to reduce audio noise. Args: audio (numpy.array): the audio data as 1D numpy array Returns: numpy.array of the audio after reducing the noise NOTE: wiener is faster than logmmse """ methods = ["logmmse", "wiener"] assert method in methods, \ "noise-reduction method must be one of these " + str(methods) output = np.array([], dtype=np.float32) if method == "logmmse": sr = 16000 #sample rate # you can tune the following values initial_noise, window_size, noise_threshold = 6, 0, 0.15 # number of frames per second chunk_size = 60*sr total_frames = audio.shape[0] frames_read = 0 # iterate 1 second at a time while frames_read < total_frames: if frames_read + chunk_size > total_frames: frames = total_frames - frames_read else: frames = chunk_size audio_subsample = audio[frames_read:frames_read + frames] frames_read = frames_read + frames _output, _ = logmmse(audio_subsample, sr, initial_noise, window_size, noise_threshold) output = np.concatenate( (output, _output) ) elif method == "wiener": output = wiener(audio) return output
def getDisparitySet(self, filtereddata): curdisparityset = np.array([]) for items in filtereddata: item = np.array(items[8:], dtype=float) lefteye = item[0:3] righteye = item[3:6] leftgaze = item[6:9] rightgaze = item[9:12] disparityangular = self.calculateDisparityAngular(lefteye, righteye, leftgaze, rightgaze) curItem = np.array([items[1], disparityangular]) if curdisparityset.size == 0: curdisparityset = curItem else: curdisparityset = np.vstack((curdisparityset, curItem)) disparityset = sg.wiener(np.array(curdisparityset[:, 1], dtype=float)) dispritysettmp = np.array([]) for i in range(len(curdisparityset)): curitem = np.array([curdisparityset[i, 0], disparityset[i]]) if dispritysettmp.size == 0: dispritysettmp = curitem else: dispritysettmp = np.vstack((dispritysettmp, curitem)) disparityset = dispritysettmp # plt.plot(map(float,curdisparityset[:,1])) # plt.xlabel(u'采样点',fontsize = 18) # plt.ylabel(u'视差角(rad)',fontsize = 18) # #plt.plot(disparityset) # plt.show() return disparityset
def _smooth_columns(self, maturation_paths): pd.options.mode.chained_assignment = None # default='warn' smoothed_maturation_paths = maturation_paths.copy() for col in maturation_paths: temp = wiener(maturation_paths[col]) smoothed_maturation_paths.loc[:, col] = temp return smoothed_maturation_paths
def wiener(input, output="wiener_output.wav", plot=False): """ Apply a Wiener filter to an input WAV file """ (nChannels, sampWidth, sampleRate, nFrames, compType, compName) = input.getparams() # extract audio from wav file input_signal = input.readframes(-1) input_signal = np.fromstring(input_signal, 'Int16') print(input_signal) input.close() # Wiener filter design dim = 10 output_signal = signal.wiener(input_signal, noise=80) if plot: # Plot input and output signals t = np.linspace(0, nFrames / sampWidth, nFrames, endpoint=False) plt.plot(t, input_signal, label='Input') plt.plot(t, output_signal, label='Output') plt.show() scaled = np.int16(output_signal / np.max(np.abs(output_signal)) * 32767) write(output, sampleRate, scaled)
def segmentlocal(img, filter_size=5, level=0.2, selem_size=3, rescale_low=(0, 50), rescale_high=(50, 100)): """ Segment image using gradients calculated locally. Apply a Wiener filter to remove salt-and-pepper noise, then calculate gradients over local neighborhoods using the specified structuring element. Threshold the image constructed my multiplying the rescaled original image with its rescaled derivative (this helps define the edges). args: img (ndarray): input image kwargs: filter_size (int): neighborhood size of Wiener filter selem_size (int): size of the disk structuring element level (float): threshold level, specified as a fraction of the calculated Otsu threshold; must by in the range [0, 1] rescale_low (tuple): (low, high) values used to rescale original image rescale_high (tuple): (low, high) values used to rescale edges image returns: ndarray: thresholded binary image (dtype = bool) """ img2 = wiener(img, (filter_size, filter_size)) img3 = median_filter(img2, filter_size) img4 = complement(rescale(img3, rescale_low)) img5 = bottomhat(img_as_uint12(img3), disk(selem_size)) img6 = fillholes(img5) img7 = rescale(img6, rescale_high) img8 = img4 * img7 return threshold(img8, level=level)
def ProcessEvent(self, S): amps = [] noise_w = np.array([self.J[a][a] for a in range(len(S))]) amps = [ sum(wiener(S[a], mysize=75, noise=noise_w[a].real)) for a in range(len(S)) ] E = sum(amps) if E < self.E_min or E > self.E_max: return False r = self.calc_r(amps) theta = get_angle_std(self.calc_theta(amps)) for i in range(len(self.map_bins_part)): if r > self.map_bins_part[i][0] and r < self.map_bins_part[i][1]: for j in range(len(self.map_bins_part[i][2])): if check_angle(theta, self.map_bins_part[i][2][j][0], self.map_bins_part[i][2][j][1]): self.last_of_index = self.map_bins_part[i][2][j][2] if self.last_of_index > -1: return self.list_of[self.last_of_index].Execute(S) else: return None self.last_of_index = -1 return None
def segmentglobal(img, filter_size=5, level=0.7): """ Segment image using gradients calculated globally. Apply a Wiener filter to remove salt-and-pepper noise and a median filter to smooth edges, then calculate gradients across the entire image (between adjacent pixels in both directions). Threshold the image constructed my multiplying the original image with its derivative (this helps define the edges). args: img (ndarray): input image kwargs: filter_size (int): neighborhood size of Wiener and median filters level (float): threshold level, specified as a fraction of the calculated Otsu threshold; must by in the range [0, 1] returns: ndarray: thresholded binary image (dtype = bool) """ img2 = complement(img, 1.) img3 = wiener(img2, (filter_size, filter_size)) img4 = median_filter(img3, filter_size) img5 = energy(img4) img6 = (1 + img5) * img4 return threshold(img6, level=level).astype(bool)
def extract(outname,root,slit,pos,width=1.): """ extract(outname,root,slit,pos,width=1.) Extracts a spectrum from 2d mask and variance images. Inputs: outname - name of output FITS file root - root name of input data (ie ROOTNAME_bgsub.fits) slit - number of slit to extract from (1 = bottom slit) pos - position along slit to extract width - gaussian-sigma width to extract Outputs: FITS file containing extracted spectrum. """ infile = root+"_bgsub.fits" spectools.cutout(infile,outname,slit) wave = spectools.wavelength(outname) data = pyfits.open(outname)[0].data.astype(scipy.float32) os.remove(outname) infile = root+"_var.fits" spectools.cutout(infile,outname,slit) varimg = pyfits.open(outname)[0].data.astype(scipy.float32) os.remove(outname) yvals = scipy.arange(data.shape[0]).astype(scipy.float32) fit = scipy.array([0.,1.,pos,width]) weight = sf.ngauss(yvals,fit) weight[abs(yvals-pos)/width>1.5] = 0. weight /= weight.sum() spec = weight*data.T spec = spec.sum(axis=1) varspec = weight*varimg.T varspec = varspec.sum(axis=1) spec[varspec==0] = 0. smooth = signal.wiener(spec,7,varspec) smooth[scipy.isnan(smooth)] = 0. hdu = pyfits.PrimaryHDU() hdu.header.update('CENTER',pos) hdu.header.update('WIDTH',width) hdulist = pyfits.HDUList([hdu]) crval = wave[0] scale = wave[1]-wave[0] for i in [spec,smooth,varspec]: thdu = pyfits.ImageHDU(i) thdu.header.update('CRVAL1',crval) thdu.header.update('CD1_1',scale) thdu.header.update('CRPIX1',1) thdu.header.update('CRVAL2',1) thdu.header.update('CD2_2',1) thdu.header.update('CRPIX2',1) thdu.header.update('CTYPE1','LINEAR') hdulist.append(thdu) hdulist.writeto(outname)
def apply_wiener_filter(data, ich, n=16384): x = [data[ich * n + i] for i in range(n)] m = np.mean(x) # y = wiener([a-m for a in x], mysize=500) y = wiener([a - m for a in x], mysize=500, noise=0.0003) for i in range(n): data[ich * n + i] = y[i] + m
def testWiener(x, y, s, npts): sigma2 = None #np.var(y) print sigma2 wi = wiener(y, mysize=29, noise=sigma2) plt.plot(x,wi,'g') print "wieerr", ssqe(wi, s, npts) return wi
def slidingWindow(P,inX=3,outX=32,inY=3,outY=64,maxM=50,norm=True): """ Enhance the constrast Cut off extreme values and demean the image Utilize scipy convolve2d to get the mean at a given pixel Remove local mean with inner exclusion region Args: P: 2-d numpy array image inX: inner exclusion region in the x-dimension outX: length of the window in the x-dimension inY: inner exclusion region in the y-dimension outY: length of the window in the y-dimension maxM: size of the output image in the y-dimension norm: boolean to cut off extreme values Returns: Q: 2-d numpy contrast enhanced """ Q = P.copy() m, n = Q.shape Q = exposure.equalize_hist(Q.astype('Float32'), nbins = 65) Q = detrend(Q.astype('Float32'), axis = 1) Q = detrend(Q.astype('Float32'), axis = 0) Q = wiener(Q.astype('Float32'), 4) return Q[:maxM,:]
def wise_psf_stamp(band): # psf noise: ~roughly 0.1 count in outskirts of W1 and W2 if band >= 3: raise ValueError('Need to stare at W3+ PSF more!') if os.getenv('WISE_PSF_DIR', None) is None: raise ValueError('WISE_PSF_DIR must be set.') psfnoise = 0.1 stamp = fits.getdata( os.path.join(os.getenv('WISE_PSF_DIR'), 'psf_model_w' + str(band) + '.fits')) edges = numpy.concatenate( [stamp[0, 1:-1], stamp[-1, 1:-1], stamp[1:-1, 0], stamp[1:-1, -1]]) medval = numpy.median(edges[edges != 0]) / 2 stamp[stamp == 0] = medval stamp -= medval from scipy import signal stamp[stamp < 0] = 0. # suppress spurious warnings in signal.wiener olderr = numpy.seterr(invalid='ignore', divide='ignore') stamp = signal.wiener(stamp, 11, psfnoise) numpy.seterr(**olderr) # taper linearly over outer 60 pixels? stampszo2 = stamp.shape[0] // 2 xx, yy = numpy.mgrid[-stampszo2:stampszo2 + 1, -stampszo2:stampszo2 + 1] edgedist = numpy.clip(stampszo2 - numpy.abs(xx), 0, stampszo2 - numpy.abs(yy)) stamp = stamp * numpy.clip(edgedist / 60., stamp < 10, 1) import psf stamp = psf.center_psf(stamp, censize=19) stamp = stamp / numpy.sum(stamp) return stamp
def _smooth_columns(self,maturation_paths): pd.options.mode.chained_assignment = None # default='warn' smoothed_maturation_paths=maturation_paths.copy() for col in maturation_paths: temp = wiener(maturation_paths[col]) smoothed_maturation_paths.loc[:,col]=temp return smoothed_maturation_paths
def smooth_by_linear_filter(data, win_size, win_type='rectangular'): # half_win = int(math.floor(win_size/2)) # total_win = half_win*2+1 total_win, half_win = actual_win_size(win_size) if win_type == 'gaussian': data_smoothed = gaussian_filter(data, total_win) return data_smoothed if win_type == 'wiener': data_smoothed = wiener(data, total_win) return data_smoothed if win_type == 'rectangular': weights = np.ones(total_win) / total_win elif win_type == 'hamming': weights = np.hamming(total_win) / np.sum(np.hamming(total_win)) data_smoothed = [] for i in range(len(data)): end = i + half_win if i + half_win >= len(data): break if i - half_win < 0: start = 0 data_smoothed.append(np.mean(data[start:end + 1])) else: start = i - half_win data_smoothed.append(weights.dot(data[start:end + 1].T)) return np.array(data_smoothed)
def main(): t = np.linspace(-1, 1, 100) def f(t): return np.sin( np.pi * t) + 0.1 * np.cos(7 * np.pi * t + 0.3) + 0.2 * np.cos( 24 * np.pi * t) + 0.3 * np.cos(12 * np.pi * t + 0.5) fig, axes = plt.subplots(1, 3, figsize=(12, 3)) axes[0].plot(t, signal.gausspulse(t, fc=5, bw=0.5)) axes[0].set_title("gausspulse") t *= 5 * np.pi axes[1].plot(t, signal.sawtooth(t)) axes[1].set_title("chirp") axes[2].plot(t, signal.square(t)) axes[2].set_title("gausspulse") t = np.linspace(0, 4, 400) plt.plot(t, f(t)) # 中值滤波函数 plt.plot(t, signal.medfilt(f(t), kernel_size=55), linewidth=2, label="medfilt") # 维纳滤波函数 plt.plot(t, signal.wiener(f(t), mysize=55), linewidth=2, label="wiener") plt.legend() plt.show()
def wiener_filter(time): rv_err = data[:,2] # Compute variance from given RV errors variance = sum(x*x for x in rv_err) / 228 # N = 228 y_wiener = wiener(data[:,1], 600, variance) # wiener(im, box_size, noise) # Using Wiener filter plt.figure(1) plt.subplot(211) plt.plot(time, y, 'o', color = 'b', label = 'Raw') plt.plot(time, y, color = 'b') # raw RV data plt.plot(time, y_wiener, 'o', color = 'r', label = 'Wiener') # smooth RV data plt.legend(loc='lower right', numpoints = 1) plt.title('Tau Ceti: Wiener Filter') plt.xlabel('JD - 2.44e6') plt.ylabel('RV (m/s)') # Using default smooth data plt.subplot(212) plt.plot(time, y, 'o', color = 'b', label = 'Raw') plt.plot(time, y, color = 'b') # raw RV data plt.plot(time, y_smooth, 'o', color = 'r', label = 'Default Smooth') # smooth RV data plt.legend(loc='lower right', numpoints = 1) plt.xlabel('JD - 2.44e6') plt.ylabel('RV (m/s)') plt.show()
def test_wiener(self, num_samps): cpu_sig = np.random.rand(num_samps) gpu_sig = cp.asarray(cpu_sig) cpu_wfilt = signal.wiener(cpu_sig) gpu_wfilt = cp.asnumpy(cusignal.wiener(gpu_sig)) assert array_equal(cpu_wfilt, gpu_wfilt)
def getDisparitySet(self,filtereddata): curdisparityset = np.array([]) for items in filtereddata: item = np.array(items[8:],dtype=float) lefteye = item[0:3] righteye = item[3:6] leftgaze = item[6:9] rightgaze = item[9:12] disparityangular = self.calculateDisparityAngular(lefteye,righteye,leftgaze,rightgaze) curItem = np.array([items[1],disparityangular]) if curdisparityset.size == 0: curdisparityset = curItem else: curdisparityset = np.vstack((curdisparityset,curItem)) disparityset = sg.wiener(np.array(curdisparityset[:,1],dtype=float)) dispritysettmp = np.array([]) for i in range(len(curdisparityset)): curitem = np.array([curdisparityset[i,0],disparityset[i]]) if dispritysettmp.size ==0: dispritysettmp = curitem else: dispritysettmp = np.vstack((dispritysettmp,curitem)) disparityset = dispritysettmp # plt.plot(map(float,curdisparityset[:,1])) # plt.xlabel(u'采样点',fontsize = 18) # plt.ylabel(u'视差角(rad)',fontsize = 18) # #plt.plot(disparityset) # plt.show() return disparityset
def doccl(data, centroids, fn, P): # setup mask (for results) maskdata = zeros_like(data) #%% (1) denoise if 'aparam' in P and 'wiener' in P['aparam']: filtered = wiener(data, [5, 5]) #TODO puts in negative values else: filtered = data #%% (2) threshold (Otsu) thres = dothres(data, filtered, maskdata, fn, P) #%% (3) morphological ops morphed = domorph(data, thres, maskdata, fn, P) #%% (4) connected component labeling if centroids is None: centroids, nlbl, badind = dolabel(morphed) else: nlbl = centroids.size badind = [] #%% (5) property analysis (centroid extraction) centroid_sum = dosum(filtered, centroids, nlbl, badind, fn, P) #print('{:0.2f} sec. to compute {} centroids in {}'.format(time()-tic,nlbl,fn)) return centroid_sum, centroids
def sigmaVV(dataset, xOff=0, yOff=0, xS=None, yS=None, \ xBufScale=None, yBufScale=None, s='abs', filter_name='wiener', ws=7, que=[]): # calculate the sinclair matrix S_VV = dataset.GetRasterBand(2).ReadAsArray(xoff=xOff, yoff=yOff, \ win_xsize=xS, win_ysize=yS, \ buf_xsize=xBufScale, buf_ysize=yBufScale) # calculate the magnitude S_VV_ABS = absolute(S_VV) # calculate the linear sigma_naught if s == 'abs': SigmaVV = S_VV_ABS**2 # calculate the sigma_naught in dB if s == 'sigma0': SigmaVV = 2*10*log10(S_VV_ABS) # SigmaVVwnr = wiener(SigmaVV,mysize=(7,7),noise=None) #we're putting return value into queue que.put(SigmaVV) #Filter the image using 'median' or Lee 'wiener' filtering methods if filter_name == 'median': # filter the image using median filter SigmaVVmed = medfilt2d(SigmaHH, kernel_size=ws) que.put(SigmaVVmed) elif filter_name == 'wiener': # filter the image using wiener filter SigmaVVwnr = wiener(SigmaVV,mysize=(ws,ws),noise=None) que.put(SigmaVVwnr) else: print 'Please specify the name of the filter: "median" or "wiener"'
def Wiener(y, n): wi = sig.wiener(y, mysize=n) if sum(np.isnan(wi)) > 0: return y return wi
def writeMorseString(ffttimeseries, freq): #starts by defining long/short highCounter = 0 spaceCounter = 0 spaceDefinition = 10000 isHigh = 0 lowest = 10000 shortSig = lowest #initial definition of a short signal longSig = lowest*2 #initial definition of a long signal fftsum = 0 for i in range(0, len(ffttimeseries)): fftsum+=ffttimeseries[i][freq] fftavg = fftsum/len(ffttimeseries) print "fftavg: ", fftavg filtered_fft = wiener(ffttimeseries[:,freq], 3) peakArray = [] for i in range(0, len(ffttimeseries)): peakArray.append(isPeak(filtered_fft[i], freq, fftavg)) for i in range(0, int(1*len(ffttimeseries))): if(peakArray[i]): highCounter +=1 isHigh = 1 if(spaceCounter!=0 and spaceCounter*2 < spaceDefinition): spaceDefinition = 3*spaceCounter print "space: ", spaceDefinition, " ", i spaceCounter = 0 elif(isHigh): if(highCounter<lowest): lowest=highCounter print lowest, " ", i highCounter = 0 isHigh = 0 if(not isHigh): spaceCounter +=1 shortSig = lowest #initial definition of a short signal longSig = lowest*2 #initial definition of a long signal print "Short sig is: ", shortSig, " Long sig is: ", longSig, "space is: ", spaceDefinition morsestring = "" highCounter = 0 spaceCounter = 0 for i in range(0, int(1*len(ffttimeseries))): if(peakArray[i]): highCounter +=1 isHigh = 1 if(spaceCounter >= spaceDefinition): morsestring += " " spaceCounter = 0 elif(isHigh): if(highCounter>longSig): morsestring += "-" elif(highCounter < longSig): morsestring += "." highCounter = 0 isHigh = 0 if(not isHigh): spaceCounter +=1 return morsestring
def NormalizeColumnGains(imgArray,target=None,PlotAll=0,Plot=0,JustDark=0,Rows=0,Method="Mean",Wiener=0): """ Purpose: Normalize an image array to remove column gain bias. Designed for DayStar images. Inputs: Outputs: Example: """ # img2=numpy.append(img,img,axis=0) if not isinstance(imgArray, np.ndarray): raise RuntimeError('numpy ndarray input required. Try using loadimg() first.') if imgArray.shape == (2192,2592): #Assumes this is a raw DayStar image # useful index numbers imgTstart = 0 # image rows top start DRTend = 1080+16 # dark rows top end DRBstart = DRTend # dark rows bottom start imgBend = 2160+32 # image rows bottom start imgTop = DarkColNormalize(imgArray[imgTstart:DRTend,:], top=1, Plot=PlotAll, Method=Method) # No Dark Columns, Just Dark Rows and pic # print "imgTop shape",imgTop.shape imgBottom = DarkColNormalize(imgArray[DRBstart:imgBend,:],Plot=PlotAll,Method=Method) # print "imgBottom shape",imgBottom.shape # Normalize Both Images to the same gain setting if target is None: # May want to change this to include all the options target=np.mean([np.mean(imgTop),np.mean(imgBottom)]) topFactor = target/np.mean(imgTop) bottomFactor = target/np.mean(imgBottom) NormImg = np.append(imgTop*topFactor,imgBottom*bottomFactor,axis=0) # Return just the dark or both? if not JustDark: NormImg = NormalizeColumnGains(NormImg,target=target,Method=Method) # if Rows: # NormImg = NormalizeColumnGains(NormImg,target=target,Rows=Rows) else: if JustDark: print "You ToolBag! You are trying to normalize the dark columns of an image with no dark columns!!!" print "flatfield.NormalizeColumnGains expects a dark column image of size [2192,2592]" print "Image size passed is: [%s]" % imgArray.shape NormImg = ImgColNormalize(imgArray,Method=Method) if Plot: PlotComparison(imgArray,NormImg,title="Full Image Gain Normalization") if Wiener: NormImg = signal.wiener(NormImg) return NormImg
def orient(image, height, width): #initial filtering and solving of gradient for calculating orientation imgN=len(image) #print(imgN) imgM=len(image[0]) #print(imgM) img = sp.wiener(image,[3, 3]) Gy, Gx = np.gradient(img) orientnum = sp.wiener(2*Gx*Gy,[3,3]) orientden = sp.wiener((Gx**2) - (Gy**2),[3,3]) W2 = max([len(image), len(image[0])]) W = 8 ll = 9 orient = np.zeros((imgN/W+1,imgM/W+1)) #print(orient.shape) snum = [] sden = [] #calculates orientation for i in range(1,(imgN/W)*(imgM/W)+1): x = m.floor((i-1)/(imgM/W))*W y = ((i-1)%(imgN/W))*W numblock = orientnum[y:y+W,x:x+W] denblock = orientden[y:y+W,x:x+W] somma_num=sum(sum(numblock)) snum.append(somma_num) somma_denom=sum(sum(denblock)) sden.append(somma_denom) if somma_denom != 0: inside = somma_num/somma_denom angle = 0.5*m.atan(inside) else: angle = pi2 if angle < 0: if somma_num < 0: angle = angle + pi2 else: angle = angle + pi else: if somma_num > 0: angle = angle + pi2 orient[(y)/W+1][(x)/W+1] = angle return orient[1:len(orient)+1,1:len(orient[0])+1]
def getSpectrogramFeatures(X): """returns spectrogram features""" specFeatures = [] for i, x in enumerate(X): print "...making spectrogram features for instance: %d ...." % (i + 1) specgramx = pl.specgram(x)[0] filteredX = signal.wiener(specgramx) specFeatures.append(filteredX.reshape(filteredX.size)) pl.close() return np.array(specFeatures)
def perform_abel_discontinuous(x, data): if data.ndim == 1: data = np.atleast_2d(data) ny, nx = data.shape dx = x[1]-x[0] r = x[nx/2:nx-1] nr = len(r) smoothed = np.zeros( (ny,nx) ) deriv = np.zeros( (ny,nx) ) f_abel = np.zeros( (ny,nr) ) sigma_deriv = 2 for i in range(0,ny): w_discont = int(np.where( data[i,nx/2:] != 0)[0][0]) # what happens if measured phase is zero? # unlikely to have exact value... if w_discont == 0: smoothed[i, :] = signal.wiener(data[i,:], mysize=10) deriv[i,:]= ifft(fft(smoothed[i,:]) * fft(periodic_gaussian_deriv(x, sigma_deriv*dx))).real tmp= abel(x, deriv[i,:]) f_abel[i,:]= tmp[0] else: # avoid performing operations over discontinuity left = range(0, int(nx/2 - w_discont +1 )) right = range( int(nx/2 + w_discont) , nx ) smoothed[i, left] = signal.wiener(data[i, left], mysize=10) smoothed[i, right] = signal.wiener(data[i, right], mysize=10) deriv[i,:]= ifft(fft(smoothed[i,:]) * fft(periodic_gaussian_deriv(x, sigma_deriv*dx))).real #deriv[i, left] = ifft(fft(smoothed[i,left]) * fft(periodic_gaussian_deriv(x[left], sigma_deriv*dx))).real #deriv[i, right] = ifft(fft(smoothed[i,right]) * fft(periodic_gaussian_deriv(x[right], sigma_deriv*dx))).real tmp= abel(x, deriv[i,:]) f_abel[i,:]= tmp[0] f_abel[i, 0: w_discont+1] = 0 return (r, f_abel, deriv, smoothed)
def speckle_filter(self, filter_name='wiener', ws=7): """ Filter the image using 'median' filtering methods """ if filter_name == 'median': # filter the image using median filter self.SigmaHHmed = medfilt2d(self.SigmaHH, kernel_size=ws) self.SigmaHVmed = medfilt2d(self.SigmaHV, kernel_size=ws) self.SigmaVHmed = medfilt2d(self.SigmaVH, kernel_size=ws) self.SigmaVVmed = medfilt2d(self.SigmaVV, kernel_size=ws) elif filter_name == 'wiener': # filter the image using wiener filter self.SigmaHHwnr = wiener(self.SigmaHH,mysize=(ws,ws),noise=None) self.SigmaHVwnr = wiener(self.SigmaHV,mysize=(ws,ws),noise=None) self.SigmaVHwnr = wiener(self.SigmaVH,mysize=(ws,ws),noise=None) self.SigmaVVwnr = wiener(self.SigmaVV,mysize=(ws,ws),noise=None) else: print 'Please specify the name of the filter: "median"'
def filter_specgrams_test(): test_dim = 54503 nfeatures = 129 * 30 testX = numpy.zeros(test_dim * nfeatures).reshape(test_dim, nfeatures) for i in range(0, test_dim): sp = specgram("../data/test/test%d.aiff" % (i+1)) y = signal.wiener(sp) print y.shape testX[i, :] = y.reshape(nfeatures) return testX
def _gufunc_filter_wiener(x, y, mysize, noise, out=None): # pragma: no cover # Pre-process xynm = preprocess_interp1d_nan_func(x, y, out) if xynm is None: # all nan return x, y, x_even, y_even, num_nan, mask = xynm # filter even data yf_even = signal.wiener(y_even, mysize=mysize, noise=noise) # Post-process postprocess_interp1d_nan_func(x, x_even, yf_even, num_nan, mask, out)
def test_basic(self): g = array([[5, 6, 4, 3], [3, 5, 6, 2], [2, 3, 5, 6], [1, 6, 9, 7]], "d") correct = array( [ [2.16374269, 3.2222222222, 2.8888888889, 1.6666666667], [2.666666667, 4.33333333333, 4.44444444444, 2.8888888888], [2.222222222, 4.4444444444, 5.4444444444, 4.801066874837], [1.33333333333, 3.92735042735, 6.0712560386, 5.0404040404], ] ) h = signal.wiener(g) assert_array_almost_equal(h, correct, decimal=6)
def resto_wiener(mat): ''' Wiener filter from scipy => [mat] Numpy array <= wmat Numpy array after restoration by Wiener filter ''' from scipy.signal import wiener #im = image_im2mat(im) res = wiener(mat) #res = image_mat2im(res) return res
def labGainProfil(wavelength, alphaSatPeak, gainBandwidth): ''' Generate a gain profile from experimentales cross-section values ''' # Construction d'une curve de cross-Section interpole eCrossSection = load('CrossSectionEm.dat') aCrossSection = load('CrossSectionAbs.dat') aCrossSectionZoom = load('CrossSectionAbs_Zoom.dat') # Petite passe pour que le pics a 975 fit en emission et en absorption aCrossSection_norm = aCrossSection[:,1]*(eCrossSection[:,1].max()/aCrossSection[:,1].max()) eCS_spline = scipy.interpolate.splrep(eCrossSection[:,0], signal.wiener(eCrossSection[:,1],5,10)) aCS_spline = scipy.interpolate.splrep(aCrossSection[:,0]*1E9, signal.wiener(aCrossSection_norm,5,10)) aCSZoom_spline = scipy.interpolate.splrep(aCrossSectionZoom[:,0]*1E9, signal.wiener(aCrossSectionZoom[:,1],5,10)) eCS_WL = arange(850, 1100, 1) aCS_WL = arange(850, 1001 ,1) aCSZoom_WL = arange(1001, 1100, 1) eCS_int = scipy.interpolate.splev(eCS_WL, eCS_spline) aCSLeft_int = scipy.interpolate.splev(aCS_WL, aCS_spline) aCSRight_int = scipy.interpolate.splev(aCSZoom_WL, aCSZoom_spline) aCS_int = r_[aCSLeft_int, aCSRight_int]
def generatePlot(fftts, freq): fftsum = 0 for i in range(0, len(fftts)): fftsum+=fftts[i][freq] fftavg = fftsum/len(fftts) peakArray = [] filtered_fft = wiener(fftts[:,freq], 19) for i in range(0, len(fftts)): peakArray.append(isPeak(filtered_fft[i], freq, fftavg)) #peakArray.append(filtered_fft[i]) print filtered_fft[i] p = peakArray f = np.linspace(0, len(peakArray), len(p)) plt.plot(f, p)
def filter_specgrams_train(seen): train_dim = len(seen) nfeatures = 129 * 30 # First create trainY f = pandas.read_csv("../data/train.csv") trainY = f["label"] trainY = trainY.ix[map(lambda x: x - 1, seen)] # Next read Train data trainX = numpy.zeros(train_dim * nfeatures).reshape(train_dim, nfeatures) for i in range(0, len(seen)): sp = specgram("../data/train/train%d.aiff" % seen[i]) y = signal.wiener(sp) print y.shape trainX[i, :] = y.reshape(nfeatures) return (trainX, trainY)
def pipeline(path, frame_ms=30, hop_ms=15): print("load") #sig, rate = librosa.load(path) #sig2, rate2 = ad.read_file(path) sig, rate = soundfile.read(path) sig = signal.wiener(sig) print("rate", rate) fsize = librosa.time_to_samples(float(frame_ms)/1000, rate)[0] hop = librosa.time_to_samples(float(hop_ms)/1000, rate)[0] print("frame size", fsize, "hop", hop) frames = librosa.util.frame(sig, fsize, hop) w = signal.hann(fsize) #frames_W = np.zeros_like(frames) #print(frames.shape) #frames = frames.T #print(w.shape) print("windowing function") frames_w = np.apply_along_axis(lambda x,w: x*w, 0, frames, w) frames = frames_w print("window suppression") frames = np.apply_along_axis(lambda x,w: x/(w+1e-15), 0, frames, w) # frames_W[i] = signal.convolve(frames[i],w, mode='same') #frames = frames_W.T #w = signal.correlate(w,w,mode='full') #w = w[w.size/2:] #print(frames.shape) #frames = sigutil.enframe(sig, fsize, hop, signal.hann) print("normalized autocorrelation") naccs = np.apply_along_axis(nacc, 0, frames) print("trimming") naccs = np.apply_along_axis(trim_frame, 0, naccs) print(naccs.shape) minacs = np.zeros_like(naccs) for i in range(len(naccs.T)): minacs[:,i] = min_ac(naccs.T, i) print(minacs.shape) print("variances") #acvars = np.apply_along_axis(acvar, 0, naccs2) acvars = np.apply_along_axis(acvar, 0, minacs) print("ltacs") ltacs = np.zeros_like(acvars) for i in range(len(acvars)): ltacs[i] = ltac(acvars, i) return sig, rate, frames, fsize, minacs, acvars, ltacs
def aggregate_windows(self, window_seq, return_velocity=False, **kwargs): """ Recommended window size is 25*32 :param window_seq: :param return_velocity: :param kwargs: :return: """ for window in window_seq: window_scaled = wiener(window, 10) for win_index, win_item in enumerate(window_scaled): # if win_index == 0: yield win_item
def plotChirp(t, eFieldSVEA, filtering = [2000,500]): ''' Plot the frequency chirp of a pulse * t: time vector * SVEA Amplitude Field of the pulse ''' nt = len(eFieldSVEA) nuInstPulse = signal.wiener(nuInst(eFieldSVEA),filtering[0], filtering[1]) width = ptsFWHM(pow(abs(eFieldSVEA),2)) fig = plt.figure() ax1 = fig.add_subplot(111) ax2 = ax1.twinx() y1, y2 = ax1.get_ylim() width = FWHM(t, pow(abs(eFieldSVEA),2)) ax2.figure.canvas.draw() ax1.plot(pow(abs(eFieldSVEA),2)) ax2.plot(nuInstPulse, color='red')
def deblurBeamObjectiveFunction(noiseRatio,beamArray,psf,actualApMeasurementX,actualApMeasurementY): """Objective function used to deblur the deconvolved beam image. An objective funtion is a function whos output is required to be optimal (in this case our optimal value is the minimal one) by some optimisation routine. One, or many arguments can be altered by the optimisation routine. In this case that parameter is the noiseRatio. INPUTS: noiseRatio -A scalar value that represents the noise-power term in the wiener deconvolution function beamArray -A 2D numpy array of floats that represents the theoretical deconvolved i_pin readings at each point in the space. psf -A 2D numpy array of floats that represents the aperture used to measure the beam intensity. actualApMeasurementX -Measured i_pin readings in the horizontal direction as a 1D numpy array of floats actualApMeasurementY -Measured i_pin readings in the vertical direction as a 1D numpy array of floats OUTPUTS: totalRMSD -The sum of the root mean squared deviations of the theoretical and measured i_pin readings. """ #Deblur the image deconvolvedBeamArray = signal.wiener(beamArray,psf.shape,noiseRatio) #simulate the aperture scans simulatedApMeasurementsX,simulatedApMeasurementsY = simulateApertureScans(deconvolvedBeamArray,psf) #Calculate the root mean squared deviations (rmsd) rmsdX = rootMeanSquaredDeviation(simulatedApMeasurementsX, actualApMeasurementX) rmsdY = rootMeanSquaredDeviation(simulatedApMeasurementsY, actualApMeasurementY) ######### I used to need the line below (as opposed to the rmsdX above), ######### however since changing distribution to Anacdonda the code below ######### throws errors because there is a mismatch in the dimensions. #rmsdX = rootMeanSquaredDeviation(simulatedApMeasurementsX, actualApMeasurementX[0:-1]) #Add the rmsds totalRMSD = rmsdX + rmsdY return totalRMSD
def slidingWindowH(P,inner=3,outer=32,maxM=50,norm=True): """ Enhance the constrast horizontally (along temporal dimension) Cut off extreme values and demean the image Utilize numpy convolve to get the mean at a given pixel Remove local mean with inner exclusion region Args: P: 2-d numpy array image inner: inner exclusion region outer: length of the window maxM: size of the output image in the y-dimension norm: boolean to cut off extreme values Returns: Q: 2-d numpy contrast enhanced vertically """ Q = P.copy() m, n = Q.shape Q = exposure.equalize_hist(Q.astype('Float32'), nbins = 65) Q = detrend(Q.astype('Float32'), axis = 0) Q = wiener(Q.astype('Float32'), 4) return Q[:maxM,:]
def wiener_filter(signal): """ Wiener filter. """ output = wiener(signal, 2**5 - 1) return output
def _edge_detect(image, high_threshold=.75, low_threshold=.4): """ Edge detection for 2D images based on Canny filtering. Parameters ========== image: 2D array The image on which edge detection is applied high_threshold: float, optional The quantile defining the upper threshold of the hysteries thresholding: decrease this to keep more edges low_threshold: float, optional The quantile defining the lower threshold of the hysteries thresholding: decrease this to extract wider edges Returns ======== grad_mag: 2D array of floats The magnitude of the gradient edge_mask: 2D array of booleans A mask of where have edges been detected Notes ====== This function is based on a Canny filter, however it has been taylored to visualization purposes on brain images: don't use it in the general case. It computes the norm of the gradient, extracts the ridge by keeping only local maximum in each direction, and performs hysteresis filtering to keep only edges with high gradient magnitude. """ # This code is loosely based on code by Stefan van der Waalt # Convert to floats to avoid overflows np_err = np.seterr(all='ignore') # Replace NaNs by 0s to avoid meaningless outputs image = np.nan_to_num(image) img = signal.wiener(image.astype(np.float)) np.seterr(**np_err) # Where the noise variance is 0, Wiener can create nans img[np.isnan(img)] = image[np.isnan(img)] img /= img.max() grad_x = ndimage.sobel(img, mode='constant', axis=0) grad_y = ndimage.sobel(img, mode='constant', axis=1) grad_mag = np.sqrt(grad_x ** 2 + grad_y ** 2) grad_angle = np.arctan2(grad_y, grad_x) # Scale the angles in the range [0, 2] grad_angle = (grad_angle + np.pi) / np.pi # Non-maximal suppression: an edge pixel is only good if its magnitude is # greater than its neighbors normal to the edge direction. thinner = np.zeros(grad_mag.shape, dtype=np.bool) for angle in np.arange(0, 2, .25): thinner = thinner | ( (grad_mag > .85 * ndimage.maximum_filter( grad_mag, footprint=_orientation_kernel(angle))) & (((grad_angle - angle) % 2) < .75) ) # Remove the edges next to the side of the image: they are not reliable thinner[0] = 0 thinner[-1] = 0 thinner[:, 0] = 0 thinner[:, -1] = 0 thinned_grad = thinner * grad_mag # Hysteresis thresholding: find seeds above a high threshold, then # expand out until we go below the low threshold grad_values = thinned_grad[thinner] high = thinned_grad > fast_abs_percentile(grad_values, 100 * high_threshold) low = thinned_grad > fast_abs_percentile(grad_values, 100 * low_threshold) edge_mask = ndimage.binary_dilation( high, structure=np.ones((3, 3)), iterations=-1, mask=low) return grad_mag, edge_mask