def fit_wavefront(diffs, detection): """Fit the wavefront that has been detected by the hough transform. Simplest case is to fit along the y-direction for some x or range of x.""" dims=diffs[0].shape answers=[] wavefront_maps=[] for i in range (0, len(diffs)): if (detection[i].max() == 0.0): #if the 'detection' array is empty then skip this image fit_map=sunpy.make_map(np.zeros(dims),diffs[0]._original_header) print("Nothing detected in image " + str(i) + ". Skipping.") answers.append([]) wavefront_maps.append(fit_map) else: #if the 'detection' array is not empty, then fit the wavefront in the image img = diffs[i] fit_map=np.zeros(dims) #get the independent variable for the columns in the image x=(np.linspace(0,dims[0],num=dims[0])*img.scale['y']) + img.yrange[0] #use 'detection' to guess the centroid of the Gaussian fit function guess_index=detection[i].argmax() guess_index=np.unravel_index(guess_index,detection[i].shape) guess_position=x[guess_index[0]] print("Analysing wavefront in image " + str(i)) column_fits=[] #for each column in image, fit along the y-direction a function to find wave parameters for n in range (0,dims[1]): #guess the amplitude of the Gaussian fit from the difference image guess_amp=np.float(img[guess_index[0],n]) #put the guess input parameters into a vector guess_params=[guess_amp,guess_position,5] #get the current image column y=img[:,n] y=y.flatten() #call Albert's fitting function result = util.fitfunc(x,y,'Gaussian',guess_params) #define a Gaussian function. Messy - clean this up later gaussian = lambda p,x: p[0]/np.sqrt(2.*np.pi)/p[2]*np.exp(-((x-p[1])/p[2])**2/2.) #Draw the Gaussian fit for the current column and save it in fit_map #save the best-fit parameters in column_fits #only want to store the successful fits, discard the others. #result contains a pass/fail integer. Keep successes ( ==1). if result[1] == 1: #if we got a pass integer, perform some other checks to eliminate unphysical values result=check_fit(result) column_fits.append(result) if result != []: fit_column = gaussian(result[0],x) else: fit_column = np.zeros(len(x)) else: #if the fit failed then save as zeros/null values result=[] column_fits.append(result) fit_column = np.zeros(len(x)) #draw the Gaussian fit for the current column and save it in fit_map #gaussian = lambda p,x: p[0]/np.sqrt(2.*np.pi)/p[2]*np.exp(-((x-p[1])/p[2])**2/2.) #save the drawn column in fit_map fit_map[:,n] = fit_column #save the fit parameters for the image in 'answers' and the drawn map in 'wavefront_maps' fit_map=sunpy.make_map(fit_map,diffs[0]._original_header) answers.append(column_fits) wavefront_maps.append(fit_map) #now get the mean values of the fitted wavefront, averaged over all x #average_fits=[] #for ans in answers: # cleaned_answers=[] # for k in range(0,len(ans)): # #ans[:,1] contains a pass/fail integer. Keep successes (==1), discard the rest # if ans[k][1] == 1: # tmp=ans[k][0] # cleaned_answers.append(tmp) # else: # cleaned_answers.append([]) # #get the mean of each fit parameter for this image and store it # #average_fits.append(np.mean(g,axis=0)) return answers, wavefront_maps
def fit_wavefront(diffs, detection): """Fit the wavefront that has been detected by the hough transform. Simplest case is to fit along the y-direction for some x or range of x.""" dims = diffs[0].shape answers = [] wavefront_maps = [] for i in range(0, len(diffs)): if (detection[i].max() == 0.0): #if the 'detection' array is empty then skip this image fit_map = sunpy.map.Map(np.zeros(dims), diffs[0].meta) print("Nothing detected in image " + str(i) + ". Skipping.") answers.append([]) wavefront_maps.append(fit_map) else: #if the 'detection' array is not empty, then fit the wavefront in the image img = diffs[i] fit_map = np.zeros(dims) #get the independent variable for the columns in the image x = (np.linspace(0, dims[0], num=dims[0]) * img.scale['y']) + img.yrange[0] #use 'detection' to guess the centroid of the Gaussian fit function guess_index = detection[i].argmax() guess_index = np.unravel_index(guess_index, detection[i].shape) guess_position = x[guess_index[0]] print("Analysing wavefront in image " + str(i)) column_fits = [] #for each column in image, fit along the y-direction a function to find wave parameters for n in range(0, dims[1]): #guess the amplitude of the Gaussian fit from the difference image guess_amp = np.float(img.data[guess_index[0], n]) #put the guess input parameters into a vector guess_params = [guess_amp, guess_position, 5] #get the current image column y = img.data[:, n] y = y.flatten() #call Albert's fitting function result = util.fitfunc(x, y, 'Gaussian', guess_params) #define a Gaussian function. Messy - clean this up later gaussian = lambda p, x: p[0] / np.sqrt(2. * np.pi) / p[ 2] * np.exp(-((x - p[1]) / p[2])**2 / 2.) #Draw the Gaussian fit for the current column and save it in fit_map #save the best-fit parameters in column_fits #only want to store the successful fits, discard the others. #result contains a pass/fail integer. Keep successes ( ==1). if result[1] == 1: #if we got a pass integer, perform some other checks to eliminate unphysical values result = check_fit(result) column_fits.append(result) if result != []: fit_column = gaussian(result[0], x) else: fit_column = np.zeros(len(x)) else: #if the fit failed then save as zeros/null values result = [] column_fits.append(result) fit_column = np.zeros(len(x)) #draw the Gaussian fit for the current column and save it in fit_map #gaussian = lambda p,x: p[0]/np.sqrt(2.*np.pi)/p[2]*np.exp(-((x-p[1])/p[2])**2/2.) #save the drawn column in fit_map fit_map[:, n] = fit_column #save the fit parameters for the image in 'answers' and the drawn map in 'wavefront_maps' fit_map = sunpy.map.Map(fit_map, diffs[0].meta) answers.append(column_fits) wavefront_maps.append(fit_map) #now get the mean values of the fitted wavefront, averaged over all x #average_fits=[] #for ans in answers: # cleaned_answers=[] # for k in range(0,len(ans)): # #ans[:,1] contains a pass/fail integer. Keep successes (==1), discard the rest # if ans[k][1] == 1: # tmp=ans[k][0] # cleaned_answers.append(tmp) # else: # cleaned_answers.append([]) # #get the mean of each fit parameter for this image and store it # #average_fits.append(np.mean(g,axis=0)) return answers, wavefront_maps
wave_normalization = np.dot(wave_normalization_coeff, time_powers).ravel() wave_peak = 90. - (p(time) + (90. - lat_min)) n0 = wave_normalization * width.clip(0, 360) / 360. m0 = wave_peak s0 = wave_thickness n1 = [] m1 = [] s1 = [] for wave in wave_maps_raw: yy = np.average(np.asarray(wave), axis=1) xx = np.arange(wave.yrange[0], wave.yrange[1], wave.scale['y']) + wave.scale['y'] / 2. p, success = util.fitfunc(xx, yy, 'gaussian', [1, 90, 1]) if p[0] < 0.1: p, success = util.fitfunc(xx, yy, 'gaussian', [1, 75, 1]) n1 += [p[0]] m1 += [p[1]] s1 += [p[2]] n2 = [] m2 = [] s2 = [] for wave in new_wave_maps: data = np.array(wave) data[np.isnan(data)] = 0. yy = np.average(data, axis=1) xx = np.linspace(wave.yrange[0], wave.yrange[1],
wave_thickness = np.dot(wave_thickness_coeff, time_powers).ravel() wave_normalization = np.dot(wave_normalization_coeff, time_powers).ravel() wave_peak = 90.-(p(time)+(90.-lat_min)) n0 = wave_normalization*width.clip(0,360)/360. m0 = wave_peak s0 = wave_thickness n1 = [] m1 = [] s1 = [] for wave in wave_maps_raw: yy = np.average(np.asarray(wave), axis=1) xx = np.arange(wave.yrange[0],wave.yrange[1],wave.scale['y'])+wave.scale['y']/2. p, success = util.fitfunc(xx, yy, 'gaussian', [1, 90, 1]) if p[0] < 0.1: p, success = util.fitfunc(xx, yy, 'gaussian', [1, 75, 1]) n1 += [p[0]] m1 += [p[1]] s1 += [p[2]] n2 = [] m2 = [] s2 = [] for wave in new_wave_maps: data = np.array(wave) data[np.isnan(data)] = 0. yy = np.average(data, axis=1) xx = np.linspace(wave.yrange[0],wave.yrange[1],wave.shape[0])+wave.scale['y']/2.