Beispiel #1
0
def test_one_free_parameter_input_output():

    fluxUnit = 1. / (u.TeV * u.cm**2 * u.s)

    temp_file = "__test_mle.fits"
    
    spectrum=Powerlaw()
    source = PointSource( "tst", ra=100 , dec=20, spectral_shape=spectrum)
    model = Model(source)

    spectrum.piv = 7*u.TeV      
    spectrum.index = -2.3
    spectrum.K = 1e-15*fluxUnit  

    spectrum.piv.fix = True

    #two free parameters (one with units)
    spectrum.index.fix = False
    spectrum.K.fix = False
    cov_matrix = np.diag([0.001]*2)
    ar = MLEResults(model, cov_matrix, {})  
    
    ar.write_to(temp_file, overwrite=True)
    ar_reloaded = load_analysis_results(temp_file)
    os.remove(temp_file)
    _results_are_same(ar, ar_reloaded)
   
    #one free parameter with units
    spectrum.index.fix = True
    spectrum.K.fix = False
    cov_matrix = np.diag([0.001]*1)
    ar = MLEResults(model, cov_matrix, {})  
    
    ar.write_to(temp_file, overwrite=True)
    ar_reloaded = load_analysis_results(temp_file)
    os.remove(temp_file)
    _results_are_same(ar, ar_reloaded)
  
   
    #one free parameter without units
    spectrum.index.fix = False
    spectrum.K.fix = True
    cov_matrix = np.diag([0.001]*1)
    ar = MLEResults(model, cov_matrix, {})  
    
    ar.write_to(temp_file, overwrite=True)
    ar_reloaded = load_analysis_results(temp_file)
    os.remove(temp_file)
    _results_are_same(ar, ar_reloaded)
def test_one_free_parameter_input_output():

    fluxUnit = 1. / (u.TeV * u.cm**2 * u.s)

    temp_file = "__test_mle.fits"

    spectrum = Powerlaw()
    source = PointSource("tst", ra=100, dec=20, spectral_shape=spectrum)
    model = Model(source)

    spectrum.piv = 7 * u.TeV
    spectrum.index = -2.3
    spectrum.K = 1e-15 * fluxUnit

    spectrum.piv.fix = True

    #two free parameters (one with units)
    spectrum.index.fix = False
    spectrum.K.fix = False
    cov_matrix = np.diag([0.001] * 2)
    ar = MLEResults(model, cov_matrix, {})

    ar.write_to(temp_file, overwrite=True)
    ar_reloaded = load_analysis_results(temp_file)
    os.remove(temp_file)
    _results_are_same(ar, ar_reloaded)

    #one free parameter with units
    spectrum.index.fix = True
    spectrum.K.fix = False
    cov_matrix = np.diag([0.001] * 1)
    ar = MLEResults(model, cov_matrix, {})

    ar.write_to(temp_file, overwrite=True)
    ar_reloaded = load_analysis_results(temp_file)
    os.remove(temp_file)
    _results_are_same(ar, ar_reloaded)

    #one free parameter without units
    spectrum.index.fix = False
    spectrum.K.fix = True
    cov_matrix = np.diag([0.001] * 1)
    ar = MLEResults(model, cov_matrix, {})

    ar.write_to(temp_file, overwrite=True)
    ar_reloaded = load_analysis_results(temp_file)
    os.remove(temp_file)
    _results_are_same(ar, ar_reloaded)
def test_analysis_set_input_output(xy_fitted_joint_likelihood):

    # Collect twice the same analysis results just to see if we can
    # save them in a file as set of results

    jl, _, _ = xy_fitted_joint_likelihood  # type: JointLikelihood, None, None

    jl.restore_best_fit()

    ar = jl.results  # type: MLEResults

    ar2 = jl.results

    analysis_set = AnalysisResultsSet([ar, ar2])

    analysis_set.set_bins("testing", [-1, 1], [3, 5], unit='s')

    temp_file = "_analysis_set_test"

    analysis_set.write_to(temp_file, overwrite=True)

    analysis_set_reloaded = load_analysis_results(temp_file)

    os.remove(temp_file)

    # Test they are the same
    assert len(analysis_set_reloaded) == len(analysis_set)

    for res1, res2 in zip(analysis_set, analysis_set_reloaded):

        _results_are_same(res1, res2)
Beispiel #4
0
def test_analysis_set_input_output(xy_fitted_joint_likelihood):

    # Collect twice the same analysis results just to see if we can
    # save them in a file as set of results

    jl, _, _ = xy_fitted_joint_likelihood  # type: JointLikelihood, None, None

    jl.restore_best_fit()

    ar = jl.results  # type: MLEResults

    ar2 = jl.results

    analysis_set = AnalysisResultsSet([ar, ar2])

    analysis_set.set_bins("testing", [-1, 1], [3, 5], unit = 's')

    temp_file = "_analysis_set_test"

    analysis_set.write_to(temp_file, overwrite=True)

    analysis_set_reloaded = load_analysis_results(temp_file)

    os.remove(temp_file)

    # Test they are the same
    assert len(analysis_set_reloaded) == len(analysis_set)

    for res1, res2 in zip(analysis_set, analysis_set_reloaded):

        _results_are_same(res1, res2)
Beispiel #5
0
    def __init__(self,
                 analysis_path,
                 trigdat_path,
                 real_coord=None,
                 det_list=all_det_list,
                 save_path=None,
                 sun=True):
        """
		:param analysis_file: path of the file with the bayesian analysis result.
		:param trigdat_path: path of the trigdat file.
		:param real_coord: real or reference equatorial coordinates for the source in the format [ra,dec] (in deg). Default is none.
		:param det_list: list of the detector you wish to plot, in the format ['n1','n5','b0']. Default is all detectors.
		:param save_path: path and name for the plotted skymap (default is 'skymap.pdf' in cwd)
                :param sun: plot sun position in the skymap (default is True)
		"""

        self._analysis_path = analysis_path
        self._trigdat_path = trigdat_path

        if real_coord != None:
            self._real_coord = [
                np.radians(-real_coord[0] + 180.),
                np.radians(real_coord[1])
            ]
        else:
            self._real_coord = None

        self._det_list = det_list

        if save_path == None:
            self._save_path = getcwd() + '/skymap.pdf'
        else:
            self._save_path = save_path

        results = load_analysis_results(self._analysis_path)
        self._ra_samples = np.radians(-np.array(results.samples[0]) + 180.)
        self._dec_samples = np.radians(np.array(results.samples[1]))

        ra_fit = np.radians(-results.get_data_frame().value[0] + 180.)
        dec_fit = np.radians(results.get_data_frame().value[1])

        self._fit_coord = [ra_fit, dec_fit]

        ra_cl_50 = results.get_equal_tailed_interval('test.position.ra',
                                                     cl=0.5)
        dec_cl_50 = results.get_equal_tailed_interval('test.position.dec',
                                                      cl=0.5)
        ra_cl_90 = results.get_equal_tailed_interval('test.position.ra',
                                                     cl=0.9)
        dec_cl_90 = results.get_equal_tailed_interval('test.position.dec',
                                                      cl=0.9)

        self._cl_50 = [np.radians(ra_cl_50), np.radians(dec_cl_50)]
        self._cl_90 = [np.radians(ra_cl_90), np.radians(dec_cl_90)]

        self._detectors = self._get_gbm_geometry()
        self._earth_circle = self._get_earth_circle()
def test_bayesian_input_output(xy_completed_bayesian_analysis):

    bs, _ = xy_completed_bayesian_analysis

    rb1 = bs.results

    temp_file = "_test_bayes.fits"

    rb1.write_to(temp_file, overwrite=True)

    rb2 = load_analysis_results(temp_file)

    os.remove(temp_file)

    _results_are_same(rb1, rb2, bayes=True)
Beispiel #7
0
def test_bayesian_input_output(xy_completed_bayesian_analysis):

    bs, _ = xy_completed_bayesian_analysis

    rb1 = bs.results

    temp_file = "_test_bayes.fits"

    rb1.write_to(temp_file, overwrite=True)

    rb2 = load_analysis_results(temp_file)

    os.remove(temp_file)

    _results_are_same(rb1, rb2, bayes=True)
def test_analysis_results_input_output(xy_fitted_joint_likelihood):

    jl, _, _ = xy_fitted_joint_likelihood  # type: JointLikelihood, None, None

    jl.restore_best_fit()

    ar = jl.results  # type: MLEResults

    temp_file = "__test_mle.fits"

    ar.write_to(temp_file, overwrite=True)

    ar_reloaded = load_analysis_results(temp_file)

    os.remove(temp_file)

    _results_are_same(ar, ar_reloaded)
Beispiel #9
0
def test_analysis_results_input_output(xy_fitted_joint_likelihood):

    jl, _, _ = xy_fitted_joint_likelihood  # type: JointLikelihood, None, None

    jl.restore_best_fit()

    ar = jl.results  # type: MLEResults

    temp_file = "__test_mle.fits"

    ar.write_to(temp_file, overwrite=True)

    ar_reloaded = load_analysis_results(temp_file)

    os.remove(temp_file)

    _results_are_same(ar, ar_reloaded)