def choose_templates(templates, age_lim = 20.0, max_nonzero = 5): #start out by loading in the template files as a table import numpy as np import astropy.table as table import SPaCT ssp_rows = [] for template in templates: template = template.rstrip('.fits').split('/')[1] spectral_range = template[0] IMF_type = template[1:3] IMF_slope = float(template[3:7]) Z = SPaCT.plusminus(template[8])*float(template[9:13]) T = float(template[14:]) #print template + ':', spectral_range, IMF_type, IMF_slope, Z, T ssp_i = [template, spectral_range, IMF_type, IMF_slope, Z, T] ssp_rows.append(ssp_i) ssps = table.Table(map(list, zip(*ssp_rows)), names = ['name', 'spectral range', 'IMF type', 'IMF slope', 'Z', 't']) ssps = ssps[ssps['t'] <= age_lim] #then pick up to `max_nonzero` number of templates to be nonzero nonzero_templates = np.random.choice(ssps['name'], np.random.randint(1, max_nonzero + 1), replace = False) template_weights = np.random.rand(len(ssps['name'])) * [1. if i in nonzero_templates else 0. for i in ssps['name']] template_weights /= template_weights.sum() ssps.add_column(table.Column(name = 'weight', data = template_weights)) return ssps
def simulate_noise(sparse_spectrum, SNR, n_skyfiber_range = [1, 20, 3]): ''' generate synthetic noise spectra for a given input spectrum, and test the required number of sky fibers (with similar noise profiles) to accurately get the SNR ''' import numpy as np import SPaCT import matplotlib.pyplot as plt plt.figure(figsize = (6, 4)) for n_skyfibers in range(n_skyfiber_range[0], n_skyfiber_range[1] + 1, n_skyfiber_range[2]): ifu, galaxy_noise = noisify_ifu(sparse_spectrum, n = n_skyfibers, SNR = SNR) fiberlist = range(1, n_skyfibers + 1) SNR_calc = ifu[0] / SPaCT.noise_edgefibers(ifu, width = 3, fiberlist = fiberlist, verbose = False) bins, edges = np.histogram(SNR_calc, 50, normed = 1) left, right = edges[:-1],edges[1:] X = np.array([left,right]).T.flatten() Y = np.array([bins,bins]).T.flatten() plt.plot(X, Y/Y.max(), label = str(n_skyfibers) + ' fibers') plt.axvline(SNR, c = 'k', linestyle = ':') SNR_annotation = plt.text(SNR, 0.35, '$S/N=' + str(SNR) + '$') SNR_annotation.set_rotation('vertical') plt.title('Effect of # of sky fibers on SNR', size = 18) plt.xscale('log') plt.ylim([-0.05, 1.05]) plt.xlabel('SNR', size = 18) plt.ylabel('normed fraction', size = 18) plt.legend(loc = 'best', prop = {'size':6}) plt.tight_layout() plt.show()