Exemple #1
0
    def test_fake(self):
        """Test the fake dataset"""
        source_model = SkyModel(spectral_model=PowerLawSpectralModel())
        dataset = SpectrumDatasetOnOff(
            counts=self.on_counts,
            counts_off=self.off_counts,
            models=source_model,
            aeff=self.aeff,
            livetime=self.livetime,
            edisp=self.edisp,
            acceptance=1,
            acceptance_off=10,
        )
        real_dataset = dataset.copy()
        # Define background model counts
        elo = self.on_counts.energy.edges[:-1]
        ehi = self.on_counts.energy.edges[1:]
        data = np.ones(self.on_counts.data.shape)
        background_model = CountsSpectrum(elo, ehi, data)
        dataset.fake(background_model=background_model,
                     random_state=314,
                     name="fake")

        assert real_dataset.counts.data.shape == dataset.counts.data.shape
        assert real_dataset.counts_off.data.shape == dataset.counts_off.data.shape
        assert (real_dataset.counts.energy.center.mean() ==
                dataset.counts.energy.center.mean())
        assert real_dataset.acceptance.mean() == dataset.acceptance.mean()
        assert real_dataset.acceptance_off.mean(
        ) == dataset.acceptance_off.mean()
        assert dataset.counts_off.data.sum() == 39
        assert dataset.counts.data.sum() == 5
        assert dataset.name == "fake"
Exemple #2
0
def simulate_spectrum_dataset(model, random_state=0):
    energy = np.logspace(-0.5, 1.5, 21) * u.TeV
    aeff = EffectiveAreaTable.from_parametrization(energy=energy)
    bkg_model = PowerLawSpectralModel(index=2.5, amplitude="1e-12 cm-2 s-1 TeV-1")

    dataset = SpectrumDatasetOnOff(
        aeff=aeff, model=model, livetime=100 * u.h, acceptance=1, acceptance_off=5
    )

    eval = SpectrumEvaluator(model=bkg_model, aeff=aeff, livetime=100 * u.h)

    bkg_model = eval.compute_npred()
    dataset.fake(random_state=random_state, background_model=bkg_model)
    return dataset
# ### OnOff analysis
#
# To do `OnOff` spectral analysis, which is the usual science case, the standard would be to use `SpectrumDatasetOnOff`, which uses the acceptance to fake off-counts

# In[ ]:

dataset_onoff = SpectrumDatasetOnOff(
    aeff=dataset.aeff,
    edisp=dataset.edisp,
    models=model,
    livetime=livetime,
    acceptance=1,
    acceptance_off=5,
)
dataset_onoff.fake(background_model=dataset.background)
print(dataset_onoff)

# You can see that off counts are now simulated as well. We now simulate several spectra using the same set of observation conditions.

# In[ ]:

get_ipython().run_cell_magic(
    'time', '',
    '\nn_obs = 100\ndatasets = []\n\nfor idx in range(n_obs):\n    dataset_onoff.fake(random_state=idx, background_model=dataset.background)\n    dataset_onoff.name = f"obs_{idx}"\n    datasets.append(dataset_onoff.copy())'
)

# Before moving on to the fit let's have a look at the simulated observations.

# In[ ]: