Ejemplo n.º 1
0
    def test_samples_per_frame(self):
        expected = self.fh.read()
        sa = SetAttribute(self.fh, samples_per_frame=11111)
        assert sa.shape == (33333, 8)
        data = sa.read()
        assert np.all(data == expected[:33333])

        sa2 = SetAttribute(self.fh,
                           samples_per_frame=11111,
                           shape=self.fh.shape)
        assert sa2.shape == self.fh.shape
        data2 = sa2.read()
        assert np.all(data2 == expected)
Ejemplo n.º 2
0
 def test_dtype(self):
     expected = self.fh.read()
     sa = SetAttribute(self.fh, dtype='f2')
     assert isinstance(sa.dtype, np.dtype)
     assert sa.dtype == 'f2'
     data = sa.read()
     assert data.dtype == 'f2'
     assert np.all(data == expected.astype('f2'))
Ejemplo n.º 3
0
 def test_set_start_time(self):
     expected = self.fh.read()
     offset = 0.1 * u.s
     sa = SetAttribute(self.fh, start_time=self.fh.start_time + offset)
     assert sa.start_time == self.fh.start_time + offset
     for attr in ('sample_rate', 'samples_per_frame', 'shape', 'dtype'):
         assert getattr(sa, attr) == getattr(self.fh, attr)
     data = sa.read()
     assert np.all(data == expected)
     sa.seek(10)
     data2 = sa.read(10)
     assert np.all(data2 == expected[10:20])
     sa.seek(10 / sa.sample_rate)
     data3 = sa.read(10)
     assert np.all(data3 == expected[10:20])
     sa.seek(sa.start_time + 10 / self.fh.sample_rate)
     data4 = sa.read(10)
     assert np.all(data4 == expected[10:20])
     for attr in ('frequency', 'sideband', 'polarization'):
         with pytest.raises(AttributeError):
             getattr(sa, attr)
     sa.close()
Ejemplo n.º 4
0
    def test_complex_stream(self, tmpdir):
        filename = str(tmpdir.join('copy.hdf5'))
        with baseband.vdif.open(baseband.data.SAMPLE_AROCHIME_VDIF,
                                'rs',
                                sample_rate=800 * u.MHz / 2048) as fh:
            wrapped = SetAttribute(fh)
            data = wrapped.read()
            wrapped.seek(0)
            with hdf5.open(filename, 'w', template=wrapped) as f5w:
                assert f5w.complex_data
                assert f5w.header0.encoded_dtype == 'c8'
                wrapped.read(out=f5w)

        with hdf5.open(filename, 'r') as f5r:
            self.check(wrapped, f5r,
                       ('sample_shape', 'dtype', 'sample_rate', 'time'))
            assert f5r.header0.encoded_dtype == 'c8'
            recovered = f5r.read()

        # Cannot recover exactly, given scaling, but should be within
        # tolerance for float16.
        assert_array_equal(recovered, data)
Ejemplo n.º 5
0
    def test_complex_stream_as_c4(self, tmpdir):
        filename = str(tmpdir.join('copy.hdf5'))
        with baseband.vdif.open(baseband.data.SAMPLE_AROCHIME_VDIF,
                                'rs',
                                sample_rate=800 * u.MHz / 2048) as fh:
            wrapped = SetAttribute(fh)
            data = wrapped.read()
            wrapped.seek(0)
            with hdf5.open(filename, 'w', template=wrapped,
                           encoded_dtype='c4') as f5w:
                assert f5w.complex_data
                assert f5w.header0.encoded_dtype == hdf5.payload.DTYPE_C4
                wrapped.read(out=f5w)

        with hdf5.open(filename, 'r') as f5r:
            self.check(wrapped, f5r,
                       ('sample_shape', 'dtype', 'sample_rate', 'time'))
            assert f5r.header0.encoded_dtype == hdf5.payload.DTYPE_C4
            recovered = f5r.read()

        # Cannot recover exactly, given scaling, but should be within
        # tolerance for float16.
        assert np.allclose(recovered, data, atol=0, rtol=np.finfo('f2').eps)
Ejemplo n.º 6
0
 def test_set_basics(self):
     expected = self.fh.read()
     frequency = 311.25 * u.MHz + (np.arange(8.) // 2) * 16. * u.MHz
     sideband = np.tile([-1, +1], 4)
     sa = SetAttribute(self.fh, frequency=frequency, sideband=sideband)
     assert np.all(sa.frequency == frequency)
     assert np.all(sa.sideband == sideband)
     for attr in ('start_time', 'sample_rate', 'samples_per_frame', 'shape',
                  'dtype'):
         assert getattr(sa, attr) == getattr(self.fh, attr)
     # Check that frequency does not propagate.
     frequency[...] = 0
     assert np.all(sa.frequency != 0)
     sideband[...] = 0
     assert np.all(np.abs(sa.sideband) == 1)
     # Check data can be read.
     data = sa.read()
     assert np.all(data == expected)
     # Check we didn't magically define polarization.
     with pytest.raises(AttributeError):
         sa.polarization
     sa.close()