Ejemplo n.º 1
0
class StreamSetup:
    def setup(self):
        self.fh = baseband.open(baseband.data.SAMPLE_VDIF)
        self.data = self.fh.read()
        self.fh.seek(0)
        frequency = 311.25 * u.MHz + (np.arange(8.) // 2) * 16. * u.MHz
        sideband = np.tile([-1, +1], 4)
        self.wrapped = SetAttribute(self.fh,
                                    frequency=frequency,
                                    sideband=sideband)

    def teardown(self):
        self.wrapped.close()
        self.fh.close()

    def check(self, stream, header, attrs=None, exclude=()):
        if attrs is None:
            if hasattr(stream, 'bps'):
                attrs = ('sample_shape', 'sample_rate', 'time', 'bps',
                         'complex_data')
                exclude = ('dtype', )
            else:
                attrs = ('sample_shape', 'dtype', 'sample_rate', 'time',
                         'frequency', 'sideband')

        is_header = isinstance(header, hdf5.HDF5Header)

        if 'shape' not in exclude:
            assert stream.shape[0] == header.samples_per_frame
            if is_header:
                assert stream.shape[0] == header['samples_per_frame']

        for attr in attrs:
            stream_attr = getattr(stream,
                                  (attr if attr != 'time' else 'start_time'))
            header_attr = getattr(header, attr)
            if attr == 'time':
                assert np.all(np.abs(header_attr - stream_attr) < 1. * u.ns)
            else:
                assert np.all(header_attr == stream_attr)
            if is_header:
                if attr in exclude:
                    assert attr not in header
                else:
                    header_value = header[attr]
                    assert np.all(header_value == stream_attr)
Ejemplo n.º 2
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.º 3
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()