Exemple #1
0
    def _test_read_cache(self, format, extension=None, exclude=['channel']):
        if extension is None:
            extension = format
        # make array
        a = self.create(name='test', t0=0, sample_rate=self.data.shape[0])
        exta = '-%d-%d.%s' % (a.span[0], a.span[1], extension)

        # write it to a file, so we can read it again later
        with tempfile.NamedTemporaryFile(prefix='tmp-', suffix=exta,
                                         delete=False) as f1:
            a.write(f1.name)

        # test reading it from the cache
        cache = Cache.from_urls([f1.name])
        b = self.TEST_CLASS.read(cache, a.name)
        self.assertArraysEqual(a, b, exclude=exclude)

        # write a cache file and read that
        try:
            with tempfile.NamedTemporaryFile(suffix='.lcf', delete=False) as f:
                cache.tofile(f)
                b = self.TEST_CLASS.read(f.name, a.name)
                self.assertArraysEqual(a, b, exclude=exclude)
                b = self.TEST_CLASS.read(open(f.name), a.name)
                self.assertArraysEqual(a, b, exclude=exclude)
        finally:
            if os.path.isfile(f.name):
                os.remove(f.name)

        # create second array with a gap
        b = self.create(name='test', t0=a.xspan[1]+1, dt=a.dt)
        extb = '-%d-%d.%s' % (b.span[0], b.span[1], extension)
        try:
            with tempfile.NamedTemporaryFile(prefix='tmp-', suffix=extb,
                                             delete=False) as f2:
                # write tmp file
                b.write(f2.name)
                # make cache of file names
                cache = Cache.from_urls([f1.name, f2.name], coltype=int)
                # assert gap='raise' actually raises by default
                self.assertRaises(ValueError, self.TEST_CLASS.read,
                                  cache, a.name)
                # read from cache
                ts = self.TEST_CLASS.read(cache, a.name, gap='pad', pad=0)
                nptest.assert_array_equal(
                    ts.value,
                    numpy.concatenate((a.value,
                                       numpy.zeros(int(a.sample_rate.value)),
                                       b.value)))
                # read with multi-processing
                ts2 = self.TEST_CLASS.read(cache, a.name, nproc=2,
                                           gap='pad', pad=0)
                self.assertArraysEqual(ts, ts2)
        finally:
            # clean up
            for f in (f1, f2):
                if os.path.exists(f.name):
                    os.remove(f.name)
Exemple #2
0
    def _test_frame_read_format(self, format):
        # test with specific format
        try:
            self.frame_read(format=format)
        except ImportError as e:
            self.skipTest(str(e))
        else:
            # test again with no format argument
            # but we need to move other readers out of the way first
            try:
                read_ = get_reader('gwf', TimeSeries)
            except Exception:
                pass
            else:
                register_reader('gwf', TimeSeries,
                                get_reader(format, TimeSeries),
                                force=True)
                try:
                    self.frame_read()
                finally:
                    register_reader('gwf', TimeSeries, read_, force=True)
            # test empty Cache()
            self.assertRaises(ValueError, self.TEST_CLASS.read, Cache(),
                              self.channel, format=format)

            # test cache method with `nproc=2`
            c = Cache.from_urls([TEST_GWF_FILE])
            ts = self.TEST_CLASS.read(c, self.channel, nproc=2, format=format)
Exemple #3
0
    def _test_frame_read_format(self, format):
        # test with specific format
        try:
            self.frame_read(format=format)
        except ImportError as e:
            self.skipTest(str(e))
        else:
            # test again with no format argument
            # but we need to move other readers out of the way first
            try:
                read_ = get_reader('gwf', TimeSeries)
            except Exception:
                pass
            else:
                register_reader('gwf',
                                TimeSeries,
                                get_reader(format, TimeSeries),
                                force=True)
                try:
                    self.frame_read()
                finally:
                    register_reader('gwf', TimeSeries, read_, force=True)
            # test empty Cache()
            self.assertRaises(ValueError,
                              self.TEST_CLASS.read,
                              Cache(),
                              self.channel,
                              format=format)

            # test cache method with `nproc=2`
            c = Cache.from_urls([TEST_GWF_FILE])
            ts = self.TEST_CLASS.read(c, self.channel, nproc=2, format=format)
Exemple #4
0
 def test_frame_read_cache(self):
     try:
         a = self.TEST_CLASS.read(TEST_GWF_FILE, self.channel)
     except Exception as e:  # don't care why this fails for this test
         self.skipTest(str(e))
     c = Cache.from_urls([TEST_GWF_FILE])
     with tempfile.NamedTemporaryFile(suffix='.lcf', delete=False) as f:
         c.tofile(f)
         f.delete = True
         b = self.TEST_CLASS.read(f.name, self.channel)
         self.assertArraysEqual(a, b)
         b = self.TEST_CLASS.read(f, self.channel)
         self.assertArraysEqual(a, b)
Exemple #5
0
 def test_frame_read_cache(self):
     try:
         a = self.TEST_CLASS.read(TEST_GWF_FILE, self.channel)
     except Exception as e:  # don't care why this fails for this test
         self.skipTest(str(e))
     c = Cache.from_urls([TEST_GWF_FILE])
     with tempfile.NamedTemporaryFile(suffix='.lcf', delete=False) as f:
         c.tofile(f)
         f.delete = True
         b = self.TEST_CLASS.read(f.name, self.channel)
         self.assertArraysEqual(a, b)
         b = self.TEST_CLASS.read(f, self.channel)
         self.assertArraysEqual(a, b)