def __init__(self, frequency, wavelength, flux, noise, voigt, chi_limit=2.5): self.frequency = frequency self.wavelength = wavelength self.flux = flux self.noise = noise self.num_pixels = len(frequency) self.voigt = voigt self.dataset = Spectrum(self.frequency, self.wavelength, self.flux, self.noise) """ Make initial guess for number of local minima in the region. Smooth the spectra with a gaussian and find the number of local minima. as a safety precaucion, set the initial guess for number of profiles to 1 if there are less than 4 local minima. """ self.initial_n = argrelextrema(gaussian_filter(self.dataset.flux, 3), np.less)[0].shape[0] if self.initial_n < 4: self.initial_n = 1. self.n = self.initial_n.copy()
def find_good_fit(mode, dataset: Spectrum): n_initial = dataset.estimate_n() n_components = n_initial + 0 phases = [] results = [] evidences = [] good_fit = False evidences.insert(0, -1. * np.inf) i = 1 while not good_fit: phase = make_phase(mode, n_components) result = phase.run(dataset=dataset) evidence = result.output.evidence # I think we need to just stop after a certain chi squared tbh ### Evaluate the result if evidence > evidences[i - 1]: n_components += 1 i += 1 phases.append(phase) results.append(result) evidences.append(evidence) else: good_fit == True return phases, results, evidences
def new_region_spectra(self): new_spectra = [] for reg in self.region_pixels: new_spectra.append( Spectrum(self.dataset.frequency[reg[0]:reg[1]], self.dataset.wavelength[reg[0]:reg[1]], self.dataset.flux[reg[0]:reg[1]], self.dataset.noise[reg[0]:reg[1]])) return new_spectra
# Setup the path to the config folder, using the autolens_workspace path. config_path = workspace_path + "config" # Use this path to explicitly set the config path and output path. af.conf.instance = af.conf.Config( config_path=config_path, output_path=workspace_path + "output" ) from vamp_workspace.make_data import FakeGauss fakeGaussA = FakeGauss(center=-1.0, sigma=2.0, intensity=0.5) fakeGaussB = FakeGauss(center=1.5, sigma=1.0, intensity=1.0) fakeGauss_2comp = fakeGaussB.gauss + fakeGaussA.gauss + fakeGaussA.noise dataset = Spectrum(fakeGaussA.x, fakeGaussA.x, 1.0 - fakeGauss_2comp, fakeGaussA.noise) phase = ph.Phase( phase_name="phase_x2_gaussians", profiles=af.CollectionPriorModel( gaussian_0=profile_models.Gaussian, gaussian_1=profile_models.Gaussian ), ) result = phase.run(dataset=dataset) plt.plot(dataset.frequency, result.most_likely_model_spectrum) plt.plot(dataset.frequency, dataset.flux) plt.show() # We also have an 'output' attribute, which in this case is a MultiNestOutput object: print(result.output)
# Setup the path to the config folder, using the vamp_workspace path. config_path = workspace_path + "config" # Use this path to explicitly set the config path and output path. af.conf.instance = af.conf.Config(config_path=config_path, output_path=workspace_path + "output") from vamp_workspace.make_data import FakeGauss fakeGauss = FakeGauss(center=-1.0, sigma=2.0, intensity=0.5) phase = ph.Phase( phase_name="phase_x1_gaussians", profiles=af.CollectionPriorModel(gaussian_0=profile_models.Gaussian)) dataset = Spectrum(fakeGauss.x, fakeGauss.x, 1.0 - fakeGauss.noisy_gauss, fakeGauss.noise) result = phase.run(dataset=dataset) plt.plot(dataset.frequency, result.most_likely_model_spectrum) plt.plot(dataset.frequency, dataset.flux) plt.show() # We also have an 'output' attribute, which in this case is a MultiNestOutput object: print(result.output) # This object acts as an interface between the MultiNest output results on your hard-disk and this Python code. For # example, we can use it to get the evidence estimated by MultiNest. print(result.output.evidence) # We can also use it to get a model-instance of the "most probable" model, which is the model where each parameter is # the value estimated from the probability distribution of parameter space. mp_instance = result.output.most_probable_instance print()
# Setup the path to the config folder, using the vamp_workspace path. config_path = workspace_path + "config" # Use this path to explicitly set the config path and output path. af.conf.instance = af.conf.Config(config_path=config_path, output_path=workspace_path + "output") from vamp_workspace.make_data import FakeVoigt fakeVoigt = FakeVoigt(center=-1.0, intensity=0.5, fwhm_L=1.0, fwhm_G=2.0) phase = ph.Phase( phase_name="phase_x1_voigt", profiles=af.CollectionPriorModel(voigt_0=profile_models.Voigt)) dataset = Spectrum(fakeVoigt.x, fakeVoigt.x, 1.0 - fakeVoigt.noisy_voigt, fakeVoigt.noise) result = phase.run(dataset=dataset) # We also have an 'output' attribute, which in this case is a MultiNestOutput object: print(result.output) # This object acts as an interface between the MultiNest output results on your hard-disk and this Python code. For # example, we can use it to get the evidence estimated by MultiNest. print(result.output.evidence) # We can also use it to get a model-instance of the "most probable" model, which is the model where each parameter is # the value estimated from the probability distribution of parameter space. mp_instance = result.output.most_probable_instance print() print("Most Probable Model:\n") print("Centre = ", [i.center for i in mp_instance.profiles]) print("Intensity = ", [i.intensity for i in mp_instance.profiles])
centers_c = [2.] sigmas_a = [1.0] sigmas_b = [1.5] sigmas_c = [2.0] intensity_a = [0.4] intensity_b = [1.0] intensity_c = [0.7] combos = ['e'] for i, combo in enumerate(combos): attrs = {} fakeGaussA = FakeGauss(center=centers_a[i], sigma=sigmas_a[i], intensity=intensity_a[i]) fakeGaussB = FakeGauss(center=centers_b[i], sigma=sigmas_b[i], intensity=intensity_b[i]) fakeGaussC = FakeGauss(center=centers_c[i], sigma=sigmas_c[i], intensity=intensity_c[i]) dataset_a = Spectrum(fakeGaussA.x, fakeGaussA.x, 1.0 - fakeGaussA.gauss - fakeGaussA.noise, fakeGaussA.noise) attrs['center_0'] = centers_a[i]; attrs['sigma_0'] = sigmas_a[i]; attrs['intensity_0'] = intensity_a[i] dataset_a.save_as_h5py(spectrum_dir+'combo_' + combo + '_1_component.h5', attributes=attrs) dataset_a.plot_spectrum(spectrum_dir+'combo_' + combo + '_1_component.png') dataset_b = Spectrum(fakeGaussB.x, fakeGaussB.x, 1.0 - fakeGaussA.gauss - fakeGaussB.gauss - fakeGaussB.noise, fakeGaussB.noise) attrs['center_1'] = centers_b[i]; attrs['sigma_1'] = sigmas_b[i]; attrs['intensity_1'] = intensity_b[i] dataset_b.save_as_h5py(spectrum_dir+'combo_' + combo + '_2_component.h5', attributes=attrs) dataset_b.plot_spectrum(spectrum_dir+'combo_' + combo + '_2_component.png') dataset_c = Spectrum(fakeGaussC.x, fakeGaussC.x, 1.0 - fakeGaussA.gauss - fakeGaussB.gauss - fakeGaussC.gauss - fakeGaussC.noise, fakeGaussC.noise) attrs['center_2'] = centers_c[i]; attrs['sigma_2'] = sigmas_c[i]; attrs['intensity_2'] = intensity_c[i] dataset_c.save_as_h5py(spectrum_dir+'combo_' + combo + '_3_component.h5', attributes=attrs) dataset_c.plot_spectrum(spectrum_dir+'combo_' + combo + '_3_component.png')