def test_modulator_unbinned(self): nh = self.nh mt = Task(nh, self.profile, samples_per_frame=1) nh.seek(0) n1 = nh.read() m1 = mt.read() phase = ((np.arange(1000.) + 0.5) / 1000. * 3.) % 1. assert np.allclose(m1, n1 * self.gaussian_profile(phase))
def test_modulator_bin10(self): nh = self.nh mt = Task(nh, self.profile, samples_per_frame=10) nh.seek(0) n1 = nh.read() m1 = mt.read() phase = ((np.arange(100.) + 0.5) / 100. * 3.) % 1. profile = self.gaussian_profile(phase) expected = (n1.reshape(-1, 10) * profile[:, np.newaxis]).ravel() assert np.allclose(m1, expected) # Also test with squarer, just for fun. st = Square(mt) s1 = st.read(300) assert np.allclose(s1, np.abs(expected[:300])**2)
def setup_class(self): self.start_time = Time('2010-11-12T13:14:15') self.sample_rate = 10. * u.kHz self.shape = (16000, 2) self.eh = EmptyStreamGenerator(shape=self.shape, start_time=self.start_time, sample_rate=self.sample_rate, samples_per_frame=200, dtype=float) self.sh = Task(self.eh, self.pulse_simulate) self.period_bin = 125 self.F0 = 1.0 / (self.period_bin / self.sh.sample_rate) self.n_phase = 50 self.raw_data = self.sh.read() self.raw_power = self.raw_data**2
def test_use_as_source(self): """Test that it looks like a file also to the squarer.""" tone = np.zeros((1000, ), dtype=np.complex64) tone[200] = 1. def set_tone(data): data[...] = tone return data eh = EmptyStreamGenerator(shape=(10, 1000), start_time=self.start_time, sample_rate=10. * u.Hz, samples_per_frame=2) sh = Task(eh, set_tone) st = Square(sh) data1 = st.read() assert st.tell() == st.shape[0] assert abs(st.time - st.start_time - 1. * u.s) < 1 * u.ns assert np.all(data1 == np.abs(tone)**2) # Seeking and selective squaring. st.seek(-3, 2) assert st.tell() == st.shape[0] - 3 data2 = st.read() assert data2.shape[0] == 3 assert np.all(data2 == np.abs(tone)**2)
class FakePulsarBase: def setup_class(self): self.start_time = Time('2010-11-12T13:14:15') self.sample_rate = 10. * u.kHz self.shape = (16000, 2) self.eh = EmptyStreamGenerator(shape=self.shape, start_time=self.start_time, sample_rate=self.sample_rate, samples_per_frame=200, dtype=float) self.sh = Task(self.eh, self.pulse_simulate) self.period_bin = 125 self.F0 = 1.0 / (self.period_bin / self.sh.sample_rate) self.n_phase = 50 self.raw_data = self.sh.read() self.raw_power = self.raw_data**2 @classmethod def pulse_simulate(self, fh, data): idx = fh.tell() + np.arange(data.shape[0]) result = np.where(idx % self.period_bin == 0, 10., 0.125) result.shape = (-1, ) + (1, ) * (data.ndim - 1) data[:] = result return data def phase(self, t): return u.cycle * self.F0 * (t - self.start_time)
def test_task_method_repr(self): ft = Task(self.fh, zero_every_8th_complex) r = repr(ft) assert r.startswith('Task(ih') assert 'task=' in r assert 'zero_every_8th' in r pre, post = r.split('\nih') assert 'samples_per_frame' not in pre assert 'VDIFStream' in post
def test_task_function_repr(self): ft = Task(self.fh, zero_channel_4) r = repr(ft) assert r.startswith('Task(ih') assert 'task=' in r assert 'zero_channel_4' in r pre, post = r.split('\nih') assert 'samples_per_frame' not in pre assert 'VDIFStream' in post
def test_invalid(self): with pytest.raises(Exception): # Cannot determine function/method. Task(self.fh, np.add) def trial(data, bla=1): return data with Task(self.fh, trial) as th: assert not inspect.ismethod(th.task) def trial2(data, bla, bla2=1): return data with Task(self.fh, trial2) as th2: assert inspect.ismethod(th2.task) def trial3(data, bla, bla2, bla3=1): return data with pytest.raises(Exception): Task(self.fh, trial3)
def test_method_task(self, samples_per_frame): count = self.fh.shape[0] if samples_per_frame is not None: count = (count // samples_per_frame) * samples_per_frame ref_data = zero_every_8th_sample(self.fh.read(count)) with Task(self.fh, zero_every_8th_complex, samples_per_frame=samples_per_frame) as ft: data1 = ft.read() assert np.all(data1 == ref_data) assert ft.closed
def test_modulator_unbinned(self): nh = self.nh mt = Task(nh, self.block_profile, samples_per_frame=1) nh.seek(0) n1 = nh.read(10) m1 = mt.read(10) assert np.all(m1 == n1 * 0.125) nh.seek(0.5 * u.s) mt.seek(0.5 * u.s) n2 = nh.read(10) m2 = mt.read(10) assert np.all(m2 == n2) nh.seek(0) mt.seek(0) n = nh.read() m = mt.read() assert n.shape == m.shape == self.shape assert np.all(m[:450] == 0.125 * n[:450]) assert np.all(m[450:550] == n[450:550]) assert np.all(m[550:] == 0.125 * n[550:])
def test_1p1j_setting(self): def set_constant(data): data[...] = 1 + 1j return data with EmptyStreamGenerator( shape=self.shape, start_time=self.start_time, sample_rate=self.sample_rate, samples_per_frame=20) as eh, \ Task(eh, set_constant) as sh: assert sh.size == np.prod(self.shape) assert sh.shape == self.shape assert sh.samples_per_frame == 20 assert abs(sh.stop_time - sh.start_time - 1. * u.s) < 1. * u.ns sh.seek(900) data1 = sh.read(20) assert data1.shape == (20, 4, 2) assert np.all(data1 == 1 + 1j)
def test_tone(self): tone = np.zeros((1000, ), dtype=np.complex64) tone[200] = 1. def set_tone(data): data[...] = tone return data with EmptyStreamGenerator( shape=(10, 1000), start_time=self.start_time, sample_rate=10. * u.Hz, samples_per_frame=2) as eh, \ Task(eh, set_tone) as sh: assert abs(sh.stop_time - sh.start_time - 1. * u.s) < 1. * u.ns sh.seek(5) data1 = sh.read(1) assert np.all(data1 == tone) data2 = sh.read() assert data2.shape == (10 - 6, 1000) assert np.all(data2 == tone)
def get_tel(self, delay=None, n=None): """Get signal as observed at a telescope with the given delay and number of channels.""" if delay is None: fh = self.raw else: delay_time = delay / self.raw.sample_rate fh = SetAttribute(self.raw, start_time=self.start_time - delay_time) # Observe the raw, possibly delayed samples, using mix_downsample. obs = Task(fh, self.mix_downsample, dtype=self.dtype, sample_rate=self.sample_rate, frequency=self.lo, sideband=self.sideband) if n is None: return obs else: return Channelize(obs, n)
def test_inversion_guppi_pfb_digitized(self): n_sample = 512 pad = 128 self.nh.seek(pad * 64 + 11 * 64 // 2) d_in = self.nh.read(n_sample * 64).reshape(-1, 64) pfb = PolyphaseFilterBank(self.nh, self.guppi_pfb) dig_level = pfb.read(n_sample).real.std() / 30. pfb_dig = Task(pfb, task=lambda ft: digitize(ft, dig_level), samples_per_frame=n_sample) ipfb = InversePolyphaseFilterBank(pfb_dig, self.guppi_pfb, sn=30, pad_start=pad, pad_end=pad, samples_per_frame=n_sample * 64, dtype=self.nh.dtype) d_out = ipfb.read(n_sample * 64).reshape(-1, 64) # Not much effect of digitization since it introduces little noise. assert_allclose(d_in, d_out, atol=0.15)
def test_inversion_chime_pfb_digitized(self): # Now test the same, but with the actual inversion class. # Here, we do not give samples_per_frame for the PFB, since we do # not need its FT (and it is exact for any value). n_sample = 128 pad = 32 self.nh.seek(pad * 2048 + 3 * 2048 // 2) d_in = self.nh.read(n_sample * 2048).reshape(-1, 2048) pfb = PolyphaseFilterBank(self.nh, self.chime_pfb) dig_level = pfb.read(n_sample).real.std() / 3. pfb_dig = Task(pfb, task=lambda ft: digitize(ft, dig_level), samples_per_frame=n_sample) ipfb = InversePolyphaseFilterBank(pfb_dig, self.chime_pfb, sn=10, pad_start=pad, pad_end=pad, samples_per_frame=n_sample * 2048, dtype=self.nh.dtype) d_out = ipfb.read(n_sample * 2048).reshape(-1, 2048) assert np.isclose((d_out - d_in).std(), 0.125, atol=0.01) assert_allclose(d_in, d_out, atol=1.1)
def test_function_tasks(self, task, sample_factor): """Test setting a channel to zero.""" # Load baseband file and get reference intensities. ref_data = task(self.fh.read()) ft = Task(self.fh, task, sample_rate=self.fh.sample_rate * sample_factor) assert ft.shape[0] == self.fh.shape[0] * sample_factor # Apply to everything. data1 = ft.read() assert ft.tell() == ft.shape[0] assert abs(ft.time - ft.start_time - ft.shape[0] / ft.sample_rate) < 1 * u.ns assert ft.dtype is ref_data.dtype is data1.dtype assert np.allclose(ref_data, data1) # Seeking and selective zeroing. ft.seek(-3, 2) assert ft.tell() == ft.shape[0] - 3 data2 = ft.read() assert data2.shape[0] == 3 assert np.allclose(ref_data[-3:], data2) ft.close() assert ft.closed with pytest.raises(ValueError): ft.read(1)
def test_modulator_binned(self): nh = self.nh mt = Task(nh, self.block_profile, samples_per_frame=50) nh.seek(0) n = nh.read() m = mt.read() assert np.all(m[:450] == 0.125 * n[:450]) assert np.all(m[450:550] == n[450:550]) assert np.all(m[550:] == 0.125 * n[550:]) # Just to show one has to be careful: not giving # samples_per_frame takes it from nh, which is 200. mt = Task(nh, self.block_profile) m = mt.read() assert np.all(m[:400] == 0.125 * n[:400]) assert np.all(m[400:600] == n[400:600]) assert np.all(m[600:] == 0.125 * n[600:]) # And more cases to show one has to be careful... mt = Task(nh, self.block_profile, samples_per_frame=500) m = mt.read() assert np.all(m == 0.125 * n) mt = Task(nh, self.block_profile, samples_per_frame=1000) m = mt.read() assert np.all(m == n)