示例#1
0
文件: test_cli.py 项目: stefco/gwpy
    def test_get_timeseries(self):
        try:  # can't use decorator because this method gets called
            import nds2
        except ImportError as e:
            pytest.skip(str(e))
        product, parser = self.test_init_cli()
        args = parser.parse_args(self.TEST_ARGS + ['--out', TEMP_PLOT_FILE])
        product.post_arg(args)

        random.seed(0)
        xts = TimeSeries(random.rand(10240), t0=1000000000,
                         sample_rate=1024, name='X1:TEST-CHANNEL')
        yts = TimeSeries(random.rand(10240), t0=1000000000,
                         sample_rate=1024, name='Y1:TEST-CHANNEL')
        nds_connection = mocks.nds2_connection(buffers=[
            mocks.nds2_buffer_from_timeseries(xts),
            mocks.nds2_buffer_from_timeseries(yts),
        ])
        with mock.patch('nds2.connection') as mock_connection, \
                mock.patch('nds2.buffer'):
            mock_connection.return_value = nds_connection

            product.getTimeSeries(args)

        assert len(product.timeseries) == (len(product.chan_list) *
                                           len(product.start_list))

        return product, args
示例#2
0
文件: test_io.py 项目: stefco/gwpy
    def test_find_frametype(self, connection):
        """Test :func:`gwpy.io.datafind.find_frametype
        """
        with mock.patch('glue.datafind.GWDataFindHTTPConnection') as \
                mock_connection, \
                mock.patch('gwpy.io.datafind.num_channels', lambda x: 1), \
                mock.patch('gwpy.io.gwf.iter_channel_names',
                           lambda x: ['L1:LDAS-STRAIN']):
            mock_connection.return_value = connection
            assert io_datafind.find_frametype('L1:LDAS-STRAIN',
                                              allow_tape=True) == 'HW100916'
            assert io_datafind.find_frametype('L1:LDAS-STRAIN',
                                              return_all=True) == ['HW100916']

            # test missing channel raises sensible error
            with pytest.raises(ValueError) as exc:
                io_datafind.find_frametype('X1:TEST', allow_tape=True)
            assert str(exc.value) == (
                'Cannot locate the following channel(s) '
                'in any known frametype:\n    X1:TEST')

            # test malformed channel name raises sensible error
            with pytest.raises(ValueError) as exc:
                io_datafind.find_frametype('bad channel name')
            assert str(exc.value) == ('Cannot parse interferometer prefix '
                                      'from channel name \'bad channel name\','
                                      ' cannot proceed with find()')

            # test trend sorting ends up with an error
            with pytest.raises(ValueError) as exc:
                io_datafind.find_frametype('X1:TEST.rms,s-trend',
                                           allow_tape=True)
            with pytest.raises(ValueError):
                io_datafind.find_frametype('X1:TEST.rms,m-trend',
                                           allow_tape=True)
示例#3
0
文件: test_cli.py 项目: stefco/gwpy
    def test_makePlot(self):
        product, parser = self.test_init_cli()
        args = parser.parse_args(self.TEST_ARGS + ['--out', TEMP_PLOT_FILE])
        product.post_arg(args)
        args.interactive = True

        random.seed(0)
        xts = TimeSeries(random.rand(10240), t0=1000000000,
                         sample_rate=1024, name='X1:TEST-CHANNEL')
        yts = TimeSeries(random.rand(10240), t0=1000000000,
                         sample_rate=1024, name='Y1:TEST-CHANNEL')
        nds_connection = mocks.nds2_connection(buffers=[
            mocks.nds2_buffer_from_timeseries(xts),
            mocks.nds2_buffer_from_timeseries(yts),
        ])

        with mock.patch('nds2.connection') as mock_connection, \
                mock.patch('nds2.buffer'):
            mock_connection.return_value = nds_connection

            try:
                product.makePlot(args, parser)
            finally:
                if os.path.isfile(args.out):
                    os.remove(args.out)

        assert product.is_interactive is True
示例#4
0
def query_dqsegdb(query_func, *args, **kwargs):
    """Mock a query to an aLIGO DQSEGDB database
    """
    try:
        with mock.patch('dqsegdb.apicalls.dqsegdbQueryTimes',
                        mocks.dqsegdb_query_times(QUERY_RESULT)), \
             mock.patch('dqsegdb.apicalls.dqsegdbCascadedQuery',
                        mocks.dqsegdb_cascaded_query(QUERY_RESULT)):
            return query_func(*args, **kwargs)
    except ImportError as e:
        pytest.skip(str(e))
示例#5
0
文件: test_io.py 项目: stefco/gwpy
 def test_find_best_frametype(self, connection):
     """Test :func:`gwpy.io.datafind.find_best_frametype
     """
     with mock.patch('glue.datafind.GWDataFindHTTPConnection') as \
             mock_connection, \
             mock.patch('gwpy.io.datafind.num_channels', lambda x: 1), \
             mock.patch('gwpy.io.gwf.iter_channel_names',
                        lambda x: ['L1:LDAS-STRAIN']):
         mock_connection.return_value = connection
         assert io_datafind.find_best_frametype(
             'L1:LDAS-STRAIN', 968654552, 968654553) == 'HW100916'
示例#6
0
文件: test_io.py 项目: stefco/gwpy
 def test_connect(self, connection):
     """Test :func:`gwpy.io.datafind.connect`
     """
     with mock.patch('glue.datafind.GWDataFindHTTPConnection',
                     connection), \
             mock.patch('glue.datafind.GWDataFindHTTPSConnection',
                        connection), \
             mock.patch('glue.datafind.find_credential',
                        mocks.mock_find_credential):
         io_datafind.connect()  # HTTP
         io_datafind.connect('host', 443)  # HTTPS
示例#7
0
def query_segdb(query_func, *args, **kwargs):
    """Mock a query to an S6-style DB2 database
    """
    try:
        with mock.patch('glue.segmentdb.segmentdb_utils.setup_database'), \
             mock.patch('glue.segmentdb.segmentdb_utils.expand_version_number',
                        mocks.segdb_expand_version_number(1, 4)), \
             mock.patch('glue.segmentdb.segmentdb_utils.query_segments',
                        mocks.segdb_query_segments(QUERY_RESULT)):
            return query_func(*args, **kwargs)
    except ImportError as e:
        pytest.skip(str(e))
示例#8
0
    def test_query(self, name):
        # build fake CIS response
        channelinfo = {'X1:TEST-CHANNEL': {
            'name': 'X1:TEST-CHANNEL',
            'units': 'm',
            'datarate': 16384,
            'datatype': 4,
            'source': 'X1MODEL',
            'displayurl': 'https://cis.ligo.org/channel/123456',
        }}
        if name in channelinfo:
            results = [channelinfo[name]]
        else:
            results = []
        jsonresponse = json.dumps({'results': results})

        # mock response and test parsing
        with mock.patch('ligo.org.request') as mocked:
            mocked.return_value = jsonresponse
            if name == 'X1:TEST-CHANNEL':
                c = self.TEST_CLASS.query(name)
                assert c.name == 'X1:TEST-CHANNEL'
                assert c.unit == units.m
                assert c.sample_rate == 16384 * units.Hz
                assert c.dtype == numpy.dtype('float32')
                assert c.model == 'x1model'
                assert c.url == 'https://cis.ligo.org/channel/123456'
            else:
                with pytest.raises(ValueError) as exc:
                    c = self.TEST_CLASS.query(name)
                assert str(exc.value) == 'No channels found matching %r' % name
示例#9
0
文件: test_table.py 项目: stefco/gwpy
    def test_fetch_hacr(self):
        table = self.create(100, names=HACR_COLUMNS)
        try:
            from pymysql import connect
        except ImportError:
            mockee = 'gwpy.table.io.hacr.connect'
        else:
            mockee = 'pymysql.connect'
        with mock.patch(mockee) as mock_connect:
            mock_connect.return_value = mock_hacr_connection(
                table, 123, 456)

            # test simple query returns the full table
            t2 = self.TABLE.fetch('hacr', 'X1:TEST-CHANNEL', 123, 456)
            utils.assert_table_equal(table, t2)

            # test column selection works
            t2 = self.TABLE.fetch('hacr', 'X1:TEST-CHANNEL', 123, 456,
                                  columns=['gps_start', 'snr'])
            utils.assert_table_equal(table['gps_start', 'snr'], t2)

            # test column selection works
            t2 = self.TABLE.fetch('hacr', 'X1:TEST-CHANNEL', 123, 456,
                                  columns=['gps_start', 'snr'],
                                  selection='freq_central>500')
            utils.assert_table_equal(
                filter_table(table, 'freq_central>500')['gps_start', 'snr'],
                t2)
示例#10
0
    def test_populate(self):
        name = QUERY_FLAGS[0]
        flag = self.TEST_CLASS(name, known=QUERY_RESULT[name].known)

        with mock.patch('dqsegdb.apicalls.dqsegdbQueryTimes',
                        mocks.dqsegdb_query_times(QUERY_RESULT)):
            flag.populate()
        utils.assert_flag_equal(flag, QUERY_RESULTC[name])
示例#11
0
    def test_connect(self):
        """Test :func:`gwpy.io.connect`
        """
        import nds2
        nds_connection = mocks.nds2_connection(host='nds.test.gwpy')
        with mock.patch('nds2.connection') as mock_connection:
            mock_connection.return_value = nds_connection
            conn = io_nds2.connect('nds.test.gwpy')
            assert conn.get_host() == 'nds.test.gwpy'
            assert conn.get_port() == 31200

        nds_connection = mocks.nds2_connection(host='nds2.test.gwpy',
                                               port=8088)
        with mock.patch('nds2.connection') as mock_connection:
            mock_connection.return_value = nds_connection
            conn = io_nds2.connect('nds2.test.gwpy')
            assert conn.get_host() == 'nds2.test.gwpy'
            assert conn.get_port() == 8088
示例#12
0
文件: test_io.py 项目: stefco/gwpy
 def test_iter_channel_names(self):
     # maybe need something better?
     from types import GeneratorType
     names = io_gwf.iter_channel_names(TEST_GWF_FILE)
     assert isinstance(names, GeneratorType)
     assert list(names) == TEST_CHANNELS
     with mock.patch('gwpy.utils.shell.call', mock_call):
         names = io_gwf.iter_channel_names(TEST_GWF_FILE)
         assert isinstance(names, GeneratorType)
         assert list(names) == TEST_CHANNELS
示例#13
0
文件: test_io.py 项目: jumbokh/gwpy
 def test_iter_channel_names(self):
     # maybe need something better?
     from types import GeneratorType
     names = io_gwf.iter_channel_names(TEST_GWF_FILE)
     assert isinstance(names, GeneratorType)
     assert list(names) == TEST_CHANNELS
     with mock.patch('gwpy.utils.shell.call', mock_call):
         names = io_gwf.iter_channel_names(TEST_GWF_FILE)
         assert isinstance(names, GeneratorType)
         assert list(names) == TEST_CHANNELS
示例#14
0
    def test_query_nds2(self, name):
        # mock NDS2 query
        ndsb = mocks.nds2_buffer('X1:TEST-CHANNEL', [], 0, 64, 'm')
        if ndsb.name == name:
            buffers = [ndsb]
        else:
            buffers = []
        conn = mocks.nds2_connection(buffers=buffers)
        with mock.patch('nds2.connection') as ndsc, \
                mock.patch('nds2.buffer', ndsb):
            ndsc.return_value = conn

            # test query_nds2
            if buffers:
                c = self.TEST_CLASS.query_nds2(name, host='test')
                assert c.name == name
                assert c.sample_rate == 64 * units.Hz
                assert c.unit == units.m
                assert c.dtype == numpy.dtype('float32')
                assert c.type == 'raw'
            else:
                with pytest.raises(ValueError):
                    c = self.TEST_CLASS.query_nds2(name, host='test')
示例#15
0
    def test_query_nds2(self, name):
        # mock NDS2 query
        ndsb = mocks.nds2_buffer('X1:TEST-CHANNEL', [], 0, 64, 'm')
        if ndsb.name == name:
            buffers = [ndsb]
        else:
            buffers = []
        conn = mocks.nds2_connection(buffers=buffers)
        with mock.patch('nds2.connection') as ndsc, \
                mock.patch('nds2.buffer', ndsb):
            ndsc.return_value = conn

            # test query_nds2
            if buffers:
                c = self.TEST_CLASS.query_nds2(name, host='test')
                assert c.name == name
                assert c.sample_rate == 64 * units.Hz
                assert c.unit == units.m
                assert c.dtype == numpy.dtype('float32')
                assert c.type == 'raw'
            else:
                with pytest.raises(ValueError):
                    c = self.TEST_CLASS.query_nds2(name, host='test')
示例#16
0
    def test_populate(self):
        def fake():
            return self.TEST_CLASS({
                x: self.ENTRY_CLASS(name=x, known=y.known)
                for x, y in QUERY_RESULT.items()
            })

        # build fake veto definer file
        vdf = fake()
        vdf2 = fake()
        vdf3 = fake()

        flag = QUERY_FLAGS[0]
        vdf2[flag].padding = (-1, 1)

        span = SegmentList([Segment(0, 2)])

        # and populate using a mocked query
        with mock.patch('dqsegdb.apicalls.dqsegdbQueryTimes',
                        mocks.dqsegdb_query_times(QUERY_RESULT)):
            vdf.populate()
            vdf2.populate()
            vdf3.populate(segments=span)

            # test warnings on bad entries
            vdf['TEST'] = self.ENTRY_CLASS('X1:BLAHBLAHBLAH:1', known=[(0, 1)])
            with pytest.warns(UserWarning) as record:
                vdf.populate(on_error='warn')
                vdf.populate(on_error='ignore')
            assert len(record) == 1
            vdf.pop('TEST')

            with pytest.raises(ValueError):
                vdf.populate(on_error='blah')

        # check basic populate worked
        utils.assert_dict_equal(vdf, QUERY_RESULTC, utils.assert_flag_equal)

        # check padded populate worked
        utils.assert_flag_equal(vdf2[flag], QUERY_RESULTC[flag].pad(-1, 1))

        # check segment-restricted populate worked
        for flag in vdf3:
            utils.assert_segmentlist_equal(vdf3[flag].known,
                                           QUERY_RESULTC[flag].known & span)
            utils.assert_segmentlist_equal(vdf3[flag].active,
                                           QUERY_RESULTC[flag].active & span)
示例#17
0
    def test_populate(self):
        def fake():
            return self.TEST_CLASS({
                x: self.ENTRY_CLASS(name=x, known=y.known) for
                x, y in QUERY_RESULT.items()})

        # build fake veto definer file
        vdf = fake()
        vdf2 = fake()
        vdf3 = fake()

        flag = QUERY_FLAGS[0]
        vdf2[flag].padding = (-1, 1)

        span = SegmentList([Segment(0, 2)])

        # and populate using a mocked query
        with mock.patch('dqsegdb.apicalls.dqsegdbQueryTimes',
                        mocks.dqsegdb_query_times(QUERY_RESULT)):
            vdf.populate()
            vdf2.populate()
            vdf3.populate(segments=span)

            # test warnings on bad entries
            vdf['TEST'] = self.ENTRY_CLASS('X1:BLAHBLAHBLAH:1', known=[(0, 1)])
            with pytest.warns(UserWarning) as record:
                vdf.populate(on_error='warn')
                vdf.populate(on_error='ignore')
            assert len(record) == 1
            vdf.pop('TEST')

            with pytest.raises(ValueError):
                vdf.populate(on_error='blah')

        # check basic populate worked
        utils.assert_dict_equal(vdf, QUERY_RESULTC, utils.assert_flag_equal)

        # check padded populate worked
        utils.assert_flag_equal(vdf2[flag], QUERY_RESULTC[flag].pad(-1, 1))

        # check segment-restricted populate worked
        for flag in vdf3:
            utils.assert_segmentlist_equal(
                vdf3[flag].known, QUERY_RESULTC[flag].known & span)
            utils.assert_segmentlist_equal(
                vdf3[flag].active, QUERY_RESULTC[flag].active & span)
示例#18
0
    def test_query_nds2_availability(self):
        # mock NDS2 query
        ndsb = mocks.nds2_buffer(self.NAMES[0], [], 0, 64, 'm')
        availability = [
            mocks.nds2_availability(self.NAMES[0], [(0, 10), (20, 30)]),
        ]
        conn = mocks.nds2_connection(buffers=[ndsb])
        conn.get_availability.return_value = availability
        with mock.patch('nds2.connection') as ndsc:
            ndsc.return_value = conn

            avail = self.TEST_CLASS.query_nds2_availability(
                [self.NAMES[0]], 0, 30, host='test')

            assert isinstance(avail, SegmentListDict)
            utils.assert_segmentlist_equal(avail[self.NAMES[0]],
                                           [(0, 10), (20, 30)])
示例#19
0
    def test_query_nds2(self):
        # mock NDS2 query
        buffers = []
        for name, fs in zip(self.NAMES[:-1], self.SAMPLE_RATES[:-1]):
            buffers.append(mocks.nds2_buffer(name, [], 0, fs, 'm'))
        conn = mocks.nds2_connection(buffers=buffers)
        with mock.patch('nds2.connection') as ndsc:
            ndsc.return_value = conn

            # test query_nds2
            c = self.TEST_CLASS.query_nds2(self.NAMES[:-1], host='test')
            assert len(c) == len(self.NAMES) - 1
            assert c[0].name == self.NAMES[0]
            assert c[0].sample_rate == self.SAMPLE_RATES[0] * units.Hz

            # check errors
            assert len(
                self.TEST_CLASS.query_nds2([self.NAMES[-1]], host='test')) == 0
示例#20
0
    def test_query_nds2(self):
        # mock NDS2 query
        buffers = []
        for name, fs in zip(self.NAMES[:-1], self.SAMPLE_RATES[:-1]):
            buffers.append(mocks.nds2_buffer(name, [], 0, fs, 'm'))
        conn = mocks.nds2_connection(buffers=buffers)
        with mock.patch('nds2.connection') as ndsc:
            ndsc.return_value = conn

            # test query_nds2
            c = self.TEST_CLASS.query_nds2(self.NAMES[:-1], host='test')
            assert len(c) == len(self.NAMES) - 1
            assert c[0].name == self.NAMES[0]
            assert c[0].sample_rate == self.SAMPLE_RATES[0] * units.Hz

            # check errors
            assert len(
                self.TEST_CLASS.query_nds2([self.NAMES[-1]], host='test')) == 0
示例#21
0
    def test_query_nds2_availability(self):
        # mock NDS2 query
        ndsb = mocks.nds2_buffer(self.NAMES[0], [], 0, 64, 'm')
        availability = [
            mocks.nds2_availability(self.NAMES[0], [(0, 10), (20, 30)]),
        ]
        conn = mocks.nds2_connection(buffers=[ndsb])
        conn.get_availability.return_value = availability
        with mock.patch('nds2.connection') as ndsc:
            ndsc.return_value = conn

            avail = self.TEST_CLASS.query_nds2_availability([self.NAMES[0]],
                                                            0,
                                                            30,
                                                            host='test')

            assert isinstance(avail, SegmentListDict)
            utils.assert_segmentlist_equal(avail[self.NAMES[0]], [(0, 10),
                                                                  (20, 30)])