Example #1
0
 def test_dechannelizetask_real(self):
     """Test dechannelization round-tripping."""
     ct = Channelize(self.fh_freq, self.n)
     dt = Dechannelize(ct, self.n, dtype=self.fh.dtype)
     nrec = (self.fh.shape[0] // self.n) * self.n
     assert dt.shape == (nrec,) + self.fh.shape[1:]
     data = dt.read()
     # Note: round-trip is not perfect due to rounding errors.
     assert np.allclose(data, self.raw_data[:nrec], atol=1.e-5)
     assert np.all(dt.frequency == self.fh_freq.frequency)
     assert np.all(dt.sideband == self.fh_freq.sideband)
     # Check class method
     dt2 = ct.inverse(ct)
     data2 = dt2.read()
     assert np.all(data2 == data)
Example #2
0
    def test_channelize_frequency_complex(self):
        """Test frequency calculation."""
        fh = self.fh_freq
        ct = Channelize(fh, self.n)
        ref_frequency = (320. * u.MHz
                         + np.fft.fftfreq(self.n, 1. / fh.sample_rate))
        assert np.all(ct.sideband == fh.sideband)
        assert np.all(ct.frequency == ref_frequency[:, np.newaxis])

        fh = SetAttribute(self.fh, frequency=self.fh_freq.frequency,
                          sideband=-self.fh_freq.sideband)
        ct = Channelize(fh, self.n)
        ref_frequency = (320. * u.MHz
                         - np.fft.fftfreq(self.n, 1. / fh.sample_rate))
        assert np.all(ct.sideband == fh.sideband)
        assert np.all(ct.frequency == ref_frequency[:, np.newaxis])
Example #3
0
 def test_repr(self):
     """Test channelization task."""
     ct = Channelize(self.fh, self.n)
     cr = repr(ct)
     assert cr.startswith('Channelize(ih')
     assert f"n={self.n}" in cr
     dt = Dechannelize(ct, self.n)
     dr = repr(dt)
     assert dr.startswith('Dechannelize(ih')
     assert f"n={self.n}" in dr
Example #4
0
 def get_tel(self, delay=None, n=None):
     """Get signal from CHIME-like telescope."""
     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)
     if n is None:
         n = self.ns_chan
     # Observe the raw, possibly delayed samples, using channelizer
     return Channelize(fh,
                       n,
                       frequency=self.full_sample_rate,
                       sideband=self.sideband)
Example #5
0
    def test_channelize_samples_per_frame(self, samples_per_frame):
        """Test channelization task."""
        ct = Channelize(self.fh, self.n, samples_per_frame=samples_per_frame)

        # Channelize everything.
        data1 = ct.read()
        assert len(data1) % samples_per_frame == 0
        assert (len(data1) // samples_per_frame
                == len(self.ref_data) // samples_per_frame)
        ref_data = self.ref_data[:len(data1)]
        assert np.all(data1 == ref_data)

        # Seeking and selective decode.
        ct.seek(-3, 2)
        assert ct.tell() == ct.shape[0] - 3
        data2 = ct.read()
        assert data2.shape[0] == 3
        assert np.all(data2 == ref_data[-3:])
Example #6
0
 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)
Example #7
0
 def test_dechannelizetask_complex(self):
     """Test dechannelization round-tripping."""
     fh = self.fh_freq
     raw_data = fh.read()
     ct = Channelize(fh, self.n)
     dt = Dechannelize(ct)
     nrec = (fh.shape[0] // self.n) * self.n
     assert dt.shape == (nrec,) + fh.shape[1:]
     data = dt.read()
     # Note: round-trip is not perfect due to rounding errors.
     assert np.allclose(data, raw_data[:nrec], atol=1.e-5)
     assert np.all(dt.frequency == fh.frequency)
     assert np.all(dt.sideband == fh.sideband)
     # Check class method
     dt2 = ct.inverse(ct)
     data2 = dt2.read()
     assert np.all(data2 == data)
     # Check inverse inverse as well.
     ct2 = dt2.inverse(fh)
     ct.seek(0)
     ft = ct.read()
     ft2 = ct2.read()
     assert np.all(ft == ft2)
     dt2.close()
Example #8
0
 def test_channelize_frequency_real(self):
     """Test frequency calculation."""
     ct = Channelize(self.fh_freq, self.n)
     assert np.all(ct.sideband == self.ref_sideband)
     assert np.all(ct.frequency == self.ref_frequency)
Example #9
0
    def test_channelizetask(self):
        """Test channelization task."""
        ct = Channelize(self.fh, self.n)

        # Channelize everything.
        data1 = ct.read()
        assert ct.tell() == ct.shape[0]
        assert (ct.time - ct.start_time
                - ct.shape[0] / ct.sample_rate) < 1*u.ns
        assert ct.dtype is self.ref_data.dtype is data1.dtype
        assert np.all(self.ref_data == data1)

        # Seeking and selective decode.
        ct.seek(-3, 2)
        assert ct.tell() == ct.shape[0] - 3
        data2 = ct.read()
        assert data2.shape[0] == 3
        assert np.all(self.ref_data[-3:] == data2)

        ct.seek(-2, 2)
        with pytest.raises(EOFError):
            ct.read(10)

        ct.close()
        assert ct.closed
        with pytest.raises(ValueError):
            ct.read(1)
        with pytest.raises(AttributeError):
            ct.ih
Example #10
0
 def test_dechannelize_real_needs_n(self):
     # For real data, need to pass in `n`
     ct = Channelize(self.fh_freq, self.n)
     with pytest.raises(ValueError):
         Dechannelize(ct, dtype=self.fh_freq.dtype)
Example #11
0
 def test_missing_frequency_sideband(self):
     with Channelize(self.fh, self.n) as ct:
         with pytest.raises(AttributeError):
             ct.frequency
         with pytest.raises(AttributeError):
             ct.sideband
Example #12
0
 def test_size_check(self):
     # Quick test of channel sanity check in __init__.
     with pytest.raises(AssertionError):
         Channelize(self.fh, 400001)