Exemple #1
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 #2
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 #3
0
 def make_cache():
     segs = SegmentList()
     cache = Cache()
     for seg in [(0, 1), (1, 2), (4, 5)]:
         d = seg[1] - seg[0]
         f = 'A-B-%d-%d.tmp' % (seg[0], d)
         cache.append(CacheEntry.from_T050017(f))
         segs.append(Segment(*seg))
     return cache, segs
Exemple #4
0
 def make_cache():
     segs = SegmentList()
     cache = Cache()
     for seg in [(0, 1), (1, 2), (4, 5)]:
         d = seg[1] - seg[0]
         f = 'A-B-%d-%d.tmp' % (seg[0], d)
         cache.append(CacheEntry.from_T050017(f))
         segs.append(Segment(*seg))
     return cache, segs
Exemple #5
0
 def make_cache():
     segs = SegmentList()
     cache = Cache()
     for seg in [(0, 1), (1, 2), (4, 5)]:
         d = seg[1] - seg[0]
         _, f = tempfile.mkstemp(prefix='A-',
                                 suffix='-%d-%d.tmp' % (seg[0], d))
         cache.append(CacheEntry.from_T050017(f))
         segs.append(Segment(*seg))
     return cache, segs
Exemple #6
0
 def make_cache():
     segs = SegmentList()
     cache = Cache()
     for seg in [(0, 1), (1, 2), (4, 5)]:
         d = seg[1] - seg[0]
         _, f = tempfile.mkstemp(prefix='A-',
                                 suffix='-%d-%d.tmp' % (seg[0], d))
         cache.append(CacheEntry.from_T050017(f))
         segs.append(Segment(*seg))
     return cache, segs
Exemple #7
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 #8
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 #9
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 #10
0
def test_io_identify(cls, extensions, modes=['read', 'write']):
    for mode in modes:
        for ext in extensions:
            p = 'X-TEST_CACHE_ENTRY-0-1.%s' % ext
            c = Cache([CacheEntry.from_T050017(p)])
            for path in [p, [p], c[0], c]:
                formats = identify_format(mode, cls, path, None, [], {})
                if len(formats) == 0:
                    raise RuntimeError(
                        "No %s.%s method identified for "
                        "file-format %r in form %r" %
                        (cls.__name__, mode, ext, type(path).__name__))
                elif len(formats) != 1:
                    raise RuntimeError(
                        "Multiple %s.%s methods identified for "
                        "file-format %r in form %r" %
                        (cls.__name__, mode, ext, type(path).__name__))