|
3 | 3 | import copy |
4 | 4 | import warnings |
5 | 5 | import importlib |
| 6 | +import tempfile |
6 | 7 |
|
7 | 8 | import pytest |
8 | 9 | import matplotlib.pyplot as plt |
|
11 | 12 | from stingray.events import EventList |
12 | 13 | from stingray.utils import HAS_NUMBA |
13 | 14 | from stingray import Powerspectrum, AveragedPowerspectrum, DynamicalPowerspectrum |
| 15 | +from stingray.powerspectrum import GtiCorrPowerspectrum |
14 | 16 | from stingray.powerspectrum import powerspectrum_from_time_array |
15 | 17 | from astropy.modeling.models import Lorentz1D |
16 | 18 | from stingray.filters import filter_for_deadtime |
@@ -381,6 +383,144 @@ def test_deadtime_corr(self): |
381 | 383 | assert np.isclose(np.std(pds.power), 2 / np.sqrt(tmax / segment_size), rtol=0.1) |
382 | 384 |
|
383 | 385 |
|
| 386 | +class TestGtiCorrPowerspectrum(object): |
| 387 | + @classmethod |
| 388 | + def setup_class(cls): |
| 389 | + """Set up a light curve and an event list with GTIs for testing GtiCorrPowerspectrum.""" |
| 390 | + tstart = 0.0 |
| 391 | + tend = 100.0 |
| 392 | + dt = 0.01 |
| 393 | + |
| 394 | + time = np.arange(tstart + 0.5 * dt, tend + 0.5 * dt, dt) |
| 395 | + |
| 396 | + mean_count_rate = 1000.0 |
| 397 | + mean_counts = mean_count_rate * dt |
| 398 | + |
| 399 | + poisson_counts = rng.poisson(mean_counts, size=time.shape[0]) |
| 400 | + |
| 401 | + cls.lc = Lightcurve(time, counts=poisson_counts, gti=[[tstart, tend]], dt=dt) |
| 402 | + cls.events = EventList( |
| 403 | + np.sort( |
| 404 | + np.random.uniform( |
| 405 | + tstart, tend, np.random.poisson(mean_count_rate * (tend - tstart)) |
| 406 | + ) |
| 407 | + ), |
| 408 | + gti=[[tstart, tend]], |
| 409 | + ) |
| 410 | + |
| 411 | + @pytest.mark.parametrize("norm", ["leahy", "frac", "abs", "none"]) |
| 412 | + def test_gti_corr_ps(self, norm): |
| 413 | + """GtiCorrPowerspectrum results match Powerspectrum when GTIs are unimportant.""" |
| 414 | + gcps = GtiCorrPowerspectrum(self.lc, norm=norm) |
| 415 | + ps = Powerspectrum(self.lc, norm=norm) |
| 416 | + for attr in [ |
| 417 | + "freq", |
| 418 | + "power", |
| 419 | + "power_err", |
| 420 | + "unnorm_power", |
| 421 | + "unnorm_power_err", |
| 422 | + "df", |
| 423 | + "m", |
| 424 | + "n", |
| 425 | + "nphots", |
| 426 | + ]: |
| 427 | + assert np.array_equal(getattr(gcps, attr), getattr(ps, attr)) |
| 428 | + |
| 429 | + @pytest.mark.parametrize("norm", ["leahy", "frac", "abs", "none"]) |
| 430 | + def test_gti_corr_ps_events(self, norm): |
| 431 | + """GtiCorrPowerspectrum results match Powerspectrum for event lists.""" |
| 432 | + gcps = GtiCorrPowerspectrum(self.events, dt=0.01, norm=norm) |
| 433 | + ps = Powerspectrum(self.events, dt=0.01, norm=norm) |
| 434 | + for attr in [ |
| 435 | + "freq", |
| 436 | + "power", |
| 437 | + "power_err", |
| 438 | + "unnorm_power", |
| 439 | + "unnorm_power_err", |
| 440 | + "df", |
| 441 | + "m", |
| 442 | + "n", |
| 443 | + "nphots", |
| 444 | + ]: |
| 445 | + assert np.array_equal(getattr(gcps, attr), getattr(ps, attr)) |
| 446 | + |
| 447 | + @pytest.mark.parametrize("norm", ["leahy", "frac", "abs", "none"]) |
| 448 | + def test_gti_corr_ps_fill_different(self, norm): |
| 449 | + """Filling BTIs or not changes the power spectrum.""" |
| 450 | + lc = copy.deepcopy(self.lc) |
| 451 | + lc.gti = [[0, 30], [35, 100]] # Two GTIs, one gap |
| 452 | + gcps_fill = GtiCorrPowerspectrum(lc, norm=norm, fill_lc=True) |
| 453 | + gcps_nofill = GtiCorrPowerspectrum(lc, norm=norm, fill_lc=False) |
| 454 | + gcps_fill = gcps_fill.clean_gti_features() |
| 455 | + gcps_nofill = gcps_nofill.clean_gti_features() |
| 456 | + mean_gcps_fill = np.mean(gcps_fill.power) |
| 457 | + mean_gcps_nofill = np.mean(gcps_nofill.power) |
| 458 | + assert not np.allclose(mean_gcps_fill, mean_gcps_nofill, rtol=0.01) |
| 459 | + |
| 460 | + @pytest.mark.parametrize("norm", ["leahy", "frac", "abs", "none"]) |
| 461 | + def test_gti_corr_ps_fill(self, norm): |
| 462 | + """Filling BTIs and correcting the normalization adjusts the power spectrum.""" |
| 463 | + lc = copy.deepcopy(self.lc) |
| 464 | + lc.gti = [[0, 30], [35, 100]] # Two GTIs, one gap |
| 465 | + gcps = GtiCorrPowerspectrum(lc, norm=norm, fill_lc=True) |
| 466 | + gcps = gcps.clean_gti_features() |
| 467 | + ps = Powerspectrum(self.lc, norm=norm) |
| 468 | + mean_ps = np.mean(ps.power) |
| 469 | + mean_gcps = np.mean(gcps.power) |
| 470 | + assert np.isclose(mean_gcps, mean_ps, rtol=0.01) |
| 471 | + |
| 472 | + @pytest.mark.parametrize("norm", ["leahy", "frac", "abs", "none"]) |
| 473 | + def test_gti_corr_ps_fill_events(self, norm): |
| 474 | + """Filling BTIs and correcting the normalization adjusts the power spectrum.""" |
| 475 | + lc = copy.deepcopy(self.events) |
| 476 | + lc.gti = [[0, 30], [35, 100]] # Two GTIs, one gap |
| 477 | + gcps = GtiCorrPowerspectrum(lc, dt=0.01, norm=norm, fill_lc=True) |
| 478 | + gcps = gcps.clean_gti_features() |
| 479 | + ps = Powerspectrum(self.events, dt=0.01, norm=norm) |
| 480 | + mean_ps = np.mean(ps.power) |
| 481 | + mean_gcps = np.mean(gcps.power) |
| 482 | + assert np.isclose(mean_gcps, mean_ps, rtol=0.01) |
| 483 | + |
| 484 | + @pytest.mark.parametrize("norm", ["leahy", "frac", "abs", "none"]) |
| 485 | + def test_gti_corr_ps_fill_rebin(self, norm): |
| 486 | + """Rebinning works on GtiCorrPowerspectrum.""" |
| 487 | + lc = copy.deepcopy(self.lc) |
| 488 | + lc.gti = [[0, 30], [35, 100]] # Two GTIs, one gap |
| 489 | + gcps = GtiCorrPowerspectrum(lc, norm=norm, fill_lc=True) |
| 490 | + gcps = gcps.clean_gti_features() |
| 491 | + ps = Powerspectrum(self.lc, norm=norm) |
| 492 | + ps = ps.rebin_log(0.01) |
| 493 | + gcps = gcps.rebin_log(0.01) |
| 494 | + |
| 495 | + mean_ps = np.mean(ps.power) |
| 496 | + mean_gcps = np.mean(gcps.power) |
| 497 | + assert np.isclose(mean_gcps, mean_ps, rtol=0.1) |
| 498 | + |
| 499 | + def test_gti_corr_apply_gti_lc_fails(self): |
| 500 | + """Applying GTIs to a light curve with gaps in the time array raises an error.""" |
| 501 | + lc = copy.deepcopy(self.lc) |
| 502 | + lc.gti = [[0, 30], [35, 100]] # Two GTIs, one gap |
| 503 | + lc.apply_gtis() |
| 504 | + with pytest.raises(ValueError, match="The time array in the light"): |
| 505 | + GtiCorrPowerspectrum(lc, norm="leahy", fill_lc=True) |
| 506 | + |
| 507 | + def test_gti_corr_plot(self): |
| 508 | + """Plotting GtiCorrPowerspectrum works.""" |
| 509 | + lc = copy.deepcopy(self.events) |
| 510 | + lc.gti = [[0, 30], [35, 100]] # Two GTIs, one gap |
| 511 | + gcps = GtiCorrPowerspectrum(lc, dt=0.01, norm="leahy", fill_lc=True) |
| 512 | + with tempfile.NamedTemporaryFile(delete=False) as tmpfile: |
| 513 | + figname = tmpfile.name |
| 514 | + try: |
| 515 | + gcps = gcps.clean_gti_features(plot=True, figname=figname) |
| 516 | + jpg_name = figname + ".jpg" |
| 517 | + assert os.path.exists(jpg_name) |
| 518 | + os.unlink(jpg_name) |
| 519 | + finally: |
| 520 | + if os.path.exists(figname): |
| 521 | + os.unlink(figname) |
| 522 | + |
| 523 | + |
384 | 524 | class TestPowerspectrum(object): |
385 | 525 | @classmethod |
386 | 526 | def setup_class(cls): |
|
0 commit comments