def test_heterodyne(self): """ Test heterodyning on fake data. """ segments = [(time, time + self.fakedataduration) for time in self.fakedatastarts] het = Heterodyne( pulsarfiles=self.fakeparfile, pulsars=["J0000+0000"], segmentlist=segments, framecache=self.fakedatadir, channel=self.fakedatachannels[0], ) with pytest.raises(TypeError): het.stride = "ksgdk" with pytest.raises(TypeError): het.stride = 4.5 with pytest.raises(ValueError): het.stride = -1 with pytest.raises(TypeError): het.filterknee = "lshdl" with pytest.raises(ValueError): het.filterknee = 0 with pytest.raises(TypeError): het.freqfactor = "ldkme" with pytest.raises(ValueError): het.freqfactor = -2.3 with pytest.raises(ValueError): # test that not setting an output gives a error het.heterodyne() # test setting an output directory outdir = os.path.join(self.fakedatadir, "heterodyne_output") het.outputfiles = outdir assert len(het.outputfiles) == 1 assert list(het.outputfiles.keys()) == ["J0000+0000"] assert list( het.outputfiles.values()) == [os.path.join(outdir, het.label)] with pytest.raises(ValueError): # attempt to include glitch evolution without setting includessb to True het.heterodyne(includeglitch=True) # perform first stage heterodyne het = Heterodyne( starttime=segments[0][0], endtime=segments[-1][-1], pulsarfiles=self.fakeparfile, segmentlist=segments, framecache=self.fakedatadir, channel=self.fakedatachannels[0], freqfactor=2, stride=self.fakedataduration // 2, output=outdir, resamplerate=1, ) het.heterodyne() labeldict = { "det": het.detector, "gpsstart": int(het.starttime), "gpsend": int(het.endtime), "freqfactor": int(het.freqfactor), } # expected length (after cropping) length = (het.resamplerate * np.diff(segments).sum() - 2 * len(segments) * het.crop) # expected start time (after cropping) t0 = segments[0][0] + het.crop + 0.5 / het.resamplerate # expected end time (after croppping) tend = segments[-1][-1] - het.crop - 0.5 / het.resamplerate # check output for psr in ["J0000+0000", "J1111+1111", "J2222+2222"]: assert os.path.isfile(het.outputfiles[psr].format(**labeldict, psr=psr)) hetdata = HeterodynedData.read( het.outputfiles[psr].format(**labeldict, psr=psr)) assert len(hetdata) == length assert het.resamplerate == hetdata.dt.value assert t0 == hetdata.times.value[0] assert tend == hetdata.times.value[-1] assert het.detector == hetdata.detector # perform second stage of heterodyne with pytest.raises(TypeError): Heterodyne(heterodyneddata=0) fineoutdir = os.path.join(self.fakedatadir, "fine_heterodyne_output") # first heterodyne without SSB het2 = Heterodyne( detector=self.fakedatadetectors[0], heterodyneddata=outdir, # pass previous output directory pulsarfiles=self.fakeparfile, freqfactor=2, resamplerate=1 / 60, includessb=False, output=fineoutdir, label="heterodyne_{psr}_{det}_{freqfactor}.hdf5", ) het2.heterodyne() models = [] for i, psr in enumerate(["J0000+0000", "J1111+1111", "J2222+2222"]): # load data hetdata = HeterodynedData.read( het2.outputfiles[psr].format(**labeldict, psr=psr)) assert het2.resamplerate == 1 / hetdata.dt.value assert len(hetdata) == int(length * het2.resamplerate) # set expected model sim = HeterodynedCWSimulator( hetdata.par, hetdata.detector, times=hetdata.times.value, earth_ephem=hetdata.ephemearth, sun_ephem=hetdata.ephemsun, ) # due to how the HeterodynedCWSimulator works we need to set # updateglphase = True for the glitching signal to generate a # signal without the glitch phase included! models.append( sim.model( usephase=True, freqfactor=hetdata.freq_factor, updateglphase=(True if psr == "J2222+2222" else False), )) # without inclusion of SSB model should not match assert np.any( np.abs(hetdata.data - models[i]) / np.abs(models[i]) > 5e-3) # now heterodyne with SSB del het2 het2 = Heterodyne( detector=self.fakedatadetectors[0], heterodyneddata=outdir, # pass previous output directory pulsarfiles=self.fakeparfile, freqfactor=2, resamplerate=1 / 60, includessb=True, output=fineoutdir, label="heterodyne_{psr}_{det}_{freqfactor}.hdf5", ) het2.heterodyne() for i, psr in enumerate(["J0000+0000", "J1111+1111", "J2222+2222"]): # load data hetdata = HeterodynedData.read( het2.outputfiles[psr].format(**labeldict, psr=psr)) assert het2.resamplerate == 1 / hetdata.dt.value assert len(hetdata) == int(length * het2.resamplerate) if psr == "J0000+0000": # isolated pulsar assert np.all( np.abs(hetdata.data - models[i]) / np.abs(models[i]) < 5e-3) else: # without inclusion of BSB/glitch phase model should not match assert np.any( np.abs(hetdata.data - models[i]) / np.abs(models[i]) > 5e-3) # now heterodyne with SSB and BSB del het2 het2 = Heterodyne( detector=self.fakedatadetectors[0], heterodyneddata={ psr: het.outputfiles[psr].format(**labeldict, psr=psr) for psr in ["J0000+0000", "J1111+1111", "J2222+2222"] }, # test using dictionary pulsarfiles=self.fakeparfile, freqfactor=2, resamplerate=1 / 60, includessb=True, includebsb=True, output=fineoutdir, label="heterodyne_{psr}_{det}_{freqfactor}.hdf5", ) het2.heterodyne() for i, psr in enumerate(["J0000+0000", "J1111+1111", "J2222+2222"]): # load data hetdata = HeterodynedData.read( het2.outputfiles[psr].format(**labeldict, psr=psr)) assert het2.resamplerate == 1 / hetdata.dt.value assert len(hetdata) == int(length * het2.resamplerate) if psr in [ "J0000+0000", "J1111+1111", ]: # isolated and binary pulsar (non-glitching) assert np.all( np.abs(hetdata.data - models[i]) / np.abs(models[i]) < 1e-2) else: # without inclusion glitch phase model should not match assert np.any( np.abs(hetdata.data - models[i]) / np.abs(models[i]) > 1e-2) # now heterodyne with SSB, BSB and glitch phase del het2 het2 = Heterodyne( detector=self.fakedatadetectors[0], heterodyneddata={ psr: het.outputfiles[psr].format(**labeldict, psr=psr) for psr in ["J0000+0000", "J1111+1111", "J2222+2222"] }, # test using dictionary pulsarfiles=self.fakeparfile, freqfactor=2, resamplerate=1 / 60, includessb=True, includebsb=True, includeglitch=True, output=fineoutdir, label="heterodyne_{psr}_{det}_{freqfactor}.hdf5", ) het2.heterodyne() for i, psr in enumerate(["J0000+0000", "J1111+1111", "J2222+2222"]): # load data hetdata = HeterodynedData.read( het2.outputfiles[psr].format(**labeldict, psr=psr)) assert het2.resamplerate == 1 / hetdata.dt.value assert len(hetdata) == int(length * het2.resamplerate) # increase tolerance for acceptance due to small outliers (still # equivalent at the ~2% level) assert np.all( np.abs(hetdata.data - models[i]) / np.abs(models[i]) < 2e-2)
def test_full_heterodyne(self): """ Test heterodyning on fake data, performing the heterodyne in one step. """ segments = [(time, time + self.fakedataduration) for time in self.fakedatastarts] # perform heterodyne in one step fulloutdir = os.path.join(self.fakedatadir, "full_heterodyne_output") het = Heterodyne( starttime=segments[0][0], endtime=segments[-1][-1], pulsarfiles=self.fakeparfile, segmentlist=segments, framecache=self.fakedatadir, channel=self.fakedatachannels[0], freqfactor=2, stride=self.fakedataduration // 2, resamplerate=1 / 60, includessb=True, includebsb=True, includeglitch=True, output=fulloutdir, label="heterodyne_{psr}_{det}_{freqfactor}.hdf5", ) het.heterodyne() labeldict = { "det": het.detector, "gpsstart": int(het.starttime), "gpsend": int(het.endtime), "freqfactor": int(het.freqfactor), } # compare against model for i, psr in enumerate(["J0000+0000", "J1111+1111", "J2222+2222"]): # load data hetdata = HeterodynedData.read( het.outputfiles[psr].format(**labeldict, psr=psr)) assert het.resamplerate == 1 / hetdata.dt.value # set expected model sim = HeterodynedCWSimulator( hetdata.par, hetdata.detector, times=hetdata.times.value, earth_ephem=hetdata.ephemearth, sun_ephem=hetdata.ephemsun, ) # due to how the HeterodynedCWSimulator works we need to set # updateglphase = True for the glitching signal to generate a # signal without the glitch phase included! model = sim.model( usephase=True, freqfactor=hetdata.freq_factor, updateglphase=(True if psr == "J2222+2222" else False), ) # increase tolerance for acceptance due to small outliers (still # equivalent at the ~2% level) assert np.all(np.abs(hetdata.data - model) / np.abs(model) < 2e-2)