def train_decomposer(train_dict, filename=None, one_phase=0): import gausspy.gp as gp import time import pickle print('\nTraining on synthetic data...') g = gp.GaussianDecomposer() g.load_training_data(filename) if one_phase: #One phase training g.set('phase', 'one') g.set('alpha1', 5) g.set('SNR_thresh', 3.) g.set('SNR2_thresh', 3.) g.train(alpha1_initial=5.0, verbose=False, mode='conv', learning_rate=1.0, eps=1.0, MAD=0.1) else: #Two phase training g.set('phase', 'two') g.set('alpha1', 3) g.set('alpha2', 8) g.set('SNR_thresh', [5, 5.]) g.set('SNR2_thresh', [5., 0.]) #g.set('SNR_thresh', [5.,5.]) #g.set('SNR2_thresh', [5.,0.]) g.train( alpha1_initial=2, alpha2_initial=5, plot=False, verbose=False, mode='conv', learning_rate=1, eps=1.0, MAD=0.1, ) print 'made it to 85' return g
def test_train(): import numpy as np import gausspy.gp as gp reload(gp) TRAINING_DATA = 'agd_data_train.pickle' g = gp.GaussianDecomposer() g.load_training_data(TRAINING_DATA) #One phase training g.set('phase', 'one') g.set('SNR_thresh', 5.) g.train(alpha1_initial=2.)
def _test_train(): import gausspy.gp as gp # imp.reload(gp) TRAINING_DATA = "agd_data_train.pickle" g = gp.GaussianDecomposer() g.load_training_data(TRAINING_DATA) # One phase training g.set("phase", "one") g.set("SNR_thresh", 5.0) g.train(alpha1_initial=2.0)
def deconvolve(args): # Load GaussPy g = gp.GaussianDecomposer() # Setting AGD parameters g.set('phase', 'one') g.set('SNR_thresh', [float(args.snr), float(args.snr)]) g.set('alpha1', float(args.alpha)) # Run GaussPy data_decomp = g.batch_decomposition(args.ccfs) ## Save decomposition information pickle.dump(data_decomp, open(args.deconvol, 'wb')) return
def test_onephase_decompose_conv(): import gausspy.gp as gp import time import pickle # imp.reload(gp) SCIENCE_DATA = "agd_data_science.pickle" g = gp.GaussianDecomposer() # Set defaults g.set("SNR_thresh", 5.0) g.set("alpha1", 1.02) g.set("phase", "one") g.set("mode", "conv") t0 = time.time() new_data = g.batch_decomposition(SCIENCE_DATA) print("Elapsed time [s]: ", int(time.time() - t0))
def test_decompose(): import numpy as np import gausspy.gp as gp import time import pickle reload(gp) SCIENCE_DATA = 'agd_data_science.pickle' g = gp.GaussianDecomposer() #Two phase g.set('phase', 'two') g.set('SNR_thresh', 5.) g.set('SNR2_thresh', 5.) g.set('alpha1', 1.02) g.set('alpha2', 2.22) t0 = time.time() new_data = g.batch_decomposition(SCIENCE_DATA) print 'Elapsed time [s]: ', int(time.time() - t0) pickle.dump(new_data, open('agd_data_science_decomposed.pickle', 'w'))
def decompose_data(filename_data, g_train=None, filename_decomposed=None, data_dict=None): import gausspy.AGD_decomposer as agd g = gp.GaussianDecomposer() print('\nDecomposing data...') #Two phase if g_train is not None: g.set('alpha1', g_train.p['alpha1']) g.set('alpha2', g_train.p['alpha2']) g.set('phase', g_train.p['phase']) g.set('SNR_thresh', g_train.p['SNR_thresh']) g.set('SNR2_thresh', g_train.p['SNR2_thresh']) else: g.set('alpha1', 2.5) g.set('alpha2', 6) g.set('BLFrac', 0.02) g.set('phase', 'two') g.set('SNR_thresh', 3.) g.set('SNR2_thresh', 3.) g.set('mode', 'conv') g.set('verbose', False) if data_dict is None: new_data = g.batch_decomposition(filename_data) if filename_decomposed is not None: pickle.dump(new_data, open(filename_decomposed, 'w')) else: results_dict = {} #results_dict['spectra'] = [] results_dict['results'] = [] #if filename_decomposed is not None: # results_dict = pickle.load(open(filename_decomposed, 'r')) x_values = data_dict['velocity_axis'] for i in xrange(len(data_dict['data_list'])): #for i in xrange(12274, 12276): print('\n\titeration ' + str(i)) try: results = g.decompose(x_values, data_dict['data_list'][i], data_dict['errors']) except (np.linalg.LinAlgError, ValueError): results['N_components'] = 0 # record location of spectrum results['spectrum_number'] = i if 0: # Construct spectrum if results['N_components'] > 0: spectrum = \ construct_spectrum(results['best_fit_parameters'], x_values) else: spectrum = np.zeros(len(x_values)) # Plot scratch plot of fits import matplotlib.pyplot as plt plt.close(); plt.clf() plt.plot(x_values, data_dict['data_list'][i]) plt.plot(x_values, spectrum, alpha=0.5, linewidth=3) plt.savefig('/d/bip3/ezbc/scratch/spectrum_fit_' + \ str(i) + '.png') #results_dict['spectra'].append(spectrum) results_dict['results'].append(results) # Add positions to results results_dict['positions'] = data_dict['positions'].copy() # Add velocity axis to results results_dict['velocity_axis'] = data_dict['velocity_axis'].copy() if filename_decomposed is not None: pickle.dump(results_dict, open(filename_decomposed, 'w')) return results_dict
def gauss_decomp(out, phase='one', alpha1=0.5, alpha2=1.5, thresh=[4, 4], plot=None, filt=False): """ Do Gaussian decomposition of CCF using gausspy Parameters: out : list of dictionaries for each frame, giving x_ccf, ccf, and ccferr phase : gausspy paramater alpha1 : gausspy parameter alpha2 : gausspy parameter for second set of gaussians if phase=='two' thresh : gausspy parameter plot (str) : if not None, do plot and use as root file name for plot filt (bool) : if true, apply filtering to remove components judged to be insignificant """ g = gp.GaussianDecomposer() g.set('phase', phase) g.set('SNR_thresh', thresh) g.set('alpha1', alpha1) g.set('alpha2', alpha2) gout = [] if plot is not None: fig, ax = plots.multi(1, len(out), hspace=0.001, figsize=(6, 2 + n)) for i, final in enumerate(out): gd, = np.where(np.isfinite(final['x_ccf'])) x = final['x_ccf'][gd] y = final['ccf'][gd] # high pass filter for better performance if filt: final['ccf'][gd] -= gaussian_filter(final['ccf'][gd], 50, mode='nearest') try: decomp = g.decompose(x, final['ccf'][gd], final['ccferr'][gd]) n = decomp['N_components'] except: print( 'Exception in Gaussian decomposition, setting to 0 components') n = 0 decomp = None if filt and n > 0: # remove components if they are within width of brighter component, or <0.25 peak , # or more than twice as wide, or if primary component is wide for j in range(1, n): pars_j = decomp['best_fit_parameters'][j::n] for k in range(j): pars_k = decomp['best_fit_parameters'][k::n] if (pars_j[0] > pars_k[0] and pars_k[0] > 0 and (abs(pars_j[2] - pars_k[2]) < abs(pars_j[1]) or pars_k[0] < 0.25 * pars_j[0] or abs(pars_j[1]) > 100 or np.abs(pars_k[1]) > 2 * np.abs(pars_j[1]))): decomp['best_fit_parameters'][k] = 0 decomp['N_components'] -= 1 elif (pars_k[0] > pars_j[0] and pars_j[0] > 0 and (abs(pars_j[2] - pars_k[2]) < abs(pars_k[1]) or pars_j[0] < 0.25 * pars_k[0] or abs(pars_k[1]) > 100 or np.abs(pars_j[1]) > 2 * np.abs(pars_k[1]))): decomp['best_fit_parameters'][j] = 0 pars_j = decomp['best_fit_parameters'][j::n] decomp['N_components'] -= 1 gout.append(decomp) if plot is not None: plots.plotl(ax[i], final['x_ccf'], final['ccf']) ax[i].plot(final['x_ccf'], final['ccferr'], color='r') for j in range(n): pars = gout[i]['best_fit_parameters'][j::n] ax[i].plot(x, gaussian(*pars)(x)) if pars[0] > 0: color = 'k' else: color = 'r' ax[i].text(0.1, 0.8 - j * 0.1, '{:8.1f}{:8.1f}{:8.1f}'.format(*pars), transform=ax[i].transAxes, color=color) fig.savefig(plot + '_ccf.png') del g return gout