Example #1
0
def create_flux_chain(analyzer, transformations, spectrum, erange = "2.0 10.0"):
	"""
	For each posterior sample, computes the flux in the given energy range.

	The so-created chain can be combined with redshift information to propagate
	the uncertainty. This is especially important if redshift is a variable
	parameter in the fit (with some prior).

	Returns erg/cm^2 energy flux (first column) and photon flux (second column)
	for each posterior sample.
	"""
	posterior = analyzer.get_equal_weighted_posterior()
	prefix = analyzer.outputfiles_basename
	modelnames = set([t['model'].name for t in transformations])

	oldchatter = Xset.chatter, Xset.logChatter
	Xset.chatter, Xset.logChatter = 0, 0
	# plot models
	flux = []
	pbar = progressbar.ProgressBar(
		widgets=[progressbar.Percentage(), progressbar.Counter('%5d'), 
		progressbar.Bar(), progressbar.ETA()],
		maxval=len(posterior)).start()
	for k, row in enumerate(posterior):
		set_parameters(parameters=row[:-1], transformations=transformations)
		AllModels.calcFlux(erange)
		f = spectrum.flux
		# compute flux in current energies
		f.append([f[0], f[3]])
		pbar.update(k)
	pbar.finish()
	
	Xset.chatter, Xset.logChatter = oldchatter		
	return numpy.array(flux)
Example #2
0
def create_flux_chain(analyzer, transformations, spectrum, erange = "2.0 10.0"):
	"""
	For each posterior sample, computes the flux in the given energy range.

	The so-created chain can be combined with redshift information to propagate
	the uncertainty. This is especially important if redshift is a variable
	parameter in the fit (with some prior).

	Returns erg/cm^2 energy flux (first column) and photon flux (second column)
	for each posterior sample.
	"""
	posterior = analyzer.get_equal_weighted_posterior()
	#prefix = analyzer.outputfiles_basename
	#modelnames = set([t['model'].name for t in transformations])

	oldchatter = Xset.chatter, Xset.logChatter
	Xset.chatter, Xset.logChatter = 0, 0
	# plot models
	flux = []
	for k, row in enumerate(tqdm(posterior, disable=None)):
		set_parameters(values=row[:-1], transformations=transformations)
		AllModels.calcFlux(erange)
		f = spectrum.flux
		# compute flux in current energies
		flux.append([f[0], f[3]])
	
	Xset.chatter, Xset.logChatter = oldchatter		
	return numpy.array(flux)
Example #3
0
def create_flux_chain(analyzer, transformations, spectrum, erange = "2.0 10.0"):
	"""
	For each posterior sample, computes the flux in the given energy range.

	The so-created chain can be combined with redshift information to propagate
	the uncertainty. This is especially important if redshift is a variable
	parameter in the fit (with some prior).

	Returns erg/cm^2 energy flux (first column) and photon flux (second column)
	for each posterior sample.
	"""
	posterior = analyzer.get_equal_weighted_posterior()
	#prefix = analyzer.outputfiles_basename
	#modelnames = set([t['model'].name for t in transformations])

	oldchatter = Xset.chatter, Xset.logChatter
	Xset.chatter, Xset.logChatter = 0, 0
	# plot models
	flux = []
	for k, row in enumerate(tqdm(posterior, disable=None)):
		set_parameters(values=row[:-1], transformations=transformations)
		AllModels.calcFlux(erange)
		f = spectrum.flux
		# compute flux in current energies
		flux.append([f[0], f[3]])
	
	Xset.chatter, Xset.logChatter = oldchatter		
	return numpy.array(flux)
Example #4
0
File: solver.py Project: ellot/BXA
    def create_flux_chain(self, spectrum, erange="2.0 10.0", nsamples=None):
        """
		For each posterior sample, computes the flux in the given energy range.

		The so-created chain can be combined with redshift information to propagate
		the uncertainty. This is especially important if redshift is a variable
		parameter in the fit (with some prior).

		Returns erg/cm^2 energy flux (first column) and photon flux (second column)
		for each posterior sample.
		"""
        #prefix = analyzer.outputfiles_basename
        #modelnames = set([t['model'].name for t in transformations])

        with XSilence():
            # plot models
            flux = []
            for k, row in enumerate(
                    tqdm(self.posterior[:nsamples], disable=None)):
                set_parameters(values=row,
                               transformations=self.transformations)
                AllModels.calcFlux(erange)
                f = spectrum.flux
                # compute flux in current energies
                flux.append([f[0], f[3]])

            return numpy.array(flux)
Example #5
0
def set_parameters(transformations, values):
	"""
	Set current parameters.
	"""
	pars = []
	for i, t in enumerate(transformations):
		v = t['aftertransform'](values[i])
		assert not isnan(v) and not isinf(v), 'ERROR: parameter %d (index %d, %s) to be set to %f' % (
			i, t['index'], t['name'], v)
		pars += [t['model'], {t['index']:v}]
	AllModels.setPars(*pars)
Example #6
0
def set_parameters(transformations, values):
	"""
	Set current parameters.
	"""
	pars = []
	for i, t in enumerate(transformations):
		v = t['aftertransform'](values[i])
		assert not isnan(v) and not isinf(v), 'ERROR: parameter %d (index %d, %s) to be set to %f' % (
			i, t['index'], t['name'], v)
		pars += [t['model'], {t['index']:v}]
	AllModels.setPars(*pars)
Example #7
0
def create_flux_chain(analyzer, transformations, spectrum, erange="2.0 10.0"):
    """
	For each posterior sample, computes the flux in the given energy range.

	The so-created chain can be combined with redshift information to propagate
	the uncertainty. This is especially important if redshift is a variable
	parameter in the fit (with some prior).

	Returns erg/cm^2 energy flux (first column) and photon flux (second column)
	for each posterior sample.
	"""
    posterior = analyzer.get_equal_weighted_posterior()
    prefix = analyzer.outputfiles_basename
    modelnames = set([t['model'].name for t in transformations])

    oldchatter = Xset.chatter, Xset.logChatter
    Xset.chatter, Xset.logChatter = 0, 0
    # plot models
    flux = []
    pbar = progressbar.ProgressBar(widgets=[
        progressbar.Percentage(),
        progressbar.Counter('%5d'),
        progressbar.Bar(),
        progressbar.ETA()
    ],
                                   maxval=len(posterior)).start()
    for k, row in enumerate(posterior):
        set_parameters(parameters=row[:-1], transformations=transformations)
        AllModels.calcFlux(erange)
        f = spectrum.flux
        # compute flux in current energies
        f.append([f[0], f[3]])
        pbar.update(k)
    pbar.finish()

    Xset.chatter, Xset.logChatter = oldchatter
    return numpy.array(flux)